Skip to content

订阅指定设备事件上报

更新时间:

该Topic用于订阅指定设备事件上报消息。

前提

Topic

/device/{productId}/{deviceId}/message/event/{eventId}

订阅消息

  1. 生成随机13位数
js
randomNum = Math.random().toFixed(13).slice(-13)
randomNum = Math.random().toFixed(13).slice(-13)
  1. 发送消息内容到 WebSocket
json
{
    "type": "sub",
    "topic": "/device/{productId}/{deviceId}/message/event/{eventId}",
    "parameter": {},
    "id": "request-id" + randomNum //请求id
}
{
    "type": "sub",
    "topic": "/device/{productId}/{deviceId}/message/event/{eventId}",
    "parameter": {},
    "id": "request-id" + randomNum //请求id
}

消息推送格式

成功订阅指定设备事件上报消息之后,当设备上报事件时,平台会将事件上报消息实时推送到 WebSocket,消息格式如下:

名称类型必选约束说明
» requestIdstringtruenone订阅ID
» topicstringtruenone订阅Topic
» payload[object]truenone消息负载
»» headers[object]truenone消息头
»»» deviceNamestringtruenone设备名称
»»» productNamestringtruenone产品名称
»»» productId[object]truenone产品ID
»»» _uidstringfalsenone平台统一生成uid
»»» creatorIdstringfalsenone创建者ID
»»» traceparentstringfalsenone链路追踪父ID
»» deviceIdstringtruenone设备ID
»» timestamplongtruenone时间戳
»» eventstringtruenone事件ID
»» data[object]falsenone输出参数
»» messageTypestringtruenone消息类型
» typestringtruenone类型

消息推送示例

json
{
    "requestId": "request-id-1691049801154",
    "topic": "/device/api-test/api-test-device1/message/event/event1",
    "payload": {
        "headers": {
            "deviceName": "api测试设备1",
            "productName": "api测试",
            "productId": "api-test",
            "_uid": "E4m6bC8OzYeAJntxlq1nW8bKv0C4f6Rx",
            "creatorId": "1199596756811550720",
            "traceparent": "00-1fac1e9ff799be4683c3af32fa307185-0250b069410996bd-01"
        },
        "deviceId": "api-test-device1",
        "timestamp": 1691049799438,
        "event": "event1",
        "data": {
            "a": 1,
            "b": 2
        },
        "messageType": "EVENT"
    },
    "type": "result"
}
{
    "requestId": "request-id-1691049801154",
    "topic": "/device/api-test/api-test-device1/message/event/event1",
    "payload": {
        "headers": {
            "deviceName": "api测试设备1",
            "productName": "api测试",
            "productId": "api-test",
            "_uid": "E4m6bC8OzYeAJntxlq1nW8bKv0C4f6Rx",
            "creatorId": "1199596756811550720",
            "traceparent": "00-1fac1e9ff799be4683c3af32fa307185-0250b069410996bd-01"
        },
        "deviceId": "api-test-device1",
        "timestamp": 1691049799438,
        "event": "event1",
        "data": {
            "a": 1,
            "b": 2
        },
        "messageType": "EVENT"
    },
    "type": "result"
}