@@ -1,7 +1,6 @@
package cn.iocoder.yudao.module.iot.core.mq.message ;
import cn.iocoder.yudao.module.iot.core.enums.IotDeviceMessageIdentifier Enum ;
import cn.iocoder.yudao.module.iot.core.enums.IotDeviceMessageTypeEnum ;
import cn.iocoder.yudao.module.iot.core.enums.IotDeviceMessageMethod Enum ;
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 ;
}
}