Skip to content

订阅平台下发功能调用

更新时间:

该Topic用于订阅平台下发功能调用消息。

前提

Topic

/device/{productId}/{deviceId}/message/send/function

订阅消息

  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/send/function",
    "parameter": {},
    "id": "request-id" + randomNum
}
{
    "type": "sub",
    "topic": "/device/{productId}/{deviceId}/message/send/function",
    "parameter": {},
    "id": "request-id" + randomNum
}

消息推送格式

成功订阅平台下发功能调用消息之后,平台调用功能时,平台会将消息实时推送到 WebSocket ,消息格式如下:

名称类型必选约束说明
» requestIdstringtruenone订阅ID
» topicstringtruenone订阅Topic
» payload[object]truenone消息负载
»» headers[object]truenone消息头
»»» deviceNamestringtruenone设备名称
»»» productNamestringtruenone产品名称
»»» productId[object]truenone产品ID
»»» _uidstringtruenone平台统一生成uid
»»» creatorIdstringtruenone创建者ID
»»» asyncbooleanfalsenone是否异步:false表示同步,true表示异步
»»» traceparentstringtruenone链路追踪父ID
»» deviceIdstringtruenone设备ID
»» timestamplongtruenone时间戳
»» functionIdstringtruenone功能ID
»» inputsarraytruenone输入参数
»»» namestringtruenone输入参数名称
»»» value类型由创建物模型时确定truenone输入参数值
»» messageTypestringtruenone消息类型
» typestringtruenone类型

消息推送示例

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