| | |
| | | package dell55AAData; |
| | | |
| | | import dell_system.MessageViewPanel; |
| | | import dell_targets.Dell_BaseStation; |
| | | import publicsWay.EfficientTimeFormatter; |
| | | |
| | | public class Dell55AA02Parser { |
| | | private static final String EXPECTED_HEADER = "55AA02"; |
| | | private static final int MIN_LENGTH = 36; // 18字节 * 2字符/字节 |
| | | private static final int BASE_ID_START = 8; // 基站ID起始位置(第4字节) |
| | | private static final int SYNC_STATUS_START = 12; // 同步状态起始位置(第5字节) |
| | | private static final int PRESSURE_START = 14; // 气压值起始位置(第6字节) |
| | | private static final int SYNC_STATUS_START = 12; // 同步状态起始位置(第5字节) |
| | | private static final int PRESSURE_START = 14; // 气压值起始位置(第6字节) |
| | | private static final int MIN_MESSAGE_LENGTH = 22; // 最小有效数据长度 |
| | | |
| | | // 重用StringBuilder减少内存分配 |
| | | private static final ThreadLocal<StringBuilder> hexDataBuilder = |
| | | ThreadLocal.withInitial(() -> new StringBuilder(64)); |
| | | |
| | | /** |
| | | * 解析55AA02格式的基站数据 |
| | | * 高效解析55AA02格式的基站数据 |
| | | * @param message 接收到的数据(十六进制字符串) |
| | | */ |
| | | public static void parseAndUpdate(String message) { |
| | | public static void parse(String message, String ip, int port) { |
| | | // 1. 基础校验 |
| | | if (message == null || message.length() < MIN_LENGTH) { |
| | | if (message == null || message.length() < MIN_MESSAGE_LENGTH) { |
| | | return; |
| | | } |
| | | |
| | | // 2. 头部校验(固定开头字符串) |
| | | for (int i = 0; i < EXPECTED_HEADER.length(); i++) { |
| | | if (message.charAt(i) != EXPECTED_HEADER.charAt(i)) { |
| | | return; |
| | | } |
| | | } |
| | | // 2. 解析标签ID (位置8-11, 小端序) |
| | | char c8 = message.charAt(8); |
| | | char c9 = message.charAt(9); |
| | | char c10 = message.charAt(10); |
| | | char c11 = message.charAt(11); |
| | | |
| | | // 3. 解析基站ID(原始数据字符串) |
| | | String baseId = new String(new char[]{ |
| | | message.charAt(BASE_ID_START), |
| | | message.charAt(BASE_ID_START + 1), |
| | | message.charAt(BASE_ID_START + 2), |
| | | message.charAt(BASE_ID_START + 3) |
| | | }); |
| | | // 直接构建baseId字符串 |
| | | String baseId = new String(new char[]{c10, c11, c8, c9}); |
| | | |
| | | // 4. 解析同步状态(1字节) |
| | | char syncChar = message.charAt(SYNC_STATUS_START + 1); // 取状态字符 |
| | | String syncStatus = (syncChar == '0') ? "0" : "1"; |
| | | // 3. 解析同步状态(1字节) |
| | | char syncStatusChar = message.charAt(SYNC_STATUS_START + 1); |
| | | String syncStatus = (syncStatusChar == '0') ? "0" : "1"; |
| | | |
| | | // 5. 解析气压值(4字节小端序) |
| | | // 4. 高效解析气压值(4字节小端序) |
| | | int pressure = 0; |
| | | for (int i = 0; i < 8; i += 2) { |
| | | int idx = PRESSURE_START + i; |
| | |
| | | int byteValue = HexUtils.fastHexToByte(message.charAt(idx), message.charAt(idx + 1)); |
| | | pressure |= (byteValue << (i * 4)); // 小端合并 |
| | | } |
| | | String pressureStr = String.valueOf(pressure); |
| | | |
| | | // 6. 更新基站数据 |
| | | updateBaseStationData(baseId, syncStatus ,pressureStr); |
| | | } |
| | | |
| | | /** |
| | | * 更新基站数据 |
| | | * @param baseId 基站ID |
| | | * @param syncStatus 同步状态 |
| | | * @param pressure 气压值字符串 |
| | | */ |
| | | private static void updateBaseStationData(String baseId, String syncStatus, String pressure) { |
| | | // 5. 更新基站数据 |
| | | if (MessageViewPanel.isWindowVisible) { |
| | | StringBuilder sb = hexDataBuilder.get(); |
| | | sb.setLength(0); |
| | | sb.append("55AA02 AnchorHeart,Anchorid:") |
| | | .append(baseId) |
| | | .append(",SyncStatus:") |
| | | .append(syncStatus) |
| | | .append(",Pressure:") |
| | | .append(pressure); |
| | | MessageViewPanel.showData(sb.toString(), ip, port, 0, "UDPA", "55AA02", baseId); |
| | | } |
| | | |
| | | // 延迟创建时间字符串直到必要时刻 |
| | | String time = EfficientTimeFormatter.getCurrentTimeFormatted(); |
| | | |
| | | // 使用预分配字符串常量减少内存分配 |
| | | Dell_BaseStation.updateBaseStationProperty(baseId, "ipAddress", ip); |
| | | Dell_BaseStation.updateBaseStationProperty(baseId, "port", Integer.toString(port)); |
| | | Dell_BaseStation.updateBaseStationProperty(baseId, "status", "1"); |
| | | Dell_BaseStation.updateBaseStationProperty(baseId, "onlineTime", time); |
| | | Dell_BaseStation.updateBaseStationProperty(baseId, "syncStatus", syncStatus); |
| | | Dell_BaseStation.updateBaseStationProperty(baseId, "barometerReading", pressure); |
| | | Dell_BaseStation.updateBaseStationProperty(baseId, "barometerReading", Integer.toString(pressure)); |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | } |