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