跳到主要内容

编写消息编解码脚本

解码脚本

提供 decode 方法实现,将设备上报消息转换为物模型消息。

//其它任意符合ECMAScript5的js代码
xxx
...

//必须提供decode方法
this.decode=function(msg){
//对msg进行解析,并返回物模型数据
//属性上报
return {
"mid":"xxx",
"productKey":"xxx", //可根据消息内容判断填写不同产品
"deviceName":"xxx",
"type":"property",
"identifier":"report", //report:属性上报,get:属性获取,set:属性设置
"occurred":xxxx, //时间戳,设备上的事件或数据产生的本地时间
"time":xxx, //时间戳,消息上报时间
"data":{
"属性x":属性x值,
"属性y":属性y值,
...
}
}
//事件上报
return {
"mid":"xxx",
"productKey":"xxx", //可根据消息内容判断填写不同产品
"deviceName":"xxx",
"type":"event",
"identifier":"xx事件",
"occurred":xxxx,
"time":xxx,
"data":{
"参数x":参数x值,
"参数y":参数y值,
...
}
}
//服务回复
return {
"mid":"xxx", //与服务调用时用的ID对应
"productKey":"xxx", //可根据消息内容判断填写不同产品
"deviceName":"xxx",
"type":"service",
"identifier":"xx服务Reply", //服务调用:xx服务,服务调用回复:xx服务Reply
"occurred":xxxx,
"time":xxx,
"code":0,
"data":{
"参数x":参数x值,
"参数y":参数y值,
...
}
}
}

参数说明

输入参数

msg:

参数名类型说明
productKeyString产品 key,必须
deviceNameString设备唯一标识,如 MAC,必须
midString经平台转换过后的消息 ID
contentObject设备上报的原始消息内容

输出参数

参数名类型说明
productKeyString产品 key,必须
deviceNameString设备唯一标识,如 MAC,必须
midString经平台转换过后的消息 ID,必须
typeString消息类型,必须。 property:属性、event:事件 、service:服务
identifierString消息标识符。
type=property 时可填: report:属性上报,set_reply:属性设置回复
type=event 时填物模型事件 identifier
type=service 时填物模型事件{identifier}_reply
occurredLong时间戳,必须,设备中事件或数据产生的本地时间
timeLong时间戳,必须,消息上报时间
codeint当消息为服务回复时,用于标识服务执行是否成功,0:成功,其它:按物模型定义填写
dataObject根据物模型填写上报的数据

编码脚本

提供 encode 方法实现,将标准物模型消息转换为设备指令。

//其它任意符合ECMAScript5的js代码
xxx
...

//必须提供encode方法
this.encode=function(service,device){
//service结构
/*
{
mid:"",
productKey:"",
deviceName:"",
type:"service",
identifier:"",
params:{
xxkey:xxvalue
}
}
*/
//device结构
/*
{
productKey:"",
deviceName:"",
properties:{
xxkey:xxvalue
},
tags:{
xxkey:xxvalue
}
}
*/

//属性获取
/*
{
mid:"",
productKey:"",
deviceName:"",
type:"property",
identifier:"",
params:[
xx属性,..
]
}
*/
service.identifier=='get'

//属性设置
service.identifier=='set'

//其它为服务调用

return {
mid:"",//设备协议中用的消息ID,返回后系统会将它与service中的mid对应关系保存
productKey:"",
deviceName:"",
content:""//发给设备的消息内容
}
}

输入参数

service:

参数名类型说明
productKeyString产品 key,必须
deviceNameString设备唯一标识,如 MAC,必须
midString平台消息 ID,必须
需要在 encode 中转换为设备消息 ID
typeString服务类型,必须
property:属性,service:服务
identifierString服务 ID,必须
type=property 时,get 为属性获取,set 为属性设置
type=service 时,填写物模型中服务对应的 identifier
paramsObject当 type=property,identifier 为 get 时,填写属性名数组,其它为 key=value 的对象

device:

参数名类型说明
productKeyString产品 key,必须
deviceNameString设备唯一标识,如 MAC,必须
propertiesObject属性 key=value
tagsObject设备标签 key=value

输出参数

参数名类型说明
productKeyString产品 key,必须
deviceNameString设备唯一标识,如 MAC,必须
midString设备消息 ID
contentObject下发给设备的消息内容