"use strict"; import _kcBaseBIZservice from "./kcBaseBIZservice.js" 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); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e } } var TAG_FUNCTION = "function"; function getCallbackParamCount(callback) { if (typeof callback !== "function") { throw new TypeError("callback 必须是一个函数"); } return callback.length; } function _asyncExecuteCallback(callback, successData, error) { if (typeof callback === "function") { try { let result; if (successData !== null) { result = successData; } else { const isError = error instanceof Error; // 更精确的判断 result = isError ? { code: -1, data: [], msg: error.message } : { code: error.serviceCode, data: [], msg: error.serviceInfo }; } // 异步执行回调(通过 Promise.resolve().then) Promise.resolve().then(() => callback(result)).catch(err => { console.error("回调执行异常:", err); }); } catch (err) { console.error("回调执行异常:", err); } } } // 辅助函数:安全回调执行 function _syncExecuteCallback(callback, successData, error) { if (typeof callback === "function") { try { let result; if (successData !== null) { result = successData; } else { const isError = typeof error === "object" && error !== null && "name" in error && "message" in error; if (isError) { result = { code: -1, data: [], msg: error.serviceInfo }; } else { result = { code: error.serviceCode, data: [], msg: error.serviceInfo }; } } callback(result); } catch (err) { console.error("回调执行异常:", err); } } } 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--1", e); setTimeout(function(){(void 0 === t ? "undefined" : _typeof(t)) == TAG_FUNCTION && t(e)}, 300); }); } 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 _kcBaseBIZservice.singleTransCmd(cmdtype, cmd); } // 修改 transCmdLoop 函数,按顺序执行 singleTransCmd,并在失败时停止循环 async function transCmdLoop(cmdArray, cmdtype, callback) { _kcBaseBIZservice.transCmdLoop(cmdArray, cmdtype, callback); } function transCmd(cmdArray, cmdtype, callback) { _kcBaseBIZservice.transCmd(cmdArray, cmdtype, function(res){ if (res.code === -5) { res.code = 0; const diff = cmdArray.length - res.data.length; if (diff > 0) { console.log(`需要补全 ${diff} 条数据`); //res.data.push(...Array(diff).fill("")); // 使用展开运算符批量追加 for (let i = 0; i < diff; i++) { res.data.push("6982"); } } } if (typeof callback === TAG_FUNCTION) { callback(res); } }); } function getDeviceInfo(n) { _kcBaseBIZservice.getDeviceNumber(n); } function getDeviceNumber(callback) { _kcBaseBIZservice.getDeviceNumber() .then(deviceInfo => { let result = { code: deviceInfo.serviceCode, data: deviceInfo.serviceData.etcVehicleInfo, msg: deviceInfo.serviceInfo }; _asyncExecuteCallback(callback, result, null); }) .catch(error => { _asyncExecuteCallback(callback, null, error); }); } function obuSetSleepTime(e, t) { _kcBaseBIZservice.obuSetSleepTime(e, t); } function getDisassemblyPillar(callback) { _kcBaseBIZservice.getDisassemblyPillar() .then(deviceInfo => { let result = { code: deviceInfo.serviceCode, data: deviceInfo.serviceData.etcVehicleInfo, msg: deviceInfo.serviceInfo }; _asyncExecuteCallback(callback, result, null); }) .catch(error => { _asyncExecuteCallback(callback, null, error); }); } function getActState(callback) { _kcBaseBIZservice.getActState() .then(deviceInfo => { let result = { code: deviceInfo.serviceCode, data: deviceInfo.serviceData.etcVehicleInfo, msg: deviceInfo.serviceInfo }; _asyncExecuteCallback(callback, result, null); }) .catch(error => { _asyncExecuteCallback(callback, null, error); }); } function getDevicePower(callback) { _kcBaseBIZservice.getDevicePower() .then(deviceInfo => { let result = { code: deviceInfo.serviceCode, data: deviceInfo.serviceData.etcVehicleInfo, msg: deviceInfo.serviceInfo }; _asyncExecuteCallback(callback, result, null); }) .catch(error => { _asyncExecuteCallback(callback, null, error); }); } function getBleDeviceVersion(callback) { _kcBaseBIZservice.getBleDeviceVersion() .then(deviceInfo => { let result = { code: deviceInfo.serviceCode, data: deviceInfo.serviceData.etcVehicleInfo, msg: deviceInfo.serviceInfo }; _asyncExecuteCallback(callback, result, null); }) .catch(error => { _asyncExecuteCallback(callback, null, error); }); } function getObuDeviceVersion(callback) { _kcBaseBIZservice.getObuDeviceVersion() .then(deviceInfo => { let result = { code: deviceInfo.serviceCode, data: deviceInfo.serviceData.etcVehicleInfo, msg: deviceInfo.serviceInfo }; _asyncExecuteCallback(callback, result, null); }) .catch(error => { _asyncExecuteCallback(callback, null, error); }); } function getIcCardInfo(callback) { _kcBaseBIZservice.getIcCardInfo() .then(deviceInfo => { let result = { code: deviceInfo.serviceCode, data: deviceInfo.serviceData.etcVehicleInfo, msg: deviceInfo.serviceInfo }; _asyncExecuteCallback(callback, result, null); }) .catch(error => { _asyncExecuteCallback(callback, null, error); }); } 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 || ''; const isValidPrefix = _kcUtils2.default.isValidDevice(name); if (isValidPrefix) { 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 = '蓝牙状态正常'; console.log(`%cdeviceName ${res.devices[i].name}, RSSI: ${res.devices[i].RSSI}`, "color: green;"); 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); } function turnOn58GANT(callback) { _kcBaseBIZservice.turnOn58GANT() .then(deviceInfo => { let result = { code: deviceInfo.serviceCode, data: deviceInfo.serviceData.etcVehicleInfo, msg: deviceInfo.serviceInfo }; _asyncExecuteCallback(callback, result, null); }) .catch(error => { _asyncExecuteCallback(callback, null, error); }); } function turnOff58GANT(callback) { _kcBaseBIZservice.turnOff58GANT() .then(deviceInfo => { let result = { code: deviceInfo.serviceCode, data: deviceInfo.serviceData.etcVehicleInfo, msg: deviceInfo.serviceInfo }; _asyncExecuteCallback(callback, result, null); }) .catch(error => { _asyncExecuteCallback(callback, null, error); }); } function get58GStatus(callback) { _kcBaseBIZservice.get58GStatus() .then(deviceInfo => { let result = { code: deviceInfo.serviceCode, data: deviceInfo.serviceData.etcVehicleInfo, msg: deviceInfo.serviceInfo }; _asyncExecuteCallback(callback, result, null); }) .catch(error => { _asyncExecuteCallback(callback, null, error); }); } function getIcCardSwitchStatus(callback) { _kcBaseBIZservice.getIcCardSwitchStatus() .then(deviceInfo => { let result = { code: deviceInfo.serviceCode, data: deviceInfo.serviceData.etcVehicleInfo, msg: deviceInfo.serviceInfo }; _asyncExecuteCallback(callback, result, null); }) .catch(error => { _asyncExecuteCallback(callback, null, error); }); } function getVoiceFeature(callback) { _kcBaseBIZservice.getVoiceFeature() .then(deviceInfo => { let result = { code: deviceInfo.serviceCode, data: deviceInfo.serviceData.etcVehicleInfo, msg: deviceInfo.serviceInfo }; _asyncExecuteCallback(callback, result, null); }) .catch(error => { _asyncExecuteCallback(callback, null, error); }); } function getBatteryPercentage(callback) { _kcBaseBIZservice.getBatteryPercentage() .then(deviceInfo => { let result = { code: deviceInfo.serviceCode, data: deviceInfo.serviceData.etcVehicleInfo, msg: deviceInfo.serviceInfo }; _asyncExecuteCallback(callback, result, null); }) .catch(error => { _asyncExecuteCallback(callback, null, error); }); } // export default { // reallyScanConnect, connectDevice, disconnectDevice, getDeviceInfo, transCmd, getActState, getDisassemblyPillar, obuSetSleepTime, scanBle, closBluetooth, disconnectDeviceBluetooth, getDeviceNumber, getDevicePower, getObuDeviceVersion, getIcCardInfo, turnOn58GANT, turnOff58GANT, get58GStatus, getIcCardSwitchStatus, getVoiceFeature, getBatteryPercentage, getBleDeviceVersion, // }; module.exports = { reallyScanConnect: reallyScanConnect, connectDevice: connectDevice, disconnectDevice: disconnectDevice, getDeviceInfo: getDeviceInfo, transCmd: transCmd, getActState: getActState, obuSetSleepTime: obuSetSleepTime, scanBle: scanBle, closBluetooth: closBluetooth, disconnectDeviceBluetooth: disconnectDeviceBluetooth, };