|
@@ -13,13 +13,15 @@ import io.vertx.mqtt.messages.MqttPublishMessage;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
|
import java.time.LocalDateTime;
|
|
|
+import java.util.Arrays;
|
|
|
|
|
|
/**
|
|
|
* IoT 设备 MQTT 消息处理器
|
|
|
* <p>
|
|
|
* 参考:
|
|
|
* <p>
|
|
|
- * "<a href="https://help.aliyun.com/zh/iot/user-guide/device-properties-events-and-services?spm=a2c4g.11186623.0.0.97a72915vRck44#section-g4j-5zg-12b">...</a>">
|
|
|
+ * "<a href=
|
|
|
+ * "https://help.aliyun.com/zh/iot/user-guide/device-properties-events-and-services?spm=a2c4g.11186623.0.0.97a72915vRck44#section-g4j-5zg-12b">...</a>">
|
|
|
*/
|
|
|
@Slf4j
|
|
|
public class IotDeviceMqttMessageHandler {
|
|
@@ -35,6 +37,12 @@ public class IotDeviceMqttMessageHandler {
|
|
|
private static final String PROPERTY_POST_TOPIC = "/thing/event/property/post";
|
|
|
private static final String EVENT_POST_TOPIC_PREFIX = "/thing/event/";
|
|
|
private static final String EVENT_POST_TOPIC_SUFFIX = "/post";
|
|
|
+ private static final String REPLY_SUFFIX = "_reply";
|
|
|
+ private static final int SUCCESS_CODE = 200;
|
|
|
+ private static final String SUCCESS_MESSAGE = "success";
|
|
|
+ private static final String PROPERTY_METHOD = "thing.event.property.post";
|
|
|
+ private static final String EVENT_METHOD_PREFIX = "thing.event.";
|
|
|
+ private static final String EVENT_METHOD_SUFFIX = ".post";
|
|
|
|
|
|
private final IotDeviceUpstreamApi deviceUpstreamApi;
|
|
|
private final MqttClient mqttClient;
|
|
@@ -44,18 +52,33 @@ public class IotDeviceMqttMessageHandler {
|
|
|
this.mqttClient = mqttClient;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 处理MQTT消息
|
|
|
+ *
|
|
|
+ * @param message MQTT发布消息
|
|
|
+ */
|
|
|
public void handle(MqttPublishMessage message) {
|
|
|
String topic = message.topicName();
|
|
|
String payload = message.payload().toString();
|
|
|
log.info("[messageHandler][接收到消息][topic: {}][payload: {}]", topic, payload);
|
|
|
|
|
|
try {
|
|
|
+ if (payload == null || payload.isEmpty()) {
|
|
|
+ log.warn("[messageHandler][消息内容为空][topic: {}]", topic);
|
|
|
+ return;
|
|
|
+ }
|
|
|
handleMessage(topic, payload);
|
|
|
} catch (Exception e) {
|
|
|
log.error("[messageHandler][处理消息失败][topic: {}][payload: {}]", topic, payload, e);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 根据主题类型处理消息
|
|
|
+ *
|
|
|
+ * @param topic 主题
|
|
|
+ * @param payload 消息内容
|
|
|
+ */
|
|
|
private void handleMessage(String topic, String payload) {
|
|
|
// 校验前缀
|
|
|
if (!topic.startsWith(SYS_TOPIC_PREFIX)) {
|
|
@@ -88,34 +111,26 @@ public class IotDeviceMqttMessageHandler {
|
|
|
* @param payload 消息内容
|
|
|
*/
|
|
|
private void handlePropertyPost(String topic, String payload) {
|
|
|
- // 解析消息内容
|
|
|
- JSONObject jsonObject = JSONUtil.parseObj(payload);
|
|
|
- String[] topicParts = topic.split("/");
|
|
|
+ try {
|
|
|
+ // 解析消息内容
|
|
|
+ JSONObject jsonObject = JSONUtil.parseObj(payload);
|
|
|
+ String[] topicParts = parseTopic(topic);
|
|
|
+ if (topicParts == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
|
|
|
- // 构建设备属性上报请求对象
|
|
|
- IotDevicePropertyReportReqDTO reportReqDTO = buildPropertyReportDTO(jsonObject, topicParts);
|
|
|
+ // 构建设备属性上报请求对象
|
|
|
+ IotDevicePropertyReportReqDTO reportReqDTO = buildPropertyReportDTO(jsonObject, topicParts);
|
|
|
|
|
|
- // 调用上游 API 处理设备上报数据
|
|
|
- deviceUpstreamApi.reportDeviceProperty(reportReqDTO);
|
|
|
- log.info("[handlePropertyPost][处理设备上行消息成功][topic: {}][reportReqDTO: {}]",
|
|
|
- topic, JSONUtil.toJsonStr(reportReqDTO));
|
|
|
+ // 调用上游 API 处理设备上报数据
|
|
|
+ deviceUpstreamApi.reportDeviceProperty(reportReqDTO);
|
|
|
+ log.info("[handlePropertyPost][处理设备属性上报成功][topic: {}]", topic);
|
|
|
|
|
|
- // 发送响应消息
|
|
|
- String replyTopic = topic + "_reply";
|
|
|
- JSONObject response = new JSONObject()
|
|
|
- .set("id", jsonObject.getStr("id"))
|
|
|
- .set("code", 200)
|
|
|
- .set("data", new JSONObject())
|
|
|
- .set("message", "success")
|
|
|
- .set("method", "thing.event.property.post");
|
|
|
-
|
|
|
- mqttClient.publish(replyTopic,
|
|
|
- Buffer.buffer(response.toString()),
|
|
|
- MqttQoS.AT_LEAST_ONCE,
|
|
|
- false,
|
|
|
- false);
|
|
|
- log.info("[handlePropertyPost][发送响应消息成功][topic: {}][response: {}]",
|
|
|
- replyTopic, response.toString());
|
|
|
+ // 发送响应消息
|
|
|
+ sendResponse(topic, jsonObject, PROPERTY_METHOD, null);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("[handlePropertyPost][处理设备属性上报失败][topic: {}][payload: {}]", topic, payload, e);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -125,35 +140,97 @@ public class IotDeviceMqttMessageHandler {
|
|
|
* @param payload 消息内容
|
|
|
*/
|
|
|
private void handleEventPost(String topic, String payload) {
|
|
|
- // 解析消息内容
|
|
|
- JSONObject jsonObject = JSONUtil.parseObj(payload);
|
|
|
+ try {
|
|
|
+ // 解析消息内容
|
|
|
+ JSONObject jsonObject = JSONUtil.parseObj(payload);
|
|
|
+ String[] topicParts = parseTopic(topic);
|
|
|
+ if (topicParts == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 构建设备事件上报请求对象
|
|
|
+ IotDeviceEventReportReqDTO reportReqDTO = buildEventReportDTO(jsonObject, topicParts);
|
|
|
+
|
|
|
+ // 调用上游 API 处理设备上报数据
|
|
|
+ deviceUpstreamApi.reportDeviceEvent(reportReqDTO);
|
|
|
+ log.info("[handleEventPost][处理设备事件上报成功][topic: {}]", topic);
|
|
|
+
|
|
|
+ // 从 topic 中获取事件标识符
|
|
|
+ String eventIdentifier = getEventIdentifier(topicParts, topic);
|
|
|
+ if (eventIdentifier == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 发送响应消息
|
|
|
+ String method = EVENT_METHOD_PREFIX + eventIdentifier + EVENT_METHOD_SUFFIX;
|
|
|
+ sendResponse(topic, jsonObject, method, null);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("[handleEventPost][处理设备事件上报失败][topic: {}][payload: {}]", topic, payload, e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 解析主题,获取主题各部分
|
|
|
+ *
|
|
|
+ * @param topic 主题
|
|
|
+ * @return 主题各部分数组,如果解析失败返回null
|
|
|
+ */
|
|
|
+ private String[] parseTopic(String topic) {
|
|
|
String[] topicParts = topic.split("/");
|
|
|
+ if (topicParts.length < 7) {
|
|
|
+ log.warn("[parseTopic][主题格式不正确][topic: {}]", topic);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ return topicParts;
|
|
|
+ }
|
|
|
|
|
|
- // 构建设备事件上报请求对象
|
|
|
- IotDeviceEventReportReqDTO reportReqDTO = buildEventReportDTO(jsonObject, topicParts);
|
|
|
+ /**
|
|
|
+ * 从主题部分中获取事件标识符
|
|
|
+ *
|
|
|
+ * @param topicParts 主题各部分
|
|
|
+ * @param topic 原始主题,用于日志
|
|
|
+ * @return 事件标识符,如果获取失败返回null
|
|
|
+ */
|
|
|
+ private String getEventIdentifier(String[] topicParts, String topic) {
|
|
|
+ try {
|
|
|
+ return topicParts[6];
|
|
|
+ } catch (ArrayIndexOutOfBoundsException e) {
|
|
|
+ log.warn("[getEventIdentifier][无法从主题中获取事件标识符][topic: {}][topicParts: {}]",
|
|
|
+ topic, Arrays.toString(topicParts));
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- // 调用上游 API 处理设备上报数据
|
|
|
- deviceUpstreamApi.reportDeviceEvent(reportReqDTO);
|
|
|
- log.info("[handleEventPost][处理设备上行消息成功][topic: {}][reportReqDTO: {}]",
|
|
|
- topic, JSONUtil.toJsonStr(reportReqDTO));
|
|
|
+ /**
|
|
|
+ * 发送响应消息
|
|
|
+ *
|
|
|
+ * @param topic 原始主题
|
|
|
+ * @param jsonObject 原始消息JSON对象
|
|
|
+ * @param method 响应方法
|
|
|
+ * @param customData 自定义数据,可为null
|
|
|
+ */
|
|
|
+ private void sendResponse(String topic, JSONObject jsonObject, String method, JSONObject customData) {
|
|
|
+ String replyTopic = topic + REPLY_SUFFIX;
|
|
|
+ JSONObject data = customData != null ? customData : new JSONObject();
|
|
|
|
|
|
- // 发送响应消息
|
|
|
- String replyTopic = topic + "_reply";
|
|
|
- String eventIdentifier = topicParts[6]; // 从 topic 中获取事件标识符
|
|
|
JSONObject response = new JSONObject()
|
|
|
.set("id", jsonObject.getStr("id"))
|
|
|
- .set("code", 200)
|
|
|
- .set("data", new JSONObject())
|
|
|
- .set("message", "success")
|
|
|
- .set("method", "thing.event." + eventIdentifier + ".post");
|
|
|
-
|
|
|
- mqttClient.publish(replyTopic,
|
|
|
- Buffer.buffer(response.toString()),
|
|
|
- MqttQoS.AT_LEAST_ONCE,
|
|
|
- false,
|
|
|
- false);
|
|
|
- log.info("[handleEventPost][发送响应消息成功][topic: {}][response: {}]",
|
|
|
- replyTopic, response.toString());
|
|
|
+ .set("code", SUCCESS_CODE)
|
|
|
+ .set("data", data)
|
|
|
+ .set("message", SUCCESS_MESSAGE)
|
|
|
+ .set("method", method);
|
|
|
+
|
|
|
+ try {
|
|
|
+ mqttClient.publish(replyTopic,
|
|
|
+ Buffer.buffer(response.toString()),
|
|
|
+ MqttQoS.AT_LEAST_ONCE,
|
|
|
+ false,
|
|
|
+ false);
|
|
|
+ log.info("[sendResponse][发送响应消息成功][topic: {}]", replyTopic);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("[sendResponse][发送响应消息失败][topic: {}][response: {}]",
|
|
|
+ replyTopic, response.toString(), e);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -163,15 +240,15 @@ public class IotDeviceMqttMessageHandler {
|
|
|
* @param topicParts 主题部分
|
|
|
* @return 设备属性上报请求对象
|
|
|
*/
|
|
|
- private IotDevicePropertyReportReqDTO buildPropertyReportDTO(JSONObject jsonObject,
|
|
|
- String[] topicParts) {
|
|
|
- return ((IotDevicePropertyReportReqDTO) new IotDevicePropertyReportReqDTO()
|
|
|
- .setRequestId(jsonObject.getStr("id"))
|
|
|
- .setProcessId(IotPluginCommonUtils.getProcessId())
|
|
|
- .setReportTime(LocalDateTime.now())
|
|
|
- .setProductKey(topicParts[2])
|
|
|
- .setDeviceName(topicParts[3]))
|
|
|
- .setProperties(jsonObject.getJSONObject("params"));
|
|
|
+ private IotDevicePropertyReportReqDTO buildPropertyReportDTO(JSONObject jsonObject, String[] topicParts) {
|
|
|
+ IotDevicePropertyReportReqDTO reportReqDTO = new IotDevicePropertyReportReqDTO();
|
|
|
+ reportReqDTO.setRequestId(jsonObject.getStr("id"));
|
|
|
+ reportReqDTO.setProcessId(IotPluginCommonUtils.getProcessId());
|
|
|
+ reportReqDTO.setReportTime(LocalDateTime.now());
|
|
|
+ reportReqDTO.setProductKey(topicParts[2]);
|
|
|
+ reportReqDTO.setDeviceName(topicParts[3]);
|
|
|
+ reportReqDTO.setProperties(jsonObject.getJSONObject("params"));
|
|
|
+ return reportReqDTO;
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -182,13 +259,14 @@ public class IotDeviceMqttMessageHandler {
|
|
|
* @return 设备事件上报请求对象
|
|
|
*/
|
|
|
private IotDeviceEventReportReqDTO buildEventReportDTO(JSONObject jsonObject, String[] topicParts) {
|
|
|
- return ((IotDeviceEventReportReqDTO) new IotDeviceEventReportReqDTO()
|
|
|
- .setRequestId(jsonObject.getStr("id"))
|
|
|
- .setProcessId(IotPluginCommonUtils.getProcessId())
|
|
|
- .setReportTime(LocalDateTime.now())
|
|
|
- .setProductKey(topicParts[2])
|
|
|
- .setDeviceName(topicParts[3]))
|
|
|
- .setIdentifier(topicParts[4])
|
|
|
- .setParams(jsonObject.getJSONObject("params"));
|
|
|
+ IotDeviceEventReportReqDTO reportReqDTO = new IotDeviceEventReportReqDTO();
|
|
|
+ reportReqDTO.setRequestId(jsonObject.getStr("id"));
|
|
|
+ reportReqDTO.setProcessId(IotPluginCommonUtils.getProcessId());
|
|
|
+ reportReqDTO.setReportTime(LocalDateTime.now());
|
|
|
+ reportReqDTO.setProductKey(topicParts[2]);
|
|
|
+ reportReqDTO.setDeviceName(topicParts[3]);
|
|
|
+ reportReqDTO.setIdentifier(topicParts[6]);
|
|
|
+ reportReqDTO.setParams(jsonObject.getJSONObject("params"));
|
|
|
+ return reportReqDTO;
|
|
|
}
|
|
|
}
|