TsiCpuPacket.java 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. package com.tsi.comm.server.protocol;
  2. import com.tsi.app.common.cpu.dto.TsiCvimAbnormal;
  3. import com.tsi.app.common.cpu.dto.TsiCvimControl;
  4. import com.tsi.app.common.cpu.dto.TsiCvimStatus;
  5. import com.tsi.app.common.cpu.enums.eLightsStatus;
  6. import com.tsi.app.common.cpu.enums.eLightsType;
  7. import com.tsi.app.common.cpu.enums.eOpCode;
  8. import com.tsi.app.common.cpu.enums.eTimeReliability;
  9. import com.tsi.app.common.utils.ByteUtils;
  10. import com.tsi.app.common.utils.CRC16Utils;
  11. import com.tsi.app.common.utils.HexString;
  12. import com.tsi.app.common.utils.TimeUtils;
  13. import com.tsi.app.common.xnet.NettyUtils;
  14. import com.tsi.comm.server.mongo.dto.TcsNodeStatus;
  15. import com.tsi.comm.server.repository.TsiNodeAddManager;
  16. import com.tsi.comm.server.vo.TsiNodeAddDetailVo;
  17. import com.tsi.comm.server.vo.TsiNodeAddVo;
  18. import com.tsi.comm.server.vo.TsiNodeVo;
  19. import io.netty.channel.Channel;
  20. import lombok.Getter;
  21. import lombok.Setter;
  22. import lombok.extern.slf4j.Slf4j;
  23. import java.nio.ByteBuffer;
  24. import java.nio.ByteOrder;
  25. import java.text.SimpleDateFormat;
  26. import java.util.*;
  27. @Slf4j
  28. @Getter
  29. @Setter
  30. public class TsiCpuPacket extends AbstractTsiPacket {
  31. // 헤더(10), 상태헤더(8), 상태정보(5)*상태정보건수, 체크섬(2)
  32. // CVIB Protocol Format(CPU-->CENTER)
  33. //TYPE STX1 STX2 LEN OPCODE DataVer NodeID DATA CHKSUM
  34. //Size Byte Byte 2Byte Byte 1Byte 4Byte NByte 2Byte
  35. //Value 0x7E 0x7E Size(LEN…CHKSUM) 0x13 0x01 … CRC16
  36. //- LEN : LEN∼CHKSUM data length
  37. // - OPCODE: 0x13, 신호상태정보전송
  38. // - NodeID: Node ID
  39. //- DATA : Opcode에 따른 데이터 영역
  40. //- CRC16 : CCITT / ITU / CRC - 16, bits shift right, final little(x) big(o) endian encoding.from LEN to DATA
  41. /*
  42. // 10 byte
  43. uint8_t stx1; // stx 0x7E
  44. uint8_t stx2; // stx 0x7E
  45. uint8_t length[2]; // length, length byte ~ check sum
  46. uint8_t opcode; // op code, 0x13
  47. uint8_t dataVer; // Data Version
  48. uint8_t nodeid[4]; // signal node id
  49. // 8 byte
  50. tsc_cvim_hdr_t hdr; // 8 byte, cvim signal status header
  51. // 5 byte * count
  52. tsc_cvim_stts_t stts[MAX_CVIM_STTS]; // 5 byte * 127 = 635 byte
  53. */
  54. // cvim-raw packet header
  55. /*
  56. #define TIMESPEC_SIZE (sizeof(struct timespec)) // 16 bytes
  57. uint8_t pktTm[TIMESPEC_SIZE]; // little endian
  58. uint8_t ipaddr[4]; // big endian
  59. uint8_t port[2]; // big endian
  60. uint8_t connect;
  61. uint8_t nodeid[4]; // big endian
  62. */
  63. public static final byte STX1 = 0x7E;
  64. public static final byte STX2 = 0x7E;
  65. public static final int SIZE_HEAD = 10;
  66. public static final int SIZE_STX1 = 1;
  67. public static final int SIZE_STX2 = 1;
  68. public static final int SIZE_LENGTH = 2;
  69. public static final int SIZE_OPCODE = 1;
  70. public static final int SIZE_VERSION = 1;
  71. public static final int SIZE_NODE_ID = 4;
  72. public static final int SIZE_STATUS_HDR = 8;
  73. public static final int SIZE_STATUS_DATA = 5;
  74. public static final int SIZE_CHECKSUM = 2;
  75. public static final int SIZE_PACKET_DATA = 18; // length, opcode, version, node_id, status_hdr, checksum
  76. public static final int INDEX_STX1 = 0; // 0
  77. public static final int INDEX_STX2 = 1; // 1
  78. public static final int INDEX_LENGTH = 2; // 2,3
  79. public static final int INDEX_OPCODE = 4; // 4
  80. public static final int INDEX_VERSION = 5; // 5
  81. public static final int INDEX_NODE_ID = 6; // 6,7,8,9
  82. public static final int INDEX_STATUS_HDR = 10; // 10,11,12,13,14,15,16,17
  83. public static final int INDEX_STATUS_DATA = 18; // 18,19,20,21,22
  84. public static final int INDEX_STATUS_DIR_ADD = 0; // 상태정보내 방향 추가 정보 인덱스
  85. public static final int INDEX_STATUS_DIRECTION = 4; // 상태정보내 방향코드
  86. public static final int SIZE_NODE_DUMMY = 1+1+2+1+1+2; // stx1, stx1, length(2), opcode, version, checksum(2)
  87. public static final int SIZE_NODE_HEAD = SIZE_NODE_ID + SIZE_STATUS_HDR;
  88. public static final int POS_NODE_HEAD_NODEID = 0; // node(4),
  89. public static final int POS_NODE_HEAD_COUNT = 7; // node(4),
  90. public static final int SIZE_IPC_SIZE = 27;
  91. public static final int SIZE_TIMESPEC = 16;
  92. public static final int POS_IPC_TIMESPEC = 0;
  93. public static final int POS_IPC_IPADDR = 16;
  94. public static final int POS_IPC_PORT = 20;
  95. public static final int POS_IPC_CONNECT = 22;
  96. public static final int POS_IPC_NODEID = 23;
  97. public static final int POS_IPC_PACKET = SIZE_IPC_SIZE;
  98. public static final byte CONNECT = 0x01;
  99. public static final byte DISCONNECT = 0x00;
  100. private Object obj;
  101. private int length;
  102. private byte dataVer;
  103. private int count;
  104. private int checkSum;
  105. protected byte[] cvimData; // for cvim-raw topic
  106. protected byte[] nodeData; // for node topic
  107. protected List<TsiCpuAddPacket> addNodes;
  108. public TsiCpuPacket(long nodeId, long msec, long nsec, Channel channel) {
  109. super(nodeId, msec, nsec, NettyUtils.getRemoteIpAddressToLong(channel), NettyUtils.getRemotePort(channel));
  110. setOpCode(eOpCode.TSI_CPU_SIGNAL_NOTIFY.getValue());
  111. }
  112. public TsiCpuPacket(long nodeId, long msec, long nsec, long remoteIpAddressToLong, int remotePort) {
  113. super(nodeId, msec, nsec, remoteIpAddressToLong, remotePort);
  114. }
  115. // FOR CVIM packet
  116. public TsiCpuPacket(long nodeId, byte[] value) {
  117. super(nodeId, TimeUtils.currentTimeSeconds(), System.nanoTime(), 0, 0);
  118. this.buf = value; // CVIM Header 를 포함한 내부 IPC 형식 데이터임
  119. int nodeLength = this.buf.length - TsiCpuPacket.SIZE_IPC_SIZE;
  120. this.cvimData = new byte[TsiCpuPacket.SIZE_IPC_SIZE];
  121. System.arraycopy(this.buf, 0, this.cvimData, 0, TsiCpuPacket.SIZE_IPC_SIZE);
  122. if (nodeLength > 0) {
  123. this.nodeData = new byte[nodeLength];
  124. System.arraycopy(this.buf, TsiCpuPacket.SIZE_IPC_SIZE, this.nodeData, 0, nodeLength);
  125. }
  126. }
  127. protected byte getStx1() {
  128. if (this.buf != null) return this.buf[INDEX_STX1];
  129. return 0x00;
  130. }
  131. protected byte getStx2() {
  132. if (this.buf != null) return this.buf[INDEX_STX2];
  133. return 0x00;
  134. }
  135. public byte[] getCvimData() {
  136. return this.cvimData;
  137. }
  138. public byte[] getTestData() {
  139. return this.buf;
  140. }
  141. public byte[] getNodeData() {
  142. return this.nodeData;
  143. }
  144. /*
  145. * Make cvim-raw packet
  146. */
  147. protected void makeCvimPacket() {
  148. int length = this.buf == null ? 0 : this.buf.length;
  149. this.cvimData = new byte[SIZE_IPC_SIZE + length];
  150. // cvim-raw header
  151. System.arraycopy(this.timespec.bytes(), 0, this.cvimData, POS_IPC_TIMESPEC, SIZE_TIMESPEC);
  152. ByteUtils.setUnsignedInt(this.cvimData, POS_IPC_IPADDR, getRemoteIp());
  153. ByteUtils.setUnsignedShort(this.cvimData, POS_IPC_PORT, getRemotePort());
  154. this.cvimData[POS_IPC_CONNECT] = opCode == (byte)eOpCode.TSI_CPU_SIGNAL_NOTIFY.getValue() ? CONNECT : DISCONNECT;
  155. ByteUtils.setUnsignedInt(this.cvimData, POS_IPC_NODEID, this.nodeId);
  156. // cvim-raw body
  157. if (length > 0) {
  158. System.arraycopy(this.buf, 0, this.cvimData, POS_IPC_PACKET, length);
  159. }
  160. //log.error("CVIM: {}", HexString.fromBytes(this.cvimData));
  161. }
  162. /*
  163. * Make cvim-raw packet
  164. */
  165. protected void makeAddNodeCvimPaket(TsiCpuPacket cpuPacket, byte[] packet) {
  166. int length = packet == null ? 0 : packet.length;
  167. final int headSize = SIZE_IPC_SIZE + 6;
  168. cpuPacket.cvimData = new byte[headSize + length + 2]; // cpu packet 6 byte(nodeid 제외), crc 2 byte
  169. // cvim-raw header
  170. System.arraycopy(timespec.bytes(), 0, cpuPacket.cvimData, POS_IPC_TIMESPEC, SIZE_TIMESPEC);
  171. ByteUtils.setUnsignedInt(cpuPacket.cvimData, POS_IPC_IPADDR, getRemoteIp());
  172. ByteUtils.setUnsignedShort(cpuPacket.cvimData, POS_IPC_PORT, getRemotePort());
  173. cpuPacket.cvimData[POS_IPC_CONNECT] = opCode == (byte)eOpCode.TSI_CPU_SIGNAL_NOTIFY.getValue() ? CONNECT : DISCONNECT;
  174. ByteUtils.setUnsignedInt(cpuPacket.cvimData, POS_IPC_NODEID, cpuPacket.nodeId);
  175. cpuPacket.cvimData[SIZE_IPC_SIZE+INDEX_STX1] = getStx1();
  176. cpuPacket.cvimData[SIZE_IPC_SIZE+INDEX_STX2] = getStx2();
  177. ByteUtils.setUnsignedShort(cpuPacket.cvimData, SIZE_IPC_SIZE+INDEX_LENGTH, length+2);
  178. cpuPacket.cvimData[SIZE_IPC_SIZE+INDEX_OPCODE] = (byte)getOpCode();
  179. cpuPacket.cvimData[SIZE_IPC_SIZE+INDEX_VERSION] = getDataVer();
  180. // cvim-raw body
  181. if (length > 0) {
  182. System.arraycopy(packet, 0, cpuPacket.cvimData, headSize, length);
  183. }
  184. // 체크섬 계산하지 않음
  185. }
  186. protected boolean checkPacket() {
  187. // 0 단계. STX1, STX2 체크
  188. if (this.buf[INDEX_STX1] != STX1 || this.buf[INDEX_STX2] != STX2) {
  189. log.info("Node: {}, STX Error: {}, {}", nodeId, this.buf[INDEX_STX1], this.buf[INDEX_STX2]);
  190. return false;
  191. }
  192. // 1 단계. 패킷 길이 체크
  193. if (this.length != ( SIZE_PACKET_DATA + (SIZE_STATUS_DATA * this.count) ) ) {
  194. log.info("Node: {}, Length Error: {}, status count: {}, {}", nodeId, this.length, this.count, SIZE_PACKET_DATA + (SIZE_STATUS_DATA * this.count));
  195. return false;
  196. }
  197. // 2단계. 체크섬
  198. this.checkSum = ByteUtils.getUnsignedShort(this.buf, this.buf.length-2);
  199. int calcCheckSum = CRC16Utils.CRC16_ccitt_cvim(this.buf, INDEX_LENGTH, this.length-2); // 시작인덱스가 있으므로 전체길이로 계산
  200. if (this.checkSum != calcCheckSum) {
  201. log.error("Node: {}, Check Sum Error: recv: {}, calc: {}", nodeId, this.checkSum, calcCheckSum);
  202. log.error("{}", HexString.fromBytes(this.buf));
  203. return false;
  204. }
  205. return true;
  206. }
  207. public boolean parsing(TsiNodeVo obj) {
  208. this.opCode = this.buf[INDEX_OPCODE];
  209. this.dataVer = this.buf[INDEX_VERSION];
  210. this.length = ByteUtils.getUnsignedShort(this.buf, INDEX_LENGTH);
  211. this.count = (int)(this.buf[INDEX_STATUS_HDR+3] & 0x7F);
  212. makeCvimPacket();
  213. if (!checkPacket()) {
  214. return false;
  215. }
  216. TsiNodeAddVo tsiNodeAddVo = TsiNodeAddManager.getInstance().get(this.nodeId);
  217. if (tsiNodeAddVo == null) {
  218. // 연등지 정보가 없는 경우
  219. int length = this.buf.length-SIZE_NODE_DUMMY;
  220. this.nodeData = new byte[length];
  221. System.arraycopy(this.buf, INDEX_NODE_ID, this.nodeData, 0, length);
  222. return true;
  223. }
  224. // 연등지 교차로 데이터 파싱
  225. byte[] head = new byte[SIZE_NODE_HEAD];
  226. List<byte[]> nodeStatus = new ArrayList<>();
  227. Map<Long, List<byte[]>> addStatus = new HashMap<>();
  228. System.arraycopy(this.buf, INDEX_NODE_ID, head, 0, SIZE_NODE_HEAD);
  229. int loop = 0;
  230. for (int ii = INDEX_STATUS_DATA; loop < this.count; ii += SIZE_STATUS_DATA, loop++) {
  231. byte[] status = new byte[SIZE_STATUS_DATA];
  232. System.arraycopy(this.buf, ii, status, 0, SIZE_STATUS_DATA);
  233. int dirAdd = (int)(status[INDEX_STATUS_DIR_ADD] & 0x0F);
  234. if (dirAdd == 0) {
  235. // 원천 노드 정보
  236. nodeStatus.add(status);
  237. }
  238. else {
  239. // 추가 노드 정보
  240. int directionCode = (int)status[INDEX_STATUS_DIRECTION];
  241. status[INDEX_STATUS_DIR_ADD] = (byte) (status[INDEX_STATUS_DIR_ADD] & 0xF0); // 방향추가정보를 0 으로 초기화
  242. TsiNodeAddDetailVo detailVo = tsiNodeAddVo.getAddNodeMap().get(directionCode * 1000 + dirAdd);
  243. if (detailVo != null) {
  244. //detailVo.getNodeObj();
  245. for (int dirIdx = 0; dirIdx < 2; dirIdx++) {
  246. if (detailVo.getAddDirCode()[dirIdx] != 0x00) {
  247. List<byte[]> list = addStatus.get(detailVo.getNodeId());
  248. if (list == null) {
  249. list = new ArrayList<>();
  250. addStatus.put(detailVo.getNodeId(), list);
  251. }
  252. if (dirIdx == 0) {
  253. status[INDEX_STATUS_DIRECTION] = detailVo.getAddDirCode()[dirIdx];
  254. list.add(status);
  255. }
  256. else {
  257. byte[] status2 = new byte[SIZE_STATUS_DATA];
  258. System.arraycopy(status, 0, status2, 0, SIZE_STATUS_DATA);
  259. status2[INDEX_STATUS_DIRECTION] = detailVo.getAddDirCode()[dirIdx];
  260. list.add(status2);
  261. }
  262. }
  263. }
  264. }
  265. }
  266. }
  267. byte splitFlag = 0x01;
  268. int statusCount = nodeStatus.size();
  269. this.nodeData = new byte[SIZE_NODE_HEAD + (statusCount*SIZE_STATUS_DATA)];
  270. System.arraycopy(head, 0, this.nodeData, 0, SIZE_NODE_HEAD);
  271. this.nodeData[POS_NODE_HEAD_COUNT] = (byte) (statusCount | (splitFlag << 7));
  272. for (int ii = 0; ii < statusCount; ii++) {
  273. System.arraycopy(nodeStatus.get(ii), 0, this.nodeData, SIZE_NODE_HEAD+(ii*SIZE_STATUS_DATA), SIZE_STATUS_DATA);
  274. }
  275. makeAddNodeCvimPaket(this, this.nodeData);
  276. if (addStatus.size() == 0) {
  277. // 연등지 정보가 설정된게 없을까???
  278. return true;
  279. }
  280. // 연등지 노드 카프카 패킷 생성
  281. this.addNodes = new ArrayList<>();
  282. for (Map.Entry<Long, List<byte[]>> entry: addStatus.entrySet()) {
  283. statusCount = entry.getValue().size();
  284. TsiCpuAddPacket addPacket = new TsiCpuAddPacket(entry.getKey(), this.timespec, this.remoteIp, this.remotePort);
  285. addPacket.setObj(TsiNodeAddManager.getInstance().get(entry.getKey()));
  286. addPacket.setNodeData(new byte[SIZE_NODE_HEAD + (statusCount*SIZE_STATUS_DATA)]);
  287. System.arraycopy(head, 0, addPacket.getNodeData(), 0, SIZE_NODE_HEAD);
  288. ByteUtils.setUnsignedInt(addPacket.getNodeData(), POS_NODE_HEAD_NODEID, entry.getKey());
  289. addPacket.getNodeData()[POS_NODE_HEAD_COUNT] = (byte) (statusCount | (splitFlag << 7));
  290. for (int ii = 0; ii < statusCount; ii++) {
  291. System.arraycopy(entry.getValue().get(ii), 0, addPacket.getNodeData(), SIZE_NODE_HEAD+(ii*SIZE_STATUS_DATA), SIZE_STATUS_DATA);
  292. }
  293. makeAddNodeCvimPaket(addPacket, addPacket.getNodeData());
  294. addNodes.add(addPacket);
  295. }
  296. /*
  297. log.error(" RAW: {}, {}", this.nodeId, HexString.fromBytes(getBuf()));
  298. log.error("NODE: {}, {}", this.nodeId, HexString.fromBytes(getNodeData()));
  299. log.error("CVIM: {}, {}", this.nodeId, HexString.fromBytes(getCvimData()));
  300. for (int ii = 0; ii < addNodes.size(); ii++) {
  301. log.error("NODE: {}, {}", addNodes.get(ii).nodeId, HexString.fromBytes(addNodes.get(ii).getNodeData()));
  302. log.error("CVIM: {}, {}", addNodes.get(ii).nodeId, HexString.fromBytes(addNodes.get(ii).getCvimData()));
  303. }*/
  304. return true;
  305. }
  306. public TcsNodeStatus getNodeStatus() {
  307. int nodeLength = this.buf.length - TsiCpuPacket.SIZE_IPC_SIZE;
  308. byte[] msec = new byte[8];
  309. byte[] nsec = new byte[8];
  310. System.arraycopy(this.cvimData, 0, msec, 0, 8);
  311. System.arraycopy(this.cvimData, 8, nsec, 0, 8);
  312. ByteBuffer byteMSec = ByteBuffer.wrap(msec);
  313. ByteBuffer byteNSec = ByteBuffer.wrap(nsec);
  314. byteMSec.order(ByteOrder.LITTLE_ENDIAN);
  315. byteNSec.order(ByteOrder.LITTLE_ENDIAN);
  316. long milliSeconds = byteMSec.getLong();
  317. long nanoSeconds = byteNSec.getLong();
  318. this.remoteIp = ByteUtils.getUnsignedInt(this.cvimData, TsiCpuPacket.POS_IPC_IPADDR);
  319. this.remotePort = ByteUtils.getUnsignedShort(this.cvimData, TsiCpuPacket.POS_IPC_PORT);
  320. /////
  321. getTimespec().tv_nsec(nanoSeconds); // recv tcp,
  322. Date date = new Date(milliSeconds * 1000L);
  323. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  324. sdf.setTimeZone(java.util.TimeZone.getTimeZone("GMT+9"));
  325. String collectTime = sdf.format(date);
  326. if (this.cvimData[TsiCpuPacket.POS_IPC_CONNECT] == TsiCpuPacket.DISCONNECT) {
  327. return TcsNodeStatus.builder()
  328. ._id(this.nodeId)
  329. .nodeId(this.nodeId)
  330. .isConnect(false)
  331. .collectTime(collectTime)
  332. .build();
  333. }
  334. if (nodeLength < TsiCpuPacket.SIZE_HEAD + TsiCpuPacket.SIZE_STATUS_HDR) {
  335. // packet error
  336. return null;
  337. }
  338. String tscDateTime;
  339. int cycleElapsedTime;
  340. TsiCvimControl tscControlInfo = new TsiCvimControl();
  341. TsiCvimAbnormal tscAbnormalInfo = new TsiCvimAbnormal();
  342. int signalStatusInfoCount;
  343. int divFlag;
  344. long localTime;
  345. List<TsiCvimStatus> signalStatusInfos = new ArrayList<>();
  346. //this.opCode = this.nodeData[TsiCpuPacket.INDEX_OPCODE];
  347. //byte dataVer = this.nodeData[TsiCpuPacket.INDEX_VERSION];
  348. //int length = ByteUtils.getUnsignedShort(this.nodeData, TsiCpuPacket.INDEX_LENGTH);
  349. //int count = (int)(this.nodeData[TsiCpuPacket.INDEX_STATUS_HDR+3] & 0x7F);
  350. byte control = this.cvimData[TsiCpuPacket.SIZE_IPC_SIZE+TsiCpuPacket.INDEX_STATUS_HDR+0];
  351. tscControlInfo.inManualControl = ((control ) & 0x01) == 0x01; //수동;
  352. tscControlInfo.inFlashingControl = ((control >> 1) & 0x01) == 0x01; //점멸;
  353. tscControlInfo.inLightsOutControl = ((control >> 2) & 0x01) == 0x01; //소등
  354. tscControlInfo.inActuationControl = ((control >> 3) & 0x01) == 0x01; //감응;
  355. tscControlInfo.inTransitionControl = ((control >> 4) & 0x01) == 0x01; //전이;
  356. byte abnormal = this.cvimData[TsiCpuPacket.SIZE_IPC_SIZE+TsiCpuPacket.INDEX_STATUS_HDR+1];
  357. tscAbnormalInfo.inSignalConflict = ((abnormal ) & 0x01) == 0x01; //모순상태;
  358. tscAbnormalInfo.inCenterComm = ((abnormal >> 1) & 0x01) == 0x01; //센터상태;
  359. tscAbnormalInfo.inScuComm = ((abnormal >> 2) & 0x01) == 0x01; //SCU 상태
  360. cycleElapsedTime = (int)(this.cvimData[TsiCpuPacket.SIZE_IPC_SIZE+TsiCpuPacket.INDEX_STATUS_HDR+2] & 0xFF);
  361. byte stts = this.cvimData[TsiCpuPacket.SIZE_IPC_SIZE+TsiCpuPacket.INDEX_STATUS_HDR+3];;
  362. signalStatusInfoCount = (int)(stts & 0x7F);
  363. //divFlag = (int)((stts >> 7) & 0x01);
  364. localTime = ByteUtils.getUnsignedInt(this.cvimData, TsiCpuPacket.SIZE_IPC_SIZE+TsiCpuPacket.INDEX_STATUS_HDR+4);
  365. int ii = TsiCpuPacket.SIZE_IPC_SIZE+TsiCpuPacket.INDEX_STATUS_DATA;
  366. for (int idx = 0; idx < signalStatusInfoCount; idx++) {
  367. TsiCvimStatus status = new TsiCvimStatus();
  368. //int dirAdd = (buffer.get(ii)) & 0x0F; //연등지
  369. int lightsType = (int)((this.cvimData[ii ] >> 4) & 0x0F);
  370. int lightsStatus = (int)((this.cvimData[ii+1] ) & 0x07);
  371. int reliability = (int)((this.cvimData[ii+1] >> 7) & 0x01);
  372. boolean readyPedestrian = ((this.cvimData[ii+1] >> 6) & 0x01) == 0x01;
  373. boolean unProtected = ((this.cvimData[ii+1] >> 3) & 0x01) == 0x01;
  374. int totalSeconds = (int)(this.cvimData[ii+2] & 0xFF);
  375. int remainSeconds = (int)(this.cvimData[ii+3] & 0xFF);
  376. int directionCode = (int)(this.cvimData[ii+4] & 0xFF);
  377. status.setLightsType(eLightsType.getByValue(lightsType)); // 신호등정보 [직진,좌,보행]
  378. status.setLightsStatus(eLightsStatus.getByValue(lightsStatus)); //신호등상태
  379. status.setTimeReliability(eTimeReliability.getByValue(reliability)); //시간정보신뢰성
  380. status.setReadyPedestrianSignal(readyPedestrian); //보행자
  381. status.setUnProtectedSignal(unProtected); //비보호 상태
  382. status.setTotalSeconds(totalSeconds); //표출시간
  383. status.setRemainingSeconds(remainSeconds); //잔여시간
  384. status.setDirectionCode(directionCode); //방향코드
  385. signalStatusInfos.add(status);
  386. ii += TsiCpuPacket.SIZE_STATUS_DATA;
  387. }
  388. date = new Date(localTime * 1000L);
  389. sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  390. sdf.setTimeZone(java.util.TimeZone.getTimeZone("GMT+9"));
  391. tscDateTime = sdf.format(date);
  392. //log.error("Packet Received: NodeId: {}, {}", this.nodeId, TimeUtils.elapsedTimeStr(System.nanoTime()-nanoSeconds));
  393. return TcsNodeStatus.builder()
  394. ._id(this.nodeId)
  395. .nodeId(this.nodeId)
  396. .isConnect(true)
  397. .collectTime(collectTime)
  398. .tscDateTime(tscDateTime)
  399. .cycleElapsedTime(cycleElapsedTime)
  400. .tscControlInfo(tscControlInfo)
  401. .tscAbnormalInfo(tscAbnormalInfo)
  402. .signalStatusInfoCount(signalStatusInfoCount)
  403. .signalStatusInfos(signalStatusInfos)
  404. .build();
  405. }
  406. }