应用端开发指南

设备状态推送

最近更新时间:2021-09-09 14:42:29

1. 接口描述

接口请求域名:iot.cloud.tencent.com/ws/explorer

设备状态推送用于在小程序或 App 实时获取用户绑定设备的上下线状态、设备上报的属性与事件以及设备行为执行结果。需要成功调用注册设备监听接口,通过监听 WebSocket 的 OnMessage 获取设备状态与属性值的实时推送,若获取到的 event.data.pushtrue,则代表该条消息为设备状态变更的主动推送。

2. 输出参数

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

3. 示例

示例1

输入示例

  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"
  }

4.消息类型说明

Type 类型说明

Type类型 描述
StatusChange 设备状态变化消息,包括设备上线与下线消息通知
Shadow 影子消息
Property 属性消息
Template 物模型消息
Event 事件消息
Action 行为消息
UserDefined 用户自定义

SubTpe 说明

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

关于不同消息类型的 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"
    }
  }

StatusChange 消息示例

消息例子

  {
    "Time": "2019-03-28T15:17:33+08:00",
    "Type": "StatusChange",
    "SubType": "Online",
    "Topic": "",
    "Payload": "",
    "Seq": 6
  }

  {
    "Time": "2019-03-28T15:17:33+08:00",
    "Type": "StatusChange",
    "SubType": "Offline",
    "Topic": "",
    "Payload": "",
    "Seq": 7
  }

Action 行为示例

消息例子

  {
    "Time": "2021-05-13T21:34:05+08:00",
    "Type": "Action",
    "SubType": "Push",
    "Topic": "",
    "Payload": "eyJtZXRob2QiOiJhY3Rpb24iLCJjbGllbnRUb2tlbiI6IjEzNjk0NzQ5Mzo6cmVxXzEiLCJhY3Rpb25JZCI6ImxpZ2h0X2JsaW5rIiwidGltZXN0YW1wIjoxNjIwOTEyODQ1LCJwYXJhbXMiOnsiY29sb3IiOjEsInRpbWUiOjYsInRvdGFsX3RpbWUiOjYwfX0K",
    "Seq": 1620912845,
    "DeviceId": "0CPSFIRP28/testble"
  }

Payload base64 解码之后

  {
    "method": "action",
    "clientToken": "136947493::req_1",
    "actionId": "light_blink",
    "timestamp": 1620912845,
    "params": {
      "color": 1,
      "time": 6,
      "total_time": 60
    }
  }

  {
    "Time": "2021-05-13T21:34:05+08:00",
    "Type": "Action",
    "SubType": "Report",
    "Topic": "",
    "Payload": "eyJtZXRob2QiOiJhY3Rpb25fcmVwbHkiLCJjbGllbnRUb2tlbiI6IjEzNjk0NzQ5Mzo6cmVxXzEiLCJjb2RlIjowLCJzdGF0dXMiOiJzdWNjIiwicmVzcG9uc2UiOnsicmVzdWx0IjogMX19",
    "Seq": 1620912845,
    "DeviceId": "0CPSFIRP28/testble"
  }

Payload base64 解码之后

  {
    "method": "action_reply",
    "clientToken": "136947493::req_1",
    "code": 0,
    "status": "succ",
    "response": {
      "result": 1
    }
  }

目录