package dell55AAData;
|
|
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) {
|
// ²ÎÊý¼ì²é
|
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();
|
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;
|
|
return result;
|
}
|
}
|