设备状态推送

最近更新时间:2020-01-14 17:40:23

1. 接口描述

接口请求域名: iot.cloud.tencent.com/ws/explorer
注册设备监听接口调用成功后,通过监听 WebSocket 的 OnMessage 获取设备状态与属性值的实时推送,若获取到的 event.data.pushtrue,则代表该条消息为设备状态变更的主动推送。

2. 输出参数

名称 类型 描述
push Boolean 该条消息为设备状态变更的主动推送
action String 固定为 DeviceChange,代表设备状态变更
params String 返回设备信息,其中 Payload 需用 base64 解码

3. 示例

输入示例

ws.onmessage = (event) => {
    let data;
    try {
        data = JSON.parse(event.data);
    } catch (e) {
        console.log(`onMessage parse event.data error: ${event.data}`);
        return;
    }

    if (data.push) {
        this.emit('push', data);
    } else if (typeof data.reqId !== 'undefined' && this.requestHandlerMap.has(data.reqId)) {
        this.requestHandlerMap.get(data.reqId)(null, data);
    }
};

输出示例:成功

{
    "push": true,
    "action": "DeviceChange",
    "params": "{\"Time\":\"2019-03-28T15:17:35+08:00\",\"Type\":\"Event\",\"SubType\":\"Report\",\"Topic\":\"$thing/up/event/LQCSPUNOZ9/testDerive1\",\"Payload\":\"eyJtZXRob2QiOiJldmVudF9wb3N0IiwiY2xpZW50VG9rZW4iOiIxMjMiLCJ2ZXJzaW9uIjoiMS4wIiwiZXZlbnRJZCI6InN0YXR1c19yZXBvcnQiLCJ0aW1lc3RhbXAiOjAsInBhcmFtcyI6eyJzdGF0dXMiOjAsIm1lc3NhZ2UiOiJEZXZpY2VPSyJ9fQ==\",\"Seq\":38,\"DeviceId\":\"LQCSPUNOZ9/testDerive\"}"
}

输出示例:失败

{
    "error":"",
    "error_message":"",
    "data":{
        "deviceIds":[]
    },
    "reqId":"req0001"
}

Type 说明

Type 类型 描述
StatusMessage 状态变化:上线、下线
Shadow 影子消息
Template 物模型消息
Event 事件消息
Property 属性消息
UserDefined 用户自定义

SubTpe 说明

SubType 类型 描述
Report 设备上报
Push 服务端推送到设备
Online 设备上线
Offline 设备下线
说明:

关于不同消息类型的 params 更多的说明,params json 被解析后,示例如下。

Template 消息例子

消息例子

{
    "Time": "2019-04-11T11:25:10+08:00",
    "Type": "Template",
    "SubType": "Report",
    "Topic": "",
    "Payload": "eyJ0eXBlIjoidXBkYXRlIiwic3RhdGUiOnsicmVwb3J0ZWQiOnsidGVzdF9udW1fc3RhcnQiOjcsImxpZ2h0X3N3aXRjaCI6MSwiY29sb3IiOjEsImJyaWdodG5lc3MiOjQxfX0sInZlcnNpb24iOjE1NSwiY2xpZW50VG9rZW4iOiJBUEktQ29udHJvbERldmljZURhdGEtMTU1NDk1MzExMCJ9",
    "Seq": 2,
    "DeviceId": "US4CJ11DIK/LIAO"
}

Payload base64 解码后

{
    "type": "update",
    "state": {
        "reported": {
            "test_num_start": 7,
            "light_switch": 1,
            "color": 1,
            "brightness": 41
        }
    },
    "version": 155,
    "clientToken": "API-ControlDeviceData-1554953110"
}

Event 消息例子

消息例子

{
    "Time": "2019-04-11T20:42:36+08:00",
    "Type": "Event",
    "SubType": "Report",
    "Topic": "$thing/up/event/US4CJ11DIK/LIAO",
    "Payload": "ewogICAgICAgICJtZXRob2QiOiAiZXZlbnRfcG9zdCIsCiAgICAgICAgInZlcnNpb24iOiAiMS4wIiwKICAgICAgICAiZXZlbnRJZCI6ICJsb3dfdm9sdGFnZSIsCiAgICAgICAgInBhcmFtcyI6IHsKICAgICAgICAgICAgInZvbHRhZ2UiOiAyLjI3ODA4MQogICAgICAgIH0sCiAgICAgICAgImNsaWVudFRva2VuIjogImNsaWVudFRva2VuLTU1OSJ9",
    "Seq": 300,
    "DeviceId": "US4CJ11DIK/LIAO"
}

Payload base64 解码后

{
    "method": "event_post",
    "version": "1.0",
    "eventId": "low_voltage",
    "params": {
        "voltage": 2.278081
    },
    "clientToken": "clientToken-559"
}

属性消息例子

消息例子

{
    "Time": "2019-10-30T10:41:00+08:00",
    "Type": "Property",
    "SubType": "Report",
    "Topic": "$thing/up/property/WTJTZX0MF2/device_property001",
    "Payload": "eyJ0aW1lc3RhbXAiOiAwLCAicGFyYW1zIjogeyJicmlnaHRuZXNzIjogNTl9LCAibWV0aG9kIjogInJlcG9ydCIsICJjbGllbnRUb2tlbiI6ICJ0bTIwMTkxMDMwMTA0MDUzXzEifQ==",
    "Seq": 6,
    "DeviceId": "WTJTZX0MF2/device_property001"
}

Payload base64 解码后

{
    "method": "report",
    "params": {
        "timestamp": 0,
        "params": {
            "brightness": 59
        },
        "method": "report",
        "clientToken": "tm20191030104053_1"
    }
}
目录