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;