NetCore物联网平台 4 - 规则引擎

NetCore物联网平台 1 - 简介

NetCore物联网平台 2 - 平台

NetCore物联网平台 3 - 通信组件

NetCore物联网平台 4 - 规则引擎

 

mqtt.js解析

示例报文:0000000102 {"topic":"/user/update_iot","method":"thing.event.property.post","params":{"temperature":258}}

function ProtocolSample(val) {
    var out = '';
    if (1 == val) { 
        out = '0000000102'; 
    }
    else if (2 == val) { 
        out = '{"method":"thing.service.property.set","params":{"temperature":258}}'; 
    } 
    return out;
}

var COMMAND_REPORT = 0x00; //属性上报
var COMMAND_SET = 0x01; //属性设置。

var PROP_REPORT_METHOD = 'thing.event.property.post'; //物联网平台Topic,设备上传属性数据到云端
var PROP_SET_METHOD = 'thing.service.property.set'; //物联网平台Topic,云端下发属性控制指令到设备端


/*  示例数据:
 *  设备上报属性数据
 *  输入: 0x000304027500000102
 *  输出:{"method":"thing.event.property.post","id":"50594421","params":{"temperature":258},"version":"1.0"}
*/
function rawDataToProtocol(hex) { 
    var bytes = Str2Bytes(hex);  
    var uint8Array = new Uint8Array(bytes.length);   
    for (var i = 0; i < bytes.length; i++) {       
        uint8Array[i] = bytes[i] & 0xff;    
    }   
    var dataView = new DataView(uint8Array.buffer, 0);  //10进制bytes数组转16进制字符串
 
    var jsonMap = {};
    var fHead = uint8Array[0]; // command
    if (fHead == COMMAND_REPORT) {
        var params = {};
        params['temperature'] = dataView.getInt32(1); // 对应产品属性中temperature
        //
        jsonMap['topic'] = '/user/update_iot';
        jsonMap['method'] = PROP_REPORT_METHOD;
        jsonMap['params'] = params; //ALink JSON格式,params标准字段。
    } 
    return JSON.stringify(jsonMap);
}

/**
 *  将Alink协议的数据转换为设备能识别的格式数据,物联网平台给设备下发数据时调用
 *  入参:jsonObj Alink JSON对象  不能为空
 *  出参:rawData byte[]数组      不能为空
 *
 *  输入: {"method":"thing.service.property.set","params":{"temperature":258}}
 *  输出: 0x010304027500000102
 *
 */
function protocolToRawData(json) {
    var jsonObject = JSON.parse(json);
    var method = jsonObject['method'];
    var params = jsonObject['params'];
    var temperature = params['temperature'];
    var payloadArray = [];
    if (method == PROP_SET_METHOD) //属性设置
    {
        payloadArray = payloadArray.concat(buffer_uint8(COMMAND_SET)); 
        payloadArray = payloadArray.concat(buffer_int32(temperature)); 
    }
    return Bytes2Str(payloadArray);
}


//十六进制字符串转字节数组
function Str2Bytes(str) 
{ 
    var pos = 0; 
    var len = str.length; 
    if(len %2 != 0) 
    { 
       return null;  
    } 
    len /= 2; 
    var hexA = new Array(); 
    for(var i=0; i<len; i++) 
    { 
       var s = str.substr(pos, 2); 
       var v = parseInt(s, 16); 
       hexA.push(v); 
       pos += 2; 
    } 
    return hexA; 
}

//字节数组转十六进制字符串
function Bytes2Str(arr)
{
    var str = "";
    for(var i=0; i<arr.length; i++)
    {
       var tmp = arr[i].toString(16);
       if(tmp.length == 1)
       {
           tmp = "0" + tmp;
       }
       str += tmp;
    }
    return str;
}

//以下是部分辅助函数。
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);
}

socket.js解析

示例报文

默认协议解析
协议规范:标识头(STR)+长度(4位)+字符串数据 

协议实例
STR0086{"key":"sqlreturn","rows":[{"Column1":"2202"}],"taskid":"3","mac":"82-19-34-61-9C-11"}
STR0002ok

 

 

 

测试示例:

574B4C59100080303030303030303198

 

 

function ProtocolSample(val) {
    var out = '';
    if (1 == val) { 
        out = '574B4C59100080303030303030303198'; 
    }
    else if (2 == val) { 
        out = ''; 
    } 
    return out;
}

function rawDataToProtocol(hex) {  
    var params = {};
    
    var bytes = hex;  
    var head = bytes.substr(0, 8);  if ("" == head || 8 != head.length) { return ResponseJson(params, 1, '', ''); }
    var bagLen = bytes.substr(8, 2); if ("" == bagLen || 2 != bagLen.length) { return ResponseJson(params, 1, '', ''); }
    bagLen = parseInt(bagLen, 16); // 16进制转10进制  
    var board = bytes.substr(10, 2); if ("" == board || 2 != board.length) { return ResponseJson(params, 1, '', ''); }
    var cmd = bytes.substr(12, 2); if ("" == cmd || 2 != cmd.length) { return ResponseJson(params, 1, '', ''); }
    var dataLen = (bagLen - 8) * 2;
    var request = bytes.substr(14, dataLen); if ("" == request || dataLen != request.length) { return ResponseJson(params, 1, '', ''); }
    var index = 14 + dataLen;
    var check = bytes.substr(index, 2); if ("" == check || 2 != check.length) { return ResponseJson(params, 1, '', ''); }
    //
    var bagstatus = 0; index = index + 2;
    var remain = bytes.substr(index, 2); if (0 != remain.length) bagstatus = 2;
    
    params['head'] = head;
    params['bagLen'] = bagLen;
    params['board'] = board;
    params['cmd'] = cmd;
    params['request'] = request;
    params['check'] = check; 
    
    return ResponseJson(params, bagstatus, (index / 2), '', '');
} 

function ResponseJson(params, bagstatus, position, uid, data) {
    var jsonMap = {};
    jsonMap['params'] = params;
    jsonMap['position'] = position;     
    jsonMap['bagstatus'] = bagstatus;// BagNone:0 BagLost:1 BagStick:2
    jsonMap['uid'] = uid; 
    jsonMap['data'] = data;
    return JSON.stringify(jsonMap);
}

function protocolToRawData(json) {
    var jsonObject = JSON.parse(json);
    var method = jsonObject['method'];
    var params = jsonObject['params'];
    var temperature = params['temperature'];
    var payloadArray = [];
    if (method == PROP_SET_METHOD) //属性设置
    {
        payloadArray = payloadArray.concat(buffer_uint8(COMMAND_SET)); 
        payloadArray = payloadArray.concat(buffer_int32(temperature)); 
    }
    return Bytes2Str(payloadArray);
}


//十六进制字符串转字节数组
function Str2Bytes(str) 
{ 
    var pos = 0; 
    var len = str.length; 
    if(len %2 != 0) 
    { 
       return null;  
    } 
    len /= 2; 
    var hexA = new Array(); 
    for(var i=0; i<len; i++) 
    { 
       var s = str.substr(pos, 2); 
       var v = parseInt(s, 16); 
       hexA.push(v); 
       pos += 2; 
    } 
    return hexA; 
}

//字节数组转十六进制字符串
function Bytes2Str(arr)
{
    var str = "";
    for(var i=0; i<arr.length; i++)
    {
       var tmp = arr[i].toString(16);
       if(tmp.length == 1)
       {
           tmp = "0" + tmp;
       }
       str += tmp;
    }
    return str;
}

//以下是部分辅助函数。
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);
}

 

posted @ 2020-09-25 14:43  CHHC  阅读(1754)  评论(0编辑  收藏  举报