在物联网平台创建NS产品解析脚本

完成设备产品物模型的创建,为了和利尔达NS平台进行通信,需要在产品管理相应产品下创建数据解析脚本进行格式转换。

示例编写了NS非透传设备的JS脚本,如下:

var property_report_name = "property_report"; //属性上报。
var module_property_report_value = "ee" ; //模组属性上报。

var command_property_set = "set_property";   //下行指令类型-属性设置
var command_property_set_value = "ff";   //属性设置的标识值

var command_property_set_ask = "property_set_ack";   //上行指令类型-属性设置回复
var command_property_set_ask_value = "fe";   //属性设置回复的标识值

var command_service_call = "service_call";   //下行指令类型-服务调用
var command_service_call_value = "af";   //下行指令类型-服务调用

var command_service_call_ack = "service_call_ack";   //下行指令类型-服务调用回复
var command_service_call_ack_value = "fa";   //下行指令类型-服务调用回复前缀

var command_event = "event_report";   //事件上报
var command_event_value = "ea";   //事件上报

var device_base_value = "dd";   //设备基础信息


//Switch
var property_set_switch = "switch";
var property_set_switch_value = "01";
var property_set_switch_int = 1;
//No_person
var property_set_No_person = "No_person";
var property_set_No_person_value = "02";
var property_set_No_person_int = 2;
//radar
var property_set_radar = "radar";
var property_set_radar_value = "03";
var property_set_radar_int = 3;


//--------------设备信息-----------
//ProtocolVersion
var device_up_ProtocolVersion = "ProtocolVersion";
var device_up_ProtocolVersion_num = "01";
var device_up_ProtocolVersion_bytes = 1;
//HardwareVersion
var device_up_HardwareVersion = "HardwareVersion";
var device_up_HardwareVersion_num = "02";
var device_up_HardwareVersion_bytes = 2;
//SoftwareVersion
var device_up_SoftwareVersion = "SoftwareVersion";
var device_up_SoftwareVersion_num = "03";
var device_up_SoftwareVersion_bytes = 3;
//DeviceSN
var device_up_DeviceSN = "DeviceSN";
var device_up_DeviceSN_num = "04";
var device_up_DeviceSN_bytes = 8;
//LedMode
var device_up_LedMode = "LedMode";
var device_up_LedMode_num = "05";
var device_up_LedMode_bytes = 1;

//--------------下行命令-----------
//Connected
var service_call_Connected = "Connected";
var service_call_Connected_value = "0b";
var service_call_Connected__bytes = 1;


/**
 * 上行16进制字符串转物模型JSON入口
 *
 */
function rawDataToProtocol(strString) {
   //头部
   var headBytes=4;
   var endBytes=2;
   var  head=strString.substr(0,2);
   var jsonMap = new Object();
   var params = {};
   //模组属性上报
   if(head==module_property_report_value){
          //封装对象
          jsonMap['report_type'] = property_report_name; 
          var  connect="0b01";
          //设置连接
          strString=connect+strString;
          //分隔传入的十六进制
          var splitStr=strString.split("ee");
          for(var i=0;i<splitStr.length;i++){
              var spString=splitStr[i];
              //属性类型
              var typeStr=spString.substr(0,2);
              //属性值转换
              var valueStr=spString.substr(2,4);
              var value_out=hexToint(valueStr);
              switch(typeStr) {
                case property_set_switch_value:
                    params['switch'] = value_out; 
                    break;
                case property_set_No_person_value:
                    params['No_person'] =value_out; 
                    break;
                case property_set_radar_value:
                    params['radar'] =value_out;
                    break;
                 default: 
                    break;
               } 
          }
        jsonMap['params'] = params; //ALink JSON格式,params标准字段。
        return  JSON.stringify(jsonMap);  
   }else if(head==command_property_set_ask_value){
          //属性设置回复
           jsonMap['report_type'] = command_property_set_ask; 
           var  connect="0b01";
          //设置连接
          strString=connect+strString;
          //分隔传入的十六进制
          var splitStr=strString.split("fe");
          for(var i=0;i<splitStr.length;i++){
              var spString=splitStr[i];
              //属性类型
              var typeStr=spString.substr(0,2);
              //属性值转换
              var valueStr=spString.substr(2,4);
              var value_out=hexToint(valueStr);
              switch(typeStr) {
                case property_set_switch_value:
                    params['Switch'] = value_out; 
                    break;
                 default: 
                    break;
               } 
          }
        jsonMap['params'] = params; //ALink JSON格式,params标准字段。
         return  JSON.stringify(jsonMap);  
   }else if(head==device_base_value){
        //设备基础信息上报
            jsonMap['report_type'] = property_report_name;
            var propertyList=getDeviceBaseString(strString).split(",");
            for(var i=0;i<propertyList.length;i++){
                var property=propertyList[i];
                if(property==""){
                    break;
                }
                //属性类型
                var propertyType=property.substr(endBytes,endBytes);
                var propertyValue="";
                var defaultValue="V";
                switch (propertyType){
                    case device_up_ProtocolVersion_num:
                        propertyValue=property.substr(headBytes,endBytes*device_up_ProtocolVersion_bytes);
                        propertyValue=parseHexToNumber(propertyValue,0);
                        params[device_up_ProtocolVersion] =defaultValue+propertyValue;
                        break;
                    case device_up_HardwareVersion_num:
                        propertyValue=property.substr(headBytes,endBytes*device_up_HardwareVersion_bytes);
                        var beginV=propertyValue.substr(0,2);
                        beginV=parseHexToNumber(beginV,0);
                        var endV=propertyValue.substr(2,2);
                        endV=parseHexToNumber(endV,0);
                        params[device_up_HardwareVersion] =defaultValue+beginV+"."+endV;
                        break;
                    case device_up_SoftwareVersion_num:
                        propertyValue=property.substr(headBytes,endBytes*device_up_SoftwareVersion_bytes);
                        var beginV=propertyValue.substr(0,2);
                        beginV=parseHexToNumber(beginV,0);
                        var midV=propertyValue.substr(2,2);
                        midV=parseHexToNumber(midV,0);
                        var endV=propertyValue.substr(4,2);
                        endV=parseHexToNumber(endV,0);
                        params[device_up_SoftwareVersion] =defaultValue+beginV+"."+midV+"."+endV;
                        break;
                    case device_up_DeviceSN_num:
                        propertyValue=property.substr(headBytes,endBytes*device_up_DeviceSN_bytes);
                        params[device_up_DeviceSN] =propertyValue;
                        break;
                    case device_up_LedMode_num:
                        propertyValue=property.substr(headBytes,endBytes*device_up_LedMode_bytes);
                        propertyValue=parseHexToNumber(propertyValue,0);
                        params[device_up_LedMode] =propertyValue;
                        break;                  
                    default:
                        break;
                }
            }
        jsonMap['params'] = params; 
        return  JSON.stringify(jsonMap);  
   }else if(head==command_service_call_ack_value){
        jsonMap['report_type'] = command_service_call_ack;
        jsonMap['serviceIdentify'] = "serv1"; 
        jsonMap['message_id'] = "53914100"; 
        params['serv1'] =1;
        jsonMap['params'] = params; //ALink JSON格式,params标准字段。
        return  JSON.stringify(jsonMap);  
   }else if(head==command_event_value){
        jsonMap['report_type'] = command_event;
        jsonMap['event'] = "even1"; 
        params['even1'] =1;
        jsonMap['params'] = params; //ALink JSON格式,params标准字段。
        return  JSON.stringify(jsonMap);  
        command_event_value
   }
}


/**
 * 下行json格式数据转十六进制字符串入口
 *
 */
function protocolToRawData(json) {
    var command_type = json['command_type'];
    var message_id = json['message_id'];
        message_id=int2hex(message_id,4);
    var service_identify=json['service_identify'];
    // 参数
    var params = json['params'];
    var returnString="";
    var value="";

    if(command_type ==  command_property_set){
        var defString="ff";
        var values
        for(ind in params){
             var values=int2hex(params[ind],2)
             switch(ind) {
                case property_set_switch:
                     returnString+=defString+property_set_switch_value+values    
                     break;
                default: 
                     break;
         }
      }
    }else if(command_type ==  command_service_call){
        switch (service_identify){
            case service_call_Connected:
                value =params[service_call_Connected];
                value =int2hex(value, 2);
                returnString=command_service_call_value+service_call_Connected_value+value+message_id;
                break;
            default:
                break;
        }
    }
     return returnString;
}

//------------设备基础信息转换-------
function getDeviceBaseString(orgStr) {
    var str1=orgStr;
    var str2="";
    var str3="";
    var str4=",";
    var str5="";
    var head= orgStr.substr(0,2);
    var headType=orgStr.substr(2,2);
    switch (headType){
        case device_up_ProtocolVersion_num:
          str3+=getBaseString(head,headType,orgStr,str2,str3,str4,str5,device_up_ProtocolVersion_bytes);
          break;
        case device_up_HardwareVersion_num:
          str3+=getBaseString(head,headType,orgStr,str2,str3,str4,str5,device_up_HardwareVersion_bytes);
          break;
        case device_up_SoftwareVersion_num:
          str3+=getBaseString(head,headType,orgStr,str2,str3,str4,str5,device_up_SoftwareVersion_bytes);
          break;
        case device_up_DeviceSN_num:
          str3+=getBaseString(head,headType,orgStr,str2,str3,str4,str5,device_up_DeviceSN_bytes);
          break;
        case device_up_LedMode_num:
           str3+=getBaseString(head,headType,orgStr,str2,str3,str4,str5,device_up_LedMode_bytes);
          break;
        default: 
            break;   
    }
    return str3;
}

function getBaseString(head,headType,orgStr,str2,str3,str4,str5,bytes){
    var headBytes=4;
    var endBytes=2*bytes;
    var longBytes=headBytes+endBytes;
    var endStr=orgStr.substr(headBytes,endBytes);
    str2=head+headType+endStr;
    str3=str2+str4;
    str5=orgStr.substring(longBytes);
    str3+=getDeviceBaseString(str5);
    return str3;
}

//------------工具类--------
function parseHexToNumber(info,sign) {
    if(sign==1){
        var res = 0;
        var l = info.length/2;
        var fh = parseInt(info.substr(0,2),16);
        if(fh>127){
            var __array = new Array(l);
            __array[0] = fh-128;
            for(var i=1;i<l;i++){ __array[i] = parseInt(info.substr(2*i,2),16); }
            for(var i=__array.length-1;i>=0;i--){
                var __data = __array[i];
                if(__data>0) {__array[i] = __data -1;break;}
                else __array[i] = 255;
            }
            for(var i=__array.length-1;i>=0;i--){
                var __data = __array[i];
                if(i>0){__array[i] = 256-1-__array[i];}
                else{__array[i] = 128-1-__array[i];}
            }
            //console.log("__array => ",__array);
            for(var i=0;i<__array.length;i++){
                res = res + __array[i];
                if(i<(__array.length-1)) res *= 256;
            }
            return -1*res;
        }
        else{
            for(var i=0;i<l;i++){
                var __res = parseInt(info.substr(2*i,2),16);
                res += __res;
                if(i<(l-1)) res *= 256;
            }
        }
        return res;

    }else{
        var res = 0;
        var l = info.length/2;
        for(var i=0;i<l;i++){
            var __res = parseInt(info.substr(2*i,2),16);
            res += __res;
            if(i<(l-1)) res *= 256;
        }
        return res;
    }
    return "ERROR";
}


//十六进制转10进制
function hexToint(hex) {
    var len = hex.length, a = new Array(len), code;
    for (var i = 0; i < len; i++) {
        code = hex.charCodeAt(i);
        if (48<=code && code < 58) {
            code -= 48;
        } else {
            code = (code & 0xdf)  - 65 + 10;
            //处理x站位符
            if(code==33){
                code=0;
            }
        }
        a[i] = code;
    }
    return a.reduce(function(acc, c) {
        acc = 16 * acc + c;
        return acc;
    }, 0);
}

//十进制转十六进制
function int2hex(num, width) {
  var hex = "0123456789abcdef";
  var s = "";
  while (num) {
    s = hex.charAt(num % 16) + s;
    num = Math.floor(num / 16);
  }
  if (typeof width === "undefined" || width <= s.length) {
    return s;
  }
  var delta = width - s.length;
  var padding = "";
  while(delta-- > 0) {
    padding += "0";
  }
  return padding + s;
}

脚本写完后可以在旁边的模拟数据中先进行测试,如果所有的模拟数据对应的模拟运行结果如预期一样则说明脚本编写完成,如下:模拟运行结果

后续步骤

测试物联网平台与利尔达NS平台上下行通信

results matching ""

    No results matching ""