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