该Topic用于订阅指定设备上线消息。
前提
设备处于离线状态。
需要设备上线,连接详情参见设备上线。
Topic
/device/{productId}/{deviceId}/online
订阅消息
- 生成随机13位数
js
randomNum = Math.random().toFixed(13).slice(-13)
randomNum = Math.random().toFixed(13).slice(-13)
- 发送消息内容到 WebSocket
json
{
"type": "sub",
"topic": "/device/{productId}/{deviceId}/online",
"parameter": {},
"id": "request-id" + randomNum
}
{
"type": "sub",
"topic": "/device/{productId}/{deviceId}/online",
"parameter": {},
"id": "request-id" + randomNum
}
订阅示例
json
{
"type": "sub",
"topic": "/device/api-test/api-test-device1/online",
"parameter": {},
"id": "request-id"
}
{
"type": "sub",
"topic": "/device/api-test/api-test-device1/online",
"parameter": {},
"id": "request-id"
}
消息推送格式
成功订阅指定设备上线消息后,当设备上线时,平台会将上线消息实时推送到 WebSocket ,消息推送格式如下:
名称 | 类型 | 必选 | 约束 | 说明 |
---|---|---|---|---|
» requestId | string | true | none | 订阅ID |
» topic | string | true | none | 订阅Topic |
» payload | [object] | true | none | 消息负载 |
»» headers | [object] | true | none | 消息头 |
»»» serverId | string | true | none | 服务ID |
»»» deviceName | string | true | none | 设备名称 |
»»» productName | string | true | none | 产品名称 |
»»» productId | string | true | none | 产品ID |
»»» connectTime | long | true | none | 设备上线时间 |
»»» _uid | string | true | none | 平台统一生成uid |
»»» creatorId | string | true | none | 创建者ID |
»» deviceId | string | true | none | 设备ID |
»» timestamp | long | true | none | 时间戳 |
»» messageType | string | true | none | 消息类型,固定为ONLINE |
» type | string | true | none | 类型 |
消息推送示例
json
{
"requestId": "request-id-1691065648625",
"topic": "/device/api-test/api-test-device1/online",
"payload": {
"headers": {
"serverId": "Himalaya-platform:9101",
"deviceName": "api测试设备1",
"productName": "api测试",
"connectTime": 1691065652076,
"from": "session",
"productId": "api-test",
"_uid": "VYm7XhNtJDuifRmkFE5bPEuW0CE887dq",
"creatorId": "1199596756811550720"
},
"deviceId": "api-test-device1",
"timestamp": 1691065652077,
"messageType": "ONLINE"
},
"type": "result"
}
{
"requestId": "request-id-1691065648625",
"topic": "/device/api-test/api-test-device1/online",
"payload": {
"headers": {
"serverId": "Himalaya-platform:9101",
"deviceName": "api测试设备1",
"productName": "api测试",
"connectTime": 1691065652076,
"from": "session",
"productId": "api-test",
"_uid": "VYm7XhNtJDuifRmkFE5bPEuW0CE887dq",
"creatorId": "1199596756811550720"
},
"deviceId": "api-test-device1",
"timestamp": 1691065652077,
"messageType": "ONLINE"
},
"type": "result"
}