|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346 |
- /*
- */
- "use strict";
- import _kcDataEncode from "./kcDataEncode.js";
- import _kcDataInteract from "./kcDataInteract.js";
- import _kcDataAnalysis from "./kcDataAnalysis.js";
- import _kcUtils from "./kcUtils.js";
-
- var TAG_FUNCTION = "function";
- let isProcessing = false;
-
- // 将 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 = _kcDataEncode.encode(cmd, o, n);
- // 发送数据
- _kcDataInteract._StartSendData(t.serviceData.dataEncode, function (e) {
- if (e.serviceCode == 0) {
- console.log("%cTPDU透传指令成功", "color: green;");
- // 解析数据
- var i = _kcDataAnalysis._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 === TAG_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 = _kcDataEncode.encode(concatenateCmd, o, n);
- _kcDataInteract._StartSendData(t.serviceData.dataEncode, function (e) {
- switch (e.serviceCode) {
- case 0:
- case -5:
- console.log("%cTPDU透传指令成功 %d", "color: green;", e.serviceCode);
- var i = _kcDataAnalysis._analyzeUnpacksIntoArrays(e.serviceData.dataBuff);
- if (e.serviceCode == -5 && i.serviceCode == 0) {
- result.code = -5;
- } else {
- result.code = i.serviceCode;
- }
- result.data = i.unpacksData;
- result.msg = '执行成功';
- break;
- default:
- result.code = -1;
- result.data = [];
- result.msg = '执行失败';
- console.error("TPDU透传指令失败");
- break;
- }
- if (typeof callback === TAG_FUNCTION) {
- callback(result);
- }
- });
- } else {
- if (typeof callback === TAG_FUNCTION) {
- result.msg = 'cmdtype参数错误';
- callback(result);
- }
- }
- }
- function generalTransCmd(cmdType = "AC", cmdArray, dataParser, dataType = "00", successLog = "透传指令") {
- var concatenateCmd = "";
- 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);
- return sendCommand(cmdType, concatenateCmd, successLog, dataParser, dataType);
- }
- function customCmd(cmdType = "7F", command, dataParser, dataType = "00", successLog = "透传指令") {
- return sendCommand(cmdType, command, successLog, dataParser, dataType, "9F");
- }
- // 通用命令发送函数
- function sendCommand(cmdType, command, successLog, dataParser, dataType = "00", ST = "33") {
- return new Promise((resolve, reject) => {
- if (isProcessing) throw new Error("请求正在处理中");
- isProcessing = true;
- try {
- const encodedData = _kcDataEncode.encode(command, dataType, cmdType, ST);
- _kcDataInteract._StartSendData(encodedData.serviceData.dataEncode, (response) => {
- try {
- if (response.serviceCode === 0) {
- const result = dataParser(response.serviceData.dataBuff);
- if (result.serviceCode === 0) {
- result.serviceInfo = successLog + "成功";
- console.log(successLog + "成功", result.serviceCode);
- resolve(result);
- } else {
- console.error(successLog + "失败", result);
- reject(result);
- }
- } else {
- console.error(successLog + "失败", response);
- reject(response);
- }
- } catch (err) {
- console.error("处理设备信息时发生异常:", err);
- reject({ serviceCode: -1, serviceInfo: "内部错误" });
- } finally {
- isProcessing = false;
- }
- });
- } finally {
- isProcessing = false;
- }
- });
- };
- // 获取防拆柱状态
- function getDisassemblyPillar() {
- return sendCommand(
- "A5",
- "01CA",
- "获取防拆柱状态",
- (dataBuff) => _kcDataAnalysis.analysisDeviceInfo(dataBuff)
- );
- }
- // 获取激活状态
- function getActState() {
- return sendCommand(
- "A5",
- "01C9",
- "获取激活状态",
- (dataBuff) => _kcDataAnalysis.analysisDeviceInfo(dataBuff)
- );
- }
- // 获取电池电量
- function getDevicePower() {
- return sendCommand(
- "A5",
- "01C2",
- "获取电池电量",
- (dataBuff) => _kcDataAnalysis.analysisDeviceInfo(dataBuff)
- );
- }
- // 获取设备编号
- function getDeviceNumber() {
- return sendCommand(
- "A5",
- "01C0",
- "获取设备编号",
- (dataBuff) => _kcDataAnalysis.analysisDeviceInfo(dataBuff)
- );
- }
- // 获取蓝牙设备版本号
- function getBleDeviceVersion() {
- return sendCommand(
- "A5",
- "01C1",
- "获取蓝牙设备版本号",
- (dataBuff) => _kcDataAnalysis.analysisDeviceInfo(dataBuff)
- );
- }
- // 获取OBU设备版本号
- function getObuDeviceVersion() {
- return sendCommand(
- "A5",
- "01C8",
- "获取OBU设备版本号",
- (dataBuff) => _kcDataAnalysis.analysisDeviceInfo(dataBuff)
- );
- }
- function obuSetSleepTime(e, t) {
- var a = ""
- , a = _kcUtils.array2Str(e)
- , e = parseInt(a, 16);
- e < 60 ? a = "3c" : 255 < e && (a = "ff"),
- e = _kcDataEncode.encode("AC01D8" + a),
- _kcDataInteract._StartSendData(e.serviceData.dataEncode, function (e) {
- e.serviceCode,
- e = e.serviceCode,
- (void 0 === t ? "undefined" : _typeof(t)) == TAG_FUNCTION && t(e)
- })
- }
- function getIcCardInfo() {
- console.log('==========Read_0015_Start==========');
- return generalTransCmd("A3", ['00A40000023F00', '00A40000021001', '00B095002B'],
- (dataBuff) => {
- let result;
- let i = _kcDataAnalysis._analysisIs9000(dataBuff);
- if (i.serviceCode === 0) {
- result = _kcDataAnalysis.analysisCardFile(i.unpacksData[2]);
- } else {
- result = i;
- }
- return result;
- });
- }
- function turnOn58GANT() {
- return customCmd(
- "7F", "03E50101",
- (dataBuff) => _kcDataAnalysis.analysisCustomDIR(dataBuff),
- "00", "打开5.8G天线"
- );
- }
- function turnOff58GANT() {
- return customCmd(
- "7F", "03E50100",
- (dataBuff) => _kcDataAnalysis.analysisCustomDIR(dataBuff),
- "00", "关闭5.8G天线"
- );
- }
- function get58GStatus() {
- return customCmd(
- "7F", "02E60F",
- (dataBuff) => _kcDataAnalysis.analysisCustomDIR(dataBuff),
- "00", "获取5.8G状态"
- );
- }
- function getIcCardSwitchStatus() {
- return customCmd(
- "7F", "02E60F",
- (dataBuff) => _kcDataAnalysis.analysisCustomDIR(dataBuff),
- "00", "获取IC卡开关状态"
- );
- }
- function getVoiceFeature() {
- return customCmd(
- "7F", "02E60F",
- (dataBuff) => _kcDataAnalysis.analysisCustomDIR(dataBuff),
- "00", "获取语音功能"
- );
- }
- function getBatteryPercentage() {
- return customCmd(
- "7F", "02E60F",
- (dataBuff) => _kcDataAnalysis.analysisCustomDIR(dataBuff),
- "00", "获取电量百分比"
- );
- }
- export default {
- singleTransCmd, transCmdLoop, transCmd, getIcCardInfo, getDeviceNumber, obuSetSleepTime, getActState, getDisassemblyPillar, getDevicePower, getObuDeviceVersion, turnOn58GANT, turnOff58GANT, get58GStatus, getIcCardSwitchStatus, getVoiceFeature, getBatteryPercentage, getBleDeviceVersion
- };
|