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.

JLDeviceWechat+33Protocol.js 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. var TAG = 'JLDeviceWechar+33Protocol';
  2. var onfire = require('./onfire');
  3. var request33 = require('./JLObuWechat33Pack.js');
  4. var recive33 = require('./JLObuWecha33UnPackt.js');
  5. var config = require('./JLConfig');
  6. var ble = require('./JLAlipayBleManager.js');
  7. var dataTool = require('./dataTool');
  8. var revc_count = 0;
  9. var sendTimeOut = 10000;
  10. var connectTimeOut = 30000;
  11. var wechat_arr = [];
  12. var dataArray = [];
  13. var isConnect = false;
  14. var frame_total = 1;
  15. var frame_count = 1;
  16. var fireFlag = '';
  17. var timeId;
  18. var receive_data = '';
  19. var receive_data_total_len = '';
  20. var wechat1 = false;
  21. var wechat2 = false;
  22. var timeId;
  23. var cmd_count = 0;
  24. var all_cmdArray = [];
  25. var flag = 0;
  26. var result_arr = [];
  27. var errMsg_arr = [];
  28. export function scanDevice(nameFlag, timeOut, callBack) {
  29. timeId = setTimeout((res) => {
  30. ble.scan_stopScanBleDevice((res) => {
  31. callBack.call(this, {
  32. code: config.timeOutCode(),
  33. err_msg: "扫描超时",
  34. data: null,
  35. msg: ''
  36. })
  37. })
  38. }, timeOut);
  39. ble.scan_stopScanBleDevice((res) => {
  40. ble.scan_StartScanBleDevice(nameFlag, (res) => {
  41. clearTimeout(timeId);
  42. console.log('返回上层数据:' + JSON.stringify(res));
  43. callBack.call(this, res)
  44. })
  45. })
  46. }
  47. export function stopScanDevice(callBack) {
  48. ble.scan_stopScanBleDevice((res) => {
  49. callBack.call(this, res)
  50. })
  51. }
  52. export function connectDevice(device, callBack) {
  53. console.log('==========connectDevice_Start==========:' + JSON.stringify(device));
  54. config.message.api = 'connectDevice';
  55. receive_data = '';
  56. if (device == null || device == 'undefined') {
  57. callBack.call(this, {
  58. code: 10003,
  59. err_msg: '参数错误',
  60. data: null,
  61. msg: ''
  62. });
  63. return
  64. }
  65. timeId = setTimeout((res) => {
  66. disConnectDevice();
  67. callBack.call(this, {
  68. code: config.timeOutCode(),
  69. err_msg: '连接超时',
  70. data: null,
  71. msg: ''
  72. });
  73. config.message.api = '';
  74. config.message.sendCm = [];
  75. config.message.status = '';
  76. config.message.reciveCmd = []
  77. }, connectTimeOut);
  78. ble.connectDevice(device, (res) => {
  79. if (res.code == config.successCode()) {
  80. ble.findDeviceServices(res.data, (res) => {
  81. console.info(JSON.stringify(res));
  82. ble.setParams(res.data.id);
  83. if (res.code != config.successCode()) {
  84. disConnectDevice();
  85. clearTimeout(timeId);
  86. callBack.call(this, {
  87. code: config.bleErrorCode(),
  88. err_msg: '连接失败',
  89. data: null,
  90. msg: ''
  91. });
  92. config.message.api = '';
  93. config.message.sendCm = [];
  94. config.message.status = '';
  95. config.message.reciveCmd = []
  96. }
  97. }, (res1) => {
  98. dealReceiveData(res1.data.value, (res) => {
  99. if (res.code == config.bleSuccessCode()) {} else {}
  100. callBack.call(this, {
  101. code: config.bleSuccessCode(),
  102. err_msg: '连接成功',
  103. data: null,
  104. msg: ''
  105. });
  106. clearTimeout(timeId);
  107. config.message.api = '';
  108. config.message.sendCm = [];
  109. config.message.status = '';
  110. config.message.reciveCmd = []
  111. })
  112. }, (res2) => {})
  113. } else {
  114. callBack.call(this, res)
  115. }
  116. })
  117. }
  118. export function disConnectDevice(callBack) {
  119. receive_data = '';
  120. ble.disConnectDevice((res) => {
  121. isConnect = false;
  122. dataArray = [];
  123. frame_count = 0;
  124. frame_total = 0;
  125. onfire.clear();
  126. clearTimeout(timeId);
  127. config.message.api = '';
  128. config.message.sendCm = [];
  129. config.message.status = '';
  130. config.message.reciveCmd = [];
  131. console.log('返回上层数据:' + JSON.stringify(res));
  132. callBack.call(this, res)
  133. })
  134. }
  135. export function initAuth(src, mac, callBack) {
  136. fireFlag = config.Auth();
  137. console.log('==========initAuth_Start==========');
  138. config.message.api = 'initAuth';
  139. receive_data = '';
  140. timeOutAction('设备认证超时', callBack);
  141. var cmd_arr = request33.obuAuth(src, mac);
  142. ble.sendMessageToDevice(cmd_arr, (res) => {
  143. recive33.receiveObuAuth((res) => {
  144. clearTimeout(timeId);
  145. console.log('返回上层数据:' + JSON.stringify(res));
  146. callBack.call(this, res)
  147. })
  148. })
  149. }
  150. export function deviceInfo(callBack) {
  151. fireFlag = config.DeviceInfo();
  152. console.log('==========deviceInfo_Start==========');
  153. config.message.api = 'deviceInfo';
  154. receive_data = '';
  155. timeOutAction('获取设备信息超时', callBack);
  156. var cmd_arr = request33.DevicesInfo();
  157. ble.sendMessageToDevice(cmd_arr, (res) => {
  158. recive33.receivedeviceinfo((res) => {
  159. clearTimeout(timeId);
  160. console.log('返回上层数据:' + JSON.stringify(res));
  161. callBack.call(this, res)
  162. })
  163. })
  164. }
  165. export function ESAMRset(callBack) {
  166. fireFlag = config.ResetFlag();
  167. console.log('==========ESAMReset_Protocol_Start==========');
  168. config.message.api = 'ESAMReset_Protocol';
  169. receive_data = '';
  170. timeOutAction('ESAM复位超时', callBack);
  171. var cmd_arr = request33.ESAMResetReq();
  172. ble.sendMessageToDevice(cmd_arr, (res) => {
  173. recive33.receiveReset((res) => {
  174. clearTimeout(timeId);
  175. console.log('返回上层数据:' + JSON.stringify(res));
  176. callBack.call(this, res)
  177. })
  178. })
  179. }
  180. export function ESAMChannel(cmd, callBack) {
  181. fireFlag = config.ChannelFlag();
  182. console.log('==========ESAMChannel_Protocol_Start==========:' + cmd);
  183. config.message.api = 'ESAMChannel_Protocol';
  184. var cmd_arr = request33.ESAMChannelTransmission('00', [cmd]);
  185. timeOutAction('ESAM透传超时', callBack);
  186. ble.sendMessageToDevice(cmd_arr, (res) => {
  187. recive33.receiveChannel((res) => {
  188. clearTimeout(timeId);
  189. console.log('返回上层数据:' + JSON.stringify(res));
  190. callBack.call(this, res);
  191. if (errMsg_arr.length == 0) {
  192. config.message.api = '';
  193. config.message.sendCmd = [];
  194. config.message.status = '';
  195. config.message.reciveCmd = []
  196. }
  197. })
  198. })
  199. }
  200. export function ESAMChannel_CmdArray(cmdArray, callBack) {
  201. config.message.api = 'ESAMChannel_CmdArray';
  202. cmd_count = cmdArray.length;
  203. all_cmdArray = cmdArray;
  204. ESAMTransfro((res) => {
  205. callBack.call(this, res)
  206. })
  207. }
  208. function ESAMTransfro(callBack) {
  209. var cmd = all_cmdArray[flag];
  210. var that = this;
  211. ESAMChannel(cmd, (res) => {
  212. if (res.code == 0) {
  213. flag++;
  214. if (errMsg_arr.length == 0) {
  215. result_arr.push(res.data)
  216. }
  217. if (flag < cmd_count) {
  218. ESAMTransfro(callBack)
  219. } else {
  220. cmd_count = 0;
  221. all_cmdArray = [];
  222. flag = 0;
  223. if (errMsg_arr.length == 0) {
  224. res.data = result_arr
  225. }
  226. result_arr = [];
  227. config.message.api = '';
  228. config.message.sendCmd = [];
  229. config.message.status = '';
  230. config.message.reciveCmd = [];
  231. callBack.call(this, res)
  232. }
  233. } else {
  234. if (errMsg_arr.length > 0) {
  235. res.err_msg = errMsg_arr[flag]
  236. }
  237. errMsg_arr = [];
  238. cmd_count = 0;
  239. all_cmdArray = [];
  240. flag = 0;
  241. config.message.api = '';
  242. config.message.sendCm = [];
  243. config.message.status = '';
  244. config.message.reciveCmd = [];
  245. callBack.call(this, res)
  246. }
  247. })
  248. }
  249. export function ICCReset(callBack) {
  250. fireFlag = config.ResetFlag();
  251. console.log('==========ICCReset_Protocol_Start==========');
  252. config.message.api = 'ICCReset_Protocol';
  253. receive_data = '';
  254. timeOutAction('ICC复位超时', callBack);
  255. var cmd_arr = request33.ICCResetReq();
  256. ble.sendMessageToDevice(cmd_arr, (res) => {
  257. recive33.receiveReset((res) => {
  258. clearTimeout(timeId);
  259. console.log('返回上层数据:' + JSON.stringify(res));
  260. callBack.call(this, res)
  261. })
  262. })
  263. }
  264. export function ICCChannel(cmd, callBack) {
  265. fireFlag = config.ChannelFlag();
  266. console.log('==========ICCChannel_Protocol_Start==========:' + cmd);
  267. config.message.api = 'ICCChannel_Protocol';
  268. var cmd_arr = request33.ICCChannelTransmission('00', [cmd]);
  269. timeOutAction('ICC透传超时', callBack);
  270. ble.sendMessageToDevice(cmd_arr, (res) => {
  271. recive33.receiveChannel((res) => {
  272. clearTimeout(timeId);
  273. console.log('返回上层数据:' + JSON.stringify(res));
  274. callBack.call(this, res);
  275. if (errMsg_arr.length == 0) {
  276. config.message.api = '';
  277. config.message.sendCmd = [];
  278. config.message.status = '';
  279. config.message.reciveCmd = []
  280. }
  281. })
  282. })
  283. }
  284. export function ICCChannel_CmdArray(cmdArray, callBack) {
  285. config.message.api = 'ICCChannel_CmdArray';
  286. cmd_count = cmdArray.length;
  287. all_cmdArray = cmdArray;
  288. ICCTransfro((res) => {
  289. callBack.call(this, res)
  290. })
  291. }
  292. function ICCTransfro(callBack) {
  293. var cmd = all_cmdArray[flag];
  294. var that = this;
  295. ICCChannel(cmd, (res) => {
  296. if (res.code == 0) {
  297. flag++;
  298. if (errMsg_arr.length == 0) {
  299. result_arr.push(res.data)
  300. }
  301. if (flag < cmd_count) {
  302. ICCTransfro(callBack)
  303. } else {
  304. cmd_count = 0;
  305. all_cmdArray = [];
  306. flag = 0;
  307. if (errMsg_arr.length == 0) {
  308. res.data = result_arr
  309. }
  310. result_arr = [];
  311. config.message.api = '';
  312. config.message.sendCm = [];
  313. config.message.status = '';
  314. config.message.reciveCmd = [];
  315. callBack.call(this, res)
  316. }
  317. } else {
  318. if (errMsg_arr.length > 0) {
  319. res.err_msg = errMsg_arr[flag]
  320. }
  321. cmd_count = 0;
  322. all_cmdArray = [];
  323. result_arr = [];
  324. flag = 0;
  325. config.message.api = '';
  326. config.message.sendCm = [];
  327. config.message.status = '';
  328. config.message.reciveCmd = [];
  329. callBack.call(this, res)
  330. }
  331. })
  332. }
  333. export function getCardInfo(callBack) {
  334. console.log('==========Read_0015_Start==========');
  335. config.message.api = 'read0015File';
  336. errMsg_arr = ['选择3F00目录失败', '选择1001目录失败', '读0015文件失败'];
  337. ICCChannel_CmdArray(['00A40000023F00', '00A40000021001', '00B095002B'], (res) => {
  338. errMsg_arr = [];
  339. callBack.call(this, res)
  340. })
  341. }
  342. export function getCardholderInfo(callBack) {
  343. console.log('==========Read_0016_Start==========');
  344. config.message.api = 'read0016File';
  345. errMsg_arr = ['选择3F00目录失败', '读0016文件失败'];
  346. ICCChannel_CmdArray(['00A40000023F00', '00b0960037'], (res) => {
  347. errMsg_arr = [];
  348. callBack.call(this, res)
  349. })
  350. }
  351. export function getCardRandNum(type, callBack) {
  352. console.log('==========readCardRand_Start==========type:' + type);
  353. config.message.api = 'readCardRand';
  354. if (type == 0) {
  355. errMsg_arr = ['选择3F00目录失败', '选择1001目录失败', '读卡0015随机失败'];
  356. ICCChannel_CmdArray(['00A40000023F00', '00A40000021001', '0084000004'], (res) => {
  357. errMsg_arr = [];
  358. callBack.call(this, res)
  359. })
  360. }
  361. if (type == 1) {
  362. errMsg_arr = ['选择3F00目录失败', '读卡0015随机失败'];
  363. ICCChannel_CmdArray(['00A40000023F00', '0084000004'], (res) => {
  364. errMsg_arr = [];
  365. callBack.call(this, res)
  366. })
  367. }
  368. }
  369. export function getSystemInfo(callBack) {
  370. console.log('==========readSystemInfo_Start==========');
  371. config.message.api = 'readSystemInfo';
  372. errMsg_arr = ['选择3F00目录失败', '读系统信息失败'];
  373. ESAMChannel_CmdArray(['00A40000023F00', '00B081001B'], (res) => {
  374. errMsg_arr = [];
  375. callBack.call(this, res)
  376. })
  377. }
  378. export function getCarInfo(rand, callBack) {
  379. console.log('==========readCarInfo_Start==========');
  380. config.message.api = 'readCarInfo';
  381. errMsg_arr = ['选择3F00目录失败', '选择DF01目录失败', '读车辆信息失败'];
  382. ESAMChannel_CmdArray(['00A40000023F00', '00A4000002DF01', '00B400000A' + rand + '3B00'], (res) => {
  383. errMsg_arr = [];
  384. callBack.call(this, res)
  385. })
  386. }
  387. export function getObuRadom(type, callBack) {
  388. console.log('==========readESAMRandInfo_Start==========type:' + type);
  389. config.message.api = 'readESAMRandInfo';
  390. if (type == 0) {
  391. errMsg_arr = ['选择3F00目录失败', '读系统信息随机数失败'];
  392. ESAMChannel_CmdArray(['00A40000023F00', '0084000004'], (res) => {
  393. errMsg_arr = [];
  394. callBack.call(this, res)
  395. })
  396. }
  397. if (type == 1) {
  398. errMsg_arr = ['选择3F00目录失败', '选择DF01目录失败', '读车辆信息随机数失败'];
  399. ESAMChannel_CmdArray(['00A40000023F00', '00A4000002DF01', '0084000004'], (res) => {
  400. errMsg_arr = [];
  401. callBack.call(this, res)
  402. })
  403. }
  404. }
  405. export function creditForLoad_Init(credit, terminalNo, pinCode, procType, keyIndex, callBack) {
  406. console.log('==========initCreditForLoad_Start==========');
  407. console.log('credit:' + credit + '====terminalNo:' + terminalNo + '====pinCode:' + pinCode + '====procType:' +
  408. procType + '====keyIndex:' + keyIndex);
  409. config.message.api = 'initCreditForLoad';
  410. errMsg_arr = ['选择1001目录失败', '选择1001目录失败', '验PIN失败', '圈存初始化失败'];
  411. ICCChannel_CmdArray(['00A40000023F00', '00A40000021001', '00200000' + dataTool.tenToHex(pinCode.length / 2, 2) +
  412. pinCode, '805000' + procType + '0B' + keyIndex + credit + terminalNo
  413. ], (res) => {
  414. errMsg_arr = [];
  415. callBack.call(this, res)
  416. })
  417. }
  418. export function creditForLoad_Load(mac2, callBack) {
  419. console.log('==========initCreditForLoad_Start==========mac2:' + mac2);
  420. config.message.api = 'creditForLoad';
  421. receive_data = '';
  422. timeOutAction('圈存写卡失败', callBack);
  423. errMsg_arr = ['圈存写卡失败'];
  424. ICCChannel_CmdArray(['805200000B' + mac2], (res) => {
  425. errMsg_arr = [];
  426. callBack.call(this, res)
  427. })
  428. }
  429. function timeOutAction(info, callBack) {
  430. timeId = setTimeout((res) => {
  431. receive_data = '';
  432. errMsg_arr = [];
  433. callBack.call(this, {
  434. code: config.timeOutCode(),
  435. err_msg: info,
  436. data: null,
  437. msg: ''
  438. })
  439. }, sendTimeOut)
  440. }
  441. function dealReceiveData(data, callBack) {
  442. if (receive_data.length == 0 && data.substr(0, 4) != 'FE01') {
  443. return
  444. }
  445. if (revc_count == 0) {
  446. if (data.substr(0, 4) == 'FE01') {
  447. receive_data += data;
  448. revc_count++
  449. }
  450. } else {
  451. receive_data += data;
  452. revc_count++
  453. }
  454. var blg = parseInt(receive_data.substring(4, 8), 16) * 2 == receive_data.length ? true : false;
  455. if (blg) {
  456. var flag_protocol = receive_data.substr(24, 2);
  457. var flag_wechat = receive_data.substr(8, 4);
  458. if (flag_protocol == '33') {
  459. console.log(TAG + '----fireFlag:' + fireFlag);
  460. var pack_count = receive_data.substr(28, 2);
  461. wechat_arr.push(receive_data);
  462. receive_data = '';
  463. if (pack_count == '80' || pack_count == '00') {
  464. onfire.fire(fireFlag, wechat_arr);
  465. revc_count == 0;
  466. wechat_arr = []
  467. }
  468. } else if (flag_wechat == '2711' || flag_wechat == '2713') {
  469. if (flag_wechat == '2711') {
  470. var mac = receive_data.substring(40, 44);
  471. var cmd_arr = request33.wechatInit1(mac);
  472. receive_data = '';
  473. revc_count == 0;
  474. ble.sendMessageToDevice(cmd_arr, (res) => {})
  475. } else if (flag_wechat == '2713') {
  476. var cmd_arr = request33.wechatInit2(mac);
  477. receive_data = '';
  478. ble.sendMessageToDevice(cmd_arr, (res) => {
  479. receive_data = '';
  480. revc_count == 0;
  481. callBack.call(this, {
  482. code: config.bleSuccessCode(),
  483. msg: '连接成功',
  484. data: null
  485. })
  486. })
  487. } else {
  488. callBack.call(this, {
  489. code: config.bleErrorCode(),
  490. msg: '非法连接',
  491. data: null
  492. })
  493. }
  494. } else {
  495. callBack.call(this, {
  496. code: config.bleErrorCode(),
  497. msg: '非法连接',
  498. data: null
  499. })
  500. }
  501. }
  502. }