2026-06-26 20b96473f2520590a0dca6b775b81e3ea06a77a0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package cn.iocoder.yudao.module.iot.core.mq.message;
 
import cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants;
import cn.iocoder.yudao.module.iot.core.enums.IotDeviceMessageMethodEnum;
import cn.iocoder.yudao.module.iot.core.enums.device.IotDeviceStateEnum;
import cn.iocoder.yudao.module.iot.core.topic.state.IotDeviceStateUpdateReqDTO;
import cn.iocoder.yudao.module.iot.core.util.IotDeviceMessageUtils;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
 
import java.time.LocalDateTime;
 
/**
 * IoT 设备消息
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@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";
 
    /**
     * 消息编号
     *
     * 由后端生成,通过 {@link IotDeviceMessageUtils#generateMessageId()}
     */
    private String id;
    /**
     * 上报时间
     *
     * 由后端生成,当前时间
     */
    private LocalDateTime reportTime;
 
    /**
     * 设备编号
     */
    private Long deviceId;
    /**
     * 租户编号
     */
    private Long tenantId;
 
    /**
     * 服务编号,该消息由哪个 server 发送
     */
    private String serverId;
 
    // ========== serialize(序列化)相关字段 ==========
 
    /**
     * 请求编号
     *
     * 由设备生成,对应阿里云 IoT 的 Alink 协议中的 id、华为云 IoTDA 协议的 request_id
     */
    private String requestId;
    /**
     * 请求方法
     *
     * 枚举 {@link IotDeviceMessageMethodEnum}
     * 例如说:thing.property.post 属性上报
     */
    private String method;
    /**
     * 请求参数
     *
     * 例如说:属性上报的 properties、事件上报的 params
     */
    private Object params;
    /**
     * 响应结果
     */
    private Object data;
    /**
     * 响应错误码
     */
    private Integer code;
    /**
     * 返回结果信息
     */
    private String msg;
 
    // ========== 基础方法:只传递"serialize(序列化)相关字段" ==========
 
    public static IotDeviceMessage requestOf(String method) {
        return requestOf(null, method, null);
    }
 
    public static IotDeviceMessage requestOf(String method, Object params) {
        return requestOf(null, method, params);
    }
 
    public static IotDeviceMessage requestOf(String requestId, String method, Object params) {
        return of(requestId, method, params, null, null, null);
    }
 
    /**
     * 创建设备请求消息(包含设备信息)
     *
     * @param deviceId 设备编号
     * @param tenantId 租户编号
     * @param serverId 服务标识
     * @param method   消息方法
     * @param params   消息参数
     * @return 消息对象
     */
    public static IotDeviceMessage requestOf(Long deviceId, Long tenantId, String serverId,
                                             String method, Object params) {
        IotDeviceMessage message = of(null, method, params, null, null, null);
        return message.setId(IotDeviceMessageUtils.generateMessageId())
                .setDeviceId(deviceId).setTenantId(tenantId).setServerId(serverId);
    }
 
    public static IotDeviceMessage replyOf(String requestId, String method,
                                           Object data, Integer code, String msg) {
        if (code == null) {
            code = GlobalErrorCodeConstants.SUCCESS.getCode();
            msg = GlobalErrorCodeConstants.SUCCESS.getMsg();
        }
        return of(requestId, method, null, data, code, msg);
    }
 
    public static IotDeviceMessage of(String requestId, String method,
                                      Object params, Object data, Integer code, String msg) {
        // 通用参数
        IotDeviceMessage message = new IotDeviceMessage()
                .setId(IotDeviceMessageUtils.generateMessageId()).setReportTime(LocalDateTime.now());
        // 当前参数
        message.setRequestId(requestId).setMethod(method).setParams(params)
                .setData(data).setCode(code).setMsg(msg);
        return message;
    }
 
    // ========== 核心方法:在 of 基础方法之上,添加对应 method ==========
 
    public static IotDeviceMessage buildStateUpdateOnline() {
        return requestOf(IotDeviceMessageMethodEnum.STATE_UPDATE.getMethod(),
                new IotDeviceStateUpdateReqDTO(IotDeviceStateEnum.ONLINE.getState()));
    }
 
    public static IotDeviceMessage buildStateOffline() {
        return requestOf(IotDeviceMessageMethodEnum.STATE_UPDATE.getMethod(),
                new IotDeviceStateUpdateReqDTO(IotDeviceStateEnum.OFFLINE.getState()));
    }
 
}