]> Pileus Git - ~andy/csm213a-hw/blob - syc/src/com/example/sycapp/SerialData.java
Add Android app from Yue
[~andy/csm213a-hw] / syc / src / com / example / sycapp / SerialData.java
1 package com.example.sycapp;
2
3 import java.util.Date;
4 import java.util.Locale;
5 import java.text.SimpleDateFormat;
6
7 public class SerialData {
8         public static final short   HEADER  =0x1234;
9         public static final int     sync_int16Cnt=3;
10         public static final int     sync_int32Cnt=3;
11         public static final int      evt_int16Cnt=5;
12         public static final int      evt_int32Cnt=4; 
13         
14         public static enum  MessageID {MSG_ID_INIT,
15                                                                    MSG_ID_SYNC,
16                                                                    MSG_ID_EVENT,
17                                                                    MSG_MAX_ID,};
18         public short msgID;                     //head
19         public short length;
20         public short cksum;
21         
22         public short valID;
23         public short devID=1;           //TODO!!ChangeBack!!!!!!
24         public long  worldT;
25         public int   wrdS;
26         public int   wrdN;
27         public long  startT;
28         public int   stS;
29         public int   stN;
30         public long  periodT;
31         public int   prS;
32         public int   prN;
33         
34         public int   msgSeq;
35         public long  receiveT;
36         public int   rcS;
37         public int   rcN;
38         
39         public short evtID;
40         public long  turnOnT;
41         public int   toS;
42         public int   toN;
43         public int   portId;
44         
45         // Constructor
46         SerialData(){
47                 
48         }
49         
50         SerialData(short[] st, int[] it, int portId){
51                 this.msgID = st[0];
52                 this.length = st[1];
53                 this.cksum = st[2];
54                 this.devID = st[3];
55                 this.evtID = st[4];
56                 this.wrdS  = it[0];
57                 this.wrdN  = it[1];
58                 this.toS   = it[2];
59                 this.toN   = it[3];
60                 this.portId = portId;
61         }
62         
63         
64         // Message Type
65         public void initMessage(short vld, short did, long wrt, long stt, long prt){
66                 this.worldT = wrt;
67                 this.startT = stt;
68                 this.periodT= prt;
69                 
70                 
71                 this.msgID = (short) MessageID.MSG_ID_INIT.ordinal();
72                 this.length= 28;
73                 this.cksum = (short)((long)vld+(long)did+wrt+stt+prt);
74                 
75                 this.valID =  vld;
76                 this.devID =  did;
77                 this.wrdS   = toSeconds(worldT);
78                 this.wrdN   = toNanoSec(worldT);
79                 this.stS    = toSeconds(startT);
80                 this.stN    = toNanoSec(startT);
81                 this.prS    = toSeconds(periodT);
82                 this.prN    = toNanoSec(periodT);
83         }
84
85         public void syncMessage(int seq, long time){
86                 this.receiveT = time;
87                 
88                 this.msgID = (short) MessageID.MSG_ID_SYNC.ordinal();
89                 this.length = 12;
90                 this.cksum = (short)((long)seq+time);
91                 
92                 this.msgSeq  = seq;
93                 this.rcS     = toSeconds(receiveT);
94                 this.rcN     = toNanoSec(receiveT);
95                 
96         }
97         
98         public void evtMessage(short did, short eid, long wrt, long lt){
99                 this.worldT  = wrt;
100                 this.turnOnT = lt;
101                                 
102                                 
103                 this.msgID   = (short) MessageID.MSG_ID_EVENT.ordinal();
104                 this.length  = 20;
105                 this.cksum   = (short)((long)did+(long)eid+wrt+lt);
106                 
107                 this.devID   = did;
108                 this.evtID   = eid;
109                 this.wrdS    = toSeconds(wrt);
110                 this.wrdN    = toNanoSec(wrt);
111                 this.toS     = toSeconds(turnOnT);
112                 this.toN     = toNanoSec(turnOnT);
113         }
114         
115         private int toSeconds(long t){
116                 return (int)(long)(t/1000);
117         }
118         private int toNanoSec(long t){
119                 int sc = toSeconds(t);
120                 return (int)(long)((t-sc*1000)*1000000);
121         }
122
123         
124         // Message to Bytes
125         public byte[] initToBytes(){
126                 byte[] dataInByte = new byte[36];
127                 int i[] = new int[1];
128                 i[0] = 0;
129                 if (this.msgID == (short)MessageID.MSG_ID_INIT.ordinal()){
130                         intoByteArray(dataInByte, HEADER ,i);
131                         intoByteArray(dataInByte, msgID  ,i);
132                         intoByteArray(dataInByte, length ,i);
133                         intoByteArray(dataInByte, cksum  ,i);
134                         
135                         intoByteArray(dataInByte, valID  ,i);
136                         intoByteArray(dataInByte, devID  ,i);
137                         
138                         intoByteArray(dataInByte, wrdS   ,i);
139                         intoByteArray(dataInByte, wrdN   ,i);
140                         intoByteArray(dataInByte, stS    ,i);
141                         intoByteArray(dataInByte, stN    ,i);
142                         intoByteArray(dataInByte, prS    ,i);
143                         intoByteArray(dataInByte, prN    ,i);
144                         
145                 }
146                 return dataInByte;
147         }
148         
149         public byte[] evtToBytes(){
150                 byte[] dataInByte = new byte[28];
151                 int i[] = new int[1];
152                 i[0] = 0;
153                 if (this.msgID == (short)MessageID.MSG_ID_EVENT.ordinal()){
154                         intoByteArray(dataInByte, HEADER ,i);
155                         intoByteArray(dataInByte, msgID  ,i);
156                         intoByteArray(dataInByte, length ,i);
157                         intoByteArray(dataInByte, cksum  ,i);
158                         
159                         intoByteArray(dataInByte, devID  ,i);
160                         intoByteArray(dataInByte, evtID  ,i);
161                         
162                         intoByteArray(dataInByte, wrdS   ,i);
163                         intoByteArray(dataInByte, wrdN   ,i);
164                         intoByteArray(dataInByte, toS    ,i);
165                         intoByteArray(dataInByte, toN    ,i);
166                 }
167                 return dataInByte;
168         }
169         
170         public byte[] syncToBytes(){
171                 byte[] dataInByte = new byte[28];
172                 int i[] = new int[1];
173                 i[0] = 0;
174                 if (this.msgID == (short)MessageID.MSG_ID_SYNC.ordinal()){
175                         intoByteArray(dataInByte, HEADER ,i);
176                         intoByteArray(dataInByte, msgID  ,i);
177                         intoByteArray(dataInByte, length ,i);
178                         intoByteArray(dataInByte, cksum  ,i);
179                         
180                         intoByteArray(dataInByte, msgSeq ,i);
181                         intoByteArray(dataInByte, rcS    ,i);
182                         intoByteArray(dataInByte, rcN    ,i);
183                         
184                 }
185                 return dataInByte;
186         }
187         
188         private void intoByteArray(byte[] bytes, short st, int[] index){
189                 for(int i=0; i<2; i++, index[0]++){
190                         bytes[index[0]]=byteNo(st,i+1);
191                 }
192         }
193         private void intoByteArray(byte[] bytes, int it, int[] index){
194                 for(int i =0; i<4; i++, index[0]++){
195                         bytes[index[0]]=byteNo(it,i+1);
196                 }
197         }
198         
199         //Original
200         /*private byte byteNo(Short st, int i){
201                 return (byte)(st>>(8*(2-i))&0xFF);
202         }
203         private byte byteNo(Integer in, int i){
204                 return (byte)(in>>(8*(4-i))&0xFF);
205         }*/
206         
207         private byte byteNo(Short st, int i){
208                 return (byte)(st>>(8*(i-1))&0xFF);
209         }
210         private byte byteNo(Integer in, int i){
211                 return (byte)(in>>(8*(i-1))&0xFF);
212         }
213         
214         
215         // Time Stamp Related
216         public String timeStamp(int seconds, int nanosec){
217
218                 long currentTimeM = (long)(((long)(seconds)*(long)(1000))+((long)(nanosec)/(long)(1000000)));
219                 if (((int)((nanosec-((int)(nanosec/1000000000))*1000000000)/10))<0){
220                         int i=0;
221                         int temp = (int)(nanosec/1000000000);
222                         Util.debug("[S#7]"+Long.toString(nanosec));
223                         Util.debug("[S#7]"+Integer.toString(temp));
224                         temp = ((int)(nanosec/1000000000))*1000000000;
225                         Util.debug("[S#7]"+Integer.toString(temp));
226                         temp = (nanosec-((int)(nanosec/1000000000))*1000000000)/10;
227                         Util.debug("[S#7]"+Integer.toString(temp));
228                         i++;
229                 }
230                 return  "" 
231                                 + new SimpleDateFormat("yyyy-MM-dd", Locale.US).
232                                   format(new Date(currentTimeM)).toString()
233                                 + "T"
234                                 + new SimpleDateFormat("HH:mm:ss", Locale.US).
235                                   format(new Date(currentTimeM)).toString()
236                                 + "." + Integer.toString((int)((nanosec-((int)(nanosec/1000000000))*1000000000)/10))
237                                 + "Z";
238         }
239         
240         public String timeStamp(){
241                 int seconds = wrdS;
242                 int nanosec = wrdN;
243                 long currentTimeM = (long)(((long)(seconds)*(long)(1000))+((long)(nanosec)/(long)(1000000)));
244                 String decimal = Integer.toString((int)((nanosec-(int)((long)(nanosec/1000000000))*1000000000)/10));
245                 while(decimal.length()<8){
246                         decimal = "0" + decimal;
247                 }
248                 return  "" 
249                                 + new SimpleDateFormat("yyyy-MM-dd", Locale.US).
250                                   format(new Date(currentTimeM)).toString()
251                                 + "T"
252                                 + new SimpleDateFormat("HH:mm:ss", Locale.US).
253                                   format(new Date(currentTimeM)).toString()
254                                 + "." + decimal
255                                 + "Z";
256         }
257         
258         public  String timeDisplay(){
259                 int seconds = wrdS;
260                 int nanosec = wrdN;
261                 long currentTimeM = (long)(((long)(seconds)*(long)(1000))+((long)(nanosec)/(long)(1000000)));
262                 String decimal = Integer.toString((int)((nanosec-(int)((long)(nanosec/1000000000))*1000000000)/10));
263                 while(decimal.length()<8){
264                         decimal = "0" + decimal;
265                 }
266                 return  "" 
267                                 + new SimpleDateFormat("MM-dd", Locale.US).
268                                   format(new Date(currentTimeM)).toString()
269                                 + "^"
270                                 + new SimpleDateFormat("HH:mm:ss", Locale.US).
271                                   format(new Date(currentTimeM)).toString()
272                                 + "." + decimal
273                                 + "Z";
274         }
275 }