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.

jy-bluetooth-obu-interface.js 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. var dataUtil = require("./jy-bluetooth-obu-DataUtil.js"),
  2. errorCode = require("./jy-bluetooth-obu-ErrorCode.js"),
  3. XXXDeviceId = "",
  4. XXXServiceId = "",
  5. XXXWriteId = "",
  6. XXXReadId = "",
  7. XXXSendBuffer = [],
  8. XXXSendIndex = 0,
  9. XXXResendCount = 3,
  10. sendCallback, stephex = "",
  11. frameLength = 0,
  12. deployCall, frameArray = [],
  13. frameCount = 0,
  14. connectCall, isInitResponse = !1,
  15. SERVICE_UUID_FEE7 = "0000FEE7-0000-1000-8000-00805F9B34FB",
  16. WRITE_UUID_FEC7 = "0000FEC7-0000-1000-8000-00805F9B34FB",
  17. READ_UUID_FEC8 = "0000FEC8-0000-1000-8000-00805F9B34FB",
  18. SHORT_SERVICE_UUID_FEE7 =
  19. "FEE7",
  20. SHORT_WRITE_UUID_FEC7 = "FEC7",
  21. SHORT_READ_UUID_FEC8 = "FEC8",
  22. SERVICE_UUID = SERVICE_UUID_FEE7,
  23. WRITE_UUID = WRITE_UUID_FEC7,
  24. READ_UUID = READ_UUID_FEC8,
  25. SHORT_SERVICE_UUID = SHORT_SERVICE_UUID_FEE7,
  26. SHORT_WRITE_UUID = SHORT_WRITE_UUID_FEC7,
  27. SHORT_READ_UUID = SHORT_READ_UUID_FEC8,
  28. foundDevices = [],
  29. COS_SUCCESS_CODE = "9000",
  30. isScanFinish, connectDevice, reConnectNum = 1,
  31. scanTimeoutId = null,
  32. mScanTimeOut = 5E3,
  33. isBLEConnectionStateChange = !1,
  34. connectTimeoutId = null,
  35. isBleConnected = !1,
  36. isSending = !1,
  37. sendDataTimeoutId;
  38. function isConnected() {
  39. return isBleConnected
  40. }
  41. function initData() {
  42. stephex = "";
  43. XXXSendBuffer = [];
  44. XXXSendIndex = 0;
  45. frameArray = []
  46. }
  47. function sendData(a, b) {
  48. stephex = "";
  49. XXXSendBuffer = a;
  50. XXXSendIndex = 0;
  51. sendCallback = b;
  52. frameArray = [];
  53. startSendData(b)
  54. }
  55. function startSendData(a) {
  56. var b = XXXSendBuffer[XXXSendIndex],
  57. c = Array.prototype.map.call(new Uint8Array(b), function(b) {
  58. return ("00" + b.toString(16)).slice(-2)
  59. }).join("");
  60. dataUtil.consoleLog("\u53d1\u9001\u6570\u636e\uff1a" + c);
  61. wx.writeBLECharacteristicValue({
  62. deviceId: XXXDeviceId,
  63. serviceId: XXXServiceId,
  64. characteristicId: XXXWriteId,
  65. value: b,
  66. success: function(b) {
  67. XXXResendCount = 3;
  68. XXXSendIndex++;
  69. XXXSendIndex < XXXSendBuffer.length ? startSendData() : isInitResponse && ("function" ==
  70. typeof sendCallback && sendCallback(errorCode.SUCCESS_CODE),
  71. isInitResponse = !1)
  72. },
  73. fail: function() {
  74. dataUtil.consoleLog("\u53d1\u9001\u5931\u8d25\u4e86\uff1a");
  75. "function" == typeof a && a(errorCode.FAILED_CODE)
  76. }
  77. })
  78. }
  79. function openReceiveData(a) {
  80. wx.notifyBLECharacteristicValueChange({
  81. deviceId: XXXDeviceId,
  82. serviceId: XXXServiceId,
  83. characteristicId: XXXReadId,
  84. state: !0,
  85. success: function(b) {
  86. "function" === typeof a && a(errorCode.SUCCESS_CODE)
  87. },
  88. fail: function() {
  89. "function" === typeof a && a(errorCode.FAILED_CODE)
  90. }
  91. });
  92. wx.onBLECharacteristicValueChange(function(b) {
  93. b = Array.prototype.map.call(new Uint8Array(b.value), function(b) {
  94. return ("00" + b.toString(16)).slice(-2)
  95. }).join("");
  96. dataUtil.consoleLog("\u63a5\u6536\u6570\u636e\uff1a" + b);
  97. 0 ==
  98. stephex.length && (frameLength = 2 * parseInt(b.substring(4, 8), 16));
  99. stephex += b;
  100. if (stephex.length == frameLength)
  101. if (b = stephex.substring(8, 12), "2711" == b) authResponse();
  102. else if ("2713" == b) initResponse();
  103. else if ("2712" == b) {
  104. b = stephex.substring(16);
  105. if (256 <= b.length - 16) {
  106. var a = 256 * parseInt(b.substring(8, 10), 16) + (2 * parseInt(b.substring(6, 8), 16) - 256);
  107. b = b.substring(10, 10 + a)
  108. } else b = b.substring(8, 8 + 2 * parseInt(b.substring(6, 8), 16));
  109. genvictDataResponse(b)
  110. }
  111. })
  112. }
  113. function initResponse() {
  114. var a = dataUtil.makeInitResponse();
  115. isInitResponse = !0;
  116. var b = {};
  117. sendData(a, function(a, d) {
  118. clearTimeout(connectTimeoutId);
  119. 0 == a ? initDevice(function(a, c) {
  120. dataUtil.consoleLog("res:" + c);
  121. a == errorCode.SUCCESS_CODE ? (2 < c.length && 10 < parseInt(c.substring(0, 2), 16) ? (
  122. dataUtil.consoleLog("\u540e\u88c5OBU"), dataUtil.setProtocolType("genvict"),
  123. dataUtil.setPackageLen(100)) : (dataUtil.consoleLog("\u524d\u88c5OBU"), dataUtil
  124. .setProtocolType("pre"), a = c.substring(2, 4), dataUtil.setPackageLen(parseInt(
  125. a,
  126. 16))), b.code = errorCode.SUCCESS_CODE, b.msg = "\u8fde\u63a5\u6210\u529f") : (b
  127. .code = errorCode.ERR_INIT_DEVICE_CODE, b.msg = "\u8fde\u63a5\u5931\u8d25");
  128. "function" == typeof connectCall && connectCall(b)
  129. }) : (b.code = errorCode.ERR_WX_INIT_CODE, "function" == typeof connectCall && connectCall(b))
  130. })
  131. }
  132. function authResponse() {
  133. var a = dataUtil.makeAuthResponse();
  134. sendData(a, function(b, a) {
  135. 0 != b && (b = {}, b.code = errorCode.ERR_WX_AUTH_CODE, "function" == typeof connectCall && connectCall(
  136. b))
  137. })
  138. }
  139. function genvictDataResponse(a) {
  140. stephex = "";
  141. frameLength = 0;
  142. 0 == frameArray.length && (frameCount = parseInt(a.substring(4, 6), 16) - 128 + 1);
  143. frameArray.push(a);
  144. if (frameCount == frameArray.length) {
  145. a = "";
  146. for (var b = 0; b < frameArray.length; b++) {
  147. var c = "" + frameArray[b];
  148. a += c.substring(8, c.length - 2)
  149. }
  150. frameArray = [];
  151. frameCount = 0;
  152. 150 != parseInt(a.substring(0, 2), 16) && "function" == typeof sendCallback && sendCallback(errorCode
  153. .SUCCESS_CODE, a)
  154. }
  155. }
  156. function deployBle(a) {
  157. initData();
  158. XXXReadId = XXXWriteId = "";
  159. var b = {};
  160. wx.getBLEDeviceServices({
  161. deviceId: XXXDeviceId,
  162. success: function(c) {
  163. dataUtil.consoleLog("getBLEDeviceServices-success");
  164. for (var d = 0; d < c.services.length; d++) {
  165. var e = c.services[d].uuid;
  166. dataUtil.consoleLog("serviceuuid\uff1a" + e);
  167. if (e === SERVICE_UUID || e === SHORT_SERVICE_UUID) {
  168. XXXServiceId = e;
  169. wx.getBLEDeviceCharacteristics({
  170. deviceId: XXXDeviceId,
  171. serviceId: XXXServiceId,
  172. success: function(c) {
  173. for (var d = 0; d < c.characteristics.length; d++) {
  174. var e = c.characteristics[d].uuid;
  175. dataUtil.consoleLog("chauuid" + e);
  176. if (e === READ_UUID || e === SHORT_READ_UUID) XXXReadId = e;
  177. else if (e === WRITE_UUID || e === SHORT_WRITE_UUID)
  178. XXXWriteId = e
  179. }
  180. dataUtil.consoleLog("XXXReadId\uff1a" + XXXReadId);
  181. dataUtil.consoleLog("XXXWriteId" + XXXWriteId);
  182. 0 === XXXWriteId.length || 0 === XXXReadId.length ? (b.code =
  183. errorCode.ERR_CONNECTBLE_CODE, "function" === typeof a && a(
  184. b)) : openReceiveData(function(c) {
  185. dataUtil.consoleLog("openReceiveData");
  186. "0" === c ? "normal" == dataUtil.protocolType() ||
  187. "hlj_ff15" == dataUtil.protocolType() ? (b.code =
  188. errorCode.SUCCESS_CODE,
  189. "function" === typeof a && a(b)) : (dataUtil
  190. .consoleLog("start wx auth init"), connectCall =
  191. a) : (b.code = errorCode.ERR_CONNECTBLE_CODE,
  192. "function" === typeof a && a(b))
  193. })
  194. },
  195. fail: function() {
  196. b.code = errorCode.ERR_DEPLOPBLE_CODE;
  197. "function" === typeof a && a(b)
  198. }
  199. });
  200. return
  201. }
  202. }
  203. b.code = errorCode.ERR_DEPLOPBLE_CODE;
  204. "function" === typeof a && a(b)
  205. },
  206. fail: function() {
  207. b.code = errorCode.ERR_DEPLOPBLE_CODE;
  208. "function" === typeof a && a(b)
  209. }
  210. })
  211. }
  212. function scanBle(a) {
  213. foundDevices = [];
  214. isScanFinish = !1;
  215. var b = {};
  216. clearTimeout(scanTimeoutId);
  217. scanTimeoutId = setTimeout(function() {
  218. isScanFinish || (stopScanBle(function(b) {}), b.code = errorCode.ERR_SCANBLE_TIMEOUT, b.err_msg =
  219. "\u626b\u63cf\u8d85\u65f6", "function" === typeof a && a(b))
  220. }, mScanTimeOut);
  221. wx.startBluetoothDevicesDiscovery({
  222. success: function(c) {
  223. wx.onBluetoothDeviceFound(function(c) {
  224. for (var d = 0; d < c.devices.length && !isInvalidBle(c.devices[d]) && !isSameBle(c
  225. .devices[d]); d++) dataUtil.consoleLog("name:" + c.devices[d].name +
  226. " address:" + c.devices[d].deviceId), foundDevices.push(c.devices[d]), b
  227. .code = errorCode.SUCCESS_CODE, b.devices = foundDevices, isScanFinish = !0, a(
  228. b)
  229. })
  230. },
  231. fail: function() {
  232. b.code = errorCode.ERR_SCANBLE_CODE;
  233. "function" === typeof a && a(b)
  234. }
  235. })
  236. }
  237. function setScanTimeOut(a) {
  238. mScanTimeOut = a
  239. }
  240. function isInvalidBle(a) {
  241. return null == a || null == a.deviceId || 0 >= a.deviceId.length || null == a.name || 0 >= a.name.length ? !0 : !1
  242. }
  243. function isSameBle(a) {
  244. var b = !1;
  245. foundDevices.forEach(function(c) {
  246. a.name === c.name && a.deviceId === c.deviceId && (b = !0)
  247. });
  248. return b
  249. }
  250. function openBle(a) {
  251. var b = {};
  252. wx.openBluetoothAdapter({
  253. success: function(c) {
  254. dataUtil.consoleLog("openBle success");
  255. b.code = errorCode.SUCCESS_CODE;
  256. "function" === typeof a && a(b)
  257. },
  258. fail: function() {
  259. dataUtil.consoleLog("openBle fail");
  260. b.code = errorCode.ERR_OPENBLE_CODE;
  261. "function" === typeof a && a(b)
  262. }
  263. })
  264. }
  265. function stopScanBle(a) {
  266. var b = {};
  267. isScanFinish = !0;
  268. wx.stopBluetoothDevicesDiscovery({
  269. success: function(c) {
  270. dataUtil.consoleLog("\u505c\u6b62\u641c\u7d22\u84dd\u7259\u8bbe\u5907");
  271. b.code = errorCode.SUCCESS_CODE;
  272. "function" === typeof a && a(b)
  273. },
  274. fail: function() {
  275. dataUtil.consoleLog("\u505c\u6b62\u641c\u7d22\u84dd\u7259\u8bbe\u5907\u5931\u8d25");
  276. b.code = errorCode.ERR_STOPSCANBLE_CODE;
  277. "function" === typeof a && a(b)
  278. }
  279. })
  280. }
  281. function connectBle(a, b) {
  282. var c = {};
  283. clearTimeout(connectTimeoutId);
  284. connectTimeoutId = setTimeout(function() {
  285. disconnectBle(function() {});
  286. c.code = errorCode.ERR_CONNECT_TIMEOUT;
  287. c.err_msg = "\u8fde\u63a5\u8d85\u65f6";
  288. "function" === typeof b && b(c)
  289. }, 1E4);
  290. startOnBleConnectionStateChange();
  291. connectDevice = a;
  292. XXXDeviceId = a.deviceId;
  293. dataUtil.consoleLog("\u5f00\u59cb\u8fde\u63a5");
  294. reConnectNum = 1;
  295. createBleConnection(function(a) {
  296. a.code == errorCode.SUCCESS_CODE ? (isBleConnected = !0, "function" === typeof b && b(a)) : 0 <
  297. reConnectNum ?
  298. (reConnectNum--, disconnectBle(function(b) {}), dataUtil.consoleLog("\u5f00\u59cb\u91cd\u8fde"),
  299. createBleConnection(b)) : (clearTimeout(connectTimeoutId), c.code = errorCode
  300. .ERR_CONNECTBLE_CODE, "function" === typeof b && b(c))
  301. })
  302. }
  303. function startOnBleConnectionStateChange() {
  304. if (!isBLEConnectionStateChange) wx.onBLEConnectionStateChange(function(a) {
  305. isBLEConnectionStateChange = !0;
  306. a.connected ? dataUtil.consoleLog("onBLEConnectionStateChange-connected") : (dataUtil.consoleLog(
  307. "onBLEConnectionStateChange-disconnect"), isBleConnected = !1, wx.closeBLEConnection({
  308. deviceId: XXXDeviceId
  309. }))
  310. })
  311. }
  312. function createBleConnection(a) {
  313. var b = {};
  314. wx.createBLEConnection({
  315. deviceId: XXXDeviceId,
  316. success: function(b) {
  317. deployBle(a)
  318. },
  319. fail: function(c) {
  320. b.code = errorCode.ERR_CONNECTBLE_CODE;
  321. "function" === typeof a && a(b)
  322. }
  323. })
  324. }
  325. function closeBle(a) {
  326. var b = {};
  327. wx.closeBluetoothAdapter({
  328. success: function(c) {
  329. dataUtil.consoleLog("\u5173\u95ed\u84dd\u7259\u9002\u914d\u5668\u6210\u529f");
  330. b.code = errorCode.SUCCESS_CODE;
  331. "function" === typeof a && a(b)
  332. },
  333. fail: function() {
  334. dataUtil.consoleLog("\u5173\u95ed\u84dd\u7259\u9002\u914d\u5668\u5931\u8d25");
  335. b.code = errorCode.FAILED_CODE;
  336. "function" === typeof a && a(b)
  337. }
  338. })
  339. }
  340. function disconnectBle(a) {
  341. wx.closeBLEConnection({
  342. XXXDeviceId: XXXDeviceId,
  343. success: function(b) {
  344. console.log("closeBLEConnection:" + b)
  345. }
  346. })
  347. }
  348. function cosChannel(a, b, c) {
  349. sendDataTimeout(c);
  350. sendData(dataUtil.makeCosSendData(a, b), function(b, a) {
  351. clearTimeout(sendDataTimeoutId);
  352. if ("0" === b)
  353. if (dataUtil.consoleLog("initDevice-res\uff1a" + a), b = a.substring(2, 4), "00" == b) {
  354. b = a.substring(10, a.length);
  355. b = tlvUnpackData(b);
  356. for (var d = 0; d < b.length; d++) {
  357. var e = b[d].substring(b[d].length - 4, b[d].length);
  358. dataUtil.consoleLog("cosCode:" + e);
  359. if (e != COS_SUCCESS_CODE) {
  360. "function" === typeof c && c(e, a);
  361. return
  362. }
  363. }
  364. "function" === typeof c && c(errorCode.SUCCESS_CODE, b)
  365. } else "function" ===
  366. typeof c && c(b, a);
  367. else "function" === typeof c && c(errorCode.ERR_CMD_EXECUTE_CODE, a)
  368. })
  369. }
  370. function initDevice(a) {
  371. sendDataTimeout(a);
  372. sendData(dataUtil.initDeviceSendData(), function(b, c) {
  373. clearTimeout(sendDataTimeoutId);
  374. if ("0" === b) {
  375. dataUtil.consoleLog("initDevice-res\uff1a" + c);
  376. var d = c.substring(2, 4);
  377. "00" == d ? (c = c.substring(6, c.length), "function" === typeof a && a(b, c)) : "function" ===
  378. typeof a && a(d, c)
  379. } else "function" === typeof a && a(errorCode.ERR_CMD_EXECUTE_CODE, c)
  380. })
  381. }
  382. function cardChannel(a, b) {
  383. console.log("a,b", a, b)
  384. sendDataTimeout(b);
  385. sendData(dataUtil.makePiccChannelSendData("00", a), function(a, d) {
  386. clearTimeout(sendDataTimeoutId);
  387. if ("0" === a)
  388. if (dataUtil.consoleLog("cardChannel-res\uff1a" + d), a = d.substring(2, 4), "00" == a) {
  389. var c = d.substring(10, d.length);
  390. c = tlvUnpackData(c);
  391. for (a = 0; a < c.length; a++) {
  392. var f = c[a].substring(c[a].length - 4, c[a].length);
  393. dataUtil.consoleLog("cosCode:" + f);
  394. if (f != COS_SUCCESS_CODE) {
  395. "function" === typeof b && b(f, d);
  396. return
  397. }
  398. }
  399. "function" === typeof b && b(errorCode.SUCCESS_CODE, c)
  400. } else "function" ===
  401. typeof b && b(a, c);
  402. else "function" === typeof b && b(errorCode.ERR_CMD_EXECUTE_CODE, d)
  403. })
  404. }
  405. function deviceChannel(a, b) {
  406. sendDataTimeout(b);
  407. sendData(dataUtil.makeDeviceChannelSendData(a), function(a, d) {
  408. clearTimeout(sendDataTimeoutId);
  409. "0" === a ? (dataUtil.consoleLog("deviceChannel-res\uff1a" + d), a = d.substring(2, 4), "00" == a ? (d =
  410. d.substring(6, d.length - 6), "function" === typeof b && b(errorCode.SUCCESS_CODE, d)) :
  411. "function" === typeof b && b(a, d)) : "function" === typeof b && b(errorCode
  412. .ERR_CMD_EXECUTE_CODE, d)
  413. })
  414. }
  415. function authenChannel(a, b, c) {
  416. sendDataTimeout(c);
  417. sendData(dataUtil.makeAuthenChannelSendData(a, b), function(a, b) {
  418. clearTimeout(sendDataTimeoutId);
  419. "0" === a ? (dataUtil.consoleLog("authenChannel-res\uff1a" + b), "function" === typeof c && c(a, b)) :
  420. "function" === typeof c && c(errorCode.ERR_CMD_EXECUTE_CODE, b)
  421. })
  422. }
  423. function cardReset(a) {
  424. sendDataTimeout(a);
  425. sendData(dataUtil.makePiccResetSendData(), function(b, c) {
  426. clearTimeout(sendDataTimeoutId);
  427. "0" === b ? (dataUtil.consoleLog("cardReset-res\uff1a" + c), b = c.substring(2, 4), "00" == b ? (c = c
  428. .substring(6, c.length - 6), "function" === typeof a && a(errorCode.SUCCESS_CODE, c)) :
  429. "function" === typeof a && a(b, c)) : "function" === typeof a && a(errorCode
  430. .ERR_CMD_EXECUTE_CODE, c)
  431. })
  432. }
  433. function esamReset(a) {
  434. sendDataTimeout(a);
  435. sendData(dataUtil.makeEsamResetSendData(), function(b, c) {
  436. clearTimeout(sendDataTimeoutId);
  437. "0" === b ? (dataUtil.consoleLog("esamReset-res\uff1a" + c), b = c.substring(2, 4), "00" == b ? (c = c
  438. .substring(6, c.length - 6), "function" === typeof a && a(errorCode.SUCCESS_CODE, c)) :
  439. "function" === typeof a && a(b, c)) : "function" === typeof a && a(errorCode
  440. .ERR_CMD_EXECUTE_CODE, c)
  441. })
  442. }
  443. function esamChannel(a, b) {
  444. sendDataTimeout(b);
  445. sendData(dataUtil.makeEsamChannelSendData("00", a), function(a, d) {
  446. clearTimeout(sendDataTimeoutId);
  447. if ("0" === a)
  448. if (dataUtil.consoleLog("esamChannel-res\uff1a" + d), a = d.substring(2, 4), "00" == a) {
  449. a = d.substring(10, d.length);
  450. a = tlvUnpackData(a);
  451. for (var c = 0; c < a.length; c++) {
  452. var f = a[c].substring(a[c].length - 4, a[c].length);
  453. dataUtil.consoleLog("cosCode:" + f);
  454. if (f != COS_SUCCESS_CODE) {
  455. "function" === typeof b && b(f, d);
  456. return
  457. }
  458. }
  459. "function" === typeof b && b(errorCode.SUCCESS_CODE, a)
  460. } else "function" ===
  461. typeof b && b(a, d);
  462. else "function" === typeof b && b(errorCode.ERR_CMD_EXECUTE_CODE, d)
  463. })
  464. }
  465. function deviceAuthenChannel(a, b, c) {
  466. sendDataTimeout(c);
  467. sendData(dataUtil.makeDeviceAuthenSendData(a, b), function(a, b) {
  468. clearTimeout(sendDataTimeoutId);
  469. "0" === a ? (dataUtil.consoleLog("deviceAuthenChannel-res\uff1a" + b), a = b.substring(2, 4), "00" ==
  470. a ? (b = b.substring(8, b.length), "function" === typeof c && c(errorCode.SUCCESS_CODE, b)) :
  471. "function" === typeof c && c(a, b)) : "function" === typeof c && c(errorCode
  472. .ERR_CMD_EXECUTE_CODE, b)
  473. })
  474. }
  475. function selfDeviceChannel(a, b) {
  476. sendDataTimeout(b);
  477. sendData(dataUtil.makeSelfDeviceChannelSendData(a), function(a, d) {
  478. clearTimeout(sendDataTimeoutId);
  479. "0" === a ? (a = d.substring(2, 4), "00" == a ? (d = d.substring(8, d.length), dataUtil.consoleLog(
  480. "selfChannel-data\uff1a" + d), "function" === typeof b && b(errorCode.SUCCESS_CODE, d)) :
  481. "function" === typeof b && b(a, d)) : "function" === typeof b && b(errorCode
  482. .ERR_CMD_EXECUTE_CODE, d)
  483. })
  484. }
  485. function updateChannel(a, b, c) {
  486. sendDataTimeout(c);
  487. sendData(dataUtil.makeUpdateChannelSendData(a, b), function(a, b) {
  488. clearTimeout(sendDataTimeoutId);
  489. if ("0" === a) {
  490. var d = b.substring(2, 4);
  491. "00" == d ? (b = b.substring(8, b.length), "function" === typeof c && c(a, b)) : "function" ===
  492. typeof c && c(d, b)
  493. } else "function" === typeof c && c(errorCode.ERR_CMD_EXECUTE_CODE, b)
  494. })
  495. }
  496. function sendDataTimeout(a) {
  497. sendDataTimeoutId = setTimeout(function() {
  498. dataUtil.consoleLog("\u6570\u636e\u54cd\u5e94\u8d85\u65f6\uff0c\u8fd4\u56de\u9519\u8bef");
  499. "function" === typeof a && a(errorCode.ERR_RECVDATA_TIMEOUT, "\u6570\u636e\u54cd\u5e94\u8d85\u65f6")
  500. }, 5E3)
  501. }
  502. function tlvUnpackData(a) {
  503. var b = parseInt(a.substring(2, 4), 16),
  504. c = [];
  505. 128 >= b ? (b = 2 * parseInt(a.substring(2, 4), 16), a = a.substring(4, a.length)) : 129 == b ? (b = 2 * parseInt(a
  506. .substring(4, 6), 16), a = a.substring(6, a.length)) : (b = 2 * parseInt(a.substring(4, 8), 16), a = a
  507. .substring(8, a.length));
  508. for (var d = 0, e = 0, f; 0 < b;) f = 2 * parseInt(a.substring(2 + e, 4 + e), 16), c[d] = a.substring(4 + e, f + 4 +
  509. e), e = e + 4 + f, d++, b = b - 4 - f;
  510. return c
  511. }
  512. function encryptRandom(a) {
  513. a = a.match(/.{1,2}/g).map(function(a) {
  514. return parseInt(a, 16)
  515. });
  516. for (var b = "67656E7669637430".match(/.{1,2}/g).map(function(a) {
  517. return parseInt(a, 16)
  518. }), c = new Uint8Array(a.length), d = 0; d < a.length; d++) c[d] = a[d] ^ b[d];
  519. return byteArrayToHexStr(c)
  520. }
  521. function stringtoHex(a) {
  522. return dataUtil.stringtoHex(a)
  523. }
  524. function hexToBytes(a) {
  525. return dataUtil.hexToBytes(a)
  526. }
  527. function hexToString(a) {
  528. return dataUtil.hexToString(a)
  529. }
  530. function byteArrayToHexStr(a) {
  531. return dataUtil.byteArrayToHexStr(a)
  532. }
  533. function fromCharCode(a) {
  534. return dataUtil.fromCharCode(a)
  535. }
  536. function numberToHexString(a, b, c) {
  537. return dataUtil.numberToHexString(a, b, c)
  538. }
  539. function calcPercent(a, b) {
  540. a = (a / b).toFixed(2);
  541. a = 100 * parseFloat(a);
  542. return parseInt(a)
  543. }
  544. function consoleLog(a) {
  545. dataUtil.consoleLog(a)
  546. }
  547. module.exports = {
  548. deployBle: deployBle,
  549. openBle: openBle,
  550. closeBle: closeBle,
  551. setScanTimeOut: setScanTimeOut,
  552. scanBle: scanBle,
  553. stopScanBle: stopScanBle,
  554. connectBle: connectBle,
  555. disconnectBle: disconnectBle,
  556. isConnected: isConnected,
  557. cosChannel: cosChannel,
  558. initDevice: initDevice,
  559. cardChannel: cardChannel,
  560. deviceChannel: deviceChannel,
  561. authenChannel: authenChannel,
  562. cardReset: cardReset,
  563. esamReset: esamReset,
  564. esamChannel: esamChannel,
  565. deviceAuthenChannel: deviceAuthenChannel,
  566. selfDeviceChannel: selfDeviceChannel,
  567. updateChannel: updateChannel,
  568. encryptRandom: encryptRandom,
  569. stringtoHex: stringtoHex,
  570. fromCharCode: fromCharCode,
  571. numberToHexString: numberToHexString,
  572. hexToBytes: hexToBytes,
  573. hexToString: hexToString,
  574. byteArrayToHexStr: byteArrayToHexStr,
  575. calcPercent: calcPercent,
  576. consoleLog: consoleLog
  577. };