826220679@qq.com
9 天以前 fb883547ede83b1c758b1a9a025898ba3f83497a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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 = 
        ThreadLocal.withInitial(ParseResult::new);
 
    // ½âÎö½á¹ûÀà
    public static class ParseResult {
        public int sequenceNum;   // ÐòÁкÅ
        public String tagId;      // ±êÇ©ID(4×Ö½ÚС¶ËÐò)
        public String anchorId;   // ÃªµãID(4×Ö½ÚС¶ËÐò)
        public int distance;      // ¾àÀë(ºÁÃ×)
        public int power;         // µçÁ¿(0-100)
        public boolean buttonPressed; // °´Å¥×´Ì¬
        
        public void reset() {
            sequenceNum = 0;
            tagId = "";
            anchorId = "";
            distance = 0;
            power = 0;
            buttonPressed = false;
        }
    }
 
    /**
     * ½âÎö55AA01ЭÒéÊý¾Ý
     * @param message Ê®Áù½øÖÆ×Ö·û´®
     * @return ½âÎö½á¹û(´íÎóʱ·µ»Ønull)
     */
    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)
        for (int i = 0; i < 6; i++) {
            if (message.charAt(i) != EXPECTED_HEADER.charAt(i)) {
                return null;
            }
        }
        
        ParseResult result = RESULT_CACHE.get();
        result.reset();
        
        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;
    }
}