Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

active-device-tools.js 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  1. import {
  2. configBlueStateMsg,
  3. decode,
  4. hex2int
  5. } from '../etc-sdk-tool/gbk-tool'
  6. import {
  7. jsonToString
  8. } from '../obj-tools'
  9. import {
  10. connectAndInit,
  11. disconnect,
  12. transCmd,
  13. getIccFile15,
  14. getObuSystemInfo,
  15. } from '../etc-sdk-tool/etc-sdk-tool'
  16. import activeCode from './active-device-code'
  17. import {
  18. apiIssueApply,
  19. apiIssueCallback
  20. } from '../etcReq'
  21. const network = require('../../network/index')
  22. let devResult = {
  23. code: 0,
  24. msg: '', //信息
  25. data: [],
  26. }
  27. let issueResult = {
  28. cmd: '',
  29. cmdType: '',
  30. stepNo: '',
  31. transOrderId: '',
  32. OrderNo: '',
  33. isOnline: '',
  34. }
  35. const CmdStatus = '9000'
  36. let _checkStatusCallback = Function()
  37. let _isDeviceEnd = false
  38. let _disConnectCallback = Function()
  39. /// -------------------- 公开方法 ----------------------- ///
  40. // 激活设备
  41. export async function ActiveDevice({
  42. device,
  43. params,
  44. isNeedShowLoad = true,
  45. isNeedContect = true,
  46. disconnectCallback = Function(),
  47. } = {}) {
  48. _disConnectCallback = disconnectCallback
  49. activeCode.params = params
  50. activeCode.isNeedShowLoad = isNeedShowLoad || true
  51. if (isNeedContect) {
  52. _configNormalActiveCode()
  53. return await _connectDevice(device, 1)
  54. } else {
  55. return await _configIssueApply()
  56. }
  57. }
  58. // 设备校验
  59. export async function EquipmentDetection({
  60. device,
  61. params,
  62. isNeedShowLoad = true,
  63. checkStatusCallback = Function(),
  64. } = {}) {
  65. _disConnectCallback = checkStatusCallback
  66. _configNormalActiveCode()
  67. activeCode.params = params
  68. activeCode.isNeedShowLoad = isNeedShowLoad || true
  69. return await _connectDevice(device, 11)
  70. }
  71. /**
  72. * 卡片检测
  73. *
  74. * @param device 设备对象
  75. * @param params 检测参数,默认为空对象,可包含 OrderId 和 PlateNumber 属性
  76. * @param isNeedShowLoad 是否需要显示加载动画,默认为 true
  77. * @param checkStatusCallback 检测状态回调函数,默认为 null
  78. * @returns 返回连接设备的 Promise 对象
  79. */
  80. export async function CardDetection({
  81. device,
  82. params,
  83. isNeedShowLoad = true,
  84. disconnectCallback = Function(),
  85. } = {}) {
  86. _disConnectCallback = disconnectCallback
  87. _configNormalActiveCode()
  88. activeCode.params = params
  89. activeCode.isNeedShowLoad = isNeedShowLoad || true
  90. return await _connectDevice(device, 2)
  91. }
  92. /**
  93. * 重新激活设备
  94. *
  95. * @async
  96. * @returns {Promise<any>} 返回一个Promise对象,表示设备连接的结果
  97. */
  98. export async function Reactivate() {
  99. const {
  100. device,
  101. params,
  102. isNeedShowLoad,
  103. disconnectCallback
  104. } = arguments[0]
  105. _disConnectCallback = disconnectCallback
  106. _configNormalActiveCode()
  107. activeCode.params = params
  108. activeCode.isNeedShowLoad = isNeedShowLoad || true
  109. return await _connectDevice(device, 4)
  110. }
  111. /**
  112. * 断开与设备的连接
  113. *
  114. * @returns 返回断开连接的结果
  115. */
  116. export async function DisConnectDevice() {
  117. _isDeviceEnd = true
  118. const res = await disconnect()
  119. console.log(jsonToString(res))
  120. return res
  121. }
  122. /**
  123. * 重新读取设备信息
  124. *
  125. * @param {*} device
  126. * @param {*} bussType
  127. */
  128. export async function ObuSystemInfo () {
  129. return await _obuSystemInfo()
  130. }
  131. /// -------------------- 私有方法 ----------------------- ///
  132. /// -------------------- 设备相关
  133. async function _connectDevice(device, bussType) {
  134. console.log('准备连接设备')
  135. const deviceName = device.name
  136. activeCode.deviceName = deviceName
  137. activeCode.bussType = bussType
  138. console.log('设备名称:' + deviceName)
  139. if (!device || !device.name) {
  140. return {
  141. code: 251,
  142. msg: "设备名称为空"
  143. }
  144. }
  145. _conFigCurrentCallBack(200, deviceName)
  146. const res = await connectAndInit(device, () => {
  147. if (!_isDeviceEnd) {
  148. if (activeCode.isNeedShowLoad) {
  149. console.log('连接断开隐藏加载框')
  150. wx.hideLoading()
  151. }
  152. console.log('设备连接断开')
  153. typeof _disConnectCallback === 'function' && _disConnectCallback()
  154. }
  155. })
  156. if (res.code == 0) {
  157. if (bussType == 1) {
  158. return await _configIssueApply()
  159. } else {
  160. return await _obuSystemInfo()
  161. }
  162. }
  163. _conFigCurrentCallBack(250, deviceName)
  164. res.code = 250
  165. return res
  166. }
  167. /// -------------------- OBU相关
  168. /// -------------------- OBU信息读取相关
  169. /**
  170. * 获取系统信息并进行处理
  171. *
  172. * @param type 类型,默认为0
  173. * @returns 返回处理结果
  174. */
  175. async function _obuSystemInfo(type = 0) {
  176. _conFigCurrentCallBack(300)
  177. const res = await getObuSystemInfo()
  178. console.log('获取系统信息结果:' + jsonToString(res))
  179. if (res.code == 0) {
  180. console.log('获取系统信息成功')
  181. const sytem = res.data
  182. if (sytem) {
  183. console.log('OBU系统信息:' + sytem)
  184. const contractType = sytem.substr(16, 2)
  185. const contractVersion = sytem.substr(18, 2)
  186. const serialNumber = sytem.substr(20, 16)
  187. const star = sytem.substr(36, 8)
  188. const end = sytem.substr(44, 8)
  189. const activeState = sytem.substr(52, 2)
  190. const lssuer = sytem.substr(8, 8)
  191. activeCode.contractType = contractType
  192. activeCode.contractVersion = contractVersion
  193. activeCode.serialNumber = serialNumber
  194. activeCode.startTime = star
  195. activeCode.expireTime = end
  196. activeCode.acState = activeState == '01' ? true : false
  197. activeCode.lssuer = lssuer
  198. console.log('协约类型' + contractType)
  199. console.log('合同版本' + contractVersion)
  200. console.log('合同序列号' + serialNumber)
  201. console.log('开始时间' + star)
  202. console.log('结束时间' + end)
  203. console.log('激活状态' + activeState)
  204. console.log('发行方' + lssuer)
  205. if (type == 0) {
  206. if (
  207. activeCode.bussType == 2 ||
  208. activeCode.bussType == 11 ||
  209. activeCode.bussType == 4 ||
  210. activeCode.bussType == 12
  211. ) {
  212. return _iccFile15Info()
  213. } else if (activeCode.bussType == 1) {
  214. return _configIssueApply()
  215. }
  216. } else {
  217. if (activeCode.bussType == 1) {
  218. if (activeCode.acState == true) {
  219. return _iccFile15Info(1)
  220. } else {
  221. return _configFailCallBack({
  222. msg: '标签位激活失败',
  223. })
  224. }
  225. } else if (activeCode.bussType == 4) {
  226. if (activeCode.acState == true) {
  227. activeCode.vehiclePlate = activeCode.cardPlateNumber
  228. activeCode.vehiclePlateColor = activeCode.cardPlateColor
  229. return _againObuActiveSuccess()
  230. } else {
  231. return {
  232. code: -1,
  233. msg: '重新激活失败',
  234. }
  235. }
  236. } else if (activeCode.bussType == 2) {
  237. return _configSuccessCallBack('卡签检测成功')
  238. }
  239. }
  240. }
  241. }
  242. _conFigCurrentCallBack(350)
  243. return res
  244. }
  245. /// -------------------- OBU信息写入相关
  246. /// -------------------- ICC相关
  247. async function _iccFile15Info(type = 0) {
  248. _conFigCurrentCallBack(304)
  249. const res = await getIccFile15()
  250. if (res.code == 0) {
  251. let data = res.data
  252. console.log('读取CPU 0015文件:' + data)
  253. let sytem = data
  254. /// 卡片类型
  255. let cardType = sytem.substr(16, 2)
  256. /// 卡片版本号
  257. let cardVersion = sytem.substr(18, 2)
  258. /// 卡片网络编号 + 用户卡片内部编号
  259. let cardNumber = sytem.substr(20, 20)
  260. activeCode.cardVersionHex = cardVersion;
  261. activeCode.cardVersion = hex2int(cardVersion)
  262. activeCode.cardType = hex2int(cardType)
  263. activeCode.faceCardNum = cardNumber
  264. /// 卡片内部编号
  265. activeCode.cardUid = sytem.substr(24, 16)
  266. console.log('卡片类型' + activeCode.cardType)
  267. console.log('卡号' + activeCode.faceCardNum)
  268. console.log('卡版本' + activeCode.cardVersion)
  269. if (sytem.length > 84) {
  270. /// 启用时间
  271. let cardStar = sytem.substring(40, 48)
  272. /// 到期时间
  273. let cardEnd = sytem.substring(48, 56)
  274. /// 车牌号码
  275. let plateNo = sytem.substring(56, 80)
  276. /// 车牌颜色
  277. let plateColor = hex2int(sytem.substring(82, 84))
  278. if (
  279. plateNo == '000000000000000000000000' ||
  280. plateNo == 'ffffffffffffffffffffffff' ||
  281. plateNo == 'FFFFFFFFFFFFFFFFFFFFFFFF'
  282. ) {
  283. console.log('车牌号为空')
  284. activeCode.cardPlateNumber = '未知车牌'
  285. activeCode.cardPlateColor = '-1'
  286. activeCode.vehiclePlate = '未知车牌'
  287. activeCode.vehiclePlateColor = '-1'
  288. } else {
  289. activeCode.cardPlateCode = plateNo
  290. activeCode.cardPlateColor = plateColor
  291. activeCode.cardPlateNumber = decode(plateNo)
  292. }
  293. activeCode.plateNumber = activeCode.cardPlateNumber
  294. activeCode.cardStar = cardStar
  295. activeCode.cardEnd = cardEnd
  296. activeCode.cardVehicleType = hex2int(sytem.substring(84, 86))
  297. console.log('卡开始时间' + cardStar)
  298. console.log('卡结束时间' + cardEnd)
  299. console.log('车牌号十六进制' + plateNo)
  300. console.log('车牌颜色' + plateColor)
  301. console.log('车型' + activeCode.cardVehicleType)
  302. }
  303. if (activeCode.bussType == 1) {
  304. if (type == 0) {
  305. return _cardIssueCheck()
  306. } else {
  307. return _configSuccessCallBack('激活成功')
  308. }
  309. } else if (activeCode.bussType == 2 || activeCode.bussType == 12) {
  310. if (activeCode.cardPlateNumber == '未知车牌' && activeCode.bussType == 12) {
  311. activeCode.plateNumber = '未知车牌'
  312. activeCode.vehiclePlate = '未知车牌'
  313. activeCode.vehiclePlateColor = '-1'
  314. activeCode.catInfo = {
  315. licensePlate: '未知车牌',
  316. }
  317. return _configSuccessCallBack('检测成功')
  318. } else {
  319. if (activeCode.bussType == 2) {
  320. if (activeCode.cardPlateNumber == '未知车牌') {
  321. return _configSuccessCallBack('检测成功')
  322. }
  323. return _obuVehicleInfo()
  324. } else if (activeCode.bussType == 12) {
  325. activeCode.vehiclePlate = activeCode.cardPlateNumber
  326. activeCode.vehiclePlateColor = activeCode.cardPlateColor
  327. return _configSuccessCallBack('检测成功')
  328. }
  329. }
  330. } else if (activeCode.bussType == 11) {
  331. return _configSuccessCallBack('校验信息获取成功')
  332. } else if (activeCode.bussType == 4) {
  333. if (activeCode.cardPlateNumber == '未知车牌') {
  334. return _configFailCallBack({
  335. msg: '设备未激活,请先激活设备',
  336. })
  337. } else {
  338. return _againObuIssueCheck()
  339. }
  340. }
  341. } else {
  342. _conFigCurrentCallBack(354)
  343. }
  344. return res
  345. }
  346. /// -------------------- 网络相关 ---------------------- ///
  347. // 新版激活接口
  348. async function _instApply() {
  349. _conFigCurrentCallBack(750, "正在激活,请等待")
  350. let params = {}
  351. params.orderNo = activeCode.params.orderNo;
  352. params.cardId = activeCode.faceCardNum;
  353. params.obuId = activeCode.serialNumber;
  354. params.cardVersion = activeCode.cardVersionHex
  355. params.obuVersion = activeCode.contractVersion
  356. const res = await network.etc.instApply(
  357. params.orderNo,
  358. params.cardId,
  359. params.obuId,
  360. params.cardVersion,
  361. params.obuVersion,
  362. );
  363. console.log(res)
  364. if (res.code == 0) {
  365. return _configIssueNetWorkData(res.data)
  366. } else {
  367. return _configFailCallBack({
  368. msg: (res.message || '发行指令申请失败,请稍后重试') + res.code
  369. })
  370. }
  371. }
  372. async function _instCallBack(cmdResult) {
  373. const res = await network.etc.instCallBack(
  374. issueResult.transOrderId,
  375. issueResult.cmd,
  376. cmdResult,
  377. issueResult.stepNo,
  378. )
  379. if (res.code == 0) {
  380. return _configIssueNetWorkData(res.data)
  381. } else {
  382. return _configFailCallBack({
  383. msg: (res.message || '发行指令回传失败,请稍后重试') + res.code
  384. })
  385. }
  386. }
  387. // 老版本
  388. /// 配置发行指令申请
  389. /// issueType - 指令类型 1-卡 2-签
  390. async function _configIssueApply() {
  391. return await _instApply()
  392. // const params = {
  393. // ...activeCode.params,
  394. // };
  395. // _conFigCurrentCallBack(750, "正在激活,请等待")
  396. // const res = await apiIssueApply(params)
  397. // console.log(res)
  398. // if (res.code == 0) {
  399. // return _configIssueNetWorkData(res.data)
  400. // } else {
  401. // return _configFailCallBack({
  402. // msg: (res.message || '发行指令申请失败,请稍后重试') + res.code
  403. // })
  404. // }
  405. }
  406. /// 配置发行指令回传
  407. async function _configIssueCallback(cmdResult) {
  408. return await _instCallBack(cmdResult);
  409. // const params = {
  410. // openId: activeCode.params.openId,
  411. // transOrderId: issueResult.transOrderId,
  412. // cmd: issueResult.cmd,
  413. // cmdResult,
  414. // stepNo: issueResult.stepNo,
  415. // };
  416. // const res = await apiIssueCallback(params)
  417. // if (res.code == 0) {
  418. // return _configIssueNetWorkData(res.data)
  419. // } else {
  420. // return _configFailCallBack({
  421. // msg: (res.message || '发行指令回传失败,请稍后重试') + res.code
  422. // })
  423. // }
  424. }
  425. async function _configIssueNetWorkData(data) {
  426. issueResult = data;
  427. const cmdArr = data.cmd.split(',')
  428. const cmdType = data.cmdType == "CARD" ? '10' : '20'
  429. if (issueResult.stepNo == 100) {
  430. return _obuSystemInfo(1)
  431. } else {
  432. const res = await transCmd(cmdArr, cmdType)
  433. if (res.code == 0) {
  434. return await _configIssueCallback(res.cmdResult)
  435. } else {
  436. return _configFailCallBack({
  437. msg: res.msg || "激活失败"
  438. })
  439. }
  440. }
  441. }
  442. /**
  443. * 配置成功回调函数
  444. *
  445. * @param msg 成功消息内容
  446. * @returns 配置成功后的返回对象
  447. */
  448. function _configSuccessCallBack(msg) {
  449. _conFigCurrentCallBack(500)
  450. return {
  451. code: 0,
  452. data: activeCode,
  453. msg: msg,
  454. }
  455. }
  456. /**
  457. * 处理网络请求失败的情况
  458. *
  459. * @param data 任意类型的数据,表示请求返回的数据
  460. * @param msg 错误信息字符串
  461. * @returns 返回包含错误码、数据和错误信息的对象
  462. */
  463. function _configFailCallBack({
  464. data = activeCode,
  465. msg = '网络请求失败',
  466. code = -1
  467. } = {}) {
  468. _conFigCurrentCallBack(550, msg)
  469. return {
  470. code,
  471. data: data,
  472. msg: msg,
  473. }
  474. }
  475. /**
  476. * 回调方法
  477. */
  478. function _conFigCurrentCallBack(code, name) {
  479. const {
  480. type,
  481. msg
  482. } = configBlueStateMsg(code, name)
  483. if (type == 1) {
  484. typeof _checkStatusCallback === 'function' && _checkStatusCallback(msg)
  485. if (activeCode.isNeedShowLoad) {
  486. wx.showLoading({
  487. title: msg,
  488. })
  489. }
  490. } else {
  491. _isDeviceEnd = true
  492. if (activeCode.isNeedDisConnect) {
  493. console.log("_conFigCurrentCallBack")
  494. console.log("执行断开连接设备")
  495. DisConnectDevice()
  496. }
  497. if (activeCode.isNeedShowLoad && activeCode.isNeedDisConnect) {
  498. wx.hideLoading()
  499. }
  500. }
  501. return type
  502. }
  503. /**
  504. * 配置普通激活码的属性
  505. */
  506. function _configNormalActiveCode() {
  507. _isDeviceEnd = false
  508. activeCode.params = {
  509. OrderId: '',
  510. PlateNumber: '',
  511. }
  512. /// 卡片版本号
  513. activeCode.cardVersion = ''
  514. // 卡片类型
  515. activeCode.cardType = ''
  516. /// 卡片里 车型 1 客车、2 货车
  517. activeCode.cardVehicleType = '1'
  518. /// 合同类型
  519. activeCode.contractType = ''
  520. /// 合同版本
  521. activeCode.contractVersion = ''
  522. /// 卡表面号, 网络编号的20位表面号
  523. activeCode.faceCardNum = ''
  524. /// 卡表面号,不带网络编号的16位表面号
  525. activeCode.cardUid = ''
  526. /// 车牌号编码
  527. activeCode.vehiclePlateCode = ''
  528. /// 车牌号
  529. activeCode.vehiclePlate = ''
  530. /// 车牌号
  531. activeCode.plateNumber = ''
  532. /// OBU 车型 1 客车、2 货车
  533. activeCode.vehicleType = '1'
  534. /// 车牌颜色
  535. activeCode.vehiclePlateColor = '0'
  536. /// 卡片车牌号码
  537. activeCode.cardPlateNumber = ''
  538. /// 卡片车牌颜色
  539. activeCode.cardPlateColor = '-1'
  540. /// 办理状态
  541. /// 0、开始发行
  542. /// 10、上传图片成功、
  543. /// 20、写Obu成功
  544. /// 30、写卡成功
  545. /// 40、激活成功
  546. /// 50、二次已激活
  547. activeCode.issueState = 10
  548. /// OBU序列号,16位合同序列号
  549. activeCode.serialNumber = ''
  550. /// 服务供应商
  551. activeCode.supplier = '贵州'
  552. /// 订单号
  553. activeCode.OrderNo = ''
  554. /// 开始时间
  555. activeCode.startTime = ''
  556. /// 结束时间
  557. activeCode.expireTime = ''
  558. /// 业务类型 1 激活 4 二次激活 2 卡签检测
  559. activeCode.bussType = 1
  560. /// 激活状态
  561. activeCode.acState = false
  562. /// obu mac 地址
  563. activeCode.obuId = 'FFFFFFFF'
  564. /// 发行方标识
  565. activeCode.lssuer = ''
  566. /// 卡片激活开始时间
  567. activeCode.cardStar = ''
  568. /// 卡片激活结束时间
  569. activeCode.cardEnd = ''
  570. /// 电量
  571. activeCode.electricity = '40'
  572. /// true 天线关闭 false 天线打开
  573. activeCode.equipmentState = false
  574. /// 是否支持 打开 和 关闭 天线
  575. activeCode.isShowEquipment = false
  576. activeCode.content = ''
  577. /// 换卡换签 网络返回数据
  578. activeCode.netData = {}
  579. /// 车辆解密信息
  580. activeCode.catInfo = {
  581. licensePlate: '未知车牌',
  582. }
  583. /// 1 换卡 2 换签 3 全套
  584. activeCode.afterType = '1'
  585. /// 设备名字
  586. activeCode.deviceName = ''
  587. /// 0 支持 1 不支持
  588. activeCode.supportLock = '0'
  589. /// 回调
  590. activeCode.callBack = ''
  591. /// 是否显示默认加载 动画
  592. activeCode.isNeedShowLoad = true
  593. /// 是否需要校验设备名字
  594. activeCode.isNeedCheckName = true
  595. activeCode.isNeedDisConnect = false
  596. }