自定义透传脚本解析功能是为了降低硬件设备的开发成本,提高产品接入效率。主要是在下行链路中,云端调用脚本将标准的json数据转换成二进制数据透传给设备,在上行链路中,云端在收到设备上报的二进制原始数据之后,通过调用解析脚本,将二进制数据解析成标准json数据,方便存储以及展示。目前脚本通过JavaScript语言开发,需要支持以下两个方法即可实现与云端上下行通讯。
1 protocolToRawData:用于在下行链路中,将云端的标准json数据转换成设备能够识别的二进制数据。方法返回byte数组
2 rawDataToProtocol:用于在上行链路中,将设备主动上报或是响应云端请求的数据解析成云端能够识别的标准json数据,方便存储以及展示。
目前脚本仅支持符合ECMAScript5.1的JavaScript语法,暂不支持ECMA6以上语法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | /** * 将下行数据转换为设备能识别的格式数据 * 入参:jsonObj 对象 不能为空 * 出参:rawData byte[]数组 不能为空 */ function protocolToRawData(jsonObj) { return rawdata; } /** * 设备上行数据解析 * 入参:rawData byte[]数组 不能为空 * 出参:jsonObj 对象 不能为空 */ function rawDataToProtocol(rawData) { return jsonObj; } |
用途:用于将云端的下行标准json协议数据转换成设备底层二进制原始数据,开发者需要重写该方法
参数:json,里面封装了method,id以及设备的属性数据
返回:字节数据
function protocolToRawData(json){}
1 2 3 4 5 | { "method" : "thing.attribute.get" , "id" : "" , "params" :[ "powerstate" , "windspeed" ] } |
1 2 3 4 5 6 7 | { "method" : "thing.attribute.set" , "id" : "" , "params" :{ "powerstate" : 1 } } |
1 2 3 4 5 6 7 8 | { "method" : "thing.service.invoke" , "id" : "" , "serviceName" : "" "params" :{ "powerstate" : 1 } } |
用于解析设备上行原始二进制数据解析成云端标准json数据,便于云端存储跟查询。
参数:二进制字节数组
返回:json数组
1 2 3 4 5 6 7 | { "method" : "thing.attribute.get" , "id" : "" , "params" : { "powerstate" : } } |
1 2 3 4 5 6 7 | { "method" : "thing.attribute.set" , "id" : "" , "params" : { "powerstate" : } } |
1 2 3 4 5 6 7 | { "method" : "thing.attribute.post" , "id" : "" , "params" : { "powerstate" : } } |
1 2 3 4 5 6 7 8 | { "method" : "thing.event.post" , "id" : "" , "eventName" : "params" : { "powerstate" : } } |
1 2 3 4 5 6 7 8 | { "method" : "thing.service.invoke" , "id" : "" , "serviceName" : "" "params" :{ "powerstate" : 1 } } |
取值 | 含义 |
---|---|
thing.attribute.get | 云端请求获取设备属性以及设备响应get请求返回响应 |
thing.attribute.set | 云端设置设备属性以及设备响应set请求返回响应 |
thing.attribute.post | 设备上报属性数据 |
thing.event.post | 设备事件上报 |
thing.service.invoke | 设备端服务调用 |
thing.service.response | 设备端响应服务调用 |
thing.attribute.adjust | 调整设备端属性以及设备端响应属性调整 |