Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

GenvictBleUtil.js 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  1. var a = require('./GenvictDataUtil.js');
  2. const common_vendor = require("../../../common/vendor.js");
  3. var uni = common_vendor.index
  4. var b = [];
  5. var c = '';
  6. var d = '';
  7. var e = '';
  8. var f = '';
  9. var g = [];
  10. var h = 0;
  11. var i = 3;
  12. var j;
  13. var k = false;
  14. var l = [];
  15. var m = [];
  16. var n = 0;
  17. var o = 0;
  18. var p = 0;
  19. var q = "";
  20. var r = 0;
  21. var s;
  22. var t = new Array();
  23. var u = 0;
  24. var v;
  25. var w = false;
  26. const ARTC_SERVICE_UUID = '0000FEE7-0000-1000-8000-00805F9B34FB';
  27. const ARTC_WRTIE_UUID = '0000FEC7-0000-1000-8000-00805F9B34FB';
  28. const ARTC_READ_UUID = '0000FEC8-0000-1000-8000-00805F9B34FB';
  29. const SUCCESS_CODE = 0;
  30. const FAILED_CODE = 1;
  31. const PLOY_SERVICE_FAILE = '2';
  32. const PLOY_NO_AIM_SERVICE = '3';
  33. const PLOY_CHARACTERISTIC_FAILE = '4';
  34. const PLOY_NO_CHARACTERISTIC = '5';
  35. const PLOY_NOTIY_FAILE = '6';
  36. const COS_SUCCESS_CODE = '9000';
  37. function openBle(x) {
  38. uni.openBluetoothAdapter({
  39. success(res) {
  40. typeof x === 'function' && x(SUCCESS_CODE)
  41. },
  42. fail() {
  43. typeof x === 'function' && x(FAILED_CODE)
  44. }
  45. })
  46. };
  47. function closeBle(x) {
  48. uni.closeBluetoothAdapter({
  49. success(res) {
  50. typeof x === 'function' && x(SUCCESS_CODE)
  51. },
  52. fail() {
  53. typeof x === 'function' && x(FAILED_CODE)
  54. }
  55. })
  56. };
  57. function scanBle(x) {
  58. b = [];
  59. uni.startBluetoothDevicesDiscovery({
  60. success(res) {
  61. uni.onBluetoothDeviceFound((res) => {
  62. for (let i = 0; i < res.devices.length; i++) {
  63. let isHave = false;
  64. for (let j = 0; j < b.length; j++) {
  65. if (res.devices[i].deviceId === b[j].deviceId) {
  66. isHave = true;
  67. break
  68. }
  69. };
  70. if (!isHave) {
  71. console.log(res.devices[i].name);
  72. if (res.devices[i].name.indexOf("GV__GD_FK") == 0 || res.devices[i].name
  73. .indexOf("GV_HCB_Q1") == 0 || res.devices[i].name.indexOf("GV__BT_WX") ==
  74. 0 || res.devices[i].name.indexOf("GENVICT") == 0 || res.devices[i].name
  75. .indexOf("GXETC_GV") == 0 || res.devices[i].name.indexOf("zj") == 0 || res
  76. .devices[i].name.indexOf("JY") == 0 || res.devices[i].name.indexOf(
  77. "SD_BT_WX") == 0 || res.devices[i].name.indexOf("GS") == 0 || res
  78. .devices[i].name.indexOf("GV") == 0 || res.devices[i].name.indexOf("ETC") ==
  79. 0 || res.devices[i].name.indexOf("HN") == 0) {
  80. b.push(res.devices[i]);
  81. x(res.devices[i])
  82. }
  83. }
  84. }
  85. })
  86. },
  87. fail() {
  88. typeof x === 'function' && x(FAILED_CODE)
  89. }
  90. })
  91. };
  92. function connectDevice(x, y, z) {
  93. uni.stopBluetoothDevicesDiscovery({
  94. success(res) {
  95. console.log('停止搜索')
  96. }
  97. });
  98. openBle(function(A) {
  99. uni.onBLEConnectionStateChange(function(B) {
  100. if (!B.connected) {
  101. var C = new Object();
  102. C.code = FAILED_CODE;
  103. C.data = '';
  104. C.msg = '蓝牙连接已断开';
  105. typeof z === 'function' && z(C)
  106. }
  107. })
  108. });
  109. c = x.deviceId;
  110. uni.createBLEConnection({
  111. deviceId: c,
  112. success(res) {
  113. deployBle(y)
  114. },
  115. fail() {
  116. var A = new Object();
  117. A.code = FAILED_CODE;
  118. A.msg = '连接失败';
  119. A.data = '';
  120. typeof y === 'function' && y(A)
  121. }
  122. })
  123. };
  124. function deployBle(x) {
  125. var y = this;
  126. e = '';
  127. f = '';
  128. uni.getBLEDeviceServices({
  129. deviceId: c,
  130. success(res) {
  131. for (let i = 0; i < res.services.length; i++) {
  132. let serviceuuid = res.services[i].uuid;
  133. if (serviceuuid === ARTC_SERVICE_UUID) {
  134. d = serviceuuid;
  135. uni.getBLEDeviceCharacteristics({
  136. deviceId: c,
  137. serviceId: d,
  138. success(res) {
  139. for (let i = 0; i < res.characteristics.length; i++) {
  140. let chauuid = res.characteristics[i].uuid;
  141. if (chauuid === ARTC_READ_UUID) {
  142. f = chauuid
  143. } else if (chauuid === ARTC_WRTIE_UUID) {
  144. e = chauuid
  145. }
  146. };
  147. if (e.length === 0 || f.length === 0) {
  148. typeof x === 'function' && x(PLOY_NO_CHARACTERISTIC)
  149. } else {
  150. openReceiveData((code) => {
  151. if (code === SUCCESS_CODE) {
  152. if (a.protocolType() == "normal") {
  153. typeof x === 'function' && x(SUCCESS_CODE)
  154. } else {
  155. v = x
  156. }
  157. } else {
  158. var A = new Object();
  159. A.code = FAILED_CODE;
  160. A.msg = '连接失败';
  161. A.data = '';
  162. typeof x === 'function' && x(A)
  163. }
  164. })
  165. }
  166. },
  167. fail() {
  168. var A = new Object();
  169. A.code = FAILED_CODE;
  170. A.msg = '连接失败';
  171. A.data = '';
  172. typeof x === 'function' && x(A)
  173. }
  174. });
  175. return
  176. }
  177. };
  178. var z = new Object();
  179. z.code = FAILED_CODE;
  180. z.msg = '连接失败';
  181. z.data = '';
  182. typeof x === 'function' && x(z)
  183. },
  184. fail() {
  185. var z = new Object();
  186. z.code = FAILED_CODE;
  187. z.msg = '连接失败';
  188. z.data = '';
  189. typeof x === 'function' && x(z)
  190. }
  191. })
  192. };
  193. function disconnectDevice(x) {
  194. uni.closeBLEConnection({
  195. deviceId: c,
  196. success(res) {
  197. var y = new Object();
  198. y.code = SUCCESS_CODE;
  199. y.msg = '蓝牙已断开';
  200. y.data = '';
  201. x(y)
  202. },
  203. fail() {
  204. var y = new Object();
  205. y.code = FAILED_CODE;
  206. y.msg = '蓝牙连接断开失败';
  207. y.data = '';
  208. x(y)
  209. }
  210. })
  211. };
  212. function openReceiveData(x) {
  213. uni.notifyBLECharacteristicValueChange({
  214. deviceId: c,
  215. serviceId: d,
  216. characteristicId: f,
  217. state: true,
  218. success(res) {
  219. typeof x === 'function' && x(SUCCESS_CODE)
  220. },
  221. fail() {
  222. typeof x === 'function' && x(FAILED_CODE)
  223. }
  224. });
  225. uni.onBLECharacteristicValueChange((characteristic) => {
  226. let hex = Array.prototype.map.call(new Uint8Array(characteristic.value), x => ('00' + x.toString(16))
  227. .slice(-2)).join('');
  228. console.log('接收数据:' + hex);
  229. if (a.protocolType() == "normal") {
  230. normalDataResponse(hex)
  231. } else {
  232. let ST = hex.substring(0, 2).toUpperCase();
  233. if (q.length == 0) {
  234. if (ST == "FE") {
  235. r = parseInt(hex.substring(4, 8), 16) * 2
  236. } else {
  237. return
  238. }
  239. };
  240. q = q + hex;
  241. if (q.length == r) {
  242. let cmdId = q.substring(8, 12);
  243. if (cmdId == "2711") {
  244. authResponse()
  245. } else if (cmdId == "2713") {
  246. initResponse()
  247. } else if (cmdId == "2712") {
  248. let frame = q.substring(16);
  249. wechatDataResponse(frame)
  250. }
  251. } else if (q.length > r) {
  252. q = "";
  253. r = 0
  254. }
  255. }
  256. })
  257. };
  258. function initResponse() {
  259. let bufferArray = a.makeInitResponse();
  260. let hasErr = false;
  261. w = true;
  262. sendData(bufferArray, function(x, y) {
  263. if (x != 0) {
  264. hasErr = true
  265. }
  266. })
  267. };
  268. function authResponse() {
  269. let bufferArray = a.makeAuthResponse();
  270. sendData(bufferArray, function(x, y) {
  271. if (x != 0) {
  272. typeof s == 'function' && s(FAILED_CODE)
  273. }
  274. })
  275. };
  276. function wechatDataResponse(x) {
  277. q = "";
  278. r = 0;
  279. x = x.substring(8, 8 + parseInt(x.substring(6, 8), 16) * 2);
  280. if (t.length == 0) {
  281. if (a.protocolType() == "xinguobiao") {
  282. let ctl = parseInt(x.substring(2, 6), 16);
  283. u = ctl - 0x8000
  284. } else if (a.protocolType() == "genvict") {
  285. let ctl = parseInt(x.substring(4, 6), 16);
  286. u = ctl - 0x80 + 1
  287. }
  288. };
  289. t.push(x);
  290. if (u == t.length) {
  291. let completeValue = "";
  292. for (let i = 0; i < t.length; i++) {
  293. let temp = "" + t[i];
  294. completeValue += temp.substring(8, temp.length - 2)
  295. };
  296. t = new Array();
  297. u = 0;
  298. if (parseInt(completeValue.substring(0, 2), 16) != 0x96) {
  299. typeof j == 'function' && j(SUCCESS_CODE, completeValue)
  300. }
  301. }
  302. };
  303. function normalDataResponse(x) {
  304. q = "";
  305. r = 0;
  306. if (t.length == 0) {
  307. let ctl = parseInt(x.substring(6, 8));
  308. u = ctl + 1
  309. };
  310. t.push(x);
  311. if (u == t.length) {
  312. let completeValue = "";
  313. for (let i = 0; i < t.length; i++) {
  314. let temp = "" + t[i];
  315. completeValue += temp.substring(10, temp.length - 2)
  316. };
  317. t = new Array();
  318. u = 0;
  319. if (parseInt(completeValue.substring(0, 2), 16) != 0x96) {
  320. typeof j == 'function' && j(SUCCESS_CODE, completeValue)
  321. }
  322. }
  323. };
  324. function sendData(x, y) {
  325. var z = this;
  326. q = '';
  327. g = x;
  328. h = 0;
  329. j = y;
  330. m = [];
  331. l = [];
  332. t = new Array();
  333. startSendData()
  334. };
  335. function startSendData() {
  336. var y = this;
  337. let buffer = g[h];
  338. let hex = Array.prototype.map.call(new Uint8Array(buffer), x => ('00' + x.toString(16)).slice(-2)).join('');
  339. console.log('发送数据:' + hex);
  340. uni.writeBLECharacteristicValue({
  341. deviceId: c,
  342. serviceId: d,
  343. characteristicId: e,
  344. value: buffer,
  345. success(res) {
  346. i = 3;
  347. h++;
  348. if (h < g.length) {
  349. startSendData()
  350. } else {
  351. if (w) {
  352. var z = new Object();
  353. z.code = SUCCESS_CODE;
  354. z.msg = '连接成功';
  355. z.data = '';
  356. typeof v == 'function' && v(z);
  357. w = false
  358. }
  359. }
  360. },
  361. fail() {
  362. console.log('发送失败了:')
  363. }
  364. })
  365. };
  366. function initDevice(x) {
  367. var y = this;
  368. console.log('接收数据:xxxx');
  369. y.sendData(a.initDeviceSendData(), (code, res) => {
  370. if (code === SUCCESS_CODE) {
  371. let stateCode = res.substring(res.length - 4);
  372. let reapdu = res.substring(18, res.length - 4);
  373. if (a.protocolType() == "normal") {
  374. console.log('接收数据:' + res);
  375. reapdu = res
  376. };
  377. typeof x === 'function' && x(code, reapdu)
  378. } else {
  379. typeof x === 'function' && x(FAILED_CODE, res)
  380. }
  381. })
  382. };
  383. function deviceChannel(x, y) {
  384. var z = this;
  385. z.sendData(a.makeDeviceChannelSendData(x), (code, res) => {
  386. if (code === SUCCESS_CODE) {
  387. let stateCode = res.substring(res.length - 4);
  388. let reapdu = res.substring(18, res.length - 4);
  389. typeof y === 'function' && y(code, res)
  390. } else {
  391. typeof y === 'function' && y(FAILED_CODE, res)
  392. }
  393. })
  394. };
  395. function authenChannel(x, y, z) {
  396. var A = this;
  397. A.sendData(a.makeAuthenChannelSendData(x, y), (code, res) => {
  398. if (code === SUCCESS_CODE) {
  399. let stateCode = res.substring(res.length - 4);
  400. let reapdu = res.substring(18, res.length - 4);
  401. typeof z === 'function' && z(code, res)
  402. } else {
  403. typeof z === 'function' && z(FAILED_CODE, res)
  404. }
  405. })
  406. };
  407. function selfDeviceChannel(x, y) {
  408. var z = this;
  409. z.sendData(a.makeSelfDeviceChannelSendData(x), (code, res) => {
  410. if (code === SUCCESS_CODE) {
  411. let stateCode = res.substring(res.length - 4);
  412. let reapdu = res.substring(18, res.length - 4);
  413. typeof y === 'function' && y(code, res)
  414. } else {
  415. typeof y === 'function' && y(FAILED_CODE, res)
  416. }
  417. })
  418. };
  419. function deviceAuthenChannel(x, y, z) {
  420. var A = this;
  421. A.sendData(a.makeDeviceAuthenSendData(x, y), (code, res) => {
  422. if (code === SUCCESS_CODE) {
  423. let stateCode = res.substring(res.length - 4);
  424. let reapdu = res.substring(18, res.length - 4);
  425. typeof z === 'function' && z(code, res)
  426. } else {
  427. typeof z === 'function' && z(FAILED_CODE, res)
  428. }
  429. })
  430. };
  431. function cosChannel(x, y, z) {
  432. var A = this;
  433. A.sendData(a.makecosSendData(x, y), (code, res) => {
  434. let stateCode = res.substring(2, 4);
  435. if (code === SUCCESS_CODE && stateCode === '00') {
  436. let reapdu = res.substring(10, res.length);
  437. if (a.protocolType() == "genvict") {
  438. var B = tlvUnpackData(reapdu);
  439. for (let i = 0; i < B.length; i++) {
  440. let cosCode = B[i].substring(B[i].length - 4, B[i].length)
  441. };
  442. typeof z === 'function' && z(code, B)
  443. } else {
  444. typeof z === 'function' && z(code, res)
  445. }
  446. } else {
  447. typeof z === 'function' && z(FAILED_CODE, res)
  448. }
  449. })
  450. };
  451. function tlvUnpackData(x) {
  452. let tlvMode = parseInt(x.substring(2, 4), 16);
  453. let cosStr;
  454. let tlvLen;
  455. var y = new Array();
  456. if (tlvMode <= 0x80) {
  457. tlvLen = parseInt(x.substring(2, 4), 16) * 2;
  458. cosStr = x.substring(4, x.length)
  459. } else if (tlvMode == 0x81) {
  460. tlvLen = parseInt(x.substring(4, 6), 16) * 2;
  461. cosStr = x.substring(6, x.length)
  462. } else {
  463. tlvLen = parseInt(x.substring(4, 8), 16) * 2;
  464. cosStr = x.substring(8, x.length)
  465. };
  466. let i = 0;
  467. let index = 0;
  468. let dataLen = 0;
  469. while (tlvLen > 0) {
  470. dataLen = parseInt(cosStr.substring(2 + index, 4 + index), 16) * 2;
  471. y[i] = cosStr.substring(4 + index, dataLen + 4 + index);
  472. index = index + 4 + dataLen;
  473. i++;
  474. tlvLen = tlvLen - 4 - dataLen
  475. };
  476. return y
  477. };
  478. function gvEsamChannel(x, y, z) {
  479. var A = this;
  480. A.sendData(a.makeGVEsamSendData(x, y), (code, res) => {
  481. let stateCode = res.substring(2, 4);
  482. if (code === SUCCESS_CODE && stateCode === '00') {
  483. let reapdu = res.substring(10, res.length);
  484. if (a.protocolType() == "genvict") {
  485. var B = tlvUnpackData(reapdu);
  486. for (let i = 0; i < B.length; i++) {
  487. let cosCode = B[i].substring(B[i].length - 4, B[i].length)
  488. };
  489. typeof z === 'function' && z(code, B)
  490. } else {
  491. typeof z === 'function' && z(code, res)
  492. }
  493. } else {
  494. typeof z === 'function' && z(FAILED_CODE, res)
  495. }
  496. })
  497. };
  498. function transGVEsamCommand(x, y) {
  499. var z = this;
  500. z.sendData(a.makeGVtransEsamSendData('20', x), (code, res) => {
  501. if (code === SUCCESS_CODE) {
  502. let stateCode = res.substring(res.length - 4);
  503. let reapdu = res.substring(18, res.length - 4);
  504. typeof y === 'function' && y(code, res)
  505. } else {
  506. typeof y === 'function' && y(FAILED_CODE, res)
  507. }
  508. })
  509. };
  510. function transCommand(x, y, z) {
  511. var A = this;
  512. A.sendData(a.maketransSendData(x, y), (code, res) => {
  513. if (code === SUCCESS_CODE) {
  514. let stateCode = res.substring(res.length - 4);
  515. let reapdu = res.substring(18, res.length - 4);
  516. typeof z === 'function' && z(code, res)
  517. } else {
  518. typeof z === 'function' && z(FAILED_CODE, res)
  519. }
  520. })
  521. };
  522. function obuChannel(x, y) {
  523. var z = this;
  524. z.sendData(a.makeObuChannel(x), (code, res) => {
  525. if (code === SUCCESS_CODE) {
  526. let stateCode = res.substring(res.length - 4);
  527. let reapdu = res.substring(18, res.length - 4);
  528. typeof y === 'function' && y(code, res)
  529. } else {
  530. typeof y === 'function' && y(FAILED_CODE, res)
  531. }
  532. })
  533. };
  534. function authenC0(x, y) {
  535. let send = "C0" + x;
  536. var z = this;
  537. z.sendData(a.makeAuthenSendData(send), (code, res) => {
  538. if (code === SUCCESS_CODE) {
  539. let stateCode = res.substring(res.length - 4);
  540. let reapdu = res.substring(18, res.length - 4);
  541. typeof y === 'function' && y(code, res)
  542. } else {
  543. typeof y === 'function' && y(FAILED_CODE, res)
  544. }
  545. })
  546. };
  547. function authenC1(x, y, z, A, B, C, D) {
  548. let send = "C1" + x + y + z + A + B + C;
  549. var E = this;
  550. E.sendData(a.makeAuthenSendData(send), (code, res) => {
  551. if (code === SUCCESS_CODE) {
  552. let stateCode = res.substring(res.length - 4);
  553. let reapdu = res.substring(18, res.length - 4);
  554. typeof D === 'function' && D(code, res)
  555. } else {
  556. typeof D === 'function' && D(FAILED_CODE, res)
  557. }
  558. })
  559. };
  560. function readCard(x) {
  561. var y = '0';
  562. let reapdu = '12'
  563. };
  564. function resetCard(x) {
  565. var y = this;
  566. y.sendData(a.makeResetCard(), (code, res) => {
  567. if (code === SUCCESS_CODE) {
  568. let stateCode = res.substring(res.length - 4);
  569. let reapdu = res.substring(18, res.length - 4);
  570. typeof x === 'function' && x(code, res)
  571. } else {
  572. typeof x === 'function' && x(FAILED_CODE, res)
  573. }
  574. })
  575. };
  576. function getICCInfo(x) {
  577. var y = this;
  578. var z = new Array();
  579. z[0] = "00A40000021001";
  580. z[1] = "00B095002B";
  581. z[2] = "805C000204";
  582. var A = "00";
  583. if (a.protocolType() == "xinguobiao") {
  584. A = "10"
  585. };
  586. y.cosChannel(A, z, function(B, C) {
  587. if (B == 0 && C[1].length >= 90) {
  588. var D = new Object();
  589. D.file0015 = C[1];
  590. D.provider = C[1].substring(0, 16);
  591. D.cardType = C[1].substring(16, 18);
  592. D.cardVersion = C[1].substring(18, 20);
  593. D.cardId = C[1].substring(20, 40);
  594. D.signedDate = C[1].substring(40, 48);
  595. D.expiredDate = C[1].substring(48, 56);
  596. D.vehicleNumber = C[1].substring(56, 80);
  597. D.userType = C[1].substring(80, 82);
  598. if (D.cardVersion == 0x40) {
  599. D.plateColor = C[1].substring(86, 88);
  600. D.vehicleModel = C[1].substring(88, 90)
  601. } else {
  602. D.plateColor = C[1].substring(88, 90);
  603. D.vehicleModel = '00'
  604. };
  605. D.balance = parseInt(C[2].substring(0, 8), 16);
  606. typeof x === 'function' && x(B, D)
  607. } else {
  608. typeof x === 'function' && x(FAILED_CODE, C)
  609. }
  610. })
  611. };
  612. function pinCode(x, y, z, A, B, C) {
  613. var D = this;
  614. var E = new Array();
  615. E[0] = "00A40000021001";
  616. E[1] = "00200000" + a.numberToHexString(x.length / 2, 1, true) + x;
  617. E[2] = "805000" + y + "0B" + z + a.numberToHexString(A, 4, true) + B + "10";
  618. console.log("回复数据:" + E);
  619. var F = "00";
  620. if (a.protocolType() == "xinguobiao") {
  621. F = "10"
  622. };
  623. D.cosChannel(F, E, function(G, H) {
  624. if (G == 0) {
  625. var I = new Object();
  626. I.balance = H[2].substring(0, 8);
  627. I.counter = H[2].substring(8, 12);
  628. I.random = H[2].substring(16, 24);
  629. I.mac1 = H[2].substring(24, 32);
  630. typeof C === 'function' && C(G, I)
  631. } else {
  632. typeof C === 'function' && C(FAILED_CODE, H)
  633. }
  634. })
  635. };
  636. function loadCreditWrite(x, y, z) {
  637. var A = this;
  638. var B = new Array();
  639. B[0] = "805200000B" + x + y;
  640. var C = "00";
  641. if (a.protocolType() == "xinguobiao") {
  642. C = "10"
  643. };
  644. A.cosChannel(C, B, function(D, E) {
  645. if (D == 0 && E.length == 1) {
  646. typeof z === 'function' && z(D, E)
  647. } else {
  648. typeof z === 'function' && z(FAILED_CODE, E)
  649. }
  650. })
  651. };
  652. function transA1reqData(x, y) {
  653. var z = this;
  654. z.sendData(a.makeTransA1reqData(x), (code, res) => {
  655. if (code === SUCCESS_CODE) {
  656. let stateCode = res.substring(res.length - 4);
  657. let reapdu = res.substring(10, res.length - 4);
  658. let credit_length = 0;
  659. if (reapdu.length > 14) {
  660. credit_length = parseInt(res.substring(12, 14), 16) * 2
  661. } else {
  662. typeof y === 'function' && y(code, "返回不正常包。")
  663. };
  664. let sign_length = 0;
  665. if (reapdu.length > 4 + credit_length) {
  666. sign_length = parseInt(res.substring(16 + credit_length, 16 + credit_length + 2), 16) * 2
  667. } else {
  668. typeof y === 'function' && y(code, "返回不正常包。")
  669. };
  670. if (res.length > 16 + credit_length + sign_length) {
  671. var A = new Object();
  672. A.original = res.substring(14, 14 + credit_length);
  673. A.sign = res.substring(18 + credit_length, 18 + credit_length + sign_length);
  674. typeof y === 'function' && y(code, A)
  675. } else {
  676. typeof y === 'function' && y(code, "返回不正常包。")
  677. }
  678. } else {
  679. typeof y === 'function' && y(FAILED_CODE, res)
  680. }
  681. })
  682. };
  683. function loadCreditT0reqData(x, y) {
  684. var z = this;
  685. z.sendData(a.makeLoadCreditT0reqData(x), (code, res) => {
  686. if (code === SUCCESS_CODE) {
  687. let stateCode = res.substring(res.length - 4);
  688. let reapdu = res.substring(10, res.length - 4);
  689. let credit_length = 0;
  690. if (reapdu.length > 14) {
  691. credit_length = parseInt(res.substring(12, 14), 16) * 2
  692. } else {
  693. typeof y === 'function' && y(code, "返回不正常包。")
  694. };
  695. let sign_length = 0;
  696. if (reapdu.length > 4 + credit_length) {
  697. sign_length = parseInt(res.substring(16 + credit_length, 16 + credit_length + 2), 16) * 2
  698. } else {
  699. typeof y === 'function' && y(code, "返回不正常包。")
  700. };
  701. if (res.length > 16 + credit_length + sign_length) {
  702. var A = new Object();
  703. A.original = res.substring(14, 14 + credit_length);
  704. A.sign = res.substring(18 + credit_length, 18 + credit_length + sign_length);
  705. typeof y === 'function' && y(code, A)
  706. } else {
  707. typeof y === 'function' && y(code, "返回不正常包。")
  708. }
  709. } else {
  710. typeof y === 'function' && y(FAILED_CODE, res)
  711. }
  712. })
  713. };
  714. function esamMiWen(x, y) {
  715. var z = this;
  716. z.sendData(a.makeEsamMiWen(x), (code, res) => {
  717. if (code === SUCCESS_CODE) {
  718. let stateCode = res.substring(res.length - 4);
  719. let reapdu = res.substring(10, res.length - 4);
  720. let credit_length = 0;
  721. if (res.length > 14) {
  722. credit_length = parseInt(res.substring(12, 14), 16) * 2
  723. } else {
  724. typeof y === 'function' && y(code, "返回不正常包。")
  725. };
  726. if (res.length > 12 + credit_length) {
  727. var A = new Object();
  728. A.original = res.substring(14, 14 + credit_length);
  729. typeof y === 'function' && y(code, A)
  730. } else {
  731. typeof y === 'function' && y(code, "返回不正常包。")
  732. }
  733. } else {
  734. typeof y === 'function' && y(FAILED_CODE, res)
  735. }
  736. })
  737. };
  738. function initiateOBU(x) {
  739. var y = this;
  740. console.log("initiateOBU");
  741. var z = new Array();
  742. z[0] = "00A40000023F00";
  743. z[1] = "00B0810063";
  744. y.gvEsamChannel('20', z, function(A, B) {
  745. if (A === SUCCESS_CODE) {
  746. console.log("回复数据:" + B);
  747. let initiateState = B[1].substring(52, 52 + 2);
  748. console.log("initiateState:" + initiateState);
  749. if (initiateState === '02') {
  750. var C = new Array();
  751. C[0] = "0059000001";
  752. y.gvEsamChannel('20', C, function(D, E) {
  753. typeof x === 'function' && x(D, E)
  754. });
  755. console.log("状态1")
  756. } else if (initiateState === '01') {
  757. typeof x === 'function' && x(FAILED_CODE, "OBU已激活。");
  758. console.log("状态2")
  759. } else if (initiateState === '00') {
  760. typeof x === 'function' && x(FAILED_CODE, "OBU已失效。");
  761. console.log("状态3")
  762. }
  763. }
  764. })
  765. };
  766. function transCmd(x, y, z) {
  767. var A = y;
  768. if (y == '10') {
  769. A = '00'
  770. };
  771. sendData(a.makeGVEsamSendData(A, x), (code, res) => {
  772. let stateCode = res.substring(2, 4);
  773. console.log("res.substring(2, 4):" + res);
  774. console.log("res.substring(2, 4):" + res.substring(2, 4));
  775. if (code === SUCCESS_CODE && stateCode === '00') {
  776. let reapdu = res.substring(10, res.length);
  777. console.log("reapdu:" + reapdu);
  778. if (a.protocolType() == "genvict") {
  779. var B = tlvUnpackData(reapdu);
  780. var C = new Object();
  781. C.code = SUCCESS_CODE;
  782. C.msg = '操作成功';
  783. C.data = B;
  784. typeof z === 'function' && z(C)
  785. }
  786. } else {
  787. var B = new Object();
  788. B.code = FAILED_CODE;
  789. B.msg = '操作失败';
  790. B.data = res;
  791. typeof z === 'function' && z(B)
  792. }
  793. })
  794. };
  795. module.exports = {
  796. sendData: sendData,
  797. openBle: openBle,
  798. closeBle: closeBle,
  799. scanBle: scanBle,
  800. connectDevice: connectDevice,
  801. deployBle: deployBle,
  802. disconnectDevice: disconnectDevice,
  803. initDevice: initDevice,
  804. deviceChannel: deviceChannel,
  805. selfDeviceChannel: selfDeviceChannel,
  806. cosChannel: cosChannel,
  807. authenC0: authenC0,
  808. authenC1: authenC1,
  809. openReceiveData: openReceiveData,
  810. deviceAuthenChannel: deviceAuthenChannel,
  811. gvEsamChannel: gvEsamChannel,
  812. transGVEsamCommand: transGVEsamCommand,
  813. transCommand: transCommand,
  814. resetCard: resetCard,
  815. getICCInfo: getICCInfo,
  816. pinCode: pinCode,
  817. loadCreditWrite: loadCreditWrite,
  818. authenChannel: authenChannel,
  819. transA1reqData: transA1reqData,
  820. loadCreditT0reqData: loadCreditT0reqData,
  821. esamMiWen: esamMiWen,
  822. obuChannel: obuChannel,
  823. initiateOBU: initiateOBU,
  824. transCmd: transCmd
  825. };