feat:【IoT 物联网】消息下行时,增加 serverId 的接入
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
package cn.iocoder.yudao.module.iot.core.enums;
|
||||
|
||||
import cn.iocoder.yudao.framework.common.core.ArrayValuable;
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* IoT 设备状态枚举
|
||||
*
|
||||
* @author haohao
|
||||
*/
|
||||
@RequiredArgsConstructor
|
||||
@Getter
|
||||
public enum IotDeviceStateEnum implements ArrayValuable<Integer> {
|
||||
|
||||
INACTIVE(0, "未激活"),
|
||||
ONLINE(1, "在线"),
|
||||
OFFLINE(2, "离线");
|
||||
|
||||
public static final Integer[] ARRAYS = Arrays.stream(values()).map(IotDeviceStateEnum::getState).toArray(Integer[]::new);
|
||||
|
||||
/**
|
||||
* 状态
|
||||
*/
|
||||
private final Integer state;
|
||||
/**
|
||||
* 状态名
|
||||
*/
|
||||
private final String name;
|
||||
|
||||
@Override
|
||||
public Integer[] array() {
|
||||
return ARRAYS;
|
||||
}
|
||||
|
||||
public static boolean isOnline(Integer state) {
|
||||
return ONLINE.getState().equals(state);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -2,7 +2,7 @@ package cn.iocoder.yudao.module.iot.core.mq.message;
|
||||
|
||||
import cn.iocoder.yudao.module.iot.core.enums.IotDeviceMessageIdentifierEnum;
|
||||
import cn.iocoder.yudao.module.iot.core.enums.IotDeviceMessageTypeEnum;
|
||||
import cn.iocoder.yudao.module.iot.core.util.IotCoreUtils;
|
||||
import cn.iocoder.yudao.module.iot.core.util.IotDeviceMessageUtils;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
@@ -79,7 +79,7 @@ public class IotDeviceMessage {
|
||||
private LocalDateTime reportTime;
|
||||
|
||||
/**
|
||||
* 服务编号,该消息由哪个消息发送
|
||||
* 服务编号,该消息由哪个 server 服务进行消费
|
||||
*/
|
||||
private String serverId;
|
||||
|
||||
@@ -97,6 +97,18 @@ public class IotDeviceMessage {
|
||||
return this;
|
||||
}
|
||||
|
||||
public IotDeviceMessage ofStateOnline() {
|
||||
this.setType(IotDeviceMessageTypeEnum.STATE.getType());
|
||||
this.setIdentifier(IotDeviceMessageIdentifierEnum.STATE_ONLINE.getIdentifier());
|
||||
return this;
|
||||
}
|
||||
|
||||
public IotDeviceMessage ofStateOffline() {
|
||||
this.setType(IotDeviceMessageTypeEnum.STATE.getType());
|
||||
this.setIdentifier(IotDeviceMessageIdentifierEnum.STATE_OFFLINE.getIdentifier());
|
||||
return this;
|
||||
}
|
||||
|
||||
public static IotDeviceMessage of(String productKey, String deviceName) {
|
||||
return of(productKey, deviceName,
|
||||
null, null);
|
||||
@@ -113,17 +125,11 @@ public class IotDeviceMessage {
|
||||
if (reportTime == null) {
|
||||
reportTime = LocalDateTime.now();
|
||||
}
|
||||
String messageId = IotCoreUtils.generateMessageId();
|
||||
String messageId = IotDeviceMessageUtils.generateMessageId();
|
||||
return IotDeviceMessage.builder()
|
||||
.messageId(messageId).reportTime(reportTime)
|
||||
.productKey(productKey).deviceName(deviceName)
|
||||
.serverId(serverId).build();
|
||||
}
|
||||
|
||||
// ========== Topic 相关 ==========
|
||||
|
||||
public static String buildMessageBusGatewayDeviceMessageTopic(String serverId) {
|
||||
return String.format(MESSAGE_BUS_GATEWAY_DEVICE_MESSAGE_TOPIC, serverId);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package cn.iocoder.yudao.module.iot.core.mq.producer;
|
||||
|
||||
import cn.iocoder.yudao.module.iot.core.messagebus.core.IotMessageBus;
|
||||
import cn.iocoder.yudao.module.iot.core.mq.message.IotDeviceMessage;
|
||||
import cn.iocoder.yudao.module.iot.core.util.IotDeviceMessageUtils;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
/**
|
||||
@@ -30,7 +31,7 @@ public class IotDeviceMessageProducer {
|
||||
* @param message 设备消息
|
||||
*/
|
||||
public void sendGatewayDeviceMessage(String serverId, Object message) {
|
||||
messageBus.post(IotDeviceMessage.buildMessageBusGatewayDeviceMessageTopic(serverId), message);
|
||||
messageBus.post(IotDeviceMessageUtils.buildMessageBusGatewayDeviceMessageTopic(serverId), message);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,29 +0,0 @@
|
||||
package cn.iocoder.yudao.module.iot.core.util;
|
||||
|
||||
import cn.hutool.core.util.IdUtil;
|
||||
import cn.hutool.system.SystemUtil;
|
||||
|
||||
/**
|
||||
* IoT 核心模块的工具类
|
||||
*
|
||||
* @author 芋道源码
|
||||
*/
|
||||
public class IotCoreUtils {
|
||||
|
||||
/**
|
||||
* 生成服务器编号
|
||||
*
|
||||
* @param serverPort 服务器端口
|
||||
* @return 服务器编号
|
||||
*/
|
||||
public static String generateServerId(Integer serverPort) {
|
||||
String serverId = String.format("%s.%d", SystemUtil.getHostInfo().getAddress(), serverPort);
|
||||
// 避免一些场景无法使用 . 符号,例如说 RocketMQ Topic
|
||||
return serverId.replaceAll("\\.", "_");
|
||||
}
|
||||
|
||||
public static String generateMessageId() {
|
||||
return IdUtil.fastSimpleUUID();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package cn.iocoder.yudao.module.iot.core.util;
|
||||
|
||||
import cn.hutool.core.util.IdUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.system.SystemUtil;
|
||||
import cn.iocoder.yudao.module.iot.core.mq.message.IotDeviceMessage;
|
||||
|
||||
/**
|
||||
* IoT 设备消息的工具类
|
||||
*
|
||||
* @author 芋道源码
|
||||
*/
|
||||
public class IotDeviceMessageUtils {
|
||||
|
||||
// ========== Message 相关 ==========
|
||||
|
||||
public static String generateMessageId() {
|
||||
return IdUtil.fastSimpleUUID();
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否是上行消息:由设备发送
|
||||
*
|
||||
* @param message 消息
|
||||
* @return 是否
|
||||
*/
|
||||
public static boolean isUpstreamMessage(IotDeviceMessage message) {
|
||||
return StrUtil.isNotEmpty(message.getServerId());
|
||||
}
|
||||
|
||||
// ========== Topic 相关 ==========
|
||||
|
||||
public static String buildMessageBusGatewayDeviceMessageTopic(String serverId) {
|
||||
return String.format(IotDeviceMessage.MESSAGE_BUS_GATEWAY_DEVICE_MESSAGE_TOPIC, serverId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成服务器编号
|
||||
*
|
||||
* @param serverPort 服务器端口
|
||||
* @return 服务器编号
|
||||
*/
|
||||
public static String generateServerId(Integer serverPort) {
|
||||
String serverId = String.format("%s.%d", SystemUtil.getHostInfo().getAddress(), serverPort);
|
||||
// 避免一些场景无法使用 . 符号,例如说 RocketMQ Topic
|
||||
return serverId.replaceAll("\\.", "_");
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user