Commit 5086e1225f55d18491b04b8f5c8638bde4c710c7

Authored by YunaiV
1 parent d6e33484

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

yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/rule/data/action/IotWebSocketDataRuleAction.java
1 package cn.iocoder.yudao.module.iot.service.rule.data.action; 1 package cn.iocoder.yudao.module.iot.service.rule.data.action;
2 2
  3 +import cn.hutool.core.util.StrUtil;
3 import cn.iocoder.yudao.module.iot.core.mq.message.IotDeviceMessage; 4 import cn.iocoder.yudao.module.iot.core.mq.message.IotDeviceMessage;
4 import cn.iocoder.yudao.module.iot.dal.dataobject.rule.config.IotDataSinkWebSocketConfig; 5 import cn.iocoder.yudao.module.iot.dal.dataobject.rule.config.IotDataSinkWebSocketConfig;
5 import cn.iocoder.yudao.module.iot.enums.rule.IotDataSinkTypeEnum; 6 import cn.iocoder.yudao.module.iot.enums.rule.IotDataSinkTypeEnum;
@@ -28,11 +29,11 @@ public class IotWebSocketDataRuleAction extends @@ -28,11 +29,11 @@ public class IotWebSocketDataRuleAction extends
28 29
29 @Override 30 @Override
30 protected IotWebSocketClient initProducer(IotDataSinkWebSocketConfig config) throws Exception { 31 protected IotWebSocketClient initProducer(IotDataSinkWebSocketConfig config) throws Exception {
31 - // 1.1 参数校验  
32 - if (config.getServerUrl() == null || config.getServerUrl().trim().isEmpty()) { 32 + // 1. 参数校验
  33 + if (StrUtil.isBlank(config.getServerUrl())) {
33 throw new IllegalArgumentException("WebSocket 服务器地址不能为空"); 34 throw new IllegalArgumentException("WebSocket 服务器地址不能为空");
34 } 35 }
35 - if (!config.getServerUrl().startsWith("ws://") && !config.getServerUrl().startsWith("wss://")) { 36 + if (!StrUtil.startWithAny(config.getServerUrl(), "ws://", "wss://")) {
36 throw new IllegalArgumentException("WebSocket 服务器地址必须以 ws:// 或 wss:// 开头"); 37 throw new IllegalArgumentException("WebSocket 服务器地址必须以 ws:// 或 wss:// 开头");
37 } 38 }
38 39
@@ -61,6 +62,7 @@ public class IotWebSocketDataRuleAction extends @@ -61,6 +62,7 @@ public class IotWebSocketDataRuleAction extends
61 protected void execute(IotDeviceMessage message, IotDataSinkWebSocketConfig config) throws Exception { 62 protected void execute(IotDeviceMessage message, IotDataSinkWebSocketConfig config) throws Exception {
62 try { 63 try {
63 // 1.1 获取或创建 WebSocket 客户端 64 // 1.1 获取或创建 WebSocket 客户端
  65 + // TODO @puhui999:需要加锁,保证必须连接上;
64 IotWebSocketClient webSocketClient = getProducer(config); 66 IotWebSocketClient webSocketClient = getProducer(config);
65 // 1.2 检查连接状态,如果断开则重新连接 67 // 1.2 检查连接状态,如果断开则重新连接
66 if (!webSocketClient.isConnected()) { 68 if (!webSocketClient.isConnected()) {
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 { @@ -31,6 +31,7 @@ public class IotTcpClient {
31 private final Integer connectTimeoutMs; 31 private final Integer connectTimeoutMs;
32 private final Integer readTimeoutMs; 32 private final Integer readTimeoutMs;
33 private final Boolean ssl; 33 private final Boolean ssl;
  34 + // TODO @puhui999:sslCertPath 是不是没在用?
34 private final String sslCertPath; 35 private final String sslCertPath;
35 private final String dataFormat; 36 private final String dataFormat;
36 37
@@ -47,6 +48,7 @@ public class IotTcpClient { @@ -47,6 +48,7 @@ public class IotTcpClient {
47 this.readTimeoutMs = readTimeoutMs != null ? readTimeoutMs : IotDataSinkTcpConfig.DEFAULT_READ_TIMEOUT_MS; 48 this.readTimeoutMs = readTimeoutMs != null ? readTimeoutMs : IotDataSinkTcpConfig.DEFAULT_READ_TIMEOUT_MS;
48 this.ssl = ssl != null ? ssl : IotDataSinkTcpConfig.DEFAULT_SSL; 49 this.ssl = ssl != null ? ssl : IotDataSinkTcpConfig.DEFAULT_SSL;
49 this.sslCertPath = sslCertPath; 50 this.sslCertPath = sslCertPath;
  51 + // TODO @puhui999:可以使用 StrUtil.defaultIfBlank 方法简化
50 this.dataFormat = dataFormat != null ? dataFormat : IotDataSinkTcpConfig.DEFAULT_DATA_FORMAT; 52 this.dataFormat = dataFormat != null ? dataFormat : IotDataSinkTcpConfig.DEFAULT_DATA_FORMAT;
51 } 53 }
52 54
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 { @@ -45,6 +45,7 @@ public class IotWebSocketClient implements WebSocket.Listener {
45 /** 45 /**
46 * 连接到 WebSocket 服务器 46 * 连接到 WebSocket 服务器
47 */ 47 */
  48 + @SuppressWarnings("resource")
48 public void connect() throws Exception { 49 public void connect() throws Exception {
49 if (connected.get()) { 50 if (connected.get()) {
50 log.warn("[connect][WebSocket 客户端已经连接,无需重复连接]"); 51 log.warn("[connect][WebSocket 客户端已经连接,无需重复连接]");
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 { @@ -406,10 +406,7 @@ public class IotSceneRuleServiceImpl implements IotSceneRuleService {
406 */ 406 */
407 private void updateLastTriggerTime(Long id) { 407 private void updateLastTriggerTime(Long id) {
408 try { 408 try {
409 - IotSceneRuleDO updateObj = new IotSceneRuleDO()  
410 - .setId(id)  
411 - .setLastTriggerTime(LocalDateTime.now());  
412 - sceneRuleMapper.updateById(updateObj); 409 + sceneRuleMapper.updateById(new IotSceneRuleDO().setId(id).setLastTriggerTime(LocalDateTime.now()));
413 } catch (Exception e) { 410 } catch (Exception e) {
414 log.error("[updateLastTriggerTime][规则场景编号({}) 更新最后触发时间异常]", id, e); 411 log.error("[updateLastTriggerTime][规则场景编号({}) 更新最后触发时间异常]", id, e);
415 } 412 }
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 @@ -38,6 +38,7 @@ public class IotDevicePropertyPostTriggerMatcher implements IotSceneRuleTriggerM
38 38
39 // 1.3 检查消息中是否包含触发器指定的属性标识符 39 // 1.3 检查消息中是否包含触发器指定的属性标识符
40 // 注意:属性上报可能同时上报多个属性,所以需要判断 trigger.getIdentifier() 是否在 message 的 params 中 40 // 注意:属性上报可能同时上报多个属性,所以需要判断 trigger.getIdentifier() 是否在 message 的 params 中
  41 + // TODO @puhui999:可以考虑 notXXX 方法,简化代码(尽量取反)
41 if (!IotDeviceMessageUtils.containsIdentifier(message, trigger.getIdentifier())) { 42 if (!IotDeviceMessageUtils.containsIdentifier(message, trigger.getIdentifier())) {
42 IotSceneRuleMatcherHelper.logTriggerMatchFailure(message, trigger, "消息中不包含属性: " + 43 IotSceneRuleMatcherHelper.logTriggerMatchFailure(message, trigger, "消息中不包含属性: " +
43 trigger.getIdentifier()); 44 trigger.getIdentifier());