package jiexi;
|
|
|
|
public class Dell55AA02Parser {
|
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格式的基站数据
|
* @param message 接收到的数据(十六进制字符串)
|
*/
|
public static void parse(String message, String ip, int port) {
|
// 1. 基础校验
|
if (message == null || message.length() < MIN_MESSAGE_LENGTH) {
|
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);
|
|
// 直接构建baseId字符串
|
String baseId = new String(new char[]{c10, c11, c8, c9});
|
|
// 3. 解析同步状态(1字节)
|
char syncStatusChar = message.charAt(SYNC_STATUS_START + 1);
|
String syncStatus = (syncStatusChar == '0') ? "0" : "1";
|
|
// 4. 高效解析气压值(4字节小端序)
|
int pressure = 0;
|
for (int i = 0; i < 8; i += 2) {
|
int idx = PRESSURE_START + i;
|
// 使用HexUtils的快速转换方法
|
int byteValue = HexUtils.fastHexToByte(message.charAt(idx), message.charAt(idx + 1));
|
pressure |= (byteValue << (i * 4)); // 小端合并
|
}
|
|
|
/*// 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", Integer.toString(pressure));*/
|
}
|
|
|
|
|
}
|