Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

kcBleAPI.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. "use strict";
  2. var _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (e) {
  3. return typeof e
  4. }
  5. : function (e) {
  6. return e && "function" == typeof Symbol && e.constructor === Symbol && e !== Symbol.prototype ? "symbol" : typeof e
  7. }
  8. , _kcUtils = require("./kcUtils.js")
  9. , _kcUtils2 = _interopRequireDefault(_kcUtils)
  10. , _kcService = require("./kcService.js")
  11. , _kcService2 = _interopRequireDefault(_kcService)
  12. , _kcDataInteract = require("./kcDataInteract.js")
  13. , _kcDataInteract2 = _interopRequireDefault(_kcDataInteract)
  14. , _kcDataAnalysis = require("./kcDataAnalysis.js")
  15. , _kcDataAnalysis2 = _interopRequireDefault(_kcDataAnalysis)
  16. , _kcDataEncode = require("./kcDataEncode.js")
  17. , _kcDataEncode2 = _interopRequireDefault(_kcDataEncode);
  18. function _interopRequireDefault(e) {
  19. return e && e.__esModule ? e : {
  20. default: e
  21. }
  22. }
  23. var TAG_FUNCTION = "function";
  24. function reallyScanConnect(t) {
  25. _kcService2.default.reallyScanConnect(function (e) {
  26. (void 0 === t ? "undefined" : _typeof(t)) == TAG_FUNCTION && t(e)
  27. })
  28. }
  29. function connectDevice(e, t) {
  30. let o = {};
  31. o.device_name = e.name;
  32. o.device_id = e.deviceId;
  33. _kcService2.default.reallyConnect(o, function (e) {
  34. console.log("3.", e),
  35. (void 0 === t ? "undefined" : _typeof(t)) == TAG_FUNCTION && t(e)
  36. });
  37. }
  38. function deployBle(t) {
  39. _kcService2.default.deployBleConf(function (e) {
  40. 0 == e && _kcUtils2.default.showLog("配置连接外设成功"),
  41. (void 0 === t ? "undefined" : _typeof(t)) == TAG_FUNCTION && t(e)
  42. })
  43. }
  44. function disconnectDevice(t) {
  45. var a;
  46. _kcService2.default.reallyDisConnect(function (e) {
  47. a = e,
  48. (void 0 === t ? "undefined" : _typeof(t)) == TAG_FUNCTION && t(a.serviceCode)
  49. })
  50. }
  51. // 将 singleTransCmd 函数改为返回 Promise
  52. function singleTransCmd(cmdtype, cmd) {
  53. return new Promise((resolve, reject) => {
  54. var n = "",
  55. o = "",
  56. result = {
  57. code: -1,
  58. data: "",
  59. msg: '执行失败'
  60. };
  61. console.log("cmdtype:" + cmdtype);
  62. if (cmdtype == '10') {
  63. n = "A3";
  64. o = "00";
  65. console.log("IC卡指令");
  66. } else if (cmdtype == '20') {
  67. n = "AC";
  68. o = "00";
  69. console.log("OBU ESAM卡指令");
  70. }
  71. // 输出原始命令
  72. console.log("strhex:" + cmd);
  73. // 处理命令
  74. if (o == "10" || o == "20" || o == "30" || o == "00") {
  75. // 编码命令
  76. var t = _kcDataEncode2.default.encode(cmd, o, n);
  77. // 发送数据
  78. _kcDataInteract2.default._StartSendData(t.serviceData.dataEncode, function (e) {
  79. if (e.serviceCode == 0) {
  80. console.log("%cTPDU透传指令成功", "color: green;");
  81. // 解析数据
  82. var i = _kcDataAnalysis2.default._analysisIs9000(e.serviceData.dataBuff);
  83. result.code = i.serviceCode;
  84. result.data = i.unpacksData[0];
  85. result.msg = i.serviceCode == 0 ? '执行成功' : '执行失败';
  86. } else {
  87. result.code = -1;
  88. result.data = "";
  89. result.msg = '执行失败';
  90. console.error("TPDU透传指令失败");
  91. }
  92. // 调用回调函数
  93. resolve(result);
  94. });
  95. } else {
  96. // 处理错误的cmdtype参数
  97. result.msg = 'cmdtype参数错误';
  98. reject(result);
  99. }
  100. });
  101. }
  102. // 修改 transCmdLoop 函数,按顺序执行 singleTransCmd,并在失败时停止循环
  103. async function transCmdLoop(cmdArray, cmdtype, callback) {
  104. let results = {
  105. code: -1,
  106. data: [],
  107. msg: ''
  108. }; // 存储每个命令的结果
  109. for (let cmd of cmdArray) {
  110. try {
  111. let concatenateCmd = "";
  112. // 计算指令长度(字符数的一半),并转换为十六进制字符串
  113. let cmdLength = (cmd.length / 2).toString(16).padStart(2, '0');
  114. // 拼接字符串
  115. concatenateCmd += "01" + cmdLength + cmd;
  116. const result = await singleTransCmd(cmdtype, concatenateCmd);
  117. results.code = result.code;
  118. results.msg = result.msg;
  119. results.data.push(result.data);
  120. console.log("命令 " + cmd + " 处理完成,结果:", result);
  121. // 如果执行结果不成功,停止循环
  122. if (result.code !== 0) {
  123. console.error("命令执行失败,停止循环");
  124. break;
  125. }
  126. } catch (error) {
  127. results.code = -1;
  128. results.msg = '执行失败';
  129. results.data.push("");
  130. console.error("命令 " + cmd + " 处理失败,错误:", error);
  131. break; // 捕获到错误时停止循环
  132. }
  133. }
  134. console.log("所有命令处理完毕");
  135. if (typeof callback === 'function') {
  136. callback(results); // 所有命令处理完毕后,调用回调函数
  137. }
  138. }
  139. function transCmd(cmdArray, cmdtype, callback) {
  140. var n = "",
  141. o = "",
  142. concatenateCmd = "",
  143. result = {
  144. code: -1,
  145. data: [],
  146. msg: '执行失败'
  147. };
  148. console.log("cmdtype:" + cmdtype);
  149. if (cmdtype == '10') {
  150. n = "A3";
  151. o = "00";
  152. console.log("IC卡指令");
  153. } else if (cmdtype == '20') {
  154. n = "AC";
  155. o = "00";
  156. console.log("OBU ESAM卡指令");
  157. }
  158. cmdArray.forEach((cmd, index) => {
  159. // 将 index 转换为十六进制字符串
  160. let hexIndex = (index + 1).toString(16).padStart(2, '0');
  161. // 计算指令长度(字符数的一半),并转换为十六进制字符串
  162. let cmdLength = (cmd.length / 2).toString(16).padStart(2, '0');
  163. // 拼接字符串
  164. concatenateCmd += hexIndex + cmdLength + cmd;
  165. });
  166. console.log("strhex:" + concatenateCmd);
  167. // Process command based on the value of o
  168. if (o == "10" || o == "20" || o == "30" || o == "00") {
  169. var t = _kcDataEncode2.default.encode(concatenateCmd, o, n);
  170. _kcDataInteract2.default._StartSendData(t.serviceData.dataEncode, function (e) {
  171. if (e.serviceCode == 0) {
  172. console.log("%cTPDU透传指令成功", "color: green;");
  173. var i = _kcDataAnalysis2.default._analyzeUnpacksIntoArrays(e.serviceData.dataBuff);
  174. result.code = i.serviceCode;
  175. result.data = i.unpacksData;
  176. result.msg = '执行成功';
  177. } else {
  178. result.code = -1;
  179. result.data = [];
  180. result.msg = '执行失败';
  181. console.error("TPDU透传指令失败");
  182. }
  183. if (typeof callback === TAG_FUNCTION) {
  184. callback(result);
  185. }
  186. });
  187. } else {
  188. if (typeof callback === TAG_FUNCTION) {
  189. result.msg = 'cmdtype参数错误';
  190. callback(result);
  191. }
  192. }
  193. }
  194. function getDeviceInfo(n) {
  195. var o = {}
  196. , e = _kcDataEncode2.default.encode("A501C0");
  197. _kcDataInteract2.default._StartSendData(e.serviceData.dataEncode, function (e) {
  198. var t, a;
  199. 0 == e.serviceCode ? (_kcUtils2.default.showLog("获取设备信息成功"),
  200. o = _kcDataAnalysis2.default.analysisDeviceInfo(e.serviceData.dataBuff),
  201. console.log(o.serviceCode, o.serviceData.deviceSN),
  202. t = o.serviceCode,
  203. a = o.serviceData.deviceSN,
  204. o.serviceData = {},
  205. o.serviceData.obuId = a,
  206. console.log(o),
  207. (void 0 === n ? "undefined" : _typeof(n)) == TAG_FUNCTION && n(t, o.serviceData)) : (o = e,
  208. (void 0 === n ? "undefined" : _typeof(n)) == TAG_FUNCTION && n(o.code))
  209. })
  210. }
  211. function obuSetSleepTime(e, t) {
  212. var a = ""
  213. , a = _kcUtils2.default.array2Str(e)
  214. , e = parseInt(a, 16);
  215. e < 60 ? a = "3c" : 255 < e && (a = "ff"),
  216. e = _kcDataEncode2.default.encode("AC01D8" + a),
  217. _kcDataInteract2.default._StartSendData(e.serviceData.dataEncode, function (e) {
  218. e.serviceCode,
  219. e = e.serviceCode,
  220. (void 0 === t ? "undefined" : _typeof(t)) == TAG_FUNCTION && t(e)
  221. })
  222. }
  223. function getActState(a) {
  224. var e = _kcDataEncode2.default.encode("A501CA");
  225. _kcDataInteract2.default._StartSendData(e.serviceData.dataEncode, function (e) {
  226. var t;
  227. e = 0 == e.serviceCode ? (t = e.serviceCode,
  228. e.serviceData.dataBuff) : (t = -1,
  229. "获取防拆失败"),
  230. (void 0 === a ? "undefined" : _typeof(a)) == TAG_FUNCTION && a(t, e)
  231. })
  232. }
  233. function scanBle(outTime, ScanDeviceResultCallBack) {
  234. var result = {
  235. code: -2,
  236. msg: '蓝牙状态异常'
  237. };
  238. let foundDevices = [];
  239. let onScanResult = [];
  240. let timeoutId; // 用于存储超时定时器的ID
  241. // 定义处理发现设备的函数
  242. function etcHandleDeviceFound(res) {
  243. for (let i = 0; i < res.devices.length; i++) {
  244. let isHave = false;
  245. console.log(res.devices[i].name);
  246. for (let j = 0; j < foundDevices.length; j++) {
  247. if (res.devices[i].deviceId === foundDevices[j].deviceId) {
  248. isHave = true;
  249. break;
  250. }
  251. }
  252. if (!isHave) {
  253. foundDevices.push(res.devices[i]);
  254. var name = res.devices[i].name;
  255. if (name && name.indexOf('ETC-KC-') !== -1) {
  256. clearTimeout(timeoutId); // 找到设备,清除超时定时器
  257. onScanResult.push(res.devices[i]);
  258. wx.stopBluetoothDevicesDiscovery({
  259. success: function (res) {
  260. console.log("1.停止扫描");
  261. },
  262. fail: function (res) {
  263. console.error('stopBluetoothDevicesDiscovery fail', res);
  264. }
  265. });
  266. result.code = 0;
  267. result.msg = '蓝牙状态正常';
  268. if (typeof ScanDeviceResultCallBack === TAG_FUNCTION) ScanDeviceResultCallBack(onScanResult, result);
  269. break;
  270. }
  271. }
  272. }
  273. }
  274. console.log('/***********Running :: Do reallyScanConnect() begin *************/');
  275. // 设置超时时间,例如30秒
  276. timeoutId = setTimeout(function () {
  277. console.log("超时了,停止扫描");
  278. wx.stopBluetoothDevicesDiscovery({
  279. success: function (res) {
  280. console.log("因超时停止扫描");
  281. },
  282. fail: function (res) {
  283. console.error('因超时停止扫描失败', res);
  284. }
  285. });
  286. result.code = -3;
  287. if (typeof ScanDeviceResultCallBack === TAG_FUNCTION) ScanDeviceResultCallBack(null, result);
  288. }, outTime);
  289. wx.closeBluetoothAdapter();
  290. wx.openBluetoothAdapter({
  291. success: function (res) {
  292. wx.startBluetoothDevicesDiscovery({
  293. services: [],
  294. success: function (res) {
  295. wx.onBluetoothDeviceFound(etcHandleDeviceFound);
  296. },
  297. fail: function (res) {
  298. console.error('startBluetoothDevicesDiscovery fail', res);
  299. clearTimeout(timeoutId);
  300. if (typeof ScanDeviceResultCallBack === TAG_FUNCTION) ScanDeviceResultCallBack(null, result);
  301. }
  302. });
  303. },
  304. fail: function (res) {
  305. console.error('openBluetoothAdapter fail', res);
  306. clearTimeout(timeoutId);
  307. if (typeof ScanDeviceResultCallBack === TAG_FUNCTION) ScanDeviceResultCallBack(null, result);
  308. }
  309. });
  310. }
  311. function closBluetooth(callBack) {
  312. var c = {
  313. code: 0,
  314. msg: '蓝牙搜索已停止'
  315. };
  316. wx.stopBluetoothDevicesDiscovery({
  317. success: function (res) {
  318. console.log("stopBluetoothDevicesDiscovery success");
  319. (void 0 === callBack ? "undefined" : _typeof(callBack)) == TAG_FUNCTION && callBack(c)
  320. },
  321. fail: function (res) {
  322. console.error('stopBluetoothDevicesDiscovery fail', res);
  323. }
  324. });
  325. }
  326. function disconnectDeviceBluetooth(callBack) {
  327. _kcService2.default.disconnectDeviceBluetooth(callBack);
  328. }
  329. module.exports = {
  330. reallyScanConnect: reallyScanConnect,
  331. connectDevice: connectDevice,
  332. disconnectDevice: disconnectDevice,
  333. getDeviceInfo: getDeviceInfo,
  334. transCmd: transCmd,
  335. getActState: getActState,
  336. obuSetSleepTime: obuSetSleepTime,
  337. scanBle: scanBle,
  338. closBluetooth: closBluetooth,
  339. disconnectDeviceBluetooth: disconnectDeviceBluetooth,
  340. };