Commit d53bc66a908098c25ceaccfb10c18638902670ec

Authored by YunaiV
1 parent 9e38f56f

review:【iot 物联网】tcp 协议

yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/codec/tcp/IotTcpJsonDeviceMessageCodec.java
1 package cn.iocoder.yudao.module.iot.gateway.codec.tcp; 1 package cn.iocoder.yudao.module.iot.gateway.codec.tcp;
2 2
3 import cn.hutool.core.lang.Assert; 3 import cn.hutool.core.lang.Assert;
  4 +import cn.hutool.core.util.StrUtil;
4 import cn.iocoder.yudao.framework.common.util.json.JsonUtils; 5 import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
5 import cn.iocoder.yudao.module.iot.core.mq.message.IotDeviceMessage; 6 import cn.iocoder.yudao.module.iot.core.mq.message.IotDeviceMessage;
6 import cn.iocoder.yudao.module.iot.gateway.codec.IotDeviceMessageCodec; 7 import cn.iocoder.yudao.module.iot.gateway.codec.IotDeviceMessageCodec;
@@ -9,8 +10,6 @@ import lombok.Data; @@ -9,8 +10,6 @@ import lombok.Data;
9 import lombok.NoArgsConstructor; 10 import lombok.NoArgsConstructor;
10 import org.springframework.stereotype.Component; 11 import org.springframework.stereotype.Component;
11 12
12 -import java.nio.charset.StandardCharsets;  
13 -  
14 /** 13 /**
15 * TCP JSON 格式 {@link IotDeviceMessage} 编解码器 14 * TCP JSON 格式 {@link IotDeviceMessage} 编解码器
16 * 15 *
@@ -95,7 +94,7 @@ public class IotTcpJsonDeviceMessageCodec implements IotDeviceMessageCodec { @@ -95,7 +94,7 @@ public class IotTcpJsonDeviceMessageCodec implements IotDeviceMessageCodec {
95 @Override 94 @Override
96 @SuppressWarnings("DataFlowIssue") 95 @SuppressWarnings("DataFlowIssue")
97 public IotDeviceMessage decode(byte[] bytes) { 96 public IotDeviceMessage decode(byte[] bytes) {
98 - String jsonStr = new String(bytes, StandardCharsets.UTF_8).trim(); 97 + String jsonStr = StrUtil.utf8Str(bytes).trim();
99 TcpJsonMessage tcpJsonMessage = JsonUtils.parseObject(jsonStr, TcpJsonMessage.class); 98 TcpJsonMessage tcpJsonMessage = JsonUtils.parseObject(jsonStr, TcpJsonMessage.class);
100 Assert.notNull(tcpJsonMessage, "消息不能为空"); 99 Assert.notNull(tcpJsonMessage, "消息不能为空");
101 Assert.notBlank(tcpJsonMessage.getMethod(), "消息方法不能为空"); 100 Assert.notBlank(tcpJsonMessage.getMethod(), "消息方法不能为空");
yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/protocol/tcp/router/IotTcpUpstreamHandler.java
@@ -364,6 +364,7 @@ public class IotTcpUpstreamHandler implements Handler<NetSocket> { @@ -364,6 +364,7 @@ public class IotTcpUpstreamHandler implements Handler<NetSocket> {
364 * @param message 消息 364 * @param message 消息
365 * @param codecType 消息编解码类型 365 * @param codecType 消息编解码类型
366 */ 366 */
  367 + @SuppressWarnings("SameParameterValue")
367 private void sendSuccessResponse(NetSocket socket, String requestId, String message, String codecType) { 368 private void sendSuccessResponse(NetSocket socket, String requestId, String message, String codecType) {
368 sendResponse(socket, true, message, requestId, codecType); 369 sendResponse(socket, true, message, requestId, codecType);
369 } 370 }
@@ -374,6 +375,7 @@ public class IotTcpUpstreamHandler implements Handler<NetSocket> { @@ -374,6 +375,7 @@ public class IotTcpUpstreamHandler implements Handler<NetSocket> {
374 * @param params 参数对象(通常为 Map 类型) 375 * @param params 参数对象(通常为 Map 类型)
375 * @return 认证参数 DTO,解析失败时返回 null 376 * @return 认证参数 DTO,解析失败时返回 null
376 */ 377 */
  378 + @SuppressWarnings("unchecked")
377 private IotDeviceAuthReqDTO parseAuthParams(Object params) { 379 private IotDeviceAuthReqDTO parseAuthParams(Object params) {
378 if (params == null) { 380 if (params == null) {
379 return null; 381 return null;
@@ -382,7 +384,6 @@ public class IotTcpUpstreamHandler implements Handler<NetSocket> { @@ -382,7 +384,6 @@ public class IotTcpUpstreamHandler implements Handler<NetSocket> {
382 try { 384 try {
383 // 参数默认为 Map 类型,直接转换 385 // 参数默认为 Map 类型,直接转换
384 if (params instanceof java.util.Map) { 386 if (params instanceof java.util.Map) {
385 - @SuppressWarnings("unchecked")  
386 java.util.Map<String, Object> paramMap = (java.util.Map<String, Object>) params; 387 java.util.Map<String, Object> paramMap = (java.util.Map<String, Object>) params;
387 return new IotDeviceAuthReqDTO() 388 return new IotDeviceAuthReqDTO()
388 .setClientId(MapUtil.getStr(paramMap, "clientId")) 389 .setClientId(MapUtil.getStr(paramMap, "clientId"))
@@ -398,9 +399,8 @@ public class IotTcpUpstreamHandler implements Handler&lt;NetSocket&gt; { @@ -398,9 +399,8 @@ public class IotTcpUpstreamHandler implements Handler&lt;NetSocket&gt; {
398 // 其他情况尝试 JSON 转换 399 // 其他情况尝试 JSON 转换
399 String jsonStr = JsonUtils.toJsonString(params); 400 String jsonStr = JsonUtils.toJsonString(params);
400 return JsonUtils.parseObject(jsonStr, IotDeviceAuthReqDTO.class); 401 return JsonUtils.parseObject(jsonStr, IotDeviceAuthReqDTO.class);
401 -  
402 } catch (Exception e) { 402 } catch (Exception e) {
403 - log.error("[parseAuthParams][解析认证参数失败]", e); 403 + log.error("[parseAuthParams][解析认证参数({})失败]", params, e);
404 return null; 404 return null;
405 } 405 }
406 } 406 }