123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353 |
- "use strict";
- var _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (e) {
- return typeof e
- }
- : function (e) {
- return e && "function" == typeof Symbol && e.constructor === Symbol && e !== Symbol.prototype ? "symbol" : typeof e
- }
- , _kcUtils = require("./kcUtils.js")
- , _kcUtils2 = _interopRequireDefault(_kcUtils)
- , _kcService = require("./kcService.js")
- , _kcService2 = _interopRequireDefault(_kcService)
- , _kcDataInteract = require("./kcDataInteract.js")
- , _kcDataInteract2 = _interopRequireDefault(_kcDataInteract)
- , _kcDataAnalysis = require("./kcDataAnalysis.js")
- , _kcDataAnalysis2 = _interopRequireDefault(_kcDataAnalysis)
- , _kcDataEncode = require("./kcDataEncode.js")
- , _kcDataEncode2 = _interopRequireDefault(_kcDataEncode);
- function _interopRequireDefault(e) {
- return e && e.__esModule ? e : {
- default: e
- }
- }
- var TAG_FUNCTION = "function";
- function reallyScanConnect(t) {
- _kcService2.default.reallyScanConnect(function (e) {
- (void 0 === t ? "undefined" : _typeof(t)) == TAG_FUNCTION && t(e)
- })
- }
- function connectDevice(e, t) {
- let o = {};
- o.device_name = e.name;
- o.device_id = e.deviceId;
- _kcService2.default.reallyConnect(o, function (e) {
- console.log("3.", e),
- (void 0 === t ? "undefined" : _typeof(t)) == TAG_FUNCTION && t(e)
- });
- }
- function deployBle(t) {
- _kcService2.default.deployBleConf(function (e) {
- 0 == e && _kcUtils2.default.showLog("配置连接外设成功"),
- (void 0 === t ? "undefined" : _typeof(t)) == TAG_FUNCTION && t(e)
- })
- }
- function disconnectDevice(t) {
- var a;
- _kcService2.default.reallyDisConnect(function (e) {
- a = e,
- (void 0 === t ? "undefined" : _typeof(t)) == TAG_FUNCTION && t(a.serviceCode)
- })
- }
-
- // 将 singleTransCmd 函数改为返回 Promise
- function singleTransCmd(cmdtype, cmd) {
- return new Promise((resolve, reject) => {
- var n = "",
- o = "",
- result = {
- code: -1,
- data: "",
- msg: '执行失败'
- };
-
- console.log("cmdtype:" + cmdtype);
-
- if (cmdtype == '10') {
- n = "A3";
- o = "00";
- console.log("IC卡指令");
- } else if (cmdtype == '20') {
- n = "AC";
- o = "00";
- console.log("OBU ESAM卡指令");
- }
-
- // 输出原始命令
- console.log("strhex:" + cmd);
-
- // 处理命令
- if (o == "10" || o == "20" || o == "30" || o == "00") {
- // 编码命令
- var t = _kcDataEncode2.default.encode(cmd, o, n);
- // 发送数据
- _kcDataInteract2.default._StartSendData(t.serviceData.dataEncode, function (e) {
- if (e.serviceCode == 0) {
- console.log("%cTPDU透传指令成功", "color: green;");
- // 解析数据
- var i = _kcDataAnalysis2.default._analysisIs9000(e.serviceData.dataBuff);
- result.code = i.serviceCode;
- result.data = i.unpacksData[0];
- result.msg = i.serviceCode == 0 ? '执行成功' : '执行失败';
- } else {
- result.code = -1;
- result.data = "";
- result.msg = '执行失败';
- console.error("TPDU透传指令失败");
- }
-
- // 调用回调函数
- resolve(result);
- });
- } else {
- // 处理错误的cmdtype参数
- result.msg = 'cmdtype参数错误';
- reject(result);
- }
- });
- }
-
- // 修改 transCmdLoop 函数,按顺序执行 singleTransCmd,并在失败时停止循环
- async function transCmdLoop(cmdArray, cmdtype, callback) {
- let results = {
- code: -1,
- data: [],
- msg: ''
- }; // 存储每个命令的结果
- for (let cmd of cmdArray) {
- try {
- let concatenateCmd = "";
- // 计算指令长度(字符数的一半),并转换为十六进制字符串
- let cmdLength = (cmd.length / 2).toString(16).padStart(2, '0');
- // 拼接字符串
- concatenateCmd += "01" + cmdLength + cmd;
- const result = await singleTransCmd(cmdtype, concatenateCmd);
- results.code = result.code;
- results.msg = result.msg;
- results.data.push(result.data);
- console.log("命令 " + cmd + " 处理完成,结果:", result);
-
- // 如果执行结果不成功,停止循环
- if (result.code !== 0) {
- console.error("命令执行失败,停止循环");
- break;
- }
- } catch (error) {
- results.code = -1;
- results.msg = '执行失败';
- results.data.push("");
- console.error("命令 " + cmd + " 处理失败,错误:", error);
- break; // 捕获到错误时停止循环
- }
- }
- console.log("所有命令处理完毕");
- if (typeof callback === 'function') {
- callback(results); // 所有命令处理完毕后,调用回调函数
- }
- }
-
- function transCmd(cmdArray, cmdtype, callback) {
- var n = "",
- o = "",
- concatenateCmd = "",
- result = {
- code: -1,
- data: [],
- msg: '执行失败'
- };
-
- console.log("cmdtype:" + cmdtype);
-
- if (cmdtype == '10') {
- n = "A3";
- o = "00";
- console.log("IC卡指令");
- } else if (cmdtype == '20') {
- n = "AC";
- o = "00";
- console.log("OBU ESAM卡指令");
- }
- cmdArray.forEach((cmd, index) => {
- // 将 index 转换为十六进制字符串
- let hexIndex = (index + 1).toString(16).padStart(2, '0');
- // 计算指令长度(字符数的一半),并转换为十六进制字符串
- let cmdLength = (cmd.length / 2).toString(16).padStart(2, '0');
- // 拼接字符串
- concatenateCmd += hexIndex + cmdLength + cmd;
- });
- console.log("strhex:" + concatenateCmd);
- // Process command based on the value of o
- if (o == "10" || o == "20" || o == "30" || o == "00") {
- var t = _kcDataEncode2.default.encode(concatenateCmd, o, n);
- _kcDataInteract2.default._StartSendData(t.serviceData.dataEncode, function (e) {
- if (e.serviceCode == 0) {
- console.log("%cTPDU透传指令成功", "color: green;");
- var i = _kcDataAnalysis2.default._analyzeUnpacksIntoArrays(e.serviceData.dataBuff);
- result.code = i.serviceCode;
- result.data = i.unpacksData;
- result.msg = '执行成功';
- } else {
- result.code = -1;
- result.data = [];
- result.msg = '执行失败';
- console.error("TPDU透传指令失败");
- }
- if (typeof callback === TAG_FUNCTION) {
- callback(result);
- }
- });
- } else {
- if (typeof callback === TAG_FUNCTION) {
- result.msg = 'cmdtype参数错误';
- callback(result);
- }
- }
- }
-
- function getDeviceInfo(n) {
- var o = {}
- , e = _kcDataEncode2.default.encode("A501C0");
- _kcDataInteract2.default._StartSendData(e.serviceData.dataEncode, function (e) {
- var t, a;
- 0 == e.serviceCode ? (_kcUtils2.default.showLog("获取设备信息成功"),
- o = _kcDataAnalysis2.default.analysisDeviceInfo(e.serviceData.dataBuff),
- console.log(o.serviceCode, o.serviceData.deviceSN),
- t = o.serviceCode,
- a = o.serviceData.deviceSN,
- o.serviceData = {},
- o.serviceData.obuId = a,
- console.log(o),
- (void 0 === n ? "undefined" : _typeof(n)) == TAG_FUNCTION && n(t, o.serviceData)) : (o = e,
- (void 0 === n ? "undefined" : _typeof(n)) == TAG_FUNCTION && n(o.code))
- })
- }
- function obuSetSleepTime(e, t) {
- var a = ""
- , a = _kcUtils2.default.array2Str(e)
- , e = parseInt(a, 16);
- e < 60 ? a = "3c" : 255 < e && (a = "ff"),
- e = _kcDataEncode2.default.encode("AC01D8" + a),
- _kcDataInteract2.default._StartSendData(e.serviceData.dataEncode, function (e) {
- e.serviceCode,
- e = e.serviceCode,
- (void 0 === t ? "undefined" : _typeof(t)) == TAG_FUNCTION && t(e)
- })
- }
- function getActState(a) {
- var e = _kcDataEncode2.default.encode("A501CA");
- _kcDataInteract2.default._StartSendData(e.serviceData.dataEncode, function (e) {
- var t;
- e = 0 == e.serviceCode ? (t = e.serviceCode,
- e.serviceData.dataBuff) : (t = -1,
- "获取防拆失败"),
- (void 0 === a ? "undefined" : _typeof(a)) == TAG_FUNCTION && a(t, e)
- })
- }
- function scanBle(outTime, ScanDeviceResultCallBack) {
- var result = {
- code: -2,
- msg: '蓝牙状态异常'
- };
- let foundDevices = [];
- let onScanResult = [];
- let timeoutId; // 用于存储超时定时器的ID
- // 定义处理发现设备的函数
- function etcHandleDeviceFound(res) {
- for (let i = 0; i < res.devices.length; i++) {
- let isHave = false;
- console.log(res.devices[i].name);
- for (let j = 0; j < foundDevices.length; j++) {
- if (res.devices[i].deviceId === foundDevices[j].deviceId) {
- isHave = true;
- break;
- }
- }
- if (!isHave) {
- foundDevices.push(res.devices[i]);
- var name = res.devices[i].name;
- if (name && name.indexOf('ETC-KC-') !== -1) {
- clearTimeout(timeoutId); // 找到设备,清除超时定时器
- onScanResult.push(res.devices[i]);
- wx.stopBluetoothDevicesDiscovery({
- success: function (res) {
- console.log("1.停止扫描");
- },
- fail: function (res) {
- console.error('stopBluetoothDevicesDiscovery fail', res);
- }
- });
- result.code = 0;
- result.msg = '蓝牙状态正常';
- if (typeof ScanDeviceResultCallBack === TAG_FUNCTION) ScanDeviceResultCallBack(onScanResult, result);
- break;
- }
- }
- }
- }
- console.log('/***********Running :: Do reallyScanConnect() begin *************/');
- // 设置超时时间,例如30秒
- timeoutId = setTimeout(function () {
- console.log("超时了,停止扫描");
- wx.stopBluetoothDevicesDiscovery({
- success: function (res) {
- console.log("因超时停止扫描");
- },
- fail: function (res) {
- console.error('因超时停止扫描失败', res);
- }
- });
- result.code = -3;
- if (typeof ScanDeviceResultCallBack === TAG_FUNCTION) ScanDeviceResultCallBack(null, result);
- }, outTime);
- wx.closeBluetoothAdapter();
- wx.openBluetoothAdapter({
- success: function (res) {
- wx.startBluetoothDevicesDiscovery({
- services: [],
- success: function (res) {
- wx.onBluetoothDeviceFound(etcHandleDeviceFound);
- },
- fail: function (res) {
- console.error('startBluetoothDevicesDiscovery fail', res);
- clearTimeout(timeoutId);
- if (typeof ScanDeviceResultCallBack === TAG_FUNCTION) ScanDeviceResultCallBack(null, result);
- }
- });
- },
- fail: function (res) {
- console.error('openBluetoothAdapter fail', res);
- clearTimeout(timeoutId);
- if (typeof ScanDeviceResultCallBack === TAG_FUNCTION) ScanDeviceResultCallBack(null, result);
- }
- });
- }
- function closBluetooth(callBack) {
- var c = {
- code: 0,
- msg: '蓝牙搜索已停止'
- };
- wx.stopBluetoothDevicesDiscovery({
- success: function (res) {
- console.log("stopBluetoothDevicesDiscovery success");
- (void 0 === callBack ? "undefined" : _typeof(callBack)) == TAG_FUNCTION && callBack(c)
- },
- fail: function (res) {
- console.error('stopBluetoothDevicesDiscovery fail', res);
- }
- });
- }
-
- function disconnectDeviceBluetooth(callBack) {
- _kcService2.default.disconnectDeviceBluetooth(callBack);
- }
- module.exports = {
- reallyScanConnect: reallyScanConnect,
- connectDevice: connectDevice,
- disconnectDevice: disconnectDevice,
- getDeviceInfo: getDeviceInfo,
- transCmd: transCmd,
- getActState: getActState,
- obuSetSleepTime: obuSetSleepTime,
- scanBle: scanBle,
- closBluetooth: closBluetooth,
- disconnectDeviceBluetooth: disconnectDeviceBluetooth,
- };
|