diff --git a/casic-server/pom.xml b/casic-server/pom.xml index 8c25977..805cc22 100644 --- a/casic-server/pom.xml +++ b/casic-server/pom.xml @@ -61,6 +61,11 @@ jna 3.0.0 + + net.jodah + expiringmap + 0.5.8 + diff --git a/casic-server/pom.xml b/casic-server/pom.xml index 8c25977..805cc22 100644 --- a/casic-server/pom.xml +++ b/casic-server/pom.xml @@ -61,6 +61,11 @@ jna 3.0.0 + + net.jodah + expiringmap + 0.5.8 + diff --git a/casic-server/src/main/java/com/casic/missiles/netty/ByteUtil.java b/casic-server/src/main/java/com/casic/missiles/netty/ByteUtil.java new file mode 100644 index 0000000..55c951e --- /dev/null +++ b/casic-server/src/main/java/com/casic/missiles/netty/ByteUtil.java @@ -0,0 +1,517 @@ +package com.casic.missiles.netty; + +import org.springframework.stereotype.Component; + +@Component +public class ByteUtil { + + /* BCD码转换为数值或字符 */ + + /** + * 单一字节中选择指定位bcd码转换为整数 + * 如ByteUtil.bcdToInt((byte) 0x33, 5, 4) = 3 + * + * @param b + * @param highBit 高位,最大7 + * @param lowBit 低位,从0开始 + * @return + */ + public static int bcdToInt(byte b, int highBit, int lowBit) { + int g = highBit - lowBit + 1; + int a = ((int) Math.pow(2, g) - 1) << lowBit; + int b2 = (b & a) >> lowBit; + if (b2 < 0 || b2 >= 10) { + return -1; + } + return b2; + } + + /** + * bcd码转换为整数 错误则返回-1 + * 如ByteUtil.bcdToInt((byte) 0x73) = 73 + * + * @param b 输入字节,默认4位一组,先高后低 + * @return + */ + public static int bcdToInt(byte b) { + int pl = 0x0f; + int ph = 0xf0; + + int h = (ph & b) >> 4; + int l = (pl & b); + + if (h >= 10 || l >= 10) { + return -1; + } + return h * 10 + l; + } + + /** + * bcd码转换为long型整数 + * 如字节数组{0x25, 0x23}转换为2523 + * + * @param + * @return + */ + public static long bcdToLong(byte[] bytes) { + if (ByteUtil.bcdToString(bytes).equals("") == false) + return Long.valueOf(ByteUtil.bcdToString(bytes)).longValue(); + else + return -1; + } + + + /** + * bcd码转换为字符串 按字节顺序 高位在前低位在后 + * 如字节数组{0x01, 0x02, 0x03}转换为"010203" + * + * @param bytes + * @return + */ + public static String bcdToString(byte[] bytes) { + StringBuilder sb = new StringBuilder(); + + for (int i = 0; i < bytes.length; i++) { + if (ByteUtil.bcdToInt(bytes[i], 7, 4) != -1 && ByteUtil.bcdToInt(bytes[i], 3, 0) != -1) { + sb.append(ByteUtil.bcdToInt(bytes[i], 7, 4)); + sb.append(ByteUtil.bcdToInt(bytes[i], 3, 0)); + } + } + + return sb.toString(); + } + + + /* BIN码转换为数值或字符 */ + + /** + * 单一字节中选择指定位bin码转换为整数 + * 如ByteUtil.binToInt((byte) 0xA3, 7, 7) = 1 + * + * @param b + * @param highBit 高位,最大7 + * @param lowBit 低位,从0开始 + * @return + */ + public static int binToInt(byte b, int highBit, int lowBit) throws RuntimeException { + int g = highBit - lowBit + 1; + int a = ((int) Math.pow(2, g) - 1) << lowBit; + int b2 = (b & a) >> lowBit; + if (b2 < 0 || b2 >= 16) { + throw new RuntimeException(); + } + return b2; + } + + /** + * BIN字节转换为整数 + * 如ByteUtil.bcdToInt((byte) 0x7B) = 123 + * + * @param b + * @return + */ + public static int binToInt(byte b) { + int i1 = 0x80 & b; + if (i1 == 0x80) { + int i2 = 0x7f & b; + return 128 + i2; + } else { + return b; + } + } + + /** + * BIN码转换为整数 高位在前低位在后 + * 如ByteUtil.bcdToInt(new byte[] {(byte) 0x7B, 0x22}) = 31522 + * + * @param bytes + * @return + */ + public static int binToInt(byte[] bytes) { + int i10 = 0; + if (null != bytes && bytes.length != 0) { + for (int i = 0; i < bytes.length; i++) { + i10 = i10 + new Double(Math.pow(16, (bytes.length - i - 1) * 2)).intValue() * ByteUtil.binToInt(bytes[i]); + } + } + return i10; + } + + /** + * BIN码转换为LONG型整数 高位在前低位在后 + * 如ByteUtil.bcdToLong(new byte[] {(byte) 0x7B, 0x22, 0x32, 0x9A}) = 2065838746 + * + * @param bytes + * @return + */ + public static int binToLong(byte[] bytes) { + int l10 = 0; + if (null != bytes && bytes.length != 0) { + for (int i = 0; i < bytes.length; i++) { + l10 = l10 + new Double(Math.pow(16, (bytes.length - i - 1) * 2)).intValue() * ByteUtil.binToInt(bytes[i]); + } + } + return l10; + } + + /** + * 字节数组转换为16进制字符串 按字节数组顺序转换 + * 如字节数组{0x21, 0x06, 0x01, 0x04, 0x40, (byte) 0xE2, 0x01}转换为"2106010440E201" + * + * @param bytes + * @return + */ + public static String binToHexString(byte[] bytes) { + StringBuilder sb = new StringBuilder(); + + for (int i = 0; i < bytes.length; i++) { + sb.append(Integer.toHexString(binToInt((byte) ((bytes[i] & 0xf0) >> 4))).toUpperCase()); + sb.append(Integer.toHexString(binToInt((byte) (bytes[i] & 0x0f))).toUpperCase()); + } + + return sb.toString(); + } + + + /** + * BIN输出为二进制字符串 + * 如字节数组{0x01, 0x38}转换为字符串"0000000100111000" + * + * @param bytes + * @return + */ + public static String binToBinString(byte[] bytes) { + StringBuilder sb = new StringBuilder(); + + for (int i = 0; i < bytes.length; i++) { + String s = Integer.toBinaryString(binToInt(bytes[i])); + while (s.length() < 8) { + s = "0" + s; + } + sb.append(s); + } + + return sb.toString(); + } + + + /* int转换为BCD码或BIN码 */ + + /** + * 两位整数转换成一个字节的BCD码 + * 大于100的整数取其十位和个位 十位在前个位在后 + * 如79转换为0x79 + * + * @param i + * @return + */ + public static byte intToBcd(int i) { + byte b = 0x00; + while (i > 99) { + i = i % 100; + } + + int l = i % 10; + int h = i / 10; + + b = (byte) (h << 4 | l); + + return b; + } + + /** + * 整数转换为字节数组 高位在前低位在后 + * 如 2379转换为0x23, 0x79 + * + * @param i + * @return + */ + public static byte[] intToBcds(long l) { + String str = String.valueOf(l); + Bytes bytes = new Bytes(); + + if (str.length() % 2 == 1) { + str = "0" + str; + } + + for (int i = 0; i < str.length() - 1; i = i + 2) { + String s = str.substring(i, i + 2); + bytes.append(ByteUtil.intToBcd(Integer.parseInt(s))); + } + + return bytes.toBytes(); + } + + /** + * 整数转换为指定字节数的BCD码数组 高位在前低位在后 从低位开始计算长度 超出部分忽略 不足部分在高位补0 + * 如 3013479指定2字节转换为0x34, 0x79, 指定6字节转换为0x00, 0x00, 0x03, 0x01, 0x34, 0x79 + * + * @param i + * @param length + * @return + */ + public static byte[] intToBcds(long l, int length) { + byte[] bts = intToBcds(l); + if (bts.length == length) { + return bts; + } else { + byte[] bytes = new byte[length]; + for (int j = length - 1; j >= 0; j--) { + if (j + bts.length - length >= 0) { + bytes[j] = bts[j + bts.length - length]; + } else { + bytes[j] = 0x00; + } + } + + return bytes; + } + } + + /** + * 整数转换为指定字节数的BCD码数组 低位在前高位在后 从低位开始计算长度 超出部分忽略 不足部分在高位补0 + * 如 3013479指定2字节转换为0x79, 0x34, 指定6字节转换为0x79, 0x34, 0x01, 0x03, 0x00, 0x00 + * + * @param i + * @param length + * @return + */ + public static byte[] intToBcdsHL(long l, int length) { + Bytes bs = new Bytes(); + + for (int i = 0; i < length; i++) { + int low = (int) (l % 100); + bs.append(ByteUtil.intToBcd(low)); + l = l / 100; + } + + return bs.toBytes(); + } + + /** + * 整数转换为BIN码字节数组 高位在前低位在后 + * 如113479转换为0x01, 0xBB, 0x47 + * + * @param l + * @return + */ + public static byte[] intToBins(long l) { + String str = Long.toHexString(l); + if (str.length() % 2 == 1) { + str = "0" + str; + } + + Bytes bytes = new Bytes(); + for (int i = 0; i < str.length() - 1; i = i + 2) { + String s = str.substring(i, i + 2); + bytes.append((byte) Integer.parseInt(s, 16)); + } + + return bytes.toBytes(); + } + + /** + * 整数转换为指定字节数的BIN码数组 高位在前低位在后 从低位开始计算长度 超出部分忽略 不足部分在高位补0 + * 如 3013479指定2字节转换为0xFB, 0x67, 指定6字节转换为0x00, 0x00, 0x00, 0x2D, 0xFB, 0x67 + * + * @param l + * @param length + * @return + */ + public static byte[] intToBins(long l, int length) { + byte[] bts = intToBins(l); + if (bts.length == length) { + return bts; + } else { + byte[] bytes = new byte[length]; + for (int j = length - 1; j >= 0; j--) { + if (j + bts.length - length >= 0) { + bytes[j] = bts[j + bts.length - length]; + } else { + bytes[j] = 0x00; + } + } + + return bytes; + } + } + + + /* 16进制字符串转换为字节数组 */ + + /** + * 16进制字符串顺序转换为字节数组 + * 如字符串"13810411703"转换为字节数组{0x01, 0x38, 0x10, 0x41, 0x17, 0x03} + * + * @param str + * @return + */ + public static byte[] hexStringToBytes(String str) { + if (str.length() % 2 == 1) { + str = "0" + str; + } + + Bytes bytes = new Bytes(); + for (int i = 0; i < str.length() - 1; i = i + 2) { + String s = str.substring(i, i + 2); + bytes.append((byte) Integer.parseInt(s, 16)); + } + + return bytes.toBytes(); + } + + /** + * 16进制字符串顺序转换为指定字节数的BIN码数组 从低位开始计算长度 超出部分忽略 不足部分在高位补0 + * 如字符串"13810411703"转换为3字节数组{0x41, 0x17, 0x03}, 指定8字节转换为{0x00, 0x00, 0x01, 0x38, 0x10, 0x41, 0x17, 0x03} + * + * @param str + * @param length + * @return + */ + public static byte[] hexStringToBytes(String str, int length) { + byte[] bts = hexStringToBytes(str); + if (bts.length == length) { + return bts; + } else { + byte[] bytes = new byte[length]; + for (int j = length - 1; j >= 0; j--) { + if (j + bts.length - length >= 0) { + bytes[j] = bts[j + bts.length - length]; + } else { + bytes[j] = 0x00; + } + } + + return bytes; + } + } + + + /* 十进制BCD码字符串转换为字节数组 */ + + /** + * 十进制BCD码字符串顺序转换为字节数组 + * 如字符串"13810411703"转换为字节数组{0x01, 0x38, 0x10, 0x41, 0x17, 0x03} + * + * @param str + * @return + */ + public static byte[] bcdStringToBytes(String str) { + if (str.length() % 2 == 1) { + str = "0" + str; + } + + Bytes bytes = new Bytes(); + for (int i = 0; i < str.length() - 1; i = i + 2) { + String s = str.substring(i, i + 2); + bytes.append(ByteUtil.intToBcd(Integer.parseInt(s))); + } + + return bytes.toBytes(); + } + + /** + * 十进制BCD码字符串顺序转换为指定字节数的BCD码数组 从低位开始计算长度 超出部分忽略 不足部分在高位补0 + * 如字符串"13810411703"转换为3字节数组{0x41, 0x17, 0x03}, 指定8字节转换为{0x00, 0x00, 0x01, 0x38, 0x10, 0x41, 0x17, 0x03} + * + * @param str + * @param length + * @return + */ + public static byte[] bcdStringToBytes(String str, int length) { + byte[] bts = bcdStringToBytes(str); + if (bts.length == length) { + return bts; + } else { + byte[] bytes = new byte[length]; + for (int j = length - 1; j >= 0; j--) { + if (j + bts.length - length >= 0) { + bytes[j] = bts[j + bts.length - length]; + } else { + bytes[j] = 0x00; + } + } + + return bytes; + } + } + + public static String buildCRC16(byte[] data) { + byte[] buf = new byte[data.length];// 存储需要产生校验码的数据 + for (int i = 0; i < data.length; i++) { + buf[i] = data[i]; + } + int len = buf.length; + int crc = 0xFFFF;//16位 + for (int pos = 0; pos < len; pos++) { + if (buf[pos] < 0) { + crc ^= (int) buf[pos] + 256; // XOR byte into least sig. byte of + // crc + } else { + crc ^= (int) buf[pos]; // XOR byte into least sig. byte of crc + } + for (int i = 8; i != 0; i--) { // Loop over each bit + if ((crc & 0x0001) != 0) { // If the LSB is set + crc >>= 1; // Shift right and XOR 0xA001 + crc ^= 0xA001; + } else + // Else LSB is not set + crc >>= 1; // Just shift right + } + } + String c = Integer.toHexString(crc); + if (c.length() == 4) { + c = c.substring(2, 4) + c.substring(0, 2); + } else if (c.length() == 3) { + c = "0" + c; + c = c.substring(2, 4) + c.substring(0, 2); + } else if (c.length() == 2) { + c = "0" + c.substring(1, 2) + "0" + c.substring(0, 1); + } + return c; + } + + + public static byte[] byteMergerAll(byte[]... values) { + int length_byte = 0; + for (int i = 0; i < values.length; i++) { + length_byte += values[i].length; + } + byte[] all_byte = new byte[length_byte]; + int countLength = 0; + for (int i = 0; i < values.length; i++) { + byte[] b = values[i]; + System.arraycopy(b, 0, all_byte, countLength, b.length); + countLength += b.length; + } + return all_byte; + } + + public static String convertToHex(String str) { + char[] charArray = str.toCharArray(); + String hexString = ""; + for (char c : charArray) { + hexString += Integer.toHexString((int) c); + } + return hexString; + } + + public static String reverseHex(String hex) { + char[] charArray = hex.toCharArray(); + int length = charArray.length; + int times = length / 2; + for (int c1i = 0; c1i < times; c1i += 2) { + int c2i = c1i + 1; + char c1 = charArray[c1i]; + char c2 = charArray[c2i]; + int c3i = length - c1i - 2; + int c4i = length - c1i - 1; + charArray[c1i] = charArray[c3i]; + charArray[c2i] = charArray[c4i]; + charArray[c3i] = c1; + charArray[c4i] = c2; + } + return new String(charArray); + } + +} diff --git a/casic-server/pom.xml b/casic-server/pom.xml index 8c25977..805cc22 100644 --- a/casic-server/pom.xml +++ b/casic-server/pom.xml @@ -61,6 +61,11 @@ jna 3.0.0 + + net.jodah + expiringmap + 0.5.8 + diff --git a/casic-server/src/main/java/com/casic/missiles/netty/ByteUtil.java b/casic-server/src/main/java/com/casic/missiles/netty/ByteUtil.java new file mode 100644 index 0000000..55c951e --- /dev/null +++ b/casic-server/src/main/java/com/casic/missiles/netty/ByteUtil.java @@ -0,0 +1,517 @@ +package com.casic.missiles.netty; + +import org.springframework.stereotype.Component; + +@Component +public class ByteUtil { + + /* BCD码转换为数值或字符 */ + + /** + * 单一字节中选择指定位bcd码转换为整数 + * 如ByteUtil.bcdToInt((byte) 0x33, 5, 4) = 3 + * + * @param b + * @param highBit 高位,最大7 + * @param lowBit 低位,从0开始 + * @return + */ + public static int bcdToInt(byte b, int highBit, int lowBit) { + int g = highBit - lowBit + 1; + int a = ((int) Math.pow(2, g) - 1) << lowBit; + int b2 = (b & a) >> lowBit; + if (b2 < 0 || b2 >= 10) { + return -1; + } + return b2; + } + + /** + * bcd码转换为整数 错误则返回-1 + * 如ByteUtil.bcdToInt((byte) 0x73) = 73 + * + * @param b 输入字节,默认4位一组,先高后低 + * @return + */ + public static int bcdToInt(byte b) { + int pl = 0x0f; + int ph = 0xf0; + + int h = (ph & b) >> 4; + int l = (pl & b); + + if (h >= 10 || l >= 10) { + return -1; + } + return h * 10 + l; + } + + /** + * bcd码转换为long型整数 + * 如字节数组{0x25, 0x23}转换为2523 + * + * @param + * @return + */ + public static long bcdToLong(byte[] bytes) { + if (ByteUtil.bcdToString(bytes).equals("") == false) + return Long.valueOf(ByteUtil.bcdToString(bytes)).longValue(); + else + return -1; + } + + + /** + * bcd码转换为字符串 按字节顺序 高位在前低位在后 + * 如字节数组{0x01, 0x02, 0x03}转换为"010203" + * + * @param bytes + * @return + */ + public static String bcdToString(byte[] bytes) { + StringBuilder sb = new StringBuilder(); + + for (int i = 0; i < bytes.length; i++) { + if (ByteUtil.bcdToInt(bytes[i], 7, 4) != -1 && ByteUtil.bcdToInt(bytes[i], 3, 0) != -1) { + sb.append(ByteUtil.bcdToInt(bytes[i], 7, 4)); + sb.append(ByteUtil.bcdToInt(bytes[i], 3, 0)); + } + } + + return sb.toString(); + } + + + /* BIN码转换为数值或字符 */ + + /** + * 单一字节中选择指定位bin码转换为整数 + * 如ByteUtil.binToInt((byte) 0xA3, 7, 7) = 1 + * + * @param b + * @param highBit 高位,最大7 + * @param lowBit 低位,从0开始 + * @return + */ + public static int binToInt(byte b, int highBit, int lowBit) throws RuntimeException { + int g = highBit - lowBit + 1; + int a = ((int) Math.pow(2, g) - 1) << lowBit; + int b2 = (b & a) >> lowBit; + if (b2 < 0 || b2 >= 16) { + throw new RuntimeException(); + } + return b2; + } + + /** + * BIN字节转换为整数 + * 如ByteUtil.bcdToInt((byte) 0x7B) = 123 + * + * @param b + * @return + */ + public static int binToInt(byte b) { + int i1 = 0x80 & b; + if (i1 == 0x80) { + int i2 = 0x7f & b; + return 128 + i2; + } else { + return b; + } + } + + /** + * BIN码转换为整数 高位在前低位在后 + * 如ByteUtil.bcdToInt(new byte[] {(byte) 0x7B, 0x22}) = 31522 + * + * @param bytes + * @return + */ + public static int binToInt(byte[] bytes) { + int i10 = 0; + if (null != bytes && bytes.length != 0) { + for (int i = 0; i < bytes.length; i++) { + i10 = i10 + new Double(Math.pow(16, (bytes.length - i - 1) * 2)).intValue() * ByteUtil.binToInt(bytes[i]); + } + } + return i10; + } + + /** + * BIN码转换为LONG型整数 高位在前低位在后 + * 如ByteUtil.bcdToLong(new byte[] {(byte) 0x7B, 0x22, 0x32, 0x9A}) = 2065838746 + * + * @param bytes + * @return + */ + public static int binToLong(byte[] bytes) { + int l10 = 0; + if (null != bytes && bytes.length != 0) { + for (int i = 0; i < bytes.length; i++) { + l10 = l10 + new Double(Math.pow(16, (bytes.length - i - 1) * 2)).intValue() * ByteUtil.binToInt(bytes[i]); + } + } + return l10; + } + + /** + * 字节数组转换为16进制字符串 按字节数组顺序转换 + * 如字节数组{0x21, 0x06, 0x01, 0x04, 0x40, (byte) 0xE2, 0x01}转换为"2106010440E201" + * + * @param bytes + * @return + */ + public static String binToHexString(byte[] bytes) { + StringBuilder sb = new StringBuilder(); + + for (int i = 0; i < bytes.length; i++) { + sb.append(Integer.toHexString(binToInt((byte) ((bytes[i] & 0xf0) >> 4))).toUpperCase()); + sb.append(Integer.toHexString(binToInt((byte) (bytes[i] & 0x0f))).toUpperCase()); + } + + return sb.toString(); + } + + + /** + * BIN输出为二进制字符串 + * 如字节数组{0x01, 0x38}转换为字符串"0000000100111000" + * + * @param bytes + * @return + */ + public static String binToBinString(byte[] bytes) { + StringBuilder sb = new StringBuilder(); + + for (int i = 0; i < bytes.length; i++) { + String s = Integer.toBinaryString(binToInt(bytes[i])); + while (s.length() < 8) { + s = "0" + s; + } + sb.append(s); + } + + return sb.toString(); + } + + + /* int转换为BCD码或BIN码 */ + + /** + * 两位整数转换成一个字节的BCD码 + * 大于100的整数取其十位和个位 十位在前个位在后 + * 如79转换为0x79 + * + * @param i + * @return + */ + public static byte intToBcd(int i) { + byte b = 0x00; + while (i > 99) { + i = i % 100; + } + + int l = i % 10; + int h = i / 10; + + b = (byte) (h << 4 | l); + + return b; + } + + /** + * 整数转换为字节数组 高位在前低位在后 + * 如 2379转换为0x23, 0x79 + * + * @param i + * @return + */ + public static byte[] intToBcds(long l) { + String str = String.valueOf(l); + Bytes bytes = new Bytes(); + + if (str.length() % 2 == 1) { + str = "0" + str; + } + + for (int i = 0; i < str.length() - 1; i = i + 2) { + String s = str.substring(i, i + 2); + bytes.append(ByteUtil.intToBcd(Integer.parseInt(s))); + } + + return bytes.toBytes(); + } + + /** + * 整数转换为指定字节数的BCD码数组 高位在前低位在后 从低位开始计算长度 超出部分忽略 不足部分在高位补0 + * 如 3013479指定2字节转换为0x34, 0x79, 指定6字节转换为0x00, 0x00, 0x03, 0x01, 0x34, 0x79 + * + * @param i + * @param length + * @return + */ + public static byte[] intToBcds(long l, int length) { + byte[] bts = intToBcds(l); + if (bts.length == length) { + return bts; + } else { + byte[] bytes = new byte[length]; + for (int j = length - 1; j >= 0; j--) { + if (j + bts.length - length >= 0) { + bytes[j] = bts[j + bts.length - length]; + } else { + bytes[j] = 0x00; + } + } + + return bytes; + } + } + + /** + * 整数转换为指定字节数的BCD码数组 低位在前高位在后 从低位开始计算长度 超出部分忽略 不足部分在高位补0 + * 如 3013479指定2字节转换为0x79, 0x34, 指定6字节转换为0x79, 0x34, 0x01, 0x03, 0x00, 0x00 + * + * @param i + * @param length + * @return + */ + public static byte[] intToBcdsHL(long l, int length) { + Bytes bs = new Bytes(); + + for (int i = 0; i < length; i++) { + int low = (int) (l % 100); + bs.append(ByteUtil.intToBcd(low)); + l = l / 100; + } + + return bs.toBytes(); + } + + /** + * 整数转换为BIN码字节数组 高位在前低位在后 + * 如113479转换为0x01, 0xBB, 0x47 + * + * @param l + * @return + */ + public static byte[] intToBins(long l) { + String str = Long.toHexString(l); + if (str.length() % 2 == 1) { + str = "0" + str; + } + + Bytes bytes = new Bytes(); + for (int i = 0; i < str.length() - 1; i = i + 2) { + String s = str.substring(i, i + 2); + bytes.append((byte) Integer.parseInt(s, 16)); + } + + return bytes.toBytes(); + } + + /** + * 整数转换为指定字节数的BIN码数组 高位在前低位在后 从低位开始计算长度 超出部分忽略 不足部分在高位补0 + * 如 3013479指定2字节转换为0xFB, 0x67, 指定6字节转换为0x00, 0x00, 0x00, 0x2D, 0xFB, 0x67 + * + * @param l + * @param length + * @return + */ + public static byte[] intToBins(long l, int length) { + byte[] bts = intToBins(l); + if (bts.length == length) { + return bts; + } else { + byte[] bytes = new byte[length]; + for (int j = length - 1; j >= 0; j--) { + if (j + bts.length - length >= 0) { + bytes[j] = bts[j + bts.length - length]; + } else { + bytes[j] = 0x00; + } + } + + return bytes; + } + } + + + /* 16进制字符串转换为字节数组 */ + + /** + * 16进制字符串顺序转换为字节数组 + * 如字符串"13810411703"转换为字节数组{0x01, 0x38, 0x10, 0x41, 0x17, 0x03} + * + * @param str + * @return + */ + public static byte[] hexStringToBytes(String str) { + if (str.length() % 2 == 1) { + str = "0" + str; + } + + Bytes bytes = new Bytes(); + for (int i = 0; i < str.length() - 1; i = i + 2) { + String s = str.substring(i, i + 2); + bytes.append((byte) Integer.parseInt(s, 16)); + } + + return bytes.toBytes(); + } + + /** + * 16进制字符串顺序转换为指定字节数的BIN码数组 从低位开始计算长度 超出部分忽略 不足部分在高位补0 + * 如字符串"13810411703"转换为3字节数组{0x41, 0x17, 0x03}, 指定8字节转换为{0x00, 0x00, 0x01, 0x38, 0x10, 0x41, 0x17, 0x03} + * + * @param str + * @param length + * @return + */ + public static byte[] hexStringToBytes(String str, int length) { + byte[] bts = hexStringToBytes(str); + if (bts.length == length) { + return bts; + } else { + byte[] bytes = new byte[length]; + for (int j = length - 1; j >= 0; j--) { + if (j + bts.length - length >= 0) { + bytes[j] = bts[j + bts.length - length]; + } else { + bytes[j] = 0x00; + } + } + + return bytes; + } + } + + + /* 十进制BCD码字符串转换为字节数组 */ + + /** + * 十进制BCD码字符串顺序转换为字节数组 + * 如字符串"13810411703"转换为字节数组{0x01, 0x38, 0x10, 0x41, 0x17, 0x03} + * + * @param str + * @return + */ + public static byte[] bcdStringToBytes(String str) { + if (str.length() % 2 == 1) { + str = "0" + str; + } + + Bytes bytes = new Bytes(); + for (int i = 0; i < str.length() - 1; i = i + 2) { + String s = str.substring(i, i + 2); + bytes.append(ByteUtil.intToBcd(Integer.parseInt(s))); + } + + return bytes.toBytes(); + } + + /** + * 十进制BCD码字符串顺序转换为指定字节数的BCD码数组 从低位开始计算长度 超出部分忽略 不足部分在高位补0 + * 如字符串"13810411703"转换为3字节数组{0x41, 0x17, 0x03}, 指定8字节转换为{0x00, 0x00, 0x01, 0x38, 0x10, 0x41, 0x17, 0x03} + * + * @param str + * @param length + * @return + */ + public static byte[] bcdStringToBytes(String str, int length) { + byte[] bts = bcdStringToBytes(str); + if (bts.length == length) { + return bts; + } else { + byte[] bytes = new byte[length]; + for (int j = length - 1; j >= 0; j--) { + if (j + bts.length - length >= 0) { + bytes[j] = bts[j + bts.length - length]; + } else { + bytes[j] = 0x00; + } + } + + return bytes; + } + } + + public static String buildCRC16(byte[] data) { + byte[] buf = new byte[data.length];// 存储需要产生校验码的数据 + for (int i = 0; i < data.length; i++) { + buf[i] = data[i]; + } + int len = buf.length; + int crc = 0xFFFF;//16位 + for (int pos = 0; pos < len; pos++) { + if (buf[pos] < 0) { + crc ^= (int) buf[pos] + 256; // XOR byte into least sig. byte of + // crc + } else { + crc ^= (int) buf[pos]; // XOR byte into least sig. byte of crc + } + for (int i = 8; i != 0; i--) { // Loop over each bit + if ((crc & 0x0001) != 0) { // If the LSB is set + crc >>= 1; // Shift right and XOR 0xA001 + crc ^= 0xA001; + } else + // Else LSB is not set + crc >>= 1; // Just shift right + } + } + String c = Integer.toHexString(crc); + if (c.length() == 4) { + c = c.substring(2, 4) + c.substring(0, 2); + } else if (c.length() == 3) { + c = "0" + c; + c = c.substring(2, 4) + c.substring(0, 2); + } else if (c.length() == 2) { + c = "0" + c.substring(1, 2) + "0" + c.substring(0, 1); + } + return c; + } + + + public static byte[] byteMergerAll(byte[]... values) { + int length_byte = 0; + for (int i = 0; i < values.length; i++) { + length_byte += values[i].length; + } + byte[] all_byte = new byte[length_byte]; + int countLength = 0; + for (int i = 0; i < values.length; i++) { + byte[] b = values[i]; + System.arraycopy(b, 0, all_byte, countLength, b.length); + countLength += b.length; + } + return all_byte; + } + + public static String convertToHex(String str) { + char[] charArray = str.toCharArray(); + String hexString = ""; + for (char c : charArray) { + hexString += Integer.toHexString((int) c); + } + return hexString; + } + + public static String reverseHex(String hex) { + char[] charArray = hex.toCharArray(); + int length = charArray.length; + int times = length / 2; + for (int c1i = 0; c1i < times; c1i += 2) { + int c2i = c1i + 1; + char c1 = charArray[c1i]; + char c2 = charArray[c2i]; + int c3i = length - c1i - 2; + int c4i = length - c1i - 1; + charArray[c1i] = charArray[c3i]; + charArray[c2i] = charArray[c4i]; + charArray[c3i] = c1; + charArray[c4i] = c2; + } + return new String(charArray); + } + +} diff --git a/casic-server/src/main/java/com/casic/missiles/netty/Bytes.java b/casic-server/src/main/java/com/casic/missiles/netty/Bytes.java new file mode 100644 index 0000000..9031d19 --- /dev/null +++ b/casic-server/src/main/java/com/casic/missiles/netty/Bytes.java @@ -0,0 +1,73 @@ +package com.casic.missiles.netty; + +public class Bytes { + + private byte[] buffer = null; + + public Bytes() { + + } + + public Bytes append(byte b) { + byte[] nb = null; + if (buffer == null) { + nb = new byte[] { b }; + } else { + nb = new byte[buffer.length + 1]; + for (int i = 0; i < buffer.length; i++) { + nb[i] = buffer[i]; + } + nb[nb.length - 1] = b; + } + buffer = nb; + return this; + } + + public Bytes append(byte[] bs) { + if (bs == null) + return this; + byte[] nb = null; + if (buffer == null) { + buffer = bs; + } else { + nb = new byte[buffer.length + bs.length]; + for (int i = 0; i < buffer.length; i++) { + nb[i] = buffer[i]; + } + for (int i = 0; i < bs.length; i++) { + nb[buffer.length + i] = bs[i]; + } + buffer = nb; + } + return this; + } + + public byte[] toBytes() { + return buffer; + } + + public byte[] toBytes(int l) { + Bytes bs = new Bytes(); + + for (int i = 0; i < l; i++) { + if (i < buffer.length) { + bs.append(buffer[i]); + } else { + bs.append((byte) 0x00); + } + } + + return bs.toBytes(); + } + + public static void main(String[] args) { + Bytes bytes = new Bytes(); + int i = 0xf2; + byte a = 0x68; + byte[] b = new byte[] { 0x11, 0x22 }; + byte c = (byte) i; + byte[] r = bytes.append(a).append(b).append(c).toBytes(); + System.out.println(r); + } + +} diff --git a/casic-server/pom.xml b/casic-server/pom.xml index 8c25977..805cc22 100644 --- a/casic-server/pom.xml +++ b/casic-server/pom.xml @@ -61,6 +61,11 @@ jna 3.0.0 + + net.jodah + expiringmap + 0.5.8 + diff --git a/casic-server/src/main/java/com/casic/missiles/netty/ByteUtil.java b/casic-server/src/main/java/com/casic/missiles/netty/ByteUtil.java new file mode 100644 index 0000000..55c951e --- /dev/null +++ b/casic-server/src/main/java/com/casic/missiles/netty/ByteUtil.java @@ -0,0 +1,517 @@ +package com.casic.missiles.netty; + +import org.springframework.stereotype.Component; + +@Component +public class ByteUtil { + + /* BCD码转换为数值或字符 */ + + /** + * 单一字节中选择指定位bcd码转换为整数 + * 如ByteUtil.bcdToInt((byte) 0x33, 5, 4) = 3 + * + * @param b + * @param highBit 高位,最大7 + * @param lowBit 低位,从0开始 + * @return + */ + public static int bcdToInt(byte b, int highBit, int lowBit) { + int g = highBit - lowBit + 1; + int a = ((int) Math.pow(2, g) - 1) << lowBit; + int b2 = (b & a) >> lowBit; + if (b2 < 0 || b2 >= 10) { + return -1; + } + return b2; + } + + /** + * bcd码转换为整数 错误则返回-1 + * 如ByteUtil.bcdToInt((byte) 0x73) = 73 + * + * @param b 输入字节,默认4位一组,先高后低 + * @return + */ + public static int bcdToInt(byte b) { + int pl = 0x0f; + int ph = 0xf0; + + int h = (ph & b) >> 4; + int l = (pl & b); + + if (h >= 10 || l >= 10) { + return -1; + } + return h * 10 + l; + } + + /** + * bcd码转换为long型整数 + * 如字节数组{0x25, 0x23}转换为2523 + * + * @param + * @return + */ + public static long bcdToLong(byte[] bytes) { + if (ByteUtil.bcdToString(bytes).equals("") == false) + return Long.valueOf(ByteUtil.bcdToString(bytes)).longValue(); + else + return -1; + } + + + /** + * bcd码转换为字符串 按字节顺序 高位在前低位在后 + * 如字节数组{0x01, 0x02, 0x03}转换为"010203" + * + * @param bytes + * @return + */ + public static String bcdToString(byte[] bytes) { + StringBuilder sb = new StringBuilder(); + + for (int i = 0; i < bytes.length; i++) { + if (ByteUtil.bcdToInt(bytes[i], 7, 4) != -1 && ByteUtil.bcdToInt(bytes[i], 3, 0) != -1) { + sb.append(ByteUtil.bcdToInt(bytes[i], 7, 4)); + sb.append(ByteUtil.bcdToInt(bytes[i], 3, 0)); + } + } + + return sb.toString(); + } + + + /* BIN码转换为数值或字符 */ + + /** + * 单一字节中选择指定位bin码转换为整数 + * 如ByteUtil.binToInt((byte) 0xA3, 7, 7) = 1 + * + * @param b + * @param highBit 高位,最大7 + * @param lowBit 低位,从0开始 + * @return + */ + public static int binToInt(byte b, int highBit, int lowBit) throws RuntimeException { + int g = highBit - lowBit + 1; + int a = ((int) Math.pow(2, g) - 1) << lowBit; + int b2 = (b & a) >> lowBit; + if (b2 < 0 || b2 >= 16) { + throw new RuntimeException(); + } + return b2; + } + + /** + * BIN字节转换为整数 + * 如ByteUtil.bcdToInt((byte) 0x7B) = 123 + * + * @param b + * @return + */ + public static int binToInt(byte b) { + int i1 = 0x80 & b; + if (i1 == 0x80) { + int i2 = 0x7f & b; + return 128 + i2; + } else { + return b; + } + } + + /** + * BIN码转换为整数 高位在前低位在后 + * 如ByteUtil.bcdToInt(new byte[] {(byte) 0x7B, 0x22}) = 31522 + * + * @param bytes + * @return + */ + public static int binToInt(byte[] bytes) { + int i10 = 0; + if (null != bytes && bytes.length != 0) { + for (int i = 0; i < bytes.length; i++) { + i10 = i10 + new Double(Math.pow(16, (bytes.length - i - 1) * 2)).intValue() * ByteUtil.binToInt(bytes[i]); + } + } + return i10; + } + + /** + * BIN码转换为LONG型整数 高位在前低位在后 + * 如ByteUtil.bcdToLong(new byte[] {(byte) 0x7B, 0x22, 0x32, 0x9A}) = 2065838746 + * + * @param bytes + * @return + */ + public static int binToLong(byte[] bytes) { + int l10 = 0; + if (null != bytes && bytes.length != 0) { + for (int i = 0; i < bytes.length; i++) { + l10 = l10 + new Double(Math.pow(16, (bytes.length - i - 1) * 2)).intValue() * ByteUtil.binToInt(bytes[i]); + } + } + return l10; + } + + /** + * 字节数组转换为16进制字符串 按字节数组顺序转换 + * 如字节数组{0x21, 0x06, 0x01, 0x04, 0x40, (byte) 0xE2, 0x01}转换为"2106010440E201" + * + * @param bytes + * @return + */ + public static String binToHexString(byte[] bytes) { + StringBuilder sb = new StringBuilder(); + + for (int i = 0; i < bytes.length; i++) { + sb.append(Integer.toHexString(binToInt((byte) ((bytes[i] & 0xf0) >> 4))).toUpperCase()); + sb.append(Integer.toHexString(binToInt((byte) (bytes[i] & 0x0f))).toUpperCase()); + } + + return sb.toString(); + } + + + /** + * BIN输出为二进制字符串 + * 如字节数组{0x01, 0x38}转换为字符串"0000000100111000" + * + * @param bytes + * @return + */ + public static String binToBinString(byte[] bytes) { + StringBuilder sb = new StringBuilder(); + + for (int i = 0; i < bytes.length; i++) { + String s = Integer.toBinaryString(binToInt(bytes[i])); + while (s.length() < 8) { + s = "0" + s; + } + sb.append(s); + } + + return sb.toString(); + } + + + /* int转换为BCD码或BIN码 */ + + /** + * 两位整数转换成一个字节的BCD码 + * 大于100的整数取其十位和个位 十位在前个位在后 + * 如79转换为0x79 + * + * @param i + * @return + */ + public static byte intToBcd(int i) { + byte b = 0x00; + while (i > 99) { + i = i % 100; + } + + int l = i % 10; + int h = i / 10; + + b = (byte) (h << 4 | l); + + return b; + } + + /** + * 整数转换为字节数组 高位在前低位在后 + * 如 2379转换为0x23, 0x79 + * + * @param i + * @return + */ + public static byte[] intToBcds(long l) { + String str = String.valueOf(l); + Bytes bytes = new Bytes(); + + if (str.length() % 2 == 1) { + str = "0" + str; + } + + for (int i = 0; i < str.length() - 1; i = i + 2) { + String s = str.substring(i, i + 2); + bytes.append(ByteUtil.intToBcd(Integer.parseInt(s))); + } + + return bytes.toBytes(); + } + + /** + * 整数转换为指定字节数的BCD码数组 高位在前低位在后 从低位开始计算长度 超出部分忽略 不足部分在高位补0 + * 如 3013479指定2字节转换为0x34, 0x79, 指定6字节转换为0x00, 0x00, 0x03, 0x01, 0x34, 0x79 + * + * @param i + * @param length + * @return + */ + public static byte[] intToBcds(long l, int length) { + byte[] bts = intToBcds(l); + if (bts.length == length) { + return bts; + } else { + byte[] bytes = new byte[length]; + for (int j = length - 1; j >= 0; j--) { + if (j + bts.length - length >= 0) { + bytes[j] = bts[j + bts.length - length]; + } else { + bytes[j] = 0x00; + } + } + + return bytes; + } + } + + /** + * 整数转换为指定字节数的BCD码数组 低位在前高位在后 从低位开始计算长度 超出部分忽略 不足部分在高位补0 + * 如 3013479指定2字节转换为0x79, 0x34, 指定6字节转换为0x79, 0x34, 0x01, 0x03, 0x00, 0x00 + * + * @param i + * @param length + * @return + */ + public static byte[] intToBcdsHL(long l, int length) { + Bytes bs = new Bytes(); + + for (int i = 0; i < length; i++) { + int low = (int) (l % 100); + bs.append(ByteUtil.intToBcd(low)); + l = l / 100; + } + + return bs.toBytes(); + } + + /** + * 整数转换为BIN码字节数组 高位在前低位在后 + * 如113479转换为0x01, 0xBB, 0x47 + * + * @param l + * @return + */ + public static byte[] intToBins(long l) { + String str = Long.toHexString(l); + if (str.length() % 2 == 1) { + str = "0" + str; + } + + Bytes bytes = new Bytes(); + for (int i = 0; i < str.length() - 1; i = i + 2) { + String s = str.substring(i, i + 2); + bytes.append((byte) Integer.parseInt(s, 16)); + } + + return bytes.toBytes(); + } + + /** + * 整数转换为指定字节数的BIN码数组 高位在前低位在后 从低位开始计算长度 超出部分忽略 不足部分在高位补0 + * 如 3013479指定2字节转换为0xFB, 0x67, 指定6字节转换为0x00, 0x00, 0x00, 0x2D, 0xFB, 0x67 + * + * @param l + * @param length + * @return + */ + public static byte[] intToBins(long l, int length) { + byte[] bts = intToBins(l); + if (bts.length == length) { + return bts; + } else { + byte[] bytes = new byte[length]; + for (int j = length - 1; j >= 0; j--) { + if (j + bts.length - length >= 0) { + bytes[j] = bts[j + bts.length - length]; + } else { + bytes[j] = 0x00; + } + } + + return bytes; + } + } + + + /* 16进制字符串转换为字节数组 */ + + /** + * 16进制字符串顺序转换为字节数组 + * 如字符串"13810411703"转换为字节数组{0x01, 0x38, 0x10, 0x41, 0x17, 0x03} + * + * @param str + * @return + */ + public static byte[] hexStringToBytes(String str) { + if (str.length() % 2 == 1) { + str = "0" + str; + } + + Bytes bytes = new Bytes(); + for (int i = 0; i < str.length() - 1; i = i + 2) { + String s = str.substring(i, i + 2); + bytes.append((byte) Integer.parseInt(s, 16)); + } + + return bytes.toBytes(); + } + + /** + * 16进制字符串顺序转换为指定字节数的BIN码数组 从低位开始计算长度 超出部分忽略 不足部分在高位补0 + * 如字符串"13810411703"转换为3字节数组{0x41, 0x17, 0x03}, 指定8字节转换为{0x00, 0x00, 0x01, 0x38, 0x10, 0x41, 0x17, 0x03} + * + * @param str + * @param length + * @return + */ + public static byte[] hexStringToBytes(String str, int length) { + byte[] bts = hexStringToBytes(str); + if (bts.length == length) { + return bts; + } else { + byte[] bytes = new byte[length]; + for (int j = length - 1; j >= 0; j--) { + if (j + bts.length - length >= 0) { + bytes[j] = bts[j + bts.length - length]; + } else { + bytes[j] = 0x00; + } + } + + return bytes; + } + } + + + /* 十进制BCD码字符串转换为字节数组 */ + + /** + * 十进制BCD码字符串顺序转换为字节数组 + * 如字符串"13810411703"转换为字节数组{0x01, 0x38, 0x10, 0x41, 0x17, 0x03} + * + * @param str + * @return + */ + public static byte[] bcdStringToBytes(String str) { + if (str.length() % 2 == 1) { + str = "0" + str; + } + + Bytes bytes = new Bytes(); + for (int i = 0; i < str.length() - 1; i = i + 2) { + String s = str.substring(i, i + 2); + bytes.append(ByteUtil.intToBcd(Integer.parseInt(s))); + } + + return bytes.toBytes(); + } + + /** + * 十进制BCD码字符串顺序转换为指定字节数的BCD码数组 从低位开始计算长度 超出部分忽略 不足部分在高位补0 + * 如字符串"13810411703"转换为3字节数组{0x41, 0x17, 0x03}, 指定8字节转换为{0x00, 0x00, 0x01, 0x38, 0x10, 0x41, 0x17, 0x03} + * + * @param str + * @param length + * @return + */ + public static byte[] bcdStringToBytes(String str, int length) { + byte[] bts = bcdStringToBytes(str); + if (bts.length == length) { + return bts; + } else { + byte[] bytes = new byte[length]; + for (int j = length - 1; j >= 0; j--) { + if (j + bts.length - length >= 0) { + bytes[j] = bts[j + bts.length - length]; + } else { + bytes[j] = 0x00; + } + } + + return bytes; + } + } + + public static String buildCRC16(byte[] data) { + byte[] buf = new byte[data.length];// 存储需要产生校验码的数据 + for (int i = 0; i < data.length; i++) { + buf[i] = data[i]; + } + int len = buf.length; + int crc = 0xFFFF;//16位 + for (int pos = 0; pos < len; pos++) { + if (buf[pos] < 0) { + crc ^= (int) buf[pos] + 256; // XOR byte into least sig. byte of + // crc + } else { + crc ^= (int) buf[pos]; // XOR byte into least sig. byte of crc + } + for (int i = 8; i != 0; i--) { // Loop over each bit + if ((crc & 0x0001) != 0) { // If the LSB is set + crc >>= 1; // Shift right and XOR 0xA001 + crc ^= 0xA001; + } else + // Else LSB is not set + crc >>= 1; // Just shift right + } + } + String c = Integer.toHexString(crc); + if (c.length() == 4) { + c = c.substring(2, 4) + c.substring(0, 2); + } else if (c.length() == 3) { + c = "0" + c; + c = c.substring(2, 4) + c.substring(0, 2); + } else if (c.length() == 2) { + c = "0" + c.substring(1, 2) + "0" + c.substring(0, 1); + } + return c; + } + + + public static byte[] byteMergerAll(byte[]... values) { + int length_byte = 0; + for (int i = 0; i < values.length; i++) { + length_byte += values[i].length; + } + byte[] all_byte = new byte[length_byte]; + int countLength = 0; + for (int i = 0; i < values.length; i++) { + byte[] b = values[i]; + System.arraycopy(b, 0, all_byte, countLength, b.length); + countLength += b.length; + } + return all_byte; + } + + public static String convertToHex(String str) { + char[] charArray = str.toCharArray(); + String hexString = ""; + for (char c : charArray) { + hexString += Integer.toHexString((int) c); + } + return hexString; + } + + public static String reverseHex(String hex) { + char[] charArray = hex.toCharArray(); + int length = charArray.length; + int times = length / 2; + for (int c1i = 0; c1i < times; c1i += 2) { + int c2i = c1i + 1; + char c1 = charArray[c1i]; + char c2 = charArray[c2i]; + int c3i = length - c1i - 2; + int c4i = length - c1i - 1; + charArray[c1i] = charArray[c3i]; + charArray[c2i] = charArray[c4i]; + charArray[c3i] = c1; + charArray[c4i] = c2; + } + return new String(charArray); + } + +} diff --git a/casic-server/src/main/java/com/casic/missiles/netty/Bytes.java b/casic-server/src/main/java/com/casic/missiles/netty/Bytes.java new file mode 100644 index 0000000..9031d19 --- /dev/null +++ b/casic-server/src/main/java/com/casic/missiles/netty/Bytes.java @@ -0,0 +1,73 @@ +package com.casic.missiles.netty; + +public class Bytes { + + private byte[] buffer = null; + + public Bytes() { + + } + + public Bytes append(byte b) { + byte[] nb = null; + if (buffer == null) { + nb = new byte[] { b }; + } else { + nb = new byte[buffer.length + 1]; + for (int i = 0; i < buffer.length; i++) { + nb[i] = buffer[i]; + } + nb[nb.length - 1] = b; + } + buffer = nb; + return this; + } + + public Bytes append(byte[] bs) { + if (bs == null) + return this; + byte[] nb = null; + if (buffer == null) { + buffer = bs; + } else { + nb = new byte[buffer.length + bs.length]; + for (int i = 0; i < buffer.length; i++) { + nb[i] = buffer[i]; + } + for (int i = 0; i < bs.length; i++) { + nb[buffer.length + i] = bs[i]; + } + buffer = nb; + } + return this; + } + + public byte[] toBytes() { + return buffer; + } + + public byte[] toBytes(int l) { + Bytes bs = new Bytes(); + + for (int i = 0; i < l; i++) { + if (i < buffer.length) { + bs.append(buffer[i]); + } else { + bs.append((byte) 0x00); + } + } + + return bs.toBytes(); + } + + public static void main(String[] args) { + Bytes bytes = new Bytes(); + int i = 0xf2; + byte a = 0x68; + byte[] b = new byte[] { 0x11, 0x22 }; + byte c = (byte) i; + byte[] r = bytes.append(a).append(b).append(c).toBytes(); + System.out.println(r); + } + +} diff --git a/casic-server/src/main/java/com/casic/missiles/netty/CacheUtils.java b/casic-server/src/main/java/com/casic/missiles/netty/CacheUtils.java new file mode 100644 index 0000000..8d2250a --- /dev/null +++ b/casic-server/src/main/java/com/casic/missiles/netty/CacheUtils.java @@ -0,0 +1,261 @@ +package com.casic.missiles.netty; + +import cn.hutool.core.util.ObjectUtil; +import lombok.extern.slf4j.Slf4j; +import net.jodah.expiringmap.ExpirationPolicy; +import net.jodah.expiringmap.ExpiringMap; +import org.springframework.stereotype.Component; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Set; +import java.util.concurrent.TimeUnit; + +@Slf4j +@Component +public class CacheUtils { + + //电压数据分包计数 + private volatile static int VOL_I = 0; + private volatile static boolean VOL_FLAG = false; + //PSD数据分包计数 + private volatile static int PSD_I = 0; + private volatile static boolean PSD_FLAG = false; + + /** + * 缓存电压信息 + */ + public static final ExpiringMap VOL_MAP = ExpiringMap.builder() + .maxSize(3000) + .expiration(5, TimeUnit.MINUTES) + .variableExpiration() + .expirationPolicy(ExpirationPolicy.ACCESSED) + .build(); + + /** + * 缓存PSD流信息 + */ + public static final ExpiringMap PSD_MAP = ExpiringMap.builder() + .maxSize(3000) + .expiration(5, TimeUnit.MINUTES) + .variableExpiration() +// .expirationListener((key,value) -> { +// System.out.println("key:" + key + "已过期……"); +// }) + .expirationPolicy(ExpirationPolicy.ACCESSED) + .build(); + + + + + public synchronized static String startVolMapCache() { + String msgs = ""; + if (CacheUtils.VOL_MAP.size() > 1) { + Set keys = CacheUtils.VOL_MAP.keySet(); + List keyInts = new ArrayList<>(keys); + Collections.sort(keyInts); + for (Integer i : keyInts) { + msgs += CacheUtils.VOL_MAP.get(i); + } + if (ObjectUtil.isNotEmpty(msgs)) { + CacheUtils.VOL_MAP.clear(); + //继续拆包处理 +// String msgLast = ""; +// if (msgs.indexOf(InstructCode.DAC_HEAD + InstructCode.DAC_HEAD_LENGTH) != -1) { +// if (msgs.length() < (msgs.indexOf(InstructCode.DAC_HEAD + InstructCode.DAC_HEAD_LENGTH) + 24 + InstructCode.DAC_DATA_LENGTH)) { +// msgLast = msgs.substring(msgs.indexOf(msgs.indexOf(InstructCode.DAC_HEAD + InstructCode.DAC_HEAD_LENGTH))); +// CacheUtils.VOL_MAP.put(keyInts.get(keyInts.size() - 1), msgLast); +// } +// } + } + + } + return msgs; + } + + public synchronized static String startPsdMapCache() { + String msgs = ""; + if (CacheUtils.PSD_MAP.size() > 1) { + Set keys = CacheUtils.PSD_MAP.keySet(); + List keyInts = new ArrayList<>(keys); + Collections.sort(keyInts); + for (Integer i : keyInts) { + msgs += CacheUtils.PSD_MAP.get(i); + } + if (ObjectUtil.isNotEmpty(msgs)) { + CacheUtils.PSD_MAP.clear(); + //继续拆包处理 +// String msgLast = ""; +// if (msgs.indexOf(InstructCode.PSD1_HEAD) != -1) { +// if (msgs.length() >= 24) { +// int totalLength = Integer.parseInt(ByteUtil.reverseHex(msgs.substring(msgs.indexOf(InstructCode.PSD1_HEAD) + 16, 24)), 16); +// if (msgs.length() < totalLength * 2) { +// msgLast = msgs.substring(msgs.indexOf(InstructCode.PSD1_HEAD)); +// CacheUtils.PSD_MAP.put(keyInts.get(keyInts.size() - 1), msgLast); +// } +// } +// } + } + } + return msgs; + } + + public static void main(String[] args) { + + String msg="f0f0a5a50300001102000c081b0070000000000059f372000d81030058d50000f0f0a5a50300001102000b081b0070000000000012c7a900bd7e03006ed3000055aa55aa4dfccf1a2c000000f0f0a5a50300001102000b081b00700000000000aa0b2e001080030084d40000"; + + msg="0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000055aa55aa8dfccf1a4e0000002331363337362c31363337362c31363337362c31363337362c31363337362c31363337362c3234342c32303030302c322c3130302c31363338332c302c323030302355aa55aa4dfccf1a2c000000f0f0a5a50300001102000c081b00700000000000df63650092810300e3d50000"; + +// msg="55aa55aa8dfccf1a4e0000002331363337362c31363337362c31363337362c31363337362c31363337362c31363337362c3234342c32303030302c322c3130302c31363338332c302c323030302355aa55aa4dfccf1a4c000000f0f0a5a50300001102000b081b00700000000000933b7100e47d0300a8d20000f0f0a5a50300001102000b081b00700000000000127c88003d810300a6d50000"; +// int totalLength = Integer.parseInt(ByteUtil.reverseHex(msg.substring(msg.indexOf(InstructCode.PSD1_HEAD) + 16, 24)), 16); + + +// msg="55aa55aa4dfccf1a2c000000f0f0a5a50300001102000b081b00700000000000b5560c00597f0300f6d3000055aa55aa4dfccf1a6c000000f0f0a5a50300001102000a081b007000000000005e8623007480030019d50000f0f0a5a50300001102000b081b0070000000000055083c006b7d030023d20000f0f0a5a50300001102000b081b007000000000007d3d3c00196103008fb7000055aa55aa5dfccf1a540000000000000000000000000000000000000000000000df2e000000000000000000000000000000000000000000000000000000000000e02e00000000000000000000000000000000000055aa55aa10fccf1a0c200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; + +// msg="55aa55aa4dfccf1a2c000000"; + + +// +// String[] msgArr = msg.split(InstructCode.PSD1_HEAD); +// if(msgArr.length>2){ +// for(int i=1;i= 24) { +//// log.info("原始msg--->"+msg); +// int totalLength = Integer.parseInt(ByteUtil.reverseHex(msg.substring(msg.indexOf(InstructCode.PSD1_HEAD) + 16, +// msg.indexOf(InstructCode.PSD1_HEAD) + 24)), 16); +// if (msg.length() >= totalLength * 2) { +// +// for (int i = 0; i < (msg.length() - (msg.indexOf(InstructCode.PSD1_HEAD) + 24)) / 64; i++) { +// String msgi = msg.substring((msg.indexOf(InstructCode.PSD1_HEAD) + 24) + i * 64, (msg.indexOf(InstructCode.PSD1_HEAD) + 24) + (i + 1) * 64); +// if (msgi.indexOf(InstructCode.PSD2_HEAD) == 0) { +// String gateOff = msgi.substring(16, 20); +// String baseline = msgi.substring(20, 24); +// String shortGate = msgi.substring(24, 28); +// String longGate = msgi.substring(28, 32); +// String qlong = msgi.substring(48, 56); +// String qshort = msgi.substring(56, 64); +// //toDo:算法计算 +// System.out.println("gateOff--->" + gateOff + +// "baseline--->" + baseline + +// "shortGate--->" + shortGate + +// "longGate--->" + longGate + +// "qlong--->" + qlong + +// "qshort--->" + qshort); +// } +// } +// } +// } + + } + + + public static void proccessData(String msg){ + //拆包处理 PSD数据 + if (msg.indexOf(InstructCode.PSD1_HEAD) != -1) { + + if (msg.length() >= 24) { +// log.info("原始msg--->"+msg); + int totalLength = Integer.parseInt(ByteUtil.reverseHex(msg.substring(msg.indexOf(InstructCode.PSD1_HEAD) + 16, + msg.indexOf(InstructCode.PSD1_HEAD) + 24)), 16); + if (msg.length() >= totalLength * 2) { + PSD_FLAG = false; + for (int i = 0; i < (msg.length() - (msg.indexOf(InstructCode.PSD1_HEAD) + 24)) / 64; i++) { + String msgi = msg.substring((msg.indexOf(InstructCode.PSD1_HEAD) + 24) + i * 64, (msg.indexOf(InstructCode.PSD1_HEAD) + 24) + (i + 1) * 64); + if (msgi.indexOf(InstructCode.PSD2_HEAD) == 0) { + String gateOff = msgi.substring(16, 20); + String baseline = msgi.substring(20, 24); + String shortGate = msgi.substring(24, 28); + String longGate = msgi.substring(28, 32); + String qlong = msgi.substring(48, 56); + String qshort = msgi.substring(56, 64); + //toDo:算法计算 + log.info("gateOff--->" + gateOff + + "baseline--->" + baseline + + "shortGate--->" + shortGate + + "longGate--->" + longGate + + "qlong--->" + qlong + + "qshort--->" + qshort); + } + } + } else { + //2.缺psd数据存入缓存处理 + PSD_FLAG = true; + PSD_I++; + CacheUtils.PSD_MAP.put(PSD_I, msg); +// log.info("psd加入数据:" + PSD_I + "------->" + msg); + } + } + } else if (PSD_FLAG) { + PSD_I++; + CacheUtils.PSD_MAP.put(PSD_I, msg); + String str = startPsdMapCache(); +// log.info("分包psd加入数据:" + PSD_I + "------->" + msg); + if (ObjectUtil.isNotEmpty(str) && str.length() >= 24) { +// log.info("**************strpsd---->" + str); + if (str.indexOf(InstructCode.PSD1_HEAD) != -1 && str.substring(str.indexOf(InstructCode.PSD1_HEAD) + 16).length() >= 8) { + int totalLength = Integer.parseInt(ByteUtil.reverseHex(str.substring(str.indexOf(InstructCode.PSD1_HEAD) + 16, + str.indexOf(InstructCode.PSD1_HEAD) + 24)), 16); + if (str.length() >= totalLength * 2) { + for (int i = 0; i < (str.length() - (str.indexOf(InstructCode.PSD1_HEAD) + 24)) / 64; i++) { + String msgi = str.substring((str.indexOf(InstructCode.PSD1_HEAD) + 24) + i * 64, (str.indexOf(InstructCode.PSD1_HEAD) + 24) + (i + 1) * 64); + if (msgi.indexOf(InstructCode.PSD2_HEAD) == 0) { + String gateOff = msgi.substring(16, 20); + String baseline = msgi.substring(20, 24); + String shortGate = msgi.substring(24, 28); + String longGate = msgi.substring(28, 32); + String qlong = msgi.substring(48, 56); + String qshort = msgi.substring(56, 64); + //toDo:算法计算 + log.info("分包:gateOff--->" + gateOff + + "baseline--->" + baseline + + "shortGate--->" + shortGate + + "longGate--->" + longGate + + "qlong--->" + qlong + + "qshort--->" + qshort); + } + } + } + } + } + } + //拆包处理 电压数据 + if (msg.indexOf(InstructCode.DAC_HEAD + InstructCode.DAC_HEAD_LENGTH) != -1) { + //完整包处理 + if (msg.length() >= (msg.indexOf(InstructCode.DAC_HEAD + InstructCode.DAC_HEAD_LENGTH) + 24 + InstructCode.DAC_DATA_LENGTH)) { + VOL_FLAG = false; + msg = msg.substring(msg.indexOf(InstructCode.DAC_HEAD + InstructCode.DAC_HEAD_LENGTH) + 24); + String[] volArr = InstructCode.getVolArr(msg); + //本设备只用到第6通道 + String vol6 = volArr[5]; + log.info("第6通道电压值-------->" + vol6); + } else { + //分包处理,先放到缓存 + VOL_FLAG = true; + VOL_I++; + CacheUtils.VOL_MAP.put(VOL_I, msg); + } + } else if (VOL_FLAG) { + VOL_I++; + CacheUtils.VOL_MAP.put(VOL_I, msg); + String str = startVolMapCache(); + if (ObjectUtil.isNotEmpty(str) && + str.length() >= (str.indexOf(InstructCode.DAC_HEAD + InstructCode.DAC_HEAD_LENGTH) + 24 + InstructCode.DAC_DATA_LENGTH)) { + VOL_FLAG = false; + str = str.substring(str.indexOf(InstructCode.DAC_HEAD + InstructCode.DAC_HEAD_LENGTH) + 24); + String[] volArr = InstructCode.getVolArr(str); + //本设备只用到第6通道 + String vol6 = volArr[5]; + log.info("分包第6通道电压值-------->" + vol6); + } + } + + } + +} \ No newline at end of file diff --git a/casic-server/pom.xml b/casic-server/pom.xml index 8c25977..805cc22 100644 --- a/casic-server/pom.xml +++ b/casic-server/pom.xml @@ -61,6 +61,11 @@ jna 3.0.0 + + net.jodah + expiringmap + 0.5.8 + diff --git a/casic-server/src/main/java/com/casic/missiles/netty/ByteUtil.java b/casic-server/src/main/java/com/casic/missiles/netty/ByteUtil.java new file mode 100644 index 0000000..55c951e --- /dev/null +++ b/casic-server/src/main/java/com/casic/missiles/netty/ByteUtil.java @@ -0,0 +1,517 @@ +package com.casic.missiles.netty; + +import org.springframework.stereotype.Component; + +@Component +public class ByteUtil { + + /* BCD码转换为数值或字符 */ + + /** + * 单一字节中选择指定位bcd码转换为整数 + * 如ByteUtil.bcdToInt((byte) 0x33, 5, 4) = 3 + * + * @param b + * @param highBit 高位,最大7 + * @param lowBit 低位,从0开始 + * @return + */ + public static int bcdToInt(byte b, int highBit, int lowBit) { + int g = highBit - lowBit + 1; + int a = ((int) Math.pow(2, g) - 1) << lowBit; + int b2 = (b & a) >> lowBit; + if (b2 < 0 || b2 >= 10) { + return -1; + } + return b2; + } + + /** + * bcd码转换为整数 错误则返回-1 + * 如ByteUtil.bcdToInt((byte) 0x73) = 73 + * + * @param b 输入字节,默认4位一组,先高后低 + * @return + */ + public static int bcdToInt(byte b) { + int pl = 0x0f; + int ph = 0xf0; + + int h = (ph & b) >> 4; + int l = (pl & b); + + if (h >= 10 || l >= 10) { + return -1; + } + return h * 10 + l; + } + + /** + * bcd码转换为long型整数 + * 如字节数组{0x25, 0x23}转换为2523 + * + * @param + * @return + */ + public static long bcdToLong(byte[] bytes) { + if (ByteUtil.bcdToString(bytes).equals("") == false) + return Long.valueOf(ByteUtil.bcdToString(bytes)).longValue(); + else + return -1; + } + + + /** + * bcd码转换为字符串 按字节顺序 高位在前低位在后 + * 如字节数组{0x01, 0x02, 0x03}转换为"010203" + * + * @param bytes + * @return + */ + public static String bcdToString(byte[] bytes) { + StringBuilder sb = new StringBuilder(); + + for (int i = 0; i < bytes.length; i++) { + if (ByteUtil.bcdToInt(bytes[i], 7, 4) != -1 && ByteUtil.bcdToInt(bytes[i], 3, 0) != -1) { + sb.append(ByteUtil.bcdToInt(bytes[i], 7, 4)); + sb.append(ByteUtil.bcdToInt(bytes[i], 3, 0)); + } + } + + return sb.toString(); + } + + + /* BIN码转换为数值或字符 */ + + /** + * 单一字节中选择指定位bin码转换为整数 + * 如ByteUtil.binToInt((byte) 0xA3, 7, 7) = 1 + * + * @param b + * @param highBit 高位,最大7 + * @param lowBit 低位,从0开始 + * @return + */ + public static int binToInt(byte b, int highBit, int lowBit) throws RuntimeException { + int g = highBit - lowBit + 1; + int a = ((int) Math.pow(2, g) - 1) << lowBit; + int b2 = (b & a) >> lowBit; + if (b2 < 0 || b2 >= 16) { + throw new RuntimeException(); + } + return b2; + } + + /** + * BIN字节转换为整数 + * 如ByteUtil.bcdToInt((byte) 0x7B) = 123 + * + * @param b + * @return + */ + public static int binToInt(byte b) { + int i1 = 0x80 & b; + if (i1 == 0x80) { + int i2 = 0x7f & b; + return 128 + i2; + } else { + return b; + } + } + + /** + * BIN码转换为整数 高位在前低位在后 + * 如ByteUtil.bcdToInt(new byte[] {(byte) 0x7B, 0x22}) = 31522 + * + * @param bytes + * @return + */ + public static int binToInt(byte[] bytes) { + int i10 = 0; + if (null != bytes && bytes.length != 0) { + for (int i = 0; i < bytes.length; i++) { + i10 = i10 + new Double(Math.pow(16, (bytes.length - i - 1) * 2)).intValue() * ByteUtil.binToInt(bytes[i]); + } + } + return i10; + } + + /** + * BIN码转换为LONG型整数 高位在前低位在后 + * 如ByteUtil.bcdToLong(new byte[] {(byte) 0x7B, 0x22, 0x32, 0x9A}) = 2065838746 + * + * @param bytes + * @return + */ + public static int binToLong(byte[] bytes) { + int l10 = 0; + if (null != bytes && bytes.length != 0) { + for (int i = 0; i < bytes.length; i++) { + l10 = l10 + new Double(Math.pow(16, (bytes.length - i - 1) * 2)).intValue() * ByteUtil.binToInt(bytes[i]); + } + } + return l10; + } + + /** + * 字节数组转换为16进制字符串 按字节数组顺序转换 + * 如字节数组{0x21, 0x06, 0x01, 0x04, 0x40, (byte) 0xE2, 0x01}转换为"2106010440E201" + * + * @param bytes + * @return + */ + public static String binToHexString(byte[] bytes) { + StringBuilder sb = new StringBuilder(); + + for (int i = 0; i < bytes.length; i++) { + sb.append(Integer.toHexString(binToInt((byte) ((bytes[i] & 0xf0) >> 4))).toUpperCase()); + sb.append(Integer.toHexString(binToInt((byte) (bytes[i] & 0x0f))).toUpperCase()); + } + + return sb.toString(); + } + + + /** + * BIN输出为二进制字符串 + * 如字节数组{0x01, 0x38}转换为字符串"0000000100111000" + * + * @param bytes + * @return + */ + public static String binToBinString(byte[] bytes) { + StringBuilder sb = new StringBuilder(); + + for (int i = 0; i < bytes.length; i++) { + String s = Integer.toBinaryString(binToInt(bytes[i])); + while (s.length() < 8) { + s = "0" + s; + } + sb.append(s); + } + + return sb.toString(); + } + + + /* int转换为BCD码或BIN码 */ + + /** + * 两位整数转换成一个字节的BCD码 + * 大于100的整数取其十位和个位 十位在前个位在后 + * 如79转换为0x79 + * + * @param i + * @return + */ + public static byte intToBcd(int i) { + byte b = 0x00; + while (i > 99) { + i = i % 100; + } + + int l = i % 10; + int h = i / 10; + + b = (byte) (h << 4 | l); + + return b; + } + + /** + * 整数转换为字节数组 高位在前低位在后 + * 如 2379转换为0x23, 0x79 + * + * @param i + * @return + */ + public static byte[] intToBcds(long l) { + String str = String.valueOf(l); + Bytes bytes = new Bytes(); + + if (str.length() % 2 == 1) { + str = "0" + str; + } + + for (int i = 0; i < str.length() - 1; i = i + 2) { + String s = str.substring(i, i + 2); + bytes.append(ByteUtil.intToBcd(Integer.parseInt(s))); + } + + return bytes.toBytes(); + } + + /** + * 整数转换为指定字节数的BCD码数组 高位在前低位在后 从低位开始计算长度 超出部分忽略 不足部分在高位补0 + * 如 3013479指定2字节转换为0x34, 0x79, 指定6字节转换为0x00, 0x00, 0x03, 0x01, 0x34, 0x79 + * + * @param i + * @param length + * @return + */ + public static byte[] intToBcds(long l, int length) { + byte[] bts = intToBcds(l); + if (bts.length == length) { + return bts; + } else { + byte[] bytes = new byte[length]; + for (int j = length - 1; j >= 0; j--) { + if (j + bts.length - length >= 0) { + bytes[j] = bts[j + bts.length - length]; + } else { + bytes[j] = 0x00; + } + } + + return bytes; + } + } + + /** + * 整数转换为指定字节数的BCD码数组 低位在前高位在后 从低位开始计算长度 超出部分忽略 不足部分在高位补0 + * 如 3013479指定2字节转换为0x79, 0x34, 指定6字节转换为0x79, 0x34, 0x01, 0x03, 0x00, 0x00 + * + * @param i + * @param length + * @return + */ + public static byte[] intToBcdsHL(long l, int length) { + Bytes bs = new Bytes(); + + for (int i = 0; i < length; i++) { + int low = (int) (l % 100); + bs.append(ByteUtil.intToBcd(low)); + l = l / 100; + } + + return bs.toBytes(); + } + + /** + * 整数转换为BIN码字节数组 高位在前低位在后 + * 如113479转换为0x01, 0xBB, 0x47 + * + * @param l + * @return + */ + public static byte[] intToBins(long l) { + String str = Long.toHexString(l); + if (str.length() % 2 == 1) { + str = "0" + str; + } + + Bytes bytes = new Bytes(); + for (int i = 0; i < str.length() - 1; i = i + 2) { + String s = str.substring(i, i + 2); + bytes.append((byte) Integer.parseInt(s, 16)); + } + + return bytes.toBytes(); + } + + /** + * 整数转换为指定字节数的BIN码数组 高位在前低位在后 从低位开始计算长度 超出部分忽略 不足部分在高位补0 + * 如 3013479指定2字节转换为0xFB, 0x67, 指定6字节转换为0x00, 0x00, 0x00, 0x2D, 0xFB, 0x67 + * + * @param l + * @param length + * @return + */ + public static byte[] intToBins(long l, int length) { + byte[] bts = intToBins(l); + if (bts.length == length) { + return bts; + } else { + byte[] bytes = new byte[length]; + for (int j = length - 1; j >= 0; j--) { + if (j + bts.length - length >= 0) { + bytes[j] = bts[j + bts.length - length]; + } else { + bytes[j] = 0x00; + } + } + + return bytes; + } + } + + + /* 16进制字符串转换为字节数组 */ + + /** + * 16进制字符串顺序转换为字节数组 + * 如字符串"13810411703"转换为字节数组{0x01, 0x38, 0x10, 0x41, 0x17, 0x03} + * + * @param str + * @return + */ + public static byte[] hexStringToBytes(String str) { + if (str.length() % 2 == 1) { + str = "0" + str; + } + + Bytes bytes = new Bytes(); + for (int i = 0; i < str.length() - 1; i = i + 2) { + String s = str.substring(i, i + 2); + bytes.append((byte) Integer.parseInt(s, 16)); + } + + return bytes.toBytes(); + } + + /** + * 16进制字符串顺序转换为指定字节数的BIN码数组 从低位开始计算长度 超出部分忽略 不足部分在高位补0 + * 如字符串"13810411703"转换为3字节数组{0x41, 0x17, 0x03}, 指定8字节转换为{0x00, 0x00, 0x01, 0x38, 0x10, 0x41, 0x17, 0x03} + * + * @param str + * @param length + * @return + */ + public static byte[] hexStringToBytes(String str, int length) { + byte[] bts = hexStringToBytes(str); + if (bts.length == length) { + return bts; + } else { + byte[] bytes = new byte[length]; + for (int j = length - 1; j >= 0; j--) { + if (j + bts.length - length >= 0) { + bytes[j] = bts[j + bts.length - length]; + } else { + bytes[j] = 0x00; + } + } + + return bytes; + } + } + + + /* 十进制BCD码字符串转换为字节数组 */ + + /** + * 十进制BCD码字符串顺序转换为字节数组 + * 如字符串"13810411703"转换为字节数组{0x01, 0x38, 0x10, 0x41, 0x17, 0x03} + * + * @param str + * @return + */ + public static byte[] bcdStringToBytes(String str) { + if (str.length() % 2 == 1) { + str = "0" + str; + } + + Bytes bytes = new Bytes(); + for (int i = 0; i < str.length() - 1; i = i + 2) { + String s = str.substring(i, i + 2); + bytes.append(ByteUtil.intToBcd(Integer.parseInt(s))); + } + + return bytes.toBytes(); + } + + /** + * 十进制BCD码字符串顺序转换为指定字节数的BCD码数组 从低位开始计算长度 超出部分忽略 不足部分在高位补0 + * 如字符串"13810411703"转换为3字节数组{0x41, 0x17, 0x03}, 指定8字节转换为{0x00, 0x00, 0x01, 0x38, 0x10, 0x41, 0x17, 0x03} + * + * @param str + * @param length + * @return + */ + public static byte[] bcdStringToBytes(String str, int length) { + byte[] bts = bcdStringToBytes(str); + if (bts.length == length) { + return bts; + } else { + byte[] bytes = new byte[length]; + for (int j = length - 1; j >= 0; j--) { + if (j + bts.length - length >= 0) { + bytes[j] = bts[j + bts.length - length]; + } else { + bytes[j] = 0x00; + } + } + + return bytes; + } + } + + public static String buildCRC16(byte[] data) { + byte[] buf = new byte[data.length];// 存储需要产生校验码的数据 + for (int i = 0; i < data.length; i++) { + buf[i] = data[i]; + } + int len = buf.length; + int crc = 0xFFFF;//16位 + for (int pos = 0; pos < len; pos++) { + if (buf[pos] < 0) { + crc ^= (int) buf[pos] + 256; // XOR byte into least sig. byte of + // crc + } else { + crc ^= (int) buf[pos]; // XOR byte into least sig. byte of crc + } + for (int i = 8; i != 0; i--) { // Loop over each bit + if ((crc & 0x0001) != 0) { // If the LSB is set + crc >>= 1; // Shift right and XOR 0xA001 + crc ^= 0xA001; + } else + // Else LSB is not set + crc >>= 1; // Just shift right + } + } + String c = Integer.toHexString(crc); + if (c.length() == 4) { + c = c.substring(2, 4) + c.substring(0, 2); + } else if (c.length() == 3) { + c = "0" + c; + c = c.substring(2, 4) + c.substring(0, 2); + } else if (c.length() == 2) { + c = "0" + c.substring(1, 2) + "0" + c.substring(0, 1); + } + return c; + } + + + public static byte[] byteMergerAll(byte[]... values) { + int length_byte = 0; + for (int i = 0; i < values.length; i++) { + length_byte += values[i].length; + } + byte[] all_byte = new byte[length_byte]; + int countLength = 0; + for (int i = 0; i < values.length; i++) { + byte[] b = values[i]; + System.arraycopy(b, 0, all_byte, countLength, b.length); + countLength += b.length; + } + return all_byte; + } + + public static String convertToHex(String str) { + char[] charArray = str.toCharArray(); + String hexString = ""; + for (char c : charArray) { + hexString += Integer.toHexString((int) c); + } + return hexString; + } + + public static String reverseHex(String hex) { + char[] charArray = hex.toCharArray(); + int length = charArray.length; + int times = length / 2; + for (int c1i = 0; c1i < times; c1i += 2) { + int c2i = c1i + 1; + char c1 = charArray[c1i]; + char c2 = charArray[c2i]; + int c3i = length - c1i - 2; + int c4i = length - c1i - 1; + charArray[c1i] = charArray[c3i]; + charArray[c2i] = charArray[c4i]; + charArray[c3i] = c1; + charArray[c4i] = c2; + } + return new String(charArray); + } + +} diff --git a/casic-server/src/main/java/com/casic/missiles/netty/Bytes.java b/casic-server/src/main/java/com/casic/missiles/netty/Bytes.java new file mode 100644 index 0000000..9031d19 --- /dev/null +++ b/casic-server/src/main/java/com/casic/missiles/netty/Bytes.java @@ -0,0 +1,73 @@ +package com.casic.missiles.netty; + +public class Bytes { + + private byte[] buffer = null; + + public Bytes() { + + } + + public Bytes append(byte b) { + byte[] nb = null; + if (buffer == null) { + nb = new byte[] { b }; + } else { + nb = new byte[buffer.length + 1]; + for (int i = 0; i < buffer.length; i++) { + nb[i] = buffer[i]; + } + nb[nb.length - 1] = b; + } + buffer = nb; + return this; + } + + public Bytes append(byte[] bs) { + if (bs == null) + return this; + byte[] nb = null; + if (buffer == null) { + buffer = bs; + } else { + nb = new byte[buffer.length + bs.length]; + for (int i = 0; i < buffer.length; i++) { + nb[i] = buffer[i]; + } + for (int i = 0; i < bs.length; i++) { + nb[buffer.length + i] = bs[i]; + } + buffer = nb; + } + return this; + } + + public byte[] toBytes() { + return buffer; + } + + public byte[] toBytes(int l) { + Bytes bs = new Bytes(); + + for (int i = 0; i < l; i++) { + if (i < buffer.length) { + bs.append(buffer[i]); + } else { + bs.append((byte) 0x00); + } + } + + return bs.toBytes(); + } + + public static void main(String[] args) { + Bytes bytes = new Bytes(); + int i = 0xf2; + byte a = 0x68; + byte[] b = new byte[] { 0x11, 0x22 }; + byte c = (byte) i; + byte[] r = bytes.append(a).append(b).append(c).toBytes(); + System.out.println(r); + } + +} diff --git a/casic-server/src/main/java/com/casic/missiles/netty/CacheUtils.java b/casic-server/src/main/java/com/casic/missiles/netty/CacheUtils.java new file mode 100644 index 0000000..8d2250a --- /dev/null +++ b/casic-server/src/main/java/com/casic/missiles/netty/CacheUtils.java @@ -0,0 +1,261 @@ +package com.casic.missiles.netty; + +import cn.hutool.core.util.ObjectUtil; +import lombok.extern.slf4j.Slf4j; +import net.jodah.expiringmap.ExpirationPolicy; +import net.jodah.expiringmap.ExpiringMap; +import org.springframework.stereotype.Component; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Set; +import java.util.concurrent.TimeUnit; + +@Slf4j +@Component +public class CacheUtils { + + //电压数据分包计数 + private volatile static int VOL_I = 0; + private volatile static boolean VOL_FLAG = false; + //PSD数据分包计数 + private volatile static int PSD_I = 0; + private volatile static boolean PSD_FLAG = false; + + /** + * 缓存电压信息 + */ + public static final ExpiringMap VOL_MAP = ExpiringMap.builder() + .maxSize(3000) + .expiration(5, TimeUnit.MINUTES) + .variableExpiration() + .expirationPolicy(ExpirationPolicy.ACCESSED) + .build(); + + /** + * 缓存PSD流信息 + */ + public static final ExpiringMap PSD_MAP = ExpiringMap.builder() + .maxSize(3000) + .expiration(5, TimeUnit.MINUTES) + .variableExpiration() +// .expirationListener((key,value) -> { +// System.out.println("key:" + key + "已过期……"); +// }) + .expirationPolicy(ExpirationPolicy.ACCESSED) + .build(); + + + + + public synchronized static String startVolMapCache() { + String msgs = ""; + if (CacheUtils.VOL_MAP.size() > 1) { + Set keys = CacheUtils.VOL_MAP.keySet(); + List keyInts = new ArrayList<>(keys); + Collections.sort(keyInts); + for (Integer i : keyInts) { + msgs += CacheUtils.VOL_MAP.get(i); + } + if (ObjectUtil.isNotEmpty(msgs)) { + CacheUtils.VOL_MAP.clear(); + //继续拆包处理 +// String msgLast = ""; +// if (msgs.indexOf(InstructCode.DAC_HEAD + InstructCode.DAC_HEAD_LENGTH) != -1) { +// if (msgs.length() < (msgs.indexOf(InstructCode.DAC_HEAD + InstructCode.DAC_HEAD_LENGTH) + 24 + InstructCode.DAC_DATA_LENGTH)) { +// msgLast = msgs.substring(msgs.indexOf(msgs.indexOf(InstructCode.DAC_HEAD + InstructCode.DAC_HEAD_LENGTH))); +// CacheUtils.VOL_MAP.put(keyInts.get(keyInts.size() - 1), msgLast); +// } +// } + } + + } + return msgs; + } + + public synchronized static String startPsdMapCache() { + String msgs = ""; + if (CacheUtils.PSD_MAP.size() > 1) { + Set keys = CacheUtils.PSD_MAP.keySet(); + List keyInts = new ArrayList<>(keys); + Collections.sort(keyInts); + for (Integer i : keyInts) { + msgs += CacheUtils.PSD_MAP.get(i); + } + if (ObjectUtil.isNotEmpty(msgs)) { + CacheUtils.PSD_MAP.clear(); + //继续拆包处理 +// String msgLast = ""; +// if (msgs.indexOf(InstructCode.PSD1_HEAD) != -1) { +// if (msgs.length() >= 24) { +// int totalLength = Integer.parseInt(ByteUtil.reverseHex(msgs.substring(msgs.indexOf(InstructCode.PSD1_HEAD) + 16, 24)), 16); +// if (msgs.length() < totalLength * 2) { +// msgLast = msgs.substring(msgs.indexOf(InstructCode.PSD1_HEAD)); +// CacheUtils.PSD_MAP.put(keyInts.get(keyInts.size() - 1), msgLast); +// } +// } +// } + } + } + return msgs; + } + + public static void main(String[] args) { + + String msg="f0f0a5a50300001102000c081b0070000000000059f372000d81030058d50000f0f0a5a50300001102000b081b0070000000000012c7a900bd7e03006ed3000055aa55aa4dfccf1a2c000000f0f0a5a50300001102000b081b00700000000000aa0b2e001080030084d40000"; + + msg="0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000055aa55aa8dfccf1a4e0000002331363337362c31363337362c31363337362c31363337362c31363337362c31363337362c3234342c32303030302c322c3130302c31363338332c302c323030302355aa55aa4dfccf1a2c000000f0f0a5a50300001102000c081b00700000000000df63650092810300e3d50000"; + +// msg="55aa55aa8dfccf1a4e0000002331363337362c31363337362c31363337362c31363337362c31363337362c31363337362c3234342c32303030302c322c3130302c31363338332c302c323030302355aa55aa4dfccf1a4c000000f0f0a5a50300001102000b081b00700000000000933b7100e47d0300a8d20000f0f0a5a50300001102000b081b00700000000000127c88003d810300a6d50000"; +// int totalLength = Integer.parseInt(ByteUtil.reverseHex(msg.substring(msg.indexOf(InstructCode.PSD1_HEAD) + 16, 24)), 16); + + +// msg="55aa55aa4dfccf1a2c000000f0f0a5a50300001102000b081b00700000000000b5560c00597f0300f6d3000055aa55aa4dfccf1a6c000000f0f0a5a50300001102000a081b007000000000005e8623007480030019d50000f0f0a5a50300001102000b081b0070000000000055083c006b7d030023d20000f0f0a5a50300001102000b081b007000000000007d3d3c00196103008fb7000055aa55aa5dfccf1a540000000000000000000000000000000000000000000000df2e000000000000000000000000000000000000000000000000000000000000e02e00000000000000000000000000000000000055aa55aa10fccf1a0c200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; + +// msg="55aa55aa4dfccf1a2c000000"; + + +// +// String[] msgArr = msg.split(InstructCode.PSD1_HEAD); +// if(msgArr.length>2){ +// for(int i=1;i= 24) { +//// log.info("原始msg--->"+msg); +// int totalLength = Integer.parseInt(ByteUtil.reverseHex(msg.substring(msg.indexOf(InstructCode.PSD1_HEAD) + 16, +// msg.indexOf(InstructCode.PSD1_HEAD) + 24)), 16); +// if (msg.length() >= totalLength * 2) { +// +// for (int i = 0; i < (msg.length() - (msg.indexOf(InstructCode.PSD1_HEAD) + 24)) / 64; i++) { +// String msgi = msg.substring((msg.indexOf(InstructCode.PSD1_HEAD) + 24) + i * 64, (msg.indexOf(InstructCode.PSD1_HEAD) + 24) + (i + 1) * 64); +// if (msgi.indexOf(InstructCode.PSD2_HEAD) == 0) { +// String gateOff = msgi.substring(16, 20); +// String baseline = msgi.substring(20, 24); +// String shortGate = msgi.substring(24, 28); +// String longGate = msgi.substring(28, 32); +// String qlong = msgi.substring(48, 56); +// String qshort = msgi.substring(56, 64); +// //toDo:算法计算 +// System.out.println("gateOff--->" + gateOff + +// "baseline--->" + baseline + +// "shortGate--->" + shortGate + +// "longGate--->" + longGate + +// "qlong--->" + qlong + +// "qshort--->" + qshort); +// } +// } +// } +// } + + } + + + public static void proccessData(String msg){ + //拆包处理 PSD数据 + if (msg.indexOf(InstructCode.PSD1_HEAD) != -1) { + + if (msg.length() >= 24) { +// log.info("原始msg--->"+msg); + int totalLength = Integer.parseInt(ByteUtil.reverseHex(msg.substring(msg.indexOf(InstructCode.PSD1_HEAD) + 16, + msg.indexOf(InstructCode.PSD1_HEAD) + 24)), 16); + if (msg.length() >= totalLength * 2) { + PSD_FLAG = false; + for (int i = 0; i < (msg.length() - (msg.indexOf(InstructCode.PSD1_HEAD) + 24)) / 64; i++) { + String msgi = msg.substring((msg.indexOf(InstructCode.PSD1_HEAD) + 24) + i * 64, (msg.indexOf(InstructCode.PSD1_HEAD) + 24) + (i + 1) * 64); + if (msgi.indexOf(InstructCode.PSD2_HEAD) == 0) { + String gateOff = msgi.substring(16, 20); + String baseline = msgi.substring(20, 24); + String shortGate = msgi.substring(24, 28); + String longGate = msgi.substring(28, 32); + String qlong = msgi.substring(48, 56); + String qshort = msgi.substring(56, 64); + //toDo:算法计算 + log.info("gateOff--->" + gateOff + + "baseline--->" + baseline + + "shortGate--->" + shortGate + + "longGate--->" + longGate + + "qlong--->" + qlong + + "qshort--->" + qshort); + } + } + } else { + //2.缺psd数据存入缓存处理 + PSD_FLAG = true; + PSD_I++; + CacheUtils.PSD_MAP.put(PSD_I, msg); +// log.info("psd加入数据:" + PSD_I + "------->" + msg); + } + } + } else if (PSD_FLAG) { + PSD_I++; + CacheUtils.PSD_MAP.put(PSD_I, msg); + String str = startPsdMapCache(); +// log.info("分包psd加入数据:" + PSD_I + "------->" + msg); + if (ObjectUtil.isNotEmpty(str) && str.length() >= 24) { +// log.info("**************strpsd---->" + str); + if (str.indexOf(InstructCode.PSD1_HEAD) != -1 && str.substring(str.indexOf(InstructCode.PSD1_HEAD) + 16).length() >= 8) { + int totalLength = Integer.parseInt(ByteUtil.reverseHex(str.substring(str.indexOf(InstructCode.PSD1_HEAD) + 16, + str.indexOf(InstructCode.PSD1_HEAD) + 24)), 16); + if (str.length() >= totalLength * 2) { + for (int i = 0; i < (str.length() - (str.indexOf(InstructCode.PSD1_HEAD) + 24)) / 64; i++) { + String msgi = str.substring((str.indexOf(InstructCode.PSD1_HEAD) + 24) + i * 64, (str.indexOf(InstructCode.PSD1_HEAD) + 24) + (i + 1) * 64); + if (msgi.indexOf(InstructCode.PSD2_HEAD) == 0) { + String gateOff = msgi.substring(16, 20); + String baseline = msgi.substring(20, 24); + String shortGate = msgi.substring(24, 28); + String longGate = msgi.substring(28, 32); + String qlong = msgi.substring(48, 56); + String qshort = msgi.substring(56, 64); + //toDo:算法计算 + log.info("分包:gateOff--->" + gateOff + + "baseline--->" + baseline + + "shortGate--->" + shortGate + + "longGate--->" + longGate + + "qlong--->" + qlong + + "qshort--->" + qshort); + } + } + } + } + } + } + //拆包处理 电压数据 + if (msg.indexOf(InstructCode.DAC_HEAD + InstructCode.DAC_HEAD_LENGTH) != -1) { + //完整包处理 + if (msg.length() >= (msg.indexOf(InstructCode.DAC_HEAD + InstructCode.DAC_HEAD_LENGTH) + 24 + InstructCode.DAC_DATA_LENGTH)) { + VOL_FLAG = false; + msg = msg.substring(msg.indexOf(InstructCode.DAC_HEAD + InstructCode.DAC_HEAD_LENGTH) + 24); + String[] volArr = InstructCode.getVolArr(msg); + //本设备只用到第6通道 + String vol6 = volArr[5]; + log.info("第6通道电压值-------->" + vol6); + } else { + //分包处理,先放到缓存 + VOL_FLAG = true; + VOL_I++; + CacheUtils.VOL_MAP.put(VOL_I, msg); + } + } else if (VOL_FLAG) { + VOL_I++; + CacheUtils.VOL_MAP.put(VOL_I, msg); + String str = startVolMapCache(); + if (ObjectUtil.isNotEmpty(str) && + str.length() >= (str.indexOf(InstructCode.DAC_HEAD + InstructCode.DAC_HEAD_LENGTH) + 24 + InstructCode.DAC_DATA_LENGTH)) { + VOL_FLAG = false; + str = str.substring(str.indexOf(InstructCode.DAC_HEAD + InstructCode.DAC_HEAD_LENGTH) + 24); + String[] volArr = InstructCode.getVolArr(str); + //本设备只用到第6通道 + String vol6 = volArr[5]; + log.info("分包第6通道电压值-------->" + vol6); + } + } + + } + +} \ No newline at end of file diff --git a/casic-server/src/main/java/com/casic/missiles/netty/HjtDecoder.java b/casic-server/src/main/java/com/casic/missiles/netty/HjtDecoder.java new file mode 100644 index 0000000..2649988 --- /dev/null +++ b/casic-server/src/main/java/com/casic/missiles/netty/HjtDecoder.java @@ -0,0 +1,24 @@ +package com.casic.missiles.netty; + +import io.netty.buffer.ByteBuf; +import io.netty.buffer.ByteBufUtil; +import io.netty.channel.ChannelHandlerContext; +import io.netty.handler.codec.MessageToMessageDecoder; +import lombok.extern.slf4j.Slf4j; + +import java.nio.charset.Charset; +import java.util.List; + +/** + * @description: 将从接口取到的数据编码 + * @author: Stone + * @create: 2019-01-11 15:15 + **/ +@Slf4j +public class HjtDecoder extends MessageToMessageDecoder { + @Override + protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List out) throws Exception { +// log.info("hexBytes : " + ByteBufUtil.hexDump(msg) + ";" + " String : " + msg.toString(Charset.defaultCharset())); + out.add(ByteBufUtil.hexDump(msg)); + } +} diff --git a/casic-server/pom.xml b/casic-server/pom.xml index 8c25977..805cc22 100644 --- a/casic-server/pom.xml +++ b/casic-server/pom.xml @@ -61,6 +61,11 @@ jna 3.0.0 + + net.jodah + expiringmap + 0.5.8 + diff --git a/casic-server/src/main/java/com/casic/missiles/netty/ByteUtil.java b/casic-server/src/main/java/com/casic/missiles/netty/ByteUtil.java new file mode 100644 index 0000000..55c951e --- /dev/null +++ b/casic-server/src/main/java/com/casic/missiles/netty/ByteUtil.java @@ -0,0 +1,517 @@ +package com.casic.missiles.netty; + +import org.springframework.stereotype.Component; + +@Component +public class ByteUtil { + + /* BCD码转换为数值或字符 */ + + /** + * 单一字节中选择指定位bcd码转换为整数 + * 如ByteUtil.bcdToInt((byte) 0x33, 5, 4) = 3 + * + * @param b + * @param highBit 高位,最大7 + * @param lowBit 低位,从0开始 + * @return + */ + public static int bcdToInt(byte b, int highBit, int lowBit) { + int g = highBit - lowBit + 1; + int a = ((int) Math.pow(2, g) - 1) << lowBit; + int b2 = (b & a) >> lowBit; + if (b2 < 0 || b2 >= 10) { + return -1; + } + return b2; + } + + /** + * bcd码转换为整数 错误则返回-1 + * 如ByteUtil.bcdToInt((byte) 0x73) = 73 + * + * @param b 输入字节,默认4位一组,先高后低 + * @return + */ + public static int bcdToInt(byte b) { + int pl = 0x0f; + int ph = 0xf0; + + int h = (ph & b) >> 4; + int l = (pl & b); + + if (h >= 10 || l >= 10) { + return -1; + } + return h * 10 + l; + } + + /** + * bcd码转换为long型整数 + * 如字节数组{0x25, 0x23}转换为2523 + * + * @param + * @return + */ + public static long bcdToLong(byte[] bytes) { + if (ByteUtil.bcdToString(bytes).equals("") == false) + return Long.valueOf(ByteUtil.bcdToString(bytes)).longValue(); + else + return -1; + } + + + /** + * bcd码转换为字符串 按字节顺序 高位在前低位在后 + * 如字节数组{0x01, 0x02, 0x03}转换为"010203" + * + * @param bytes + * @return + */ + public static String bcdToString(byte[] bytes) { + StringBuilder sb = new StringBuilder(); + + for (int i = 0; i < bytes.length; i++) { + if (ByteUtil.bcdToInt(bytes[i], 7, 4) != -1 && ByteUtil.bcdToInt(bytes[i], 3, 0) != -1) { + sb.append(ByteUtil.bcdToInt(bytes[i], 7, 4)); + sb.append(ByteUtil.bcdToInt(bytes[i], 3, 0)); + } + } + + return sb.toString(); + } + + + /* BIN码转换为数值或字符 */ + + /** + * 单一字节中选择指定位bin码转换为整数 + * 如ByteUtil.binToInt((byte) 0xA3, 7, 7) = 1 + * + * @param b + * @param highBit 高位,最大7 + * @param lowBit 低位,从0开始 + * @return + */ + public static int binToInt(byte b, int highBit, int lowBit) throws RuntimeException { + int g = highBit - lowBit + 1; + int a = ((int) Math.pow(2, g) - 1) << lowBit; + int b2 = (b & a) >> lowBit; + if (b2 < 0 || b2 >= 16) { + throw new RuntimeException(); + } + return b2; + } + + /** + * BIN字节转换为整数 + * 如ByteUtil.bcdToInt((byte) 0x7B) = 123 + * + * @param b + * @return + */ + public static int binToInt(byte b) { + int i1 = 0x80 & b; + if (i1 == 0x80) { + int i2 = 0x7f & b; + return 128 + i2; + } else { + return b; + } + } + + /** + * BIN码转换为整数 高位在前低位在后 + * 如ByteUtil.bcdToInt(new byte[] {(byte) 0x7B, 0x22}) = 31522 + * + * @param bytes + * @return + */ + public static int binToInt(byte[] bytes) { + int i10 = 0; + if (null != bytes && bytes.length != 0) { + for (int i = 0; i < bytes.length; i++) { + i10 = i10 + new Double(Math.pow(16, (bytes.length - i - 1) * 2)).intValue() * ByteUtil.binToInt(bytes[i]); + } + } + return i10; + } + + /** + * BIN码转换为LONG型整数 高位在前低位在后 + * 如ByteUtil.bcdToLong(new byte[] {(byte) 0x7B, 0x22, 0x32, 0x9A}) = 2065838746 + * + * @param bytes + * @return + */ + public static int binToLong(byte[] bytes) { + int l10 = 0; + if (null != bytes && bytes.length != 0) { + for (int i = 0; i < bytes.length; i++) { + l10 = l10 + new Double(Math.pow(16, (bytes.length - i - 1) * 2)).intValue() * ByteUtil.binToInt(bytes[i]); + } + } + return l10; + } + + /** + * 字节数组转换为16进制字符串 按字节数组顺序转换 + * 如字节数组{0x21, 0x06, 0x01, 0x04, 0x40, (byte) 0xE2, 0x01}转换为"2106010440E201" + * + * @param bytes + * @return + */ + public static String binToHexString(byte[] bytes) { + StringBuilder sb = new StringBuilder(); + + for (int i = 0; i < bytes.length; i++) { + sb.append(Integer.toHexString(binToInt((byte) ((bytes[i] & 0xf0) >> 4))).toUpperCase()); + sb.append(Integer.toHexString(binToInt((byte) (bytes[i] & 0x0f))).toUpperCase()); + } + + return sb.toString(); + } + + + /** + * BIN输出为二进制字符串 + * 如字节数组{0x01, 0x38}转换为字符串"0000000100111000" + * + * @param bytes + * @return + */ + public static String binToBinString(byte[] bytes) { + StringBuilder sb = new StringBuilder(); + + for (int i = 0; i < bytes.length; i++) { + String s = Integer.toBinaryString(binToInt(bytes[i])); + while (s.length() < 8) { + s = "0" + s; + } + sb.append(s); + } + + return sb.toString(); + } + + + /* int转换为BCD码或BIN码 */ + + /** + * 两位整数转换成一个字节的BCD码 + * 大于100的整数取其十位和个位 十位在前个位在后 + * 如79转换为0x79 + * + * @param i + * @return + */ + public static byte intToBcd(int i) { + byte b = 0x00; + while (i > 99) { + i = i % 100; + } + + int l = i % 10; + int h = i / 10; + + b = (byte) (h << 4 | l); + + return b; + } + + /** + * 整数转换为字节数组 高位在前低位在后 + * 如 2379转换为0x23, 0x79 + * + * @param i + * @return + */ + public static byte[] intToBcds(long l) { + String str = String.valueOf(l); + Bytes bytes = new Bytes(); + + if (str.length() % 2 == 1) { + str = "0" + str; + } + + for (int i = 0; i < str.length() - 1; i = i + 2) { + String s = str.substring(i, i + 2); + bytes.append(ByteUtil.intToBcd(Integer.parseInt(s))); + } + + return bytes.toBytes(); + } + + /** + * 整数转换为指定字节数的BCD码数组 高位在前低位在后 从低位开始计算长度 超出部分忽略 不足部分在高位补0 + * 如 3013479指定2字节转换为0x34, 0x79, 指定6字节转换为0x00, 0x00, 0x03, 0x01, 0x34, 0x79 + * + * @param i + * @param length + * @return + */ + public static byte[] intToBcds(long l, int length) { + byte[] bts = intToBcds(l); + if (bts.length == length) { + return bts; + } else { + byte[] bytes = new byte[length]; + for (int j = length - 1; j >= 0; j--) { + if (j + bts.length - length >= 0) { + bytes[j] = bts[j + bts.length - length]; + } else { + bytes[j] = 0x00; + } + } + + return bytes; + } + } + + /** + * 整数转换为指定字节数的BCD码数组 低位在前高位在后 从低位开始计算长度 超出部分忽略 不足部分在高位补0 + * 如 3013479指定2字节转换为0x79, 0x34, 指定6字节转换为0x79, 0x34, 0x01, 0x03, 0x00, 0x00 + * + * @param i + * @param length + * @return + */ + public static byte[] intToBcdsHL(long l, int length) { + Bytes bs = new Bytes(); + + for (int i = 0; i < length; i++) { + int low = (int) (l % 100); + bs.append(ByteUtil.intToBcd(low)); + l = l / 100; + } + + return bs.toBytes(); + } + + /** + * 整数转换为BIN码字节数组 高位在前低位在后 + * 如113479转换为0x01, 0xBB, 0x47 + * + * @param l + * @return + */ + public static byte[] intToBins(long l) { + String str = Long.toHexString(l); + if (str.length() % 2 == 1) { + str = "0" + str; + } + + Bytes bytes = new Bytes(); + for (int i = 0; i < str.length() - 1; i = i + 2) { + String s = str.substring(i, i + 2); + bytes.append((byte) Integer.parseInt(s, 16)); + } + + return bytes.toBytes(); + } + + /** + * 整数转换为指定字节数的BIN码数组 高位在前低位在后 从低位开始计算长度 超出部分忽略 不足部分在高位补0 + * 如 3013479指定2字节转换为0xFB, 0x67, 指定6字节转换为0x00, 0x00, 0x00, 0x2D, 0xFB, 0x67 + * + * @param l + * @param length + * @return + */ + public static byte[] intToBins(long l, int length) { + byte[] bts = intToBins(l); + if (bts.length == length) { + return bts; + } else { + byte[] bytes = new byte[length]; + for (int j = length - 1; j >= 0; j--) { + if (j + bts.length - length >= 0) { + bytes[j] = bts[j + bts.length - length]; + } else { + bytes[j] = 0x00; + } + } + + return bytes; + } + } + + + /* 16进制字符串转换为字节数组 */ + + /** + * 16进制字符串顺序转换为字节数组 + * 如字符串"13810411703"转换为字节数组{0x01, 0x38, 0x10, 0x41, 0x17, 0x03} + * + * @param str + * @return + */ + public static byte[] hexStringToBytes(String str) { + if (str.length() % 2 == 1) { + str = "0" + str; + } + + Bytes bytes = new Bytes(); + for (int i = 0; i < str.length() - 1; i = i + 2) { + String s = str.substring(i, i + 2); + bytes.append((byte) Integer.parseInt(s, 16)); + } + + return bytes.toBytes(); + } + + /** + * 16进制字符串顺序转换为指定字节数的BIN码数组 从低位开始计算长度 超出部分忽略 不足部分在高位补0 + * 如字符串"13810411703"转换为3字节数组{0x41, 0x17, 0x03}, 指定8字节转换为{0x00, 0x00, 0x01, 0x38, 0x10, 0x41, 0x17, 0x03} + * + * @param str + * @param length + * @return + */ + public static byte[] hexStringToBytes(String str, int length) { + byte[] bts = hexStringToBytes(str); + if (bts.length == length) { + return bts; + } else { + byte[] bytes = new byte[length]; + for (int j = length - 1; j >= 0; j--) { + if (j + bts.length - length >= 0) { + bytes[j] = bts[j + bts.length - length]; + } else { + bytes[j] = 0x00; + } + } + + return bytes; + } + } + + + /* 十进制BCD码字符串转换为字节数组 */ + + /** + * 十进制BCD码字符串顺序转换为字节数组 + * 如字符串"13810411703"转换为字节数组{0x01, 0x38, 0x10, 0x41, 0x17, 0x03} + * + * @param str + * @return + */ + public static byte[] bcdStringToBytes(String str) { + if (str.length() % 2 == 1) { + str = "0" + str; + } + + Bytes bytes = new Bytes(); + for (int i = 0; i < str.length() - 1; i = i + 2) { + String s = str.substring(i, i + 2); + bytes.append(ByteUtil.intToBcd(Integer.parseInt(s))); + } + + return bytes.toBytes(); + } + + /** + * 十进制BCD码字符串顺序转换为指定字节数的BCD码数组 从低位开始计算长度 超出部分忽略 不足部分在高位补0 + * 如字符串"13810411703"转换为3字节数组{0x41, 0x17, 0x03}, 指定8字节转换为{0x00, 0x00, 0x01, 0x38, 0x10, 0x41, 0x17, 0x03} + * + * @param str + * @param length + * @return + */ + public static byte[] bcdStringToBytes(String str, int length) { + byte[] bts = bcdStringToBytes(str); + if (bts.length == length) { + return bts; + } else { + byte[] bytes = new byte[length]; + for (int j = length - 1; j >= 0; j--) { + if (j + bts.length - length >= 0) { + bytes[j] = bts[j + bts.length - length]; + } else { + bytes[j] = 0x00; + } + } + + return bytes; + } + } + + public static String buildCRC16(byte[] data) { + byte[] buf = new byte[data.length];// 存储需要产生校验码的数据 + for (int i = 0; i < data.length; i++) { + buf[i] = data[i]; + } + int len = buf.length; + int crc = 0xFFFF;//16位 + for (int pos = 0; pos < len; pos++) { + if (buf[pos] < 0) { + crc ^= (int) buf[pos] + 256; // XOR byte into least sig. byte of + // crc + } else { + crc ^= (int) buf[pos]; // XOR byte into least sig. byte of crc + } + for (int i = 8; i != 0; i--) { // Loop over each bit + if ((crc & 0x0001) != 0) { // If the LSB is set + crc >>= 1; // Shift right and XOR 0xA001 + crc ^= 0xA001; + } else + // Else LSB is not set + crc >>= 1; // Just shift right + } + } + String c = Integer.toHexString(crc); + if (c.length() == 4) { + c = c.substring(2, 4) + c.substring(0, 2); + } else if (c.length() == 3) { + c = "0" + c; + c = c.substring(2, 4) + c.substring(0, 2); + } else if (c.length() == 2) { + c = "0" + c.substring(1, 2) + "0" + c.substring(0, 1); + } + return c; + } + + + public static byte[] byteMergerAll(byte[]... values) { + int length_byte = 0; + for (int i = 0; i < values.length; i++) { + length_byte += values[i].length; + } + byte[] all_byte = new byte[length_byte]; + int countLength = 0; + for (int i = 0; i < values.length; i++) { + byte[] b = values[i]; + System.arraycopy(b, 0, all_byte, countLength, b.length); + countLength += b.length; + } + return all_byte; + } + + public static String convertToHex(String str) { + char[] charArray = str.toCharArray(); + String hexString = ""; + for (char c : charArray) { + hexString += Integer.toHexString((int) c); + } + return hexString; + } + + public static String reverseHex(String hex) { + char[] charArray = hex.toCharArray(); + int length = charArray.length; + int times = length / 2; + for (int c1i = 0; c1i < times; c1i += 2) { + int c2i = c1i + 1; + char c1 = charArray[c1i]; + char c2 = charArray[c2i]; + int c3i = length - c1i - 2; + int c4i = length - c1i - 1; + charArray[c1i] = charArray[c3i]; + charArray[c2i] = charArray[c4i]; + charArray[c3i] = c1; + charArray[c4i] = c2; + } + return new String(charArray); + } + +} diff --git a/casic-server/src/main/java/com/casic/missiles/netty/Bytes.java b/casic-server/src/main/java/com/casic/missiles/netty/Bytes.java new file mode 100644 index 0000000..9031d19 --- /dev/null +++ b/casic-server/src/main/java/com/casic/missiles/netty/Bytes.java @@ -0,0 +1,73 @@ +package com.casic.missiles.netty; + +public class Bytes { + + private byte[] buffer = null; + + public Bytes() { + + } + + public Bytes append(byte b) { + byte[] nb = null; + if (buffer == null) { + nb = new byte[] { b }; + } else { + nb = new byte[buffer.length + 1]; + for (int i = 0; i < buffer.length; i++) { + nb[i] = buffer[i]; + } + nb[nb.length - 1] = b; + } + buffer = nb; + return this; + } + + public Bytes append(byte[] bs) { + if (bs == null) + return this; + byte[] nb = null; + if (buffer == null) { + buffer = bs; + } else { + nb = new byte[buffer.length + bs.length]; + for (int i = 0; i < buffer.length; i++) { + nb[i] = buffer[i]; + } + for (int i = 0; i < bs.length; i++) { + nb[buffer.length + i] = bs[i]; + } + buffer = nb; + } + return this; + } + + public byte[] toBytes() { + return buffer; + } + + public byte[] toBytes(int l) { + Bytes bs = new Bytes(); + + for (int i = 0; i < l; i++) { + if (i < buffer.length) { + bs.append(buffer[i]); + } else { + bs.append((byte) 0x00); + } + } + + return bs.toBytes(); + } + + public static void main(String[] args) { + Bytes bytes = new Bytes(); + int i = 0xf2; + byte a = 0x68; + byte[] b = new byte[] { 0x11, 0x22 }; + byte c = (byte) i; + byte[] r = bytes.append(a).append(b).append(c).toBytes(); + System.out.println(r); + } + +} diff --git a/casic-server/src/main/java/com/casic/missiles/netty/CacheUtils.java b/casic-server/src/main/java/com/casic/missiles/netty/CacheUtils.java new file mode 100644 index 0000000..8d2250a --- /dev/null +++ b/casic-server/src/main/java/com/casic/missiles/netty/CacheUtils.java @@ -0,0 +1,261 @@ +package com.casic.missiles.netty; + +import cn.hutool.core.util.ObjectUtil; +import lombok.extern.slf4j.Slf4j; +import net.jodah.expiringmap.ExpirationPolicy; +import net.jodah.expiringmap.ExpiringMap; +import org.springframework.stereotype.Component; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Set; +import java.util.concurrent.TimeUnit; + +@Slf4j +@Component +public class CacheUtils { + + //电压数据分包计数 + private volatile static int VOL_I = 0; + private volatile static boolean VOL_FLAG = false; + //PSD数据分包计数 + private volatile static int PSD_I = 0; + private volatile static boolean PSD_FLAG = false; + + /** + * 缓存电压信息 + */ + public static final ExpiringMap VOL_MAP = ExpiringMap.builder() + .maxSize(3000) + .expiration(5, TimeUnit.MINUTES) + .variableExpiration() + .expirationPolicy(ExpirationPolicy.ACCESSED) + .build(); + + /** + * 缓存PSD流信息 + */ + public static final ExpiringMap PSD_MAP = ExpiringMap.builder() + .maxSize(3000) + .expiration(5, TimeUnit.MINUTES) + .variableExpiration() +// .expirationListener((key,value) -> { +// System.out.println("key:" + key + "已过期……"); +// }) + .expirationPolicy(ExpirationPolicy.ACCESSED) + .build(); + + + + + public synchronized static String startVolMapCache() { + String msgs = ""; + if (CacheUtils.VOL_MAP.size() > 1) { + Set keys = CacheUtils.VOL_MAP.keySet(); + List keyInts = new ArrayList<>(keys); + Collections.sort(keyInts); + for (Integer i : keyInts) { + msgs += CacheUtils.VOL_MAP.get(i); + } + if (ObjectUtil.isNotEmpty(msgs)) { + CacheUtils.VOL_MAP.clear(); + //继续拆包处理 +// String msgLast = ""; +// if (msgs.indexOf(InstructCode.DAC_HEAD + InstructCode.DAC_HEAD_LENGTH) != -1) { +// if (msgs.length() < (msgs.indexOf(InstructCode.DAC_HEAD + InstructCode.DAC_HEAD_LENGTH) + 24 + InstructCode.DAC_DATA_LENGTH)) { +// msgLast = msgs.substring(msgs.indexOf(msgs.indexOf(InstructCode.DAC_HEAD + InstructCode.DAC_HEAD_LENGTH))); +// CacheUtils.VOL_MAP.put(keyInts.get(keyInts.size() - 1), msgLast); +// } +// } + } + + } + return msgs; + } + + public synchronized static String startPsdMapCache() { + String msgs = ""; + if (CacheUtils.PSD_MAP.size() > 1) { + Set keys = CacheUtils.PSD_MAP.keySet(); + List keyInts = new ArrayList<>(keys); + Collections.sort(keyInts); + for (Integer i : keyInts) { + msgs += CacheUtils.PSD_MAP.get(i); + } + if (ObjectUtil.isNotEmpty(msgs)) { + CacheUtils.PSD_MAP.clear(); + //继续拆包处理 +// String msgLast = ""; +// if (msgs.indexOf(InstructCode.PSD1_HEAD) != -1) { +// if (msgs.length() >= 24) { +// int totalLength = Integer.parseInt(ByteUtil.reverseHex(msgs.substring(msgs.indexOf(InstructCode.PSD1_HEAD) + 16, 24)), 16); +// if (msgs.length() < totalLength * 2) { +// msgLast = msgs.substring(msgs.indexOf(InstructCode.PSD1_HEAD)); +// CacheUtils.PSD_MAP.put(keyInts.get(keyInts.size() - 1), msgLast); +// } +// } +// } + } + } + return msgs; + } + + public static void main(String[] args) { + + String msg="f0f0a5a50300001102000c081b0070000000000059f372000d81030058d50000f0f0a5a50300001102000b081b0070000000000012c7a900bd7e03006ed3000055aa55aa4dfccf1a2c000000f0f0a5a50300001102000b081b00700000000000aa0b2e001080030084d40000"; + + msg="0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000055aa55aa8dfccf1a4e0000002331363337362c31363337362c31363337362c31363337362c31363337362c31363337362c3234342c32303030302c322c3130302c31363338332c302c323030302355aa55aa4dfccf1a2c000000f0f0a5a50300001102000c081b00700000000000df63650092810300e3d50000"; + +// msg="55aa55aa8dfccf1a4e0000002331363337362c31363337362c31363337362c31363337362c31363337362c31363337362c3234342c32303030302c322c3130302c31363338332c302c323030302355aa55aa4dfccf1a4c000000f0f0a5a50300001102000b081b00700000000000933b7100e47d0300a8d20000f0f0a5a50300001102000b081b00700000000000127c88003d810300a6d50000"; +// int totalLength = Integer.parseInt(ByteUtil.reverseHex(msg.substring(msg.indexOf(InstructCode.PSD1_HEAD) + 16, 24)), 16); + + +// msg="55aa55aa4dfccf1a2c000000f0f0a5a50300001102000b081b00700000000000b5560c00597f0300f6d3000055aa55aa4dfccf1a6c000000f0f0a5a50300001102000a081b007000000000005e8623007480030019d50000f0f0a5a50300001102000b081b0070000000000055083c006b7d030023d20000f0f0a5a50300001102000b081b007000000000007d3d3c00196103008fb7000055aa55aa5dfccf1a540000000000000000000000000000000000000000000000df2e000000000000000000000000000000000000000000000000000000000000e02e00000000000000000000000000000000000055aa55aa10fccf1a0c200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; + +// msg="55aa55aa4dfccf1a2c000000"; + + +// +// String[] msgArr = msg.split(InstructCode.PSD1_HEAD); +// if(msgArr.length>2){ +// for(int i=1;i= 24) { +//// log.info("原始msg--->"+msg); +// int totalLength = Integer.parseInt(ByteUtil.reverseHex(msg.substring(msg.indexOf(InstructCode.PSD1_HEAD) + 16, +// msg.indexOf(InstructCode.PSD1_HEAD) + 24)), 16); +// if (msg.length() >= totalLength * 2) { +// +// for (int i = 0; i < (msg.length() - (msg.indexOf(InstructCode.PSD1_HEAD) + 24)) / 64; i++) { +// String msgi = msg.substring((msg.indexOf(InstructCode.PSD1_HEAD) + 24) + i * 64, (msg.indexOf(InstructCode.PSD1_HEAD) + 24) + (i + 1) * 64); +// if (msgi.indexOf(InstructCode.PSD2_HEAD) == 0) { +// String gateOff = msgi.substring(16, 20); +// String baseline = msgi.substring(20, 24); +// String shortGate = msgi.substring(24, 28); +// String longGate = msgi.substring(28, 32); +// String qlong = msgi.substring(48, 56); +// String qshort = msgi.substring(56, 64); +// //toDo:算法计算 +// System.out.println("gateOff--->" + gateOff + +// "baseline--->" + baseline + +// "shortGate--->" + shortGate + +// "longGate--->" + longGate + +// "qlong--->" + qlong + +// "qshort--->" + qshort); +// } +// } +// } +// } + + } + + + public static void proccessData(String msg){ + //拆包处理 PSD数据 + if (msg.indexOf(InstructCode.PSD1_HEAD) != -1) { + + if (msg.length() >= 24) { +// log.info("原始msg--->"+msg); + int totalLength = Integer.parseInt(ByteUtil.reverseHex(msg.substring(msg.indexOf(InstructCode.PSD1_HEAD) + 16, + msg.indexOf(InstructCode.PSD1_HEAD) + 24)), 16); + if (msg.length() >= totalLength * 2) { + PSD_FLAG = false; + for (int i = 0; i < (msg.length() - (msg.indexOf(InstructCode.PSD1_HEAD) + 24)) / 64; i++) { + String msgi = msg.substring((msg.indexOf(InstructCode.PSD1_HEAD) + 24) + i * 64, (msg.indexOf(InstructCode.PSD1_HEAD) + 24) + (i + 1) * 64); + if (msgi.indexOf(InstructCode.PSD2_HEAD) == 0) { + String gateOff = msgi.substring(16, 20); + String baseline = msgi.substring(20, 24); + String shortGate = msgi.substring(24, 28); + String longGate = msgi.substring(28, 32); + String qlong = msgi.substring(48, 56); + String qshort = msgi.substring(56, 64); + //toDo:算法计算 + log.info("gateOff--->" + gateOff + + "baseline--->" + baseline + + "shortGate--->" + shortGate + + "longGate--->" + longGate + + "qlong--->" + qlong + + "qshort--->" + qshort); + } + } + } else { + //2.缺psd数据存入缓存处理 + PSD_FLAG = true; + PSD_I++; + CacheUtils.PSD_MAP.put(PSD_I, msg); +// log.info("psd加入数据:" + PSD_I + "------->" + msg); + } + } + } else if (PSD_FLAG) { + PSD_I++; + CacheUtils.PSD_MAP.put(PSD_I, msg); + String str = startPsdMapCache(); +// log.info("分包psd加入数据:" + PSD_I + "------->" + msg); + if (ObjectUtil.isNotEmpty(str) && str.length() >= 24) { +// log.info("**************strpsd---->" + str); + if (str.indexOf(InstructCode.PSD1_HEAD) != -1 && str.substring(str.indexOf(InstructCode.PSD1_HEAD) + 16).length() >= 8) { + int totalLength = Integer.parseInt(ByteUtil.reverseHex(str.substring(str.indexOf(InstructCode.PSD1_HEAD) + 16, + str.indexOf(InstructCode.PSD1_HEAD) + 24)), 16); + if (str.length() >= totalLength * 2) { + for (int i = 0; i < (str.length() - (str.indexOf(InstructCode.PSD1_HEAD) + 24)) / 64; i++) { + String msgi = str.substring((str.indexOf(InstructCode.PSD1_HEAD) + 24) + i * 64, (str.indexOf(InstructCode.PSD1_HEAD) + 24) + (i + 1) * 64); + if (msgi.indexOf(InstructCode.PSD2_HEAD) == 0) { + String gateOff = msgi.substring(16, 20); + String baseline = msgi.substring(20, 24); + String shortGate = msgi.substring(24, 28); + String longGate = msgi.substring(28, 32); + String qlong = msgi.substring(48, 56); + String qshort = msgi.substring(56, 64); + //toDo:算法计算 + log.info("分包:gateOff--->" + gateOff + + "baseline--->" + baseline + + "shortGate--->" + shortGate + + "longGate--->" + longGate + + "qlong--->" + qlong + + "qshort--->" + qshort); + } + } + } + } + } + } + //拆包处理 电压数据 + if (msg.indexOf(InstructCode.DAC_HEAD + InstructCode.DAC_HEAD_LENGTH) != -1) { + //完整包处理 + if (msg.length() >= (msg.indexOf(InstructCode.DAC_HEAD + InstructCode.DAC_HEAD_LENGTH) + 24 + InstructCode.DAC_DATA_LENGTH)) { + VOL_FLAG = false; + msg = msg.substring(msg.indexOf(InstructCode.DAC_HEAD + InstructCode.DAC_HEAD_LENGTH) + 24); + String[] volArr = InstructCode.getVolArr(msg); + //本设备只用到第6通道 + String vol6 = volArr[5]; + log.info("第6通道电压值-------->" + vol6); + } else { + //分包处理,先放到缓存 + VOL_FLAG = true; + VOL_I++; + CacheUtils.VOL_MAP.put(VOL_I, msg); + } + } else if (VOL_FLAG) { + VOL_I++; + CacheUtils.VOL_MAP.put(VOL_I, msg); + String str = startVolMapCache(); + if (ObjectUtil.isNotEmpty(str) && + str.length() >= (str.indexOf(InstructCode.DAC_HEAD + InstructCode.DAC_HEAD_LENGTH) + 24 + InstructCode.DAC_DATA_LENGTH)) { + VOL_FLAG = false; + str = str.substring(str.indexOf(InstructCode.DAC_HEAD + InstructCode.DAC_HEAD_LENGTH) + 24); + String[] volArr = InstructCode.getVolArr(str); + //本设备只用到第6通道 + String vol6 = volArr[5]; + log.info("分包第6通道电压值-------->" + vol6); + } + } + + } + +} \ No newline at end of file diff --git a/casic-server/src/main/java/com/casic/missiles/netty/HjtDecoder.java b/casic-server/src/main/java/com/casic/missiles/netty/HjtDecoder.java new file mode 100644 index 0000000..2649988 --- /dev/null +++ b/casic-server/src/main/java/com/casic/missiles/netty/HjtDecoder.java @@ -0,0 +1,24 @@ +package com.casic.missiles.netty; + +import io.netty.buffer.ByteBuf; +import io.netty.buffer.ByteBufUtil; +import io.netty.channel.ChannelHandlerContext; +import io.netty.handler.codec.MessageToMessageDecoder; +import lombok.extern.slf4j.Slf4j; + +import java.nio.charset.Charset; +import java.util.List; + +/** + * @description: 将从接口取到的数据编码 + * @author: Stone + * @create: 2019-01-11 15:15 + **/ +@Slf4j +public class HjtDecoder extends MessageToMessageDecoder { + @Override + protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List out) throws Exception { +// log.info("hexBytes : " + ByteBufUtil.hexDump(msg) + ";" + " String : " + msg.toString(Charset.defaultCharset())); + out.add(ByteBufUtil.hexDump(msg)); + } +} diff --git a/casic-server/src/main/java/com/casic/missiles/netty/InstructCode.java b/casic-server/src/main/java/com/casic/missiles/netty/InstructCode.java index 0732347..5c61759 100644 --- a/casic-server/src/main/java/com/casic/missiles/netty/InstructCode.java +++ b/casic-server/src/main/java/com/casic/missiles/netty/InstructCode.java @@ -3,13 +3,13 @@ import org.springframework.stereotype.Component; @Component -public class InstructCode { +public class InstructCode { /** * 设备初始化时需要依次发送1~16号指令 */ public static String[] InitCodeArr = { - "1", "设置高压", "set set_voltage 4 1.2\\n", + "1", "设置高压", "set set_voltage 6 1.2\\n", "2", "复位adc", "cmd set_adc_delay_rst 1 0\\n", "3", "逻辑复位", "cmd set_logic_rst 1 0\\n", "4", "设置波形采样触发方式为下降沿", "cmd set_raw_trig_sel_adc2 3 0\\n", @@ -30,30 +30,51 @@ /** * 加高压指令 */ - public static final String DAC_UP="cmd dac_up 0 0\\n"; + public static final String DAC_UP = "cmd dac_up 0 0\\n"; /** * 关闭高压指令 */ - public static final String DAC_DOWN="cmd dac_down 0 0\\n"; + public static final String DAC_DOWN = "cmd dac_down 0 0\\n"; /** + * ` * 电压采样帧头 */ - public static final String DAC_HEAD="AA55AA551ACFFC5D"; +// public static final String DAC_HEAD="AA55AA551ACFFC5D"; + public static final String DAC_HEAD = "55aa55aa5dfccf1a"; + public static final String DAC_HEAD_LENGTH = "54000000"; + public static final int DAC_DATA_LENGTH = 144; /** * 波形采样帧头 */ - public static final String WAVE_HEAD="AA55AA551ACFFC3D"; +// public static final String WAVE_HEAD="AA55AA551ACFFC3D"; + public static final String WAVE_HEAD = "55aa55aa3dfccf1a"; /** * PSD1采样帧头 */ - public static final String PSD1_HEAD="AA55AA551ACFFC4D"; +// public static final String PSD1_HEAD="AA55AA551ACFFC4D"; + public static final String PSD1_HEAD = "55aa55aa4dfccf1a"; /** * PSD2采样帧头 */ - public static final String PSD2_HEAD="F0F0A5A5"; +// public static final String PSD2_HEAD="F0F0A5A5"; + public static final String PSD2_HEAD = "f0f0a5a5"; + + + public static String[] getVolArr(String msg) { + String[] volArr = new String[24]; + for (int j = 0; j < 24; j++) { + //前16为电压值 + if (j < 16) + volArr[j] = ByteUtil.reverseHex(msg.substring(j * 8, (j + 1) * 8)); + //后8为高低位值 + else + volArr[j] = ByteUtil.reverseHex(msg.substring(128 + (j - 16) * 2, 128 + (j - 15) * 2)); + } + return volArr; + } } diff --git a/casic-server/pom.xml b/casic-server/pom.xml index 8c25977..805cc22 100644 --- a/casic-server/pom.xml +++ b/casic-server/pom.xml @@ -61,6 +61,11 @@ jna 3.0.0 + + net.jodah + expiringmap + 0.5.8 + diff --git a/casic-server/src/main/java/com/casic/missiles/netty/ByteUtil.java b/casic-server/src/main/java/com/casic/missiles/netty/ByteUtil.java new file mode 100644 index 0000000..55c951e --- /dev/null +++ b/casic-server/src/main/java/com/casic/missiles/netty/ByteUtil.java @@ -0,0 +1,517 @@ +package com.casic.missiles.netty; + +import org.springframework.stereotype.Component; + +@Component +public class ByteUtil { + + /* BCD码转换为数值或字符 */ + + /** + * 单一字节中选择指定位bcd码转换为整数 + * 如ByteUtil.bcdToInt((byte) 0x33, 5, 4) = 3 + * + * @param b + * @param highBit 高位,最大7 + * @param lowBit 低位,从0开始 + * @return + */ + public static int bcdToInt(byte b, int highBit, int lowBit) { + int g = highBit - lowBit + 1; + int a = ((int) Math.pow(2, g) - 1) << lowBit; + int b2 = (b & a) >> lowBit; + if (b2 < 0 || b2 >= 10) { + return -1; + } + return b2; + } + + /** + * bcd码转换为整数 错误则返回-1 + * 如ByteUtil.bcdToInt((byte) 0x73) = 73 + * + * @param b 输入字节,默认4位一组,先高后低 + * @return + */ + public static int bcdToInt(byte b) { + int pl = 0x0f; + int ph = 0xf0; + + int h = (ph & b) >> 4; + int l = (pl & b); + + if (h >= 10 || l >= 10) { + return -1; + } + return h * 10 + l; + } + + /** + * bcd码转换为long型整数 + * 如字节数组{0x25, 0x23}转换为2523 + * + * @param + * @return + */ + public static long bcdToLong(byte[] bytes) { + if (ByteUtil.bcdToString(bytes).equals("") == false) + return Long.valueOf(ByteUtil.bcdToString(bytes)).longValue(); + else + return -1; + } + + + /** + * bcd码转换为字符串 按字节顺序 高位在前低位在后 + * 如字节数组{0x01, 0x02, 0x03}转换为"010203" + * + * @param bytes + * @return + */ + public static String bcdToString(byte[] bytes) { + StringBuilder sb = new StringBuilder(); + + for (int i = 0; i < bytes.length; i++) { + if (ByteUtil.bcdToInt(bytes[i], 7, 4) != -1 && ByteUtil.bcdToInt(bytes[i], 3, 0) != -1) { + sb.append(ByteUtil.bcdToInt(bytes[i], 7, 4)); + sb.append(ByteUtil.bcdToInt(bytes[i], 3, 0)); + } + } + + return sb.toString(); + } + + + /* BIN码转换为数值或字符 */ + + /** + * 单一字节中选择指定位bin码转换为整数 + * 如ByteUtil.binToInt((byte) 0xA3, 7, 7) = 1 + * + * @param b + * @param highBit 高位,最大7 + * @param lowBit 低位,从0开始 + * @return + */ + public static int binToInt(byte b, int highBit, int lowBit) throws RuntimeException { + int g = highBit - lowBit + 1; + int a = ((int) Math.pow(2, g) - 1) << lowBit; + int b2 = (b & a) >> lowBit; + if (b2 < 0 || b2 >= 16) { + throw new RuntimeException(); + } + return b2; + } + + /** + * BIN字节转换为整数 + * 如ByteUtil.bcdToInt((byte) 0x7B) = 123 + * + * @param b + * @return + */ + public static int binToInt(byte b) { + int i1 = 0x80 & b; + if (i1 == 0x80) { + int i2 = 0x7f & b; + return 128 + i2; + } else { + return b; + } + } + + /** + * BIN码转换为整数 高位在前低位在后 + * 如ByteUtil.bcdToInt(new byte[] {(byte) 0x7B, 0x22}) = 31522 + * + * @param bytes + * @return + */ + public static int binToInt(byte[] bytes) { + int i10 = 0; + if (null != bytes && bytes.length != 0) { + for (int i = 0; i < bytes.length; i++) { + i10 = i10 + new Double(Math.pow(16, (bytes.length - i - 1) * 2)).intValue() * ByteUtil.binToInt(bytes[i]); + } + } + return i10; + } + + /** + * BIN码转换为LONG型整数 高位在前低位在后 + * 如ByteUtil.bcdToLong(new byte[] {(byte) 0x7B, 0x22, 0x32, 0x9A}) = 2065838746 + * + * @param bytes + * @return + */ + public static int binToLong(byte[] bytes) { + int l10 = 0; + if (null != bytes && bytes.length != 0) { + for (int i = 0; i < bytes.length; i++) { + l10 = l10 + new Double(Math.pow(16, (bytes.length - i - 1) * 2)).intValue() * ByteUtil.binToInt(bytes[i]); + } + } + return l10; + } + + /** + * 字节数组转换为16进制字符串 按字节数组顺序转换 + * 如字节数组{0x21, 0x06, 0x01, 0x04, 0x40, (byte) 0xE2, 0x01}转换为"2106010440E201" + * + * @param bytes + * @return + */ + public static String binToHexString(byte[] bytes) { + StringBuilder sb = new StringBuilder(); + + for (int i = 0; i < bytes.length; i++) { + sb.append(Integer.toHexString(binToInt((byte) ((bytes[i] & 0xf0) >> 4))).toUpperCase()); + sb.append(Integer.toHexString(binToInt((byte) (bytes[i] & 0x0f))).toUpperCase()); + } + + return sb.toString(); + } + + + /** + * BIN输出为二进制字符串 + * 如字节数组{0x01, 0x38}转换为字符串"0000000100111000" + * + * @param bytes + * @return + */ + public static String binToBinString(byte[] bytes) { + StringBuilder sb = new StringBuilder(); + + for (int i = 0; i < bytes.length; i++) { + String s = Integer.toBinaryString(binToInt(bytes[i])); + while (s.length() < 8) { + s = "0" + s; + } + sb.append(s); + } + + return sb.toString(); + } + + + /* int转换为BCD码或BIN码 */ + + /** + * 两位整数转换成一个字节的BCD码 + * 大于100的整数取其十位和个位 十位在前个位在后 + * 如79转换为0x79 + * + * @param i + * @return + */ + public static byte intToBcd(int i) { + byte b = 0x00; + while (i > 99) { + i = i % 100; + } + + int l = i % 10; + int h = i / 10; + + b = (byte) (h << 4 | l); + + return b; + } + + /** + * 整数转换为字节数组 高位在前低位在后 + * 如 2379转换为0x23, 0x79 + * + * @param i + * @return + */ + public static byte[] intToBcds(long l) { + String str = String.valueOf(l); + Bytes bytes = new Bytes(); + + if (str.length() % 2 == 1) { + str = "0" + str; + } + + for (int i = 0; i < str.length() - 1; i = i + 2) { + String s = str.substring(i, i + 2); + bytes.append(ByteUtil.intToBcd(Integer.parseInt(s))); + } + + return bytes.toBytes(); + } + + /** + * 整数转换为指定字节数的BCD码数组 高位在前低位在后 从低位开始计算长度 超出部分忽略 不足部分在高位补0 + * 如 3013479指定2字节转换为0x34, 0x79, 指定6字节转换为0x00, 0x00, 0x03, 0x01, 0x34, 0x79 + * + * @param i + * @param length + * @return + */ + public static byte[] intToBcds(long l, int length) { + byte[] bts = intToBcds(l); + if (bts.length == length) { + return bts; + } else { + byte[] bytes = new byte[length]; + for (int j = length - 1; j >= 0; j--) { + if (j + bts.length - length >= 0) { + bytes[j] = bts[j + bts.length - length]; + } else { + bytes[j] = 0x00; + } + } + + return bytes; + } + } + + /** + * 整数转换为指定字节数的BCD码数组 低位在前高位在后 从低位开始计算长度 超出部分忽略 不足部分在高位补0 + * 如 3013479指定2字节转换为0x79, 0x34, 指定6字节转换为0x79, 0x34, 0x01, 0x03, 0x00, 0x00 + * + * @param i + * @param length + * @return + */ + public static byte[] intToBcdsHL(long l, int length) { + Bytes bs = new Bytes(); + + for (int i = 0; i < length; i++) { + int low = (int) (l % 100); + bs.append(ByteUtil.intToBcd(low)); + l = l / 100; + } + + return bs.toBytes(); + } + + /** + * 整数转换为BIN码字节数组 高位在前低位在后 + * 如113479转换为0x01, 0xBB, 0x47 + * + * @param l + * @return + */ + public static byte[] intToBins(long l) { + String str = Long.toHexString(l); + if (str.length() % 2 == 1) { + str = "0" + str; + } + + Bytes bytes = new Bytes(); + for (int i = 0; i < str.length() - 1; i = i + 2) { + String s = str.substring(i, i + 2); + bytes.append((byte) Integer.parseInt(s, 16)); + } + + return bytes.toBytes(); + } + + /** + * 整数转换为指定字节数的BIN码数组 高位在前低位在后 从低位开始计算长度 超出部分忽略 不足部分在高位补0 + * 如 3013479指定2字节转换为0xFB, 0x67, 指定6字节转换为0x00, 0x00, 0x00, 0x2D, 0xFB, 0x67 + * + * @param l + * @param length + * @return + */ + public static byte[] intToBins(long l, int length) { + byte[] bts = intToBins(l); + if (bts.length == length) { + return bts; + } else { + byte[] bytes = new byte[length]; + for (int j = length - 1; j >= 0; j--) { + if (j + bts.length - length >= 0) { + bytes[j] = bts[j + bts.length - length]; + } else { + bytes[j] = 0x00; + } + } + + return bytes; + } + } + + + /* 16进制字符串转换为字节数组 */ + + /** + * 16进制字符串顺序转换为字节数组 + * 如字符串"13810411703"转换为字节数组{0x01, 0x38, 0x10, 0x41, 0x17, 0x03} + * + * @param str + * @return + */ + public static byte[] hexStringToBytes(String str) { + if (str.length() % 2 == 1) { + str = "0" + str; + } + + Bytes bytes = new Bytes(); + for (int i = 0; i < str.length() - 1; i = i + 2) { + String s = str.substring(i, i + 2); + bytes.append((byte) Integer.parseInt(s, 16)); + } + + return bytes.toBytes(); + } + + /** + * 16进制字符串顺序转换为指定字节数的BIN码数组 从低位开始计算长度 超出部分忽略 不足部分在高位补0 + * 如字符串"13810411703"转换为3字节数组{0x41, 0x17, 0x03}, 指定8字节转换为{0x00, 0x00, 0x01, 0x38, 0x10, 0x41, 0x17, 0x03} + * + * @param str + * @param length + * @return + */ + public static byte[] hexStringToBytes(String str, int length) { + byte[] bts = hexStringToBytes(str); + if (bts.length == length) { + return bts; + } else { + byte[] bytes = new byte[length]; + for (int j = length - 1; j >= 0; j--) { + if (j + bts.length - length >= 0) { + bytes[j] = bts[j + bts.length - length]; + } else { + bytes[j] = 0x00; + } + } + + return bytes; + } + } + + + /* 十进制BCD码字符串转换为字节数组 */ + + /** + * 十进制BCD码字符串顺序转换为字节数组 + * 如字符串"13810411703"转换为字节数组{0x01, 0x38, 0x10, 0x41, 0x17, 0x03} + * + * @param str + * @return + */ + public static byte[] bcdStringToBytes(String str) { + if (str.length() % 2 == 1) { + str = "0" + str; + } + + Bytes bytes = new Bytes(); + for (int i = 0; i < str.length() - 1; i = i + 2) { + String s = str.substring(i, i + 2); + bytes.append(ByteUtil.intToBcd(Integer.parseInt(s))); + } + + return bytes.toBytes(); + } + + /** + * 十进制BCD码字符串顺序转换为指定字节数的BCD码数组 从低位开始计算长度 超出部分忽略 不足部分在高位补0 + * 如字符串"13810411703"转换为3字节数组{0x41, 0x17, 0x03}, 指定8字节转换为{0x00, 0x00, 0x01, 0x38, 0x10, 0x41, 0x17, 0x03} + * + * @param str + * @param length + * @return + */ + public static byte[] bcdStringToBytes(String str, int length) { + byte[] bts = bcdStringToBytes(str); + if (bts.length == length) { + return bts; + } else { + byte[] bytes = new byte[length]; + for (int j = length - 1; j >= 0; j--) { + if (j + bts.length - length >= 0) { + bytes[j] = bts[j + bts.length - length]; + } else { + bytes[j] = 0x00; + } + } + + return bytes; + } + } + + public static String buildCRC16(byte[] data) { + byte[] buf = new byte[data.length];// 存储需要产生校验码的数据 + for (int i = 0; i < data.length; i++) { + buf[i] = data[i]; + } + int len = buf.length; + int crc = 0xFFFF;//16位 + for (int pos = 0; pos < len; pos++) { + if (buf[pos] < 0) { + crc ^= (int) buf[pos] + 256; // XOR byte into least sig. byte of + // crc + } else { + crc ^= (int) buf[pos]; // XOR byte into least sig. byte of crc + } + for (int i = 8; i != 0; i--) { // Loop over each bit + if ((crc & 0x0001) != 0) { // If the LSB is set + crc >>= 1; // Shift right and XOR 0xA001 + crc ^= 0xA001; + } else + // Else LSB is not set + crc >>= 1; // Just shift right + } + } + String c = Integer.toHexString(crc); + if (c.length() == 4) { + c = c.substring(2, 4) + c.substring(0, 2); + } else if (c.length() == 3) { + c = "0" + c; + c = c.substring(2, 4) + c.substring(0, 2); + } else if (c.length() == 2) { + c = "0" + c.substring(1, 2) + "0" + c.substring(0, 1); + } + return c; + } + + + public static byte[] byteMergerAll(byte[]... values) { + int length_byte = 0; + for (int i = 0; i < values.length; i++) { + length_byte += values[i].length; + } + byte[] all_byte = new byte[length_byte]; + int countLength = 0; + for (int i = 0; i < values.length; i++) { + byte[] b = values[i]; + System.arraycopy(b, 0, all_byte, countLength, b.length); + countLength += b.length; + } + return all_byte; + } + + public static String convertToHex(String str) { + char[] charArray = str.toCharArray(); + String hexString = ""; + for (char c : charArray) { + hexString += Integer.toHexString((int) c); + } + return hexString; + } + + public static String reverseHex(String hex) { + char[] charArray = hex.toCharArray(); + int length = charArray.length; + int times = length / 2; + for (int c1i = 0; c1i < times; c1i += 2) { + int c2i = c1i + 1; + char c1 = charArray[c1i]; + char c2 = charArray[c2i]; + int c3i = length - c1i - 2; + int c4i = length - c1i - 1; + charArray[c1i] = charArray[c3i]; + charArray[c2i] = charArray[c4i]; + charArray[c3i] = c1; + charArray[c4i] = c2; + } + return new String(charArray); + } + +} diff --git a/casic-server/src/main/java/com/casic/missiles/netty/Bytes.java b/casic-server/src/main/java/com/casic/missiles/netty/Bytes.java new file mode 100644 index 0000000..9031d19 --- /dev/null +++ b/casic-server/src/main/java/com/casic/missiles/netty/Bytes.java @@ -0,0 +1,73 @@ +package com.casic.missiles.netty; + +public class Bytes { + + private byte[] buffer = null; + + public Bytes() { + + } + + public Bytes append(byte b) { + byte[] nb = null; + if (buffer == null) { + nb = new byte[] { b }; + } else { + nb = new byte[buffer.length + 1]; + for (int i = 0; i < buffer.length; i++) { + nb[i] = buffer[i]; + } + nb[nb.length - 1] = b; + } + buffer = nb; + return this; + } + + public Bytes append(byte[] bs) { + if (bs == null) + return this; + byte[] nb = null; + if (buffer == null) { + buffer = bs; + } else { + nb = new byte[buffer.length + bs.length]; + for (int i = 0; i < buffer.length; i++) { + nb[i] = buffer[i]; + } + for (int i = 0; i < bs.length; i++) { + nb[buffer.length + i] = bs[i]; + } + buffer = nb; + } + return this; + } + + public byte[] toBytes() { + return buffer; + } + + public byte[] toBytes(int l) { + Bytes bs = new Bytes(); + + for (int i = 0; i < l; i++) { + if (i < buffer.length) { + bs.append(buffer[i]); + } else { + bs.append((byte) 0x00); + } + } + + return bs.toBytes(); + } + + public static void main(String[] args) { + Bytes bytes = new Bytes(); + int i = 0xf2; + byte a = 0x68; + byte[] b = new byte[] { 0x11, 0x22 }; + byte c = (byte) i; + byte[] r = bytes.append(a).append(b).append(c).toBytes(); + System.out.println(r); + } + +} diff --git a/casic-server/src/main/java/com/casic/missiles/netty/CacheUtils.java b/casic-server/src/main/java/com/casic/missiles/netty/CacheUtils.java new file mode 100644 index 0000000..8d2250a --- /dev/null +++ b/casic-server/src/main/java/com/casic/missiles/netty/CacheUtils.java @@ -0,0 +1,261 @@ +package com.casic.missiles.netty; + +import cn.hutool.core.util.ObjectUtil; +import lombok.extern.slf4j.Slf4j; +import net.jodah.expiringmap.ExpirationPolicy; +import net.jodah.expiringmap.ExpiringMap; +import org.springframework.stereotype.Component; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Set; +import java.util.concurrent.TimeUnit; + +@Slf4j +@Component +public class CacheUtils { + + //电压数据分包计数 + private volatile static int VOL_I = 0; + private volatile static boolean VOL_FLAG = false; + //PSD数据分包计数 + private volatile static int PSD_I = 0; + private volatile static boolean PSD_FLAG = false; + + /** + * 缓存电压信息 + */ + public static final ExpiringMap VOL_MAP = ExpiringMap.builder() + .maxSize(3000) + .expiration(5, TimeUnit.MINUTES) + .variableExpiration() + .expirationPolicy(ExpirationPolicy.ACCESSED) + .build(); + + /** + * 缓存PSD流信息 + */ + public static final ExpiringMap PSD_MAP = ExpiringMap.builder() + .maxSize(3000) + .expiration(5, TimeUnit.MINUTES) + .variableExpiration() +// .expirationListener((key,value) -> { +// System.out.println("key:" + key + "已过期……"); +// }) + .expirationPolicy(ExpirationPolicy.ACCESSED) + .build(); + + + + + public synchronized static String startVolMapCache() { + String msgs = ""; + if (CacheUtils.VOL_MAP.size() > 1) { + Set keys = CacheUtils.VOL_MAP.keySet(); + List keyInts = new ArrayList<>(keys); + Collections.sort(keyInts); + for (Integer i : keyInts) { + msgs += CacheUtils.VOL_MAP.get(i); + } + if (ObjectUtil.isNotEmpty(msgs)) { + CacheUtils.VOL_MAP.clear(); + //继续拆包处理 +// String msgLast = ""; +// if (msgs.indexOf(InstructCode.DAC_HEAD + InstructCode.DAC_HEAD_LENGTH) != -1) { +// if (msgs.length() < (msgs.indexOf(InstructCode.DAC_HEAD + InstructCode.DAC_HEAD_LENGTH) + 24 + InstructCode.DAC_DATA_LENGTH)) { +// msgLast = msgs.substring(msgs.indexOf(msgs.indexOf(InstructCode.DAC_HEAD + InstructCode.DAC_HEAD_LENGTH))); +// CacheUtils.VOL_MAP.put(keyInts.get(keyInts.size() - 1), msgLast); +// } +// } + } + + } + return msgs; + } + + public synchronized static String startPsdMapCache() { + String msgs = ""; + if (CacheUtils.PSD_MAP.size() > 1) { + Set keys = CacheUtils.PSD_MAP.keySet(); + List keyInts = new ArrayList<>(keys); + Collections.sort(keyInts); + for (Integer i : keyInts) { + msgs += CacheUtils.PSD_MAP.get(i); + } + if (ObjectUtil.isNotEmpty(msgs)) { + CacheUtils.PSD_MAP.clear(); + //继续拆包处理 +// String msgLast = ""; +// if (msgs.indexOf(InstructCode.PSD1_HEAD) != -1) { +// if (msgs.length() >= 24) { +// int totalLength = Integer.parseInt(ByteUtil.reverseHex(msgs.substring(msgs.indexOf(InstructCode.PSD1_HEAD) + 16, 24)), 16); +// if (msgs.length() < totalLength * 2) { +// msgLast = msgs.substring(msgs.indexOf(InstructCode.PSD1_HEAD)); +// CacheUtils.PSD_MAP.put(keyInts.get(keyInts.size() - 1), msgLast); +// } +// } +// } + } + } + return msgs; + } + + public static void main(String[] args) { + + String msg="f0f0a5a50300001102000c081b0070000000000059f372000d81030058d50000f0f0a5a50300001102000b081b0070000000000012c7a900bd7e03006ed3000055aa55aa4dfccf1a2c000000f0f0a5a50300001102000b081b00700000000000aa0b2e001080030084d40000"; + + msg="0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000055aa55aa8dfccf1a4e0000002331363337362c31363337362c31363337362c31363337362c31363337362c31363337362c3234342c32303030302c322c3130302c31363338332c302c323030302355aa55aa4dfccf1a2c000000f0f0a5a50300001102000c081b00700000000000df63650092810300e3d50000"; + +// msg="55aa55aa8dfccf1a4e0000002331363337362c31363337362c31363337362c31363337362c31363337362c31363337362c3234342c32303030302c322c3130302c31363338332c302c323030302355aa55aa4dfccf1a4c000000f0f0a5a50300001102000b081b00700000000000933b7100e47d0300a8d20000f0f0a5a50300001102000b081b00700000000000127c88003d810300a6d50000"; +// int totalLength = Integer.parseInt(ByteUtil.reverseHex(msg.substring(msg.indexOf(InstructCode.PSD1_HEAD) + 16, 24)), 16); + + +// msg="55aa55aa4dfccf1a2c000000f0f0a5a50300001102000b081b00700000000000b5560c00597f0300f6d3000055aa55aa4dfccf1a6c000000f0f0a5a50300001102000a081b007000000000005e8623007480030019d50000f0f0a5a50300001102000b081b0070000000000055083c006b7d030023d20000f0f0a5a50300001102000b081b007000000000007d3d3c00196103008fb7000055aa55aa5dfccf1a540000000000000000000000000000000000000000000000df2e000000000000000000000000000000000000000000000000000000000000e02e00000000000000000000000000000000000055aa55aa10fccf1a0c200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; + +// msg="55aa55aa4dfccf1a2c000000"; + + +// +// String[] msgArr = msg.split(InstructCode.PSD1_HEAD); +// if(msgArr.length>2){ +// for(int i=1;i= 24) { +//// log.info("原始msg--->"+msg); +// int totalLength = Integer.parseInt(ByteUtil.reverseHex(msg.substring(msg.indexOf(InstructCode.PSD1_HEAD) + 16, +// msg.indexOf(InstructCode.PSD1_HEAD) + 24)), 16); +// if (msg.length() >= totalLength * 2) { +// +// for (int i = 0; i < (msg.length() - (msg.indexOf(InstructCode.PSD1_HEAD) + 24)) / 64; i++) { +// String msgi = msg.substring((msg.indexOf(InstructCode.PSD1_HEAD) + 24) + i * 64, (msg.indexOf(InstructCode.PSD1_HEAD) + 24) + (i + 1) * 64); +// if (msgi.indexOf(InstructCode.PSD2_HEAD) == 0) { +// String gateOff = msgi.substring(16, 20); +// String baseline = msgi.substring(20, 24); +// String shortGate = msgi.substring(24, 28); +// String longGate = msgi.substring(28, 32); +// String qlong = msgi.substring(48, 56); +// String qshort = msgi.substring(56, 64); +// //toDo:算法计算 +// System.out.println("gateOff--->" + gateOff + +// "baseline--->" + baseline + +// "shortGate--->" + shortGate + +// "longGate--->" + longGate + +// "qlong--->" + qlong + +// "qshort--->" + qshort); +// } +// } +// } +// } + + } + + + public static void proccessData(String msg){ + //拆包处理 PSD数据 + if (msg.indexOf(InstructCode.PSD1_HEAD) != -1) { + + if (msg.length() >= 24) { +// log.info("原始msg--->"+msg); + int totalLength = Integer.parseInt(ByteUtil.reverseHex(msg.substring(msg.indexOf(InstructCode.PSD1_HEAD) + 16, + msg.indexOf(InstructCode.PSD1_HEAD) + 24)), 16); + if (msg.length() >= totalLength * 2) { + PSD_FLAG = false; + for (int i = 0; i < (msg.length() - (msg.indexOf(InstructCode.PSD1_HEAD) + 24)) / 64; i++) { + String msgi = msg.substring((msg.indexOf(InstructCode.PSD1_HEAD) + 24) + i * 64, (msg.indexOf(InstructCode.PSD1_HEAD) + 24) + (i + 1) * 64); + if (msgi.indexOf(InstructCode.PSD2_HEAD) == 0) { + String gateOff = msgi.substring(16, 20); + String baseline = msgi.substring(20, 24); + String shortGate = msgi.substring(24, 28); + String longGate = msgi.substring(28, 32); + String qlong = msgi.substring(48, 56); + String qshort = msgi.substring(56, 64); + //toDo:算法计算 + log.info("gateOff--->" + gateOff + + "baseline--->" + baseline + + "shortGate--->" + shortGate + + "longGate--->" + longGate + + "qlong--->" + qlong + + "qshort--->" + qshort); + } + } + } else { + //2.缺psd数据存入缓存处理 + PSD_FLAG = true; + PSD_I++; + CacheUtils.PSD_MAP.put(PSD_I, msg); +// log.info("psd加入数据:" + PSD_I + "------->" + msg); + } + } + } else if (PSD_FLAG) { + PSD_I++; + CacheUtils.PSD_MAP.put(PSD_I, msg); + String str = startPsdMapCache(); +// log.info("分包psd加入数据:" + PSD_I + "------->" + msg); + if (ObjectUtil.isNotEmpty(str) && str.length() >= 24) { +// log.info("**************strpsd---->" + str); + if (str.indexOf(InstructCode.PSD1_HEAD) != -1 && str.substring(str.indexOf(InstructCode.PSD1_HEAD) + 16).length() >= 8) { + int totalLength = Integer.parseInt(ByteUtil.reverseHex(str.substring(str.indexOf(InstructCode.PSD1_HEAD) + 16, + str.indexOf(InstructCode.PSD1_HEAD) + 24)), 16); + if (str.length() >= totalLength * 2) { + for (int i = 0; i < (str.length() - (str.indexOf(InstructCode.PSD1_HEAD) + 24)) / 64; i++) { + String msgi = str.substring((str.indexOf(InstructCode.PSD1_HEAD) + 24) + i * 64, (str.indexOf(InstructCode.PSD1_HEAD) + 24) + (i + 1) * 64); + if (msgi.indexOf(InstructCode.PSD2_HEAD) == 0) { + String gateOff = msgi.substring(16, 20); + String baseline = msgi.substring(20, 24); + String shortGate = msgi.substring(24, 28); + String longGate = msgi.substring(28, 32); + String qlong = msgi.substring(48, 56); + String qshort = msgi.substring(56, 64); + //toDo:算法计算 + log.info("分包:gateOff--->" + gateOff + + "baseline--->" + baseline + + "shortGate--->" + shortGate + + "longGate--->" + longGate + + "qlong--->" + qlong + + "qshort--->" + qshort); + } + } + } + } + } + } + //拆包处理 电压数据 + if (msg.indexOf(InstructCode.DAC_HEAD + InstructCode.DAC_HEAD_LENGTH) != -1) { + //完整包处理 + if (msg.length() >= (msg.indexOf(InstructCode.DAC_HEAD + InstructCode.DAC_HEAD_LENGTH) + 24 + InstructCode.DAC_DATA_LENGTH)) { + VOL_FLAG = false; + msg = msg.substring(msg.indexOf(InstructCode.DAC_HEAD + InstructCode.DAC_HEAD_LENGTH) + 24); + String[] volArr = InstructCode.getVolArr(msg); + //本设备只用到第6通道 + String vol6 = volArr[5]; + log.info("第6通道电压值-------->" + vol6); + } else { + //分包处理,先放到缓存 + VOL_FLAG = true; + VOL_I++; + CacheUtils.VOL_MAP.put(VOL_I, msg); + } + } else if (VOL_FLAG) { + VOL_I++; + CacheUtils.VOL_MAP.put(VOL_I, msg); + String str = startVolMapCache(); + if (ObjectUtil.isNotEmpty(str) && + str.length() >= (str.indexOf(InstructCode.DAC_HEAD + InstructCode.DAC_HEAD_LENGTH) + 24 + InstructCode.DAC_DATA_LENGTH)) { + VOL_FLAG = false; + str = str.substring(str.indexOf(InstructCode.DAC_HEAD + InstructCode.DAC_HEAD_LENGTH) + 24); + String[] volArr = InstructCode.getVolArr(str); + //本设备只用到第6通道 + String vol6 = volArr[5]; + log.info("分包第6通道电压值-------->" + vol6); + } + } + + } + +} \ No newline at end of file diff --git a/casic-server/src/main/java/com/casic/missiles/netty/HjtDecoder.java b/casic-server/src/main/java/com/casic/missiles/netty/HjtDecoder.java new file mode 100644 index 0000000..2649988 --- /dev/null +++ b/casic-server/src/main/java/com/casic/missiles/netty/HjtDecoder.java @@ -0,0 +1,24 @@ +package com.casic.missiles.netty; + +import io.netty.buffer.ByteBuf; +import io.netty.buffer.ByteBufUtil; +import io.netty.channel.ChannelHandlerContext; +import io.netty.handler.codec.MessageToMessageDecoder; +import lombok.extern.slf4j.Slf4j; + +import java.nio.charset.Charset; +import java.util.List; + +/** + * @description: 将从接口取到的数据编码 + * @author: Stone + * @create: 2019-01-11 15:15 + **/ +@Slf4j +public class HjtDecoder extends MessageToMessageDecoder { + @Override + protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List out) throws Exception { +// log.info("hexBytes : " + ByteBufUtil.hexDump(msg) + ";" + " String : " + msg.toString(Charset.defaultCharset())); + out.add(ByteBufUtil.hexDump(msg)); + } +} diff --git a/casic-server/src/main/java/com/casic/missiles/netty/InstructCode.java b/casic-server/src/main/java/com/casic/missiles/netty/InstructCode.java index 0732347..5c61759 100644 --- a/casic-server/src/main/java/com/casic/missiles/netty/InstructCode.java +++ b/casic-server/src/main/java/com/casic/missiles/netty/InstructCode.java @@ -3,13 +3,13 @@ import org.springframework.stereotype.Component; @Component -public class InstructCode { +public class InstructCode { /** * 设备初始化时需要依次发送1~16号指令 */ public static String[] InitCodeArr = { - "1", "设置高压", "set set_voltage 4 1.2\\n", + "1", "设置高压", "set set_voltage 6 1.2\\n", "2", "复位adc", "cmd set_adc_delay_rst 1 0\\n", "3", "逻辑复位", "cmd set_logic_rst 1 0\\n", "4", "设置波形采样触发方式为下降沿", "cmd set_raw_trig_sel_adc2 3 0\\n", @@ -30,30 +30,51 @@ /** * 加高压指令 */ - public static final String DAC_UP="cmd dac_up 0 0\\n"; + public static final String DAC_UP = "cmd dac_up 0 0\\n"; /** * 关闭高压指令 */ - public static final String DAC_DOWN="cmd dac_down 0 0\\n"; + public static final String DAC_DOWN = "cmd dac_down 0 0\\n"; /** + * ` * 电压采样帧头 */ - public static final String DAC_HEAD="AA55AA551ACFFC5D"; +// public static final String DAC_HEAD="AA55AA551ACFFC5D"; + public static final String DAC_HEAD = "55aa55aa5dfccf1a"; + public static final String DAC_HEAD_LENGTH = "54000000"; + public static final int DAC_DATA_LENGTH = 144; /** * 波形采样帧头 */ - public static final String WAVE_HEAD="AA55AA551ACFFC3D"; +// public static final String WAVE_HEAD="AA55AA551ACFFC3D"; + public static final String WAVE_HEAD = "55aa55aa3dfccf1a"; /** * PSD1采样帧头 */ - public static final String PSD1_HEAD="AA55AA551ACFFC4D"; +// public static final String PSD1_HEAD="AA55AA551ACFFC4D"; + public static final String PSD1_HEAD = "55aa55aa4dfccf1a"; /** * PSD2采样帧头 */ - public static final String PSD2_HEAD="F0F0A5A5"; +// public static final String PSD2_HEAD="F0F0A5A5"; + public static final String PSD2_HEAD = "f0f0a5a5"; + + + public static String[] getVolArr(String msg) { + String[] volArr = new String[24]; + for (int j = 0; j < 24; j++) { + //前16为电压值 + if (j < 16) + volArr[j] = ByteUtil.reverseHex(msg.substring(j * 8, (j + 1) * 8)); + //后8为高低位值 + else + volArr[j] = ByteUtil.reverseHex(msg.substring(128 + (j - 16) * 2, 128 + (j - 15) * 2)); + } + return volArr; + } } diff --git a/casic-server/src/main/java/com/casic/missiles/netty/NettyClient.java b/casic-server/src/main/java/com/casic/missiles/netty/NettyClient.java index 70c4e6e..3626e51 100644 --- a/casic-server/src/main/java/com/casic/missiles/netty/NettyClient.java +++ b/casic-server/src/main/java/com/casic/missiles/netty/NettyClient.java @@ -1,23 +1,18 @@ package com.casic.missiles.netty; import io.netty.bootstrap.Bootstrap; -import io.netty.channel.Channel; -import io.netty.channel.ChannelFuture; -import io.netty.channel.ChannelInitializer; -import io.netty.channel.ChannelOption; -import io.netty.channel.ChannelPipeline; -import io.netty.channel.EventLoopGroup; +import io.netty.buffer.Unpooled; +import io.netty.channel.*; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioSocketChannel; -import io.netty.handler.codec.string.StringDecoder; -import io.netty.handler.codec.string.StringEncoder; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; import javax.annotation.Resource; +import java.nio.charset.StandardCharsets; import java.util.concurrent.ForkJoinPool; @Slf4j @@ -45,8 +40,7 @@ @Override public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline p = ch.pipeline(); - p.addLast("decoder", new StringDecoder()); - p.addLast("encoder", new StringEncoder()); + p.addLast(new HjtDecoder()); p.addLast(nettyClientHandler); } }); @@ -68,7 +62,7 @@ if (mChannel.get() == null) { mChannel.set(mChannelFuture.channel()); } - mChannel.get().writeAndFlush(data); + mChannel.get().writeAndFlush(Unpooled.copiedBuffer(data.getBytes(StandardCharsets.UTF_8))); } catch (Exception e) { log.error(this.getClass().getName().concat(".send has error"), e); } @@ -77,7 +71,8 @@ // 客户端启动,并连上服务器端 @PostConstruct public void init() { - ForkJoinPool.commonPool().submit(() -> startClient("127.0.0.1", 52002)); + ForkJoinPool.commonPool().submit(() -> startClient("192.168.1.50", 52002)); +// ForkJoinPool.commonPool().submit(() -> startClient("127.0.0.1", 52002)); } @PreDestroy diff --git a/casic-server/pom.xml b/casic-server/pom.xml index 8c25977..805cc22 100644 --- a/casic-server/pom.xml +++ b/casic-server/pom.xml @@ -61,6 +61,11 @@ jna 3.0.0 + + net.jodah + expiringmap + 0.5.8 + diff --git a/casic-server/src/main/java/com/casic/missiles/netty/ByteUtil.java b/casic-server/src/main/java/com/casic/missiles/netty/ByteUtil.java new file mode 100644 index 0000000..55c951e --- /dev/null +++ b/casic-server/src/main/java/com/casic/missiles/netty/ByteUtil.java @@ -0,0 +1,517 @@ +package com.casic.missiles.netty; + +import org.springframework.stereotype.Component; + +@Component +public class ByteUtil { + + /* BCD码转换为数值或字符 */ + + /** + * 单一字节中选择指定位bcd码转换为整数 + * 如ByteUtil.bcdToInt((byte) 0x33, 5, 4) = 3 + * + * @param b + * @param highBit 高位,最大7 + * @param lowBit 低位,从0开始 + * @return + */ + public static int bcdToInt(byte b, int highBit, int lowBit) { + int g = highBit - lowBit + 1; + int a = ((int) Math.pow(2, g) - 1) << lowBit; + int b2 = (b & a) >> lowBit; + if (b2 < 0 || b2 >= 10) { + return -1; + } + return b2; + } + + /** + * bcd码转换为整数 错误则返回-1 + * 如ByteUtil.bcdToInt((byte) 0x73) = 73 + * + * @param b 输入字节,默认4位一组,先高后低 + * @return + */ + public static int bcdToInt(byte b) { + int pl = 0x0f; + int ph = 0xf0; + + int h = (ph & b) >> 4; + int l = (pl & b); + + if (h >= 10 || l >= 10) { + return -1; + } + return h * 10 + l; + } + + /** + * bcd码转换为long型整数 + * 如字节数组{0x25, 0x23}转换为2523 + * + * @param + * @return + */ + public static long bcdToLong(byte[] bytes) { + if (ByteUtil.bcdToString(bytes).equals("") == false) + return Long.valueOf(ByteUtil.bcdToString(bytes)).longValue(); + else + return -1; + } + + + /** + * bcd码转换为字符串 按字节顺序 高位在前低位在后 + * 如字节数组{0x01, 0x02, 0x03}转换为"010203" + * + * @param bytes + * @return + */ + public static String bcdToString(byte[] bytes) { + StringBuilder sb = new StringBuilder(); + + for (int i = 0; i < bytes.length; i++) { + if (ByteUtil.bcdToInt(bytes[i], 7, 4) != -1 && ByteUtil.bcdToInt(bytes[i], 3, 0) != -1) { + sb.append(ByteUtil.bcdToInt(bytes[i], 7, 4)); + sb.append(ByteUtil.bcdToInt(bytes[i], 3, 0)); + } + } + + return sb.toString(); + } + + + /* BIN码转换为数值或字符 */ + + /** + * 单一字节中选择指定位bin码转换为整数 + * 如ByteUtil.binToInt((byte) 0xA3, 7, 7) = 1 + * + * @param b + * @param highBit 高位,最大7 + * @param lowBit 低位,从0开始 + * @return + */ + public static int binToInt(byte b, int highBit, int lowBit) throws RuntimeException { + int g = highBit - lowBit + 1; + int a = ((int) Math.pow(2, g) - 1) << lowBit; + int b2 = (b & a) >> lowBit; + if (b2 < 0 || b2 >= 16) { + throw new RuntimeException(); + } + return b2; + } + + /** + * BIN字节转换为整数 + * 如ByteUtil.bcdToInt((byte) 0x7B) = 123 + * + * @param b + * @return + */ + public static int binToInt(byte b) { + int i1 = 0x80 & b; + if (i1 == 0x80) { + int i2 = 0x7f & b; + return 128 + i2; + } else { + return b; + } + } + + /** + * BIN码转换为整数 高位在前低位在后 + * 如ByteUtil.bcdToInt(new byte[] {(byte) 0x7B, 0x22}) = 31522 + * + * @param bytes + * @return + */ + public static int binToInt(byte[] bytes) { + int i10 = 0; + if (null != bytes && bytes.length != 0) { + for (int i = 0; i < bytes.length; i++) { + i10 = i10 + new Double(Math.pow(16, (bytes.length - i - 1) * 2)).intValue() * ByteUtil.binToInt(bytes[i]); + } + } + return i10; + } + + /** + * BIN码转换为LONG型整数 高位在前低位在后 + * 如ByteUtil.bcdToLong(new byte[] {(byte) 0x7B, 0x22, 0x32, 0x9A}) = 2065838746 + * + * @param bytes + * @return + */ + public static int binToLong(byte[] bytes) { + int l10 = 0; + if (null != bytes && bytes.length != 0) { + for (int i = 0; i < bytes.length; i++) { + l10 = l10 + new Double(Math.pow(16, (bytes.length - i - 1) * 2)).intValue() * ByteUtil.binToInt(bytes[i]); + } + } + return l10; + } + + /** + * 字节数组转换为16进制字符串 按字节数组顺序转换 + * 如字节数组{0x21, 0x06, 0x01, 0x04, 0x40, (byte) 0xE2, 0x01}转换为"2106010440E201" + * + * @param bytes + * @return + */ + public static String binToHexString(byte[] bytes) { + StringBuilder sb = new StringBuilder(); + + for (int i = 0; i < bytes.length; i++) { + sb.append(Integer.toHexString(binToInt((byte) ((bytes[i] & 0xf0) >> 4))).toUpperCase()); + sb.append(Integer.toHexString(binToInt((byte) (bytes[i] & 0x0f))).toUpperCase()); + } + + return sb.toString(); + } + + + /** + * BIN输出为二进制字符串 + * 如字节数组{0x01, 0x38}转换为字符串"0000000100111000" + * + * @param bytes + * @return + */ + public static String binToBinString(byte[] bytes) { + StringBuilder sb = new StringBuilder(); + + for (int i = 0; i < bytes.length; i++) { + String s = Integer.toBinaryString(binToInt(bytes[i])); + while (s.length() < 8) { + s = "0" + s; + } + sb.append(s); + } + + return sb.toString(); + } + + + /* int转换为BCD码或BIN码 */ + + /** + * 两位整数转换成一个字节的BCD码 + * 大于100的整数取其十位和个位 十位在前个位在后 + * 如79转换为0x79 + * + * @param i + * @return + */ + public static byte intToBcd(int i) { + byte b = 0x00; + while (i > 99) { + i = i % 100; + } + + int l = i % 10; + int h = i / 10; + + b = (byte) (h << 4 | l); + + return b; + } + + /** + * 整数转换为字节数组 高位在前低位在后 + * 如 2379转换为0x23, 0x79 + * + * @param i + * @return + */ + public static byte[] intToBcds(long l) { + String str = String.valueOf(l); + Bytes bytes = new Bytes(); + + if (str.length() % 2 == 1) { + str = "0" + str; + } + + for (int i = 0; i < str.length() - 1; i = i + 2) { + String s = str.substring(i, i + 2); + bytes.append(ByteUtil.intToBcd(Integer.parseInt(s))); + } + + return bytes.toBytes(); + } + + /** + * 整数转换为指定字节数的BCD码数组 高位在前低位在后 从低位开始计算长度 超出部分忽略 不足部分在高位补0 + * 如 3013479指定2字节转换为0x34, 0x79, 指定6字节转换为0x00, 0x00, 0x03, 0x01, 0x34, 0x79 + * + * @param i + * @param length + * @return + */ + public static byte[] intToBcds(long l, int length) { + byte[] bts = intToBcds(l); + if (bts.length == length) { + return bts; + } else { + byte[] bytes = new byte[length]; + for (int j = length - 1; j >= 0; j--) { + if (j + bts.length - length >= 0) { + bytes[j] = bts[j + bts.length - length]; + } else { + bytes[j] = 0x00; + } + } + + return bytes; + } + } + + /** + * 整数转换为指定字节数的BCD码数组 低位在前高位在后 从低位开始计算长度 超出部分忽略 不足部分在高位补0 + * 如 3013479指定2字节转换为0x79, 0x34, 指定6字节转换为0x79, 0x34, 0x01, 0x03, 0x00, 0x00 + * + * @param i + * @param length + * @return + */ + public static byte[] intToBcdsHL(long l, int length) { + Bytes bs = new Bytes(); + + for (int i = 0; i < length; i++) { + int low = (int) (l % 100); + bs.append(ByteUtil.intToBcd(low)); + l = l / 100; + } + + return bs.toBytes(); + } + + /** + * 整数转换为BIN码字节数组 高位在前低位在后 + * 如113479转换为0x01, 0xBB, 0x47 + * + * @param l + * @return + */ + public static byte[] intToBins(long l) { + String str = Long.toHexString(l); + if (str.length() % 2 == 1) { + str = "0" + str; + } + + Bytes bytes = new Bytes(); + for (int i = 0; i < str.length() - 1; i = i + 2) { + String s = str.substring(i, i + 2); + bytes.append((byte) Integer.parseInt(s, 16)); + } + + return bytes.toBytes(); + } + + /** + * 整数转换为指定字节数的BIN码数组 高位在前低位在后 从低位开始计算长度 超出部分忽略 不足部分在高位补0 + * 如 3013479指定2字节转换为0xFB, 0x67, 指定6字节转换为0x00, 0x00, 0x00, 0x2D, 0xFB, 0x67 + * + * @param l + * @param length + * @return + */ + public static byte[] intToBins(long l, int length) { + byte[] bts = intToBins(l); + if (bts.length == length) { + return bts; + } else { + byte[] bytes = new byte[length]; + for (int j = length - 1; j >= 0; j--) { + if (j + bts.length - length >= 0) { + bytes[j] = bts[j + bts.length - length]; + } else { + bytes[j] = 0x00; + } + } + + return bytes; + } + } + + + /* 16进制字符串转换为字节数组 */ + + /** + * 16进制字符串顺序转换为字节数组 + * 如字符串"13810411703"转换为字节数组{0x01, 0x38, 0x10, 0x41, 0x17, 0x03} + * + * @param str + * @return + */ + public static byte[] hexStringToBytes(String str) { + if (str.length() % 2 == 1) { + str = "0" + str; + } + + Bytes bytes = new Bytes(); + for (int i = 0; i < str.length() - 1; i = i + 2) { + String s = str.substring(i, i + 2); + bytes.append((byte) Integer.parseInt(s, 16)); + } + + return bytes.toBytes(); + } + + /** + * 16进制字符串顺序转换为指定字节数的BIN码数组 从低位开始计算长度 超出部分忽略 不足部分在高位补0 + * 如字符串"13810411703"转换为3字节数组{0x41, 0x17, 0x03}, 指定8字节转换为{0x00, 0x00, 0x01, 0x38, 0x10, 0x41, 0x17, 0x03} + * + * @param str + * @param length + * @return + */ + public static byte[] hexStringToBytes(String str, int length) { + byte[] bts = hexStringToBytes(str); + if (bts.length == length) { + return bts; + } else { + byte[] bytes = new byte[length]; + for (int j = length - 1; j >= 0; j--) { + if (j + bts.length - length >= 0) { + bytes[j] = bts[j + bts.length - length]; + } else { + bytes[j] = 0x00; + } + } + + return bytes; + } + } + + + /* 十进制BCD码字符串转换为字节数组 */ + + /** + * 十进制BCD码字符串顺序转换为字节数组 + * 如字符串"13810411703"转换为字节数组{0x01, 0x38, 0x10, 0x41, 0x17, 0x03} + * + * @param str + * @return + */ + public static byte[] bcdStringToBytes(String str) { + if (str.length() % 2 == 1) { + str = "0" + str; + } + + Bytes bytes = new Bytes(); + for (int i = 0; i < str.length() - 1; i = i + 2) { + String s = str.substring(i, i + 2); + bytes.append(ByteUtil.intToBcd(Integer.parseInt(s))); + } + + return bytes.toBytes(); + } + + /** + * 十进制BCD码字符串顺序转换为指定字节数的BCD码数组 从低位开始计算长度 超出部分忽略 不足部分在高位补0 + * 如字符串"13810411703"转换为3字节数组{0x41, 0x17, 0x03}, 指定8字节转换为{0x00, 0x00, 0x01, 0x38, 0x10, 0x41, 0x17, 0x03} + * + * @param str + * @param length + * @return + */ + public static byte[] bcdStringToBytes(String str, int length) { + byte[] bts = bcdStringToBytes(str); + if (bts.length == length) { + return bts; + } else { + byte[] bytes = new byte[length]; + for (int j = length - 1; j >= 0; j--) { + if (j + bts.length - length >= 0) { + bytes[j] = bts[j + bts.length - length]; + } else { + bytes[j] = 0x00; + } + } + + return bytes; + } + } + + public static String buildCRC16(byte[] data) { + byte[] buf = new byte[data.length];// 存储需要产生校验码的数据 + for (int i = 0; i < data.length; i++) { + buf[i] = data[i]; + } + int len = buf.length; + int crc = 0xFFFF;//16位 + for (int pos = 0; pos < len; pos++) { + if (buf[pos] < 0) { + crc ^= (int) buf[pos] + 256; // XOR byte into least sig. byte of + // crc + } else { + crc ^= (int) buf[pos]; // XOR byte into least sig. byte of crc + } + for (int i = 8; i != 0; i--) { // Loop over each bit + if ((crc & 0x0001) != 0) { // If the LSB is set + crc >>= 1; // Shift right and XOR 0xA001 + crc ^= 0xA001; + } else + // Else LSB is not set + crc >>= 1; // Just shift right + } + } + String c = Integer.toHexString(crc); + if (c.length() == 4) { + c = c.substring(2, 4) + c.substring(0, 2); + } else if (c.length() == 3) { + c = "0" + c; + c = c.substring(2, 4) + c.substring(0, 2); + } else if (c.length() == 2) { + c = "0" + c.substring(1, 2) + "0" + c.substring(0, 1); + } + return c; + } + + + public static byte[] byteMergerAll(byte[]... values) { + int length_byte = 0; + for (int i = 0; i < values.length; i++) { + length_byte += values[i].length; + } + byte[] all_byte = new byte[length_byte]; + int countLength = 0; + for (int i = 0; i < values.length; i++) { + byte[] b = values[i]; + System.arraycopy(b, 0, all_byte, countLength, b.length); + countLength += b.length; + } + return all_byte; + } + + public static String convertToHex(String str) { + char[] charArray = str.toCharArray(); + String hexString = ""; + for (char c : charArray) { + hexString += Integer.toHexString((int) c); + } + return hexString; + } + + public static String reverseHex(String hex) { + char[] charArray = hex.toCharArray(); + int length = charArray.length; + int times = length / 2; + for (int c1i = 0; c1i < times; c1i += 2) { + int c2i = c1i + 1; + char c1 = charArray[c1i]; + char c2 = charArray[c2i]; + int c3i = length - c1i - 2; + int c4i = length - c1i - 1; + charArray[c1i] = charArray[c3i]; + charArray[c2i] = charArray[c4i]; + charArray[c3i] = c1; + charArray[c4i] = c2; + } + return new String(charArray); + } + +} diff --git a/casic-server/src/main/java/com/casic/missiles/netty/Bytes.java b/casic-server/src/main/java/com/casic/missiles/netty/Bytes.java new file mode 100644 index 0000000..9031d19 --- /dev/null +++ b/casic-server/src/main/java/com/casic/missiles/netty/Bytes.java @@ -0,0 +1,73 @@ +package com.casic.missiles.netty; + +public class Bytes { + + private byte[] buffer = null; + + public Bytes() { + + } + + public Bytes append(byte b) { + byte[] nb = null; + if (buffer == null) { + nb = new byte[] { b }; + } else { + nb = new byte[buffer.length + 1]; + for (int i = 0; i < buffer.length; i++) { + nb[i] = buffer[i]; + } + nb[nb.length - 1] = b; + } + buffer = nb; + return this; + } + + public Bytes append(byte[] bs) { + if (bs == null) + return this; + byte[] nb = null; + if (buffer == null) { + buffer = bs; + } else { + nb = new byte[buffer.length + bs.length]; + for (int i = 0; i < buffer.length; i++) { + nb[i] = buffer[i]; + } + for (int i = 0; i < bs.length; i++) { + nb[buffer.length + i] = bs[i]; + } + buffer = nb; + } + return this; + } + + public byte[] toBytes() { + return buffer; + } + + public byte[] toBytes(int l) { + Bytes bs = new Bytes(); + + for (int i = 0; i < l; i++) { + if (i < buffer.length) { + bs.append(buffer[i]); + } else { + bs.append((byte) 0x00); + } + } + + return bs.toBytes(); + } + + public static void main(String[] args) { + Bytes bytes = new Bytes(); + int i = 0xf2; + byte a = 0x68; + byte[] b = new byte[] { 0x11, 0x22 }; + byte c = (byte) i; + byte[] r = bytes.append(a).append(b).append(c).toBytes(); + System.out.println(r); + } + +} diff --git a/casic-server/src/main/java/com/casic/missiles/netty/CacheUtils.java b/casic-server/src/main/java/com/casic/missiles/netty/CacheUtils.java new file mode 100644 index 0000000..8d2250a --- /dev/null +++ b/casic-server/src/main/java/com/casic/missiles/netty/CacheUtils.java @@ -0,0 +1,261 @@ +package com.casic.missiles.netty; + +import cn.hutool.core.util.ObjectUtil; +import lombok.extern.slf4j.Slf4j; +import net.jodah.expiringmap.ExpirationPolicy; +import net.jodah.expiringmap.ExpiringMap; +import org.springframework.stereotype.Component; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Set; +import java.util.concurrent.TimeUnit; + +@Slf4j +@Component +public class CacheUtils { + + //电压数据分包计数 + private volatile static int VOL_I = 0; + private volatile static boolean VOL_FLAG = false; + //PSD数据分包计数 + private volatile static int PSD_I = 0; + private volatile static boolean PSD_FLAG = false; + + /** + * 缓存电压信息 + */ + public static final ExpiringMap VOL_MAP = ExpiringMap.builder() + .maxSize(3000) + .expiration(5, TimeUnit.MINUTES) + .variableExpiration() + .expirationPolicy(ExpirationPolicy.ACCESSED) + .build(); + + /** + * 缓存PSD流信息 + */ + public static final ExpiringMap PSD_MAP = ExpiringMap.builder() + .maxSize(3000) + .expiration(5, TimeUnit.MINUTES) + .variableExpiration() +// .expirationListener((key,value) -> { +// System.out.println("key:" + key + "已过期……"); +// }) + .expirationPolicy(ExpirationPolicy.ACCESSED) + .build(); + + + + + public synchronized static String startVolMapCache() { + String msgs = ""; + if (CacheUtils.VOL_MAP.size() > 1) { + Set keys = CacheUtils.VOL_MAP.keySet(); + List keyInts = new ArrayList<>(keys); + Collections.sort(keyInts); + for (Integer i : keyInts) { + msgs += CacheUtils.VOL_MAP.get(i); + } + if (ObjectUtil.isNotEmpty(msgs)) { + CacheUtils.VOL_MAP.clear(); + //继续拆包处理 +// String msgLast = ""; +// if (msgs.indexOf(InstructCode.DAC_HEAD + InstructCode.DAC_HEAD_LENGTH) != -1) { +// if (msgs.length() < (msgs.indexOf(InstructCode.DAC_HEAD + InstructCode.DAC_HEAD_LENGTH) + 24 + InstructCode.DAC_DATA_LENGTH)) { +// msgLast = msgs.substring(msgs.indexOf(msgs.indexOf(InstructCode.DAC_HEAD + InstructCode.DAC_HEAD_LENGTH))); +// CacheUtils.VOL_MAP.put(keyInts.get(keyInts.size() - 1), msgLast); +// } +// } + } + + } + return msgs; + } + + public synchronized static String startPsdMapCache() { + String msgs = ""; + if (CacheUtils.PSD_MAP.size() > 1) { + Set keys = CacheUtils.PSD_MAP.keySet(); + List keyInts = new ArrayList<>(keys); + Collections.sort(keyInts); + for (Integer i : keyInts) { + msgs += CacheUtils.PSD_MAP.get(i); + } + if (ObjectUtil.isNotEmpty(msgs)) { + CacheUtils.PSD_MAP.clear(); + //继续拆包处理 +// String msgLast = ""; +// if (msgs.indexOf(InstructCode.PSD1_HEAD) != -1) { +// if (msgs.length() >= 24) { +// int totalLength = Integer.parseInt(ByteUtil.reverseHex(msgs.substring(msgs.indexOf(InstructCode.PSD1_HEAD) + 16, 24)), 16); +// if (msgs.length() < totalLength * 2) { +// msgLast = msgs.substring(msgs.indexOf(InstructCode.PSD1_HEAD)); +// CacheUtils.PSD_MAP.put(keyInts.get(keyInts.size() - 1), msgLast); +// } +// } +// } + } + } + return msgs; + } + + public static void main(String[] args) { + + String msg="f0f0a5a50300001102000c081b0070000000000059f372000d81030058d50000f0f0a5a50300001102000b081b0070000000000012c7a900bd7e03006ed3000055aa55aa4dfccf1a2c000000f0f0a5a50300001102000b081b00700000000000aa0b2e001080030084d40000"; + + msg="0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000055aa55aa8dfccf1a4e0000002331363337362c31363337362c31363337362c31363337362c31363337362c31363337362c3234342c32303030302c322c3130302c31363338332c302c323030302355aa55aa4dfccf1a2c000000f0f0a5a50300001102000c081b00700000000000df63650092810300e3d50000"; + +// msg="55aa55aa8dfccf1a4e0000002331363337362c31363337362c31363337362c31363337362c31363337362c31363337362c3234342c32303030302c322c3130302c31363338332c302c323030302355aa55aa4dfccf1a4c000000f0f0a5a50300001102000b081b00700000000000933b7100e47d0300a8d20000f0f0a5a50300001102000b081b00700000000000127c88003d810300a6d50000"; +// int totalLength = Integer.parseInt(ByteUtil.reverseHex(msg.substring(msg.indexOf(InstructCode.PSD1_HEAD) + 16, 24)), 16); + + +// msg="55aa55aa4dfccf1a2c000000f0f0a5a50300001102000b081b00700000000000b5560c00597f0300f6d3000055aa55aa4dfccf1a6c000000f0f0a5a50300001102000a081b007000000000005e8623007480030019d50000f0f0a5a50300001102000b081b0070000000000055083c006b7d030023d20000f0f0a5a50300001102000b081b007000000000007d3d3c00196103008fb7000055aa55aa5dfccf1a540000000000000000000000000000000000000000000000df2e000000000000000000000000000000000000000000000000000000000000e02e00000000000000000000000000000000000055aa55aa10fccf1a0c200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; + +// msg="55aa55aa4dfccf1a2c000000"; + + +// +// String[] msgArr = msg.split(InstructCode.PSD1_HEAD); +// if(msgArr.length>2){ +// for(int i=1;i= 24) { +//// log.info("原始msg--->"+msg); +// int totalLength = Integer.parseInt(ByteUtil.reverseHex(msg.substring(msg.indexOf(InstructCode.PSD1_HEAD) + 16, +// msg.indexOf(InstructCode.PSD1_HEAD) + 24)), 16); +// if (msg.length() >= totalLength * 2) { +// +// for (int i = 0; i < (msg.length() - (msg.indexOf(InstructCode.PSD1_HEAD) + 24)) / 64; i++) { +// String msgi = msg.substring((msg.indexOf(InstructCode.PSD1_HEAD) + 24) + i * 64, (msg.indexOf(InstructCode.PSD1_HEAD) + 24) + (i + 1) * 64); +// if (msgi.indexOf(InstructCode.PSD2_HEAD) == 0) { +// String gateOff = msgi.substring(16, 20); +// String baseline = msgi.substring(20, 24); +// String shortGate = msgi.substring(24, 28); +// String longGate = msgi.substring(28, 32); +// String qlong = msgi.substring(48, 56); +// String qshort = msgi.substring(56, 64); +// //toDo:算法计算 +// System.out.println("gateOff--->" + gateOff + +// "baseline--->" + baseline + +// "shortGate--->" + shortGate + +// "longGate--->" + longGate + +// "qlong--->" + qlong + +// "qshort--->" + qshort); +// } +// } +// } +// } + + } + + + public static void proccessData(String msg){ + //拆包处理 PSD数据 + if (msg.indexOf(InstructCode.PSD1_HEAD) != -1) { + + if (msg.length() >= 24) { +// log.info("原始msg--->"+msg); + int totalLength = Integer.parseInt(ByteUtil.reverseHex(msg.substring(msg.indexOf(InstructCode.PSD1_HEAD) + 16, + msg.indexOf(InstructCode.PSD1_HEAD) + 24)), 16); + if (msg.length() >= totalLength * 2) { + PSD_FLAG = false; + for (int i = 0; i < (msg.length() - (msg.indexOf(InstructCode.PSD1_HEAD) + 24)) / 64; i++) { + String msgi = msg.substring((msg.indexOf(InstructCode.PSD1_HEAD) + 24) + i * 64, (msg.indexOf(InstructCode.PSD1_HEAD) + 24) + (i + 1) * 64); + if (msgi.indexOf(InstructCode.PSD2_HEAD) == 0) { + String gateOff = msgi.substring(16, 20); + String baseline = msgi.substring(20, 24); + String shortGate = msgi.substring(24, 28); + String longGate = msgi.substring(28, 32); + String qlong = msgi.substring(48, 56); + String qshort = msgi.substring(56, 64); + //toDo:算法计算 + log.info("gateOff--->" + gateOff + + "baseline--->" + baseline + + "shortGate--->" + shortGate + + "longGate--->" + longGate + + "qlong--->" + qlong + + "qshort--->" + qshort); + } + } + } else { + //2.缺psd数据存入缓存处理 + PSD_FLAG = true; + PSD_I++; + CacheUtils.PSD_MAP.put(PSD_I, msg); +// log.info("psd加入数据:" + PSD_I + "------->" + msg); + } + } + } else if (PSD_FLAG) { + PSD_I++; + CacheUtils.PSD_MAP.put(PSD_I, msg); + String str = startPsdMapCache(); +// log.info("分包psd加入数据:" + PSD_I + "------->" + msg); + if (ObjectUtil.isNotEmpty(str) && str.length() >= 24) { +// log.info("**************strpsd---->" + str); + if (str.indexOf(InstructCode.PSD1_HEAD) != -1 && str.substring(str.indexOf(InstructCode.PSD1_HEAD) + 16).length() >= 8) { + int totalLength = Integer.parseInt(ByteUtil.reverseHex(str.substring(str.indexOf(InstructCode.PSD1_HEAD) + 16, + str.indexOf(InstructCode.PSD1_HEAD) + 24)), 16); + if (str.length() >= totalLength * 2) { + for (int i = 0; i < (str.length() - (str.indexOf(InstructCode.PSD1_HEAD) + 24)) / 64; i++) { + String msgi = str.substring((str.indexOf(InstructCode.PSD1_HEAD) + 24) + i * 64, (str.indexOf(InstructCode.PSD1_HEAD) + 24) + (i + 1) * 64); + if (msgi.indexOf(InstructCode.PSD2_HEAD) == 0) { + String gateOff = msgi.substring(16, 20); + String baseline = msgi.substring(20, 24); + String shortGate = msgi.substring(24, 28); + String longGate = msgi.substring(28, 32); + String qlong = msgi.substring(48, 56); + String qshort = msgi.substring(56, 64); + //toDo:算法计算 + log.info("分包:gateOff--->" + gateOff + + "baseline--->" + baseline + + "shortGate--->" + shortGate + + "longGate--->" + longGate + + "qlong--->" + qlong + + "qshort--->" + qshort); + } + } + } + } + } + } + //拆包处理 电压数据 + if (msg.indexOf(InstructCode.DAC_HEAD + InstructCode.DAC_HEAD_LENGTH) != -1) { + //完整包处理 + if (msg.length() >= (msg.indexOf(InstructCode.DAC_HEAD + InstructCode.DAC_HEAD_LENGTH) + 24 + InstructCode.DAC_DATA_LENGTH)) { + VOL_FLAG = false; + msg = msg.substring(msg.indexOf(InstructCode.DAC_HEAD + InstructCode.DAC_HEAD_LENGTH) + 24); + String[] volArr = InstructCode.getVolArr(msg); + //本设备只用到第6通道 + String vol6 = volArr[5]; + log.info("第6通道电压值-------->" + vol6); + } else { + //分包处理,先放到缓存 + VOL_FLAG = true; + VOL_I++; + CacheUtils.VOL_MAP.put(VOL_I, msg); + } + } else if (VOL_FLAG) { + VOL_I++; + CacheUtils.VOL_MAP.put(VOL_I, msg); + String str = startVolMapCache(); + if (ObjectUtil.isNotEmpty(str) && + str.length() >= (str.indexOf(InstructCode.DAC_HEAD + InstructCode.DAC_HEAD_LENGTH) + 24 + InstructCode.DAC_DATA_LENGTH)) { + VOL_FLAG = false; + str = str.substring(str.indexOf(InstructCode.DAC_HEAD + InstructCode.DAC_HEAD_LENGTH) + 24); + String[] volArr = InstructCode.getVolArr(str); + //本设备只用到第6通道 + String vol6 = volArr[5]; + log.info("分包第6通道电压值-------->" + vol6); + } + } + + } + +} \ No newline at end of file diff --git a/casic-server/src/main/java/com/casic/missiles/netty/HjtDecoder.java b/casic-server/src/main/java/com/casic/missiles/netty/HjtDecoder.java new file mode 100644 index 0000000..2649988 --- /dev/null +++ b/casic-server/src/main/java/com/casic/missiles/netty/HjtDecoder.java @@ -0,0 +1,24 @@ +package com.casic.missiles.netty; + +import io.netty.buffer.ByteBuf; +import io.netty.buffer.ByteBufUtil; +import io.netty.channel.ChannelHandlerContext; +import io.netty.handler.codec.MessageToMessageDecoder; +import lombok.extern.slf4j.Slf4j; + +import java.nio.charset.Charset; +import java.util.List; + +/** + * @description: 将从接口取到的数据编码 + * @author: Stone + * @create: 2019-01-11 15:15 + **/ +@Slf4j +public class HjtDecoder extends MessageToMessageDecoder { + @Override + protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List out) throws Exception { +// log.info("hexBytes : " + ByteBufUtil.hexDump(msg) + ";" + " String : " + msg.toString(Charset.defaultCharset())); + out.add(ByteBufUtil.hexDump(msg)); + } +} diff --git a/casic-server/src/main/java/com/casic/missiles/netty/InstructCode.java b/casic-server/src/main/java/com/casic/missiles/netty/InstructCode.java index 0732347..5c61759 100644 --- a/casic-server/src/main/java/com/casic/missiles/netty/InstructCode.java +++ b/casic-server/src/main/java/com/casic/missiles/netty/InstructCode.java @@ -3,13 +3,13 @@ import org.springframework.stereotype.Component; @Component -public class InstructCode { +public class InstructCode { /** * 设备初始化时需要依次发送1~16号指令 */ public static String[] InitCodeArr = { - "1", "设置高压", "set set_voltage 4 1.2\\n", + "1", "设置高压", "set set_voltage 6 1.2\\n", "2", "复位adc", "cmd set_adc_delay_rst 1 0\\n", "3", "逻辑复位", "cmd set_logic_rst 1 0\\n", "4", "设置波形采样触发方式为下降沿", "cmd set_raw_trig_sel_adc2 3 0\\n", @@ -30,30 +30,51 @@ /** * 加高压指令 */ - public static final String DAC_UP="cmd dac_up 0 0\\n"; + public static final String DAC_UP = "cmd dac_up 0 0\\n"; /** * 关闭高压指令 */ - public static final String DAC_DOWN="cmd dac_down 0 0\\n"; + public static final String DAC_DOWN = "cmd dac_down 0 0\\n"; /** + * ` * 电压采样帧头 */ - public static final String DAC_HEAD="AA55AA551ACFFC5D"; +// public static final String DAC_HEAD="AA55AA551ACFFC5D"; + public static final String DAC_HEAD = "55aa55aa5dfccf1a"; + public static final String DAC_HEAD_LENGTH = "54000000"; + public static final int DAC_DATA_LENGTH = 144; /** * 波形采样帧头 */ - public static final String WAVE_HEAD="AA55AA551ACFFC3D"; +// public static final String WAVE_HEAD="AA55AA551ACFFC3D"; + public static final String WAVE_HEAD = "55aa55aa3dfccf1a"; /** * PSD1采样帧头 */ - public static final String PSD1_HEAD="AA55AA551ACFFC4D"; +// public static final String PSD1_HEAD="AA55AA551ACFFC4D"; + public static final String PSD1_HEAD = "55aa55aa4dfccf1a"; /** * PSD2采样帧头 */ - public static final String PSD2_HEAD="F0F0A5A5"; +// public static final String PSD2_HEAD="F0F0A5A5"; + public static final String PSD2_HEAD = "f0f0a5a5"; + + + public static String[] getVolArr(String msg) { + String[] volArr = new String[24]; + for (int j = 0; j < 24; j++) { + //前16为电压值 + if (j < 16) + volArr[j] = ByteUtil.reverseHex(msg.substring(j * 8, (j + 1) * 8)); + //后8为高低位值 + else + volArr[j] = ByteUtil.reverseHex(msg.substring(128 + (j - 16) * 2, 128 + (j - 15) * 2)); + } + return volArr; + } } diff --git a/casic-server/src/main/java/com/casic/missiles/netty/NettyClient.java b/casic-server/src/main/java/com/casic/missiles/netty/NettyClient.java index 70c4e6e..3626e51 100644 --- a/casic-server/src/main/java/com/casic/missiles/netty/NettyClient.java +++ b/casic-server/src/main/java/com/casic/missiles/netty/NettyClient.java @@ -1,23 +1,18 @@ package com.casic.missiles.netty; import io.netty.bootstrap.Bootstrap; -import io.netty.channel.Channel; -import io.netty.channel.ChannelFuture; -import io.netty.channel.ChannelInitializer; -import io.netty.channel.ChannelOption; -import io.netty.channel.ChannelPipeline; -import io.netty.channel.EventLoopGroup; +import io.netty.buffer.Unpooled; +import io.netty.channel.*; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioSocketChannel; -import io.netty.handler.codec.string.StringDecoder; -import io.netty.handler.codec.string.StringEncoder; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; import javax.annotation.Resource; +import java.nio.charset.StandardCharsets; import java.util.concurrent.ForkJoinPool; @Slf4j @@ -45,8 +40,7 @@ @Override public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline p = ch.pipeline(); - p.addLast("decoder", new StringDecoder()); - p.addLast("encoder", new StringEncoder()); + p.addLast(new HjtDecoder()); p.addLast(nettyClientHandler); } }); @@ -68,7 +62,7 @@ if (mChannel.get() == null) { mChannel.set(mChannelFuture.channel()); } - mChannel.get().writeAndFlush(data); + mChannel.get().writeAndFlush(Unpooled.copiedBuffer(data.getBytes(StandardCharsets.UTF_8))); } catch (Exception e) { log.error(this.getClass().getName().concat(".send has error"), e); } @@ -77,7 +71,8 @@ // 客户端启动,并连上服务器端 @PostConstruct public void init() { - ForkJoinPool.commonPool().submit(() -> startClient("127.0.0.1", 52002)); + ForkJoinPool.commonPool().submit(() -> startClient("192.168.1.50", 52002)); +// ForkJoinPool.commonPool().submit(() -> startClient("127.0.0.1", 52002)); } @PreDestroy diff --git a/casic-server/src/main/java/com/casic/missiles/netty/NettyClientHandler.java b/casic-server/src/main/java/com/casic/missiles/netty/NettyClientHandler.java index b602605..08abcc9 100644 --- a/casic-server/src/main/java/com/casic/missiles/netty/NettyClientHandler.java +++ b/casic-server/src/main/java/com/casic/missiles/netty/NettyClientHandler.java @@ -24,7 +24,11 @@ log.info(String.format("客户端读取到从服务器端:%s 发送过来的数据:%s", ctx.channel().remoteAddress(), msg.toString())); // ctx.channel().writeAndFlush(String.format("server write:%s", msg)); //解析数据 - neutronOptService.analysis(msg.toString()); + try { + neutronOptService.analysis(msg.toString()); + }catch (Exception e){ + e.printStackTrace(); + } } // 捕获到异常的处理 diff --git a/casic-server/pom.xml b/casic-server/pom.xml index 8c25977..805cc22 100644 --- a/casic-server/pom.xml +++ b/casic-server/pom.xml @@ -61,6 +61,11 @@ jna 3.0.0 + + net.jodah + expiringmap + 0.5.8 + diff --git a/casic-server/src/main/java/com/casic/missiles/netty/ByteUtil.java b/casic-server/src/main/java/com/casic/missiles/netty/ByteUtil.java new file mode 100644 index 0000000..55c951e --- /dev/null +++ b/casic-server/src/main/java/com/casic/missiles/netty/ByteUtil.java @@ -0,0 +1,517 @@ +package com.casic.missiles.netty; + +import org.springframework.stereotype.Component; + +@Component +public class ByteUtil { + + /* BCD码转换为数值或字符 */ + + /** + * 单一字节中选择指定位bcd码转换为整数 + * 如ByteUtil.bcdToInt((byte) 0x33, 5, 4) = 3 + * + * @param b + * @param highBit 高位,最大7 + * @param lowBit 低位,从0开始 + * @return + */ + public static int bcdToInt(byte b, int highBit, int lowBit) { + int g = highBit - lowBit + 1; + int a = ((int) Math.pow(2, g) - 1) << lowBit; + int b2 = (b & a) >> lowBit; + if (b2 < 0 || b2 >= 10) { + return -1; + } + return b2; + } + + /** + * bcd码转换为整数 错误则返回-1 + * 如ByteUtil.bcdToInt((byte) 0x73) = 73 + * + * @param b 输入字节,默认4位一组,先高后低 + * @return + */ + public static int bcdToInt(byte b) { + int pl = 0x0f; + int ph = 0xf0; + + int h = (ph & b) >> 4; + int l = (pl & b); + + if (h >= 10 || l >= 10) { + return -1; + } + return h * 10 + l; + } + + /** + * bcd码转换为long型整数 + * 如字节数组{0x25, 0x23}转换为2523 + * + * @param + * @return + */ + public static long bcdToLong(byte[] bytes) { + if (ByteUtil.bcdToString(bytes).equals("") == false) + return Long.valueOf(ByteUtil.bcdToString(bytes)).longValue(); + else + return -1; + } + + + /** + * bcd码转换为字符串 按字节顺序 高位在前低位在后 + * 如字节数组{0x01, 0x02, 0x03}转换为"010203" + * + * @param bytes + * @return + */ + public static String bcdToString(byte[] bytes) { + StringBuilder sb = new StringBuilder(); + + for (int i = 0; i < bytes.length; i++) { + if (ByteUtil.bcdToInt(bytes[i], 7, 4) != -1 && ByteUtil.bcdToInt(bytes[i], 3, 0) != -1) { + sb.append(ByteUtil.bcdToInt(bytes[i], 7, 4)); + sb.append(ByteUtil.bcdToInt(bytes[i], 3, 0)); + } + } + + return sb.toString(); + } + + + /* BIN码转换为数值或字符 */ + + /** + * 单一字节中选择指定位bin码转换为整数 + * 如ByteUtil.binToInt((byte) 0xA3, 7, 7) = 1 + * + * @param b + * @param highBit 高位,最大7 + * @param lowBit 低位,从0开始 + * @return + */ + public static int binToInt(byte b, int highBit, int lowBit) throws RuntimeException { + int g = highBit - lowBit + 1; + int a = ((int) Math.pow(2, g) - 1) << lowBit; + int b2 = (b & a) >> lowBit; + if (b2 < 0 || b2 >= 16) { + throw new RuntimeException(); + } + return b2; + } + + /** + * BIN字节转换为整数 + * 如ByteUtil.bcdToInt((byte) 0x7B) = 123 + * + * @param b + * @return + */ + public static int binToInt(byte b) { + int i1 = 0x80 & b; + if (i1 == 0x80) { + int i2 = 0x7f & b; + return 128 + i2; + } else { + return b; + } + } + + /** + * BIN码转换为整数 高位在前低位在后 + * 如ByteUtil.bcdToInt(new byte[] {(byte) 0x7B, 0x22}) = 31522 + * + * @param bytes + * @return + */ + public static int binToInt(byte[] bytes) { + int i10 = 0; + if (null != bytes && bytes.length != 0) { + for (int i = 0; i < bytes.length; i++) { + i10 = i10 + new Double(Math.pow(16, (bytes.length - i - 1) * 2)).intValue() * ByteUtil.binToInt(bytes[i]); + } + } + return i10; + } + + /** + * BIN码转换为LONG型整数 高位在前低位在后 + * 如ByteUtil.bcdToLong(new byte[] {(byte) 0x7B, 0x22, 0x32, 0x9A}) = 2065838746 + * + * @param bytes + * @return + */ + public static int binToLong(byte[] bytes) { + int l10 = 0; + if (null != bytes && bytes.length != 0) { + for (int i = 0; i < bytes.length; i++) { + l10 = l10 + new Double(Math.pow(16, (bytes.length - i - 1) * 2)).intValue() * ByteUtil.binToInt(bytes[i]); + } + } + return l10; + } + + /** + * 字节数组转换为16进制字符串 按字节数组顺序转换 + * 如字节数组{0x21, 0x06, 0x01, 0x04, 0x40, (byte) 0xE2, 0x01}转换为"2106010440E201" + * + * @param bytes + * @return + */ + public static String binToHexString(byte[] bytes) { + StringBuilder sb = new StringBuilder(); + + for (int i = 0; i < bytes.length; i++) { + sb.append(Integer.toHexString(binToInt((byte) ((bytes[i] & 0xf0) >> 4))).toUpperCase()); + sb.append(Integer.toHexString(binToInt((byte) (bytes[i] & 0x0f))).toUpperCase()); + } + + return sb.toString(); + } + + + /** + * BIN输出为二进制字符串 + * 如字节数组{0x01, 0x38}转换为字符串"0000000100111000" + * + * @param bytes + * @return + */ + public static String binToBinString(byte[] bytes) { + StringBuilder sb = new StringBuilder(); + + for (int i = 0; i < bytes.length; i++) { + String s = Integer.toBinaryString(binToInt(bytes[i])); + while (s.length() < 8) { + s = "0" + s; + } + sb.append(s); + } + + return sb.toString(); + } + + + /* int转换为BCD码或BIN码 */ + + /** + * 两位整数转换成一个字节的BCD码 + * 大于100的整数取其十位和个位 十位在前个位在后 + * 如79转换为0x79 + * + * @param i + * @return + */ + public static byte intToBcd(int i) { + byte b = 0x00; + while (i > 99) { + i = i % 100; + } + + int l = i % 10; + int h = i / 10; + + b = (byte) (h << 4 | l); + + return b; + } + + /** + * 整数转换为字节数组 高位在前低位在后 + * 如 2379转换为0x23, 0x79 + * + * @param i + * @return + */ + public static byte[] intToBcds(long l) { + String str = String.valueOf(l); + Bytes bytes = new Bytes(); + + if (str.length() % 2 == 1) { + str = "0" + str; + } + + for (int i = 0; i < str.length() - 1; i = i + 2) { + String s = str.substring(i, i + 2); + bytes.append(ByteUtil.intToBcd(Integer.parseInt(s))); + } + + return bytes.toBytes(); + } + + /** + * 整数转换为指定字节数的BCD码数组 高位在前低位在后 从低位开始计算长度 超出部分忽略 不足部分在高位补0 + * 如 3013479指定2字节转换为0x34, 0x79, 指定6字节转换为0x00, 0x00, 0x03, 0x01, 0x34, 0x79 + * + * @param i + * @param length + * @return + */ + public static byte[] intToBcds(long l, int length) { + byte[] bts = intToBcds(l); + if (bts.length == length) { + return bts; + } else { + byte[] bytes = new byte[length]; + for (int j = length - 1; j >= 0; j--) { + if (j + bts.length - length >= 0) { + bytes[j] = bts[j + bts.length - length]; + } else { + bytes[j] = 0x00; + } + } + + return bytes; + } + } + + /** + * 整数转换为指定字节数的BCD码数组 低位在前高位在后 从低位开始计算长度 超出部分忽略 不足部分在高位补0 + * 如 3013479指定2字节转换为0x79, 0x34, 指定6字节转换为0x79, 0x34, 0x01, 0x03, 0x00, 0x00 + * + * @param i + * @param length + * @return + */ + public static byte[] intToBcdsHL(long l, int length) { + Bytes bs = new Bytes(); + + for (int i = 0; i < length; i++) { + int low = (int) (l % 100); + bs.append(ByteUtil.intToBcd(low)); + l = l / 100; + } + + return bs.toBytes(); + } + + /** + * 整数转换为BIN码字节数组 高位在前低位在后 + * 如113479转换为0x01, 0xBB, 0x47 + * + * @param l + * @return + */ + public static byte[] intToBins(long l) { + String str = Long.toHexString(l); + if (str.length() % 2 == 1) { + str = "0" + str; + } + + Bytes bytes = new Bytes(); + for (int i = 0; i < str.length() - 1; i = i + 2) { + String s = str.substring(i, i + 2); + bytes.append((byte) Integer.parseInt(s, 16)); + } + + return bytes.toBytes(); + } + + /** + * 整数转换为指定字节数的BIN码数组 高位在前低位在后 从低位开始计算长度 超出部分忽略 不足部分在高位补0 + * 如 3013479指定2字节转换为0xFB, 0x67, 指定6字节转换为0x00, 0x00, 0x00, 0x2D, 0xFB, 0x67 + * + * @param l + * @param length + * @return + */ + public static byte[] intToBins(long l, int length) { + byte[] bts = intToBins(l); + if (bts.length == length) { + return bts; + } else { + byte[] bytes = new byte[length]; + for (int j = length - 1; j >= 0; j--) { + if (j + bts.length - length >= 0) { + bytes[j] = bts[j + bts.length - length]; + } else { + bytes[j] = 0x00; + } + } + + return bytes; + } + } + + + /* 16进制字符串转换为字节数组 */ + + /** + * 16进制字符串顺序转换为字节数组 + * 如字符串"13810411703"转换为字节数组{0x01, 0x38, 0x10, 0x41, 0x17, 0x03} + * + * @param str + * @return + */ + public static byte[] hexStringToBytes(String str) { + if (str.length() % 2 == 1) { + str = "0" + str; + } + + Bytes bytes = new Bytes(); + for (int i = 0; i < str.length() - 1; i = i + 2) { + String s = str.substring(i, i + 2); + bytes.append((byte) Integer.parseInt(s, 16)); + } + + return bytes.toBytes(); + } + + /** + * 16进制字符串顺序转换为指定字节数的BIN码数组 从低位开始计算长度 超出部分忽略 不足部分在高位补0 + * 如字符串"13810411703"转换为3字节数组{0x41, 0x17, 0x03}, 指定8字节转换为{0x00, 0x00, 0x01, 0x38, 0x10, 0x41, 0x17, 0x03} + * + * @param str + * @param length + * @return + */ + public static byte[] hexStringToBytes(String str, int length) { + byte[] bts = hexStringToBytes(str); + if (bts.length == length) { + return bts; + } else { + byte[] bytes = new byte[length]; + for (int j = length - 1; j >= 0; j--) { + if (j + bts.length - length >= 0) { + bytes[j] = bts[j + bts.length - length]; + } else { + bytes[j] = 0x00; + } + } + + return bytes; + } + } + + + /* 十进制BCD码字符串转换为字节数组 */ + + /** + * 十进制BCD码字符串顺序转换为字节数组 + * 如字符串"13810411703"转换为字节数组{0x01, 0x38, 0x10, 0x41, 0x17, 0x03} + * + * @param str + * @return + */ + public static byte[] bcdStringToBytes(String str) { + if (str.length() % 2 == 1) { + str = "0" + str; + } + + Bytes bytes = new Bytes(); + for (int i = 0; i < str.length() - 1; i = i + 2) { + String s = str.substring(i, i + 2); + bytes.append(ByteUtil.intToBcd(Integer.parseInt(s))); + } + + return bytes.toBytes(); + } + + /** + * 十进制BCD码字符串顺序转换为指定字节数的BCD码数组 从低位开始计算长度 超出部分忽略 不足部分在高位补0 + * 如字符串"13810411703"转换为3字节数组{0x41, 0x17, 0x03}, 指定8字节转换为{0x00, 0x00, 0x01, 0x38, 0x10, 0x41, 0x17, 0x03} + * + * @param str + * @param length + * @return + */ + public static byte[] bcdStringToBytes(String str, int length) { + byte[] bts = bcdStringToBytes(str); + if (bts.length == length) { + return bts; + } else { + byte[] bytes = new byte[length]; + for (int j = length - 1; j >= 0; j--) { + if (j + bts.length - length >= 0) { + bytes[j] = bts[j + bts.length - length]; + } else { + bytes[j] = 0x00; + } + } + + return bytes; + } + } + + public static String buildCRC16(byte[] data) { + byte[] buf = new byte[data.length];// 存储需要产生校验码的数据 + for (int i = 0; i < data.length; i++) { + buf[i] = data[i]; + } + int len = buf.length; + int crc = 0xFFFF;//16位 + for (int pos = 0; pos < len; pos++) { + if (buf[pos] < 0) { + crc ^= (int) buf[pos] + 256; // XOR byte into least sig. byte of + // crc + } else { + crc ^= (int) buf[pos]; // XOR byte into least sig. byte of crc + } + for (int i = 8; i != 0; i--) { // Loop over each bit + if ((crc & 0x0001) != 0) { // If the LSB is set + crc >>= 1; // Shift right and XOR 0xA001 + crc ^= 0xA001; + } else + // Else LSB is not set + crc >>= 1; // Just shift right + } + } + String c = Integer.toHexString(crc); + if (c.length() == 4) { + c = c.substring(2, 4) + c.substring(0, 2); + } else if (c.length() == 3) { + c = "0" + c; + c = c.substring(2, 4) + c.substring(0, 2); + } else if (c.length() == 2) { + c = "0" + c.substring(1, 2) + "0" + c.substring(0, 1); + } + return c; + } + + + public static byte[] byteMergerAll(byte[]... values) { + int length_byte = 0; + for (int i = 0; i < values.length; i++) { + length_byte += values[i].length; + } + byte[] all_byte = new byte[length_byte]; + int countLength = 0; + for (int i = 0; i < values.length; i++) { + byte[] b = values[i]; + System.arraycopy(b, 0, all_byte, countLength, b.length); + countLength += b.length; + } + return all_byte; + } + + public static String convertToHex(String str) { + char[] charArray = str.toCharArray(); + String hexString = ""; + for (char c : charArray) { + hexString += Integer.toHexString((int) c); + } + return hexString; + } + + public static String reverseHex(String hex) { + char[] charArray = hex.toCharArray(); + int length = charArray.length; + int times = length / 2; + for (int c1i = 0; c1i < times; c1i += 2) { + int c2i = c1i + 1; + char c1 = charArray[c1i]; + char c2 = charArray[c2i]; + int c3i = length - c1i - 2; + int c4i = length - c1i - 1; + charArray[c1i] = charArray[c3i]; + charArray[c2i] = charArray[c4i]; + charArray[c3i] = c1; + charArray[c4i] = c2; + } + return new String(charArray); + } + +} diff --git a/casic-server/src/main/java/com/casic/missiles/netty/Bytes.java b/casic-server/src/main/java/com/casic/missiles/netty/Bytes.java new file mode 100644 index 0000000..9031d19 --- /dev/null +++ b/casic-server/src/main/java/com/casic/missiles/netty/Bytes.java @@ -0,0 +1,73 @@ +package com.casic.missiles.netty; + +public class Bytes { + + private byte[] buffer = null; + + public Bytes() { + + } + + public Bytes append(byte b) { + byte[] nb = null; + if (buffer == null) { + nb = new byte[] { b }; + } else { + nb = new byte[buffer.length + 1]; + for (int i = 0; i < buffer.length; i++) { + nb[i] = buffer[i]; + } + nb[nb.length - 1] = b; + } + buffer = nb; + return this; + } + + public Bytes append(byte[] bs) { + if (bs == null) + return this; + byte[] nb = null; + if (buffer == null) { + buffer = bs; + } else { + nb = new byte[buffer.length + bs.length]; + for (int i = 0; i < buffer.length; i++) { + nb[i] = buffer[i]; + } + for (int i = 0; i < bs.length; i++) { + nb[buffer.length + i] = bs[i]; + } + buffer = nb; + } + return this; + } + + public byte[] toBytes() { + return buffer; + } + + public byte[] toBytes(int l) { + Bytes bs = new Bytes(); + + for (int i = 0; i < l; i++) { + if (i < buffer.length) { + bs.append(buffer[i]); + } else { + bs.append((byte) 0x00); + } + } + + return bs.toBytes(); + } + + public static void main(String[] args) { + Bytes bytes = new Bytes(); + int i = 0xf2; + byte a = 0x68; + byte[] b = new byte[] { 0x11, 0x22 }; + byte c = (byte) i; + byte[] r = bytes.append(a).append(b).append(c).toBytes(); + System.out.println(r); + } + +} diff --git a/casic-server/src/main/java/com/casic/missiles/netty/CacheUtils.java b/casic-server/src/main/java/com/casic/missiles/netty/CacheUtils.java new file mode 100644 index 0000000..8d2250a --- /dev/null +++ b/casic-server/src/main/java/com/casic/missiles/netty/CacheUtils.java @@ -0,0 +1,261 @@ +package com.casic.missiles.netty; + +import cn.hutool.core.util.ObjectUtil; +import lombok.extern.slf4j.Slf4j; +import net.jodah.expiringmap.ExpirationPolicy; +import net.jodah.expiringmap.ExpiringMap; +import org.springframework.stereotype.Component; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Set; +import java.util.concurrent.TimeUnit; + +@Slf4j +@Component +public class CacheUtils { + + //电压数据分包计数 + private volatile static int VOL_I = 0; + private volatile static boolean VOL_FLAG = false; + //PSD数据分包计数 + private volatile static int PSD_I = 0; + private volatile static boolean PSD_FLAG = false; + + /** + * 缓存电压信息 + */ + public static final ExpiringMap VOL_MAP = ExpiringMap.builder() + .maxSize(3000) + .expiration(5, TimeUnit.MINUTES) + .variableExpiration() + .expirationPolicy(ExpirationPolicy.ACCESSED) + .build(); + + /** + * 缓存PSD流信息 + */ + public static final ExpiringMap PSD_MAP = ExpiringMap.builder() + .maxSize(3000) + .expiration(5, TimeUnit.MINUTES) + .variableExpiration() +// .expirationListener((key,value) -> { +// System.out.println("key:" + key + "已过期……"); +// }) + .expirationPolicy(ExpirationPolicy.ACCESSED) + .build(); + + + + + public synchronized static String startVolMapCache() { + String msgs = ""; + if (CacheUtils.VOL_MAP.size() > 1) { + Set keys = CacheUtils.VOL_MAP.keySet(); + List keyInts = new ArrayList<>(keys); + Collections.sort(keyInts); + for (Integer i : keyInts) { + msgs += CacheUtils.VOL_MAP.get(i); + } + if (ObjectUtil.isNotEmpty(msgs)) { + CacheUtils.VOL_MAP.clear(); + //继续拆包处理 +// String msgLast = ""; +// if (msgs.indexOf(InstructCode.DAC_HEAD + InstructCode.DAC_HEAD_LENGTH) != -1) { +// if (msgs.length() < (msgs.indexOf(InstructCode.DAC_HEAD + InstructCode.DAC_HEAD_LENGTH) + 24 + InstructCode.DAC_DATA_LENGTH)) { +// msgLast = msgs.substring(msgs.indexOf(msgs.indexOf(InstructCode.DAC_HEAD + InstructCode.DAC_HEAD_LENGTH))); +// CacheUtils.VOL_MAP.put(keyInts.get(keyInts.size() - 1), msgLast); +// } +// } + } + + } + return msgs; + } + + public synchronized static String startPsdMapCache() { + String msgs = ""; + if (CacheUtils.PSD_MAP.size() > 1) { + Set keys = CacheUtils.PSD_MAP.keySet(); + List keyInts = new ArrayList<>(keys); + Collections.sort(keyInts); + for (Integer i : keyInts) { + msgs += CacheUtils.PSD_MAP.get(i); + } + if (ObjectUtil.isNotEmpty(msgs)) { + CacheUtils.PSD_MAP.clear(); + //继续拆包处理 +// String msgLast = ""; +// if (msgs.indexOf(InstructCode.PSD1_HEAD) != -1) { +// if (msgs.length() >= 24) { +// int totalLength = Integer.parseInt(ByteUtil.reverseHex(msgs.substring(msgs.indexOf(InstructCode.PSD1_HEAD) + 16, 24)), 16); +// if (msgs.length() < totalLength * 2) { +// msgLast = msgs.substring(msgs.indexOf(InstructCode.PSD1_HEAD)); +// CacheUtils.PSD_MAP.put(keyInts.get(keyInts.size() - 1), msgLast); +// } +// } +// } + } + } + return msgs; + } + + public static void main(String[] args) { + + String msg="f0f0a5a50300001102000c081b0070000000000059f372000d81030058d50000f0f0a5a50300001102000b081b0070000000000012c7a900bd7e03006ed3000055aa55aa4dfccf1a2c000000f0f0a5a50300001102000b081b00700000000000aa0b2e001080030084d40000"; + + msg="0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000055aa55aa8dfccf1a4e0000002331363337362c31363337362c31363337362c31363337362c31363337362c31363337362c3234342c32303030302c322c3130302c31363338332c302c323030302355aa55aa4dfccf1a2c000000f0f0a5a50300001102000c081b00700000000000df63650092810300e3d50000"; + +// msg="55aa55aa8dfccf1a4e0000002331363337362c31363337362c31363337362c31363337362c31363337362c31363337362c3234342c32303030302c322c3130302c31363338332c302c323030302355aa55aa4dfccf1a4c000000f0f0a5a50300001102000b081b00700000000000933b7100e47d0300a8d20000f0f0a5a50300001102000b081b00700000000000127c88003d810300a6d50000"; +// int totalLength = Integer.parseInt(ByteUtil.reverseHex(msg.substring(msg.indexOf(InstructCode.PSD1_HEAD) + 16, 24)), 16); + + +// msg="55aa55aa4dfccf1a2c000000f0f0a5a50300001102000b081b00700000000000b5560c00597f0300f6d3000055aa55aa4dfccf1a6c000000f0f0a5a50300001102000a081b007000000000005e8623007480030019d50000f0f0a5a50300001102000b081b0070000000000055083c006b7d030023d20000f0f0a5a50300001102000b081b007000000000007d3d3c00196103008fb7000055aa55aa5dfccf1a540000000000000000000000000000000000000000000000df2e000000000000000000000000000000000000000000000000000000000000e02e00000000000000000000000000000000000055aa55aa10fccf1a0c200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; + +// msg="55aa55aa4dfccf1a2c000000"; + + +// +// String[] msgArr = msg.split(InstructCode.PSD1_HEAD); +// if(msgArr.length>2){ +// for(int i=1;i= 24) { +//// log.info("原始msg--->"+msg); +// int totalLength = Integer.parseInt(ByteUtil.reverseHex(msg.substring(msg.indexOf(InstructCode.PSD1_HEAD) + 16, +// msg.indexOf(InstructCode.PSD1_HEAD) + 24)), 16); +// if (msg.length() >= totalLength * 2) { +// +// for (int i = 0; i < (msg.length() - (msg.indexOf(InstructCode.PSD1_HEAD) + 24)) / 64; i++) { +// String msgi = msg.substring((msg.indexOf(InstructCode.PSD1_HEAD) + 24) + i * 64, (msg.indexOf(InstructCode.PSD1_HEAD) + 24) + (i + 1) * 64); +// if (msgi.indexOf(InstructCode.PSD2_HEAD) == 0) { +// String gateOff = msgi.substring(16, 20); +// String baseline = msgi.substring(20, 24); +// String shortGate = msgi.substring(24, 28); +// String longGate = msgi.substring(28, 32); +// String qlong = msgi.substring(48, 56); +// String qshort = msgi.substring(56, 64); +// //toDo:算法计算 +// System.out.println("gateOff--->" + gateOff + +// "baseline--->" + baseline + +// "shortGate--->" + shortGate + +// "longGate--->" + longGate + +// "qlong--->" + qlong + +// "qshort--->" + qshort); +// } +// } +// } +// } + + } + + + public static void proccessData(String msg){ + //拆包处理 PSD数据 + if (msg.indexOf(InstructCode.PSD1_HEAD) != -1) { + + if (msg.length() >= 24) { +// log.info("原始msg--->"+msg); + int totalLength = Integer.parseInt(ByteUtil.reverseHex(msg.substring(msg.indexOf(InstructCode.PSD1_HEAD) + 16, + msg.indexOf(InstructCode.PSD1_HEAD) + 24)), 16); + if (msg.length() >= totalLength * 2) { + PSD_FLAG = false; + for (int i = 0; i < (msg.length() - (msg.indexOf(InstructCode.PSD1_HEAD) + 24)) / 64; i++) { + String msgi = msg.substring((msg.indexOf(InstructCode.PSD1_HEAD) + 24) + i * 64, (msg.indexOf(InstructCode.PSD1_HEAD) + 24) + (i + 1) * 64); + if (msgi.indexOf(InstructCode.PSD2_HEAD) == 0) { + String gateOff = msgi.substring(16, 20); + String baseline = msgi.substring(20, 24); + String shortGate = msgi.substring(24, 28); + String longGate = msgi.substring(28, 32); + String qlong = msgi.substring(48, 56); + String qshort = msgi.substring(56, 64); + //toDo:算法计算 + log.info("gateOff--->" + gateOff + + "baseline--->" + baseline + + "shortGate--->" + shortGate + + "longGate--->" + longGate + + "qlong--->" + qlong + + "qshort--->" + qshort); + } + } + } else { + //2.缺psd数据存入缓存处理 + PSD_FLAG = true; + PSD_I++; + CacheUtils.PSD_MAP.put(PSD_I, msg); +// log.info("psd加入数据:" + PSD_I + "------->" + msg); + } + } + } else if (PSD_FLAG) { + PSD_I++; + CacheUtils.PSD_MAP.put(PSD_I, msg); + String str = startPsdMapCache(); +// log.info("分包psd加入数据:" + PSD_I + "------->" + msg); + if (ObjectUtil.isNotEmpty(str) && str.length() >= 24) { +// log.info("**************strpsd---->" + str); + if (str.indexOf(InstructCode.PSD1_HEAD) != -1 && str.substring(str.indexOf(InstructCode.PSD1_HEAD) + 16).length() >= 8) { + int totalLength = Integer.parseInt(ByteUtil.reverseHex(str.substring(str.indexOf(InstructCode.PSD1_HEAD) + 16, + str.indexOf(InstructCode.PSD1_HEAD) + 24)), 16); + if (str.length() >= totalLength * 2) { + for (int i = 0; i < (str.length() - (str.indexOf(InstructCode.PSD1_HEAD) + 24)) / 64; i++) { + String msgi = str.substring((str.indexOf(InstructCode.PSD1_HEAD) + 24) + i * 64, (str.indexOf(InstructCode.PSD1_HEAD) + 24) + (i + 1) * 64); + if (msgi.indexOf(InstructCode.PSD2_HEAD) == 0) { + String gateOff = msgi.substring(16, 20); + String baseline = msgi.substring(20, 24); + String shortGate = msgi.substring(24, 28); + String longGate = msgi.substring(28, 32); + String qlong = msgi.substring(48, 56); + String qshort = msgi.substring(56, 64); + //toDo:算法计算 + log.info("分包:gateOff--->" + gateOff + + "baseline--->" + baseline + + "shortGate--->" + shortGate + + "longGate--->" + longGate + + "qlong--->" + qlong + + "qshort--->" + qshort); + } + } + } + } + } + } + //拆包处理 电压数据 + if (msg.indexOf(InstructCode.DAC_HEAD + InstructCode.DAC_HEAD_LENGTH) != -1) { + //完整包处理 + if (msg.length() >= (msg.indexOf(InstructCode.DAC_HEAD + InstructCode.DAC_HEAD_LENGTH) + 24 + InstructCode.DAC_DATA_LENGTH)) { + VOL_FLAG = false; + msg = msg.substring(msg.indexOf(InstructCode.DAC_HEAD + InstructCode.DAC_HEAD_LENGTH) + 24); + String[] volArr = InstructCode.getVolArr(msg); + //本设备只用到第6通道 + String vol6 = volArr[5]; + log.info("第6通道电压值-------->" + vol6); + } else { + //分包处理,先放到缓存 + VOL_FLAG = true; + VOL_I++; + CacheUtils.VOL_MAP.put(VOL_I, msg); + } + } else if (VOL_FLAG) { + VOL_I++; + CacheUtils.VOL_MAP.put(VOL_I, msg); + String str = startVolMapCache(); + if (ObjectUtil.isNotEmpty(str) && + str.length() >= (str.indexOf(InstructCode.DAC_HEAD + InstructCode.DAC_HEAD_LENGTH) + 24 + InstructCode.DAC_DATA_LENGTH)) { + VOL_FLAG = false; + str = str.substring(str.indexOf(InstructCode.DAC_HEAD + InstructCode.DAC_HEAD_LENGTH) + 24); + String[] volArr = InstructCode.getVolArr(str); + //本设备只用到第6通道 + String vol6 = volArr[5]; + log.info("分包第6通道电压值-------->" + vol6); + } + } + + } + +} \ No newline at end of file diff --git a/casic-server/src/main/java/com/casic/missiles/netty/HjtDecoder.java b/casic-server/src/main/java/com/casic/missiles/netty/HjtDecoder.java new file mode 100644 index 0000000..2649988 --- /dev/null +++ b/casic-server/src/main/java/com/casic/missiles/netty/HjtDecoder.java @@ -0,0 +1,24 @@ +package com.casic.missiles.netty; + +import io.netty.buffer.ByteBuf; +import io.netty.buffer.ByteBufUtil; +import io.netty.channel.ChannelHandlerContext; +import io.netty.handler.codec.MessageToMessageDecoder; +import lombok.extern.slf4j.Slf4j; + +import java.nio.charset.Charset; +import java.util.List; + +/** + * @description: 将从接口取到的数据编码 + * @author: Stone + * @create: 2019-01-11 15:15 + **/ +@Slf4j +public class HjtDecoder extends MessageToMessageDecoder { + @Override + protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List out) throws Exception { +// log.info("hexBytes : " + ByteBufUtil.hexDump(msg) + ";" + " String : " + msg.toString(Charset.defaultCharset())); + out.add(ByteBufUtil.hexDump(msg)); + } +} diff --git a/casic-server/src/main/java/com/casic/missiles/netty/InstructCode.java b/casic-server/src/main/java/com/casic/missiles/netty/InstructCode.java index 0732347..5c61759 100644 --- a/casic-server/src/main/java/com/casic/missiles/netty/InstructCode.java +++ b/casic-server/src/main/java/com/casic/missiles/netty/InstructCode.java @@ -3,13 +3,13 @@ import org.springframework.stereotype.Component; @Component -public class InstructCode { +public class InstructCode { /** * 设备初始化时需要依次发送1~16号指令 */ public static String[] InitCodeArr = { - "1", "设置高压", "set set_voltage 4 1.2\\n", + "1", "设置高压", "set set_voltage 6 1.2\\n", "2", "复位adc", "cmd set_adc_delay_rst 1 0\\n", "3", "逻辑复位", "cmd set_logic_rst 1 0\\n", "4", "设置波形采样触发方式为下降沿", "cmd set_raw_trig_sel_adc2 3 0\\n", @@ -30,30 +30,51 @@ /** * 加高压指令 */ - public static final String DAC_UP="cmd dac_up 0 0\\n"; + public static final String DAC_UP = "cmd dac_up 0 0\\n"; /** * 关闭高压指令 */ - public static final String DAC_DOWN="cmd dac_down 0 0\\n"; + public static final String DAC_DOWN = "cmd dac_down 0 0\\n"; /** + * ` * 电压采样帧头 */ - public static final String DAC_HEAD="AA55AA551ACFFC5D"; +// public static final String DAC_HEAD="AA55AA551ACFFC5D"; + public static final String DAC_HEAD = "55aa55aa5dfccf1a"; + public static final String DAC_HEAD_LENGTH = "54000000"; + public static final int DAC_DATA_LENGTH = 144; /** * 波形采样帧头 */ - public static final String WAVE_HEAD="AA55AA551ACFFC3D"; +// public static final String WAVE_HEAD="AA55AA551ACFFC3D"; + public static final String WAVE_HEAD = "55aa55aa3dfccf1a"; /** * PSD1采样帧头 */ - public static final String PSD1_HEAD="AA55AA551ACFFC4D"; +// public static final String PSD1_HEAD="AA55AA551ACFFC4D"; + public static final String PSD1_HEAD = "55aa55aa4dfccf1a"; /** * PSD2采样帧头 */ - public static final String PSD2_HEAD="F0F0A5A5"; +// public static final String PSD2_HEAD="F0F0A5A5"; + public static final String PSD2_HEAD = "f0f0a5a5"; + + + public static String[] getVolArr(String msg) { + String[] volArr = new String[24]; + for (int j = 0; j < 24; j++) { + //前16为电压值 + if (j < 16) + volArr[j] = ByteUtil.reverseHex(msg.substring(j * 8, (j + 1) * 8)); + //后8为高低位值 + else + volArr[j] = ByteUtil.reverseHex(msg.substring(128 + (j - 16) * 2, 128 + (j - 15) * 2)); + } + return volArr; + } } diff --git a/casic-server/src/main/java/com/casic/missiles/netty/NettyClient.java b/casic-server/src/main/java/com/casic/missiles/netty/NettyClient.java index 70c4e6e..3626e51 100644 --- a/casic-server/src/main/java/com/casic/missiles/netty/NettyClient.java +++ b/casic-server/src/main/java/com/casic/missiles/netty/NettyClient.java @@ -1,23 +1,18 @@ package com.casic.missiles.netty; import io.netty.bootstrap.Bootstrap; -import io.netty.channel.Channel; -import io.netty.channel.ChannelFuture; -import io.netty.channel.ChannelInitializer; -import io.netty.channel.ChannelOption; -import io.netty.channel.ChannelPipeline; -import io.netty.channel.EventLoopGroup; +import io.netty.buffer.Unpooled; +import io.netty.channel.*; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioSocketChannel; -import io.netty.handler.codec.string.StringDecoder; -import io.netty.handler.codec.string.StringEncoder; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; import javax.annotation.Resource; +import java.nio.charset.StandardCharsets; import java.util.concurrent.ForkJoinPool; @Slf4j @@ -45,8 +40,7 @@ @Override public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline p = ch.pipeline(); - p.addLast("decoder", new StringDecoder()); - p.addLast("encoder", new StringEncoder()); + p.addLast(new HjtDecoder()); p.addLast(nettyClientHandler); } }); @@ -68,7 +62,7 @@ if (mChannel.get() == null) { mChannel.set(mChannelFuture.channel()); } - mChannel.get().writeAndFlush(data); + mChannel.get().writeAndFlush(Unpooled.copiedBuffer(data.getBytes(StandardCharsets.UTF_8))); } catch (Exception e) { log.error(this.getClass().getName().concat(".send has error"), e); } @@ -77,7 +71,8 @@ // 客户端启动,并连上服务器端 @PostConstruct public void init() { - ForkJoinPool.commonPool().submit(() -> startClient("127.0.0.1", 52002)); + ForkJoinPool.commonPool().submit(() -> startClient("192.168.1.50", 52002)); +// ForkJoinPool.commonPool().submit(() -> startClient("127.0.0.1", 52002)); } @PreDestroy diff --git a/casic-server/src/main/java/com/casic/missiles/netty/NettyClientHandler.java b/casic-server/src/main/java/com/casic/missiles/netty/NettyClientHandler.java index b602605..08abcc9 100644 --- a/casic-server/src/main/java/com/casic/missiles/netty/NettyClientHandler.java +++ b/casic-server/src/main/java/com/casic/missiles/netty/NettyClientHandler.java @@ -24,7 +24,11 @@ log.info(String.format("客户端读取到从服务器端:%s 发送过来的数据:%s", ctx.channel().remoteAddress(), msg.toString())); // ctx.channel().writeAndFlush(String.format("server write:%s", msg)); //解析数据 - neutronOptService.analysis(msg.toString()); + try { + neutronOptService.analysis(msg.toString()); + }catch (Exception e){ + e.printStackTrace(); + } } // 捕获到异常的处理 diff --git a/casic-server/src/main/java/com/casic/missiles/neutron/service/impl/NeutronOptServiceImpl.java b/casic-server/src/main/java/com/casic/missiles/neutron/service/impl/NeutronOptServiceImpl.java index 7243ea6..65eeee4 100644 --- a/casic-server/src/main/java/com/casic/missiles/neutron/service/impl/NeutronOptServiceImpl.java +++ b/casic-server/src/main/java/com/casic/missiles/neutron/service/impl/NeutronOptServiceImpl.java @@ -1,13 +1,17 @@ package com.casic.missiles.neutron.service.impl; +import cn.hutool.core.util.ObjectUtil; +import com.casic.missiles.netty.ByteUtil; +import com.casic.missiles.netty.CacheUtils; import com.casic.missiles.netty.InstructCode; import com.casic.missiles.netty.NettyClient; import com.casic.missiles.neutron.service.INeutronOptService; +import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import javax.annotation.Resource; - +@Slf4j @Service public class NeutronOptServiceImpl implements INeutronOptService { @@ -32,23 +36,124 @@ } +// //电压数据分包计数 +// private volatile static int VOL_I = 0; +// private volatile static boolean VOL_FLAG = false; +// //PSD数据分包计数 +// private volatile static int PSD_I = 0; +// private volatile static boolean PSD_FLAG = false; + @Override public void analysis(String msg) { - - if(msg.indexOf(InstructCode.PSD1_HEAD)==0){ - //截取psd流数据 - msg=msg.substring(24); - if(msg.indexOf(InstructCode.PSD2_HEAD)==0&&msg.length()>=64){ - String gateOff= msg.substring(16,20); - String baseline= msg.substring(20,24); - String shortGate= msg.substring(24,28); - String longGate= msg.substring(28,32); - String qlong= msg.substring(48,56); - String qshort= msg.substring(56,64); - //toDo:算法计算 + String[] msgArr = msg.split(InstructCode.PSD1_HEAD); + if(msgArr.length>2){ + for(int i=1;i= 24) { +//// log.info("原始msg--->"+msg); +// int totalLength = Integer.parseInt(ByteUtil.reverseHex(msg.substring(msg.indexOf(InstructCode.PSD1_HEAD) + 16, +// msg.indexOf(InstructCode.PSD1_HEAD) + 24)), 16); +// if (msg.length() >= totalLength * 2) { +// PSD_FLAG = false; +// for (int i = 0; i < (msg.length() - (msg.indexOf(InstructCode.PSD1_HEAD) + 24)) / 64; i++) { +// String msgi = msg.substring((msg.indexOf(InstructCode.PSD1_HEAD) + 24) + i * 64, (msg.indexOf(InstructCode.PSD1_HEAD) + 24) + (i + 1) * 64); +// if (msgi.indexOf(InstructCode.PSD2_HEAD) == 0) { +// String gateOff = msgi.substring(16, 20); +// String baseline = msgi.substring(20, 24); +// String shortGate = msgi.substring(24, 28); +// String longGate = msgi.substring(28, 32); +// String qlong = msgi.substring(48, 56); +// String qshort = msgi.substring(56, 64); +// //toDo:算法计算 +// log.info("gateOff--->" + gateOff + +// "baseline--->" + baseline + +// "shortGate--->" + shortGate + +// "longGate--->" + longGate + +// "qlong--->" + qlong + +// "qshort--->" + qshort); +// } +// } +// } else { +// //2.缺psd数据存入缓存处理 +// PSD_FLAG = true; +// PSD_I++; +// CacheUtils.PSD_MAP.put(PSD_I, msg); +//// log.info("psd加入数据:" + PSD_I + "------->" + msg); +// } +// } +// } else if (PSD_FLAG) { +// PSD_I++; +// CacheUtils.PSD_MAP.put(PSD_I, msg); +// String str = CacheUtils.startPsdMapCache(); +//// log.info("分包psd加入数据:" + PSD_I + "------->" + msg); +// if (ObjectUtil.isNotEmpty(str) && str.length() >= 24) { +//// log.info("**************strpsd---->" + str); +// if (str.indexOf(InstructCode.PSD1_HEAD) != -1 && str.substring(str.indexOf(InstructCode.PSD1_HEAD) + 16).length() >= 8) { +// int totalLength = Integer.parseInt(ByteUtil.reverseHex(str.substring(str.indexOf(InstructCode.PSD1_HEAD) + 16, +// str.indexOf(InstructCode.PSD1_HEAD) + 24)), 16); +// if (str.length() >= totalLength * 2) { +// for (int i = 0; i < (str.length() - (str.indexOf(InstructCode.PSD1_HEAD) + 24)) / 64; i++) { +// String msgi = str.substring((str.indexOf(InstructCode.PSD1_HEAD) + 24) + i * 64, (str.indexOf(InstructCode.PSD1_HEAD) + 24) + (i + 1) * 64); +// if (msgi.indexOf(InstructCode.PSD2_HEAD) == 0) { +// String gateOff = msgi.substring(16, 20); +// String baseline = msgi.substring(20, 24); +// String shortGate = msgi.substring(24, 28); +// String longGate = msgi.substring(28, 32); +// String qlong = msgi.substring(48, 56); +// String qshort = msgi.substring(56, 64); +// //toDo:算法计算 +// log.info("分包:gateOff--->" + gateOff + +// "baseline--->" + baseline + +// "shortGate--->" + shortGate + +// "longGate--->" + longGate + +// "qlong--->" + qlong + +// "qshort--->" + qshort); +// } +// } +// } +// } +// } +// } +// //拆包处理 电压数据 +// if (msg.indexOf(InstructCode.DAC_HEAD + InstructCode.DAC_HEAD_LENGTH) != -1) { +// //完整包处理 +// if (msg.length() >= (msg.indexOf(InstructCode.DAC_HEAD + InstructCode.DAC_HEAD_LENGTH) + 24 + InstructCode.DAC_DATA_LENGTH)) { +// VOL_FLAG = false; +// msg = msg.substring(msg.indexOf(InstructCode.DAC_HEAD + InstructCode.DAC_HEAD_LENGTH) + 24); +// String[] volArr = InstructCode.getVolArr(msg); +// //本设备只用到第6通道 +// String vol6 = volArr[5]; +// log.info("第6通道电压值-------->" + vol6); +// } else { +// //分包处理,先放到缓存 +// VOL_FLAG = true; +// VOL_I++; +// CacheUtils.VOL_MAP.put(VOL_I, msg); +// } +// } else if (VOL_FLAG) { +// VOL_I++; +// CacheUtils.VOL_MAP.put(VOL_I, msg); +// String str = CacheUtils.startVolMapCache(); +// if (ObjectUtil.isNotEmpty(str) && +// str.length() >= (str.indexOf(InstructCode.DAC_HEAD + InstructCode.DAC_HEAD_LENGTH) + 24 + InstructCode.DAC_DATA_LENGTH)) { +// VOL_FLAG = false; +// str = str.substring(str.indexOf(InstructCode.DAC_HEAD + InstructCode.DAC_HEAD_LENGTH) + 24); +// String[] volArr = InstructCode.getVolArr(str); +// //本设备只用到第6通道 +// String vol6 = volArr[5]; +// log.info("分包第6通道电压值-------->" + vol6); +// } +// } + + } @Override @@ -59,6 +164,9 @@ @Override public void control(String command) { switch (command) { + case "init": + this.deviceInit(); + break; case "start": this.pressurize(); break; @@ -70,4 +178,8 @@ break; } } + + } + + diff --git a/casic-server/pom.xml b/casic-server/pom.xml index 8c25977..805cc22 100644 --- a/casic-server/pom.xml +++ b/casic-server/pom.xml @@ -61,6 +61,11 @@ jna 3.0.0 + + net.jodah + expiringmap + 0.5.8 + diff --git a/casic-server/src/main/java/com/casic/missiles/netty/ByteUtil.java b/casic-server/src/main/java/com/casic/missiles/netty/ByteUtil.java new file mode 100644 index 0000000..55c951e --- /dev/null +++ b/casic-server/src/main/java/com/casic/missiles/netty/ByteUtil.java @@ -0,0 +1,517 @@ +package com.casic.missiles.netty; + +import org.springframework.stereotype.Component; + +@Component +public class ByteUtil { + + /* BCD码转换为数值或字符 */ + + /** + * 单一字节中选择指定位bcd码转换为整数 + * 如ByteUtil.bcdToInt((byte) 0x33, 5, 4) = 3 + * + * @param b + * @param highBit 高位,最大7 + * @param lowBit 低位,从0开始 + * @return + */ + public static int bcdToInt(byte b, int highBit, int lowBit) { + int g = highBit - lowBit + 1; + int a = ((int) Math.pow(2, g) - 1) << lowBit; + int b2 = (b & a) >> lowBit; + if (b2 < 0 || b2 >= 10) { + return -1; + } + return b2; + } + + /** + * bcd码转换为整数 错误则返回-1 + * 如ByteUtil.bcdToInt((byte) 0x73) = 73 + * + * @param b 输入字节,默认4位一组,先高后低 + * @return + */ + public static int bcdToInt(byte b) { + int pl = 0x0f; + int ph = 0xf0; + + int h = (ph & b) >> 4; + int l = (pl & b); + + if (h >= 10 || l >= 10) { + return -1; + } + return h * 10 + l; + } + + /** + * bcd码转换为long型整数 + * 如字节数组{0x25, 0x23}转换为2523 + * + * @param + * @return + */ + public static long bcdToLong(byte[] bytes) { + if (ByteUtil.bcdToString(bytes).equals("") == false) + return Long.valueOf(ByteUtil.bcdToString(bytes)).longValue(); + else + return -1; + } + + + /** + * bcd码转换为字符串 按字节顺序 高位在前低位在后 + * 如字节数组{0x01, 0x02, 0x03}转换为"010203" + * + * @param bytes + * @return + */ + public static String bcdToString(byte[] bytes) { + StringBuilder sb = new StringBuilder(); + + for (int i = 0; i < bytes.length; i++) { + if (ByteUtil.bcdToInt(bytes[i], 7, 4) != -1 && ByteUtil.bcdToInt(bytes[i], 3, 0) != -1) { + sb.append(ByteUtil.bcdToInt(bytes[i], 7, 4)); + sb.append(ByteUtil.bcdToInt(bytes[i], 3, 0)); + } + } + + return sb.toString(); + } + + + /* BIN码转换为数值或字符 */ + + /** + * 单一字节中选择指定位bin码转换为整数 + * 如ByteUtil.binToInt((byte) 0xA3, 7, 7) = 1 + * + * @param b + * @param highBit 高位,最大7 + * @param lowBit 低位,从0开始 + * @return + */ + public static int binToInt(byte b, int highBit, int lowBit) throws RuntimeException { + int g = highBit - lowBit + 1; + int a = ((int) Math.pow(2, g) - 1) << lowBit; + int b2 = (b & a) >> lowBit; + if (b2 < 0 || b2 >= 16) { + throw new RuntimeException(); + } + return b2; + } + + /** + * BIN字节转换为整数 + * 如ByteUtil.bcdToInt((byte) 0x7B) = 123 + * + * @param b + * @return + */ + public static int binToInt(byte b) { + int i1 = 0x80 & b; + if (i1 == 0x80) { + int i2 = 0x7f & b; + return 128 + i2; + } else { + return b; + } + } + + /** + * BIN码转换为整数 高位在前低位在后 + * 如ByteUtil.bcdToInt(new byte[] {(byte) 0x7B, 0x22}) = 31522 + * + * @param bytes + * @return + */ + public static int binToInt(byte[] bytes) { + int i10 = 0; + if (null != bytes && bytes.length != 0) { + for (int i = 0; i < bytes.length; i++) { + i10 = i10 + new Double(Math.pow(16, (bytes.length - i - 1) * 2)).intValue() * ByteUtil.binToInt(bytes[i]); + } + } + return i10; + } + + /** + * BIN码转换为LONG型整数 高位在前低位在后 + * 如ByteUtil.bcdToLong(new byte[] {(byte) 0x7B, 0x22, 0x32, 0x9A}) = 2065838746 + * + * @param bytes + * @return + */ + public static int binToLong(byte[] bytes) { + int l10 = 0; + if (null != bytes && bytes.length != 0) { + for (int i = 0; i < bytes.length; i++) { + l10 = l10 + new Double(Math.pow(16, (bytes.length - i - 1) * 2)).intValue() * ByteUtil.binToInt(bytes[i]); + } + } + return l10; + } + + /** + * 字节数组转换为16进制字符串 按字节数组顺序转换 + * 如字节数组{0x21, 0x06, 0x01, 0x04, 0x40, (byte) 0xE2, 0x01}转换为"2106010440E201" + * + * @param bytes + * @return + */ + public static String binToHexString(byte[] bytes) { + StringBuilder sb = new StringBuilder(); + + for (int i = 0; i < bytes.length; i++) { + sb.append(Integer.toHexString(binToInt((byte) ((bytes[i] & 0xf0) >> 4))).toUpperCase()); + sb.append(Integer.toHexString(binToInt((byte) (bytes[i] & 0x0f))).toUpperCase()); + } + + return sb.toString(); + } + + + /** + * BIN输出为二进制字符串 + * 如字节数组{0x01, 0x38}转换为字符串"0000000100111000" + * + * @param bytes + * @return + */ + public static String binToBinString(byte[] bytes) { + StringBuilder sb = new StringBuilder(); + + for (int i = 0; i < bytes.length; i++) { + String s = Integer.toBinaryString(binToInt(bytes[i])); + while (s.length() < 8) { + s = "0" + s; + } + sb.append(s); + } + + return sb.toString(); + } + + + /* int转换为BCD码或BIN码 */ + + /** + * 两位整数转换成一个字节的BCD码 + * 大于100的整数取其十位和个位 十位在前个位在后 + * 如79转换为0x79 + * + * @param i + * @return + */ + public static byte intToBcd(int i) { + byte b = 0x00; + while (i > 99) { + i = i % 100; + } + + int l = i % 10; + int h = i / 10; + + b = (byte) (h << 4 | l); + + return b; + } + + /** + * 整数转换为字节数组 高位在前低位在后 + * 如 2379转换为0x23, 0x79 + * + * @param i + * @return + */ + public static byte[] intToBcds(long l) { + String str = String.valueOf(l); + Bytes bytes = new Bytes(); + + if (str.length() % 2 == 1) { + str = "0" + str; + } + + for (int i = 0; i < str.length() - 1; i = i + 2) { + String s = str.substring(i, i + 2); + bytes.append(ByteUtil.intToBcd(Integer.parseInt(s))); + } + + return bytes.toBytes(); + } + + /** + * 整数转换为指定字节数的BCD码数组 高位在前低位在后 从低位开始计算长度 超出部分忽略 不足部分在高位补0 + * 如 3013479指定2字节转换为0x34, 0x79, 指定6字节转换为0x00, 0x00, 0x03, 0x01, 0x34, 0x79 + * + * @param i + * @param length + * @return + */ + public static byte[] intToBcds(long l, int length) { + byte[] bts = intToBcds(l); + if (bts.length == length) { + return bts; + } else { + byte[] bytes = new byte[length]; + for (int j = length - 1; j >= 0; j--) { + if (j + bts.length - length >= 0) { + bytes[j] = bts[j + bts.length - length]; + } else { + bytes[j] = 0x00; + } + } + + return bytes; + } + } + + /** + * 整数转换为指定字节数的BCD码数组 低位在前高位在后 从低位开始计算长度 超出部分忽略 不足部分在高位补0 + * 如 3013479指定2字节转换为0x79, 0x34, 指定6字节转换为0x79, 0x34, 0x01, 0x03, 0x00, 0x00 + * + * @param i + * @param length + * @return + */ + public static byte[] intToBcdsHL(long l, int length) { + Bytes bs = new Bytes(); + + for (int i = 0; i < length; i++) { + int low = (int) (l % 100); + bs.append(ByteUtil.intToBcd(low)); + l = l / 100; + } + + return bs.toBytes(); + } + + /** + * 整数转换为BIN码字节数组 高位在前低位在后 + * 如113479转换为0x01, 0xBB, 0x47 + * + * @param l + * @return + */ + public static byte[] intToBins(long l) { + String str = Long.toHexString(l); + if (str.length() % 2 == 1) { + str = "0" + str; + } + + Bytes bytes = new Bytes(); + for (int i = 0; i < str.length() - 1; i = i + 2) { + String s = str.substring(i, i + 2); + bytes.append((byte) Integer.parseInt(s, 16)); + } + + return bytes.toBytes(); + } + + /** + * 整数转换为指定字节数的BIN码数组 高位在前低位在后 从低位开始计算长度 超出部分忽略 不足部分在高位补0 + * 如 3013479指定2字节转换为0xFB, 0x67, 指定6字节转换为0x00, 0x00, 0x00, 0x2D, 0xFB, 0x67 + * + * @param l + * @param length + * @return + */ + public static byte[] intToBins(long l, int length) { + byte[] bts = intToBins(l); + if (bts.length == length) { + return bts; + } else { + byte[] bytes = new byte[length]; + for (int j = length - 1; j >= 0; j--) { + if (j + bts.length - length >= 0) { + bytes[j] = bts[j + bts.length - length]; + } else { + bytes[j] = 0x00; + } + } + + return bytes; + } + } + + + /* 16进制字符串转换为字节数组 */ + + /** + * 16进制字符串顺序转换为字节数组 + * 如字符串"13810411703"转换为字节数组{0x01, 0x38, 0x10, 0x41, 0x17, 0x03} + * + * @param str + * @return + */ + public static byte[] hexStringToBytes(String str) { + if (str.length() % 2 == 1) { + str = "0" + str; + } + + Bytes bytes = new Bytes(); + for (int i = 0; i < str.length() - 1; i = i + 2) { + String s = str.substring(i, i + 2); + bytes.append((byte) Integer.parseInt(s, 16)); + } + + return bytes.toBytes(); + } + + /** + * 16进制字符串顺序转换为指定字节数的BIN码数组 从低位开始计算长度 超出部分忽略 不足部分在高位补0 + * 如字符串"13810411703"转换为3字节数组{0x41, 0x17, 0x03}, 指定8字节转换为{0x00, 0x00, 0x01, 0x38, 0x10, 0x41, 0x17, 0x03} + * + * @param str + * @param length + * @return + */ + public static byte[] hexStringToBytes(String str, int length) { + byte[] bts = hexStringToBytes(str); + if (bts.length == length) { + return bts; + } else { + byte[] bytes = new byte[length]; + for (int j = length - 1; j >= 0; j--) { + if (j + bts.length - length >= 0) { + bytes[j] = bts[j + bts.length - length]; + } else { + bytes[j] = 0x00; + } + } + + return bytes; + } + } + + + /* 十进制BCD码字符串转换为字节数组 */ + + /** + * 十进制BCD码字符串顺序转换为字节数组 + * 如字符串"13810411703"转换为字节数组{0x01, 0x38, 0x10, 0x41, 0x17, 0x03} + * + * @param str + * @return + */ + public static byte[] bcdStringToBytes(String str) { + if (str.length() % 2 == 1) { + str = "0" + str; + } + + Bytes bytes = new Bytes(); + for (int i = 0; i < str.length() - 1; i = i + 2) { + String s = str.substring(i, i + 2); + bytes.append(ByteUtil.intToBcd(Integer.parseInt(s))); + } + + return bytes.toBytes(); + } + + /** + * 十进制BCD码字符串顺序转换为指定字节数的BCD码数组 从低位开始计算长度 超出部分忽略 不足部分在高位补0 + * 如字符串"13810411703"转换为3字节数组{0x41, 0x17, 0x03}, 指定8字节转换为{0x00, 0x00, 0x01, 0x38, 0x10, 0x41, 0x17, 0x03} + * + * @param str + * @param length + * @return + */ + public static byte[] bcdStringToBytes(String str, int length) { + byte[] bts = bcdStringToBytes(str); + if (bts.length == length) { + return bts; + } else { + byte[] bytes = new byte[length]; + for (int j = length - 1; j >= 0; j--) { + if (j + bts.length - length >= 0) { + bytes[j] = bts[j + bts.length - length]; + } else { + bytes[j] = 0x00; + } + } + + return bytes; + } + } + + public static String buildCRC16(byte[] data) { + byte[] buf = new byte[data.length];// 存储需要产生校验码的数据 + for (int i = 0; i < data.length; i++) { + buf[i] = data[i]; + } + int len = buf.length; + int crc = 0xFFFF;//16位 + for (int pos = 0; pos < len; pos++) { + if (buf[pos] < 0) { + crc ^= (int) buf[pos] + 256; // XOR byte into least sig. byte of + // crc + } else { + crc ^= (int) buf[pos]; // XOR byte into least sig. byte of crc + } + for (int i = 8; i != 0; i--) { // Loop over each bit + if ((crc & 0x0001) != 0) { // If the LSB is set + crc >>= 1; // Shift right and XOR 0xA001 + crc ^= 0xA001; + } else + // Else LSB is not set + crc >>= 1; // Just shift right + } + } + String c = Integer.toHexString(crc); + if (c.length() == 4) { + c = c.substring(2, 4) + c.substring(0, 2); + } else if (c.length() == 3) { + c = "0" + c; + c = c.substring(2, 4) + c.substring(0, 2); + } else if (c.length() == 2) { + c = "0" + c.substring(1, 2) + "0" + c.substring(0, 1); + } + return c; + } + + + public static byte[] byteMergerAll(byte[]... values) { + int length_byte = 0; + for (int i = 0; i < values.length; i++) { + length_byte += values[i].length; + } + byte[] all_byte = new byte[length_byte]; + int countLength = 0; + for (int i = 0; i < values.length; i++) { + byte[] b = values[i]; + System.arraycopy(b, 0, all_byte, countLength, b.length); + countLength += b.length; + } + return all_byte; + } + + public static String convertToHex(String str) { + char[] charArray = str.toCharArray(); + String hexString = ""; + for (char c : charArray) { + hexString += Integer.toHexString((int) c); + } + return hexString; + } + + public static String reverseHex(String hex) { + char[] charArray = hex.toCharArray(); + int length = charArray.length; + int times = length / 2; + for (int c1i = 0; c1i < times; c1i += 2) { + int c2i = c1i + 1; + char c1 = charArray[c1i]; + char c2 = charArray[c2i]; + int c3i = length - c1i - 2; + int c4i = length - c1i - 1; + charArray[c1i] = charArray[c3i]; + charArray[c2i] = charArray[c4i]; + charArray[c3i] = c1; + charArray[c4i] = c2; + } + return new String(charArray); + } + +} diff --git a/casic-server/src/main/java/com/casic/missiles/netty/Bytes.java b/casic-server/src/main/java/com/casic/missiles/netty/Bytes.java new file mode 100644 index 0000000..9031d19 --- /dev/null +++ b/casic-server/src/main/java/com/casic/missiles/netty/Bytes.java @@ -0,0 +1,73 @@ +package com.casic.missiles.netty; + +public class Bytes { + + private byte[] buffer = null; + + public Bytes() { + + } + + public Bytes append(byte b) { + byte[] nb = null; + if (buffer == null) { + nb = new byte[] { b }; + } else { + nb = new byte[buffer.length + 1]; + for (int i = 0; i < buffer.length; i++) { + nb[i] = buffer[i]; + } + nb[nb.length - 1] = b; + } + buffer = nb; + return this; + } + + public Bytes append(byte[] bs) { + if (bs == null) + return this; + byte[] nb = null; + if (buffer == null) { + buffer = bs; + } else { + nb = new byte[buffer.length + bs.length]; + for (int i = 0; i < buffer.length; i++) { + nb[i] = buffer[i]; + } + for (int i = 0; i < bs.length; i++) { + nb[buffer.length + i] = bs[i]; + } + buffer = nb; + } + return this; + } + + public byte[] toBytes() { + return buffer; + } + + public byte[] toBytes(int l) { + Bytes bs = new Bytes(); + + for (int i = 0; i < l; i++) { + if (i < buffer.length) { + bs.append(buffer[i]); + } else { + bs.append((byte) 0x00); + } + } + + return bs.toBytes(); + } + + public static void main(String[] args) { + Bytes bytes = new Bytes(); + int i = 0xf2; + byte a = 0x68; + byte[] b = new byte[] { 0x11, 0x22 }; + byte c = (byte) i; + byte[] r = bytes.append(a).append(b).append(c).toBytes(); + System.out.println(r); + } + +} diff --git a/casic-server/src/main/java/com/casic/missiles/netty/CacheUtils.java b/casic-server/src/main/java/com/casic/missiles/netty/CacheUtils.java new file mode 100644 index 0000000..8d2250a --- /dev/null +++ b/casic-server/src/main/java/com/casic/missiles/netty/CacheUtils.java @@ -0,0 +1,261 @@ +package com.casic.missiles.netty; + +import cn.hutool.core.util.ObjectUtil; +import lombok.extern.slf4j.Slf4j; +import net.jodah.expiringmap.ExpirationPolicy; +import net.jodah.expiringmap.ExpiringMap; +import org.springframework.stereotype.Component; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Set; +import java.util.concurrent.TimeUnit; + +@Slf4j +@Component +public class CacheUtils { + + //电压数据分包计数 + private volatile static int VOL_I = 0; + private volatile static boolean VOL_FLAG = false; + //PSD数据分包计数 + private volatile static int PSD_I = 0; + private volatile static boolean PSD_FLAG = false; + + /** + * 缓存电压信息 + */ + public static final ExpiringMap VOL_MAP = ExpiringMap.builder() + .maxSize(3000) + .expiration(5, TimeUnit.MINUTES) + .variableExpiration() + .expirationPolicy(ExpirationPolicy.ACCESSED) + .build(); + + /** + * 缓存PSD流信息 + */ + public static final ExpiringMap PSD_MAP = ExpiringMap.builder() + .maxSize(3000) + .expiration(5, TimeUnit.MINUTES) + .variableExpiration() +// .expirationListener((key,value) -> { +// System.out.println("key:" + key + "已过期……"); +// }) + .expirationPolicy(ExpirationPolicy.ACCESSED) + .build(); + + + + + public synchronized static String startVolMapCache() { + String msgs = ""; + if (CacheUtils.VOL_MAP.size() > 1) { + Set keys = CacheUtils.VOL_MAP.keySet(); + List keyInts = new ArrayList<>(keys); + Collections.sort(keyInts); + for (Integer i : keyInts) { + msgs += CacheUtils.VOL_MAP.get(i); + } + if (ObjectUtil.isNotEmpty(msgs)) { + CacheUtils.VOL_MAP.clear(); + //继续拆包处理 +// String msgLast = ""; +// if (msgs.indexOf(InstructCode.DAC_HEAD + InstructCode.DAC_HEAD_LENGTH) != -1) { +// if (msgs.length() < (msgs.indexOf(InstructCode.DAC_HEAD + InstructCode.DAC_HEAD_LENGTH) + 24 + InstructCode.DAC_DATA_LENGTH)) { +// msgLast = msgs.substring(msgs.indexOf(msgs.indexOf(InstructCode.DAC_HEAD + InstructCode.DAC_HEAD_LENGTH))); +// CacheUtils.VOL_MAP.put(keyInts.get(keyInts.size() - 1), msgLast); +// } +// } + } + + } + return msgs; + } + + public synchronized static String startPsdMapCache() { + String msgs = ""; + if (CacheUtils.PSD_MAP.size() > 1) { + Set keys = CacheUtils.PSD_MAP.keySet(); + List keyInts = new ArrayList<>(keys); + Collections.sort(keyInts); + for (Integer i : keyInts) { + msgs += CacheUtils.PSD_MAP.get(i); + } + if (ObjectUtil.isNotEmpty(msgs)) { + CacheUtils.PSD_MAP.clear(); + //继续拆包处理 +// String msgLast = ""; +// if (msgs.indexOf(InstructCode.PSD1_HEAD) != -1) { +// if (msgs.length() >= 24) { +// int totalLength = Integer.parseInt(ByteUtil.reverseHex(msgs.substring(msgs.indexOf(InstructCode.PSD1_HEAD) + 16, 24)), 16); +// if (msgs.length() < totalLength * 2) { +// msgLast = msgs.substring(msgs.indexOf(InstructCode.PSD1_HEAD)); +// CacheUtils.PSD_MAP.put(keyInts.get(keyInts.size() - 1), msgLast); +// } +// } +// } + } + } + return msgs; + } + + public static void main(String[] args) { + + String msg="f0f0a5a50300001102000c081b0070000000000059f372000d81030058d50000f0f0a5a50300001102000b081b0070000000000012c7a900bd7e03006ed3000055aa55aa4dfccf1a2c000000f0f0a5a50300001102000b081b00700000000000aa0b2e001080030084d40000"; + + msg="0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000055aa55aa8dfccf1a4e0000002331363337362c31363337362c31363337362c31363337362c31363337362c31363337362c3234342c32303030302c322c3130302c31363338332c302c323030302355aa55aa4dfccf1a2c000000f0f0a5a50300001102000c081b00700000000000df63650092810300e3d50000"; + +// msg="55aa55aa8dfccf1a4e0000002331363337362c31363337362c31363337362c31363337362c31363337362c31363337362c3234342c32303030302c322c3130302c31363338332c302c323030302355aa55aa4dfccf1a4c000000f0f0a5a50300001102000b081b00700000000000933b7100e47d0300a8d20000f0f0a5a50300001102000b081b00700000000000127c88003d810300a6d50000"; +// int totalLength = Integer.parseInt(ByteUtil.reverseHex(msg.substring(msg.indexOf(InstructCode.PSD1_HEAD) + 16, 24)), 16); + + +// msg="55aa55aa4dfccf1a2c000000f0f0a5a50300001102000b081b00700000000000b5560c00597f0300f6d3000055aa55aa4dfccf1a6c000000f0f0a5a50300001102000a081b007000000000005e8623007480030019d50000f0f0a5a50300001102000b081b0070000000000055083c006b7d030023d20000f0f0a5a50300001102000b081b007000000000007d3d3c00196103008fb7000055aa55aa5dfccf1a540000000000000000000000000000000000000000000000df2e000000000000000000000000000000000000000000000000000000000000e02e00000000000000000000000000000000000055aa55aa10fccf1a0c200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; + +// msg="55aa55aa4dfccf1a2c000000"; + + +// +// String[] msgArr = msg.split(InstructCode.PSD1_HEAD); +// if(msgArr.length>2){ +// for(int i=1;i= 24) { +//// log.info("原始msg--->"+msg); +// int totalLength = Integer.parseInt(ByteUtil.reverseHex(msg.substring(msg.indexOf(InstructCode.PSD1_HEAD) + 16, +// msg.indexOf(InstructCode.PSD1_HEAD) + 24)), 16); +// if (msg.length() >= totalLength * 2) { +// +// for (int i = 0; i < (msg.length() - (msg.indexOf(InstructCode.PSD1_HEAD) + 24)) / 64; i++) { +// String msgi = msg.substring((msg.indexOf(InstructCode.PSD1_HEAD) + 24) + i * 64, (msg.indexOf(InstructCode.PSD1_HEAD) + 24) + (i + 1) * 64); +// if (msgi.indexOf(InstructCode.PSD2_HEAD) == 0) { +// String gateOff = msgi.substring(16, 20); +// String baseline = msgi.substring(20, 24); +// String shortGate = msgi.substring(24, 28); +// String longGate = msgi.substring(28, 32); +// String qlong = msgi.substring(48, 56); +// String qshort = msgi.substring(56, 64); +// //toDo:算法计算 +// System.out.println("gateOff--->" + gateOff + +// "baseline--->" + baseline + +// "shortGate--->" + shortGate + +// "longGate--->" + longGate + +// "qlong--->" + qlong + +// "qshort--->" + qshort); +// } +// } +// } +// } + + } + + + public static void proccessData(String msg){ + //拆包处理 PSD数据 + if (msg.indexOf(InstructCode.PSD1_HEAD) != -1) { + + if (msg.length() >= 24) { +// log.info("原始msg--->"+msg); + int totalLength = Integer.parseInt(ByteUtil.reverseHex(msg.substring(msg.indexOf(InstructCode.PSD1_HEAD) + 16, + msg.indexOf(InstructCode.PSD1_HEAD) + 24)), 16); + if (msg.length() >= totalLength * 2) { + PSD_FLAG = false; + for (int i = 0; i < (msg.length() - (msg.indexOf(InstructCode.PSD1_HEAD) + 24)) / 64; i++) { + String msgi = msg.substring((msg.indexOf(InstructCode.PSD1_HEAD) + 24) + i * 64, (msg.indexOf(InstructCode.PSD1_HEAD) + 24) + (i + 1) * 64); + if (msgi.indexOf(InstructCode.PSD2_HEAD) == 0) { + String gateOff = msgi.substring(16, 20); + String baseline = msgi.substring(20, 24); + String shortGate = msgi.substring(24, 28); + String longGate = msgi.substring(28, 32); + String qlong = msgi.substring(48, 56); + String qshort = msgi.substring(56, 64); + //toDo:算法计算 + log.info("gateOff--->" + gateOff + + "baseline--->" + baseline + + "shortGate--->" + shortGate + + "longGate--->" + longGate + + "qlong--->" + qlong + + "qshort--->" + qshort); + } + } + } else { + //2.缺psd数据存入缓存处理 + PSD_FLAG = true; + PSD_I++; + CacheUtils.PSD_MAP.put(PSD_I, msg); +// log.info("psd加入数据:" + PSD_I + "------->" + msg); + } + } + } else if (PSD_FLAG) { + PSD_I++; + CacheUtils.PSD_MAP.put(PSD_I, msg); + String str = startPsdMapCache(); +// log.info("分包psd加入数据:" + PSD_I + "------->" + msg); + if (ObjectUtil.isNotEmpty(str) && str.length() >= 24) { +// log.info("**************strpsd---->" + str); + if (str.indexOf(InstructCode.PSD1_HEAD) != -1 && str.substring(str.indexOf(InstructCode.PSD1_HEAD) + 16).length() >= 8) { + int totalLength = Integer.parseInt(ByteUtil.reverseHex(str.substring(str.indexOf(InstructCode.PSD1_HEAD) + 16, + str.indexOf(InstructCode.PSD1_HEAD) + 24)), 16); + if (str.length() >= totalLength * 2) { + for (int i = 0; i < (str.length() - (str.indexOf(InstructCode.PSD1_HEAD) + 24)) / 64; i++) { + String msgi = str.substring((str.indexOf(InstructCode.PSD1_HEAD) + 24) + i * 64, (str.indexOf(InstructCode.PSD1_HEAD) + 24) + (i + 1) * 64); + if (msgi.indexOf(InstructCode.PSD2_HEAD) == 0) { + String gateOff = msgi.substring(16, 20); + String baseline = msgi.substring(20, 24); + String shortGate = msgi.substring(24, 28); + String longGate = msgi.substring(28, 32); + String qlong = msgi.substring(48, 56); + String qshort = msgi.substring(56, 64); + //toDo:算法计算 + log.info("分包:gateOff--->" + gateOff + + "baseline--->" + baseline + + "shortGate--->" + shortGate + + "longGate--->" + longGate + + "qlong--->" + qlong + + "qshort--->" + qshort); + } + } + } + } + } + } + //拆包处理 电压数据 + if (msg.indexOf(InstructCode.DAC_HEAD + InstructCode.DAC_HEAD_LENGTH) != -1) { + //完整包处理 + if (msg.length() >= (msg.indexOf(InstructCode.DAC_HEAD + InstructCode.DAC_HEAD_LENGTH) + 24 + InstructCode.DAC_DATA_LENGTH)) { + VOL_FLAG = false; + msg = msg.substring(msg.indexOf(InstructCode.DAC_HEAD + InstructCode.DAC_HEAD_LENGTH) + 24); + String[] volArr = InstructCode.getVolArr(msg); + //本设备只用到第6通道 + String vol6 = volArr[5]; + log.info("第6通道电压值-------->" + vol6); + } else { + //分包处理,先放到缓存 + VOL_FLAG = true; + VOL_I++; + CacheUtils.VOL_MAP.put(VOL_I, msg); + } + } else if (VOL_FLAG) { + VOL_I++; + CacheUtils.VOL_MAP.put(VOL_I, msg); + String str = startVolMapCache(); + if (ObjectUtil.isNotEmpty(str) && + str.length() >= (str.indexOf(InstructCode.DAC_HEAD + InstructCode.DAC_HEAD_LENGTH) + 24 + InstructCode.DAC_DATA_LENGTH)) { + VOL_FLAG = false; + str = str.substring(str.indexOf(InstructCode.DAC_HEAD + InstructCode.DAC_HEAD_LENGTH) + 24); + String[] volArr = InstructCode.getVolArr(str); + //本设备只用到第6通道 + String vol6 = volArr[5]; + log.info("分包第6通道电压值-------->" + vol6); + } + } + + } + +} \ No newline at end of file diff --git a/casic-server/src/main/java/com/casic/missiles/netty/HjtDecoder.java b/casic-server/src/main/java/com/casic/missiles/netty/HjtDecoder.java new file mode 100644 index 0000000..2649988 --- /dev/null +++ b/casic-server/src/main/java/com/casic/missiles/netty/HjtDecoder.java @@ -0,0 +1,24 @@ +package com.casic.missiles.netty; + +import io.netty.buffer.ByteBuf; +import io.netty.buffer.ByteBufUtil; +import io.netty.channel.ChannelHandlerContext; +import io.netty.handler.codec.MessageToMessageDecoder; +import lombok.extern.slf4j.Slf4j; + +import java.nio.charset.Charset; +import java.util.List; + +/** + * @description: 将从接口取到的数据编码 + * @author: Stone + * @create: 2019-01-11 15:15 + **/ +@Slf4j +public class HjtDecoder extends MessageToMessageDecoder { + @Override + protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List out) throws Exception { +// log.info("hexBytes : " + ByteBufUtil.hexDump(msg) + ";" + " String : " + msg.toString(Charset.defaultCharset())); + out.add(ByteBufUtil.hexDump(msg)); + } +} diff --git a/casic-server/src/main/java/com/casic/missiles/netty/InstructCode.java b/casic-server/src/main/java/com/casic/missiles/netty/InstructCode.java index 0732347..5c61759 100644 --- a/casic-server/src/main/java/com/casic/missiles/netty/InstructCode.java +++ b/casic-server/src/main/java/com/casic/missiles/netty/InstructCode.java @@ -3,13 +3,13 @@ import org.springframework.stereotype.Component; @Component -public class InstructCode { +public class InstructCode { /** * 设备初始化时需要依次发送1~16号指令 */ public static String[] InitCodeArr = { - "1", "设置高压", "set set_voltage 4 1.2\\n", + "1", "设置高压", "set set_voltage 6 1.2\\n", "2", "复位adc", "cmd set_adc_delay_rst 1 0\\n", "3", "逻辑复位", "cmd set_logic_rst 1 0\\n", "4", "设置波形采样触发方式为下降沿", "cmd set_raw_trig_sel_adc2 3 0\\n", @@ -30,30 +30,51 @@ /** * 加高压指令 */ - public static final String DAC_UP="cmd dac_up 0 0\\n"; + public static final String DAC_UP = "cmd dac_up 0 0\\n"; /** * 关闭高压指令 */ - public static final String DAC_DOWN="cmd dac_down 0 0\\n"; + public static final String DAC_DOWN = "cmd dac_down 0 0\\n"; /** + * ` * 电压采样帧头 */ - public static final String DAC_HEAD="AA55AA551ACFFC5D"; +// public static final String DAC_HEAD="AA55AA551ACFFC5D"; + public static final String DAC_HEAD = "55aa55aa5dfccf1a"; + public static final String DAC_HEAD_LENGTH = "54000000"; + public static final int DAC_DATA_LENGTH = 144; /** * 波形采样帧头 */ - public static final String WAVE_HEAD="AA55AA551ACFFC3D"; +// public static final String WAVE_HEAD="AA55AA551ACFFC3D"; + public static final String WAVE_HEAD = "55aa55aa3dfccf1a"; /** * PSD1采样帧头 */ - public static final String PSD1_HEAD="AA55AA551ACFFC4D"; +// public static final String PSD1_HEAD="AA55AA551ACFFC4D"; + public static final String PSD1_HEAD = "55aa55aa4dfccf1a"; /** * PSD2采样帧头 */ - public static final String PSD2_HEAD="F0F0A5A5"; +// public static final String PSD2_HEAD="F0F0A5A5"; + public static final String PSD2_HEAD = "f0f0a5a5"; + + + public static String[] getVolArr(String msg) { + String[] volArr = new String[24]; + for (int j = 0; j < 24; j++) { + //前16为电压值 + if (j < 16) + volArr[j] = ByteUtil.reverseHex(msg.substring(j * 8, (j + 1) * 8)); + //后8为高低位值 + else + volArr[j] = ByteUtil.reverseHex(msg.substring(128 + (j - 16) * 2, 128 + (j - 15) * 2)); + } + return volArr; + } } diff --git a/casic-server/src/main/java/com/casic/missiles/netty/NettyClient.java b/casic-server/src/main/java/com/casic/missiles/netty/NettyClient.java index 70c4e6e..3626e51 100644 --- a/casic-server/src/main/java/com/casic/missiles/netty/NettyClient.java +++ b/casic-server/src/main/java/com/casic/missiles/netty/NettyClient.java @@ -1,23 +1,18 @@ package com.casic.missiles.netty; import io.netty.bootstrap.Bootstrap; -import io.netty.channel.Channel; -import io.netty.channel.ChannelFuture; -import io.netty.channel.ChannelInitializer; -import io.netty.channel.ChannelOption; -import io.netty.channel.ChannelPipeline; -import io.netty.channel.EventLoopGroup; +import io.netty.buffer.Unpooled; +import io.netty.channel.*; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioSocketChannel; -import io.netty.handler.codec.string.StringDecoder; -import io.netty.handler.codec.string.StringEncoder; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; import javax.annotation.Resource; +import java.nio.charset.StandardCharsets; import java.util.concurrent.ForkJoinPool; @Slf4j @@ -45,8 +40,7 @@ @Override public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline p = ch.pipeline(); - p.addLast("decoder", new StringDecoder()); - p.addLast("encoder", new StringEncoder()); + p.addLast(new HjtDecoder()); p.addLast(nettyClientHandler); } }); @@ -68,7 +62,7 @@ if (mChannel.get() == null) { mChannel.set(mChannelFuture.channel()); } - mChannel.get().writeAndFlush(data); + mChannel.get().writeAndFlush(Unpooled.copiedBuffer(data.getBytes(StandardCharsets.UTF_8))); } catch (Exception e) { log.error(this.getClass().getName().concat(".send has error"), e); } @@ -77,7 +71,8 @@ // 客户端启动,并连上服务器端 @PostConstruct public void init() { - ForkJoinPool.commonPool().submit(() -> startClient("127.0.0.1", 52002)); + ForkJoinPool.commonPool().submit(() -> startClient("192.168.1.50", 52002)); +// ForkJoinPool.commonPool().submit(() -> startClient("127.0.0.1", 52002)); } @PreDestroy diff --git a/casic-server/src/main/java/com/casic/missiles/netty/NettyClientHandler.java b/casic-server/src/main/java/com/casic/missiles/netty/NettyClientHandler.java index b602605..08abcc9 100644 --- a/casic-server/src/main/java/com/casic/missiles/netty/NettyClientHandler.java +++ b/casic-server/src/main/java/com/casic/missiles/netty/NettyClientHandler.java @@ -24,7 +24,11 @@ log.info(String.format("客户端读取到从服务器端:%s 发送过来的数据:%s", ctx.channel().remoteAddress(), msg.toString())); // ctx.channel().writeAndFlush(String.format("server write:%s", msg)); //解析数据 - neutronOptService.analysis(msg.toString()); + try { + neutronOptService.analysis(msg.toString()); + }catch (Exception e){ + e.printStackTrace(); + } } // 捕获到异常的处理 diff --git a/casic-server/src/main/java/com/casic/missiles/neutron/service/impl/NeutronOptServiceImpl.java b/casic-server/src/main/java/com/casic/missiles/neutron/service/impl/NeutronOptServiceImpl.java index 7243ea6..65eeee4 100644 --- a/casic-server/src/main/java/com/casic/missiles/neutron/service/impl/NeutronOptServiceImpl.java +++ b/casic-server/src/main/java/com/casic/missiles/neutron/service/impl/NeutronOptServiceImpl.java @@ -1,13 +1,17 @@ package com.casic.missiles.neutron.service.impl; +import cn.hutool.core.util.ObjectUtil; +import com.casic.missiles.netty.ByteUtil; +import com.casic.missiles.netty.CacheUtils; import com.casic.missiles.netty.InstructCode; import com.casic.missiles.netty.NettyClient; import com.casic.missiles.neutron.service.INeutronOptService; +import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import javax.annotation.Resource; - +@Slf4j @Service public class NeutronOptServiceImpl implements INeutronOptService { @@ -32,23 +36,124 @@ } +// //电压数据分包计数 +// private volatile static int VOL_I = 0; +// private volatile static boolean VOL_FLAG = false; +// //PSD数据分包计数 +// private volatile static int PSD_I = 0; +// private volatile static boolean PSD_FLAG = false; + @Override public void analysis(String msg) { - - if(msg.indexOf(InstructCode.PSD1_HEAD)==0){ - //截取psd流数据 - msg=msg.substring(24); - if(msg.indexOf(InstructCode.PSD2_HEAD)==0&&msg.length()>=64){ - String gateOff= msg.substring(16,20); - String baseline= msg.substring(20,24); - String shortGate= msg.substring(24,28); - String longGate= msg.substring(28,32); - String qlong= msg.substring(48,56); - String qshort= msg.substring(56,64); - //toDo:算法计算 + String[] msgArr = msg.split(InstructCode.PSD1_HEAD); + if(msgArr.length>2){ + for(int i=1;i= 24) { +//// log.info("原始msg--->"+msg); +// int totalLength = Integer.parseInt(ByteUtil.reverseHex(msg.substring(msg.indexOf(InstructCode.PSD1_HEAD) + 16, +// msg.indexOf(InstructCode.PSD1_HEAD) + 24)), 16); +// if (msg.length() >= totalLength * 2) { +// PSD_FLAG = false; +// for (int i = 0; i < (msg.length() - (msg.indexOf(InstructCode.PSD1_HEAD) + 24)) / 64; i++) { +// String msgi = msg.substring((msg.indexOf(InstructCode.PSD1_HEAD) + 24) + i * 64, (msg.indexOf(InstructCode.PSD1_HEAD) + 24) + (i + 1) * 64); +// if (msgi.indexOf(InstructCode.PSD2_HEAD) == 0) { +// String gateOff = msgi.substring(16, 20); +// String baseline = msgi.substring(20, 24); +// String shortGate = msgi.substring(24, 28); +// String longGate = msgi.substring(28, 32); +// String qlong = msgi.substring(48, 56); +// String qshort = msgi.substring(56, 64); +// //toDo:算法计算 +// log.info("gateOff--->" + gateOff + +// "baseline--->" + baseline + +// "shortGate--->" + shortGate + +// "longGate--->" + longGate + +// "qlong--->" + qlong + +// "qshort--->" + qshort); +// } +// } +// } else { +// //2.缺psd数据存入缓存处理 +// PSD_FLAG = true; +// PSD_I++; +// CacheUtils.PSD_MAP.put(PSD_I, msg); +//// log.info("psd加入数据:" + PSD_I + "------->" + msg); +// } +// } +// } else if (PSD_FLAG) { +// PSD_I++; +// CacheUtils.PSD_MAP.put(PSD_I, msg); +// String str = CacheUtils.startPsdMapCache(); +//// log.info("分包psd加入数据:" + PSD_I + "------->" + msg); +// if (ObjectUtil.isNotEmpty(str) && str.length() >= 24) { +//// log.info("**************strpsd---->" + str); +// if (str.indexOf(InstructCode.PSD1_HEAD) != -1 && str.substring(str.indexOf(InstructCode.PSD1_HEAD) + 16).length() >= 8) { +// int totalLength = Integer.parseInt(ByteUtil.reverseHex(str.substring(str.indexOf(InstructCode.PSD1_HEAD) + 16, +// str.indexOf(InstructCode.PSD1_HEAD) + 24)), 16); +// if (str.length() >= totalLength * 2) { +// for (int i = 0; i < (str.length() - (str.indexOf(InstructCode.PSD1_HEAD) + 24)) / 64; i++) { +// String msgi = str.substring((str.indexOf(InstructCode.PSD1_HEAD) + 24) + i * 64, (str.indexOf(InstructCode.PSD1_HEAD) + 24) + (i + 1) * 64); +// if (msgi.indexOf(InstructCode.PSD2_HEAD) == 0) { +// String gateOff = msgi.substring(16, 20); +// String baseline = msgi.substring(20, 24); +// String shortGate = msgi.substring(24, 28); +// String longGate = msgi.substring(28, 32); +// String qlong = msgi.substring(48, 56); +// String qshort = msgi.substring(56, 64); +// //toDo:算法计算 +// log.info("分包:gateOff--->" + gateOff + +// "baseline--->" + baseline + +// "shortGate--->" + shortGate + +// "longGate--->" + longGate + +// "qlong--->" + qlong + +// "qshort--->" + qshort); +// } +// } +// } +// } +// } +// } +// //拆包处理 电压数据 +// if (msg.indexOf(InstructCode.DAC_HEAD + InstructCode.DAC_HEAD_LENGTH) != -1) { +// //完整包处理 +// if (msg.length() >= (msg.indexOf(InstructCode.DAC_HEAD + InstructCode.DAC_HEAD_LENGTH) + 24 + InstructCode.DAC_DATA_LENGTH)) { +// VOL_FLAG = false; +// msg = msg.substring(msg.indexOf(InstructCode.DAC_HEAD + InstructCode.DAC_HEAD_LENGTH) + 24); +// String[] volArr = InstructCode.getVolArr(msg); +// //本设备只用到第6通道 +// String vol6 = volArr[5]; +// log.info("第6通道电压值-------->" + vol6); +// } else { +// //分包处理,先放到缓存 +// VOL_FLAG = true; +// VOL_I++; +// CacheUtils.VOL_MAP.put(VOL_I, msg); +// } +// } else if (VOL_FLAG) { +// VOL_I++; +// CacheUtils.VOL_MAP.put(VOL_I, msg); +// String str = CacheUtils.startVolMapCache(); +// if (ObjectUtil.isNotEmpty(str) && +// str.length() >= (str.indexOf(InstructCode.DAC_HEAD + InstructCode.DAC_HEAD_LENGTH) + 24 + InstructCode.DAC_DATA_LENGTH)) { +// VOL_FLAG = false; +// str = str.substring(str.indexOf(InstructCode.DAC_HEAD + InstructCode.DAC_HEAD_LENGTH) + 24); +// String[] volArr = InstructCode.getVolArr(str); +// //本设备只用到第6通道 +// String vol6 = volArr[5]; +// log.info("分包第6通道电压值-------->" + vol6); +// } +// } + + } @Override @@ -59,6 +164,9 @@ @Override public void control(String command) { switch (command) { + case "init": + this.deviceInit(); + break; case "start": this.pressurize(); break; @@ -70,4 +178,8 @@ break; } } + + } + + diff --git a/casic-web/src/main/resources/logback-spring.xml b/casic-web/src/main/resources/logback-spring.xml index c0c7ac9..be4d799 100644 --- a/casic-web/src/main/resources/logback-spring.xml +++ b/casic-web/src/main/resources/logback-spring.xml @@ -53,7 +53,7 @@ - 10MB + 200MB @@ -89,7 +89,7 @@ - 10MB + 200MB