瀏覽代碼

【代码评审】IoT:MQTT 插件

YunaiV 5 月之前
父節點
當前提交
36dd18d41f

+ 2 - 0
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/util/MqttSignUtils.java

@@ -59,9 +59,11 @@ public class MqttSignUtils {
     @Getter
     @AllArgsConstructor
     public static class MqttSignResult {
+
         private final String clientId;
         private final String username;
         private final String password;
+
     }
 
 }

+ 1 - 0
yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-emqx/src/main/java/cn/iocoder/yudao/module/iot/plugin/emqx/config/IotPluginEmqxProperties.java

@@ -36,6 +36,7 @@ public class IotPluginEmqxProperties {
      */
     private boolean mqttSsl;
 
+    // TODO @haohao:这个是不是改成数组?
     /**
      * 订阅的主题
      */

+ 1 - 0
yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-emqx/src/main/java/cn/iocoder/yudao/module/iot/plugin/emqx/downstream/IotDeviceDownstreamHandlerImpl.java

@@ -26,6 +26,7 @@ public class IotDeviceDownstreamHandlerImpl implements IotDeviceDownstreamHandle
 
     private static final String SYS_TOPIC_PREFIX = "/sys/";
 
+    // TODO @haohao:讨论,感觉 mqtt 和 http,可以做个相对统一的格式哈。
     // 设备服务调用 标准 JSON
     // 请求Topic:/sys/${productKey}/${deviceName}/thing/service/${tsl.service.identifier}
     // 响应Topic:/sys/${productKey}/${deviceName}/thing/service/${tsl.service.identifier}_reply

+ 22 - 5
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

@@ -26,17 +26,33 @@ import java.util.concurrent.TimeUnit;
 @Slf4j
 public class IotDeviceUpstreamServer {
 
-    private static final int RECONNECT_DELAY_MS = 5000; // 重连延迟时间(毫秒)
-    private static final int CONNECTION_TIMEOUT_MS = 10000; // 连接超时时间(毫秒)
-    private static final String TOPIC_SEPARATOR = ","; // 主题分隔符
-    private static final MqttQoS DEFAULT_QOS = MqttQoS.AT_LEAST_ONCE; // 默认QoS级别
+    /**
+     * 重连延迟时间(毫秒)
+     */
+    private static final int RECONNECT_DELAY_MS = 5000;
+    /**
+     * 连接超时时间(毫秒)
+     */
+    private static final int CONNECTION_TIMEOUT_MS = 10000;
+    /**
+     * 主题分隔符
+     */
+    private static final String TOPIC_SEPARATOR = ",";
+    /**
+     * 默认 QoS 级别
+     */
+    private static final MqttQoS DEFAULT_QOS = MqttQoS.AT_LEAST_ONCE;
 
     private final Vertx vertx;
     private final HttpServer server;
     private final MqttClient client;
     private final IotPluginEmqxProperties emqxProperties;
     private final IotDeviceMqttMessageHandler mqttMessageHandler;
-    private volatile boolean isRunning = false; // 服务运行状态标志
+
+    /**
+     * 服务运行状态标志
+     */
+    private volatile boolean isRunning = false;
 
     public IotDeviceUpstreamServer(IotPluginEmqxProperties emqxProperties,
                                    IotDeviceUpstreamApi deviceUpstreamApi,
@@ -50,6 +66,7 @@ public class IotDeviceUpstreamServer {
         Router router = Router.router(vertx);
         router.route().handler(BodyHandler.create()); // 处理 Body
         router.post(IotDeviceAuthVertxHandler.PATH)
+                // TODO @haohao:疑问,mqtt 的认证,需要通过 http 呀?
                 .handler(new IotDeviceAuthVertxHandler(deviceUpstreamApi));
         // 创建 HttpServer 实例
         this.server = vertx.createHttpServer().requestHandler(router);

+ 3 - 1
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

@@ -27,7 +27,7 @@ public class IotDeviceAuthVertxHandler implements Handler<RoutingContext> {
     @Override
     @SuppressWarnings("unchecked")
     public void handle(RoutingContext routingContext) {
-
+        // TODO @haohao:try catch 兜底异常
         JsonObject json = routingContext.body().asJsonObject();
         String clientId = json.getString("clientid");
         String username = json.getString("username");
@@ -40,9 +40,11 @@ public class IotDeviceAuthVertxHandler implements Handler<RoutingContext> {
             denyAccess(routingContext);
             return;
         }
+        // TODO @haohao:貌似可以考虑封装一个 writeJson ,里面有个参数是 data,然后里面去 JsonUtils.toJsonString(data)
         IotPluginCommonUtils.writeJson(routingContext, "{\"result\": \"allow\"}");
     }
 
+    // TODO @haohao:下面两个简单方法,貌似可以考虑不抽小方法哈。
     private void denyAccess(RoutingContext routingContext) {
         IotPluginCommonUtils.writeJson(routingContext, "{\"result\": \"deny\"}");
     }

+ 6 - 4
yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-emqx/src/main/java/cn/iocoder/yudao/module/iot/plugin/emqx/upstream/router/IotDeviceMqttMessageHandler.java

@@ -26,12 +26,14 @@ import java.util.Arrays;
 @Slf4j
 public class IotDeviceMqttMessageHandler {
 
+    // TODO @haohao:讨论,感觉 mqtt 和 http,可以做个相对统一的格式哈。
     // 设备上报属性 标准 JSON
-    // 请求Topic:/sys/${productKey}/${deviceName}/thing/event/property/post
-    // 响应Topic:/sys/${productKey}/${deviceName}/thing/event/property/post_reply
+    // 请求 Topic:/sys/${productKey}/${deviceName}/thing/event/property/post
+    // 响应 Topic:/sys/${productKey}/${deviceName}/thing/event/property/post_reply
+
     // 设备上报事件 标准 JSON
-    // 请求Topic:/sys/${productKey}/${deviceName}/thing/event/${tsl.event.identifier}/post
-    // 响应Topic:/sys/${productKey}/${deviceName}/thing/event/${tsl.event.identifier}/post_reply
+    // 请求 Topic:/sys/${productKey}/${deviceName}/thing/event/${tsl.event.identifier}/post
+    // 响应 Topic:/sys/${productKey}/${deviceName}/thing/event/${tsl.event.identifier}/post_reply
 
     private static final String SYS_TOPIC_PREFIX = "/sys/";
     private static final String PROPERTY_POST_TOPIC = "/thing/event/property/post";