package dell55AAData; import java.util.Arrays; import dell_system.MessageViewPanel; import dell_targets.Dell_BaseStation; import dell_targets.Dell_tag; import publicsWay.EfficientTimeFormatter; public class Dell55AA01Parser { // ³£Á¿¶¨Òå private static final String EXPECTED_HEADER = "55AA01"; // ЭÒéÍ· private static final int MIN_LENGTH = 42; // ×îС³¤¶È(21×Ö½Ú*2×Ö·û) private static final ThreadLocal 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 int buttonPressed; // °´Å¥×´Ì¬ public void reset() { sequenceNum = 0; tagId = ""; anchorId = ""; distance = 0; power = 0; buttonPressed = 0; } } /** * ½âÎö55AA01ЭÒéÊý¾Ý * @param message Ê®Áù½øÖÆ×Ö·û´® * @return ½âÎö½á¹û(´íÎóʱ·µ»Ønull) */ public static ParseResult parse(String message, String ip, int port) { if (message == null || message.isEmpty()) { return null; } // ÇåÏ´Êý¾Ý£ºÒƳýËùÓзÇÊ®Áù½øÖÆ×Ö·û char[] cleanedMessage = cleanMessage(message); // Êý¾ÝУÑé if (cleanedMessage == null || cleanedMessage.length < MIN_LENGTH) { return null; } // ЭÒéͷУÑé (55AA01) if (!new String(cleanedMessage, 0, 6).equals(EXPECTED_HEADER)) { return null; } ParseResult result = RESULT_CACHE.get(); result.reset(); try { if (cleanedMessage.length < 30) { // È·±£ÓÐ×ã¹»³¤¶È·ÃÎÊcharAt(28) return null; } // ½âÎöÐòÁкŠ(λÖÃ8-9) result.sequenceNum = HexUtils.fastHexToByte(cleanedMessage[8], cleanedMessage[9]); // ½âÎö±êÇ©ID (λÖÃ10-13, С¶ËÐò) result.tagId = new String(new char[]{ cleanedMessage[12], cleanedMessage[13], // ¸ßλ cleanedMessage[10], cleanedMessage[11] // µÍλ }); // ½âÎöêµãID (λÖÃ14-17, С¶ËÐò) result.anchorId = new String(new char[]{ cleanedMessage[16], cleanedMessage[17], // ¸ßλ cleanedMessage[14], cleanedMessage[15] // µÍλ }); // ½âÎö¾àÀë (λÖÃ18-25, 4×Ö½ÚС¶ËÕûÊý) int b0 = HexUtils.fastHexToByte(cleanedMessage[18], cleanedMessage[19]); // ×îµÍλ int b1 = HexUtils.fastHexToByte(cleanedMessage[20], cleanedMessage[21]); int b2 = HexUtils.fastHexToByte(cleanedMessage[22], cleanedMessage[23]); int b3 = HexUtils.fastHexToByte(cleanedMessage[24], cleanedMessage[25]); // ×î¸ßλ int raw = (b3 << 24) | (b2 << 16) | (b1 << 8) | b0; result.distance = raw; // ±£³ÖԭʼÕûÊýÖµ // ½âÎöµçÁ¿ (λÖÃ26-27) result.power = HexUtils.fastHexToByte(cleanedMessage[26], cleanedMessage[27]); // ½âÎö°´Å¥×´Ì¬ (λÖÃ28-29) result.buttonPressed = HexUtils.fastHexToByte(cleanedMessage[28], cleanedMessage[29]); // ÈÕÖ¾ºÍ¸üвÙ×÷¿ÉÒÔ¿¼ÂÇÓÅ»¯»ò¼õÉÙµ÷ÓÃÆµÂÊ String hexData = "55AA01 °üÐò:" + result.sequenceNum + ",±êÇ©±àºÅ:" + result.tagId + ",»ùÕ¾±àºÅ:" + result.anchorId + ",¾àÀë:" + result.distance + ",µçÁ¿:" + result.power + ",°´Å¥×´Ì¬:" + result.buttonPressed; MessageViewPanel.showData(hexData, ip, port, 0, "UDPA", "55AA01", "ALL"); String time = EfficientTimeFormatter.getCurrentTimeFormatted(); Dell_BaseStation.updateBaseStationProperty(result.anchorId, "ipAddress", ip); Dell_BaseStation.updateBaseStationProperty(result.anchorId, "port", port + ""); Dell_BaseStation.updateBaseStationProperty(result.anchorId, "status", "1"); Dell_BaseStation.updateBaseStationProperty(result.anchorId, "onlineTime", time); Dell_tag.updateLocationTagProperty(result.tagId, "sosStatus", result.buttonPressed + ""); Dell_tag.updateLocationTagProperty(result.tagId, "onlineStatus", "1"); Dell_tag.updateLocationTagProperty(result.tagId, "lastUwbSignalTime", time); Dell_tag.updateLocationTagProperty(result.tagId, "latestRangingSeq", result.sequenceNum + ""); Dell_tag.updateLocationTagProperty(result.tagId, "latestRangingBaseId", result.anchorId); Dell_tag.updateLocationTagProperty(result.tagId, "latestRangingDistance", result.distance + ""); Dell_tag.updateLocationTagProperty(result.tagId, "latestRangingBaseCount", "1"); } catch (IndexOutOfBoundsException | NumberFormatException e) { System.err.println("Parsing error in packet from " + ip + ":" + port); return null; } return result; } private static char[] cleanMessage(String message) { char[] cleaned = new char[message.length()]; int j = 0; for (char c : message.toCharArray()) { if (Character.isDigit(c) || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f')) { cleaned[j++] = Character.toUpperCase(c); } } if (j == 0) return null; return Arrays.copyOf(cleaned, j); } public static void updateBase(String baseStationId, String propertyName, String value) { Dell_BaseStation.updateBaseStationProperty(baseStationId, propertyName, value); Dell_BaseStation.updateBaseStationProperty(baseStationId, propertyName, value); } }