|
@@ -3,6 +3,7 @@ package com.hfln.device.infrastructure.mqtt.handler;
|
|
|
import com.hfln.device.common.util.JsonUtil;
|
|
|
import com.hfln.device.domain.port.DeviceEventPort;
|
|
|
import com.hfln.device.domain.service.DeviceManagerService;
|
|
|
+import com.hfln.device.infrastructure.config.TopicDealExecutor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Qualifier;
|
|
@@ -12,6 +13,7 @@ import org.springframework.stereotype.Component;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
+import java.util.concurrent.RejectedExecutionException;
|
|
|
import java.util.regex.Matcher;
|
|
|
import java.util.regex.Pattern;
|
|
|
import java.util.stream.Collectors;
|
|
@@ -51,6 +53,9 @@ public class DeviceMessageHandler {
|
|
|
@Autowired
|
|
|
private DeviceManagerService deviceManagerService;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private TopicDealExecutor topicDealExecutor;
|
|
|
+
|
|
|
/**
|
|
|
* MQTT消息统一入口处理方法
|
|
|
*
|
|
@@ -72,32 +77,105 @@ public class DeviceMessageHandler {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- log.debug("Received device message: topic={}, payload={}", topic, payload);
|
|
|
+// log.debug("Received device message: topic={}, payload={}", topic, payload);
|
|
|
|
|
|
// 根据主题路由到不同的处理方法
|
|
|
// 提取主题的操作部分(最后一段)
|
|
|
- String action = extractActionFromTopic(topic);
|
|
|
- if (action != null) {
|
|
|
- switch (action) {
|
|
|
- case "login":
|
|
|
- handleDeviceLogin(topic, payload);
|
|
|
- break;
|
|
|
- case "keepalive":
|
|
|
- handleDeviceKeepAlive(topic, payload);
|
|
|
- break;
|
|
|
- case "report_device_info":
|
|
|
- handleDeviceReportDeviceInfo(topic, payload);
|
|
|
- break;
|
|
|
- case "report_device_param":
|
|
|
- handleDeviceReportDeviceParam(topic, payload);
|
|
|
- break;
|
|
|
- case "dsp_data":
|
|
|
- handleDeviceDspData(topic, payload);
|
|
|
- break;
|
|
|
+ String[] parts = topic.split("/");
|
|
|
+ String action = parts[parts.length - 1];
|
|
|
+ String devId = parts[parts.length - 2];
|
|
|
+
|
|
|
+ try {
|
|
|
+
|
|
|
+ topicDealExecutor.submitTask(devId, () -> this.handleMessage(action, topic, payload));
|
|
|
+ } catch (RejectedExecutionException e) {
|
|
|
+
|
|
|
+ log.error("Rejected execution message, devId:{}, topic:{}", devId, topic, e);
|
|
|
+ try {
|
|
|
+ this.handleMessage(action, topic, payload);
|
|
|
+ } catch (Exception ex) {
|
|
|
+ log.error("Error handling message", ex);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("Error handling message, devId:{}, topic:{}", devId, topic, e);
|
|
|
+ }
|
|
|
+//
|
|
|
+// if (action != null) {
|
|
|
+// switch (action) {
|
|
|
+// case "login":
|
|
|
+// handleDeviceLogin(topic, payload);
|
|
|
+// break;
|
|
|
+// case "keepalive":
|
|
|
+// handleDeviceKeepAlive(topic, payload);
|
|
|
+// break;
|
|
|
+// case "report_device_info":
|
|
|
+// handleDeviceReportDeviceInfo(topic, payload);
|
|
|
+// break;
|
|
|
+// case "report_device_param":
|
|
|
+// handleDeviceReportDeviceParam(topic, payload);
|
|
|
+// break;
|
|
|
+// case "dsp_data":
|
|
|
+// handleDeviceDspData(topic, payload);
|
|
|
+// break;
|
|
|
+//
|
|
|
+// case "disconnect":
|
|
|
+// handleDeviceDisconnect(topic, payload);
|
|
|
+// break;
|
|
|
+//// case "cloudpoint":
|
|
|
+//// // todo 目前没有 lna 设备,暂不考虑点云数据改造
|
|
|
+//// handleDeviceCloudPoint(topic, payload);
|
|
|
+//// break;
|
|
|
+//// case "report_falling_event":
|
|
|
+//// // todo 这个 主题 待确认是否废弃
|
|
|
+//// handleDeviceReportFallEvent(topic, payload);
|
|
|
+//// break;
|
|
|
+//// case "report_presence_event":
|
|
|
+//// handleDeviceReportPresenceEvent(topic, payload);
|
|
|
+//// break;
|
|
|
+////
|
|
|
+//// // todo 待确认是否废弃
|
|
|
+//// case "set_debug_param":
|
|
|
+//// handleSetDebugParam(topic, payload);
|
|
|
+//// break;
|
|
|
+//// case "get_debug_param":
|
|
|
+//// handleGetDebugParam(topic, payload);
|
|
|
+//// break;
|
|
|
+// default:
|
|
|
+// log.debug("Unhandled device topic action: {} for topic: {}", action, topic);
|
|
|
+// break;
|
|
|
+// }
|
|
|
+// } else {
|
|
|
+// log.debug("Could not extract action from device topic: {}", topic);
|
|
|
+// }
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("Error handling device message: {}", e.getMessage(), e);
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- case "disconnect":
|
|
|
- handleDeviceDisconnect(topic, payload);
|
|
|
- break;
|
|
|
+ private void handleMessage(String action, String topic, String payload) {
|
|
|
+
|
|
|
+ if (action != null) {
|
|
|
+ switch (action) {
|
|
|
+ case "login":
|
|
|
+ handleDeviceLogin(topic, payload);
|
|
|
+ break;
|
|
|
+ case "keepalive":
|
|
|
+ handleDeviceKeepAlive(topic, payload);
|
|
|
+ break;
|
|
|
+ case "report_device_info":
|
|
|
+ handleDeviceReportDeviceInfo(topic, payload);
|
|
|
+ break;
|
|
|
+ case "report_device_param":
|
|
|
+ handleDeviceReportDeviceParam(topic, payload);
|
|
|
+ break;
|
|
|
+ case "dsp_data":
|
|
|
+ handleDeviceDspData(topic, payload);
|
|
|
+ break;
|
|
|
+
|
|
|
+ case "disconnect":
|
|
|
+ handleDeviceDisconnect(topic, payload);
|
|
|
+ break;
|
|
|
// case "cloudpoint":
|
|
|
// // todo 目前没有 lna 设备,暂不考虑点云数据改造
|
|
|
// handleDeviceCloudPoint(topic, payload);
|
|
@@ -117,16 +195,12 @@ public class DeviceMessageHandler {
|
|
|
// case "get_debug_param":
|
|
|
// handleGetDebugParam(topic, payload);
|
|
|
// break;
|
|
|
- default:
|
|
|
- log.debug("Unhandled device topic action: {} for topic: {}", action, topic);
|
|
|
- break;
|
|
|
- }
|
|
|
- } else {
|
|
|
- log.debug("Could not extract action from device topic: {}", topic);
|
|
|
+ default:
|
|
|
+ log.debug("Unhandled device topic action: {} for topic: {}", action, topic);
|
|
|
+ break;
|
|
|
}
|
|
|
-
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("Error handling device message: {}", e.getMessage(), e);
|
|
|
+ } else {
|
|
|
+ log.debug("Could not extract action from device topic: {}", topic);
|
|
|
}
|
|
|
}
|
|
|
|