You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

GenvictBleUtil.js 17KB

1
  1. var a=require('./GenvictDataUtil.js');var b=[];var c='';var d='';var e='';var f='';var g=[];var h=0;var i=3;var j;var k=false;var l=[];var m=[];var n=0;var o=0;var p=0;var q="";var r=0;var s;var t=new Array();var u=0;var v;var w=false;const ARTC_SERVICE_UUID='0000FEE7-0000-1000-8000-00805F9B34FB';const ARTC_WRTIE_UUID='0000FEC7-0000-1000-8000-00805F9B34FB';const ARTC_READ_UUID='0000FEC8-0000-1000-8000-00805F9B34FB';const SUCCESS_CODE=0;const FAILED_CODE=1;const PLOY_SERVICE_FAILE='2';const PLOY_NO_AIM_SERVICE='3';const PLOY_CHARACTERISTIC_FAILE='4';const PLOY_NO_CHARACTERISTIC='5';const PLOY_NOTIY_FAILE='6';const COS_SUCCESS_CODE='9000';function openBle(x){wx.openBluetoothAdapter({success(res){typeof x==='function'&&x(SUCCESS_CODE)},fail(){typeof x==='function'&&x(FAILED_CODE)}})};function closeBle(x){wx.closeBluetoothAdapter({success(res){typeof x==='function'&&x(SUCCESS_CODE)},fail(){typeof x==='function'&&x(FAILED_CODE)}})};function scanBle(x){b=[];wx.startBluetoothDevicesDiscovery({success(res){wx.onBluetoothDeviceFound((res)=>{for(let i=0;i<res.devices.length;i++){let isHave=false;for(let j=0;j<b.length;j++){if(res.devices[i].deviceId===b[j].deviceId){isHave=true;break}};if(!isHave){console.log(res.devices[i].name);if(res.devices[i].name.indexOf("GV__GD_FK")==0||res.devices[i].name.indexOf("GV_HCB_Q1")==0||res.devices[i].name.indexOf("GV__BT_WX")==0||res.devices[i].name.indexOf("GENVICT")==0||res.devices[i].name.indexOf("GXETC_GV")==0||res.devices[i].name.indexOf("zj")==0||res.devices[i].name.indexOf("JY")==0||res.devices[i].name.indexOf("SD_BT_WX")==0||res.devices[i].name.indexOf("GS")==0||res.devices[i].name.indexOf("GV")==0||res.devices[i].name.indexOf("ETC")==0||res.devices[i].name.indexOf("HN")==0){b.push(res.devices[i]);x(res.devices[i])}}}})},fail(){typeof x==='function'&&x(FAILED_CODE)}})};function connectDevice(x,y,z){wx.stopBluetoothDevicesDiscovery({success(res){console.log('停止搜索')}});openBle(function(A){wx.onBLEConnectionStateChange(function(B){if(!B.connected){var C=new Object();C.code=FAILED_CODE;C.data='';C.msg='蓝牙连接已断开';typeof z==='function'&&z(C)}})});c=x.deviceId;wx.createBLEConnection({deviceId:c,success(res){deployBle(y)},fail(){var A=new Object();A.code=FAILED_CODE;A.msg='连接失败';A.data='';typeof y==='function'&&y(A)}})};function deployBle(x){var y=this;e='';f='';wx.getBLEDeviceServices({deviceId:c,success(res){for(let i=0;i<res.services.length;i++){let serviceuuid=res.services[i].uuid;if(serviceuuid===ARTC_SERVICE_UUID){d=serviceuuid;wx.getBLEDeviceCharacteristics({deviceId:c,serviceId:d,success(res){for(let i=0;i<res.characteristics.length;i++){let chauuid=res.characteristics[i].uuid;if(chauuid===ARTC_READ_UUID){f=chauuid}else if(chauuid===ARTC_WRTIE_UUID){e=chauuid}};if(e.length===0||f.length===0){typeof x==='function'&&x(PLOY_NO_CHARACTERISTIC)}else{openReceiveData((code)=>{if(code===SUCCESS_CODE){if(a.protocolType()=="normal"){typeof x==='function'&&x(SUCCESS_CODE)}else{v=x}}else{var A=new Object();A.code=FAILED_CODE;A.msg='连接失败';A.data='';typeof x==='function'&&x(A)}})}},fail(){var A=new Object();A.code=FAILED_CODE;A.msg='连接失败';A.data='';typeof x==='function'&&x(A)}});return}};var z=new Object();z.code=FAILED_CODE;z.msg='连接失败';z.data='';typeof x==='function'&&x(z)},fail(){var z=new Object();z.code=FAILED_CODE;z.msg='连接失败';z.data='';typeof x==='function'&&x(z)}})};function disconnectDevice(x){wx.closeBLEConnection({deviceId:c,success(res){var y=new Object();y.code=SUCCESS_CODE;y.msg='蓝牙已断开';y.data='';x(y)},fail(){var y=new Object();y.code=FAILED_CODE;y.msg='蓝牙连接断开失败';y.data='';x(y)}})};function openReceiveData(x){wx.notifyBLECharacteristicValueChange({deviceId:c,serviceId:d,characteristicId:f,state:true,success(res){typeof x==='function'&&x(SUCCESS_CODE)},fail(){typeof x==='function'&&x(FAILED_CODE)}});wx.onBLECharacteristicValueChange((characteristic)=>{let hex=Array.prototype.map.call(new Uint8Array(characteristic.value),x=>('00'+x.toString(16)).slice(-2)).join('');console.log('接收数据:'+hex);if(a.protocolType()=="normal"){normalDataResponse(hex)}else{let ST=hex.substring(0,2).toUpperCase();if(q.length==0){if(ST=="FE"){r=parseInt(hex.substring(4,8),16)*2}else{return}};q=q+hex;if(q.length==r){let cmdId=q.substring(8,12);if(cmdId=="2711"){authResponse()}else if(cmdId=="2713"){initResponse()}else if(cmdId=="2712"){let frame=q.substring(16);wechatDataResponse(frame)}}else if(q.length>r){q="";r=0}}})};function initResponse(){let bufferArray=a.makeInitResponse();let hasErr=false;w=true;sendData(bufferArray,function(x,y){if(x!=0){hasErr=true}})};function authResponse(){let bufferArray=a.makeAuthResponse();sendData(bufferArray,function(x,y){if(x!=0){typeof s=='function'&&s(FAILED_CODE)}})};function wechatDataResponse(x){q="";r=0;x=x.substring(8,8+parseInt(x.substring(6,8),16)*2);if(t.length==0){if(a.protocolType()=="xinguobiao"){let ctl=parseInt(x.substring(2,6),16);u=ctl-0x8000}else if(a.protocolType()=="genvict"){let ctl=parseInt(x.substring(4,6),16);u=ctl-0x80+1}};t.push(x);if(u==t.length){let completeValue="";for(let i=0;i<t.length;i++){let temp=""+t[i];completeValue+=temp.substring(8,temp.length-2)};t=new Array();u=0;if(parseInt(completeValue.substring(0,2),16)!=0x96){typeof j=='function'&&j(SUCCESS_CODE,completeValue)}}};function normalDataResponse(x){q="";r=0;if(t.length==0){let ctl=parseInt(x.substring(6,8));u=ctl+1};t.push(x);if(u==t.length){let completeValue="";for(let i=0;i<t.length;i++){let temp=""+t[i];completeValue+=temp.substring(10,temp.length-2)};t=new Array();u=0;if(parseInt(completeValue.substring(0,2),16)!=0x96){typeof j=='function'&&j(SUCCESS_CODE,completeValue)}}};function sendData(x,y){var z=this;q='';g=x;h=0;j=y;m=[];l=[];t=new Array();startSendData()};function startSendData(){var y=this;let buffer=g[h];let hex=Array.prototype.map.call(new Uint8Array(buffer),x=>('00'+x.toString(16)).slice(-2)).join('');console.log('发送数据:'+hex);wx.writeBLECharacteristicValue({deviceId:c,serviceId:d,characteristicId:e,value:buffer,success(res){i=3;h++;if(h<g.length){startSendData()}else{if(w){var z=new Object();z.code=SUCCESS_CODE;z.msg='连接成功';z.data='';typeof v=='function'&&v(z);w=false}}},fail(){console.log('发送失败了:')}})};function initDevice(x){var y=this;console.log('接收数据:xxxx');y.sendData(a.initDeviceSendData(),(code,res)=>{if(code===SUCCESS_CODE){let stateCode=res.substring(res.length-4);let reapdu=res.substring(18,res.length-4);if(a.protocolType()=="normal"){console.log('接收数据:'+res);reapdu=res};typeof x==='function'&&x(code,reapdu)}else{typeof x==='function'&&x(FAILED_CODE,res)}})};function deviceChannel(x,y){var z=this;z.sendData(a.makeDeviceChannelSendData(x),(code,res)=>{if(code===SUCCESS_CODE){let stateCode=res.substring(res.length-4);let reapdu=res.substring(18,res.length-4);typeof y==='function'&&y(code,res)}else{typeof y==='function'&&y(FAILED_CODE,res)}})};function authenChannel(x,y,z){var A=this;A.sendData(a.makeAuthenChannelSendData(x,y),(code,res)=>{if(code===SUCCESS_CODE){let stateCode=res.substring(res.length-4);let reapdu=res.substring(18,res.length-4);typeof z==='function'&&z(code,res)}else{typeof z==='function'&&z(FAILED_CODE,res)}})};function selfDeviceChannel(x,y){var z=this;z.sendData(a.makeSelfDeviceChannelSendData(x),(code,res)=>{if(code===SUCCESS_CODE){let stateCode=res.substring(res.length-4);let reapdu=res.substring(18,res.length-4);typeof y==='function'&&y(code,res)}else{typeof y==='function'&&y(FAILED_CODE,res)}})};function deviceAuthenChannel(x,y,z){var A=this;A.sendData(a.makeDeviceAuthenSendData(x,y),(code,res)=>{if(code===SUCCESS_CODE){let stateCode=res.substring(res.length-4);let reapdu=res.substring(18,res.length-4);typeof z==='function'&&z(code,res)}else{typeof z==='function'&&z(FAILED_CODE,res)}})};function cosChannel(x,y,z){var A=this;A.sendData(a.makecosSendData(x,y),(code,res)=>{let stateCode=res.substring(2,4);if(code===SUCCESS_CODE&&stateCode==='00'){let reapdu=res.substring(10,res.length);if(a.protocolType()=="genvict"){var B=tlvUnpackData(reapdu);for(let i=0;i<B.length;i++){let cosCode=B[i].substring(B[i].length-4,B[i].length)};typeof z==='function'&&z(code,B)}else{typeof z==='function'&&z(code,res)}}else{typeof z==='function'&&z(FAILED_CODE,res)}})};function tlvUnpackData(x){let tlvMode=parseInt(x.substring(2,4),16);let cosStr;let tlvLen;var y=new Array();if(tlvMode<=0x80){tlvLen=parseInt(x.substring(2,4),16)*2;cosStr=x.substring(4,x.length)}else if(tlvMode==0x81){tlvLen=parseInt(x.substring(4,6),16)*2;cosStr=x.substring(6,x.length)}else{tlvLen=parseInt(x.substring(4,8),16)*2;cosStr=x.substring(8,x.length)};let i=0;let index=0;let dataLen=0;while(tlvLen>0){dataLen=parseInt(cosStr.substring(2+index,4+index),16)*2;y[i]=cosStr.substring(4+index,dataLen+4+index);index=index+4+dataLen;i++;tlvLen=tlvLen-4-dataLen};return y};function gvEsamChannel(x,y,z){var A=this;A.sendData(a.makeGVEsamSendData(x,y),(code,res)=>{let stateCode=res.substring(2,4);if(code===SUCCESS_CODE&&stateCode==='00'){let reapdu=res.substring(10,res.length);if(a.protocolType()=="genvict"){var B=tlvUnpackData(reapdu);for(let i=0;i<B.length;i++){let cosCode=B[i].substring(B[i].length-4,B[i].length)};typeof z==='function'&&z(code,B)}else{typeof z==='function'&&z(code,res)}}else{typeof z==='function'&&z(FAILED_CODE,res)}})};function transGVEsamCommand(x,y){var z=this;z.sendData(a.makeGVtransEsamSendData('20',x),(code,res)=>{if(code===SUCCESS_CODE){let stateCode=res.substring(res.length-4);let reapdu=res.substring(18,res.length-4);typeof y==='function'&&y(code,res)}else{typeof y==='function'&&y(FAILED_CODE,res)}})};function transCommand(x,y,z){var A=this;A.sendData(a.maketransSendData(x,y),(code,res)=>{if(code===SUCCESS_CODE){let stateCode=res.substring(res.length-4);let reapdu=res.substring(18,res.length-4);typeof z==='function'&&z(code,res)}else{typeof z==='function'&&z(FAILED_CODE,res)}})};function obuChannel(x,y){var z=this;z.sendData(a.makeObuChannel(x),(code,res)=>{if(code===SUCCESS_CODE){let stateCode=res.substring(res.length-4);let reapdu=res.substring(18,res.length-4);typeof y==='function'&&y(code,res)}else{typeof y==='function'&&y(FAILED_CODE,res)}})};function authenC0(x,y){let send="C0"+x;var z=this;z.sendData(a.makeAuthenSendData(send),(code,res)=>{if(code===SUCCESS_CODE){let stateCode=res.substring(res.length-4);let reapdu=res.substring(18,res.length-4);typeof y==='function'&&y(code,res)}else{typeof y==='function'&&y(FAILED_CODE,res)}})};function authenC1(x,y,z,A,B,C,D){let send="C1"+x+y+z+A+B+C;var E=this;E.sendData(a.makeAuthenSendData(send),(code,res)=>{if(code===SUCCESS_CODE){let stateCode=res.substring(res.length-4);let reapdu=res.substring(18,res.length-4);typeof D==='function'&&D(code,res)}else{typeof D==='function'&&D(FAILED_CODE,res)}})};function readCard(x){var y='0';let reapdu='12'};function resetCard(x){var y=this;y.sendData(a.makeResetCard(),(code,res)=>{if(code===SUCCESS_CODE){let stateCode=res.substring(res.length-4);let reapdu=res.substring(18,res.length-4);typeof x==='function'&&x(code,res)}else{typeof x==='function'&&x(FAILED_CODE,res)}})};function getICCInfo(x){var y=this;var z=new Array();z[0]="00A40000021001";z[1]="00B095002B";z[2]="805C000204";var A="00";if(a.protocolType()=="xinguobiao"){A="10"};y.cosChannel(A,z,function(B,C){if(B==0&&C[1].length>=90){var D=new Object();D.file0015=C[1];D.provider=C[1].substring(0,16);D.cardType=C[1].substring(16,18);D.cardVersion=C[1].substring(18,20);D.cardId=C[1].substring(20,40);D.signedDate=C[1].substring(40,48);D.expiredDate=C[1].substring(48,56);D.vehicleNumber=C[1].substring(56,80);D.userType=C[1].substring(80,82);if(D.cardVersion==0x40){D.plateColor=C[1].substring(86,88);D.vehicleModel=C[1].substring(88,90)}else{D.plateColor=C[1].substring(88,90);D.vehicleModel='00'};D.balance=parseInt(C[2].substring(0,8),16);typeof x==='function'&&x(B,D)}else{typeof x==='function'&&x(FAILED_CODE,C)}})};function pinCode(x,y,z,A,B,C){var D=this;var E=new Array();E[0]="00A40000021001";E[1]="00200000"+a.numberToHexString(x.length/2,1,true)+x;E[2]="805000"+y+"0B"+z+a.numberToHexString(A,4,true)+B+"10";console.log("回复数据:"+E);var F="00";if(a.protocolType()=="xinguobiao"){F="10"};D.cosChannel(F,E,function(G,H){if(G==0){var I=new Object();I.balance=H[2].substring(0,8);I.counter=H[2].substring(8,12);I.random=H[2].substring(16,24);I.mac1=H[2].substring(24,32);typeof C==='function'&&C(G,I)}else{typeof C==='function'&&C(FAILED_CODE,H)}})};function loadCreditWrite(x,y,z){var A=this;var B=new Array();B[0]="805200000B"+x+y;var C="00";if(a.protocolType()=="xinguobiao"){C="10"};A.cosChannel(C,B,function(D,E){if(D==0&&E.length==1){typeof z==='function'&&z(D,E)}else{typeof z==='function'&&z(FAILED_CODE,E)}})};function transA1reqData(x,y){var z=this;z.sendData(a.makeTransA1reqData(x),(code,res)=>{if(code===SUCCESS_CODE){let stateCode=res.substring(res.length-4);let reapdu=res.substring(10,res.length-4);let credit_length=0;if(reapdu.length>14){credit_length=parseInt(res.substring(12,14),16)*2}else{typeof y==='function'&&y(code,"返回不正常包。")};let sign_length=0;if(reapdu.length>4+credit_length){sign_length=parseInt(res.substring(16+credit_length,16+credit_length+2),16)*2}else{typeof y==='function'&&y(code,"返回不正常包。")};if(res.length>16+credit_length+sign_length){var A=new Object();A.original=res.substring(14,14+credit_length);A.sign=res.substring(18+credit_length,18+credit_length+sign_length);typeof y==='function'&&y(code,A)}else{typeof y==='function'&&y(code,"返回不正常包。")}}else{typeof y==='function'&&y(FAILED_CODE,res)}})};function loadCreditT0reqData(x,y){var z=this;z.sendData(a.makeLoadCreditT0reqData(x),(code,res)=>{if(code===SUCCESS_CODE){let stateCode=res.substring(res.length-4);let reapdu=res.substring(10,res.length-4);let credit_length=0;if(reapdu.length>14){credit_length=parseInt(res.substring(12,14),16)*2}else{typeof y==='function'&&y(code,"返回不正常包。")};let sign_length=0;if(reapdu.length>4+credit_length){sign_length=parseInt(res.substring(16+credit_length,16+credit_length+2),16)*2}else{typeof y==='function'&&y(code,"返回不正常包。")};if(res.length>16+credit_length+sign_length){var A=new Object();A.original=res.substring(14,14+credit_length);A.sign=res.substring(18+credit_length,18+credit_length+sign_length);typeof y==='function'&&y(code,A)}else{typeof y==='function'&&y(code,"返回不正常包。")}}else{typeof y==='function'&&y(FAILED_CODE,res)}})};function esamMiWen(x,y){var z=this;z.sendData(a.makeEsamMiWen(x),(code,res)=>{if(code===SUCCESS_CODE){let stateCode=res.substring(res.length-4);let reapdu=res.substring(10,res.length-4);let credit_length=0;if(res.length>14){credit_length=parseInt(res.substring(12,14),16)*2}else{typeof y==='function'&&y(code,"返回不正常包。")};if(res.length>12+credit_length){var A=new Object();A.original=res.substring(14,14+credit_length);typeof y==='function'&&y(code,A)}else{typeof y==='function'&&y(code,"返回不正常包。")}}else{typeof y==='function'&&y(FAILED_CODE,res)}})};function initiateOBU(x){var y=this;console.log("initiateOBU");var z=new Array();z[0]="00A40000023F00";z[1]="00B0810063";y.gvEsamChannel('20',z,function(A,B){if(A===SUCCESS_CODE){console.log("回复数据:"+B);let initiateState=B[1].substring(52,52+2);console.log("initiateState:"+initiateState);if(initiateState==='02'){var C=new Array();C[0]="0059000001";y.gvEsamChannel('20',C,function(D,E){typeof x==='function'&&x(D,E)});console.log("状态1")}else if(initiateState==='01'){typeof x==='function'&&x(FAILED_CODE,"OBU已激活。");console.log("状态2")}else if(initiateState==='00'){typeof x==='function'&&x(FAILED_CODE,"OBU已失效。");console.log("状态3")}}})};function transCmd(x,y,z){var A=y;if(y=='10'){A='00'};sendData(a.makeGVEsamSendData(A,x),(code,res)=>{let stateCode=res.substring(2,4);console.log("res.substring(2, 4):"+res);console.log("res.substring(2, 4):"+res.substring(2,4));if(code===SUCCESS_CODE&&stateCode==='00'){let reapdu=res.substring(10,res.length);console.log("reapdu:"+reapdu);if(a.protocolType()=="genvict"){var B=tlvUnpackData(reapdu);var C=new Object();C.code=SUCCESS_CODE;C.msg='操作成功';C.data=B;typeof z==='function'&&z(C)}}else{var B=new Object();B.code=FAILED_CODE;B.msg='操作失败';B.data=res;typeof z==='function'&&z(B)}})};module.exports={sendData:sendData,openBle:openBle,closeBle:closeBle,scanBle:scanBle,connectDevice:connectDevice,deployBle:deployBle,disconnectDevice:disconnectDevice,initDevice:initDevice,deviceChannel:deviceChannel,selfDeviceChannel:selfDeviceChannel,cosChannel:cosChannel,authenC0:authenC0,authenC1:authenC1,openReceiveData:openReceiveData,deviceAuthenChannel:deviceAuthenChannel,gvEsamChannel:gvEsamChannel,transGVEsamCommand:transGVEsamCommand,transCommand:transCommand,resetCard:resetCard,getICCInfo:getICCInfo,pinCode:pinCode,loadCreditWrite:loadCreditWrite,authenChannel:authenChannel,transA1reqData:transA1reqData,loadCreditT0reqData:loadCreditT0reqData,esamMiWen:esamMiWen,obuChannel:obuChannel,initiateOBU:initiateOBU,transCmd:transCmd};