CctvCommServerApplicationTests.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. package com.its.app;
  2. import com.google.common.primitives.Bytes;
  3. import com.its.app.utils.BcdConverter;
  4. import com.its.app.utils.SysUtils;
  5. import com.its.cctv.CctvCommServerApplication;
  6. import com.its.cctv.entity.TbCctvCtlr;
  7. import com.its.cctv.global.AppRepository;
  8. import com.its.cctv.xnettcp.cctv.process.CctvData;
  9. import com.its.cctv.xnettcp.cctv.process.CctvDataProcess;
  10. import com.its.cctv.xnettcp.cctv.protocol.CctvProtocol;
  11. import com.its.cctv.xnettcp.cctv.protocol.CctvResFramePacket;
  12. import lombok.extern.slf4j.Slf4j;
  13. import org.junit.jupiter.api.Test;
  14. import org.springframework.boot.test.context.SpringBootTest;
  15. import java.util.ArrayList;
  16. import java.util.List;
  17. @Slf4j
  18. @SpringBootTest(classes = CctvCommServerApplication.class)
  19. public class CctvCommServerApplicationTests {
  20. @Test
  21. void test1() {
  22. byte[] sysdate = new byte[7];
  23. sysdate[0] = (byte)(('2' - '0') << 4 | ('0' - '0'));
  24. sysdate[1] = (byte)(('2' - '0') << 4 | ('2' - '0'));
  25. sysdate[2] = (byte)(('0' - '0') << 4 | ('7' - '0'));
  26. sysdate[3] = (byte)(('0' - '0') << 4 | ('8' - '0'));
  27. sysdate[4] = (byte)(('1' - '0') << 4 | ('2' - '0'));
  28. sysdate[5] = (byte)(('3' - '0') << 4 | ('4' - '0'));
  29. sysdate[6] = (byte)(('5' - '0') << 4 | ('6' - '0'));
  30. String bcdSysdate = BcdConverter.bcdToString(sysdate);
  31. log.error("bcdSysdate: {}", bcdSysdate);
  32. String now = SysUtils.getSysTime();
  33. log.error("-------now: {}", now);
  34. byte[] result = BcdConverter.stringToBcd(now);
  35. log.error("----result: {}, {} Bytes", SysUtils.byteArrayToHex(result), result.length);
  36. String res = BcdConverter.bcdToString(result);
  37. log.error("-------res: {}", res);
  38. String now1 = "1234567890123";
  39. log.error("------now1: {}", now1);
  40. byte[] result1 = BcdConverter.stringToBcd(now1);
  41. log.error("---result: {}, {} Bytes", SysUtils.byteArrayToHex(result1), result1.length);
  42. String res1 = BcdConverter.bcdToString(result1);
  43. log.error("------res: {}", res1);
  44. }
  45. @Test
  46. void Test2() {
  47. int readableBytes = 23;
  48. byte[] recvBytes = new byte[readableBytes];
  49. recvBytes[ 0] = (byte)0x10;
  50. recvBytes[ 1] = (byte)0x02;
  51. recvBytes[ 2] = (byte)0x10;
  52. recvBytes[ 3] = (byte)0x10;
  53. recvBytes[ 4] = (byte)0x00;
  54. recvBytes[ 5] = (byte)0x14;
  55. recvBytes[ 6] = (byte)0x01;
  56. recvBytes[ 7] = (byte)0x01;
  57. recvBytes[ 8] = (byte)0x10;
  58. recvBytes[ 9] = (byte)0x03;
  59. recvBytes[10] = (byte)0x54;
  60. recvBytes[11] = (byte)0x80;
  61. recvBytes[12] = (byte)0x10;
  62. recvBytes[13] = (byte)0x02;
  63. recvBytes[14] = (byte)0x00;
  64. recvBytes[15] = (byte)0x00;
  65. recvBytes[16] = (byte)0x00;
  66. recvBytes[17] = (byte)0x14;
  67. recvBytes[18] = (byte)0x01;
  68. recvBytes[19] = (byte)0x01;
  69. recvBytes[20] = (byte)0x10;
  70. recvBytes[21] = (byte)0x03;
  71. recvBytes[22] = (byte)0x54;
  72. //recvBytes[23] = (byte)0x80;
  73. byte[] packets = new byte[readableBytes];
  74. int readIdx = 0;
  75. int msgSize = 0; // 순수 데이터 사이즈
  76. int totalMsgSize = 0;
  77. boolean foundDle = false;
  78. boolean remainPacket = false;
  79. String ipAddress = "0.0.0.0";
  80. log.error("recvBytes: {}", SysUtils.byteArrayToHex(recvBytes));
  81. while (true) {
  82. if (CctvProtocol.MIN_PACKET_SIZE > (readableBytes-totalMsgSize)) {
  83. break;
  84. }
  85. msgSize = 0;
  86. foundDle = false;
  87. log.error("xxxxxxxxxxxxx: {}, {}, {}, {}", readIdx, readableBytes-totalMsgSize, totalMsgSize, readableBytes);
  88. for (readIdx = totalMsgSize; readIdx < readableBytes; readIdx++) {
  89. byte data = recvBytes[readIdx];
  90. if (foundDle) {
  91. foundDle = false;
  92. if (data == CctvProtocol.cctv_DLE) {
  93. // Skip Data
  94. log.warn("RECV_0: [{}]. ReadableBytes: {} Bytes, ReadIndex: {}, DLE Stuffing Data Recv.", ipAddress, readableBytes, readIdx);
  95. } else if (data == CctvProtocol.cctv_STX) {
  96. packets[msgSize++] = data;
  97. } else if (data == CctvProtocol.cctv_ETX) {
  98. // 이전데이터가 DLE 이고 현재 데이터가 ETX 이면 패킷의 끝임.
  99. // 다음 2바이트는 CRC 데이터임, 남아 있는 데이터가 CRC 2바이트를 포함한 크기보다 작으면
  100. // 하나의 패킷을 완전히 수신하지 못한 것임
  101. log.error("LENGTH: {}, {}", (readIdx + 2), (readableBytes-totalMsgSize));
  102. if ((readIdx + 3) > (readableBytes)) {
  103. // 하나의 패킷을 읽지 못함
  104. log.warn("RECV_0: [{}]. ReadableBytes: {} Bytes, ReadIndex: {}, DLE Data Remain.", ipAddress, readableBytes, readIdx);
  105. remainPacket = true;
  106. break;
  107. }
  108. packets[msgSize++] = data;
  109. packets[msgSize++] = recvBytes[readIdx+1]; // CRC1
  110. packets[msgSize++] = recvBytes[readIdx+2]; // CRC2
  111. // 하나의 패킷을 읽었음 ***************
  112. totalMsgSize += msgSize;
  113. log.error("OnePacket: {} Bytes, {} Bytes, {}", msgSize, totalMsgSize, SysUtils.byteArrayToHex(packets));
  114. CctvResFramePacket packet = new CctvResFramePacket(null, packets, msgSize);
  115. log.error("OnePacket: {} Bytes, {}", packet.getByteBuffer().array().length, SysUtils.byteArrayToHex(packet.getByteBuffer().array()));
  116. break;
  117. } else {
  118. // DLE 다음에 와야할 데이터가 들어오지 않았다. 패킷에 오류가 발생함.
  119. log.warn("RECV_0: [{}]. ReadableBytes: {} Bytes, ReadIndex: {}, DLE Next Data Recv Error.", ipAddress, readableBytes, readIdx);
  120. }
  121. } else {
  122. packets[msgSize++] = data;
  123. if (data == CctvProtocol.cctv_DLE) {
  124. foundDle = true;
  125. }
  126. }
  127. }
  128. log.error("yyyyyyyyyyyyyyyyyyy: {}, {}, {}", totalMsgSize, readableBytes, remainPacket);
  129. if (totalMsgSize >= readableBytes || remainPacket) {
  130. break;
  131. }
  132. }
  133. }
  134. @Test
  135. void Test3() {
  136. int readableBytes = 10;
  137. byte[] recvBytes = new byte[readableBytes];
  138. recvBytes[ 0] = (byte)0x10;
  139. recvBytes[ 1] = (byte)0x02;
  140. recvBytes[ 2] = (byte)0x10;
  141. recvBytes[ 3] = (byte)0x10;
  142. recvBytes[ 4] = (byte)0x00;
  143. recvBytes[ 5] = (byte)0x14;
  144. recvBytes[ 6] = (byte)0x01;
  145. recvBytes[ 7] = (byte)0x01;
  146. recvBytes[ 8] = (byte)0x10;
  147. recvBytes[ 9] = (byte)0x03;
  148. log.error("{}", SysUtils.byteArrayToHex(recvBytes));
  149. List<Byte> list = new ArrayList<Byte>();
  150. for (byte b : recvBytes) {
  151. list.add(b);
  152. }
  153. log.error("LENGTH: {}", list.size());
  154. byte[] arr = Bytes.toArray(list);
  155. log.error("{}", SysUtils.byteArrayToHex(arr));
  156. }
  157. @Test
  158. void Test4() {
  159. }
  160. @Test
  161. void Test5() {
  162. // 온도 수신
  163. byte[] recvBytes = { (byte)0x10, (byte)0x02, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x12, (byte)0x1E,
  164. (byte)0x8A, (byte)0x40, (byte)0x21, (byte)0x00, (byte)0xDF, (byte)0x10, (byte)0x03, (byte)0xF2, (byte)0xB9};
  165. //(byte)0x8A, (byte)0x00, (byte)0x21, (byte)0x00, (byte)0xDF, (byte)0x10, (byte)0x03, (byte)0xF2, (byte)0xB9};
  166. CctvDataProcess cctvDataProcess = (CctvDataProcess)AppUtils.getBean(CctvDataProcess.class);
  167. TbCctvCtlr obj = AppRepository.getInstance().getCtlrMap().get("00000018");
  168. CctvResFramePacket packet = new CctvResFramePacket(obj, recvBytes, recvBytes.length);
  169. cctvDataProcess.add(new CctvData(obj, obj.getCCTV_CTLR_IP(), null, packet));
  170. CctvProtocol.sleep(6000);
  171. }
  172. //[2022-07-12 09:59:44] [22번 제어기 교통정보응답 수신] 10 02 00 00 00 16 04 88 40 01 00 00 00 00 00 00 00 00 00 00 00 00 00 0B 27 1A 61 3E 03 05 46 5A 08 32 0F 5D 40 04 03 29 47 06 35 0B 11 3F 03 02 25 42 10 03 E9 54
  173. @Test
  174. void Test6() {
  175. // 교통정보응답수신(22번 제어기)
  176. //byte[] recvBytes = { (byte)0x10, (byte)0x02, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x16, (byte)0x04, (byte)0x88, (byte)0x40, (byte)0x01, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x0B, (byte)0x27, (byte)0x1A, (byte)0x61, (byte)0x3E, (byte)0x03, (byte)0x05, (byte)0x46, (byte)0x5A, (byte)0x08, (byte)0x32, (byte)0x0F, (byte)0x5D, (byte)0x40, (byte)0x04, (byte)0x03, (byte)0x29, (byte)0x47, (byte)0x06, (byte)0x35, (byte)0x0B, (byte)0x11, (byte)0x3F, (byte)0x03, (byte)0x02, (byte)0x25, (byte)0x42, (byte)0x10, (byte)0x03, (byte)0xE9, (byte)0x54};
  177. // 교통정보응답수신(15번 제어기)
  178. byte[] recvBytes = {(byte)0x10, (byte)0x02, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x0F, (byte)0x04, (byte)0x8A, (byte)0x40, (byte)0x01, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x07, (byte)0x5E, (byte)0x05, (byte)0x48, (byte)0x2E, (byte)0x04, (byte)0x01, (byte)0x0E, (byte)0x26, (byte)0x0C, (byte)0x4B, (byte)0x0E, (byte)0x02, (byte)0x36, (byte)0x02, (byte)0x02, (byte)0x5D, (byte)0x2F, (byte)0x05, (byte)0x45, (byte)0x07, (byte)0x03, (byte)0x3C, (byte)0x06, (byte)0x01, (byte)0x2D, (byte)0x33, (byte)0x02, (byte)0x47, (byte)0x04, (byte)0x16, (byte)0x69, (byte)0x0A, (byte)0x00, (byte)0x62, (byte)0x32, (byte)0x07, (byte)0x40, (byte)0x0C, (byte)0x57, (byte)0x44, (byte)0x04, (byte)0x02, (byte)0x30, (byte)0x37, (byte)0x03, (byte)0x3C, (byte)0x06, (byte)0x41, (byte)0x5B, (byte)0x07, (byte)0x01, (byte)0x33, (byte)0x3B, (byte)0x10, (byte)0x03, (byte)0xB5, (byte)0x2C};
  179. //(byte)0x8A, (byte)0x00, (byte)0x21, (byte)0x00, (byte)0xDF, (byte)0x10, (byte)0x03, (byte)0xF2, (byte)0xB9};
  180. CctvDataProcess cctvDataProcess = (CctvDataProcess)AppUtils.getBean(CctvDataProcess.class);
  181. TbCctvCtlr obj = AppRepository.getInstance().getCtlrMap().get("00000015");
  182. CctvResFramePacket packet = new CctvResFramePacket(obj, recvBytes, recvBytes.length);
  183. cctvDataProcess.add(new CctvData(obj, obj.getCCTV_CTLR_IP(), null, packet));
  184. CctvProtocol.sleep(6000);
  185. }
  186. @Test
  187. void test7() {
  188. String xCrdn = String.format("%09d", 128422119);
  189. String yCrdn = String.format("%09d", 36136305);
  190. log.error("{}", xCrdn);
  191. log.error("{}", yCrdn);
  192. Double x_crdn = (Integer.parseInt(xCrdn) / 1000000.);
  193. Double y_crdn = (Integer.parseInt(yCrdn) / 1000000.);
  194. log.error("{}", x_crdn);
  195. log.error("{}", y_crdn);
  196. int aRingPhase = 2087276;
  197. String aPhase = String.format("%07d", aRingPhase);
  198. int aDir = aRingPhase / 1000000;
  199. log.error("{}", aPhase);
  200. log.error("{}", aPhase.substring(0, 1));
  201. log.error("{}", aPhase.substring(1, 4));
  202. log.error("{}", aPhase.substring(4, 7));
  203. }
  204. }