Commit 6639d371328a51bdc70ea43e09d74f35f3d6af0e

Authored by YunaiV
1 parent e9a99c1e

【代码评审】IoT:整体实现

yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/job/rule/IotRuleSceneJob.java
1 1 package cn.iocoder.yudao.module.iot.job.rule;
2 2  
  3 +import cn.hutool.core.map.MapUtil;
3 4 import cn.iocoder.yudao.module.iot.enums.rule.IotRuleSceneTriggerTypeEnum;
4 5 import cn.iocoder.yudao.module.iot.service.rule.IotRuleSceneService;
5 6 import jakarta.annotation.Resource;
... ... @@ -41,7 +42,7 @@ public class IotRuleSceneJob extends QuartzJobBean {
41 42 * @return JobData Map
42 43 */
43 44 public static Map<String, Object> buildJobDataMap(Long ruleSceneId) {
44   - return Map.of(JOB_DATA_KEY_RULE_SCENE_ID, ruleSceneId);
  45 + return MapUtil.of(JOB_DATA_KEY_RULE_SCENE_ID, ruleSceneId);
45 46 }
46 47  
47 48 /**
... ...
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/device/IotDeviceGroupService.java
1 1 package cn.iocoder.yudao.module.iot.service.device;
2 2  
3 3 import cn.hutool.core.collection.CollUtil;
  4 +import cn.hutool.core.collection.ListUtil;
4 5 import cn.iocoder.yudao.framework.common.pojo.PageResult;
5 6 import cn.iocoder.yudao.module.iot.controller.admin.device.vo.group.IotDeviceGroupPageReqVO;
6 7 import cn.iocoder.yudao.module.iot.controller.admin.device.vo.group.IotDeviceGroupSaveReqVO;
... ... @@ -48,7 +49,7 @@ public interface IotDeviceGroupService {
48 49 */
49 50 default List<IotDeviceGroupDO> validateDeviceGroupExists(Collection<Long> ids) {
50 51 if (CollUtil.isEmpty(ids)) {
51   - return List.of();
  52 + return ListUtil.empty();
52 53 }
53 54 return convertList(ids, this::validateDeviceGroupExists);
54 55 }
... ...
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/device/data/IotDeviceLogServiceImpl.java
1 1 package cn.iocoder.yudao.module.iot.service.device.data;
2 2  
3 3 import cn.hutool.core.date.LocalDateTimeUtil;
  4 +import cn.hutool.core.map.MapUtil;
4 5 import cn.hutool.core.util.IdUtil;
5 6 import cn.hutool.core.util.StrUtil;
6 7 import cn.iocoder.yudao.framework.common.pojo.PageResult;
... ... @@ -87,7 +88,7 @@ public class IotDeviceLogServiceImpl implements IotDeviceLogService {
87 88 Long timeMillis = timestamp.getTime();
88 89 // 消息数量转换
89 90 Integer count = ((Number) map.get("data")).intValue();
90   - return Map.of(timeMillis, count);
  91 + return MapUtil.of(timeMillis, count);
91 92 })
92 93 .collect(Collectors.toList());
93 94 }
... ... @@ -103,7 +104,7 @@ public class IotDeviceLogServiceImpl implements IotDeviceLogService {
103 104 Long timeMillis = timestamp.getTime();
104 105 // 消息数量转换
105 106 Integer count = ((Number) map.get("data")).intValue();
106   - return Map.of(timeMillis, count);
  107 + return MapUtil.of(timeMillis, count);
107 108 })
108 109 .collect(Collectors.toList());
109 110 }
... ...
yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-emqx/src/main/java/cn/iocoder/yudao/module/iot/plugin/emqx/upstream/IotDeviceUpstreamServer.java
... ... @@ -85,11 +85,12 @@ public class IotDeviceUpstreamServer {
85 85 }
86 86 log.info("[start][开始启动服务]");
87 87  
  88 + // TODO @haohao:建议先启动 MQTT Broker,再启动 HTTP Server。类似 jdbc 先连接了,在启动 tomcat 的味道
88 89 // 1. 启动 HTTP 服务器
89 90 CompletableFuture<Void> httpFuture = server.listen(emqxProperties.getAuthPort())
90 91 .toCompletionStage()
91 92 .toCompletableFuture()
92   - .thenAccept(v -> log.info("[start][HTTP服务器启动完成,端口: {}]", server.actualPort()));
  93 + .thenAccept(v -> log.info("[start][HTTP 服务器启动完成,端口: {}]", server.actualPort()));
93 94  
94 95 // 2. 连接 MQTT Broker
95 96 CompletableFuture<Void> mqttFuture = connectMqtt()
... ... @@ -98,16 +99,16 @@ public class IotDeviceUpstreamServer {
98 99 .thenAccept(v -> {
99 100 // 2.1 添加 MQTT 断开重连监听器
100 101 client.closeHandler(closeEvent -> {
101   - log.warn("[closeHandler][MQTT连接已断开,准备重连]");
  102 + log.warn("[closeHandler][MQTT 连接已断开,准备重连]");
102 103 reconnectWithDelay();
103 104 });
104   - // 2. 设置 MQTT 消息处理器
  105 + // 2.2 设置 MQTT 消息处理器
105 106 setupMessageHandler();
106 107 });
107 108  
108 109 // 3. 等待所有服务启动完成
109 110 CompletableFuture.allOf(httpFuture, mqttFuture)
110   - .orTimeout(CONNECTION_TIMEOUT_MS, TimeUnit.MILLISECONDS)
  111 + .orTimeout(CONNECTION_TIMEOUT_MS, TimeUnit.MILLISECONDS) // TODO @芋艿:JDK8 不兼容
111 112 .whenComplete((result, error) -> {
112 113 if (error != null) {
113 114 log.error("[start][服务启动失败]", error);
... ... @@ -123,7 +124,7 @@ public class IotDeviceUpstreamServer {
123 124 */
124 125 private void setupMessageHandler() {
125 126 client.publishHandler(mqttMessageHandler::handle);
126   - log.debug("[setupMessageHandler][MQTT消息处理器设置完成]");
  127 + log.debug("[setupMessageHandler][MQTT 消息处理器设置完成]");
127 128 }
128 129  
129 130 /**
... ... @@ -203,26 +204,30 @@ public class IotDeviceUpstreamServer {
203 204 isRunning = false;
204 205  
205 206 try {
206   - CompletableFuture<Void> serverFuture = server != null
207   - ? server.close().toCompletionStage().toCompletableFuture()
208   - : CompletableFuture.completedFuture(null);
209   - CompletableFuture<Void> clientFuture = client != null
210   - ? client.disconnect().toCompletionStage().toCompletableFuture()
211   - : CompletableFuture.completedFuture(null);
212   - CompletableFuture<Void> vertxFuture = vertx != null
213   - ? vertx.close().toCompletionStage().toCompletableFuture()
214   - : CompletableFuture.completedFuture(null);
215   -
216   - // 等待所有资源关闭
217   - CompletableFuture.allOf(serverFuture, clientFuture, vertxFuture)
218   - .orTimeout(CONNECTION_TIMEOUT_MS, TimeUnit.MILLISECONDS)
219   - .whenComplete((result, error) -> {
220   - if (error != null) {
221   - log.error("[stop][服务关闭过程中发生异常]", error);
222   - } else {
223   - log.info("[stop][所有服务关闭完成]");
224   - }
225   - });
  207 + // 关闭 HTTP 服务器
  208 + if (server != null) {
  209 + server.close()
  210 + .toCompletionStage()
  211 + .toCompletableFuture()
  212 + .join();
  213 + }
  214 +
  215 + // 关闭 MQTT 客户端
  216 + if (client != null) {
  217 + client.disconnect()
  218 + .toCompletionStage()
  219 + .toCompletableFuture()
  220 + .join();
  221 + }
  222 +
  223 + // 关闭 Vertx 实例
  224 + if (vertx!= null) {
  225 + vertx.close()
  226 + .toCompletionStage()
  227 + .toCompletableFuture()
  228 + .join();
  229 + }
  230 + log.info("[stop][关闭完成]");
226 231 } catch (Exception e) {
227 232 log.error("[stop][关闭服务异常]", e);
228 233 throw new RuntimeException("关闭 IoT 设备上行服务失败", e);
... ...