Commit 71b45a29a304e033ea29dafc546873caa50c6678

Authored by YunaiV
1 parent 3c998597

【代码评审】IoT:MQTT 插件

yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-common/src/main/java/cn/iocoder/yudao/module/iot/plugin/common/util/IotPluginCommonUtils.java
... ... @@ -50,10 +50,10 @@ public class IotPluginCommonUtils {
50 50 }
51 51  
52 52 /**
53   - * 将对象转换为JSON字符串后写入响应
  53 + * 将对象转换为 JSON 字符串后写入响应
54 54 *
55 55 * @param routingContext 路由上下文
56   - * @param data 要转换为JSON的数据对象
  56 + * @param data 数据对象
57 57 */
58 58 @SuppressWarnings("deprecation")
59 59 public static void writeJson(RoutingContext routingContext, Object data) {
... ...
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
1 1 package cn.iocoder.yudao.module.iot.plugin.emqx.upstream;
2 2  
  3 +import cn.hutool.core.util.ArrayUtil;
3 4 import cn.iocoder.yudao.module.iot.api.device.IotDeviceUpstreamApi;
4 5 import cn.iocoder.yudao.module.iot.plugin.emqx.config.IotPluginEmqxProperties;
5 6 import cn.iocoder.yudao.module.iot.plugin.emqx.upstream.router.IotDeviceAuthVertxHandler;
... ... @@ -167,33 +168,28 @@ public class IotDeviceUpstreamServer {
167 168 */
168 169 private Future<Void> subscribeToTopics() {
169 170 String[] topics = emqxProperties.getMqttTopics();
170   - if (topics == null || topics.length == 0) {
  171 + if (ArrayUtil.isEmpty(topics)) {
171 172 log.warn("[subscribeToTopics] 未配置MQTT主题,跳过订阅");
172 173 return Future.succeededFuture();
173 174 }
174   -
175 175 log.info("[subscribeToTopics] 开始订阅设备上行消息主题");
176 176  
177 177 Future<Void> compositeFuture = Future.succeededFuture();
178   -
179 178 for (String topic : topics) {
180 179 String trimmedTopic = topic.trim();
181 180 if (trimmedTopic.isEmpty()) {
182 181 continue;
183 182 }
184   -
185 183 compositeFuture = compositeFuture.compose(v -> client.subscribe(trimmedTopic, DEFAULT_QOS.value())
186 184 .<Void>map(ack -> {
187 185 log.info("[subscribeToTopics] 成功订阅主题: {}", trimmedTopic);
188 186 return null;
189 187 })
190 188 .recover(err -> {
191   - log.error("[subscribeToTopics] 订阅主题失败: {}, 原因: {}",
192   - trimmedTopic, err.getMessage());
  189 + log.error("[subscribeToTopics] 订阅主题失败: {}, 原因: {}", trimmedTopic, err.getMessage());
193 190 return Future.<Void>succeededFuture(); // 继续订阅其他主题
194 191 }));
195 192 }
196   -
197 193 return compositeFuture;
198 194 }
199 195  
... ... @@ -205,7 +201,6 @@ public class IotDeviceUpstreamServer {
205 201 log.warn("[stop] 服务未运行,无需停止");
206 202 return;
207 203 }
208   -
209 204 log.info("[stop] 开始关闭服务");
210 205 isRunning = false;
211 206  
... ... @@ -213,11 +208,9 @@ public class IotDeviceUpstreamServer {
213 208 CompletableFuture<Void> serverFuture = server != null
214 209 ? server.close().toCompletionStage().toCompletableFuture()
215 210 : CompletableFuture.completedFuture(null);
216   -
217 211 CompletableFuture<Void> clientFuture = client != null
218 212 ? client.disconnect().toCompletionStage().toCompletableFuture()
219 213 : CompletableFuture.completedFuture(null);
220   -
221 214 CompletableFuture<Void> vertxFuture = vertx != null
222 215 ? vertx.close().toCompletionStage().toCompletableFuture()
223 216 : CompletableFuture.completedFuture(null);
... ...
yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-emqx/src/main/java/cn/iocoder/yudao/module/iot/plugin/emqx/upstream/router/IotDeviceAuthVertxHandler.java
... ... @@ -14,8 +14,7 @@ import java.util.Collections;
14 14  
15 15 /**
16 16 * IoT Emqx 连接认证的 Vert.x Handler
17   - * <a href=
18   - * "https://docs.emqx.com/zh/emqx/latest/access-control/authn/http.html">...</a>
  17 + * <a href="https://docs.emqx.com/zh/emqx/latest/access-control/authn/http.html">MQTT HTTP</a>
19 18 *
20 19 * @author haohao
21 20 */
... ... @@ -31,28 +30,29 @@ public class IotDeviceAuthVertxHandler implements Handler&lt;RoutingContext&gt; {
31 30 @SuppressWarnings("unchecked")
32 31 public void handle(RoutingContext routingContext) {
33 32 try {
  33 + // 构建认证请求 DTO
34 34 JsonObject json = routingContext.body().asJsonObject();
35 35 String clientId = json.getString("clientid");
36 36 String username = json.getString("username");
37 37 String password = json.getString("password");
38   -
39   - // 构建认证请求DTO
40 38 IotDeviceEmqxAuthReqDTO authReqDTO = new IotDeviceEmqxAuthReqDTO()
41 39 .setClientId(clientId)
42 40 .setUsername(username)
43 41 .setPassword(password);
44 42  
45   - // 调用认证API
  43 + // 调用认证 API
46 44 CommonResult<Boolean> authResult = deviceUpstreamApi.authenticateEmqxConnection(authReqDTO);
47 45 if (authResult.getCode() != 0 || !authResult.getData()) {
48 46 IotPluginCommonUtils.writeJson(routingContext, Collections.singletonMap("result", "deny"));
49 47 return;
50 48 }
51 49  
  50 + // 响应结果
52 51 IotPluginCommonUtils.writeJson(routingContext, Collections.singletonMap("result", "allow"));
53 52 } catch (Exception e) {
54   - log.error("[handle][EMQX认证异常]", e);
  53 + log.error("[handle][EMQX 认证异常]", e);
55 54 IotPluginCommonUtils.writeJson(routingContext, Collections.singletonMap("result", "deny"));
56 55 }
57 56 }
  57 +
58 58 }
59 59 \ No newline at end of file
... ...