| | |
| | | package dell55AAData; |
| | | |
| | | import dell_system.MessageViewPanel; |
| | | |
| | | public class Dell55AA01Parser { |
| | | // 协议常量 |
| | | // 常量定义 |
| | | private static final String EXPECTED_HEADER = "55AA01"; // 协议头 |
| | | private static final int MIN_LENGTH = 42; // 最小长度(21字节*2字符) |
| | | private static final ThreadLocal<ParseResult> RESULT_CACHE = |
| | |
| | | // 解析结果类 |
| | | public static class ParseResult { |
| | | public int sequenceNum; // 序列号 |
| | | public String tagId; // 标签ID(4字节十六进制) |
| | | public String anchorId; // 锚点ID(4字节十六进制) |
| | | public String tagId; // 标签ID(4字节小端序) |
| | | public String anchorId; // 锚点ID(4字节小端序) |
| | | public int distance; // 距离(毫米) |
| | | public int power; // 电量(0-100) |
| | | public boolean buttonPressed; // 按钮状态 |
| | |
| | | /** |
| | | * 解析55AA01协议数据 |
| | | * @param message 十六进制字符串 |
| | | * @return 解析结果(失败返回null) |
| | | * @return 解析结果(错误时返回null) |
| | | */ |
| | | public static ParseResult parse(String message) { |
| | | // 参数检查 |
| | | public static ParseResult parse(String message, String ip, int port) { |
| | | if (message == null || message.isEmpty()) { |
| | | return null; |
| | | } |
| | | // 清洗数据:移除所有非十六进制字符 |
| | | message = message.replaceAll("[^0-9A-Fa-f]", "").toUpperCase(); |
| | | |
| | | // 数据校验 |
| | | if (message == null || message.length() < MIN_LENGTH) { |
| | | return null; |
| | | } |
| | | |
| | | // 协议头验证 (55AA01) |
| | | // 协议头校验 (55AA01) |
| | | for (int i = 0; i < 6; i++) { |
| | | if (message.charAt(i) != EXPECTED_HEADER.charAt(i)) { |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | // 获取线程本地资源 |
| | | ParseResult result = RESULT_CACHE.get(); |
| | | result.reset(); |
| | | char[] chars = HexUtils.getThreadLocalBuffer(); |
| | | message.getChars(0, Math.min(message.length(), chars.length), chars, 0); |
| | | |
| | | // 解析序列号 (位置8-9) |
| | | result.sequenceNum = HexUtils.fastHexToByte(chars[8], chars[9]); |
| | | |
| | | // 解析标签ID (位置10-13, 小端序) |
| | | result.tagId = new String(new char[] { |
| | | chars[12], chars[13], // 高位 |
| | | chars[10], chars[11] // 低位 |
| | | }); |
| | | |
| | | // 解析锚点ID (位置14-17, 小端序) |
| | | result.anchorId = new String(new char[] { |
| | | chars[16], chars[17], // 高位 |
| | | chars[14], chars[15] // 低位 |
| | | }); |
| | | |
| | | // 解析距离 (位置18-25, 4字节小端序整数) |
| | | int b0 = HexUtils.fastHexToByte(chars[18], chars[19]); // 最低位 |
| | | int b1 = HexUtils.fastHexToByte(chars[20], chars[21]); |
| | | int b2 = HexUtils.fastHexToByte(chars[22], chars[23]); |
| | | int b3 = HexUtils.fastHexToByte(chars[24], chars[25]); // 最高位 |
| | | int raw = (b3 << 24) | (b2 << 16) | (b1 << 8) | b0; |
| | | result.distance = raw; // 保持原始整数值 |
| | | |
| | | // 解析电量 (位置26-27) |
| | | result.power = HexUtils.fastHexToByte(chars[26], chars[27]); |
| | | |
| | | // 解析按钮状态 (位置28-29) |
| | | result.buttonPressed = HexUtils.fastHexToByte(chars[28], chars[29]) == 1; |
| | | try { |
| | | if (message.length() < 30) { // 确保有足够长度访问charAt(28) |
| | | return null; |
| | | } |
| | | // 解析序列号 (位置8-9) |
| | | result.sequenceNum = HexUtils.fastHexToByte( |
| | | message.charAt(8), message.charAt(9) |
| | | ); |
| | | |
| | | // 解析标签ID (位置10-13, 小端序) |
| | | result.tagId = new String(new char[] { |
| | | message.charAt(12), message.charAt(13), // 高位 |
| | | message.charAt(10), message.charAt(11) // 低位 |
| | | }); |
| | | |
| | | // 解析锚点ID (位置14-17, 小端序) |
| | | result.anchorId = new String(new char[] { |
| | | message.charAt(16), message.charAt(17), // 高位 |
| | | message.charAt(14), message.charAt(15) // 低位 |
| | | }); |
| | | |
| | | // 解析距离 (位置18-25, 4字节小端整数) |
| | | int b0 = HexUtils.fastHexToByte(message.charAt(18), message.charAt(19)); // 最低位 |
| | | int b1 = HexUtils.fastHexToByte(message.charAt(20), message.charAt(21)); |
| | | int b2 = HexUtils.fastHexToByte(message.charAt(22), message.charAt(23)); |
| | | int b3 = HexUtils.fastHexToByte(message.charAt(24), message.charAt(25)); // 最高位 |
| | | int raw = (b3 << 24) | (b2 << 16) | (b1 << 8) | b0; |
| | | result.distance = raw; // 保持原始整数值 |
| | | |
| | | // 解析电量 (位置26-27) |
| | | result.power = HexUtils.fastHexToByte(message.charAt(26), message.charAt(27)); |
| | | |
| | | // 解析按钮状态 (位置28-29) |
| | | result.buttonPressed = HexUtils.fastHexToByte(message.charAt(28), message.charAt(29)) == 1; |
| | | |
| | | String hexData = "55AA01标签地址:" + result.tagId + "基站地址:" + result.anchorId + |
| | | "距离:" + result.distance + "电量:" + result.power + |
| | | "按钮状态:" + result.buttonPressed; |
| | | MessageViewPanel.showData(hexData, ip, port, 0, "UDPA", "55AA01", "ALL"); |
| | | } catch (IndexOutOfBoundsException | NumberFormatException e) { |
| | | System.err.println("Parsing error in packet from " + ip + ":" + port); |
| | | return null; |
| | | } |
| | | |
| | | return result; |
| | | } |