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