Explorar el Código

review:【iot】“修复了一些 Iot 模块 TODO 提到的问题”

YunaiV hace 5 meses
padre
commit
5086e1225f

+ 5 - 3
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/rule/data/action/IotWebSocketDataRuleAction.java

@@ -1,5 +1,6 @@
 package cn.iocoder.yudao.module.iot.service.rule.data.action;
 
+import cn.hutool.core.util.StrUtil;
 import cn.iocoder.yudao.module.iot.core.mq.message.IotDeviceMessage;
 import cn.iocoder.yudao.module.iot.dal.dataobject.rule.config.IotDataSinkWebSocketConfig;
 import cn.iocoder.yudao.module.iot.enums.rule.IotDataSinkTypeEnum;
@@ -28,11 +29,11 @@ public class IotWebSocketDataRuleAction extends
 
     @Override
     protected IotWebSocketClient initProducer(IotDataSinkWebSocketConfig config) throws Exception {
-        // 1.1 参数校验
-        if (config.getServerUrl() == null || config.getServerUrl().trim().isEmpty()) {
+        // 1. 参数校验
+        if (StrUtil.isBlank(config.getServerUrl())) {
             throw new IllegalArgumentException("WebSocket 服务器地址不能为空");
         }
-        if (!config.getServerUrl().startsWith("ws://") && !config.getServerUrl().startsWith("wss://")) {
+        if (!StrUtil.startWithAny(config.getServerUrl(), "ws://", "wss://")) {
             throw new IllegalArgumentException("WebSocket 服务器地址必须以 ws:// 或 wss:// 开头");
         }
 
@@ -61,6 +62,7 @@ public class IotWebSocketDataRuleAction extends
     protected void execute(IotDeviceMessage message, IotDataSinkWebSocketConfig config) throws Exception {
         try {
             // 1.1 获取或创建 WebSocket 客户端
+            // TODO @puhui999:需要加锁,保证必须连接上;
             IotWebSocketClient webSocketClient = getProducer(config);
             // 1.2 检查连接状态,如果断开则重新连接
             if (!webSocketClient.isConnected()) {

+ 2 - 0
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/rule/data/action/tcp/IotTcpClient.java

@@ -31,6 +31,7 @@ public class IotTcpClient {
     private final Integer connectTimeoutMs;
     private final Integer readTimeoutMs;
     private final Boolean ssl;
+    // TODO @puhui999:sslCertPath 是不是没在用?
     private final String sslCertPath;
     private final String dataFormat;
 
@@ -47,6 +48,7 @@ public class IotTcpClient {
         this.readTimeoutMs = readTimeoutMs != null ? readTimeoutMs : IotDataSinkTcpConfig.DEFAULT_READ_TIMEOUT_MS;
         this.ssl = ssl != null ? ssl : IotDataSinkTcpConfig.DEFAULT_SSL;
         this.sslCertPath = sslCertPath;
+        // TODO @puhui999:可以使用 StrUtil.defaultIfBlank 方法简化
         this.dataFormat = dataFormat != null ? dataFormat : IotDataSinkTcpConfig.DEFAULT_DATA_FORMAT;
     }
 

+ 1 - 0
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/rule/data/action/websocket/IotWebSocketClient.java

@@ -45,6 +45,7 @@ public class IotWebSocketClient implements WebSocket.Listener {
     /**
      * 连接到 WebSocket 服务器
      */
+    @SuppressWarnings("resource")
     public void connect() throws Exception {
         if (connected.get()) {
             log.warn("[connect][WebSocket 客户端已经连接,无需重复连接]");

+ 1 - 4
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/rule/scene/IotSceneRuleServiceImpl.java

@@ -406,10 +406,7 @@ public class IotSceneRuleServiceImpl implements IotSceneRuleService {
      */
     private void updateLastTriggerTime(Long id) {
         try {
-            IotSceneRuleDO updateObj = new IotSceneRuleDO()
-                    .setId(id)
-                    .setLastTriggerTime(LocalDateTime.now());
-            sceneRuleMapper.updateById(updateObj);
+            sceneRuleMapper.updateById(new IotSceneRuleDO().setId(id).setLastTriggerTime(LocalDateTime.now()));
         } catch (Exception e) {
             log.error("[updateLastTriggerTime][规则场景编号({}) 更新最后触发时间异常]", id, e);
         }

+ 1 - 0
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/rule/scene/matcher/trigger/IotDevicePropertyPostTriggerMatcher.java

@@ -38,6 +38,7 @@ public class IotDevicePropertyPostTriggerMatcher implements IotSceneRuleTriggerM
 
         // 1.3 检查消息中是否包含触发器指定的属性标识符
         // 注意:属性上报可能同时上报多个属性,所以需要判断 trigger.getIdentifier() 是否在 message 的 params 中
+        // TODO @puhui999:可以考虑 notXXX 方法,简化代码(尽量取反)
         if (!IotDeviceMessageUtils.containsIdentifier(message, trigger.getIdentifier())) {
             IotSceneRuleMatcherHelper.logTriggerMatchFailure(message, trigger, "消息中不包含属性: " +
                     trigger.getIdentifier());