package com.its.app; import com.google.common.primitives.Bytes; import com.its.app.utils.BcdConverter; import com.its.app.utils.SysUtils; import com.its.cctv.CctvCommServerApplication; import com.its.cctv.entity.TbCctvCtlr; import com.its.cctv.global.AppRepository; import com.its.cctv.xnettcp.cctv.process.CctvData; import com.its.cctv.xnettcp.cctv.process.CctvDataProcess; import com.its.cctv.xnettcp.cctv.protocol.CctvProtocol; import com.its.cctv.xnettcp.cctv.protocol.CctvResFramePacket; import lombok.extern.slf4j.Slf4j; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; import java.util.ArrayList; import java.util.List; @Slf4j @SpringBootTest(classes = CctvCommServerApplication.class) public class CctvCommServerApplicationTests { @Test void test1() { byte[] sysdate = new byte[7]; sysdate[0] = (byte)(('2' - '0') << 4 | ('0' - '0')); sysdate[1] = (byte)(('2' - '0') << 4 | ('2' - '0')); sysdate[2] = (byte)(('0' - '0') << 4 | ('7' - '0')); sysdate[3] = (byte)(('0' - '0') << 4 | ('8' - '0')); sysdate[4] = (byte)(('1' - '0') << 4 | ('2' - '0')); sysdate[5] = (byte)(('3' - '0') << 4 | ('4' - '0')); sysdate[6] = (byte)(('5' - '0') << 4 | ('6' - '0')); String bcdSysdate = BcdConverter.bcdToString(sysdate); log.error("bcdSysdate: {}", bcdSysdate); String now = SysUtils.getSysTime(); log.error("-------now: {}", now); byte[] result = BcdConverter.stringToBcd(now); log.error("----result: {}, {} Bytes", SysUtils.byteArrayToHex(result), result.length); String res = BcdConverter.bcdToString(result); log.error("-------res: {}", res); String now1 = "1234567890123"; log.error("------now1: {}", now1); byte[] result1 = BcdConverter.stringToBcd(now1); log.error("---result: {}, {} Bytes", SysUtils.byteArrayToHex(result1), result1.length); String res1 = BcdConverter.bcdToString(result1); log.error("------res: {}", res1); } @Test void Test2() { int readableBytes = 23; byte[] recvBytes = new byte[readableBytes]; recvBytes[ 0] = (byte)0x10; recvBytes[ 1] = (byte)0x02; recvBytes[ 2] = (byte)0x10; recvBytes[ 3] = (byte)0x10; recvBytes[ 4] = (byte)0x00; recvBytes[ 5] = (byte)0x14; recvBytes[ 6] = (byte)0x01; recvBytes[ 7] = (byte)0x01; recvBytes[ 8] = (byte)0x10; recvBytes[ 9] = (byte)0x03; recvBytes[10] = (byte)0x54; recvBytes[11] = (byte)0x80; recvBytes[12] = (byte)0x10; recvBytes[13] = (byte)0x02; recvBytes[14] = (byte)0x00; recvBytes[15] = (byte)0x00; recvBytes[16] = (byte)0x00; recvBytes[17] = (byte)0x14; recvBytes[18] = (byte)0x01; recvBytes[19] = (byte)0x01; recvBytes[20] = (byte)0x10; recvBytes[21] = (byte)0x03; recvBytes[22] = (byte)0x54; //recvBytes[23] = (byte)0x80; byte[] packets = new byte[readableBytes]; int readIdx = 0; int msgSize = 0; // 순수 데이터 사이즈 int totalMsgSize = 0; boolean foundDle = false; boolean remainPacket = false; String ipAddress = "0.0.0.0"; log.error("recvBytes: {}", SysUtils.byteArrayToHex(recvBytes)); while (true) { if (CctvProtocol.MIN_PACKET_SIZE > (readableBytes-totalMsgSize)) { break; } msgSize = 0; foundDle = false; log.error("xxxxxxxxxxxxx: {}, {}, {}, {}", readIdx, readableBytes-totalMsgSize, totalMsgSize, readableBytes); for (readIdx = totalMsgSize; readIdx < readableBytes; readIdx++) { byte data = recvBytes[readIdx]; if (foundDle) { foundDle = false; if (data == CctvProtocol.cctv_DLE) { // Skip Data log.warn("RECV_0: [{}]. ReadableBytes: {} Bytes, ReadIndex: {}, DLE Stuffing Data Recv.", ipAddress, readableBytes, readIdx); } else if (data == CctvProtocol.cctv_STX) { packets[msgSize++] = data; } else if (data == CctvProtocol.cctv_ETX) { // 이전데이터가 DLE 이고 현재 데이터가 ETX 이면 패킷의 끝임. // 다음 2바이트는 CRC 데이터임, 남아 있는 데이터가 CRC 2바이트를 포함한 크기보다 작으면 // 하나의 패킷을 완전히 수신하지 못한 것임 log.error("LENGTH: {}, {}", (readIdx + 2), (readableBytes-totalMsgSize)); if ((readIdx + 3) > (readableBytes)) { // 하나의 패킷을 읽지 못함 log.warn("RECV_0: [{}]. ReadableBytes: {} Bytes, ReadIndex: {}, DLE Data Remain.", ipAddress, readableBytes, readIdx); remainPacket = true; break; } packets[msgSize++] = data; packets[msgSize++] = recvBytes[readIdx+1]; // CRC1 packets[msgSize++] = recvBytes[readIdx+2]; // CRC2 // 하나의 패킷을 읽었음 *************** totalMsgSize += msgSize; log.error("OnePacket: {} Bytes, {} Bytes, {}", msgSize, totalMsgSize, SysUtils.byteArrayToHex(packets)); CctvResFramePacket packet = new CctvResFramePacket(null, packets, msgSize); log.error("OnePacket: {} Bytes, {}", packet.getByteBuffer().array().length, SysUtils.byteArrayToHex(packet.getByteBuffer().array())); break; } else { // DLE 다음에 와야할 데이터가 들어오지 않았다. 패킷에 오류가 발생함. log.warn("RECV_0: [{}]. ReadableBytes: {} Bytes, ReadIndex: {}, DLE Next Data Recv Error.", ipAddress, readableBytes, readIdx); } } else { packets[msgSize++] = data; if (data == CctvProtocol.cctv_DLE) { foundDle = true; } } } log.error("yyyyyyyyyyyyyyyyyyy: {}, {}, {}", totalMsgSize, readableBytes, remainPacket); if (totalMsgSize >= readableBytes || remainPacket) { break; } } } @Test void Test3() { int readableBytes = 10; byte[] recvBytes = new byte[readableBytes]; recvBytes[ 0] = (byte)0x10; recvBytes[ 1] = (byte)0x02; recvBytes[ 2] = (byte)0x10; recvBytes[ 3] = (byte)0x10; recvBytes[ 4] = (byte)0x00; recvBytes[ 5] = (byte)0x14; recvBytes[ 6] = (byte)0x01; recvBytes[ 7] = (byte)0x01; recvBytes[ 8] = (byte)0x10; recvBytes[ 9] = (byte)0x03; log.error("{}", SysUtils.byteArrayToHex(recvBytes)); List list = new ArrayList(); for (byte b : recvBytes) { list.add(b); } log.error("LENGTH: {}", list.size()); byte[] arr = Bytes.toArray(list); log.error("{}", SysUtils.byteArrayToHex(arr)); } @Test void Test4() { } @Test void Test5() { // 온도 수신 byte[] recvBytes = { (byte)0x10, (byte)0x02, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x12, (byte)0x1E, (byte)0x8A, (byte)0x40, (byte)0x21, (byte)0x00, (byte)0xDF, (byte)0x10, (byte)0x03, (byte)0xF2, (byte)0xB9}; //(byte)0x8A, (byte)0x00, (byte)0x21, (byte)0x00, (byte)0xDF, (byte)0x10, (byte)0x03, (byte)0xF2, (byte)0xB9}; CctvDataProcess cctvDataProcess = (CctvDataProcess)AppUtils.getBean(CctvDataProcess.class); TbCctvCtlr obj = AppRepository.getInstance().getCtlrMap().get("00000018"); CctvResFramePacket packet = new CctvResFramePacket(obj, recvBytes, recvBytes.length); cctvDataProcess.add(new CctvData(obj, obj.getCCTV_CTLR_IP(), null, packet)); CctvProtocol.sleep(6000); } //[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 @Test void Test6() { // 교통정보응답수신(22번 제어기) //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}; // 교통정보응답수신(15번 제어기) 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}; //(byte)0x8A, (byte)0x00, (byte)0x21, (byte)0x00, (byte)0xDF, (byte)0x10, (byte)0x03, (byte)0xF2, (byte)0xB9}; CctvDataProcess cctvDataProcess = (CctvDataProcess)AppUtils.getBean(CctvDataProcess.class); TbCctvCtlr obj = AppRepository.getInstance().getCtlrMap().get("00000015"); CctvResFramePacket packet = new CctvResFramePacket(obj, recvBytes, recvBytes.length); cctvDataProcess.add(new CctvData(obj, obj.getCCTV_CTLR_IP(), null, packet)); CctvProtocol.sleep(6000); } @Test void test7() { String xCrdn = String.format("%09d", 128422119); String yCrdn = String.format("%09d", 36136305); log.error("{}", xCrdn); log.error("{}", yCrdn); Double x_crdn = (Integer.parseInt(xCrdn) / 1000000.); Double y_crdn = (Integer.parseInt(yCrdn) / 1000000.); log.error("{}", x_crdn); log.error("{}", y_crdn); int aRingPhase = 2087276; String aPhase = String.format("%07d", aRingPhase); int aDir = aRingPhase / 1000000; log.error("{}", aPhase); log.error("{}", aPhase.substring(0, 1)); log.error("{}", aPhase.substring(1, 4)); log.error("{}", aPhase.substring(4, 7)); } }