fix: 修改了 IotJt808DeviceMessageCodec.java:82-111 的 decode() 方法
All checks were successful
aiot-platform CI/CD / build-and-deploy (push) Successful in 7m33s
All checks were successful
aiot-platform CI/CD / build-and-deploy (push) Successful in 7m33s
This commit is contained in:
@@ -82,15 +82,27 @@ public class IotJt808DeviceMessageCodec implements IotDeviceMessageCodec {
|
||||
public IotDeviceMessage decode(byte[] bytes) {
|
||||
Assert.notNull(bytes, "待解码数据不能为空");
|
||||
Assert.isTrue(bytes.length >= 12, "数据包长度不足");
|
||||
|
||||
|
||||
try {
|
||||
// 1. 反转义(去除首尾标识符)
|
||||
// 1. 验证并去除首尾标识符 0x7e
|
||||
// JT808格式: 0x7e + 消息头 + 消息体 + 校验码 + 0x7e
|
||||
if (bytes[0] != (byte) 0x7e || bytes[bytes.length - 1] != (byte) 0x7e) {
|
||||
throw new IllegalArgumentException("JT808消息格式错误:缺少首尾标识符0x7e");
|
||||
}
|
||||
|
||||
// 2. 反转义处理(处理0x7d01->0x7d, 0x7d02->0x7e)
|
||||
// doEscape4Receive会保留start之前和end之后的字节,所以传入1和length-1来处理中间部分
|
||||
byte[] unescapedBytes = protocolUtil.doEscape4Receive(bytes, 1, bytes.length - 1);
|
||||
|
||||
// 2. 解析为 JT808 数据包
|
||||
Jt808DataPack dataPack = decoder.bytes2PackageData(unescapedBytes);
|
||||
|
||||
// 3. 转换为统一的 IotDeviceMessage
|
||||
|
||||
// 3. 从反转义后的数据中提取实际内容(去掉首尾的0x7e)
|
||||
// doEscape4Receive保留了首尾的0x7e,需要再次去除
|
||||
byte[] actualData = new byte[unescapedBytes.length - 2];
|
||||
System.arraycopy(unescapedBytes, 1, actualData, 0, actualData.length);
|
||||
|
||||
// 4. 解析为 JT808 数据包(此时数据已经是纯净的消息内容,不含0x7e)
|
||||
Jt808DataPack dataPack = decoder.bytes2PackageData(actualData);
|
||||
|
||||
// 5. 转换为统一的 IotDeviceMessage
|
||||
return convertToIotDeviceMessage(dataPack);
|
||||
} catch (Exception e) {
|
||||
log.error("[decode][JT808 消息解码失败,数据长度: {}]", bytes.length, e);
|
||||
|
||||
Reference in New Issue
Block a user