trphoenix
2025-07-15 3191826fdef076c67ed56b0ff5f821f6033bf45c
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
const messageId = "sts_" + Date.now();
const timestamp = Date.now();
//nameSpace:命名空间,需要改改为正确的命名空间代码
const nameSpace = "Environment"
const clientCode = "Data-It_XzOffice";
//此处需改为正确的设备实体唯一ID
const deviceId = "sensor.miaomiaoc_cn_blt_3_11a1sbr5k5o01_t2_temperature_p_2_1";
const deviceType = "sensor";
 
// 计算设备运行时间(从第一次启动开始)
if (!context.get("startTime")) {
    context.set("startTime", timestamp);
}
const startTime = context.get("startTime");
const uptime = timestamp - startTime;
 
// 按照胜透物联网通信协议生成状态数据
const statusMessage = {
    "messageId": messageId,
    "timestamp": timestamp,
    "clientCode": clientCode,
    "deviceId": deviceId,
    "deviceType": deviceType,
    "version": "1.0",
    "data": {
        "status": {
            "online": true,
            "lastHeartbeat": timestamp,
            "uptime": uptime
        }
    }
};
 
// 设置MQTT发布主题
msg.topic = `${nameSpace}/${deviceType}/${deviceId}/status`;
msg.payload = JSON.stringify(statusMessage);
msg.qos = 0;
 
node.log("设备状态心跳已发送,发布到主题: " + msg.topic);
 
return msg;