fix: 修复TCP上报空Json数据解析失败bug
All checks were successful
aiot-platform CI/CD / build-and-deploy (push) Successful in 6m23s

This commit is contained in:
lzh
2025-12-27 23:05:19 +08:00
parent 69b0767fd7
commit bb8f76d531
2 changed files with 53 additions and 12 deletions

View File

@@ -58,7 +58,7 @@ public class IotJt808DeviceMessageCodec implements IotDeviceMessageCodec {
// 根据方法名路由到不同的编码器
return switch (method) {
// === 标准物模型方法 ===
// === 标准物模型方法(下行) ===
case "thing.service.invoke" -> encodeServiceInvoke(message, phone, flowId); // 服务调用
case "thing.property.set" -> encodePropertySet(message, phone, flowId); // 属性设置
@@ -67,8 +67,14 @@ public class IotJt808DeviceMessageCodec implements IotDeviceMessageCodec {
case "jt808.platform.registerResp", "registerResp" -> encodeRegisterResp(message, phone, flowId);
case "jt808.platform.textDown", "textDown" -> encodeTextDown(message, phone, flowId);
// === 上行消息方法(不应用于下行) ===
case "thing.property.post", "thing.property.report" -> {
log.warn("[encode][JT808 协议不支持下行属性上报方法: {},请使用 thing.property.set 进行属性设置]", method);
yield new byte[0];
}
default -> {
log.warn("[encode][不支持的消息方法: {}]", method);
log.warn("[encode][不支持的消息方法: {}JT808 协议仅支持下行方法: thing.service.invoke, thing.property.set]", method);
yield new byte[0];
}
};

View File

@@ -95,16 +95,51 @@ public class IotTcpJsonDeviceMessageCodec implements IotDeviceMessageCodec {
@SuppressWarnings("DataFlowIssue")
public IotDeviceMessage decode(byte[] bytes) {
String jsonStr = StrUtil.utf8Str(bytes).trim();
TcpJsonMessage tcpJsonMessage = JsonUtils.parseObject(jsonStr, TcpJsonMessage.class);
Assert.notNull(tcpJsonMessage, "消息不能为空");
Assert.notBlank(tcpJsonMessage.getMethod(), "消息方法不能为空");
return IotDeviceMessage.of(
tcpJsonMessage.getId(),
tcpJsonMessage.getMethod(),
tcpJsonMessage.getParams(),
tcpJsonMessage.getData(),
tcpJsonMessage.getCode(),
tcpJsonMessage.getMsg());
// 快速验证是否为 JSON 格式(必须以 { 或 [ 开头)
if (StrUtil.isBlank(jsonStr)) {
throw new IllegalArgumentException("JSON 数据为空");
}
String trimmedJson = jsonStr.trim();
if (!trimmedJson.startsWith("{") && !trimmedJson.startsWith("[")) {
throw new IllegalArgumentException(
String.format("不是有效的 JSON 格式,数据开头: %s",
jsonStr.length() > 20 ? jsonStr.substring(0, 20) : jsonStr));
}
try {
TcpJsonMessage tcpJsonMessage = JsonUtils.parseObject(jsonStr, TcpJsonMessage.class);
// 检查解析结果
if (tcpJsonMessage == null) {
throw new IllegalArgumentException(
String.format("JSON 解析返回 null可能是空对象或格式错误数据: %s",
jsonStr.length() > 100 ? jsonStr.substring(0, 100) + "..." : jsonStr));
}
// 检查必要字段
if (StrUtil.isBlank(tcpJsonMessage.getMethod())) {
throw new IllegalArgumentException(
String.format("JSON 消息缺少 method 字段,数据: %s",
jsonStr.length() > 100 ? jsonStr.substring(0, 100) + "..." : jsonStr));
}
return IotDeviceMessage.of(
tcpJsonMessage.getId(),
tcpJsonMessage.getMethod(),
tcpJsonMessage.getParams(),
tcpJsonMessage.getData(),
tcpJsonMessage.getCode(),
tcpJsonMessage.getMsg());
} catch (IllegalArgumentException e) {
// 重新抛出 IllegalArgumentException已经是我们自己的错误信息
throw e;
} catch (Exception e) {
throw new IllegalArgumentException(
String.format("JSON 解析失败,数据开头: %s错误: %s",
jsonStr.length() > 50 ? jsonStr.substring(0, 50) : jsonStr, e.getMessage()), e);
}
}
}