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 15KB

1 년 전
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  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. clearTimeout(timeId);
  100. config.message.api = '';
  101. config.message.sendCm = [];
  102. config.message.status = '';
  103. config.message.reciveCmd = [];
  104. ICCReset((res) => {
  105. if (res.code == 0) {
  106. callBack.call(this, {
  107. code: config.bleSuccessCode(),
  108. err_msg: '连接成功,卡片复位成功',
  109. data: null,
  110. msg: ''
  111. })
  112. } else {
  113. disConnectDevice((res) => {
  114. callBack.call(this, {
  115. code: config.bleErrorCode(),
  116. err_msg: '断开连接,卡片复位失败',
  117. data: null,
  118. msg: ''
  119. })
  120. })
  121. }
  122. })
  123. })
  124. }, (res2) => {})
  125. } else {
  126. callBack.call(this, res)
  127. }
  128. })
  129. }
  130. export function onConnectState(callBack) {
  131. ble.onDeviceConnectStateChange((res) => {
  132. receive_data = '';
  133. isConnect = false;
  134. dataArray = [];
  135. frame_count = 0;
  136. frame_total = 0;
  137. onfire.clear();
  138. clearTimeout(timeId);
  139. config.message.api = '';
  140. config.message.sendCm = [];
  141. config.message.status = '';
  142. config.message.reciveCmd = [];
  143. console.log('返回上层数据:' + JSON.stringify(res));
  144. callBack.call(this, res)
  145. })
  146. }
  147. export function disConnectDevice(callBack) {
  148. receive_data = '';
  149. ble.disConnectDevice((res) => {
  150. isConnect = false;
  151. dataArray = [];
  152. frame_count = 0;
  153. frame_total = 0;
  154. onfire.clear();
  155. clearTimeout(timeId);
  156. config.message.api = '';
  157. config.message.sendCm = [];
  158. config.message.status = '';
  159. config.message.reciveCmd = [];
  160. console.log('返回上层数据:' + JSON.stringify(res));
  161. callBack.call(this, res)
  162. })
  163. }
  164. export function initAuth(src, mac, callBack) {
  165. fireFlag = config.Auth();
  166. console.log('==========initAuth_Start==========');
  167. config.message.api = 'initAuth';
  168. receive_data = '';
  169. timeOutAction('设备认证超时', callBack);
  170. var cmd_arr = request33.obuAuth(src, mac);
  171. ble.sendMessageToDevice(cmd_arr, (res) => {
  172. recive33.receiveObuAuth((res) => {
  173. clearTimeout(timeId);
  174. console.log('返回上层数据:' + JSON.stringify(res));
  175. callBack.call(this, res)
  176. })
  177. })
  178. }
  179. export function deviceInfo(callBack) {
  180. fireFlag = config.DeviceInfo();
  181. console.log('==========deviceInfo_Start==========');
  182. config.message.api = 'deviceInfo';
  183. receive_data = '';
  184. timeOutAction('获取设备信息超时', callBack);
  185. var cmd_arr = request33.DevicesInfo();
  186. ble.sendMessageToDevice(cmd_arr, (res) => {
  187. recive33.receivedeviceinfo((res) => {
  188. clearTimeout(timeId);
  189. console.log('返回上层数据:' + JSON.stringify(res));
  190. callBack.call(this, res)
  191. })
  192. })
  193. }
  194. export function ESAMRset(callBack) {
  195. fireFlag = config.ResetFlag();
  196. console.log('==========ESAMReset_Protocol_Start==========');
  197. config.message.api = 'ESAMReset_Protocol';
  198. receive_data = '';
  199. timeOutAction('ESAM复位超时', callBack);
  200. var cmd_arr = request33.ESAMResetReq();
  201. ble.sendMessageToDevice(cmd_arr, (res) => {
  202. recive33.receiveReset((res) => {
  203. clearTimeout(timeId);
  204. console.log('返回上层数据:' + JSON.stringify(res));
  205. callBack.call(this, res)
  206. })
  207. })
  208. }
  209. export function ESAMChannel(cmd, callBack) {
  210. fireFlag = config.ChannelFlag();
  211. console.log('==========ESAMChannel_Protocol_Start==========:' + cmd);
  212. config.message.api = 'ESAMChannel_Protocol';
  213. var cmd_arr = request33.ESAMChannelTransmission('00', [cmd]);
  214. timeOutAction('ESAM透传超时', callBack);
  215. ble.sendMessageToDevice(cmd_arr, (res) => {
  216. recive33.receiveChannel((res) => {
  217. clearTimeout(timeId);
  218. console.log('返回上层数据:' + JSON.stringify(res));
  219. callBack.call(this, res);
  220. if (errMsg_arr.length == 0) {
  221. config.message.api = '';
  222. config.message.sendCmd = [];
  223. config.message.status = '';
  224. config.message.reciveCmd = []
  225. }
  226. })
  227. })
  228. }
  229. export function ESAMChannel_CmdArray(cmdArray, callBack) {
  230. config.message.api = 'ESAMChannel_CmdArray';
  231. cmd_count = cmdArray.length;
  232. all_cmdArray = cmdArray;
  233. ESAMTransfro((res) => {
  234. callBack.call(this, res)
  235. })
  236. }
  237. function ESAMTransfro(callBack) {
  238. var cmd = all_cmdArray[flag];
  239. var that = this;
  240. ESAMChannel(cmd, (res) => {
  241. if (res.code == 0) {
  242. flag++;
  243. if (errMsg_arr.length == 0) {
  244. result_arr.push(res.data)
  245. }
  246. if (flag < cmd_count) {
  247. ESAMTransfro(callBack)
  248. } else {
  249. cmd_count = 0;
  250. all_cmdArray = [];
  251. flag = 0;
  252. if (errMsg_arr.length == 0) {
  253. res.data = result_arr
  254. }
  255. result_arr = [];
  256. config.message.api = '';
  257. config.message.sendCmd = [];
  258. config.message.status = '';
  259. config.message.reciveCmd = [];
  260. callBack.call(this, res)
  261. }
  262. } else {
  263. if (errMsg_arr.length > 0) {
  264. res.err_msg = errMsg_arr[flag]
  265. }
  266. errMsg_arr = [];
  267. cmd_count = 0;
  268. all_cmdArray = [];
  269. flag = 0;
  270. config.message.api = '';
  271. config.message.sendCm = [];
  272. config.message.status = '';
  273. config.message.reciveCmd = [];
  274. callBack.call(this, res)
  275. }
  276. })
  277. }
  278. export function ICCReset(callBack) {
  279. fireFlag = config.ResetFlag();
  280. console.log('==========ICCReset_Protocol_Start==========');
  281. config.message.api = 'ICCReset_Protocol';
  282. receive_data = '';
  283. timeOutAction('ICC复位超时', callBack);
  284. var cmd_arr = request33.ICCResetReq();
  285. ble.sendMessageToDevice(cmd_arr, (res) => {
  286. recive33.receiveReset((res) => {
  287. clearTimeout(timeId);
  288. console.log('返回上层数据:' + JSON.stringify(res));
  289. callBack.call(this, res)
  290. })
  291. })
  292. }
  293. export function ICCChannel(cmd, callBack) {
  294. fireFlag = config.ChannelFlag();
  295. console.log('==========ICCChannel_Protocol_Start==========:' + cmd);
  296. config.message.api = 'ICCChannel_Protocol';
  297. var cmd_arr = request33.ICCChannelTransmission('00', [cmd]);
  298. timeOutAction('连接设备蓝牙超时', callBack);
  299. ble.sendMessageToDevice(cmd_arr, (res) => {
  300. recive33.receiveChannel((res) => {
  301. clearTimeout(timeId);
  302. console.log('返回上层数据:' + JSON.stringify(res));
  303. callBack.call(this, res);
  304. if (errMsg_arr.length == 0) {
  305. config.message.api = '';
  306. config.message.sendCmd = [];
  307. config.message.status = '';
  308. config.message.reciveCmd = []
  309. }
  310. })
  311. })
  312. }
  313. export function ICCChannel_CmdArray(cmdArray, callBack) {
  314. config.message.api = 'ICCChannel_CmdArray';
  315. cmd_count = cmdArray.length;
  316. all_cmdArray = cmdArray;
  317. ICCTransfro((res) => {
  318. callBack.call(this, res)
  319. })
  320. }
  321. function ICCTransfro(callBack) {
  322. var cmd = all_cmdArray[flag];
  323. var that = this;
  324. ICCChannel(cmd, (res) => {
  325. if (res.code == 0) {
  326. flag++;
  327. if (errMsg_arr.length == 0) {
  328. result_arr.push(res.data)
  329. }
  330. if (flag < cmd_count) {
  331. ICCTransfro(callBack)
  332. } else {
  333. cmd_count = 0;
  334. all_cmdArray = [];
  335. flag = 0;
  336. if (errMsg_arr.length == 0) {
  337. res.data = result_arr
  338. }
  339. result_arr = [];
  340. config.message.api = '';
  341. config.message.sendCm = [];
  342. config.message.status = '';
  343. config.message.reciveCmd = [];
  344. callBack.call(this, res)
  345. }
  346. } else {
  347. if (errMsg_arr.length > 0) {
  348. res.err_msg = errMsg_arr[flag]
  349. }
  350. cmd_count = 0;
  351. all_cmdArray = [];
  352. result_arr = [];
  353. flag = 0;
  354. config.message.api = '';
  355. config.message.sendCm = [];
  356. config.message.status = '';
  357. config.message.reciveCmd = [];
  358. callBack.call(this, res)
  359. }
  360. })
  361. }
  362. export function getCardInfo(callBack) {
  363. console.log('==========Read_0015_Start==========');
  364. config.message.api = 'read0015File';
  365. errMsg_arr = ['选择3F00目录失败', '选择1001目录失败', '读0015文件失败'];
  366. ICCChannel_CmdArray(['00A40000023F00', '00A40000021001', '00B095002B'], (res) => {
  367. errMsg_arr = [];
  368. callBack.call(this, res)
  369. })
  370. }
  371. export function getCardholderInfo(callBack) {
  372. console.log('==========Read_0016_Start==========');
  373. config.message.api = 'read0016File';
  374. errMsg_arr = ['选择3F00目录失败', '读0016文件失败'];
  375. ICCChannel_CmdArray(['00A40000023F00', '00b0960037'], (res) => {
  376. errMsg_arr = [];
  377. callBack.call(this, res)
  378. })
  379. }
  380. export function getCardRandNum(type, callBack) {
  381. console.log('==========readCardRand_Start==========type:' + type);
  382. config.message.api = 'readCardRand';
  383. if (type == 0) {
  384. errMsg_arr = ['选择3F00目录失败', '选择1001目录失败', '读卡0015随机失败'];
  385. ICCChannel_CmdArray(['00A40000023F00', '00A40000021001', '0084000004'], (res) => {
  386. errMsg_arr = [];
  387. callBack.call(this, res)
  388. })
  389. }
  390. if (type == 1) {
  391. errMsg_arr = ['选择3F00目录失败', '读卡0015随机失败'];
  392. ICCChannel_CmdArray(['00A40000023F00', '0084000004'], (res) => {
  393. errMsg_arr = [];
  394. callBack.call(this, res)
  395. })
  396. }
  397. }
  398. export function getSystemInfo(callBack) {
  399. console.log('==========readSystemInfo_Start==========');
  400. config.message.api = 'readSystemInfo';
  401. errMsg_arr = ['选择3F00目录失败', '读系统信息失败'];
  402. ESAMChannel_CmdArray(['00A40000023F00', '00B081001B'], (res) => {
  403. errMsg_arr = [];
  404. callBack.call(this, res)
  405. })
  406. }
  407. export function getCarInfo(rand, callBack) {
  408. console.log('==========readCarInfo_Start==========');
  409. config.message.api = 'readCarInfo';
  410. errMsg_arr = ['选择3F00目录失败', '选择DF01目录失败', '读车辆信息失败'];
  411. ESAMChannel_CmdArray(['00A40000023F00', '00A4000002DF01', '00B400000A' + rand + '3B00'], (res) => {
  412. errMsg_arr = [];
  413. callBack.call(this, res)
  414. })
  415. }
  416. export function getObuRadom(type, callBack) {
  417. console.log('==========readESAMRandInfo_Start==========type:' + type);
  418. config.message.api = 'readESAMRandInfo';
  419. if (type == 0) {
  420. errMsg_arr = ['选择3F00目录失败', '读系统信息随机数失败'];
  421. ESAMChannel_CmdArray(['00A40000023F00', '0084000004'], (res) => {
  422. errMsg_arr = [];
  423. callBack.call(this, res)
  424. })
  425. }
  426. if (type == 1) {
  427. errMsg_arr = ['选择3F00目录失败', '选择DF01目录失败', '读车辆信息随机数失败'];
  428. ESAMChannel_CmdArray(['00A40000023F00', '00A4000002DF01', '0084000004'], (res) => {
  429. errMsg_arr = [];
  430. callBack.call(this, res)
  431. })
  432. }
  433. }
  434. export function creditForLoad_Init(credit, terminalNo, pinCode, procType, keyIndex, callBack) {
  435. console.log('==========initCreditForLoad_Start==========');
  436. console.log('credit:' + credit + '====terminalNo:' + terminalNo + '====pinCode:' + pinCode + '====procType:' +
  437. procType + '====keyIndex:' + keyIndex);
  438. config.message.api = 'initCreditForLoad';
  439. errMsg_arr = ['选择1001目录失败', '选择1001目录失败', '验PIN失败', '圈存初始化失败'];
  440. ICCChannel_CmdArray(['00A40000023F00', '00A40000021001', '00200000' + dataTool.tenToHex(pinCode.length / 2, 2) +
  441. pinCode, '805000' + procType + '0B' + keyIndex + credit + terminalNo
  442. ], (res) => {
  443. errMsg_arr = [];
  444. callBack.call(this, res)
  445. })
  446. }
  447. export function creditForLoad_Load(mac2, callBack) {
  448. console.log('==========initCreditForLoad_Start==========mac2:' + mac2);
  449. config.message.api = 'creditForLoad';
  450. receive_data = '';
  451. timeOutAction('圈存写卡失败', callBack);
  452. errMsg_arr = ['圈存写卡失败'];
  453. ICCChannel_CmdArray(['805200000B' + mac2], (res) => {
  454. errMsg_arr = [];
  455. callBack.call(this, res)
  456. })
  457. }
  458. function timeOutAction(info, callBack) {
  459. timeId = setTimeout((res) => {
  460. receive_data = '';
  461. errMsg_arr = [];
  462. callBack.call(this, {
  463. code: config.timeOutCode(),
  464. err_msg: info,
  465. data: null,
  466. msg: ''
  467. })
  468. }, sendTimeOut)
  469. }
  470. function dealReceiveData(data, callBack) {
  471. if (receive_data.length == 0 && data.substr(0, 4) != 'FE01') {
  472. return
  473. }
  474. if (revc_count == 0) {
  475. if (data.substr(0, 4) == 'FE01') {
  476. receive_data += data;
  477. revc_count++
  478. }
  479. } else {
  480. receive_data += data;
  481. revc_count++
  482. }
  483. var blg = parseInt(receive_data.substring(4, 8), 16) * 2 == receive_data.length ? true : false;
  484. if (blg) {
  485. var flag_protocol = receive_data.substr(24, 2);
  486. var flag_wechat = receive_data.substr(8, 4);
  487. if (flag_protocol == '33') {
  488. console.log(TAG + '----fireFlag:' + fireFlag);
  489. var pack_count = receive_data.substr(28, 2);
  490. wechat_arr.push(receive_data);
  491. receive_data = '';
  492. if (pack_count == '80' || pack_count == '00') {
  493. onfire.fire(fireFlag, wechat_arr);
  494. revc_count == 0;
  495. wechat_arr = []
  496. }
  497. } else if (flag_wechat == '2711' || flag_wechat == '2713') {
  498. if (flag_wechat == '2711') {
  499. var mac = receive_data.substring(40, 44);
  500. var cmd_arr = request33.wechatInit1(mac);
  501. receive_data = '';
  502. revc_count == 0;
  503. ble.sendMessageToDevice(cmd_arr, (res) => {})
  504. } else if (flag_wechat == '2713') {
  505. var cmd_arr = request33.wechatInit2(mac);
  506. receive_data = '';
  507. ble.sendMessageToDevice(cmd_arr, (res) => {
  508. receive_data = '';
  509. revc_count == 0;
  510. callBack.call(this, {
  511. code: config.bleSuccessCode(),
  512. msg: '连接成功',
  513. data: null
  514. })
  515. })
  516. } else {
  517. callBack.call(this, {
  518. code: config.bleErrorCode(),
  519. msg: '非法连接',
  520. data: null
  521. })
  522. }
  523. } else {
  524. callBack.call(this, {
  525. code: config.bleErrorCode(),
  526. msg: '非法连接',
  527. data: null
  528. })
  529. }
  530. }
  531. }