欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

MQTT物联网协议在物体模型基础上的实现与描述

最编程 2024-07-29 10:56:49
...

目录​​​​​​​

协议内容:

网关连接和注册

子设备注册

子设备注销

服务调用

事件上报

属性设置

属性获取

属性上报

设备获取配置

设备配置下发


注:该协议在我的开源项目iotkit(gitee)中有用到

协议内容:

网关连接和注册

通过mqtt连接:

mqttClientId: productKey+"_"+deviceName+"_"+model

mqttUserName: deviceName

mqttPassword: md5(产品密钥,mqttClientId) 

密钥:*****

mqtt-broker: 服务器mqtt地址

说明:

productKey: 在平台创建的产品productKey

deviceName: 设备唯一标识,如MAC

model: 设备型号

网关连接mqtt成功后,云端会对网关进行注册

连接成功后网关订阅topic: /sys/{productKey}/{deviceName}/c/#

云端验证订阅成功后,会将网关置为在线。

子设备注册

由网关上报

请求

Topic:/sys/{网关productKey}/{网关deviceName}/s/register

payload参数:

参数

类型

说明

id

String

消息ID

method

String

thing.lifetime.register

params

JSON

内容固定为:

{

  "productKey":设备PK,

  "deviceName":设mac,

  "model":设备型号

}

示例

topic:/sys/hbtgIA0SuVw9lxjB/AA:BB:CC:DD:22/s/register,
payload:{"id":"9f981472-7681-4392-b27e-9dc53fe0b020","params":{"productKey":"Rf4QSjbm65X45753","deviceName":"ABC12300002","model":"S01"}}

响应

Topic:/sys/{网关productKey}/{网关deviceName}/c/register_reply

payload参数:

参数

类型

说明

id

String

消息ID

method

String

thing.lifetime.register_reply

code

int

响应码,0:成功,x:其它错误

data

JSON

注册成功的设备信息:

{

  "productKey":设备PK,

  "deviceName":设mac,

  "model":设备型号

}

子设备注册成功后,订阅topic: /sys/{productKey}/{deviceName}/c/# 设备将变为在线,反之取消订阅该topic,设备将变为离线。

子设备注销

网关接收

请求

Topic:/sys/{网关productKey}/{网关deviceName}/c/deregister

payload参数:

参数

类型

说明

id

String

消息ID

method

String

thing.lifetime.deregister

params

JSON

内容固定为:

{

  "productKey":设备PK,

  "deviceName":设mac

}

响应

Topic:/sys/{网关productKey}/{网关deviceName}/s/deregister_reply

payload参数:

参数

类型

说明

id

String

消息ID

method

String

thing.lifetime.deregister_reply

code

int

响应码,0:成功,x:其它错误

云端先对子设备解绑然后网关再注销,方能在其它网关注册。

服务调用

请求

Topic:/sys/{productKey}/{deviceName}/c/service/服务名

payload参数:

参数

类型

说明

id

String

消息ID

method

String

thing.service.服务名

params

JSON

服务输入参数,格式:{"参数1":值1}

具体见产品物模型中服务的定义

响应

Topic:/sys/{productKey}/{deviceName}/s/service/服务名_reply

payload参数:

参数

类型

说明

id

String

消息ID

method

String

thing.service.服务名_reply

code

int

响应码,0:成功,x:其它错误

data

JSON

服务调用结果数据

事件上报

请求

Topic:/sys/{productKey}/{deviceName}/s/event/事件名

payload参数:

参数

类型

说明

id

String

消息ID

method

String

thing.event.事件名

params

JSON

事件上报参数,

格式:{"参数1":值1}

具体见产品物模型中事件的定义

响应

Topic:/sys/{productKey}/{deviceName}/c/event/事件名_reply

payload参数:

参数

类型

说明

id

String

消息ID

method

String

thing.event.事件名_reply

code

int

响应码,0:成功,x:其它错误

属性设置

请求

Topic:/sys/{productKey}/{deviceName}/c/service/property/set

payload参数:

参数

类型

说明

id

String

消息ID

method

String

thing.service.property.set

params

JSON

属性参数,

格式:{"参数1":值1}

具体见产品物模型中属性的定义

响应

Topic:/sys/{productKey}/{deviceName}/s/service/property/set_reply

payload参数:

参数

类型

说明

id

String

消息ID

method

String

thing.service.property.set_reply

code

int

响应码,0:成功,x:其它错误

属性获取

请求

Topic:/sys/{productKey}/{deviceName}/c/service/property/get

payload参数:

参数

类型

说明

id

String

消息ID

method

String

thing.service.property.get

params

JSON

数组,要获取的属性列表

["a","b","c"...]

响应

Topic:/sys/{productKey}/{deviceName}/s/service/property/get_reply

payload参数:

参数

类型

说明

id

String

消息ID

method

String

thing.service.property.get_reply

code

int

响应码,0:成功,x:其它错误

属性上报

请求

Topic:/sys/{productKey}/{deviceName}/s/event/property/post

payload参数:

参数

类型

说明

id

String

消息ID

method

String

thing.event.property.post

params

JSON

属性参数,

格式:{"参数1":值1}

具体见产品物模型中属性的定义

示例

topic:/sys/Rf4QSjbm65X45753/TEST_SW_000005/s/event/property/post,
payload:{"id":"2a72ddb8-7a84-4a57-b745-97698716fb9e","params":{"volt":26}}

响应

Topic:/sys/{productKey}/{deviceName}/c/event/property/post_reply

payload参数:

参数

类型

说明

id

String

消息ID

method

String

thing.event.property.post_reply

code

int

响应码,0:成功,x:其它错误

设备获取配置

请求

Topic:/sys/{productKey}/{deviceName}/s/config/get

payload参数:

参数

类型

说明

id

String

消息ID

method

String

thing.config.get

响应

Topic:/sys/{productKey}/{deviceName}/c/config/get_reply

payload参数:

参数

类型

说明

id

String

消息ID

method

String

thing.config.get_reply

code

int

响应码,0:成功,x:其它错误

data

JSON

json格式设备配置内容

设备配置下发

请求

Topic:/sys/{productKey}/{deviceName}/c/config/set

payload参数:

参数

类型

说明

id

String

消息ID

method

String

thing.config.set

params

JSON

json格式设备配置内容

响应

Topic:/sys/{productKey}/{deviceName}/s/config/set_reply

payload参数:

参数

类型

说明

id

String

消息ID

method

String

thing.config.set_reply

code

int

响应码,0:成功,x:其它错误

推荐阅读