该Topic用于订阅平台下发功能调用消息。
前提
Topic
/device/{productId}/{deviceId}/message/send/function
订阅消息
- 生成随机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/send/function",
"parameter": {},
"id": "request-id" + randomNum
}
{
"type": "sub",
"topic": "/device/{productId}/{deviceId}/message/send/function",
"parameter": {},
"id": "request-id" + randomNum
}
消息推送格式
成功订阅平台下发功能调用消息之后,平台调用功能时,平台会将消息实时推送到 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 | true | none | 平台统一生成uid |
»»» creatorId | string | true | none | 创建者ID |
»»» async | boolean | false | none | 是否异步:false表示同步,true表示异步 |
»»» traceparent | string | true | none | 链路追踪父ID |
»» deviceId | string | true | none | 设备ID |
»» timestamp | long | true | none | 时间戳 |
»» functionId | string | true | none | 功能ID |
»» inputs | array | true | none | 输入参数 |
»»» name | string | true | none | 输入参数名称 |
»»» value | 类型由创建物模型时确定 | true | none | 输入参数值 |
»» messageType | string | true | none | 消息类型 |
» type | string | true | none | 类型 |
消息推送示例
json
{
"requestId": "request-id-1691050894608",
"topic": "/device/api-test/api-test-device1/message/send/function",
"payload": {
"headers": {
"deviceName": "api测试设备1",
"productName": "api测试",
"productId": "api-test",
"_uid": "E4m6fNur332Zvil1-fwkBMBN8AgE29_b",
"creatorId": "1199596756811550720",
"async": false,
"traceparent": "00-db32b11c33a01d4302ee97bfa1680593-6bd781b267f29305-01"
},
"messageId": "1687015831828840449",
"deviceId": "api-test-device1",
"timestamp": 1691050892203,
"functionId": "fun1",
"inputs": [
{
"name": "a",
"value": 1
},
{
"name": "b",
"value": 1
}
],
"messageType": "INVOKE_FUNCTION"
},
"type": "result"
}
{
"requestId": "request-id-1691050894608",
"topic": "/device/api-test/api-test-device1/message/send/function",
"payload": {
"headers": {
"deviceName": "api测试设备1",
"productName": "api测试",
"productId": "api-test",
"_uid": "E4m6fNur332Zvil1-fwkBMBN8AgE29_b",
"creatorId": "1199596756811550720",
"async": false,
"traceparent": "00-db32b11c33a01d4302ee97bfa1680593-6bd781b267f29305-01"
},
"messageId": "1687015831828840449",
"deviceId": "api-test-device1",
"timestamp": 1691050892203,
"functionId": "fun1",
"inputs": [
{
"name": "a",
"value": 1
},
{
"name": "b",
"value": 1
}
],
"messageType": "INVOKE_FUNCTION"
},
"type": "result"
}