该Topic用于订阅指定设备事件上报消息。
前提
Topic
/device/{productId}/{deviceId}/message/event/{eventId}
订阅消息
- 生成随机13位数
js
randomNum = Math.random().toFixed(13).slice(-13)
randomNum = Math.random().toFixed(13).slice(-13)
- 发送消息内容到 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,消息格式如下:
名称 | 类型 | 必选 | 约束 | 说明 |
---|---|---|---|---|
» requestId | string | true | none | 订阅ID |
» topic | string | true | none | 订阅Topic |
» payload | [object] | true | none | 消息负载 |
»» headers | [object] | true | none | 消息头 |
»»» deviceName | string | true | none | 设备名称 |
»»» productName | string | true | none | 产品名称 |
»»» productId | [object] | true | none | 产品ID |
»»» _uid | string | false | none | 平台统一生成uid |
»»» creatorId | string | false | none | 创建者ID |
»»» traceparent | string | false | none | 链路追踪父ID |
»» deviceId | string | true | none | 设备ID |
»» timestamp | long | true | none | 时间戳 |
»» event | string | true | none | 事件ID |
»» data | [object] | false | none | 输出参数 |
»» messageType | string | true | none | 消息类型 |
» type | string | true | none | 类型 |
消息推送示例
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"
}