概述

除了使用自定义协议以外,物联中台提供了默认的标准协议支持. 设备可以使用此协议接入平台. 设备协议已经确定并且无法修改协议的时候,建议使用自定义协议接入。

Topic

  • 一个topic对应一个物联云标准消息格式

  • 一个物联云标准消息格式可以对应多个topic

  • topic是物联平台与终端交互信息时区分消息归属、消息类别的凭证。

Topic 定义

所有设备消息的topic的前缀均为: /{productId}/{deviceId}.
如:产品product-1下的设备device-1上线消息: /dproduct-1/device-1/online.

topic 类型 说明
/online DeviceOnlineMessage 设备上线
/offline DeviceOfflineMessage 设备离线
/message/event/{eventId} DeviceEventMessage 设备事件
/message/property/report ReportPropertyMessage 设备上报属性
/message/send/property/read ReadPropertyMessage 平台下发读取消息指令
/message/send/property/write WritePropertyMessage 平台下发修改消息指令
/message/property/read/reply ReadPropertyMessageReply 读取属性回复
/message/property/write/reply WritePropertyMessageReply 修改属性回复
/message/send/function FunctionInvokeMessage 平台下发功能调用
/message/function/reply FunctionInvokeMessageReply 调用功能回复
/register DeviceRegisterMessage 设备注册,通常与子设备消息配合使用
/unregister DeviceUnRegisterMessage 设备注销,同上
/message/children/{childrenDeviceId}/{topic} ChildDeviceMessage 子设备消息,{topic}为子设备消息对应的topic
/message/children/reply/{childrenDeviceId}/{topic} ChildDeviceMessage 子设备回复消息,同上
/message/direct DirectDeviceMessage 透传消息
/message/tags/update UpdateTagMessage 更新标签消息 since 1.5
/firmware/pull RequestFirmwareMessage 拉取固件请求 (设备->平台)
/firmware/pull/reply RequestFirmwareMessageReply 拉取固件请求回复 (平台->设备)
/firmware/report ReportFirmwareMessage 上报固件信息
/firmware/progress UpgradeFirmwareProgressMessage 上报更新固件进度
/firmware/push UpgradeFirmwareMessage 推送固件更新
/firmware/push/reply UpgradeFirmwareMessageReply 固件更新回复

协议与Topic的关系说明

MQTT - Topic对应的是消息主题
HTTP - Topic对应的是请求接口地址
COAP - Topic对应的是请求接口地址
WebSocket -

上下行数据转换

  • 标准编解码器
  • 包含:码库、编码、解码
  • 码库:产品标识+设备标识+码库特定义
  • 如:属性上报: 产品标识+设备标识+ /properties/report
  • 注释:
  • A.默认为标准物模型数据格式,这直接 payload.toJavaObject(type) 转换
  • B.非标准物模型数据格式,则重写相应的doEncode、doDecode方法,进行标准物模型数据格式转换
public enum TopicMessageCodec {

    // 上报属性数据  A.按物联云标准物模型数据格式上报的无需进行转行
    reportProperty("/*/properties/report", ReportPropertyMessage.class),


    // 事件上报 B.非标准物模型数据格式,则重写相应的doEncode、doDecode方法,进行标准物模型数据格式转换
    event("/*/event/*", EventMessage.class) {
        @Override
        Publisher<DeviceMessage> doDecode(ObjectMapper mapper, String[] topic, byte[] payload) {
            String event = topic[topic.length - 1];

            return Mono.from(super.doDecode(mapper, topic, payload))
                    .cast(EventMessage.class)
                    .doOnNext(e -> e.setEvent(event))
                    .cast(DeviceMessage.class);
        }

        @Override
        void refactorTopic(String[] topics, DeviceMessage message) {
            super.refactorTopic(topics, message);
            EventMessage event = ((EventMessage) message);
            topics[topics.length - 1] = String.valueOf(event.getEvent());
        }
    },
}

MQTT 解读

目前支持MQTT3.1.1和3.1版本协议
1.认证(简单)
CONNECT报文:
clientId: 设备实例ID(固定)
username: 根据实际设备参数配置,与普通一致
password: 根据实际设备参数配置,与普通一致

2.读取设备属性
topic: /{productId}/{deviceId}/properties/read

方向: 下行

消息格式:

{
"timestamp":1601196762389, //毫秒时间戳
"messageId":"消息ID",
"deviceId":"设备ID",
"properties":["sn","model"] //要读取到属性列表
}

回复Topic: /{productId}/{deviceId}/properties/read/reply

回复消息格式:

//成功

{
    "timestamp":1601196762389, //毫秒时间戳
    "messageId":"与下行消息中的messageId相同",
    "properties":{"sn":"test","model":"test"}, //key与设备模型中定义的属性id一致
    "deviceId":"设备ID",
    "success":true
}

//失败. 下同

{
    "timestamp":1601196762389, //毫秒时间戳
    "messageId":"与下行消息中的messageId相同",
    "success":false,
    "code":"error_code",
    "message":"失败原因"
}

3修改设备属性:
topic: /{productId}/{deviceId}/properties/write

方向: 下行

消息格式:

{
"timestamp":1601196762389, //毫秒时间戳
"messageId":"消息ID",
"deviceId":"设备ID",
"properties":{"color":"red"} //要设置的属性
}

回复Topic: /{productId}/{deviceId}/properties/write/reply

方向: 上行

回复消息格式:

{
"timestamp":1601196762389, //毫秒时间戳
"messageId":"与下行消息中的messageId相同",
"properties":{"color":"red"}, //设置成功后的属性,可不返回
"success":true
}

4.设备属性上报
topic: /{productId}/{deviceId}/properties/report

方向: 上行

消息格式:

{
"timestamp":1601196762389, //毫秒时间戳
"messageId":"随机消息ID",
"properties":{"temp":36.8} //上报数据
}

5.调用设备功能
topic: /{productId}/{deviceId}/function/invoke

方向: 下行

消息格式:

{
"timestamp":1601196762389, //毫秒时间戳
"messageId":"消息ID",
"deviceId":"设备ID",
"function":"playVoice",//功能ID
"inputs":[{"name":"text","value":"播放声音"}] //参数
}

回复Topic: /{productId}/{deviceId}/function/invoke/reply

方向: 上行

消息格式:

{
"timestamp":1601196762389, //毫秒时间戳
"messageId":"与下行消息中的messageId相同",
"output":"success", //返回执行结果,具体类型与物模型中功能输出类型一致
"success":true,
}

6.设备事件上报
topic: /{productId}/{deviceId}/event/{eventId}

方向: 上行

消息格式:

{
"timestamp":1601196762389, //毫秒时间戳
"messageId":"随机消息ID",
"data":100 //上报数据,类型与物模型事件中定义的类型一致
}
`

7.子设备注册
与子设备消息配合使用,实现设备与网关设备进行自动绑定.

topic: /{productId}/{deviceId}/child/{childDeviceId}/register

方向: 上行

消息格式:

{
"timestamp":1601196762389, //毫秒时间戳
"messageId":"随机消息ID",
"deviceId":"子设备ID"
}
```
8.子设备注销
与子设备消息配合使用,实现设备与网关设备进行自动解绑.

topic: /{productId}/{deviceId}/child/{childDeviceId}/unregister

方向: 上行

消息格式:
````json
{
"timestamp":1601196762389, //毫秒时间戳
"messageId":"随机消息ID",
"deviceId":"子设备ID"
}

9.子设备上线
与子设备消息配合使用,实现关联到网关的子设备上线.(默认情况下,网关上线,子设备也会全部自动上线.)

topic: /{productId}/{deviceId}/child/{childDeviceId}/connected

方向: 上行

消息格式:

{
"timestamp":1601196762389, //毫秒时间戳
"messageId":"随机消息ID",
"timestamp":1584331469964//时间戳
}

10.子设备离线
与子设备消息配合使用,实现关联到网关的子设备离线.(默认情况下,网关离线,子设备也会全部自动离线.)

topic: /{productId}/{deviceId}/child/{childDeviceId}/disconnect

方向: 上行

消息格式:

{
"timestamp":1601196762389, //毫秒时间戳
"messageId":"随机消息ID",
"timestamp":1584331469964//时间戳
}

11.子设备消息
topic: /{productId}/{deviceId}/child/{childDeviceId}/{topic}

方向: 上行或下行, 根据{topic}决定.

文档更新时间: 2022-06-01 15:07   作者:管理员