mirror of
https://gitee.com/yudaocode/yudao-boot-mini.git
synced 2026-03-22 05:27:15 +08:00
review:【iot】“修复了一些 Iot 模块 TODO 提到的问题”
This commit is contained in:
@@ -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()) {
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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 客户端已经连接,无需重复连接]");
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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());
|
||||
|
||||
Reference in New Issue
Block a user