--- /dev/null
+package com.example.sycapp;
+
+import java.util.Date;
+import java.util.Locale;
+import java.text.SimpleDateFormat;
+
+public class SerialData {
+ public static final short HEADER =0x1234;
+ public static final int sync_int16Cnt=3;
+ public static final int sync_int32Cnt=3;
+ public static final int evt_int16Cnt=5;
+ public static final int evt_int32Cnt=4;
+
+ public static enum MessageID {MSG_ID_INIT,
+ MSG_ID_SYNC,
+ MSG_ID_EVENT,
+ MSG_MAX_ID,};
+ public short msgID; //head
+ public short length;
+ public short cksum;
+
+ public short valID;
+ public short devID=1; //TODO!!ChangeBack!!!!!!
+ public long worldT;
+ public int wrdS;
+ public int wrdN;
+ public long startT;
+ public int stS;
+ public int stN;
+ public long periodT;
+ public int prS;
+ public int prN;
+
+ public int msgSeq;
+ public long receiveT;
+ public int rcS;
+ public int rcN;
+
+ public short evtID;
+ public long turnOnT;
+ public int toS;
+ public int toN;
+ public int portId;
+
+ // Constructor
+ SerialData(){
+
+ }
+
+ SerialData(short[] st, int[] it, int portId){
+ this.msgID = st[0];
+ this.length = st[1];
+ this.cksum = st[2];
+ this.devID = st[3];
+ this.evtID = st[4];
+ this.wrdS = it[0];
+ this.wrdN = it[1];
+ this.toS = it[2];
+ this.toN = it[3];
+ this.portId = portId;
+ }
+
+
+ // Message Type
+ public void initMessage(short vld, short did, long wrt, long stt, long prt){
+ this.worldT = wrt;
+ this.startT = stt;
+ this.periodT= prt;
+
+
+ this.msgID = (short) MessageID.MSG_ID_INIT.ordinal();
+ this.length= 28;
+ this.cksum = (short)((long)vld+(long)did+wrt+stt+prt);
+
+ this.valID = vld;
+ this.devID = did;
+ this.wrdS = toSeconds(worldT);
+ this.wrdN = toNanoSec(worldT);
+ this.stS = toSeconds(startT);
+ this.stN = toNanoSec(startT);
+ this.prS = toSeconds(periodT);
+ this.prN = toNanoSec(periodT);
+ }
+
+ public void syncMessage(int seq, long time){
+ this.receiveT = time;
+
+ this.msgID = (short) MessageID.MSG_ID_SYNC.ordinal();
+ this.length = 12;
+ this.cksum = (short)((long)seq+time);
+
+ this.msgSeq = seq;
+ this.rcS = toSeconds(receiveT);
+ this.rcN = toNanoSec(receiveT);
+
+ }
+
+ public void evtMessage(short did, short eid, long wrt, long lt){
+ this.worldT = wrt;
+ this.turnOnT = lt;
+
+
+ this.msgID = (short) MessageID.MSG_ID_EVENT.ordinal();
+ this.length = 20;
+ this.cksum = (short)((long)did+(long)eid+wrt+lt);
+
+ this.devID = did;
+ this.evtID = eid;
+ this.wrdS = toSeconds(wrt);
+ this.wrdN = toNanoSec(wrt);
+ this.toS = toSeconds(turnOnT);
+ this.toN = toNanoSec(turnOnT);
+ }
+
+ private int toSeconds(long t){
+ return (int)(long)(t/1000);
+ }
+ private int toNanoSec(long t){
+ int sc = toSeconds(t);
+ return (int)(long)((t-sc*1000)*1000000);
+ }
+
+
+ // Message to Bytes
+ public byte[] initToBytes(){
+ byte[] dataInByte = new byte[36];
+ int i[] = new int[1];
+ i[0] = 0;
+ if (this.msgID == (short)MessageID.MSG_ID_INIT.ordinal()){
+ intoByteArray(dataInByte, HEADER ,i);
+ intoByteArray(dataInByte, msgID ,i);
+ intoByteArray(dataInByte, length ,i);
+ intoByteArray(dataInByte, cksum ,i);
+
+ intoByteArray(dataInByte, valID ,i);
+ intoByteArray(dataInByte, devID ,i);
+
+ intoByteArray(dataInByte, wrdS ,i);
+ intoByteArray(dataInByte, wrdN ,i);
+ intoByteArray(dataInByte, stS ,i);
+ intoByteArray(dataInByte, stN ,i);
+ intoByteArray(dataInByte, prS ,i);
+ intoByteArray(dataInByte, prN ,i);
+
+ }
+ return dataInByte;
+ }
+
+ public byte[] evtToBytes(){
+ byte[] dataInByte = new byte[28];
+ int i[] = new int[1];
+ i[0] = 0;
+ if (this.msgID == (short)MessageID.MSG_ID_EVENT.ordinal()){
+ intoByteArray(dataInByte, HEADER ,i);
+ intoByteArray(dataInByte, msgID ,i);
+ intoByteArray(dataInByte, length ,i);
+ intoByteArray(dataInByte, cksum ,i);
+
+ intoByteArray(dataInByte, devID ,i);
+ intoByteArray(dataInByte, evtID ,i);
+
+ intoByteArray(dataInByte, wrdS ,i);
+ intoByteArray(dataInByte, wrdN ,i);
+ intoByteArray(dataInByte, toS ,i);
+ intoByteArray(dataInByte, toN ,i);
+ }
+ return dataInByte;
+ }
+
+ public byte[] syncToBytes(){
+ byte[] dataInByte = new byte[28];
+ int i[] = new int[1];
+ i[0] = 0;
+ if (this.msgID == (short)MessageID.MSG_ID_SYNC.ordinal()){
+ intoByteArray(dataInByte, HEADER ,i);
+ intoByteArray(dataInByte, msgID ,i);
+ intoByteArray(dataInByte, length ,i);
+ intoByteArray(dataInByte, cksum ,i);
+
+ intoByteArray(dataInByte, msgSeq ,i);
+ intoByteArray(dataInByte, rcS ,i);
+ intoByteArray(dataInByte, rcN ,i);
+
+ }
+ return dataInByte;
+ }
+
+ private void intoByteArray(byte[] bytes, short st, int[] index){
+ for(int i=0; i<2; i++, index[0]++){
+ bytes[index[0]]=byteNo(st,i+1);
+ }
+ }
+ private void intoByteArray(byte[] bytes, int it, int[] index){
+ for(int i =0; i<4; i++, index[0]++){
+ bytes[index[0]]=byteNo(it,i+1);
+ }
+ }
+
+ //Original
+ /*private byte byteNo(Short st, int i){
+ return (byte)(st>>(8*(2-i))&0xFF);
+ }
+ private byte byteNo(Integer in, int i){
+ return (byte)(in>>(8*(4-i))&0xFF);
+ }*/
+
+ private byte byteNo(Short st, int i){
+ return (byte)(st>>(8*(i-1))&0xFF);
+ }
+ private byte byteNo(Integer in, int i){
+ return (byte)(in>>(8*(i-1))&0xFF);
+ }
+
+
+ // Time Stamp Related
+ public String timeStamp(int seconds, int nanosec){
+
+ long currentTimeM = (long)(((long)(seconds)*(long)(1000))+((long)(nanosec)/(long)(1000000)));
+ if (((int)((nanosec-((int)(nanosec/1000000000))*1000000000)/10))<0){
+ int i=0;
+ int temp = (int)(nanosec/1000000000);
+ Util.debug("[S#7]"+Long.toString(nanosec));
+ Util.debug("[S#7]"+Integer.toString(temp));
+ temp = ((int)(nanosec/1000000000))*1000000000;
+ Util.debug("[S#7]"+Integer.toString(temp));
+ temp = (nanosec-((int)(nanosec/1000000000))*1000000000)/10;
+ Util.debug("[S#7]"+Integer.toString(temp));
+ i++;
+ }
+ return ""
+ + new SimpleDateFormat("yyyy-MM-dd", Locale.US).
+ format(new Date(currentTimeM)).toString()
+ + "T"
+ + new SimpleDateFormat("HH:mm:ss", Locale.US).
+ format(new Date(currentTimeM)).toString()
+ + "." + Integer.toString((int)((nanosec-((int)(nanosec/1000000000))*1000000000)/10))
+ + "Z";
+ }
+
+ public String timeStamp(){
+ int seconds = wrdS;
+ int nanosec = wrdN;
+ long currentTimeM = (long)(((long)(seconds)*(long)(1000))+((long)(nanosec)/(long)(1000000)));
+ String decimal = Integer.toString((int)((nanosec-(int)((long)(nanosec/1000000000))*1000000000)/10));
+ while(decimal.length()<8){
+ decimal = "0" + decimal;
+ }
+ return ""
+ + new SimpleDateFormat("yyyy-MM-dd", Locale.US).
+ format(new Date(currentTimeM)).toString()
+ + "T"
+ + new SimpleDateFormat("HH:mm:ss", Locale.US).
+ format(new Date(currentTimeM)).toString()
+ + "." + decimal
+ + "Z";
+ }
+
+ public String timeDisplay(){
+ int seconds = wrdS;
+ int nanosec = wrdN;
+ long currentTimeM = (long)(((long)(seconds)*(long)(1000))+((long)(nanosec)/(long)(1000000)));
+ String decimal = Integer.toString((int)((nanosec-(int)((long)(nanosec/1000000000))*1000000000)/10));
+ while(decimal.length()<8){
+ decimal = "0" + decimal;
+ }
+ return ""
+ + new SimpleDateFormat("MM-dd", Locale.US).
+ format(new Date(currentTimeM)).toString()
+ + "^"
+ + new SimpleDateFormat("HH:mm:ss", Locale.US).
+ format(new Date(currentTimeM)).toString()
+ + "." + decimal
+ + "Z";
+ }
+}