123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506 |
- var TAG = 'JLDeviceWechar+33Protocol';
- var onfire = require('./onfire');
- var request33 = require('./JLObuWechat33Pack.js');
- var recive33 = require('./JLObuWecha33UnPackt.js');
- var config = require('./JLConfig');
- var ble = require('./JLAlipayBleManager.js');
- var dataTool = require('./dataTool');
- var revc_count = 0;
- var sendTimeOut = 10000;
- var connectTimeOut = 30000;
- var wechat_arr = [];
- var dataArray = [];
- var isConnect = false;
- var frame_total = 1;
- var frame_count = 1;
- var fireFlag = '';
- var timeId;
- var receive_data = '';
- var receive_data_total_len = '';
- var wechat1 = false;
- var wechat2 = false;
- var timeId;
- var cmd_count = 0;
- var all_cmdArray = [];
- var flag = 0;
- var result_arr = [];
- var errMsg_arr = [];
- export function scanDevice(nameFlag, timeOut, callBack) {
- timeId = setTimeout((res) => {
- ble.scan_stopScanBleDevice((res) => {
- callBack.call(this, {
- code: config.timeOutCode(),
- err_msg: "扫描超时",
- data: null,
- msg: ''
- })
- })
- }, timeOut);
- ble.scan_stopScanBleDevice((res) => {
- ble.scan_StartScanBleDevice(nameFlag, (res) => {
- clearTimeout(timeId);
- console.log('返回上层数据:' + JSON.stringify(res));
- callBack.call(this, res)
- })
- })
- }
- export function stopScanDevice(callBack) {
- ble.scan_stopScanBleDevice((res) => {
- callBack.call(this, res)
- })
- }
- export function connectDevice(device, callBack) {
- console.log('==========connectDevice_Start==========:' + JSON.stringify(device));
- config.message.api = 'connectDevice';
- receive_data = '';
- if (device == null || device == 'undefined') {
- callBack.call(this, {
- code: 10003,
- err_msg: '参数错误',
- data: null,
- msg: ''
- });
- return
- }
- timeId = setTimeout((res) => {
- disConnectDevice();
- callBack.call(this, {
- code: config.timeOutCode(),
- err_msg: '连接超时',
- data: null,
- msg: ''
- });
- config.message.api = '';
- config.message.sendCm = [];
- config.message.status = '';
- config.message.reciveCmd = []
- }, connectTimeOut);
- ble.connectDevice(device, (res) => {
- if (res.code == config.successCode()) {
- ble.findDeviceServices(res.data, (res) => {
- console.info(JSON.stringify(res));
- ble.setParams(res.data.id);
- if (res.code != config.successCode()) {
- disConnectDevice();
- clearTimeout(timeId);
- callBack.call(this, {
- code: config.bleErrorCode(),
- err_msg: '连接失败',
- data: null,
- msg: ''
- });
- config.message.api = '';
- config.message.sendCm = [];
- config.message.status = '';
- config.message.reciveCmd = []
- }
- }, (res1) => {
- dealReceiveData(res1.data.value, (res) => {
- if (res.code == config.bleSuccessCode()) {} else {}
- callBack.call(this, {
- code: config.bleSuccessCode(),
- err_msg: '连接成功',
- data: null,
- msg: ''
- });
- clearTimeout(timeId);
- config.message.api = '';
- config.message.sendCm = [];
- config.message.status = '';
- config.message.reciveCmd = []
- })
- }, (res2) => {})
- } else {
- callBack.call(this, res)
- }
- })
- }
- export function disConnectDevice(callBack) {
- receive_data = '';
- ble.disConnectDevice((res) => {
- isConnect = false;
- dataArray = [];
- frame_count = 0;
- frame_total = 0;
- onfire.clear();
- clearTimeout(timeId);
- config.message.api = '';
- config.message.sendCm = [];
- config.message.status = '';
- config.message.reciveCmd = [];
- console.log('返回上层数据:' + JSON.stringify(res));
- callBack.call(this, res)
- })
- }
- export function initAuth(src, mac, callBack) {
- fireFlag = config.Auth();
- console.log('==========initAuth_Start==========');
- config.message.api = 'initAuth';
- receive_data = '';
- timeOutAction('设备认证超时', callBack);
- var cmd_arr = request33.obuAuth(src, mac);
- ble.sendMessageToDevice(cmd_arr, (res) => {
- recive33.receiveObuAuth((res) => {
- clearTimeout(timeId);
- console.log('返回上层数据:' + JSON.stringify(res));
- callBack.call(this, res)
- })
- })
- }
- export function deviceInfo(callBack) {
- fireFlag = config.DeviceInfo();
- console.log('==========deviceInfo_Start==========');
- config.message.api = 'deviceInfo';
- receive_data = '';
- timeOutAction('获取设备信息超时', callBack);
- var cmd_arr = request33.DevicesInfo();
- ble.sendMessageToDevice(cmd_arr, (res) => {
- recive33.receivedeviceinfo((res) => {
- clearTimeout(timeId);
- console.log('返回上层数据:' + JSON.stringify(res));
- callBack.call(this, res)
- })
- })
- }
- export function ESAMRset(callBack) {
- fireFlag = config.ResetFlag();
- console.log('==========ESAMReset_Protocol_Start==========');
- config.message.api = 'ESAMReset_Protocol';
- receive_data = '';
- timeOutAction('ESAM复位超时', callBack);
- var cmd_arr = request33.ESAMResetReq();
- ble.sendMessageToDevice(cmd_arr, (res) => {
- recive33.receiveReset((res) => {
- clearTimeout(timeId);
- console.log('返回上层数据:' + JSON.stringify(res));
- callBack.call(this, res)
- })
- })
- }
- export function ESAMChannel(cmd, callBack) {
- fireFlag = config.ChannelFlag();
- console.log('==========ESAMChannel_Protocol_Start==========:' + cmd);
- config.message.api = 'ESAMChannel_Protocol';
- var cmd_arr = request33.ESAMChannelTransmission('00', [cmd]);
- timeOutAction('ESAM透传超时', callBack);
- ble.sendMessageToDevice(cmd_arr, (res) => {
- recive33.receiveChannel((res) => {
- clearTimeout(timeId);
- console.log('返回上层数据:' + JSON.stringify(res));
- callBack.call(this, res);
- if (errMsg_arr.length == 0) {
- config.message.api = '';
- config.message.sendCmd = [];
- config.message.status = '';
- config.message.reciveCmd = []
- }
- })
- })
- }
- export function ESAMChannel_CmdArray(cmdArray, callBack) {
- config.message.api = 'ESAMChannel_CmdArray';
- cmd_count = cmdArray.length;
- all_cmdArray = cmdArray;
- ESAMTransfro((res) => {
- callBack.call(this, res)
- })
- }
-
- function ESAMTransfro(callBack) {
- var cmd = all_cmdArray[flag];
- var that = this;
- ESAMChannel(cmd, (res) => {
- if (res.code == 0) {
- flag++;
- if (errMsg_arr.length == 0) {
- result_arr.push(res.data)
- }
- if (flag < cmd_count) {
- ESAMTransfro(callBack)
- } else {
- cmd_count = 0;
- all_cmdArray = [];
- flag = 0;
- if (errMsg_arr.length == 0) {
- res.data = result_arr
- }
- result_arr = [];
- config.message.api = '';
- config.message.sendCmd = [];
- config.message.status = '';
- config.message.reciveCmd = [];
- callBack.call(this, res)
- }
- } else {
- if (errMsg_arr.length > 0) {
- res.err_msg = errMsg_arr[flag]
- }
- errMsg_arr = [];
- cmd_count = 0;
- all_cmdArray = [];
- flag = 0;
- config.message.api = '';
- config.message.sendCm = [];
- config.message.status = '';
- config.message.reciveCmd = [];
- callBack.call(this, res)
- }
- })
- }
- export function ICCReset(callBack) {
- fireFlag = config.ResetFlag();
- console.log('==========ICCReset_Protocol_Start==========');
- config.message.api = 'ICCReset_Protocol';
- receive_data = '';
- timeOutAction('ICC复位超时', callBack);
- var cmd_arr = request33.ICCResetReq();
- ble.sendMessageToDevice(cmd_arr, (res) => {
- recive33.receiveReset((res) => {
- clearTimeout(timeId);
- console.log('返回上层数据:' + JSON.stringify(res));
- callBack.call(this, res)
- })
- })
- }
- export function ICCChannel(cmd, callBack) {
- fireFlag = config.ChannelFlag();
- console.log('==========ICCChannel_Protocol_Start==========:' + cmd);
- config.message.api = 'ICCChannel_Protocol';
- var cmd_arr = request33.ICCChannelTransmission('00', [cmd]);
- timeOutAction('ICC透传超时', callBack);
- ble.sendMessageToDevice(cmd_arr, (res) => {
- recive33.receiveChannel((res) => {
- clearTimeout(timeId);
- console.log('返回上层数据:' + JSON.stringify(res));
- callBack.call(this, res);
- if (errMsg_arr.length == 0) {
- config.message.api = '';
- config.message.sendCmd = [];
- config.message.status = '';
- config.message.reciveCmd = []
- }
- })
- })
- }
- export function ICCChannel_CmdArray(cmdArray, callBack) {
- config.message.api = 'ICCChannel_CmdArray';
- cmd_count = cmdArray.length;
- all_cmdArray = cmdArray;
- ICCTransfro((res) => {
- callBack.call(this, res)
- })
- }
-
- function ICCTransfro(callBack) {
- var cmd = all_cmdArray[flag];
- var that = this;
- ICCChannel(cmd, (res) => {
- if (res.code == 0) {
- flag++;
- if (errMsg_arr.length == 0) {
- result_arr.push(res.data)
- }
- if (flag < cmd_count) {
- ICCTransfro(callBack)
- } else {
- cmd_count = 0;
- all_cmdArray = [];
- flag = 0;
- if (errMsg_arr.length == 0) {
- res.data = result_arr
- }
- result_arr = [];
- config.message.api = '';
- config.message.sendCm = [];
- config.message.status = '';
- config.message.reciveCmd = [];
- callBack.call(this, res)
- }
- } else {
- if (errMsg_arr.length > 0) {
- res.err_msg = errMsg_arr[flag]
- }
- cmd_count = 0;
- all_cmdArray = [];
- result_arr = [];
- flag = 0;
- config.message.api = '';
- config.message.sendCm = [];
- config.message.status = '';
- config.message.reciveCmd = [];
- callBack.call(this, res)
- }
- })
- }
- export function getCardInfo(callBack) {
- console.log('==========Read_0015_Start==========');
- config.message.api = 'read0015File';
- errMsg_arr = ['选择3F00目录失败', '选择1001目录失败', '读0015文件失败'];
- ICCChannel_CmdArray(['00A40000023F00', '00A40000021001', '00B095002B'], (res) => {
- errMsg_arr = [];
- callBack.call(this, res)
- })
- }
- export function getCardholderInfo(callBack) {
- console.log('==========Read_0016_Start==========');
- config.message.api = 'read0016File';
- errMsg_arr = ['选择3F00目录失败', '读0016文件失败'];
- ICCChannel_CmdArray(['00A40000023F00', '00b0960037'], (res) => {
- errMsg_arr = [];
- callBack.call(this, res)
- })
- }
- export function getCardRandNum(type, callBack) {
- console.log('==========readCardRand_Start==========type:' + type);
- config.message.api = 'readCardRand';
- if (type == 0) {
- errMsg_arr = ['选择3F00目录失败', '选择1001目录失败', '读卡0015随机失败'];
- ICCChannel_CmdArray(['00A40000023F00', '00A40000021001', '0084000004'], (res) => {
- errMsg_arr = [];
- callBack.call(this, res)
- })
- }
- if (type == 1) {
- errMsg_arr = ['选择3F00目录失败', '读卡0015随机失败'];
- ICCChannel_CmdArray(['00A40000023F00', '0084000004'], (res) => {
- errMsg_arr = [];
- callBack.call(this, res)
- })
- }
- }
- export function getSystemInfo(callBack) {
- console.log('==========readSystemInfo_Start==========');
- config.message.api = 'readSystemInfo';
- errMsg_arr = ['选择3F00目录失败', '读系统信息失败'];
- ESAMChannel_CmdArray(['00A40000023F00', '00B081001B'], (res) => {
- errMsg_arr = [];
- callBack.call(this, res)
- })
- }
- export function getCarInfo(rand, callBack) {
- console.log('==========readCarInfo_Start==========');
- config.message.api = 'readCarInfo';
- errMsg_arr = ['选择3F00目录失败', '选择DF01目录失败', '读车辆信息失败'];
- ESAMChannel_CmdArray(['00A40000023F00', '00A4000002DF01', '00B400000A' + rand + '3B00'], (res) => {
- errMsg_arr = [];
- callBack.call(this, res)
- })
- }
- export function getObuRadom(type, callBack) {
- console.log('==========readESAMRandInfo_Start==========type:' + type);
- config.message.api = 'readESAMRandInfo';
- if (type == 0) {
- errMsg_arr = ['选择3F00目录失败', '读系统信息随机数失败'];
- ESAMChannel_CmdArray(['00A40000023F00', '0084000004'], (res) => {
- errMsg_arr = [];
- callBack.call(this, res)
- })
- }
- if (type == 1) {
- errMsg_arr = ['选择3F00目录失败', '选择DF01目录失败', '读车辆信息随机数失败'];
- ESAMChannel_CmdArray(['00A40000023F00', '00A4000002DF01', '0084000004'], (res) => {
- errMsg_arr = [];
- callBack.call(this, res)
- })
- }
- }
- export function creditForLoad_Init(credit, terminalNo, pinCode, procType, keyIndex, callBack) {
- console.log('==========initCreditForLoad_Start==========');
- console.log('credit:' + credit + '====terminalNo:' + terminalNo + '====pinCode:' + pinCode + '====procType:' +
- procType + '====keyIndex:' + keyIndex);
- config.message.api = 'initCreditForLoad';
- errMsg_arr = ['选择1001目录失败', '选择1001目录失败', '验PIN失败', '圈存初始化失败'];
- ICCChannel_CmdArray(['00A40000023F00', '00A40000021001', '00200000' + dataTool.tenToHex(pinCode.length / 2, 2) +
- pinCode, '805000' + procType + '0B' + keyIndex + credit + terminalNo
- ], (res) => {
- errMsg_arr = [];
- callBack.call(this, res)
- })
- }
- export function creditForLoad_Load(mac2, callBack) {
- console.log('==========initCreditForLoad_Start==========mac2:' + mac2);
- config.message.api = 'creditForLoad';
- receive_data = '';
- timeOutAction('圈存写卡失败', callBack);
- errMsg_arr = ['圈存写卡失败'];
- ICCChannel_CmdArray(['805200000B' + mac2], (res) => {
- errMsg_arr = [];
- callBack.call(this, res)
- })
- }
-
- function timeOutAction(info, callBack) {
- timeId = setTimeout((res) => {
- receive_data = '';
- errMsg_arr = [];
- callBack.call(this, {
- code: config.timeOutCode(),
- err_msg: info,
- data: null,
- msg: ''
- })
- }, sendTimeOut)
- }
-
- function dealReceiveData(data, callBack) {
- if (receive_data.length == 0 && data.substr(0, 4) != 'FE01') {
- return
- }
- if (revc_count == 0) {
- if (data.substr(0, 4) == 'FE01') {
- receive_data += data;
- revc_count++
- }
- } else {
- receive_data += data;
- revc_count++
- }
- var blg = parseInt(receive_data.substring(4, 8), 16) * 2 == receive_data.length ? true : false;
- if (blg) {
- var flag_protocol = receive_data.substr(24, 2);
- var flag_wechat = receive_data.substr(8, 4);
- if (flag_protocol == '33') {
- console.log(TAG + '----fireFlag:' + fireFlag);
- var pack_count = receive_data.substr(28, 2);
- wechat_arr.push(receive_data);
- receive_data = '';
- if (pack_count == '80' || pack_count == '00') {
- onfire.fire(fireFlag, wechat_arr);
- revc_count == 0;
- wechat_arr = []
- }
- } else if (flag_wechat == '2711' || flag_wechat == '2713') {
- if (flag_wechat == '2711') {
- var mac = receive_data.substring(40, 44);
- var cmd_arr = request33.wechatInit1(mac);
- receive_data = '';
- revc_count == 0;
- ble.sendMessageToDevice(cmd_arr, (res) => {})
- } else if (flag_wechat == '2713') {
- var cmd_arr = request33.wechatInit2(mac);
- receive_data = '';
- ble.sendMessageToDevice(cmd_arr, (res) => {
- receive_data = '';
- revc_count == 0;
- callBack.call(this, {
- code: config.bleSuccessCode(),
- msg: '连接成功',
- data: null
- })
- })
- } else {
- callBack.call(this, {
- code: config.bleErrorCode(),
- msg: '非法连接',
- data: null
- })
- }
- } else {
- callBack.call(this, {
- code: config.bleErrorCode(),
- msg: '非法连接',
- data: null
- })
- }
- }
- }
|