import { configBlueStateMsg, decode, hex2int } from '../etc-sdk-tool/gbk-tool' import { jsonToString } from '../obj-tools' import { connectAndInit, disconnect, transCmd, getIccFile15, getObuSystemInfo, } from '../etc-sdk-tool/etc-sdk-tool' import activeCode from './active-device-code' import { apiIssueApply, apiIssueCallback } from '../etcReq' const network = require('../../network/index') let devResult = { code: 0, msg: '', //信息 data: [], } let issueResult = { cmd: '', cmdType: '', stepNo: '', transOrderId: '', OrderNo: '', isOnline: '', } const CmdStatus = '9000' let _checkStatusCallback = Function() let _isDeviceEnd = false let _disConnectCallback = Function() /// -------------------- 公开方法 ----------------------- /// // 激活设备 export async function ActiveDevice({ device, params, isNeedShowLoad = true, isNeedContect = true, disconnectCallback = Function(), } = {}) { _disConnectCallback = disconnectCallback activeCode.params = params activeCode.isNeedShowLoad = isNeedShowLoad || true if (isNeedContect) { _configNormalActiveCode() return await _connectDevice(device, 1) } else { return await _configIssueApply() } } // 设备校验 export async function EquipmentDetection({ device, params, isNeedShowLoad = true, checkStatusCallback = Function(), } = {}) { _disConnectCallback = checkStatusCallback _configNormalActiveCode() activeCode.params = params activeCode.isNeedShowLoad = isNeedShowLoad || true return await _connectDevice(device, 11) } /** * 卡片检测 * * @param device 设备对象 * @param params 检测参数,默认为空对象,可包含 OrderId 和 PlateNumber 属性 * @param isNeedShowLoad 是否需要显示加载动画,默认为 true * @param checkStatusCallback 检测状态回调函数,默认为 null * @returns 返回连接设备的 Promise 对象 */ export async function CardDetection({ device, params, isNeedShowLoad = true, disconnectCallback = Function(), } = {}) { _disConnectCallback = disconnectCallback _configNormalActiveCode() activeCode.params = params activeCode.isNeedShowLoad = isNeedShowLoad || true return await _connectDevice(device, 2) } /** * 重新激活设备 * * @async * @returns {Promise} 返回一个Promise对象,表示设备连接的结果 */ export async function Reactivate() { const { device, params, isNeedShowLoad, disconnectCallback } = arguments[0] _disConnectCallback = disconnectCallback _configNormalActiveCode() activeCode.params = params activeCode.isNeedShowLoad = isNeedShowLoad || true return await _connectDevice(device, 4) } /** * 断开与设备的连接 * * @returns 返回断开连接的结果 */ export async function DisConnectDevice() { _isDeviceEnd = true const res = await disconnect() console.log(jsonToString(res)) return res } /** * 重新读取设备信息 * * @param {*} device * @param {*} bussType */ export async function ObuSystemInfo () { return await _obuSystemInfo() } /// -------------------- 私有方法 ----------------------- /// /// -------------------- 设备相关 async function _connectDevice(device, bussType) { console.log('准备连接设备') const deviceName = device.name activeCode.deviceName = deviceName activeCode.bussType = bussType console.log('设备名称:' + deviceName) if (!device || !device.name) { return { code: 251, msg: "设备名称为空" } } _conFigCurrentCallBack(200, deviceName) const res = await connectAndInit(device, () => { if (!_isDeviceEnd) { if (activeCode.isNeedShowLoad) { console.log('连接断开隐藏加载框') wx.hideLoading() } console.log('设备连接断开') typeof _disConnectCallback === 'function' && _disConnectCallback() } }) if (res.code == 0) { if (bussType == 1) { return await _configIssueApply() } else { return await _obuSystemInfo() } } _conFigCurrentCallBack(250, deviceName) res.code = 250 return res } /// -------------------- OBU相关 /// -------------------- OBU信息读取相关 /** * 获取系统信息并进行处理 * * @param type 类型,默认为0 * @returns 返回处理结果 */ async function _obuSystemInfo(type = 0) { _conFigCurrentCallBack(300) const res = await getObuSystemInfo() console.log('获取系统信息结果:' + jsonToString(res)) if (res.code == 0) { console.log('获取系统信息成功') const sytem = res.data if (sytem) { console.log('OBU系统信息:' + sytem) const contractType = sytem.substr(16, 2) const contractVersion = sytem.substr(18, 2) const serialNumber = sytem.substr(20, 16) const star = sytem.substr(36, 8) const end = sytem.substr(44, 8) const activeState = sytem.substr(52, 2) const lssuer = sytem.substr(8, 8) activeCode.contractType = contractType activeCode.contractVersion = contractVersion activeCode.serialNumber = serialNumber activeCode.startTime = star activeCode.expireTime = end activeCode.acState = activeState == '01' ? true : false activeCode.lssuer = lssuer console.log('协约类型' + contractType) console.log('合同版本' + contractVersion) console.log('合同序列号' + serialNumber) console.log('开始时间' + star) console.log('结束时间' + end) console.log('激活状态' + activeState) console.log('发行方' + lssuer) if (type == 0) { if ( activeCode.bussType == 2 || activeCode.bussType == 11 || activeCode.bussType == 4 || activeCode.bussType == 12 ) { return _iccFile15Info() } else if (activeCode.bussType == 1) { return _configIssueApply() } } else { if (activeCode.bussType == 1) { if (activeCode.acState == true) { return _iccFile15Info(1) } else { return _configFailCallBack({ msg: '标签位激活失败', }) } } else if (activeCode.bussType == 4) { if (activeCode.acState == true) { activeCode.vehiclePlate = activeCode.cardPlateNumber activeCode.vehiclePlateColor = activeCode.cardPlateColor return _againObuActiveSuccess() } else { return { code: -1, msg: '重新激活失败', } } } else if (activeCode.bussType == 2) { return _configSuccessCallBack('卡签检测成功') } } } } _conFigCurrentCallBack(350) return res } /// -------------------- OBU信息写入相关 /// -------------------- ICC相关 async function _iccFile15Info(type = 0) { _conFigCurrentCallBack(304) const res = await getIccFile15() if (res.code == 0) { let data = res.data console.log('读取CPU 0015文件:' + data) let sytem = data /// 卡片类型 let cardType = sytem.substr(16, 2) /// 卡片版本号 let cardVersion = sytem.substr(18, 2) /// 卡片网络编号 + 用户卡片内部编号 let cardNumber = sytem.substr(20, 20) activeCode.cardVersionHex = cardVersion; activeCode.cardVersion = hex2int(cardVersion) activeCode.cardType = hex2int(cardType) activeCode.faceCardNum = cardNumber /// 卡片内部编号 activeCode.cardUid = sytem.substr(24, 16) console.log('卡片类型' + activeCode.cardType) console.log('卡号' + activeCode.faceCardNum) console.log('卡版本' + activeCode.cardVersion) if (sytem.length > 84) { /// 启用时间 let cardStar = sytem.substring(40, 48) /// 到期时间 let cardEnd = sytem.substring(48, 56) /// 车牌号码 let plateNo = sytem.substring(56, 80) /// 车牌颜色 let plateColor = hex2int(sytem.substring(82, 84)) if ( plateNo == '000000000000000000000000' || plateNo == 'ffffffffffffffffffffffff' || plateNo == 'FFFFFFFFFFFFFFFFFFFFFFFF' ) { console.log('车牌号为空') activeCode.cardPlateNumber = '未知车牌' activeCode.cardPlateColor = '-1' activeCode.vehiclePlate = '未知车牌' activeCode.vehiclePlateColor = '-1' } else { activeCode.cardPlateCode = plateNo activeCode.cardPlateColor = plateColor activeCode.cardPlateNumber = decode(plateNo) } activeCode.plateNumber = activeCode.cardPlateNumber activeCode.cardStar = cardStar activeCode.cardEnd = cardEnd activeCode.cardVehicleType = hex2int(sytem.substring(84, 86)) console.log('卡开始时间' + cardStar) console.log('卡结束时间' + cardEnd) console.log('车牌号十六进制' + plateNo) console.log('车牌颜色' + plateColor) console.log('车型' + activeCode.cardVehicleType) } if (activeCode.bussType == 1) { if (type == 0) { return _cardIssueCheck() } else { return _configSuccessCallBack('激活成功') } } else if (activeCode.bussType == 2 || activeCode.bussType == 12) { if (activeCode.cardPlateNumber == '未知车牌' && activeCode.bussType == 12) { activeCode.plateNumber = '未知车牌' activeCode.vehiclePlate = '未知车牌' activeCode.vehiclePlateColor = '-1' activeCode.catInfo = { licensePlate: '未知车牌', } return _configSuccessCallBack('检测成功') } else { if (activeCode.bussType == 2) { if (activeCode.cardPlateNumber == '未知车牌') { return _configSuccessCallBack('检测成功') } return _obuVehicleInfo() } else if (activeCode.bussType == 12) { activeCode.vehiclePlate = activeCode.cardPlateNumber activeCode.vehiclePlateColor = activeCode.cardPlateColor return _configSuccessCallBack('检测成功') } } } else if (activeCode.bussType == 11) { return _configSuccessCallBack('校验信息获取成功') } else if (activeCode.bussType == 4) { if (activeCode.cardPlateNumber == '未知车牌') { return _configFailCallBack({ msg: '设备未激活,请先激活设备', }) } else { return _againObuIssueCheck() } } } else { _conFigCurrentCallBack(354) } return res } /// -------------------- 网络相关 ---------------------- /// // 新版激活接口 async function _instApply() { _conFigCurrentCallBack(750, "正在激活,请等待") let params = {} params.orderNo = activeCode.params.orderNo; params.cardId = activeCode.faceCardNum; params.obuId = activeCode.serialNumber; params.cardVersion = activeCode.cardVersionHex params.obuVersion = activeCode.contractVersion const res = await network.etc.instApply( params.orderNo, params.cardId, params.obuId, params.cardVersion, params.obuVersion, ); console.log(res) if (res.code == 0) { return _configIssueNetWorkData(res.data) } else { return _configFailCallBack({ msg: (res.message || '发行指令申请失败,请稍后重试') + res.code }) } } async function _instCallBack(cmdResult) { const res = await network.etc.instCallBack( issueResult.transOrderId, issueResult.cmd, cmdResult, issueResult.stepNo, ) if (res.code == 0) { return _configIssueNetWorkData(res.data) } else { return _configFailCallBack({ msg: (res.message || '发行指令回传失败,请稍后重试') + res.code }) } } // 老版本 /// 配置发行指令申请 /// issueType - 指令类型 1-卡 2-签 async function _configIssueApply() { return await _instApply() // const params = { // ...activeCode.params, // }; // _conFigCurrentCallBack(750, "正在激活,请等待") // const res = await apiIssueApply(params) // console.log(res) // if (res.code == 0) { // return _configIssueNetWorkData(res.data) // } else { // return _configFailCallBack({ // msg: (res.message || '发行指令申请失败,请稍后重试') + res.code // }) // } } /// 配置发行指令回传 async function _configIssueCallback(cmdResult) { return await _instCallBack(cmdResult); // const params = { // openId: activeCode.params.openId, // transOrderId: issueResult.transOrderId, // cmd: issueResult.cmd, // cmdResult, // stepNo: issueResult.stepNo, // }; // const res = await apiIssueCallback(params) // if (res.code == 0) { // return _configIssueNetWorkData(res.data) // } else { // return _configFailCallBack({ // msg: (res.message || '发行指令回传失败,请稍后重试') + res.code // }) // } } async function _configIssueNetWorkData(data) { issueResult = data; const cmdArr = data.cmd.split(',') const cmdType = data.cmdType == "CARD" ? '10' : '20' if (issueResult.stepNo == 100) { return _obuSystemInfo(1) } else { const res = await transCmd(cmdArr, cmdType) if (res.code == 0) { return await _configIssueCallback(res.cmdResult) } else { return _configFailCallBack({ msg: res.msg || "激活失败" }) } } } /** * 配置成功回调函数 * * @param msg 成功消息内容 * @returns 配置成功后的返回对象 */ function _configSuccessCallBack(msg) { _conFigCurrentCallBack(500) return { code: 0, data: activeCode, msg: msg, } } /** * 处理网络请求失败的情况 * * @param data 任意类型的数据,表示请求返回的数据 * @param msg 错误信息字符串 * @returns 返回包含错误码、数据和错误信息的对象 */ function _configFailCallBack({ data = activeCode, msg = '网络请求失败', code = -1 } = {}) { _conFigCurrentCallBack(550, msg) return { code, data: data, msg: msg, } } /** * 回调方法 */ function _conFigCurrentCallBack(code, name) { const { type, msg } = configBlueStateMsg(code, name) if (type == 1) { typeof _checkStatusCallback === 'function' && _checkStatusCallback(msg) if (activeCode.isNeedShowLoad) { wx.showLoading({ title: msg, }) } } else { _isDeviceEnd = true if (activeCode.isNeedDisConnect) { console.log("_conFigCurrentCallBack") console.log("执行断开连接设备") DisConnectDevice() } if (activeCode.isNeedShowLoad && activeCode.isNeedDisConnect) { wx.hideLoading() } } return type } /** * 配置普通激活码的属性 */ function _configNormalActiveCode() { _isDeviceEnd = false activeCode.params = { OrderId: '', PlateNumber: '', } /// 卡片版本号 activeCode.cardVersion = '' // 卡片类型 activeCode.cardType = '' /// 卡片里 车型 1 客车、2 货车 activeCode.cardVehicleType = '1' /// 合同类型 activeCode.contractType = '' /// 合同版本 activeCode.contractVersion = '' /// 卡表面号, 网络编号的20位表面号 activeCode.faceCardNum = '' /// 卡表面号,不带网络编号的16位表面号 activeCode.cardUid = '' /// 车牌号编码 activeCode.vehiclePlateCode = '' /// 车牌号 activeCode.vehiclePlate = '' /// 车牌号 activeCode.plateNumber = '' /// OBU 车型 1 客车、2 货车 activeCode.vehicleType = '1' /// 车牌颜色 activeCode.vehiclePlateColor = '0' /// 卡片车牌号码 activeCode.cardPlateNumber = '' /// 卡片车牌颜色 activeCode.cardPlateColor = '-1' /// 办理状态 /// 0、开始发行 /// 10、上传图片成功、 /// 20、写Obu成功 /// 30、写卡成功 /// 40、激活成功 /// 50、二次已激活 activeCode.issueState = 10 /// OBU序列号,16位合同序列号 activeCode.serialNumber = '' /// 服务供应商 activeCode.supplier = '贵州' /// 订单号 activeCode.OrderNo = '' /// 开始时间 activeCode.startTime = '' /// 结束时间 activeCode.expireTime = '' /// 业务类型 1 激活 4 二次激活 2 卡签检测 activeCode.bussType = 1 /// 激活状态 activeCode.acState = false /// obu mac 地址 activeCode.obuId = 'FFFFFFFF' /// 发行方标识 activeCode.lssuer = '' /// 卡片激活开始时间 activeCode.cardStar = '' /// 卡片激活结束时间 activeCode.cardEnd = '' /// 电量 activeCode.electricity = '40' /// true 天线关闭 false 天线打开 activeCode.equipmentState = false /// 是否支持 打开 和 关闭 天线 activeCode.isShowEquipment = false activeCode.content = '' /// 换卡换签 网络返回数据 activeCode.netData = {} /// 车辆解密信息 activeCode.catInfo = { licensePlate: '未知车牌', } /// 1 换卡 2 换签 3 全套 activeCode.afterType = '1' /// 设备名字 activeCode.deviceName = '' /// 0 支持 1 不支持 activeCode.supportLock = '0' /// 回调 activeCode.callBack = '' /// 是否显示默认加载 动画 activeCode.isNeedShowLoad = true /// 是否需要校验设备名字 activeCode.isNeedCheckName = true activeCode.isNeedDisConnect = false }