39 lines
27 KiB
JSON
Executable File
39 lines
27 KiB
JSON
Executable File
[
|
||
{
|
||
"id": "62995ba4dbf51a5ec41d5f7b",
|
||
"uid": "1",
|
||
"name": "自定义表计协议",
|
||
"desc": "用于燃气表的协议",
|
||
"createAt": 1654217636597
|
||
},
|
||
{
|
||
"id": "628ceb14addfdb2a3b4b5727",
|
||
"uid": "1",
|
||
"name": "奇特HTTP标准协议",
|
||
"desc": "奇特HTTP标准协议转换器",
|
||
"createAt": 1653402388275
|
||
},
|
||
{
|
||
"id": "6260396d67aced2696184053",
|
||
"uid": "1",
|
||
"name": "奇特MQTT标准协议",
|
||
"desc": "奇特MQTT标准协议转换器",
|
||
"createAt": 1650473325173
|
||
},
|
||
{
|
||
"id": "d7e84930-5460-4638-aa3f-e0c2015628f4",
|
||
"uid": "1",
|
||
"name": "WS标准协议",
|
||
"desc": "WS标准协议转换器",
|
||
"createAt": 1650473325173
|
||
},
|
||
{
|
||
"id": "50b0fdd0-9bc8-448d-a7cf-d3b1b4df944c",
|
||
"createAt": null,
|
||
"desc": null,
|
||
"name": "铱塔智联-NB透传单泵",
|
||
"script": "var mid = 1;\n\nfunction getMid() {\n mid++;\n if (mid > 10000) {\n mid = 1;\n }\n return mid + \"\";\n}\n//上行数据\nthis.decode = function (msg) {\n var content = msg.content;\n var topic = content.topic;\n var bytes = CRC.strToByte(content.payload);\n\n if (topic.endsWith(\"/thing/model/up_raw\")) {\n var byteData = ab2hex(bytes)\n var data = arrayGroup(byteData, 2);\n var params = {};\n var uint8Array = new Uint8Array(bytes.length);\n for (var i = 0; i < bytes.length; i++) {\n uint8Array[i] = bytes[i] & 0xff;\n }\n if (data[1] == '03') {\n if (byteData.length == 22 ) {\n params['Switch'] = Number(parseInt(data[3] + data[4], 16))\n params['HandMode'] = Number(parseInt(data[5] + data[6], 16))+1\n params['TempGear'] = Number(data[7].slice(1,2))+1\n params['RatioGear'] = Number(data[7].slice(0,1))+1\n params['PressureGear'] = data[8].slice(0,1) != 'f' ? Number(data[8].slice(0,1))+1 : 0\n params['SpeedGear'] = data[8].slice(1,2) != 'f' ? Number(data[8].slice(1,2))+1 : 0\n\n } else if (byteData.length == 126) {\n params['Pressure'] = Number((parseInt(data[3] + data[4], 16) / 100).toFixed(1))\n params['EmtyRunPressure'] = Number((parseInt(data[5] + data[6], 16) / 100).toFixed(1))\n params['StartPressure'] = Number((parseInt(data[7] + data[8], 16) / 100).toFixed(1))\n params['WorkMode'] = Number((parseInt(data[9] + data[10], 16)).toFixed(0))\n params['ClearIceEn'] = Number((parseInt(data[11] + data[12], 16)).toFixed(0))\n params['WaterT'] = Number((parseInt(data[13] + data[14], 16)).toFixed(0))\n params['WaterTReset'] = Number((parseInt(data[15] + data[16], 16)).toFixed(0))\n params['SensorMode'] = Number((parseInt(data[17] + data[18], 16)).toFixed(0))\n params['SensorGroup'] = Number((parseInt(data[19] + data[20], 16)).toFixed(0))\n params['ElectronicTMax'] = Number(parseInt(data[59], 16))\n params['ElectronicTMaxReset'] = Number(parseInt(data[60], 16))\n } else if (byteData.length == 26) {\n params['ActiveTime'] = String(parseInt(data[3], 16))+'-'+String(parseInt(data[4], 16))+'-'+String(parseInt(data[5], 16))\n }\telse if (byteData.length == 190) {\n params['Pressure'] = Number((parseInt(data[3] + data[4], 16) / 100).toFixed(1))\n params['EmtyRunPressure'] = Number((parseInt(data[5] + data[6], 16) / 100).toFixed(1))\n params['StartPressure'] = Number((parseInt(data[7] + data[8], 16) / 100).toFixed(1))\n params['WorkMode'] = Number((parseInt(data[9] + data[10], 16)).toFixed(0))\n params['ClearIceEn'] = Number((parseInt(data[11] + data[12], 16)).toFixed(0))\n params['WaterT'] = Number((parseInt(data[13] + data[14], 16)).toFixed(0))\n params['WaterTReset'] = Number((parseInt(data[15] + data[16], 16)).toFixed(0))\n params['SensorMode'] = Number((parseInt(data[17] + data[18], 16)).toFixed(0))\n params['SensorGroup'] = Number((parseInt(data[19] + data[20], 16)).toFixed(0))\n params['ElectronicTMax'] = Number(parseInt(data[59], 16))\n params['ElectronicTMaxReset'] = Number(parseInt(data[60], 16))\n params['SceneMode'] = Number((parseInt(data[63] + data[64], 16)).toFixed(0))\n params['EnergyModeTime'] = Number((parseInt(data[65] + data[66], 16)).toFixed(0))\n params['WaterTime'] = String(Number((parseInt(data[68].substring(0, 1), 16)).toFixed(0))) + ',' + String(Number((parseInt(data[68].substring(1), 16)).toFixed(0)))\n params['HotWaterTime'] = Number((parseInt(data[69] + data[70], 16)).toFixed(0))\n params['TempSet'] = String(Number((parseInt(data[71], 16)).toFixed(0)))+','+ String(Number((parseInt(data[72], 16)).toFixed(0)))\n params['TimeModeSet'] = parseInt(data[73] + data[74], 16)+'-'+parseInt(data[75] + data[76], 16)+','+ parseInt(data[77] + data[78], 16)+'-'+parseInt(data[79] + data[80], 16)+','+ parseInt(data[81] + data[82], 16)+'-'+parseInt(data[83] + data[84], 16)+','+ parseInt(data[85] + data[86], 16)+'-'+parseInt(data[87] + data[88], 16)+','+ parseInt(data[89] + data[90], 16)+'-'+parseInt(data[91] + data[92], 16)\n\n } else if (byteData.length == 94) {\n let timeStamp = parseInt(String(data[41])+String(data[42])+String(data[43])+String(data[44]),16)\n let time = timestampToTime(timeStamp)\n params['ActiveTime'] = String(parseInt(data[3], 16))+'-'+String(parseInt(data[4], 16))+'-'+String(parseInt(data[5], 16))\n params['Time'] = time + '-'+ timeStamp\n\n }\n } else if (data[1] == '04') {\n if (byteData.length == 54) {\n params['ErrorMsg'] = Number(parseInt(data[3]+data[4], 16))\n params['Voltage'] = Number(parseInt(data[5]+data[6], 16))\n params['Electric'] = Number(parseInt(data[7]+data[8], 16))/10\n params['Power'] = Number(parseInt(data[9]+data[10], 16))\n params['Speed'] = Number(parseInt(data[11]+data[12], 16))\n params['CurrentPressure1'] = data[13] != 'ff' ? Number((parseInt(data[13]+data[14], 16)/100).toFixed(1)) : 0\n params['CurrentPressure2'] = data[15] != 'ff' ? Number((parseInt(data[15]+data[16], 16)/100).toFixed(1)) : 0\n params['IpmTemperature'] = Number(parseInt(data[17]+data[18], 16))-55\n params['MotorTemperature'] = Number(parseInt(data[19]+data[20], 16))-55\n params['WaterTemperature'] = Number(parseInt(data[21]+data[22], 16))-55\n let warnArray = reverseStr(hex2bin(data[23] + data[24]))\n let warnInfo = \"\"\n for (let i = 0; i < warnArray.length; i++) {\n if (warnArray[i] === \"1\") {\n if (warnInfo === \"\") {\n warnInfo = DEVICE_ERROR[i]\n } else {\n warnInfo += `、${DEVICE_ERROR[i]}`\n }\n }\n }\n\n params['WarnInfo'] = warnInfo\n\n } else if (byteData.length == 18) {\n if (String(byteData) == '130404000800004847') {\n params['query'] = String(byteData)\n } else if (String(byteData).slice(9,10) == 3) {\n params['query'] = String(byteData)\n } else if (String(byteData).slice(9,10) == 4) {\n params['query'] = String(byteData)\n }\n } else if (byteData.length == 30) {\n params['McuVersion'] = String(Number(parseInt(data[3].slice(1,2), 16)))+'.'+String(Number(parseInt(data[4].slice(0,1), 16)))+'.'+String(Number(parseInt(data[4].slice(1,2), 16)))\n params['McuStatus'] = Number(parseInt(data[3].slice(0,1), 16))\n params['Scene'] = Number(parseInt(data[9]+data[10], 16))\n params['Model'] = String(Number(parseInt(data[11]+data[12], 16)))+'W'\n\n } else if (byteData.length == 66) {\n params['ErrorMsg'] = Number(parseInt(data[3]+data[4], 16))\n params['Voltage'] = Number(parseInt(data[5]+data[6], 16))\n params['Electric'] = Number(parseInt(data[7]+data[8], 16))/10\n params['Power'] = Number(parseInt(data[9]+data[10], 16))\n params['Speed'] = Number(parseInt(data[11]+data[12], 16))\n params['CurrentPressure1'] = data[13] != 'ff' ? Number((parseInt(data[13]+data[14], 16)/100).toFixed(1)) : 0\n params['CurrentPressure2'] = data[15] != 'ff' ? Number((parseInt(data[15]+data[16], 16)/100).toFixed(1)) : 0\n params['IpmTemperature'] = Number(parseInt(data[17]+data[18], 16))-55\n params['MotorTemperature'] = Number(parseInt(data[19]+data[20], 16))-55\n params['WaterTemperature'] = Number(parseInt(data[21]+data[22], 16))-55\n let warnArray = reverseStr(hex2bin(data[23] + data[24]))\n let warnInfo = \"\"\n for (let i = 0; i < warnArray.length; i++) {\n if (warnArray[i] === \"1\") {\n if (warnInfo === \"\") {\n warnInfo = DEVICE_ERROR[i]\n } else {\n warnInfo += `、${DEVICE_ERROR[i]}`\n }\n }\n }\n params['CountDown'] = Number(parseInt(data[25]+data[26], 16))\n params['WarnInfo'] = warnInfo\n params['Enabled'] = Number(parseInt(data[29]+data[30], 16))\n\n }\n }\n params['History'] = byteData\n //属性上报\n return {\n mid: msg.mid,\n productKey: msg.productKey,\n deviceName: msg.deviceName,\n type: \"property\",\n identifier: \"report\", //属性上报\n occur: new Date().getTime(), //时间戳,设备上的事件或数据产生的本地时间\n time: new Date().getTime(), //时间戳,消息上报时间\n data: params,\n };\n } else if (topic.indexOf(\"/event/\") > 0) {\n //事件上报\n } else if (topic.endsWith(\"/service/property/set_reply\")) {\n //属性设置回复\n } else if (topic.endsWith(\"/config/set_reply\")) {\n //设备配置设置回复\n } else if (topic.endsWith(\"/config/get\")) {\n //设备配置获取\n } else if (topic.endsWith(\"_reply\")) {\n //服务回复\n }\n return null;\n};\n\n//下行数据\nthis.encode = function (service, device) {\n var deviceMid = getMid();\n var method = \"thing.service.\";\n var topic = \"/sys/\" + service.productKey + \"/\" + service.deviceName + \"/thing/model/down_raw\";\n var params = {};\n\n var type = service.type;\n var identifier = service.identifier;\n if (type == \"property\" && identifier == \"get\") {\n var listParams = []\n for (var p in service.params) {\n listParams.push(service.params[p]);\n }\n return {\n productKey: service.productKey,\n deviceName: service.deviceName,\n mid: deviceMid,\n content: {\n topic: topic,\n payload: JSON.stringify({\n id: deviceMid,\n method: method += \"property.\" + identifier,\n params: listParams\n })\n }\n }\n } else if (type == \"property\" && identifier == \"set\") {\n for (var p in service.params) {\n params[p] = service.params[p];\n }\n //站地址\n if (paramsArr.includes('query')) {\n let queryHexData = arrayGroup(params['query'],2)\n let queryData = []\n queryHexData.map(function(value) {\n queryData.push(parseInt(value,16))\n })\n payloadArray = queryData\n } else {\n const stationAddress = \"49\";\n totalArray.push(stationAddress)\n //功能码\n var functionCode = \"\";\n // 寄存器数量\n var register = ''\n var baseVal = ''\n //起始地址\n var startAddressCode = '';\n if (paramsArr.length == 1) {\n functionCode = '06'\n totalArray.push(functionCode)\n } else {\n functionCode = '10'\n paramsArr = ['Pressure', 'EmtyRunPressure', 'StartPressure', 'WorkMode', 'ClearIceEn', 'WaterT', 'WaterTReset']\n totalArray.push(parseInt(Number(functionCode),16))\n }\n if (paramsArr.includes('Switch')) {\n startAddressCode = 300\n totalArray.push('01','44')\n } else if (paramsArr.includes('Pressure')) {\n startAddressCode = 400\n totalArray.push('01','144')\n register = '00' + add0(String(paramsArr.length),2) + add0(String((paramsArr.length*2).toString(16)),2)\n totalArray.push('00', add0(paramsArr.length,2), add0(String((paramsArr.length)*2),2))\n }\n if (functionCode == 6) {\n paramsArr.forEach(function(value){\n totalArray.push('00',params[value])\n itemArray = pad(params[value].toString(16),4)\n deviceArray.push(itemArray[0],itemArray[1])\n })\n } else if (functionCode == 10) {\n paramsArr.forEach(function(value, index){\n if (index < 3) {\n // if (params[value]*100 < 256) {\n // totalItemArray = ['00', String((params[value]*100).toFixed(0))]\n // itemArray = pad(String((params[value]*100).toString(16)),4)\n // totalItemArray = pad(String((params[value]*100).toFixed(0)),4)\n // } else {\n // outFFIndex = ((Number(params[value]*100))/256).toFixed(0)\n // totalItemArray = [outFFIndex, params[value]*100-outFFIndex*256]\n // }\n\n itemArray = pad(Number((params[value]*100).toFixed(0)).toString(16),4)\n totalItemArray = pad(String((params[value]*100).toFixed(0)),4)\n } else {\n itemArray = pad(String(params[value].toString(16)),4)\n totalItemArray = pad(String(params[value]),4)\n }\n // totalArray.push(params[value])\n // totalArray.push(totalItemArray[0],totalItemArray[1])\n totalArray.push(parseInt(itemArray[0],16),parseInt(itemArray[1],16))\n // deviceArray.push(parseInt(itemArray[0],16),parseInt(itemArray[1],16))\n deviceArray.push(itemArray[0],itemArray[1])\n\n })\n }\n\n var startAddressHex = pad(startAddressCode.toString(16), 4);\n baseVal = String(stationAddress) + String(functionCode) + String(startAddressHex[0]) + String(startAddressHex[1])\n if (functionCode != 6) {\n baseVal += register\n }\n deviceArray.forEach(function(item) {\n baseVal += item\n })\n // baseVal += '00070e0064000a00640001000100370037'\n var Crc = CRC.ToModbusCRC16(baseVal)\n var crcArray = arrayGroup(Crc,2)\n totalArray.push(parseInt(crcArray[0],16),parseInt(crcArray[1],16))\n payloadArray = totalArray;\n }\n // var params = json['params'];\n // var prop_float = params['prop_float'];\n // var prop_int16 = params['prop_int16'];\n // var prop_bool = params['prop_bool'];\n // //按照自定义协议格式拼接 rawData。\n // payloadArray = payloadArray.concat(buffer_uint8(COMMAND_SET)); //command字段。\n // payloadArray = payloadArray.concat(buffer_int32(parseInt(id))); //ALink JSON格式 'id'。\n // payloadArray = payloadArray.concat(buffer_int16(prop_int16)); //属性'prop_int16'的值。\n // payloadArray = payloadArray.concat(buffer_uint8(prop_bool)); //属性'prop_bool'的值。\n // payloadArray = payloadArray.concat(buffer_float32(prop_float)); //属性'prop_float'的值。\n\n }else if (method == 'thing.event.property.post') { //设备上报数据返回结果,如果不需要回复,可以去除该内容\n var code = json['code'];\n payloadArray = payloadArray.concat(buffer_uint8(COMMAND_REPORT_REPLY)); //command字段\n payloadArray = payloadArray.concat(buffer_int32(parseInt(id))); // ALink JSON格式 'id'\n payloadArray = payloadArray.concat(buffer_uint8(code));\n } else { //未知命令,对于有些命令不做处理\n var code = json['code'];\n payloadArray = payloadArray.concat(buffer_uint8(COMMAD_UNKOWN)); //command字段\n payloadArray = payloadArray.concat(buffer_int32(parseInt(id))); // ALink JSON格式 'id'\n payloadArray = payloadArray.concat(buffer_uint8(code));\n }\n return {\n productKey: service.productKey,\n deviceName: service.deviceName,\n mid: deviceMid,\n content: {\n topic: topic,\n payload: JSON.stringify({\n id: deviceMid,\n method: method += \"property.\" + identifier,\n params: payloadArray\n })\n }\n }\n};\n\n\nconst DEVICE_ERROR = [\n \"压力传感器故障\", //0\n \"高温限功率\", //1\n \"渗漏\", // 2\n \"电机温度传感器故障\", //3\n \"水温传感器故障\", //4\n \"保留\", //5\n \"保留\", //6\n \"保留\", //7\n \"保留\", //8\n \"保留\", //9\n \"保留\", //10\n \"保留\", //11\n \"保留\", //12\n \"保留\", //13\n \"保留\", //14\n \"保留\", // 15\n \"保留\", //16\n]\nfunction buffer_uint8(value) {\n var uint8Array = new Uint8Array(1);\n var dv = new DataView(uint8Array.buffer, 0);\n dv.setUint8(0, value);\n return [].slice.call(uint8Array);\n}\nfunction buffer_int16(value) {\n var uint8Array = new Uint8Array(2);\n var dv = new DataView(uint8Array.buffer, 0);\n dv.setInt16(0, value);\n return [].slice.call(uint8Array);\n}\nfunction buffer_int32(value) {\n var uint8Array = new Uint8Array(4);\n var dv = new DataView(uint8Array.buffer, 0);\n dv.setInt32(0, value);\n return [].slice.call(uint8Array);\n}\nfunction buffer_float32(value) {\n var uint8Array = new Uint8Array(4);\n var dv = new DataView(uint8Array.buffer, 0);\n dv.setFloat32(0, value);\n return [].slice.call(uint8Array);\n}\n\nfunction ab2hex(buffer) {\n var hexArr = Array.prototype.map.call(\n new Uint8Array(buffer),\n function (bit) {\n return ('00' + bit.toString(16)).slice(-2)\n }\n )\n return hexArr.join('');\n}\n\nfunction arrayGroup(ss, step) {\n var r = [];\n\n function doGroup(s) {\n if (!s) return;\n r.push(s.substr(0, step));\n s = s.substr(step);\n doGroup(s);\n }\n doGroup(ss);\n return r;\n}\n\nvar CRC = {};\n\nCRC._auchCRCHi = [\n 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,\n 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,\n 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,\n 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,\n 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,\n 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,\n 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,\n 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,\n 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,\n 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,\n 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,\n 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,\n 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,\n 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,\n 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,\n 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,\n 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,\n 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,\n 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,\n 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,\n 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,\n 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,\n 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,\n 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,\n 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,\n 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40\n];\nCRC._auchCRCLo = [\n 0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06,\n 0x07, 0xC7, 0x05, 0xC5, 0xC4, 0x04, 0xCC, 0x0C, 0x0D, 0xCD,\n 0x0F, 0xCF, 0xCE, 0x0E, 0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09,\n 0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9, 0x1B, 0xDB, 0xDA, 0x1A,\n 0x1E, 0xDE, 0xDF, 0x1F, 0xDD, 0x1D, 0x1C, 0xDC, 0x14, 0xD4,\n 0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6, 0xD2, 0x12, 0x13, 0xD3,\n 0x11, 0xD1, 0xD0, 0x10, 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3,\n 0xF2, 0x32, 0x36, 0xF6, 0xF7, 0x37, 0xF5, 0x35, 0x34, 0xF4,\n 0x3C, 0xFC, 0xFD, 0x3D, 0xFF, 0x3F, 0x3E, 0xFE, 0xFA, 0x3A,\n 0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38, 0x28, 0xE8, 0xE9, 0x29,\n 0xEB, 0x2B, 0x2A, 0xEA, 0xEE, 0x2E, 0x2F, 0xEF, 0x2D, 0xED,\n 0xEC, 0x2C, 0xE4, 0x24, 0x25, 0xE5, 0x27, 0xE7, 0xE6, 0x26,\n 0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21, 0x20, 0xE0, 0xA0, 0x60,\n 0x61, 0xA1, 0x63, 0xA3, 0xA2, 0x62, 0x66, 0xA6, 0xA7, 0x67,\n 0xA5, 0x65, 0x64, 0xA4, 0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F,\n 0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB, 0x69, 0xA9, 0xA8, 0x68,\n 0x78, 0xB8, 0xB9, 0x79, 0xBB, 0x7B, 0x7A, 0xBA, 0xBE, 0x7E,\n 0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4, 0x74, 0x75, 0xB5,\n 0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71,\n 0x70, 0xB0, 0x50, 0x90, 0x91, 0x51, 0x93, 0x53, 0x52, 0x92,\n 0x96, 0x56, 0x57, 0x97, 0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C,\n 0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E, 0x5A, 0x9A, 0x9B, 0x5B,\n 0x99, 0x59, 0x58, 0x98, 0x88, 0x48, 0x49, 0x89, 0x4B, 0x8B,\n 0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C,\n 0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42,\n 0x43, 0x83, 0x41, 0x81, 0x80, 0x40\n];\n\nCRC.CRC16 = function (buffer) {\n var hi = 0xff;\n var lo = 0xff;\n for (var i = 0; i < buffer.length; i++) {\n var idx = hi ^ buffer[i];\n hi = (lo ^ CRC._auchCRCHi[idx]);\n lo = CRC._auchCRCLo[idx];\n }\n return CRC.padLeft((hi << 8 | lo).toString(16).toUpperCase(), 4, '0');\n};\n\nCRC.isArray = function (arr) {\n return Object.prototype.toString.call(arr) === '[object Array]';\n};\n\nCRC.ToCRC16 = function (str) {\n return CRC.CRC16(CRC.isArray(str) ? str : CRC.strToByte(str));\n};\n\nCRC.ToModbusCRC16 = function (str) {\n return CRC.CRC16(CRC.isArray(str) ? str : CRC.strToHex(str));\n};\n\nCRC.strToByte = function (str) {\n var tmp = str.split(''),\n arr = [];\n for (var i = 0, c = tmp.length; i < c; i++) {\n var j = encodeURI(tmp[i]);\n if (j.length == 1) {\n arr.push(j.charCodeAt());\n } else {\n var b = j.split('%');\n for (var m = 1; m < b.length; m++) {\n arr.push(parseInt('0x' + b[m]));\n }\n }\n }\n return arr;\n};\n\nCRC.convertChinese = function (str) {\n var tmp = str.split(''),\n arr = [];\n for (var i = 0, c = tmp.length; i < c; i++) {\n var s = tmp[i].charCodeAt();\n if (s <= 0 || s >= 127) {\n arr.push(s.toString(16));\n } else {\n arr.push(tmp[i]);\n }\n }\n return arr;\n};\n\nCRC.filterChinese = function (str) {\n var tmp = str.split(''),\n arr = [];\n for (var i = 0, c = tmp.length; i < c; i++) {\n var s = tmp[i].charCodeAt();\n if (s > 0 && s < 127) {\n arr.push(tmp[i]);\n }\n }\n return arr;\n};\n\nCRC.strToHex = function (hex, isFilterChinese) {\n hex = isFilterChinese ? CRC.filterChinese(hex).join('') : CRC.convertChinese(hex).join('');\n\n //清除所有空格\n hex = hex.replace(/\\s/g, \"\");\n //若字符个数为奇数,补一个空格\n hex += hex.length % 2 != 0 ? \" \" : \"\";\n\n var c = hex.length / 2,\n arr = [];\n for (var i = 0; i < c; i++) {\n arr.push(parseInt(hex.substr(i * 2, 2), 16));\n }\n return arr;\n};\n\nCRC.padLeft = function (s, w, pc) {\n if (pc == undefined) {\n pc = '0';\n }\n for (var i = 0, c = w - s.length; i < c; i++) {\n s = pc + s;\n }\n return s;\n};\n\nfunction str2ab(str) {\n var buffer = new ArrayBuffer(str.length / 2); // 2 bytes for each char\n var dataView = new DataView(buffer);\n var str = str.split(\"\")\n var n = 0;\n for (var i = 0; i < str.length; i = i + 2) {\n dataView.setUint8(n, `0x${str[i]}${str[i+1]}`)\n n++;\n }\n return buffer;\n}\n\nfunction pad(num, n, flag = false) {\n var len = num.toString().length;\n while (len < n) {\n num = \"0\" + num;\n len++;\n }\n\n const arr = arrayGroup(num, 2)\n //高低位互换\n if (arr[1] == '00') {\n flag = true\n }\n return flag ? [arr[1], arr[0]] : [arr[0], arr[1]];\n}\n\nfunction add0(num,n) {\n let len = num.length\n while(len < n){\n num = '0'+num\n len++\n }\n if (len > n) {\n num = String(num).slice(1)\n }\n //console.log(num)\n return num\n}\n\nfunction hex2bin(hex, len) {\n var bin = parseInt(hex, 16).toString(2);\n var l = bin.length;\n for (var i = 0; i < len - l; i++) {\n bin = \"0\" + bin;\n }\n return bin;\n}\n\n//翻转字符串并切割成数组\nfunction reverseStr(str) {\n var arr = str.split(\"\");\n arr.reverse();\n return arr;\n}\n\nfunction timestampToTime(timestamp) {\n var date = new Date(timestamp * 1000);//时间戳为10位需*1000,时间戳为13位的话不需乘1000\n var Y = date.getFullYear() + '-';\n var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';\n var D = date.getDate() + ' ';\n var h = date.getHours() + ':';\n var m = date.getMinutes() + ':';\n var s = date.getSeconds();\n return Y+M+D+h+m+s;\n}",
|
||
"typ": "js",
|
||
"uid": null
|
||
}
|
||
] |