826220679@qq.com
4 天以前 487a5b943571d6ab57c4baddca1cbcc7b2062e73
src/home/Dell55AA1FParser.java
@@ -4,6 +4,7 @@
public class Dell55AA1FParser {
    // 常量定义
    private static final String EXPECTED_HEADER = "55AA1F"; // 协议头
    private static final int MIN_PACKET_LENGTH = 36; // 最小包长度(字符数): 固定字段18字节=36字符
    private static final int FIXED_FIELDS_LENGTH = 11; // 从标签ID到保留字段的固定长度(字节)
    private static final ThreadLocal<ParseResult> RESULT_CACHE = 
            ThreadLocal.withInitial(ParseResult::new);
@@ -44,6 +45,7 @@
     */
    public static ParseResult parse(String message, String ip, int port) {
        if (message == null || message.isEmpty()) {
            System.err.println("Empty message from " + ip + ":" + port);
            return null;
        }
@@ -51,12 +53,22 @@
        char[] cleanedMessage = cleanMessage(message);
        if (cleanedMessage == null) {
            System.err.println("Invalid characters in message from " + ip + ":" + port);
            return null;
        }
        // 检查最小长度
        if (cleanedMessage.length < MIN_PACKET_LENGTH) {
            System.err.println("Message too short from " + ip + ":" + port +
                             ". Expected at least " + MIN_PACKET_LENGTH +
                             " characters, got " + cleanedMessage.length);
            return null;
        }
        // 协议头校验 (55AA1F)
        if (cleanedMessage.length < 6 || 
            !new String(cleanedMessage, 0, 6).equals(EXPECTED_HEADER)) {
            System.err.println("Invalid header in message from " + ip + ":" + port);
            return null;
        }
@@ -68,13 +80,12 @@
            result.dataLength = HexUtils.fastHexToByte(cleanedMessage[6], cleanedMessage[7]);
            
            // 计算期望的总字符长度
            int expectedCharLength = 8 + // 包头(4字符) + 消息类型(2字符) + 数据长度(2字符)
                                    result.dataLength * 2 + // 数据部分
                                    4; // 校验和(4字符)
            int expectedCharLength = 14 + result.dataLength * 2; // 14=包头6+消息类型2+数据长度2+校验和4
//            if (cleanedMessage.length != expectedCharLength) {
//                System.err.println("Data length mismatch: expected " + expectedCharLength +
//                                 ", got " + cleanedMessage.length);
//            if (cleanedMessage.length < expectedCharLength) {
//                System.err.println("Incomplete message from " + ip + ":" + port +
//                                 ". Expected " + expectedCharLength +
//                                 " characters, got " + cleanedMessage.length);
//                return null;
//            }
@@ -83,6 +94,12 @@
            // 解析标签ID (位置8-11),直接取字符串
            result.tagId = new String(cleanedMessage, 8, 4);
            // 检查是否有足够长度解析固定字段
            if (cleanedMessage.length < 30) { // 需要至少15字节=30字符
                System.err.println("Message too short for fixed fields from " + ip + ":" + port);
                return null;
            }
            // 解析距离 (位置12-15, 2字节小端整数)
            int distLow = HexUtils.fastHexToByte(cleanedMessage[12], cleanedMessage[13]);
@@ -111,7 +128,12 @@
//            int userDataStart = 30;
//            int userDataCharLength = (result.dataLength - FIXED_FIELDS_LENGTH) * 2;
//            if (userDataCharLength > 0) {
//                result.userData = new String(cleanedMessage, userDataStart, userDataCharLength);
//                if (userDataStart + userDataCharLength <= cleanedMessage.length) {
//                    result.userData = new String(cleanedMessage, userDataStart, userDataCharLength);
//                } else {
//                    System.err.println("User data truncated in message from " + ip + ":" + port);
//                    result.userData = "";
//                }
//            } else {
//                result.userData = "";
//            }