|
@@ -9,8 +9,12 @@ import com.yunfeiyun.agmp.iots.core.cmd.checker.CmdResultCheckService;
|
|
|
import org.eclipse.paho.client.mqttv3.*;
|
|
import org.eclipse.paho.client.mqttv3.*;
|
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
+import org.springframework.scheduling.annotation.Async;
|
|
|
|
|
+import org.springframework.scheduling.annotation.EnableAsync;
|
|
|
|
|
+import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
|
|
|
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
|
+import java.util.concurrent.CompletableFuture;
|
|
|
|
|
|
|
|
public class MqttSubscriber implements MqttCallbackExtended {
|
|
public class MqttSubscriber implements MqttCallbackExtended {
|
|
|
|
|
|
|
@@ -19,11 +23,13 @@ public class MqttSubscriber implements MqttCallbackExtended {
|
|
|
private MqttClient mqttClient;
|
|
private MqttClient mqttClient;
|
|
|
|
|
|
|
|
private MqttCore mqttCore;
|
|
private MqttCore mqttCore;
|
|
|
|
|
+ private ThreadPoolTaskExecutor threadPoolTaskExecutor;
|
|
|
|
|
|
|
|
|
|
|
|
|
public void init(MqttCore mqttCore) throws MqttException {
|
|
public void init(MqttCore mqttCore) throws MqttException {
|
|
|
this.mqttCore = mqttCore;
|
|
this.mqttCore = mqttCore;
|
|
|
this.mqttClient = mqttCore.getClient();
|
|
this.mqttClient = mqttCore.getClient();
|
|
|
|
|
+ this.threadPoolTaskExecutor = SpringUtils.getBean("MqttTopicMessageHandlerExecutor");
|
|
|
mqttClient.setCallback(this);
|
|
mqttClient.setCallback(this);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -39,34 +45,34 @@ public class MqttSubscriber implements MqttCallbackExtended {
|
|
|
*/
|
|
*/
|
|
|
@Override
|
|
@Override
|
|
|
public void messageArrived(String topic, MqttMessage mqttMessage) {
|
|
public void messageArrived(String topic, MqttMessage mqttMessage) {
|
|
|
- try {
|
|
|
|
|
-
|
|
|
|
|
- String msgContent = new String(mqttMessage.getPayload(), StandardCharsets.UTF_8);
|
|
|
|
|
- log.info("【上报数据:收到】收到mqtt消息:" + topic + ", " + msgContent);
|
|
|
|
|
- JSONObject obj = null;
|
|
|
|
|
|
|
+ CompletableFuture.runAsync(() -> {
|
|
|
try {
|
|
try {
|
|
|
- obj = JSON.parseObject(msgContent);
|
|
|
|
|
|
|
+ String msgContent = new String(mqttMessage.getPayload(), StandardCharsets.UTF_8);
|
|
|
|
|
+ log.info("【上报数据:收到】收到mqtt消息:" + topic + ", " + msgContent);
|
|
|
|
|
+ JSONObject obj = null;
|
|
|
|
|
+ try {
|
|
|
|
|
+ obj = JSON.parseObject(msgContent);
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error("【上报数据:解析异常】收到mqtt消息:" + topic + ", " + msgContent);
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ Device device = mqttCore.getDevice(topic);
|
|
|
|
|
+ if (null == device) {
|
|
|
|
|
+ // 当收到mqtt消息无对应的设备类型时,则丢弃消息。
|
|
|
|
|
+ IotException.UNKNOWN_DEVICE.throwException();
|
|
|
|
|
+ }
|
|
|
|
|
+ Object result = device.receiveData(topic, obj, mqttCore.getConnectionId());
|
|
|
|
|
+ log.info("【上报数据:处理结果】{}", result);
|
|
|
|
|
+ //如果是“设备属性”消息,进行执行结果检查
|
|
|
|
|
+ if (device.isDeviceProps(obj)) {
|
|
|
|
|
+ SpringUtils.getBean(CmdResultCheckService.class).beginCheck(device.findIotDevice(topic, obj, mqttCore.getConnectionId()), obj);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ log.error("其它数据");
|
|
|
|
|
+ }
|
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
|
- log.error("【上报数据:解析异常】收到mqtt消息:" + topic + ", " + msgContent);
|
|
|
|
|
- return;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- Device device = mqttCore.getDevice(topic);
|
|
|
|
|
- if (null == device) {
|
|
|
|
|
- // 当收到mqtt消息无对应的设备类型时,则丢弃消息。
|
|
|
|
|
- IotException.UNKNOWN_DEVICE.throwException();
|
|
|
|
|
|
|
+ log.error("【接收上报】异常{}", e);
|
|
|
}
|
|
}
|
|
|
- Object result = device.receiveData(topic, obj, mqttCore.getConnectionId());
|
|
|
|
|
- log.info("【上报数据:处理结果】{}", result);
|
|
|
|
|
- //如果是“设备属性”消息,进行执行结果检查
|
|
|
|
|
- if (device.isDeviceProps(obj)) {
|
|
|
|
|
- SpringUtils.getBean(CmdResultCheckService.class).beginCheck(device.findIotDevice(topic, obj, mqttCore.getConnectionId()), obj);
|
|
|
|
|
- } else {
|
|
|
|
|
- log.error("其它数据");
|
|
|
|
|
- }
|
|
|
|
|
- } catch (Exception e) {
|
|
|
|
|
- log.error("【接收上报】异常{}", e);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ },threadPoolTaskExecutor);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|