reactor:【IoT 物联网】增加 http 网络组件,接入 rocketmq 消息总线

This commit is contained in:
YunaiV
2025-05-30 20:47:01 +08:00
parent 385cea8d90
commit 1b59aa9ccb
14 changed files with 177 additions and 174 deletions

View File

@@ -1,5 +1,6 @@
package cn.iocoder.yudao.module.iot.core.mq.message;
import cn.hutool.core.util.IdUtil;
import cn.iocoder.yudao.module.iot.core.enums.IotDeviceMessageIdentifierEnum;
import cn.iocoder.yudao.module.iot.core.enums.IotDeviceMessageTypeEnum;
import lombok.AllArgsConstructor;
@@ -8,6 +9,7 @@ import lombok.Data;
import lombok.NoArgsConstructor;
import java.time.LocalDateTime;
import java.util.Map;
// TODO @芋艿:参考阿里云的物模型,优化 IoT 上下行消息的设计,尽量保持一致(渐进式,不要一口气)!
@@ -20,6 +22,18 @@ import java.time.LocalDateTime;
@Builder
public class IotDeviceMessage {
/**
* 【消息总线】应用的设备消息 Topic由 iot-gateway 发给 iot-biz 进行消费
*/
public static final String MESSAGE_BUS_DEVICE_MESSAGE_TOPIC = "iot_device_message";
/**
* 【消息总线】设备消息 Topic由 iot-biz 发送给 iot-gateway 的某个 “server”(protocol) 进行消费
*
* 其中,%s 就是该“server”(protocol) 的标识
*/
public static final String MESSAGE_BUS_GATEWAY_DEVICE_MESSAGE_TOPIC = MESSAGE_BUS_DEVICE_MESSAGE_TOPIC + "/%s";
/**
* 请求编号
*/
@@ -69,9 +83,42 @@ public class IotDeviceMessage {
*/
private LocalDateTime reportTime;
/**
* 服务编号,该消息由哪个消息发送
*/
private String serverId;
/**
* 租户编号
*/
private Long tenantId;
public IotDeviceMessage ofPropertyReport(Map<String, Object> properties) {
this.setType(IotDeviceMessageTypeEnum.PROPERTY.getType());
this.setIdentifier(IotDeviceMessageIdentifierEnum.PROPERTY_REPORT.getIdentifier());
this.setData(properties);
return this;
}
public static IotDeviceMessage of(String productKey, String deviceName, String deviceKey,
String requestId, LocalDateTime reportTime,
String serverId, Long tenantId) {
if (requestId == null) {
requestId = IdUtil.fastSimpleUUID();
}
if (reportTime == null) {
reportTime = LocalDateTime.now();
}
return IotDeviceMessage.builder()
.requestId(requestId).reportTime(reportTime)
.productKey(productKey).deviceName(deviceName).deviceKey(deviceKey)
.serverId(serverId).tenantId(tenantId).build();
}
// ========== Topic 相关 ==========
public static String getMessageBusGatewayDeviceMessageTopic(String serverId) {
return String.format(MESSAGE_BUS_GATEWAY_DEVICE_MESSAGE_TOPIC, serverId);
}
}

View File

@@ -12,18 +12,6 @@ import lombok.RequiredArgsConstructor;
@RequiredArgsConstructor
public class IotDeviceMessageProducer {
/**
* 【消息总线】应用的设备消息 Topic由 iot-gateway 发给 iot-biz 进行消费
*/
private static final String MESSAGE_BUS_DEVICE_MESSAGE_TOPIC = "iot_device_message";
/**
* 【消息总线】设备消息 Topic由 iot-biz 发送给 iot-gateway 的某个 “server”(protocol) 进行消费
*
* 其中,%s 就是该“server”(protocol) 的标识
*/
private static final String MESSAGE_BUS_GATEWAY_DEVICE_MESSAGE_TOPIC = MESSAGE_BUS_DEVICE_MESSAGE_TOPIC + "/%s";
private final IotMessageBus messageBus;
/**
@@ -32,17 +20,17 @@ public class IotDeviceMessageProducer {
* @param message 设备消息
*/
public void sendDeviceMessage(IotDeviceMessage message) {
messageBus.post(MESSAGE_BUS_DEVICE_MESSAGE_TOPIC, message);
messageBus.post(IotDeviceMessage.MESSAGE_BUS_DEVICE_MESSAGE_TOPIC, message);
}
/**
* 发送网关设备消息
*
* @param server 网关的 server 标识
* @param serverId 网关的 serverId 标识
* @param message 设备消息
*/
public void sendGatewayDeviceMessage(String server, Object message) {
messageBus.post(String.format(MESSAGE_BUS_GATEWAY_DEVICE_MESSAGE_TOPIC, server), message);
public void sendGatewayDeviceMessage(String serverId, Object message) {
messageBus.post(IotDeviceMessage.getMessageBusGatewayDeviceMessageTopic(serverId), message);
}
}

View File

@@ -1,2 +0,0 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
cn.iocoder.yudao.module.iot.core.messagebus.config.IotMessageBusAutoConfiguration

View File

@@ -0,0 +1 @@
cn.iocoder.yudao.module.iot.core.messagebus.config.IotMessageBusAutoConfiguration