reactor:【IoT 物联网】实现 alink 的 IotAlinkDeviceMessageCodec,并尝试接入 http 协议

This commit is contained in:
YunaiV
2025-06-09 21:44:47 +08:00
parent f58cf282dd
commit 120029bb17
14 changed files with 392 additions and 137 deletions

View File

@@ -0,0 +1,25 @@
package cn.iocoder.yudao.module.iot.core.enums;
import lombok.AllArgsConstructor;
import lombok.Getter;
/**
* IoT 设备消息的方法枚举
*
* @author haohao
*/
@Getter
@AllArgsConstructor
public enum IotDeviceMessageMethodEnum {
// ========== 设备状态 ==========
STATE_ONLINE("thing.state.online"),
STATE_OFFLINE("thing.state.offline"),
;
private final String method;
}

View File

@@ -1,7 +1,6 @@
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.enums.IotDeviceMessageMethodEnum;
import cn.iocoder.yudao.module.iot.core.util.IotDeviceMessageUtils;
import lombok.AllArgsConstructor;
import lombok.Builder;
@@ -9,7 +8,6 @@ import lombok.Data;
import lombok.NoArgsConstructor;
import java.time.LocalDateTime;
import java.util.Map;
/**
* IoT 设备消息
@@ -32,109 +30,126 @@ public class IotDeviceMessage {
*/
public static final String MESSAGE_BUS_GATEWAY_DEVICE_MESSAGE_TOPIC = MESSAGE_BUS_DEVICE_MESSAGE_TOPIC + "_%s";
// TODO @芋艿thingsboard 对应 id全部由后端生成由于追溯是不是调整下
/**
* 消息编号
*/
private String messageId;
// TODO @芋艿thingsboard 是使用 deviceId
/**
* 设备信息
*/
private String productKey;
/**
* 设备名称
*/
private String deviceName;
// TODO @芋艿thingsboard 只定义了 type相当于 type + identifier 结合TbMsgType
/**
* 消息类型
*
* 枚举 {@link IotDeviceMessageTypeEnum}
* 由后端生成,通过 {@link IotDeviceMessageUtils#generateMessageId()}
*/
private String type;
private String id;
/**
* 标识符
* 上报时间
*
* 枚举 {@link IotDeviceMessageIdentifierEnum}
* 由后端生成,当前时间
*/
private String identifier;
private LocalDateTime reportTime;
// TODO @芋艿thingsboard 只有 data 字段,没有 code 字段;
// TODO @芋艿:要不提前序列化成字符串???类似 thingsboard 的 data 字段
// ========== codec编解码字段 ==========
/**
* 请求编号
*
* 由设备生成,对应阿里云 IoT 的 Alink 协议中的 id、华为云 IoTDA 协议的 request_id
*/
private String requestId;
/**
* 请求方法
*
* 枚举 {@link IotDeviceMessageMethodEnum}
* 例如说thing.property.report 属性上报
*/
private String method;
/**
* 请求参数
*
* 例如说:属性上报的 properties、事件上报的 params
*/
private Object data;
// TODO @芋艿:可能会去掉
private Object params;
/**
* 响应
*
* 目前只有 server 下行消息给 device 设备时,才会有响应码
* 响应结果
*/
private Object data;
/**
* 响应错误码
*/
private Integer code;
// ========== 后端字段 ==========
/**
* 上报时间
* 设备编号
*/
private LocalDateTime reportTime;
private Long deviceId;
/**
* 租户编号
*/
private Long tenantId;
/**
* 服务编号,该消息由哪个 server 服务进行消费
*/
private String serverId;
public IotDeviceMessage ofPropertyReport(Map<String, Object> properties) {
this.setType(IotDeviceMessageTypeEnum.PROPERTY.getType());
this.setIdentifier(IotDeviceMessageIdentifierEnum.PROPERTY_REPORT.getIdentifier());
this.setData(properties);
return this;
// public IotDeviceMessage ofPropertyReport(Map<String, Object> properties) {
// this.setType(IotDeviceMessageTypeEnum.PROPERTY.getType());
// this.setIdentifier(IotDeviceMessageIdentifierEnum.PROPERTY_REPORT.getIdentifier());
// this.setData(properties);
// return this;
// }
//
// public IotDeviceMessage ofPropertySet(Map<String, Object> properties) {
// this.setType(IotDeviceMessageTypeEnum.PROPERTY.getType());
// this.setIdentifier(IotDeviceMessageIdentifierEnum.PROPERTY_SET.getIdentifier());
// this.setData(properties);
// 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);
// }
//
// public static IotDeviceMessage of(String productKey, String deviceName,
// String serverId) {
// return of(productKey, deviceName,
// null, serverId);
// }
//
// public static IotDeviceMessage of(String productKey, String deviceName,
// LocalDateTime reportTime, String serverId) {
// if (reportTime == null) {
// reportTime = LocalDateTime.now();
// }
// String messageId = IotDeviceMessageUtils.generateMessageId();
// return IotDeviceMessage.builder()
// .messageId(messageId).reportTime(reportTime)
// .productKey(productKey).deviceName(deviceName)
// .serverId(serverId).build();
// }
public static IotDeviceMessage of(String requestId, String method, Object params) {
return of(requestId, method, params, null, null);
}
public IotDeviceMessage ofPropertySet(Map<String, Object> properties) {
this.setType(IotDeviceMessageTypeEnum.PROPERTY.getType());
this.setIdentifier(IotDeviceMessageIdentifierEnum.PROPERTY_SET.getIdentifier());
this.setData(properties);
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);
}
public static IotDeviceMessage of(String productKey, String deviceName,
String serverId) {
return of(productKey, deviceName,
null, serverId);
}
public static IotDeviceMessage of(String productKey, String deviceName,
LocalDateTime reportTime, String serverId) {
if (reportTime == null) {
reportTime = LocalDateTime.now();
}
String messageId = IotDeviceMessageUtils.generateMessageId();
return IotDeviceMessage.builder()
.messageId(messageId).reportTime(reportTime)
.productKey(productKey).deviceName(deviceName)
.serverId(serverId).build();
public static IotDeviceMessage of(String requestId, String method,
Object params, Object data, Integer code) {
// 通用参数
IotDeviceMessage message = new IotDeviceMessage()
.setId(IotDeviceMessageUtils.generateMessageId()).setReportTime(LocalDateTime.now());
// 当前参数
message.setRequestId(requestId).setMethod(method).setParams(params).setData(data).setCode(code);
return message;
}
}

View File

@@ -30,7 +30,7 @@ public class IotDeviceMessageProducer {
* @param serverId 网关的 serverId 标识
* @param message 设备消息
*/
public void sendGatewayDeviceMessage(String serverId, Object message) {
public void sendDeviceMessageToGateway(String serverId, IotDeviceMessage message) {
messageBus.post(IotDeviceMessageUtils.buildMessageBusGatewayDeviceMessageTopic(serverId), message);
}

View File

@@ -18,6 +18,7 @@ public class IotDeviceMessageUtils {
return IdUtil.fastSimpleUUID();
}
// TODO @芋艿:需要优化下;
/**
* 是否是上行消息:由设备发送
*