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