|
|
@@ -1,14 +1,14 @@
|
|
|
package cn.iocoder.yudao.module.iot.gateway.protocol.mqtt;
|
|
|
|
|
|
import cn.hutool.json.JSONObject;
|
|
|
-import cn.hutool.json.JSONUtil;
|
|
|
-import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
|
|
|
import cn.iocoder.yudao.module.iot.core.messagebus.core.IotMessageBus;
|
|
|
import cn.iocoder.yudao.module.iot.core.messagebus.core.IotMessageSubscriber;
|
|
|
import cn.iocoder.yudao.module.iot.core.mq.message.IotDeviceMessage;
|
|
|
import cn.iocoder.yudao.module.iot.core.util.IotDeviceMessageUtils;
|
|
|
import cn.iocoder.yudao.module.iot.gateway.enums.IotDeviceTopicEnum;
|
|
|
+import cn.iocoder.yudao.module.iot.gateway.service.device.IotDeviceCacheService;
|
|
|
import jakarta.annotation.PostConstruct;
|
|
|
+import jakarta.annotation.Resource;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
|
@@ -24,6 +24,9 @@ public class IotMqttDownstreamSubscriber implements IotMessageSubscriber<IotDevi
|
|
|
private final IotMqttUpstreamProtocol protocol;
|
|
|
private final IotMessageBus messageBus;
|
|
|
|
|
|
+ @Resource
|
|
|
+ private IotDeviceCacheService deviceCacheService;
|
|
|
+
|
|
|
@PostConstruct
|
|
|
public void init() {
|
|
|
messageBus.register(this);
|
|
|
@@ -45,24 +48,24 @@ public class IotMqttDownstreamSubscriber implements IotMessageSubscriber<IotDevi
|
|
|
log.info("[onMessage][接收到下行消息:{}]", message);
|
|
|
|
|
|
try {
|
|
|
- // 根据消息类型处理不同的下行消息
|
|
|
- String messageType = message.getType();
|
|
|
- switch (messageType) {
|
|
|
- case "property":
|
|
|
- handlePropertyMessage(message);
|
|
|
- break;
|
|
|
- case "service":
|
|
|
- handleServiceMessage(message);
|
|
|
- break;
|
|
|
- case "config":
|
|
|
- handleConfigMessage(message);
|
|
|
- break;
|
|
|
- case "ota":
|
|
|
- handleOtaMessage(message);
|
|
|
- break;
|
|
|
- default:
|
|
|
- log.warn("[onMessage][未知的消息类型:{}]", messageType);
|
|
|
- break;
|
|
|
+ // 根据消息方法处理不同的下行消息
|
|
|
+ String method = message.getMethod();
|
|
|
+ if (method == null) {
|
|
|
+ log.warn("[onMessage][消息方法为空]");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (method.startsWith("thing.service.property.")) {
|
|
|
+ handlePropertyMessage(message);
|
|
|
+ } else if (method.startsWith("thing.service.") && !method.contains("property") && !method.contains("config")
|
|
|
+ && !method.contains("ota")) {
|
|
|
+ handleServiceMessage(message);
|
|
|
+ } else if (method.startsWith("thing.service.config.")) {
|
|
|
+ handleConfigMessage(message);
|
|
|
+ } else if (method.startsWith("thing.service.ota.")) {
|
|
|
+ handleOtaMessage(message);
|
|
|
+ } else {
|
|
|
+ log.warn("[onMessage][未知的消息方法:{}]", method);
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
log.error("[onMessage][处理下行消息失败:{}]", message, e);
|
|
|
@@ -75,24 +78,32 @@ public class IotMqttDownstreamSubscriber implements IotMessageSubscriber<IotDevi
|
|
|
* @param message 设备消息
|
|
|
*/
|
|
|
private void handlePropertyMessage(IotDeviceMessage message) {
|
|
|
- String identifier = message.getIdentifier();
|
|
|
- String productKey = message.getProductKey();
|
|
|
- String deviceName = message.getDeviceName();
|
|
|
+ String method = message.getMethod();
|
|
|
|
|
|
- if ("set".equals(identifier)) {
|
|
|
+ // 通过 deviceId 获取设备信息
|
|
|
+ IotDeviceCacheService.DeviceInfo deviceInfo = deviceCacheService.getDeviceInfoById(message.getDeviceId());
|
|
|
+ if (deviceInfo == null) {
|
|
|
+ log.warn("[handlePropertyMessage][设备信息不存在][deviceId: {}]", message.getDeviceId());
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ String productKey = deviceInfo.getProductKey();
|
|
|
+ String deviceName = deviceInfo.getDeviceName();
|
|
|
+
|
|
|
+ if ("thing.service.property.set".equals(method)) {
|
|
|
// 属性设置
|
|
|
String topic = IotDeviceTopicEnum.buildPropertySetTopic(productKey, deviceName);
|
|
|
- JSONObject payload = buildDownstreamPayload(message, "thing.service.property.set");
|
|
|
+ JSONObject payload = buildDownstreamPayload(message, method);
|
|
|
protocol.publishMessage(topic, payload.toString());
|
|
|
log.info("[handlePropertyMessage][发送属性设置消息][topic: {}]", topic);
|
|
|
- } else if ("get".equals(identifier)) {
|
|
|
+ } else if ("thing.service.property.get".equals(method)) {
|
|
|
// 属性获取
|
|
|
String topic = IotDeviceTopicEnum.buildPropertyGetTopic(productKey, deviceName);
|
|
|
- JSONObject payload = buildDownstreamPayload(message, "thing.service.property.get");
|
|
|
+ JSONObject payload = buildDownstreamPayload(message, method);
|
|
|
protocol.publishMessage(topic, payload.toString());
|
|
|
log.info("[handlePropertyMessage][发送属性获取消息][topic: {}]", topic);
|
|
|
} else {
|
|
|
- log.warn("[handlePropertyMessage][未知的属性操作:{}]", identifier);
|
|
|
+ log.warn("[handlePropertyMessage][未知的属性操作:{}]", method);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -102,12 +113,23 @@ public class IotMqttDownstreamSubscriber implements IotMessageSubscriber<IotDevi
|
|
|
* @param message 设备消息
|
|
|
*/
|
|
|
private void handleServiceMessage(IotDeviceMessage message) {
|
|
|
- String identifier = message.getIdentifier();
|
|
|
- String productKey = message.getProductKey();
|
|
|
- String deviceName = message.getDeviceName();
|
|
|
+ String method = message.getMethod();
|
|
|
+
|
|
|
+ // 通过 deviceId 获取设备信息
|
|
|
+ IotDeviceCacheService.DeviceInfo deviceInfo = deviceCacheService.getDeviceInfoById(message.getDeviceId());
|
|
|
+ if (deviceInfo == null) {
|
|
|
+ log.warn("[handleServiceMessage][设备信息不存在][deviceId: {}]", message.getDeviceId());
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ String productKey = deviceInfo.getProductKey();
|
|
|
+ String deviceName = deviceInfo.getDeviceName();
|
|
|
|
|
|
- String topic = IotDeviceTopicEnum.buildServiceTopic(productKey, deviceName, identifier);
|
|
|
- JSONObject payload = buildDownstreamPayload(message, "thing.service." + identifier);
|
|
|
+ // 从方法中提取服务标识符
|
|
|
+ String serviceIdentifier = method.substring("thing.service.".length());
|
|
|
+
|
|
|
+ String topic = IotDeviceTopicEnum.buildServiceTopic(productKey, deviceName, serviceIdentifier);
|
|
|
+ JSONObject payload = buildDownstreamPayload(message, method);
|
|
|
protocol.publishMessage(topic, payload.toString());
|
|
|
log.info("[handleServiceMessage][发送服务调用消息][topic: {}]", topic);
|
|
|
}
|
|
|
@@ -118,11 +140,18 @@ public class IotMqttDownstreamSubscriber implements IotMessageSubscriber<IotDevi
|
|
|
* @param message 设备消息
|
|
|
*/
|
|
|
private void handleConfigMessage(IotDeviceMessage message) {
|
|
|
- String productKey = message.getProductKey();
|
|
|
- String deviceName = message.getDeviceName();
|
|
|
+ // 通过 deviceId 获取设备信息
|
|
|
+ IotDeviceCacheService.DeviceInfo deviceInfo = deviceCacheService.getDeviceInfoById(message.getDeviceId());
|
|
|
+ if (deviceInfo == null) {
|
|
|
+ log.warn("[handleConfigMessage][设备信息不存在][deviceId: {}]", message.getDeviceId());
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ String productKey = deviceInfo.getProductKey();
|
|
|
+ String deviceName = deviceInfo.getDeviceName();
|
|
|
|
|
|
String topic = IotDeviceTopicEnum.buildConfigSetTopic(productKey, deviceName);
|
|
|
- JSONObject payload = buildDownstreamPayload(message, "thing.service.config.set");
|
|
|
+ JSONObject payload = buildDownstreamPayload(message, message.getMethod());
|
|
|
protocol.publishMessage(topic, payload.toString());
|
|
|
log.info("[handleConfigMessage][发送配置设置消息][topic: {}]", topic);
|
|
|
}
|
|
|
@@ -133,11 +162,18 @@ public class IotMqttDownstreamSubscriber implements IotMessageSubscriber<IotDevi
|
|
|
* @param message 设备消息
|
|
|
*/
|
|
|
private void handleOtaMessage(IotDeviceMessage message) {
|
|
|
- String productKey = message.getProductKey();
|
|
|
- String deviceName = message.getDeviceName();
|
|
|
+ // 通过 deviceId 获取设备信息
|
|
|
+ IotDeviceCacheService.DeviceInfo deviceInfo = deviceCacheService.getDeviceInfoById(message.getDeviceId());
|
|
|
+ if (deviceInfo == null) {
|
|
|
+ log.warn("[handleOtaMessage][设备信息不存在][deviceId: {}]", message.getDeviceId());
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ String productKey = deviceInfo.getProductKey();
|
|
|
+ String deviceName = deviceInfo.getDeviceName();
|
|
|
|
|
|
String topic = IotDeviceTopicEnum.buildOtaUpgradeTopic(productKey, deviceName);
|
|
|
- JSONObject payload = buildDownstreamPayload(message, "thing.service.ota.upgrade");
|
|
|
+ JSONObject payload = buildDownstreamPayload(message, message.getMethod());
|
|
|
protocol.publishMessage(topic, payload.toString());
|
|
|
log.info("[handleOtaMessage][发送 OTA 升级消息][topic: {}]", topic);
|
|
|
}
|
|
|
@@ -151,7 +187,7 @@ public class IotMqttDownstreamSubscriber implements IotMessageSubscriber<IotDevi
|
|
|
*/
|
|
|
private JSONObject buildDownstreamPayload(IotDeviceMessage message, String method) {
|
|
|
JSONObject payload = new JSONObject();
|
|
|
- payload.set("id", message.getMessageId());
|
|
|
+ payload.set("id", message.getId()); // 使用正确的消息ID字段
|
|
|
payload.set("version", "1.0");
|
|
|
payload.set("method", method);
|
|
|
payload.set("params", message.getData());
|