附录:示例脚本

<p>根据脚本示例Demo如下:</p> <pre> <code>var PA_ID = "12345"; var PA_VERSION = "1.0"; var PA_PROP_POST_METHOD = 'thing.event.property.post'; var PA_PROP_SET_METHOD = 'thing.service.property.set'; var COMMAND_REPORT_REPLY = 0x02; //上 报 数 据 返 回 结 果 var COMMAND_SET_REPLY = 0x03; //属 性 设 置 设 备 返 回 结 果 /* * 示 例 数 据: * 传 入 参 数 -&gt; * 0x00000000110506 // 共 7 个 字 节 * 输 出 结 果 -&gt; * {"method":"thing.event.property.post", "id":17, "params":{"Temperature":5,"Humidity":7}, "version":"1.0"} * 传 入 参 数 -&gt; * 0x0300000044c8 // 共 6个 字 节 * 输 出 结 果 -&gt; * {"id":68,"code":"200","data":{},"version":"1.0"} * */ function rawDataToProtocol(bytes){ var uint8Array = new Uint8Array(bytes.length); for (var i = 0; i &lt; bytes.length; i++){ uint8Array[i] = bytes[i] &amp; 0xff; } var params = {}; var jsonMap = {}; var dataView = new DataView(uint8Array.buffer, 0); var cmd = uint8Array[0]; // command if (cmd === 0x00){ params['Temperature'] = dataView.getInt8(5); params['Humidity'] = dataView.getInt8(6); jsonMap['method'] = PA_PROP_POST_METHOD; jsonMap['params'] = params; } else if(cmd == COMMAND_SET_REPLY) { jsonMap['version'] = '1.0'; jsonMap['id'] = '' + dataView.getInt32(1); jsonMap['code'] = ''+ dataView.getUint8(5); jsonMap['data'] = {}; } jsonMap['version'] = PA_VERSION; jsonMap['id'] = dataView.getInt32(1); return jsonMap; } /* * 示 例 数 据: * 传 入 参 数 -&gt; * {"method":"thing.service.property.set", "id":"12345", "version":"1.1", "params":{"Temperature":42, "Humidity":70}} * 输 出 结 果 -&gt; * 0x01000030392A46 * 传 入 参 数 -&gt; * {"id": "17","version": "1.0","code": 200,"method": "thing.event.property.post","data": {}} * 输 出 结 果 -&gt; * 0x0200000011C8 */ function protocolToRawData(json) { var id = json['id']; var method = json['method']; var version = json['version']; var payloadArray = []; if (method == PA_PROP_SET_METHOD){ // 追 加 下 行 帧 头 部 payloadArray = payloadArray.concat(0x01); payloadArray = payloadArray.concat(buffer_int32(parseInt(id))); var params = json['params']; var temperature = params['Temperature'] var humidity = params['Humidity']; if (temperature !== null){ payloadArray = payloadArray.concat(temperature); } if (humidity !== null){ payloadArray = payloadArray.concat(humidity); } } else if (method == PA_PROP_POST_METHOD) { //设 备 上 报 数 据 返 回 结 果 var code = json['code']; payloadArray = payloadArray.concat(buffer_uint8(COMMAND_REPORT_REPLY)); //command字 段 payloadArray = payloadArray.concat(buffer_int32(parseInt(id))); // JSON格式 'id' payloadArray = payloadArray.concat(buffer_uint8(code)); } return payloadArray; } //以 下 是 部 分 辅 助 函 数 function buffer_uint8(value) { var uint8Array = new Uint8Array(1); var dv = new DataView(uint8Array.buffer, 0); dv.setUint8(0, value); return [].slice.call(uint8Array); } function buffer_int16(value) { var uint8Array = new Uint8Array(2); var dv = new DataView(uint8Array.buffer, 0); dv.setInt16(0, value); return [].slice.call(uint8Array); } function buffer_int32(value) { var uint8Array = new Uint8Array(4); var dv = new DataView(uint8Array.buffer, 0); dv.setInt32(0, value); return [].slice.call(uint8Array); } function buffer_float32(value) { var uint8Array = new Uint8Array(4); var dv = new DataView(uint8Array.buffer, 0); dv.setFloat32(0, value); return [].slice.call(uint8Array); }</code></pre> <p>&nbsp;</p>
以上内容是否解决了您的问题?
请补全提交信息!
咨询·建议

电话咨询 - 7x24 小时

400-151-8800

邮件咨询

cloud@pingan.com

在线客服

7x24 小时,急速解答

工单支持

解决云产品相关技术问题