| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410 |
- /*
- * 从tcp发来的数据
- * 第一步:解析。
- * 解析tcp数据
- */
- function fromTcp(rawData){
- var obj = {};
- fourfaithHandle(rawData, obj)
- obj.productId = '1dbfd476b7nm2';
- var json = JSON.stringify(obj);
- return json;
- }
- // 四信网关
- function fourfaithHandle(rawData, obj) {
- var rawDataArray = ConvertToUint8Array(rawData);
- // obj.rawDataArray = ArrayToHexString(rawDataArray);
- // obj.rawDataArrayLength = rawDataArray.length
- if (rawDataArray.length == 1 && rawDataArray[0] == 0xFE){
- obj.msg = '网关心跳';
- return ;
- }
- if (rawDataArray[0] == 0x78 && rawDataArray[1] == 0x56){
- obj.msg = '网关连接';
- return ;
- }
- // 校验
- if (!xor_verify(rawDataArray)){
- throw 'API数据异或和校验失败';
- }
- // 获取api协议中的数据
- var dataArray = ConvertToUint8Array(rawData, 6, rawData.length - 1);
- // obj.dataArray = ArrayToHexString(dataArray);
- // obj.dataArrayLength = dataArray.length;
- // 判断新老协议
- if (dataArray[0] == 0xEF){
- analysis_new(dataArray, obj);
- }else {
- analysis_old(dataArray, obj);
- }
- // 获取api协议中的目的地址
- var addressArray = ConvertToUint8Array(rawData, 4, 6);
- if (obj.patientCodeArray){
- obj.responseHex = ArrayToHexString(responseData(addressArray, obj.patientCodeArray));
- delete obj.patientCodeArray
- }
- }
- // 解析报警数据
- function analysis_old_alarm(dataArray, obj) {
- obj.dataType = 5;
- // 报警数据
- var dataView = new DataView(dataArray.buffer, 0);
- // 类型【1】
- var type = dataView.getUint8(1);
- if (type == 0x15){
- obj.pumpType = 1;
- }else if (type == 0x25){
- obj.pumpType = 2;
- }else if (type == 0x35){
- obj.pumpType = 3;
- }else {
- obj.pumpType = 0;
- obj.msg = '泵类型不存在';
- return;
- }
- // 泵号【2-9】
- obj.pumpCode = ArrayToHexString(dataArray, 2, 10);
- // 住院号【10-13】
- if (obj.pumpType == 1 && dataArray.length == 36){
- obj.patientCodeArray = concat(ConvertToUint8Array(dataArray, 10, 14), ConvertToUint8Array(dataArray, 32, 34));
- } else if (obj.pumpType == 2 && dataArray.length == 39){
- obj.patientCodeArray = concat(ConvertToUint8Array(dataArray, 10, 14), ConvertToUint8Array(dataArray, 35, 37));
- } else {
- obj.patientCodeArray = ConvertToUint8Array(dataArray, 10, 14);
- }
- var patientCodeDataView = new DataView(obj.patientCodeArray.buffer);
- var patientCode = patientCodeDataView.getUint32(0, true);
- if (patientCodeDataView.byteLength == 6){
- patientCode += patientCodeDataView.getUint16(4, true) * 1000000000;
- }
- obj.patientCode = patientCode + '';
- // 病区【14-15】
- obj.ward = dataView.getUint16(14, true);
- // 床号【16-17】
- obj.bedNo = dataView.getUint16(16, true);
- // 持续量【18-19】
- obj.continueDose = dataView.getUint16(18, true) * 0.1;
- // 锁定时间【20-21】
- obj.lockTime = dataView.getUint16(20, true);
- // 极限量【22-23】
- obj.ultimateDose = dataView.getUint16(22, true);
- // 首次量【24】
- obj.firstDose = dataView.getUint8(24);
- // 追加量【25-26】
- obj.appendDose = dataView.getUint16(25, true) * 0.1;
- // 报警【29】
- obj.alarm = [];
- obj.forcast = [];
- var alarmByte = dataView.getUint8(29);
- // 未装药盒 9
- if ((alarmByte & 0x03) == 0x03){
- obj.alarm.push(9);
- } else if ((alarmByte & 0x01) == 0x01){
- // 气泡或无液 1
- obj.alarm.push(1);
- } else if ((alarmByte & 0x02) == 0x02){
- // 堵塞 2
- obj.alarm.push(2);
- }
- // 总量 3
- if ((alarmByte & 0x04) == 0x04){
- obj.alarm.push(3);
- }
- // 极限量 4
- if ((alarmByte & 0x08) == 0x08){
- obj.alarm.push(4);
- }
- // 输液将结束 1
- if ((alarmByte & 0x10) == 0x10){
- obj.forcast.push(1);
- }
- // 输液结束 6
- if ((alarmByte & 0x20) == 0x20){
- obj.alarm.push(6);
- }
- // 电量偏低预警 5
- if ((alarmByte & 0x40) == 0x40){
- obj.alarm.push(5);
- }
- // 电量偏低预报 3
- if ((alarmByte & 0x80) == 0x80){
- obj.forcast.push(3);
- }
- var alarmByte30 = dataView.getUint8(30);
- // 点击失控 7
- if ((alarmByte30 & 0x0f) == 0x01){
- obj.alarm.push(7);
- } else if ((alarmByte30 & 0x0f) == 0x02){
- // 机器故障 8
- obj.alarm.push(8);
- }
- // 无报警
- if (obj.alarm.length == 0){
- obj.alarm.push(0);
- }
- // 无预报
- if (obj.forcast.length == 0){
- obj.forcast.push(0);
- }
- // 运行状态【30】
- obj.runStatus = (dataArray[30] & 0xf0) >> 4;
- // 电池电量【31】
- obj.electricity = dataView.getUint8(31);
- // 脉冲泵的属性
- if (obj.pumpType == 2){
- // 首次量锁时【32】
- obj.firstLockTime = dataView.getUint8(32);
- // 脉冲量【33】
- obj.pulseDose = dataView.getUint8(33);
- // 脉冲量锁时【34】
- obj.pulseLockTime = dataView.getUint8(34);
- } else if (obj.pumpType == 3){
- // 智能泵的属性
- // 加档时间【32】
- obj.filingCycle = dataView.getUint8(32) * 0.1;
- // 减档时间【33】
- obj.reductionPeriod = dataView.getUint8(33) * 0.1;
- // 加档PCA有效计数【34】
- obj.addTrueFrequency = dataView.getUint8(34);
- // 自调比例【35】
- obj.customScate = dataView.getUint8(35);
- // 加档上限【36】
- var data36 = dataView.getUint8(36);
- if (data36 > 100){
- obj.upperLimit = (data36 - 128) * 1.0;
- }else {
- obj.upperLimit = data36 * 0.1;
- }
- // 减档下限【37】
- obj.lowerLimit = dataView.getUint8(37) * 0.1;
- }
- }
- // 解析运行数据
- function analysis_old_run(dataArray, obj) {
- obj.dataType = 3;
- // 报警数据
- var dataView = new DataView(dataArray.buffer, 0);
- // 类型【1】
- var type = dataView.getUint8(1);
- if (type == 0x13){
- obj.pumpType = 1;
- }else if (type == 0x23){
- obj.pumpType = 2;
- }else if (type == 0x33){
- obj.pumpType = 3;
- }else {
- obj.pumpType = 0;
- obj.msg = '泵类型不存在';
- return;
- }
- // 住院号【2-5】
- if (obj.pumpType == 1 && dataArray.length == 18){
- obj.patientCodeArray = concat(ConvertToUint8Array(dataArray, 2, 6), ConvertToUint8Array(dataArray, 14, 16));
- } else if (obj.pumpType == 2 && dataArray.length == 18){
- obj.patientCodeArray = concat(ConvertToUint8Array(dataArray, 2, 6), ConvertToUint8Array(dataArray, 14, 16));
- } else {
- obj.patientCodeArray = ConvertToUint8Array(dataArray, 2, 6);
- }
- var patientCodeDataView = new DataView(obj.patientCodeArray.buffer);
- var patientCode = patientCodeDataView.getUint32(0, true);
- if (patientCodeDataView.byteLength == 6){
- patientCode += patientCodeDataView.getUint16(4, true) * 1000000000;
- }
- obj.patientCode = patientCode + '';
- // 总量。【6-7】
- obj.totalDose = dataView.getUint16(6, true);
- // 已输入量【8-9】
- obj.finishDose = dataView.getUint16(8, true) * 0.1;
- // 电池状态【10】
- obj.electricity = dataView.getUint8(10);
- // 有效次数【11】
- obj.validTimes = dataView.getUint8(11);
- // 无效次数【12】
- obj.invalidTimes = dataView.getUint8(12);
- // 预报【13】和报警
- obj.forcast = [];
- obj.alarm = [];
- var warnByte = dataView.getUint8(13);
- // 电量偏低预报 3
- if ((warnByte & 0x04) == 0x04){
- obj.forcast.push(3);
- }
- // 无预报
- if (obj.forcast.length == 0){
- obj.forcast.push(0);
- }
- // 无报警
- if (obj.alarm.length == 0){
- obj.alarm.push(0);
- }
- // 运行状态
- obj.runStatus = 2;
- }
- // 解析老协议
- function analysis_old(dataArray, obj) {
- // crc校验
- if (!crc_verify(dataArray)){
- obj.msg = '老协议数据CRC校验失败';
- return ;
- }
- // 泵数据
- var type = dataArray[1] & 0x0f;
- if (type == 5){
- analysis_old_alarm(dataArray, obj);
- }else if (type == 3){
- analysis_old_run(dataArray, obj);
- }else {
- obj.msg = '数据格式错误';
- }
- }
- // 解析新协议
- function analysis_new(dataArray, obj) {
- // crc校验
- if (!crc_verify(dataArray)){
- obj.msg = '新协议数据CRC校验失败';
- return ;
- }
- // 泵数据
- var dataView = new DataView(dataArray.buffer, 0);
- // 泵类型【2】 高4位
- obj.pumpType = (dataArray[2] & 0xf0) >> 4;
- // 版本【2】 低4位
- obj.version = dataArray[2] & 0x0f;
- // 泵号【3-10】
- obj.pumpCode = ArrayToHexString(dataArray, 3, 11);
- // 输注标识【11】
- obj.infusionId = dataView.getUint8(11);
- // 发送条数【12】
- obj.dataNumber = dataView.getUint8(12);
- // 医院编号【13-14】
- obj.userId = dataView.getUint16(13, true);
- // 住院号【15-20】
- var patientCodeArray = ConvertToUint8Array(dataArray, 15, 21);
- var patientCodeDataView = new DataView(patientCodeArray.buffer);
- var patientCode = patientCodeDataView.getUint32(0, true)
- + patientCodeDataView.getUint16(4, true) * 4294967295;
- obj.patientCode = patientCode + '';
- obj.patientCodeArray = patientCodeArray;
- // 持续量【21-22】
- obj.continueDose = dataView.getUint16(21, true) * 0.1;
- // 锁定时间【23】
- obj.lockTime = dataView.getUint8(23);
- // 极限量【24】
- obj.ultimateDose = dataView.getUint8(24);
- // 首次量【25】
- obj.firstDose = dataView.getUint8(25);
- // 追加量【26】
- obj.appendDose = dataView.getUint8(26) * 0.1;
- // 总量【27-28】
- obj.totalDose = dataView.getUint16(27, true);
- // 已输入量【29-30】
- obj.finishDose = dataView.getUint16(29, true) * 0.1;
- // 有效次数【31】
- obj.validTimes = dataView.getUint8(31);
- // 无效次数【32】
- obj.invalidTimes = dataView.getUint8(32);
- // 报警【33】
- obj.alarm = [];
- obj.forcast = [];
- var alarmByte = dataView.getUint8(33);
- // 气泡或无液 1
- if ((alarmByte & 0x01) == 0x01){
- obj.alarm.push(1);
- }
- // 堵塞 2
- if ((alarmByte & 0x02) == 0x02){
- obj.alarm.push(2);
- }
- // 未装药盒 9
- if ((alarmByte & 0x04) == 0x04){
- obj.alarm.push(9);
- }
- // 极限量 4
- if ((alarmByte & 0x08) == 0x08){
- obj.alarm.push(4);
- }
- // 总量 3
- if ((alarmByte & 0x10) == 0x10){
- obj.alarm.push(3);
- }
- // 输液将结束 1
- if ((alarmByte & 0x20) == 0x20){
- obj.forcast.push(1);
- }
- // 输液结束 6
- if ((alarmByte & 0x40) == 0x40){
- obj.alarm.push(6);
- }
- // 机器故障 8
- if ((alarmByte & 0x80) == 0x80){
- obj.alarm.push(8);
- }
- // 预报 [34]
- var warnByte = dataView.getUint8(34);
- // 电量偏低预警 5
- if ((warnByte & 0x01) == 0x01){
- obj.alarm.push(5);
- }
- // 电量偏低预报 3
- if ((warnByte & 0x02) == 0x02){
- obj.forcast.push(3);
- }
- // 遗忘报警 4
- if ((warnByte & 0x04) == 0x04){
- obj.forcast.push(4);
- }
- // 无报警
- if (obj.alarm.length == 0){
- obj.alarm.push(0);
- }
- // 无预报
- if (obj.forcast.length == 0){
- obj.forcast.push(0);
- }
- // 运行状态【35】
- obj.runStatus = (dataArray[35] & 0xf0) >> 4;
- // 电池电量【36】
- obj.electricity = dataView.getUint8(36);
- // CRC【37-38】不处理
- }
- // 获取返回的字节
- function responseData(dest, patientCodeArray){
- var length = dest.length + patientCodeArray.length + 7;
- var responseBytes = new Uint8Array(length);
- // 帧头
- responseBytes[0] = 0xFE;
- // 长度域
- responseBytes[1] = (dest.length + patientCodeArray.length + 2) & 0xff;
- // 命令域
- responseBytes[2] = 0x24;
- responseBytes[3] = 0x5f;
- // 目的地址
- responseBytes[4] = dest[0];
- responseBytes[5] = dest[1];
- // 固定位。0xAD 0xFC
- responseBytes[6] = 0xad;
- responseBytes[7] = 0xfc;
- // 数据。住院号
- for (var i = 0; i < patientCodeArray.length; i++){
- responseBytes[8 + i] = patientCodeArray[i];
- }
- // 异或和
- var xor_value = xor(responseBytes, 1, length - 1);
- responseBytes[length - 1] = xor_value & 0xff;
- return responseBytes;
- }
|