1 package com.example.sycapp;
4 import java.util.Locale;
5 import java.text.SimpleDateFormat;
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;
14 public static enum MessageID {MSG_ID_INIT,
18 public short msgID; //head
23 public short devID=1; //TODO!!ChangeBack!!!!!!
50 SerialData(short[] st, int[] it, int portId){
65 public void initMessage(short vld, short did, long wrt, long stt, long prt){
71 this.msgID = (short) MessageID.MSG_ID_INIT.ordinal();
73 this.cksum = (short)((long)vld+(long)did+wrt+stt+prt);
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);
85 public void syncMessage(int seq, long time){
88 this.msgID = (short) MessageID.MSG_ID_SYNC.ordinal();
90 this.cksum = (short)((long)seq+time);
93 this.rcS = toSeconds(receiveT);
94 this.rcN = toNanoSec(receiveT);
98 public void evtMessage(short did, short eid, long wrt, long lt){
103 this.msgID = (short) MessageID.MSG_ID_EVENT.ordinal();
105 this.cksum = (short)((long)did+(long)eid+wrt+lt);
109 this.wrdS = toSeconds(wrt);
110 this.wrdN = toNanoSec(wrt);
111 this.toS = toSeconds(turnOnT);
112 this.toN = toNanoSec(turnOnT);
115 private int toSeconds(long t){
116 return (int)(long)(t/1000);
118 private int toNanoSec(long t){
119 int sc = toSeconds(t);
120 return (int)(long)((t-sc*1000)*1000000);
125 public byte[] initToBytes(){
126 byte[] dataInByte = new byte[36];
127 int i[] = new int[1];
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);
135 intoByteArray(dataInByte, valID ,i);
136 intoByteArray(dataInByte, devID ,i);
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);
149 public byte[] evtToBytes(){
150 byte[] dataInByte = new byte[28];
151 int i[] = new int[1];
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);
159 intoByteArray(dataInByte, devID ,i);
160 intoByteArray(dataInByte, evtID ,i);
162 intoByteArray(dataInByte, wrdS ,i);
163 intoByteArray(dataInByte, wrdN ,i);
164 intoByteArray(dataInByte, toS ,i);
165 intoByteArray(dataInByte, toN ,i);
170 public byte[] syncToBytes(){
171 byte[] dataInByte = new byte[28];
172 int i[] = new int[1];
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);
180 intoByteArray(dataInByte, msgSeq ,i);
181 intoByteArray(dataInByte, rcS ,i);
182 intoByteArray(dataInByte, rcN ,i);
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);
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);
200 /*private byte byteNo(Short st, int i){
201 return (byte)(st>>(8*(2-i))&0xFF);
203 private byte byteNo(Integer in, int i){
204 return (byte)(in>>(8*(4-i))&0xFF);
207 private byte byteNo(Short st, int i){
208 return (byte)(st>>(8*(i-1))&0xFF);
210 private byte byteNo(Integer in, int i){
211 return (byte)(in>>(8*(i-1))&0xFF);
215 // Time Stamp Related
216 public String timeStamp(int seconds, int nanosec){
218 long currentTimeM = (long)(((long)(seconds)*(long)(1000))+((long)(nanosec)/(long)(1000000)));
219 if (((int)((nanosec-((int)(nanosec/1000000000))*1000000000)/10))<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));
231 + new SimpleDateFormat("yyyy-MM-dd", Locale.US).
232 format(new Date(currentTimeM)).toString()
234 + new SimpleDateFormat("HH:mm:ss", Locale.US).
235 format(new Date(currentTimeM)).toString()
236 + "." + Integer.toString((int)((nanosec-((int)(nanosec/1000000000))*1000000000)/10))
240 public String timeStamp(){
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;
249 + new SimpleDateFormat("yyyy-MM-dd", Locale.US).
250 format(new Date(currentTimeM)).toString()
252 + new SimpleDateFormat("HH:mm:ss", Locale.US).
253 format(new Date(currentTimeM)).toString()
258 public String timeDisplay(){
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;
267 + new SimpleDateFormat("MM-dd", Locale.US).
268 format(new Date(currentTimeM)).toString()
270 + new SimpleDateFormat("HH:mm:ss", Locale.US).
271 format(new Date(currentTimeM)).toString()