review:【iot 物联网】tcp 协议

This commit is contained in:
YunaiV
2025-08-20 12:31:45 +08:00
parent 9e38f56f88
commit d53bc66a90
2 changed files with 5 additions and 6 deletions

View File

@@ -1,6 +1,7 @@
package cn.iocoder.yudao.module.iot.gateway.codec.tcp;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
import cn.iocoder.yudao.module.iot.core.mq.message.IotDeviceMessage;
import cn.iocoder.yudao.module.iot.gateway.codec.IotDeviceMessageCodec;
@@ -9,8 +10,6 @@ import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.stereotype.Component;
import java.nio.charset.StandardCharsets;
/**
* TCP JSON 格式 {@link IotDeviceMessage} 编解码器
*
@@ -95,7 +94,7 @@ public class IotTcpJsonDeviceMessageCodec implements IotDeviceMessageCodec {
@Override
@SuppressWarnings("DataFlowIssue")
public IotDeviceMessage decode(byte[] bytes) {
String jsonStr = new String(bytes, StandardCharsets.UTF_8).trim();
String jsonStr = StrUtil.utf8Str(bytes).trim();
TcpJsonMessage tcpJsonMessage = JsonUtils.parseObject(jsonStr, TcpJsonMessage.class);
Assert.notNull(tcpJsonMessage, "消息不能为空");
Assert.notBlank(tcpJsonMessage.getMethod(), "消息方法不能为空");

View File

@@ -364,6 +364,7 @@ public class IotTcpUpstreamHandler implements Handler<NetSocket> {
* @param message 消息
* @param codecType 消息编解码类型
*/
@SuppressWarnings("SameParameterValue")
private void sendSuccessResponse(NetSocket socket, String requestId, String message, String codecType) {
sendResponse(socket, true, message, requestId, codecType);
}
@@ -374,6 +375,7 @@ public class IotTcpUpstreamHandler implements Handler<NetSocket> {
* @param params 参数对象(通常为 Map 类型)
* @return 认证参数 DTO解析失败时返回 null
*/
@SuppressWarnings("unchecked")
private IotDeviceAuthReqDTO parseAuthParams(Object params) {
if (params == null) {
return null;
@@ -382,7 +384,6 @@ public class IotTcpUpstreamHandler implements Handler<NetSocket> {
try {
// 参数默认为 Map 类型,直接转换
if (params instanceof java.util.Map) {
@SuppressWarnings("unchecked")
java.util.Map<String, Object> paramMap = (java.util.Map<String, Object>) params;
return new IotDeviceAuthReqDTO()
.setClientId(MapUtil.getStr(paramMap, "clientId"))
@@ -398,9 +399,8 @@ public class IotTcpUpstreamHandler implements Handler<NetSocket> {
// 其他情况尝试 JSON 转换
String jsonStr = JsonUtils.toJsonString(params);
return JsonUtils.parseObject(jsonStr, IotDeviceAuthReqDTO.class);
} catch (Exception e) {
log.error("[parseAuthParams][解析认证参数失败]", e);
log.error("[parseAuthParams][解析认证参数({})失败]", params, e);
return null;
}
}