Commit a3fc0730e9ddf951a94bf22d846860062ac193a5

Authored by YunaiV
1 parent 9805cf24

review:【IoT 物联网】mqtt 协议的实现(整体没问题了)

yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/protocol/emqx/IotEmqxUpstreamProtocol.java
... ... @@ -67,6 +67,7 @@ public class IotEmqxUpstreamProtocol {
67 67 stop();
68 68  
69 69 // 异步关闭应用
  70 + // TODO haohao:是不是不用 sleep 也行哈?
70 71 Thread shutdownThread = new Thread(() -> {
71 72 ThreadUtil.sleep(1000);
72 73 log.error("[start][由于 MQTT 连接失败,正在关闭应用]");
... ... @@ -128,12 +129,9 @@ public class IotEmqxUpstreamProtocol {
128 129 private void connectMqttSync() {
129 130 String host = emqxProperties.getMqttHost();
130 131 int port = emqxProperties.getMqttPort();
131   -
132   - // 1. 创建同步等待对象
  132 + // 1. 连接 MQTT Broker
133 133 CountDownLatch latch = new CountDownLatch(1);
134 134 AtomicBoolean success = new AtomicBoolean(false);
135   -
136   - // 2. 连接 MQTT Broker
137 135 mqttClient.connect(port, host, connectResult -> {
138 136 if (connectResult.succeeded()) {
139 137 log.info("[connectMqttSync][MQTT 客户端连接成功, host: {}, port: {}]", host, port);
... ... @@ -147,16 +145,16 @@ public class IotEmqxUpstreamProtocol {
147 145 latch.countDown();
148 146 });
149 147  
150   - // 3. 等待连接结果
  148 + // 2. 等待连接结果
151 149 try {
  150 + // TODO @haohao:想了下,timeout 可以不设置,全靠 mqttclient 的超时时间?
152 151 boolean awaitResult = latch.await(10, java.util.concurrent.TimeUnit.SECONDS);
153 152 if (!awaitResult) {
154 153 log.error("[connectMqttSync][等待连接结果超时]");
155 154 throw new RuntimeException("连接 MQTT Broker 超时");
156 155 }
157 156 if (!success.get()) {
158   - throw new RuntimeException(String.format("首次连接 MQTT Broker 失败,地址: %s, 端口: %d",
159   - host, port));
  157 + throw new RuntimeException(String.format("首次连接 MQTT Broker 失败,地址: %s, 端口: %d", host, port));
160 158 }
161 159 } catch (InterruptedException e) {
162 160 Thread.currentThread().interrupt();
... ... @@ -171,7 +169,6 @@ public class IotEmqxUpstreamProtocol {
171 169 private void connectMqttAsync() {
172 170 String host = emqxProperties.getMqttHost();
173 171 int port = emqxProperties.getMqttPort();
174   -
175 172 mqttClient.connect(port, host, connectResult -> {
176 173 if (connectResult.succeeded()) {
177 174 log.info("[connectMqttAsync][MQTT 客户端重连成功]");
... ... @@ -199,7 +196,6 @@ public class IotEmqxUpstreamProtocol {
199 196  
200 197 long delay = emqxProperties.getReconnectDelayMs();
201 198 log.info("[reconnectWithDelay][将在 {} 毫秒后尝试重连 MQTT Broker]", delay);
202   -
203 199 vertx.setTimer(delay, timerId -> {
204 200 if (!isRunning) {
205 201 return;
... ... @@ -226,7 +222,6 @@ public class IotEmqxUpstreamProtocol {
226 222 if (mqttClient == null) {
227 223 return;
228 224 }
229   -
230 225 try {
231 226 if (mqttClient.isConnected()) {
232 227 // 1. 取消订阅所有主题
... ...
yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/protocol/emqx/router/IotEmqxDownstreamHandler.java
... ... @@ -79,6 +79,7 @@ public class IotEmqxDownstreamHandler {
79 79 boolean isReply = IotDeviceMessageUtils.isReplyMessage(message);
80 80  
81 81 // TODO @芋艿:需要添加对应的 Topic,所以需要先判断消息方法类型
  82 + // TODO @haohao:基于 method,然后逆推对应的 topic,可以哇?约定好~
82 83 // 根据消息方法和回复状态构建对应的主题
83 84 switch (methodEnum) {
84 85 case PROPERTY_POST:
... ...