阿里云物联网脚本示例

 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; //属 性 设 置 设 备 返 回 结 果
    /*
     * 示 例 数 据:
     *  传 入 参 数  ->
     *      0x00000000110506 // 共 7 个 字 节
     *  输 出 结 果 ->
     *      {"method":"thing.event.property.post", "id":17, "params":{"Temperature":5,"Humidity":7}, "version":"1.0"}
     *  传 入 参 数 ->
     *      0x0300000044c8 // 共 6个 字 节
     *  输 出 结 果 ->
     *      {"id":68,"code":"200","data":{},"version":"1.0"}
     * 
     */
    function rawDataToProtocol(bytes){
        var uint8Array = new Uint8Array(bytes.length);
        for (var i = 0; i < bytes.length; i++){
            uint8Array[i] = bytes[i] & 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;
    }
    
    /*
     * 示 例 数 据:
     *  传 入 参 数 ->
     *      {"method":"thing.service.property.set", "id":"12345", "version":"1.1", "params":{"Temperature":42, "Humidity":70}}
     *  输 出 结 果 ->
     *      0x01000030392A46
     *  传 入 参 数 ->
     *      {"id": "17","version": "1.0","code": 200,"method": "thing.event.property.post","data": {}}
     *  输 出 结 果 ->
     *      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);
    }