您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

kcBaseBIZservice.js 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /*
  2. */
  3. "use strict";
  4. import _kcDataEncode from "./kcDataEncode.js";
  5. import _kcDataInteract from "./kcDataInteract.js";
  6. import _kcDataAnalysis from "./kcDataAnalysis.js";
  7. import _kcUtils from "./kcUtils.js";
  8. var TAG_FUNCTION = "function";
  9. let isProcessing = false;
  10. // 将 singleTransCmd 函数改为返回 Promise
  11. function singleTransCmd(cmdtype, cmd) {
  12. return new Promise((resolve, reject) => {
  13. var n = "",
  14. o = "",
  15. result = {
  16. code: -1,
  17. data: "",
  18. msg: '执行失败'
  19. };
  20. console.log("cmdtype:" + cmdtype);
  21. if (cmdtype == '10') {
  22. n = "A3";
  23. o = "00";
  24. console.log("IC卡指令");
  25. } else if (cmdtype == '20') {
  26. n = "AC";
  27. o = "00";
  28. console.log("OBU ESAM卡指令");
  29. }
  30. // 输出原始命令
  31. console.log("strhex:" + cmd);
  32. // 处理命令
  33. if (o == "10" || o == "20" || o == "30" || o == "00") {
  34. // 编码命令
  35. var t = _kcDataEncode.encode(cmd, o, n);
  36. // 发送数据
  37. _kcDataInteract._StartSendData(t.serviceData.dataEncode, function (e) {
  38. if (e.serviceCode == 0) {
  39. console.log("%cTPDU透传指令成功", "color: green;");
  40. // 解析数据
  41. var i = _kcDataAnalysis._analysisIs9000(e.serviceData.dataBuff);
  42. result.code = i.serviceCode;
  43. result.data = i.unpacksData[0];
  44. result.msg = i.serviceCode == 0 ? '执行成功' : '执行失败';
  45. } else {
  46. result.code = -1;
  47. result.data = "";
  48. result.msg = '执行失败';
  49. console.error("TPDU透传指令失败");
  50. }
  51. // 调用回调函数
  52. resolve(result);
  53. });
  54. } else {
  55. // 处理错误的cmdtype参数
  56. result.msg = 'cmdtype参数错误';
  57. reject(result);
  58. }
  59. });
  60. }
  61. // 修改 transCmdLoop 函数,按顺序执行 singleTransCmd,并在失败时停止循环
  62. async function transCmdLoop(cmdArray, cmdtype, callback) {
  63. let results = {
  64. code: -1,
  65. data: [],
  66. msg: ''
  67. }; // 存储每个命令的结果
  68. for (let cmd of cmdArray) {
  69. try {
  70. let concatenateCmd = "";
  71. // 计算指令长度(字符数的一半),并转换为十六进制字符串
  72. let cmdLength = (cmd.length / 2).toString(16).padStart(2, '0');
  73. // 拼接字符串
  74. concatenateCmd += "01" + cmdLength + cmd;
  75. const result = await singleTransCmd(cmdtype, concatenateCmd);
  76. results.code = result.code;
  77. results.msg = result.msg;
  78. results.data.push(result.data);
  79. console.log("命令 " + cmd + " 处理完成,结果:", result);
  80. // 如果执行结果不成功,停止循环
  81. if (result.code !== 0) {
  82. console.error("命令执行失败,停止循环");
  83. break;
  84. }
  85. } catch (error) {
  86. results.code = -1;
  87. results.msg = '执行失败';
  88. results.data.push("");
  89. console.error("命令 " + cmd + " 处理失败,错误:", error);
  90. break; // 捕获到错误时停止循环
  91. }
  92. }
  93. console.log("所有命令处理完毕");
  94. if (typeof callback === TAG_FUNCTION) {
  95. callback(results); // 所有命令处理完毕后,调用回调函数
  96. }
  97. }
  98. function transCmd(cmdArray, cmdtype, callback) {
  99. var n = "",
  100. o = "",
  101. concatenateCmd = "",
  102. result = {
  103. code: -1,
  104. data: [],
  105. msg: '执行失败'
  106. };
  107. console.log("cmdtype:" + cmdtype);
  108. if (cmdtype == '10') {
  109. n = "A3";
  110. o = "00";
  111. console.log("IC卡指令");
  112. } else if (cmdtype == '20') {
  113. n = "AC";
  114. o = "00";
  115. console.log("OBU ESAM卡指令");
  116. }
  117. cmdArray.forEach((cmd, index) => {
  118. // 将 index 转换为十六进制字符串
  119. let hexIndex = (index + 1).toString(16).padStart(2, '0');
  120. // 计算指令长度(字符数的一半),并转换为十六进制字符串
  121. let cmdLength = (cmd.length / 2).toString(16).padStart(2, '0');
  122. // 拼接字符串
  123. concatenateCmd += hexIndex + cmdLength + cmd;
  124. });
  125. console.log("strhex:" + concatenateCmd);
  126. // Process command based on the value of o
  127. if (o == "10" || o == "20" || o == "30" || o == "00") {
  128. var t = _kcDataEncode.encode(concatenateCmd, o, n);
  129. _kcDataInteract._StartSendData(t.serviceData.dataEncode, function (e) {
  130. switch (e.serviceCode) {
  131. case 0:
  132. case -5:
  133. console.log("%cTPDU透传指令成功 %d", "color: green;", e.serviceCode);
  134. var i = _kcDataAnalysis._analyzeUnpacksIntoArrays(e.serviceData.dataBuff);
  135. if (e.serviceCode == -5 && i.serviceCode == 0) {
  136. result.code = -5;
  137. } else {
  138. result.code = i.serviceCode;
  139. }
  140. result.data = i.unpacksData;
  141. result.msg = '执行成功';
  142. break;
  143. default:
  144. result.code = -1;
  145. result.data = [];
  146. result.msg = '执行失败';
  147. console.error("TPDU透传指令失败");
  148. break;
  149. }
  150. if (typeof callback === TAG_FUNCTION) {
  151. callback(result);
  152. }
  153. });
  154. } else {
  155. if (typeof callback === TAG_FUNCTION) {
  156. result.msg = 'cmdtype参数错误';
  157. callback(result);
  158. }
  159. }
  160. }
  161. function generalTransCmd(cmdType = "AC", cmdArray, dataParser, dataType = "00", successLog = "透传指令") {
  162. var concatenateCmd = "";
  163. cmdArray.forEach((cmd, index) => {
  164. // 将 index 转换为十六进制字符串
  165. let hexIndex = (index + 1).toString(16).padStart(2, '0');
  166. // 计算指令长度(字符数的一半),并转换为十六进制字符串
  167. let cmdLength = (cmd.length / 2).toString(16).padStart(2, '0');
  168. // 拼接字符串
  169. concatenateCmd += hexIndex + cmdLength + cmd;
  170. });
  171. console.log("strhex:" + concatenateCmd);
  172. return sendCommand(cmdType, concatenateCmd, successLog, dataParser, dataType);
  173. }
  174. function customCmd(cmdType = "7F", command, dataParser, dataType = "00", successLog = "透传指令") {
  175. return sendCommand(cmdType, command, successLog, dataParser, dataType, "9F");
  176. }
  177. // 通用命令发送函数
  178. function sendCommand(cmdType, command, successLog, dataParser, dataType = "00", ST = "33") {
  179. return new Promise((resolve, reject) => {
  180. if (isProcessing) throw new Error("请求正在处理中");
  181. isProcessing = true;
  182. try {
  183. const encodedData = _kcDataEncode.encode(command, dataType, cmdType, ST);
  184. _kcDataInteract._StartSendData(encodedData.serviceData.dataEncode, (response) => {
  185. try {
  186. if (response.serviceCode === 0) {
  187. const result = dataParser(response.serviceData.dataBuff);
  188. if (result.serviceCode === 0) {
  189. result.serviceInfo = successLog + "成功";
  190. console.log(successLog + "成功", result.serviceCode);
  191. resolve(result);
  192. } else {
  193. console.error(successLog + "失败", result);
  194. reject(result);
  195. }
  196. } else {
  197. console.error(successLog + "失败", response);
  198. reject(response);
  199. }
  200. } catch (err) {
  201. console.error("处理设备信息时发生异常:", err);
  202. reject({ serviceCode: -1, serviceInfo: "内部错误" });
  203. } finally {
  204. isProcessing = false;
  205. }
  206. });
  207. } finally {
  208. isProcessing = false;
  209. }
  210. });
  211. };
  212. // 获取防拆柱状态
  213. function getDisassemblyPillar() {
  214. return sendCommand(
  215. "A5",
  216. "01CA",
  217. "获取防拆柱状态",
  218. (dataBuff) => _kcDataAnalysis.analysisDeviceInfo(dataBuff)
  219. );
  220. }
  221. // 获取激活状态
  222. function getActState() {
  223. return sendCommand(
  224. "A5",
  225. "01C9",
  226. "获取激活状态",
  227. (dataBuff) => _kcDataAnalysis.analysisDeviceInfo(dataBuff)
  228. );
  229. }
  230. // 获取电池电量
  231. function getDevicePower() {
  232. return sendCommand(
  233. "A5",
  234. "01C2",
  235. "获取电池电量",
  236. (dataBuff) => _kcDataAnalysis.analysisDeviceInfo(dataBuff)
  237. );
  238. }
  239. // 获取设备编号
  240. function getDeviceNumber() {
  241. return sendCommand(
  242. "A5",
  243. "01C0",
  244. "获取设备编号",
  245. (dataBuff) => _kcDataAnalysis.analysisDeviceInfo(dataBuff)
  246. );
  247. }
  248. // 获取蓝牙设备版本号
  249. function getBleDeviceVersion() {
  250. return sendCommand(
  251. "A5",
  252. "01C1",
  253. "获取蓝牙设备版本号",
  254. (dataBuff) => _kcDataAnalysis.analysisDeviceInfo(dataBuff)
  255. );
  256. }
  257. // 获取OBU设备版本号
  258. function getObuDeviceVersion() {
  259. return sendCommand(
  260. "A5",
  261. "01C8",
  262. "获取OBU设备版本号",
  263. (dataBuff) => _kcDataAnalysis.analysisDeviceInfo(dataBuff)
  264. );
  265. }
  266. function obuSetSleepTime(e, t) {
  267. var a = ""
  268. , a = _kcUtils.array2Str(e)
  269. , e = parseInt(a, 16);
  270. e < 60 ? a = "3c" : 255 < e && (a = "ff"),
  271. e = _kcDataEncode.encode("AC01D8" + a),
  272. _kcDataInteract._StartSendData(e.serviceData.dataEncode, function (e) {
  273. e.serviceCode,
  274. e = e.serviceCode,
  275. (void 0 === t ? "undefined" : _typeof(t)) == TAG_FUNCTION && t(e)
  276. })
  277. }
  278. function getIcCardInfo() {
  279. console.log('==========Read_0015_Start==========');
  280. return generalTransCmd("A3", ['00A40000023F00', '00A40000021001', '00B095002B'],
  281. (dataBuff) => {
  282. let result;
  283. let i = _kcDataAnalysis._analysisIs9000(dataBuff);
  284. if (i.serviceCode === 0) {
  285. result = _kcDataAnalysis.analysisCardFile(i.unpacksData[2]);
  286. } else {
  287. result = i;
  288. }
  289. return result;
  290. });
  291. }
  292. function turnOn58GANT() {
  293. return customCmd(
  294. "7F", "03E50101",
  295. (dataBuff) => _kcDataAnalysis.analysisCustomDIR(dataBuff),
  296. "00", "打开5.8G天线"
  297. );
  298. }
  299. function turnOff58GANT() {
  300. return customCmd(
  301. "7F", "03E50100",
  302. (dataBuff) => _kcDataAnalysis.analysisCustomDIR(dataBuff),
  303. "00", "关闭5.8G天线"
  304. );
  305. }
  306. function get58GStatus() {
  307. return customCmd(
  308. "7F", "02E60F",
  309. (dataBuff) => _kcDataAnalysis.analysisCustomDIR(dataBuff),
  310. "00", "获取5.8G状态"
  311. );
  312. }
  313. function getIcCardSwitchStatus() {
  314. return customCmd(
  315. "7F", "02E60F",
  316. (dataBuff) => _kcDataAnalysis.analysisCustomDIR(dataBuff),
  317. "00", "获取IC卡开关状态"
  318. );
  319. }
  320. function getVoiceFeature() {
  321. return customCmd(
  322. "7F", "02E60F",
  323. (dataBuff) => _kcDataAnalysis.analysisCustomDIR(dataBuff),
  324. "00", "获取语音功能"
  325. );
  326. }
  327. function getBatteryPercentage() {
  328. return customCmd(
  329. "7F", "02E60F",
  330. (dataBuff) => _kcDataAnalysis.analysisCustomDIR(dataBuff),
  331. "00", "获取电量百分比"
  332. );
  333. }
  334. export default {
  335. singleTransCmd, transCmdLoop, transCmd, getIcCardInfo, getDeviceNumber, obuSetSleepTime, getActState, getDisassemblyPillar, getDevicePower, getObuDeviceVersion, turnOn58GANT, turnOff58GANT, get58GStatus, getIcCardSwitchStatus, getVoiceFeature, getBatteryPercentage, getBleDeviceVersion
  336. };