You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

4106 lines
157 KiB

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
  1. import sm4 from "./sm4";
  2. import md5 from "./md5.min";
  3. import crc from "./crc-index";
  4. var userIdSaved;
  5. var authUserIdSaved;
  6. var deviceNameSaved;
  7. var pwdSaved;
  8. var indexSaved;
  9. var fingerNoSaved;
  10. var startTimeSaved;
  11. var endTimeSaved;
  12. var useCountLimitSaved;
  13. var staticCallback;
  14. var keyIdSaved;
  15. var signKeySaved;
  16. var roleSaved;
  17. var pwdNoSaved;
  18. var actionSaved;
  19. var alarmFingerSaved;
  20. var encKeySaved;
  21. var openModeSaved;
  22. var blueKeyIdSaved;
  23. const constString = "uiYMMSwerw122@^#&@87";
  24. var receDataSaved = "";
  25. var fileVersionSaved;
  26. var fileSizeSaved;
  27. var fileCRCSaved;
  28. var tokenSaved;
  29. var packageSize;
  30. var blueKey;
  31. export default class ICINLocker {
  32. constructor(obj) {
  33. this.callback = obj;
  34. }
  35. static checkTheWechatVersion() {
  36. return new Promise(function (resolve, reject) {
  37. wx.openBluetoothAdapter({
  38. success: function (res) {
  39. wx.closeBluetoothAdapter({
  40. success: (res) => {
  41. resolve({
  42. status: 0,
  43. statusReason: "成功"
  44. })
  45. },
  46. fail: (res) => {
  47. reject({
  48. status: 1,
  49. statusReason: res.errMsg
  50. })
  51. }
  52. })
  53. },
  54. fail: function (res) {
  55. reject({
  56. status: 1,
  57. statusReason: res.errMsg
  58. })
  59. }
  60. })
  61. });
  62. }
  63. static disConnect(lockId) {
  64. // let connectedDeviceId = wx.getStorageSync('connectedDeviceId')
  65. return new Promise(function (resolve, reject) {
  66. wx.closeBLEConnection({
  67. deviceId: lockId,
  68. success: function (res) {
  69. console.log("断开连接成功");
  70. resolve({
  71. status: 0,
  72. statusReason: "成功"
  73. })
  74. },
  75. fail: function (res) {
  76. reject({
  77. stauts: 1,
  78. statusReason: res.errMsg
  79. })
  80. }
  81. });
  82. })
  83. }
  84. static showLoading(msg) {
  85. wx.showLoading({
  86. title: msg,
  87. });
  88. }
  89. static getLocationPermission() {
  90. return new Promise(function (resolve, reject) {
  91. wx.getLocation({
  92. type: "wgs84",
  93. success(res) {
  94. resolve({
  95. status: 0,
  96. statusReason: "获取地理位置成功",
  97. });
  98. console.log(res);
  99. },
  100. fail(res) {
  101. reject({
  102. status: 1,
  103. statusReason: res.errMsg,
  104. });
  105. return;
  106. },
  107. });
  108. });
  109. }
  110. /**
  111. * 扫描周围蓝牙设备中的名称为deviceName的设备
  112. * @param {*} deviceName
  113. */
  114. static scanDevices(deviceName) {
  115. return new Promise(function (resolve, reject) {
  116. wx.openBluetoothAdapter({
  117. success: function (res) {
  118. let deviceId;
  119. /****** 超时计时器 */
  120. let timeoutInterval;
  121. /****** 搜寻列表计时器 */
  122. let intervalForScanedList;
  123. /****** 记录扫描的具体位置 */
  124. let devicesScanedPos = 0
  125. console.log("初始化蓝牙适配器成功");
  126. // 设置计时器 10s直接超时
  127. timeoutInterval = setInterval(function () {
  128. wx.closeBluetoothAdapter({
  129. success: (res) => {
  130. ICINLocker.cleanDevicesScanner(timeoutInterval, intervalForScanedList);
  131. reject({
  132. status: 1,
  133. statusReason: "超时搜索蓝牙设备,请靠近设备后再试"
  134. })
  135. return;
  136. },
  137. })
  138. }, 10000)
  139. // 本机蓝牙适配器状态
  140. wx.getBluetoothAdapterState({
  141. success: function (res) {
  142. console.log("搜索状态" + res.discovering);
  143. console.log("可用状态" + res.available);
  144. },
  145. });
  146. // 如上一次与这一次的蓝牙连接目标相同,不用继续搜寻直接返回之前的connectedDeviceId
  147. // let imei = wx.getStorageSync("deviceName");
  148. // let connectedDeviceId = wx.getStorageSync("connectedDeviceId");
  149. // if (imei == deviceName) {
  150. // if (connectedDeviceId) {
  151. // ICINLocker.disConnect(connectedDeviceId);
  152. // ICINLocker.cleanDevicesScanner(timeoutInterval, intervalForScanedList, connectedDeviceId, deviceName);
  153. // resolve({
  154. // status: 0,
  155. // deviceId: connectedDeviceId
  156. // });
  157. // return;
  158. // }
  159. // }
  160. //开始搜索设备 start----
  161. wx.startBluetoothDevicesDiscovery({
  162. allowDuplicatesKey: false,
  163. success: function (res) {
  164. // 设置定时器 0.5s一次查看所有搜索出来的设备
  165. intervalForScanedList = setInterval(function () {
  166. wx.getBluetoothDevices({
  167. success: function (res) {
  168. // 搜不到设备直接返回
  169. if (res.devices.length == 0) {
  170. console.log("未搜索到蓝牙设备,请稍后再试");
  171. ICINLocker.cleanDevicesScanner(timeoutInterval, intervalForScanedList);
  172. reject({
  173. status: 1,
  174. statusReason: "未搜索到蓝牙设备,请靠近设备后再试",
  175. });
  176. return;
  177. }
  178. /***** 单个发现新设备,如有设备直接返回 */
  179. wx.onBluetoothDeviceFound(function (res) {
  180. if(res.devices[0].name == null){
  181. return;
  182. }
  183. let ondeviceNameOfitem = res.devices[0].name;
  184. if (ondeviceNameOfitem === deviceName) {
  185. deviceId = res.devices[0].deviceId;
  186. ICINLocker.cleanDevicesScanner(timeoutInterval, intervalForScanedList, deviceId, deviceName);
  187. resolve({
  188. status: 0,
  189. deviceId: deviceId,
  190. });
  191. return;
  192. }
  193. });
  194. /***** 在搜索到的设备列表中查询需要连接的设备 */
  195. for (; devicesScanedPos <= res.devices.length; devicesScanedPos++) {
  196. console.log(res.devices, "210210");
  197. if (res.devices[devicesScanedPos].name == null) {
  198. return;
  199. }
  200. let deviceNameOfitem = res.devices[devicesScanedPos].name;
  201. if (deviceNameOfitem === deviceName) {
  202. deviceId = res.devices[devicesScanedPos].deviceId;
  203. ICINLocker.cleanDevicesScanner(timeoutInterval, intervalForScanedList, deviceId, deviceName);
  204. resolve({
  205. status: 0,
  206. deviceId: deviceId,
  207. });
  208. return;
  209. }
  210. }
  211. },
  212. });
  213. }, 500)
  214. },
  215. });
  216. },
  217. fail: function (res) {
  218. console.log("调用失败,请打开蓝牙");
  219. ICINLocker.cleanDevicesScanner();
  220. reject({
  221. status: 1,
  222. statusReason: res,
  223. });
  224. return;
  225. },
  226. });
  227. });
  228. }
  229. /**
  230. * 扫描周围蓝牙设备
  231. */
  232. static searchDevices() {
  233. return new Promise(function (resolve, reject) {
  234. const self = this;
  235. wx.openBluetoothAdapter({
  236. success: function (res) {
  237. // 本机蓝牙适配器状态
  238. wx.getBluetoothAdapterState({
  239. success: function (res) {
  240. console.log("搜索状态" + res.discovering);
  241. console.log("可用状态" + res.available);
  242. },
  243. });
  244. //开始搜索设备 start----
  245. wx.startBluetoothDevicesDiscovery({
  246. allowDuplicatesKey: false,
  247. success: function (res) {
  248. //获取所有已发现的设备 start
  249. setTimeout(function () {
  250. wx.getBluetoothDevices({
  251. success: function (res) {
  252. //是否有已连接设备
  253. wx.getConnectedBluetoothDevices({
  254. success: function (res) {
  255. console.log("被连接的设备" + JSON.stringify(res.devices));
  256. },
  257. fail: function (res) {
  258. console.log("无连接设备");
  259. },
  260. });
  261. console.log(
  262. "获取到周边搜到的设备信息:" + JSON.stringify(res.devices)
  263. );
  264. if (res.devices.length == 0) {
  265. reject({
  266. status: 1,
  267. statusReason: "未搜索到蓝牙设备,请靠近设备后再试",
  268. });
  269. } else {
  270. const deviceList = res.devices.filter(
  271. (item) => item.connectable === true
  272. );
  273. const reg = /^BLEKEY/;
  274. let filterDevice = deviceList.filter(
  275. (item) => reg.test(item.name) === true
  276. );
  277. if (filterDevice.length === 0) {
  278. reject({
  279. status: 1,
  280. statusReason: "未搜索到蓝牙设备,请靠近设备后再试",
  281. });
  282. }
  283. console.log(filterDevice)
  284. resolve({
  285. status: 0,
  286. deviceList: filterDevice
  287. })
  288. }
  289. }
  290. });
  291. }, 2000);
  292. },
  293. });
  294. },
  295. fail: function (res) {
  296. reject({
  297. status: 1,
  298. statusReason: res,
  299. });
  300. },
  301. });
  302. })
  303. }
  304. /***** 清理设备扫描器 */
  305. static cleanDevicesScanner(_timeoutInterval, _intervalForScanedList, _deviceId, _deviceName) {
  306. if (_timeoutInterval) {
  307. clearInterval(_timeoutInterval);
  308. }
  309. if (_intervalForScanedList) {
  310. clearInterval(_intervalForScanedList);
  311. }
  312. if (_deviceName) {
  313. wx.setStorageSync("deviceName", _deviceName);
  314. }
  315. if (_deviceId) {
  316. wx.setStorageSync("connectedDeviceId", _deviceId);
  317. }
  318. ICINLocker.stopBluetoothDevicesDiscovery();
  319. return;
  320. }
  321. /**
  322. * 连接设备
  323. * @param {设备ID 从scanDevice方法的回调获取} deviceId
  324. * @param {*} isBlue
  325. */
  326. static connect(deviceId, isBlue) {
  327. return new Promise(function (resolve, reject) {
  328. var primaryServiceId = "";
  329. var connectedDeviceId = deviceId;
  330. wx.setStorage({
  331. data: connectedDeviceId,
  332. key: "connectedDeviceId",
  333. });
  334. wx.createBLEConnection({
  335. deviceId: connectedDeviceId,
  336. timeout: 5000,
  337. success: function (res) {
  338. wx.getBLEDeviceServices({
  339. deviceId: connectedDeviceId,
  340. success: function (res) {
  341. for (let i = 0; i < res.services.length; i++) {
  342. let primaryUUid = res.services[i].uuid.substring(0, 8);
  343. let uuid;
  344. if (isBlue) {
  345. //蓝牙扣的
  346. uuid = "F000C0E0";
  347. packageSize = 40
  348. blueKey = true
  349. } else {
  350. //锁的 FFF0 联通FE70
  351. uuid = "0000FFF0";
  352. packageSize = 360
  353. }
  354. if (res.services[i].isPrimary && primaryUUid === uuid) {
  355. primaryServiceId = res.services[i].uuid;
  356. }
  357. }
  358. wx.getBLEDeviceCharacteristics({
  359. deviceId: connectedDeviceId,
  360. serviceId: primaryServiceId,
  361. success: function (res) {
  362. for (var i = 0; i < res.characteristics.length; i++) {
  363. if (res.characteristics[i].properties.notify) {
  364. console.log(
  365. "获取开启notify的ServicweId:",
  366. primaryServiceId
  367. );
  368. console.log(
  369. "获取开启notify的CharacteristicsId:",
  370. res.characteristics[i].uuid
  371. );
  372. try {
  373. wx.setStorageSync("notifyServicweId", primaryServiceId);
  374. wx.setStorageSync(
  375. "notifyCharacteristicsId",
  376. res.characteristics[i].uuid
  377. );
  378. } catch (e) {
  379. console.log("notifyCharacteristicsId get wrong");
  380. }
  381. }
  382. if (res.characteristics[i].properties.write) {
  383. console.log(
  384. "获取开启write的ServicweId:",
  385. primaryServiceId
  386. );
  387. console.log(
  388. "获取开启write的CharacteristicsId:",
  389. res.characteristics[i].uuid
  390. );
  391. try {
  392. wx.setStorageSync("writeServicweId", primaryServiceId);
  393. if (isBlue) {
  394. wx.setStorageSync(
  395. "writeCharacteristicsId",
  396. res.characteristics[i].uuid
  397. );
  398. } else {
  399. wx.setStorageSync(
  400. "writeCharacteristicsId",
  401. res.characteristics[1].uuid
  402. );
  403. }
  404. } catch (e) {
  405. console.log("writeCharacteristicsId get wrong");
  406. }
  407. }
  408. if (res.characteristics[i].properties.read) {
  409. console.log("读取函数ServiceId:", primaryServiceId);
  410. console.log(
  411. "读取函数CharacteristicsId:",
  412. res.characteristics[i].uuid
  413. );
  414. try {
  415. wx.setStorageSync("readServicweId", primaryServiceId);
  416. wx.setStorageSync(
  417. "readCharacteristicsId",
  418. res.characteristics[i].uuid
  419. );
  420. } catch (e) {
  421. console.log("readCharacteristicsId get wrong");
  422. }
  423. }
  424. }
  425. console.log(
  426. "device getBLEDeviceCharacteristics:",
  427. res.characteristics
  428. );
  429. var notifyServicweId = wx
  430. .getStorageSync("notifyServicweId")
  431. .toUpperCase();
  432. var notifyCharacteristicsId = wx
  433. .getStorageSync("notifyCharacteristicsId")
  434. .toUpperCase();
  435. console.log("启用notify的serviceId", notifyServicweId);
  436. console.log(
  437. "启用notify的notifyCharacteristicsId",
  438. notifyCharacteristicsId
  439. );
  440. wx.notifyBLECharacteristicValueChange({
  441. state: true,
  442. type: "notification",
  443. deviceId: connectedDeviceId,
  444. serviceId: primaryServiceId,
  445. characteristicId: notifyCharacteristicsId,
  446. success: function (res) {
  447. console.log(
  448. "启用通知notifyBLECharacteristicValueChange success",
  449. res.errMsg
  450. );
  451. setTimeout(function () {
  452. ICINLocker.onBLECharacteristicValueChange();
  453. }, 1000);
  454. resolve({
  455. status: 0,
  456. mac: connectedDeviceId,
  457. uuid: primaryServiceId,
  458. statusReason: "成功连接",
  459. });
  460. },
  461. fail: function (res) {
  462. console.log("未能打开监听");
  463. console.log("notifyServicweId" + notifyServicweId);
  464. console.log(
  465. "notifyCharacteristicsId" + notifyCharacteristicsId
  466. );
  467. reject({
  468. status: 1,
  469. statusReason: "未能打开监听",
  470. });
  471. },
  472. });
  473. },
  474. fail: function (res) {
  475. reject({
  476. status: 1,
  477. statusReason: res.errMsg,
  478. });
  479. },
  480. complete: function () {
  481. console.log("complete");
  482. },
  483. });
  484. },
  485. });
  486. },
  487. fail: function (res) {
  488. console.log(res);
  489. if (res.errCode == -1) { //已连接 断开蓝牙连接
  490. // ICINLocker.disConnect(connectedDeviceId)
  491. reject({
  492. status: 1,
  493. statusReason: res.errMsg,
  494. });
  495. } else if (res.errCode == 10013 || res.errCode == 10002) {
  496. wx.removeStorageSync('connectedDeviceId');
  497. wx.removeStorageSync('deviceName');
  498. reject({
  499. status: 1,
  500. statusReason: res.errMsg,
  501. });
  502. } else if (res.errCode == 10012 || res.errCode == 10003) {
  503. console.log('蓝牙连接问题 10012,10003');
  504. reject({
  505. status: 1,
  506. statusReason: res.errMsg,
  507. });
  508. } else {
  509. reject({
  510. status: 1,
  511. statusReason: res.errMsg,
  512. });
  513. }
  514. },
  515. complete: function () {
  516. console.log("调用结束");
  517. },
  518. });
  519. console.log(connectedDeviceId);
  520. });
  521. }
  522. static onBLECharacteristicValueChange() {
  523. wx.onBLECharacteristicValueChange(function (res) {
  524. console.log(res.value);
  525. console.log("接收到:" + ab2hex(res.value));
  526. console.log("接收到长度:" + ab2hex(res.value).length);
  527. // 获取多包
  528. let tempData = "" + ab2hex(res.value);
  529. receDataSaved += tempData;
  530. let dataLength = (parseInt(receDataSaved.substring(16, 20), 16) + 14) * 2;
  531. if (receDataSaved.length < dataLength) {
  532. return;
  533. }
  534. let receData = "";
  535. let resultData = receDataSaved;
  536. let ciper = resultData.substring(15, 16);
  537. console.log("ciper的值为" + ciper);
  538. if (ciper == "3" || ciper == "2") {
  539. let encData = resultData.substring(24, resultData.length - 4);
  540. let decData = byte2HexStr(
  541. sm4.sm4_dec(
  542. hexStr2Byte(encData),
  543. encData.length / 2,
  544. hexStr2Byte(encKeySaved)
  545. )
  546. );
  547. receData =
  548. resultData.substring(0, 24) +
  549. decData +
  550. resultData.substring(resultData.length - 4, resultData.length);
  551. } else {
  552. receData = resultData;
  553. }
  554. console.log("receData为:" + receData);
  555. console.log("receData长度为:" + receData.length);
  556. if (receData == "") {
  557. return;
  558. }
  559. wx.setStorage({
  560. data: receData,
  561. key: "receData",
  562. });
  563. let cmdId = receData.substring(24, 28);
  564. let cmdStatus = receData.substring(28, 30);
  565. let signKey = "";
  566. let commonKey = "";
  567. switch (cmdId) {
  568. case "300a":
  569. if (cmdStatus == "00") {
  570. let softWareVersion =
  571. hex2int(receData.substring(30, 32)) +
  572. "." +
  573. hex2int(receData.substring(32, 34)) +
  574. "." +
  575. hex2int(receData.substring(34, 36)) +
  576. "." +
  577. hex2int(receData.substring(36, 38));
  578. let appUserCount = parseInt(receData.substring(124, 128), 16);
  579. let remainCount = parseInt(receData.substring(132, 136), 16);
  580. let notUploadCount = parseInt(receData.substring(136, 140), 16);
  581. let pwdCount = parseInt(receData.substring(140, 142), 16);
  582. let lockStatus = receData.substring(154, 156);
  583. let lockerMode = hexToString(receData.substring(184, 200));
  584. let power = parseInt(receData.substring(38, 40), 16);
  585. let nbStat = receData.substring(40, 42);
  586. let nbSignal = parseInt(receData.substring(42, 44), 16);
  587. let nbIMEI = hexToString(receData.substring(44, 84));
  588. let nvIMSI = hexToString(receData.substring(84, 124));
  589. let blackListCount = parseInt(receData.substring(128, 130), 16);
  590. let bleKeyCount = parseInt(receData.substring(130, 132), 16);
  591. let fingerPrintsCount = parseInt(receData.substring(142, 144), 16);
  592. let RFCount = parseInt(receData.substring(144, 146), 16);
  593. let lockTime = timeConvert(
  594. parseInt(receData.substring(146, 154), 16),
  595. 1
  596. );
  597. let lockSn = hexToString(receData.substring(200, 240));
  598. let wifiSsid = hexToString(receData.substring(240, 304));
  599. let serverIp = hexToString(receData.substring(304, 344));
  600. let serverPort = hexToString(receData.substring(344, 356));
  601. let isNbLock = parseInt(hex2int(receData.substring(30, 32)))
  602. .toString(2)
  603. .PadLeft(8, "0")
  604. .substring(3, 4);
  605. let isWifiLock = parseInt(hex2int(receData.substring(30, 32)))
  606. .toString(2)
  607. .PadLeft(8, "0")
  608. .substring(2, 3);
  609. staticCallback({
  610. cmdId: cmdId,
  611. status: cmdStatus,
  612. softWareVersion: softWareVersion,
  613. appUserCount: appUserCount,
  614. remainCount: remainCount,
  615. notUploadCount: notUploadCount,
  616. pwdCount: pwdCount,
  617. lockStatus: lockStatus,
  618. lockerMode: lockerMode,
  619. power: power,
  620. nbStat: nbStat,
  621. nbSignal: nbSignal,
  622. nbIMEI: nbIMEI,
  623. nvIMSI: nvIMSI,
  624. blackListCount: blackListCount,
  625. bleKeyCount: bleKeyCount,
  626. fingerPrintsCount: fingerPrintsCount,
  627. lockTime: lockTime,
  628. RFCount: RFCount,
  629. lockSn: lockSn,
  630. wifiSsid: wifiSsid,
  631. serverIp: serverIp,
  632. serverPort: serverPort,
  633. isNbLock: isNbLock,
  634. isWifiLock: isWifiLock,
  635. });
  636. } else {
  637. staticCallback({
  638. cmdId: cmdId,
  639. status: cmdStatus,
  640. });
  641. }
  642. break;
  643. case "3092":
  644. if (cmdStatus == "00") {
  645. signKey = receData.substring(62, 94);
  646. commonKey = receData.substring(30, 62);
  647. try {
  648. wx.setStorageSync("authUserSignKey", signKey);
  649. wx.setStorageSync("commonKey", commonKey);
  650. signKeySaved = signKey;
  651. } catch (e) {}
  652. staticCallback({
  653. cmdId: cmdId,
  654. status: cmdStatus,
  655. signKey: signKey,
  656. commonKey: commonKey,
  657. });
  658. } else {
  659. staticCallback({
  660. cmdId: cmdId,
  661. status: cmdStatus,
  662. });
  663. }
  664. break;
  665. case "3093":
  666. if (cmdStatus == "00") {
  667. commonKey = receData.substring(30, 62);
  668. wx.setStorageSync("commonKey", commonKey);
  669. staticCallback({
  670. cmdId: cmdId,
  671. status: cmdStatus,
  672. commonKey: commonKey,
  673. });
  674. } else {
  675. staticCallback({
  676. cmdId: cmdId,
  677. status: cmdStatus,
  678. });
  679. }
  680. break;
  681. case "3094":
  682. if (cmdStatus == "00") {
  683. let userSignKey = receData.substring(62, 94);
  684. let userCommonKey = receData.substring(30, 62);
  685. wx.setStorageSync("userSignKey", userSignKey);
  686. wx.setStorageSync("userCommonKey", userCommonKey);
  687. staticCallback({
  688. cmdId: cmdId,
  689. status: cmdStatus,
  690. userSignKey: userSignKey,
  691. userCommonKey: userCommonKey,
  692. });
  693. } else {
  694. staticCallback({
  695. cmdId: cmdId,
  696. status: cmdStatus,
  697. });
  698. }
  699. break;
  700. case "3095":
  701. let dataLength = parseInt(receData.substring(16, 24));
  702. console.log("3095截取本次的数据长度是" + dataLength);
  703. let nbIotResp = receData.substring(28, 28 + dataLength);
  704. staticCallback({
  705. cmdId: cmdId,
  706. nbIotResp: nbIotResp
  707. })
  708. break;
  709. case "3001":
  710. let addUserToken = receData.substring(108, 116);
  711. let status3001 = receData.substring(116, 118);
  712. console.log("cmdId" + cmdId + ", status3001" + status3001);
  713. if (status3001 == "06") {
  714. // signKey = wx.getStorageSync('authUserSignKey');
  715. if (signKeySaved) {
  716. ICINLocker.addUserToLock(
  717. deviceNameSaved,
  718. authUserIdSaved,
  719. userIdSaved,
  720. keyIdSaved,
  721. roleSaved,
  722. signKeySaved,
  723. addUserToken,
  724. encKeySaved,
  725. startTimeSaved,
  726. endTimeSaved,
  727. staticCallback
  728. );
  729. } else {
  730. staticCallback({
  731. cmdId: cmdId,
  732. status: status3001,
  733. });
  734. }
  735. } else if (status3001 == "00") {
  736. console.log("添加用户成功");
  737. staticCallback({
  738. cmdId: cmdId,
  739. status: status3001,
  740. });
  741. } else {
  742. staticCallback({
  743. cmdId: cmdId,
  744. status: status3001,
  745. });
  746. }
  747. break;
  748. case "3005":
  749. let tokenOfOpenTheDoor = receData.substring(28, 36);
  750. let status3005 = receData.substring(36, 38);
  751. if (status3005 == "06") {
  752. ICINLocker.openTheDoor(
  753. deviceNameSaved,
  754. userIdSaved,
  755. signKeySaved,
  756. keyIdSaved,
  757. tokenOfOpenTheDoor,
  758. openModeSaved,
  759. encKeySaved,
  760. staticCallback
  761. );
  762. } else if (status3005 == "00") {
  763. staticCallback({
  764. cmdId: cmdId,
  765. status: status3005,
  766. });
  767. } else {
  768. staticCallback({
  769. cmdId: cmdId,
  770. status: status3005,
  771. });
  772. }
  773. break;
  774. case "3004":
  775. let tokenOfResetLock = receData.substring(28, 36);
  776. let status3004 = receData.substring(36, 38);
  777. if (status3004 == "06") {
  778. // signKey = wx.getStorageSync('authUserSignKey');
  779. ICINLocker.resetLock(
  780. deviceNameSaved,
  781. userIdSaved,
  782. tokenOfResetLock,
  783. signKeySaved,
  784. encKeySaved,
  785. staticCallback
  786. );
  787. } else if (status3004 == "00") {
  788. staticCallback({
  789. cmdId: cmdId,
  790. status: status3004,
  791. });
  792. } else {
  793. staticCallback({
  794. cmdId: cmdId,
  795. status: status3004,
  796. });
  797. }
  798. break;
  799. case "3002":
  800. let tokenOfDelUser = receData.substring(28, 36);
  801. let status3002 = receData.substring(36, 38);
  802. if (status3002 == "06") {
  803. wx.setStorageSync("tokenOfDelUser", tokenOfDelUser);
  804. ICINLocker.deleteUser(
  805. deviceNameSaved,
  806. authUserIdSaved,
  807. userIdSaved,
  808. keyIdSaved,
  809. tokenOfDelUser,
  810. signKeySaved,
  811. encKeySaved,
  812. staticCallback
  813. );
  814. } else if (status3002 == "00") {
  815. staticCallback({
  816. cmdId: cmdId,
  817. status: status3002,
  818. });
  819. } else {
  820. staticCallback({
  821. cmdId: cmdId,
  822. status: status3002,
  823. });
  824. }
  825. break;
  826. case "3008":
  827. let status3008 = receData.substring(108, 110);
  828. if (status3008 == "00") {
  829. let lockRecordTotal = receData.substring(110, 114);
  830. let lockRecordOpenStatus = receData.substring(114, 118);
  831. let lockRecordCount = receData.substring(118, 122);
  832. let lockRecordLen = receData.substring(122, 126);
  833. let forLoop = receData.substring(126, receData.length);
  834. console.log("循环体为-----" + forLoop);
  835. let lockRecordList = [];
  836. for (let i = 1; i <= parseInt(lockRecordLen, 16) * 2; i++) {
  837. if (i % 54 == 0) {
  838. let tempRecords = forLoop.substring(i - 54, i);
  839. console.log(tempRecords);
  840. /**
  841. * 0x1F防撬报警;0x2F胁迫指纹报警;0x3X试错报警低4位代表具体开锁方式);0x4F预留重置报警
  842. * 开锁方式:0机械钥匙;1指纹;2密码;3app;4蓝扣;5远程;6RF卡;7人脸;8虹膜9临时授权21常开22落锁;
  843. */
  844. let data = {
  845. userId: hexToString(tempRecords.substring(0, 40)),
  846. openMode: tempRecords.substring(40, 42),
  847. openTime: timeConvert(
  848. parseInt(tempRecords.substring(42, 50), 16),
  849. 1
  850. ),
  851. uploadFlag: tempRecords.substring(50, 52),
  852. power: parseInt(tempRecords.substring(52, 54), 16),
  853. };
  854. lockRecordList.push(data);
  855. }
  856. }
  857. staticCallback({
  858. cmdId: cmdId,
  859. status: status3008,
  860. lockRecordLen: parseInt(lockRecordLen, 16) * 2,
  861. lockRecordTotal: parseInt(lockRecordTotal, 16),
  862. lockRecordList: lockRecordList,
  863. });
  864. } else {
  865. staticCallback({
  866. cmdId: cmdId,
  867. status: status3008,
  868. });
  869. }
  870. break;
  871. case "3009":
  872. let status3009 = receData.substring(28, 30);
  873. staticCallback({
  874. cmdId: cmdId,
  875. status: status3009,
  876. });
  877. break;
  878. case "3010":
  879. let status3010 = receData.substring(28, 30);
  880. let blueKeyId = receData.substring(30, 70);
  881. staticCallback({
  882. cmdId: cmdId,
  883. status: status3010,
  884. blueKeyId: blueKeyId,
  885. });
  886. break;
  887. case "3011":
  888. let tokenOfBlueKey = receData.substring(28, 36);
  889. let status3011 = receData.substring(36, 38);
  890. if (status3011 == "06") {
  891. ICINLocker.registeBluetoothBuckle(
  892. authUserIdSaved,
  893. blueKeyIdSaved,
  894. keyIdSaved,
  895. pwdSaved,
  896. tokenOfBlueKey,
  897. signKeySaved,
  898. encKeySaved,
  899. staticCallback
  900. );
  901. } else {
  902. staticCallback({
  903. status: status3011,
  904. });
  905. }
  906. break;
  907. case "300b":
  908. let tokenOf300b = receData.substring(28, 36);
  909. let status300b = receData.substring(36, 38);
  910. if (status300b == "06") {
  911. ICINLocker.updateFirmware(
  912. deviceNameSaved,
  913. authUserIdSaved,
  914. fileVersionSaved,
  915. fileSizeSaved,
  916. fileCRCSaved,
  917. tokenOf300b,
  918. signKeySaved,
  919. encKeySaved,
  920. staticCallback
  921. );
  922. } else {
  923. staticCallback({
  924. token: tokenOf300b,
  925. status: status300b,
  926. });
  927. }
  928. break;
  929. case "300c":
  930. let status300c = receData.substring(28, 30);
  931. staticCallback({
  932. status: status300c,
  933. });
  934. break;
  935. case "3012":
  936. let status3012 = receData.substring(36, 38);
  937. staticCallback({
  938. cmdId: cmdId,
  939. status: status3012,
  940. });
  941. break;
  942. case "3007":
  943. break;
  944. case "3030":
  945. let subCmdId = receData.substring(30, 32);
  946. let status3030 = receData.substring(28, 30);
  947. switch (subCmdId) {
  948. case "01":
  949. staticCallback({
  950. cmdId: cmdId,
  951. status: status3030,
  952. subCmdId: subCmdId,
  953. });
  954. break;
  955. case "02":
  956. if (status3030 == "00") {
  957. let IMEI = receData.substring(34, 74);
  958. let IMSI = receData.substring(74, 114);
  959. staticCallback({
  960. cmdId: cmdId,
  961. status: status3030,
  962. subCmdId: subCmdId,
  963. IMEI: IMEI,
  964. IMSI: IMSI,
  965. });
  966. } else {
  967. staticCallback({
  968. cmdId: cmdId,
  969. status: status3030,
  970. subCmdId: subCmdId,
  971. });
  972. }
  973. break;
  974. case "03":
  975. let tokenOfHandlePwd = receData.substring(34, 42);
  976. if (status3030 == "06") {
  977. ICINLocker.handlePwd(
  978. deviceNameSaved,
  979. authUserIdSaved,
  980. userIdSaved,
  981. pwdSaved,
  982. pwdNoSaved,
  983. keyIdSaved,
  984. signKeySaved,
  985. tokenOfHandlePwd,
  986. startTimeSaved,
  987. endTimeSaved,
  988. useCountLimitSaved,
  989. encKeySaved,
  990. staticCallback
  991. );
  992. } else if (status3030 == "00") {
  993. staticCallback({
  994. cmdId: cmdId,
  995. status: status3030,
  996. subCmdId: subCmdId,
  997. });
  998. } else {
  999. staticCallback({
  1000. cmdId: cmdId,
  1001. status: status3030,
  1002. subCmdId: subCmdId,
  1003. });
  1004. }
  1005. break;
  1006. case "05":
  1007. let pwdCount = receData.substring(34, 36);
  1008. let pwdAllInfo = receData.substring(36, receData.length);
  1009. let pwdLength = parseInt(pwdCount, 16) * 4;
  1010. console.log(pwdAllInfo);
  1011. let pwdList = [];
  1012. for (let i = 1; i <= pwdLength; i++) {
  1013. if (i % 4 == 0) {
  1014. let tempCard = pwdAllInfo.substring(i - 4, i);
  1015. let data = {
  1016. pwdNo: tempCard.substring(0, 2),
  1017. useCountLimit: tempCard.substring(2, 4),
  1018. };
  1019. pwdList.push(data);
  1020. }
  1021. }
  1022. staticCallback({
  1023. cmdId: cmdId,
  1024. status: status3030,
  1025. subCmdId: subCmdId,
  1026. pwdList: pwdList,
  1027. });
  1028. break;
  1029. case "09":
  1030. let tokenOfFinger = receData.substring(34, 42);
  1031. let completeFlag = receData.substring(46, 48);
  1032. let fingerprintNo = receData.substring(48, 52);
  1033. if (status3030 == "06") {
  1034. ICINLocker.addFingerPrints(
  1035. deviceNameSaved,
  1036. authUserIdSaved,
  1037. userIdSaved,
  1038. keyIdSaved,
  1039. tokenOfFinger,
  1040. signKeySaved,
  1041. startTimeSaved,
  1042. endTimeSaved,
  1043. alarmFingerSaved,
  1044. encKeySaved,
  1045. staticCallback
  1046. );
  1047. } else if (completeFlag == "01" && status3030 == "00") {
  1048. staticCallback({
  1049. cmdId: cmdId,
  1050. status: status3030,
  1051. subCmdId: subCmdId,
  1052. fingerprintNo: fingerprintNo,
  1053. completeFlag: completeFlag,
  1054. });
  1055. } else {
  1056. staticCallback({
  1057. cmdId: cmdId,
  1058. status: status3030,
  1059. subCmdId: subCmdId,
  1060. completeFlag: completeFlag,
  1061. });
  1062. }
  1063. break;
  1064. case "0a":
  1065. let fingerprintTotalCnt = receData.substring(34, 36);
  1066. let currCnt = receData.substring(36, 38);
  1067. let fingerprintAllInfo = receData.substring(38, receData.length);
  1068. let fingerprintLength = parseInt(fingerprintTotalCnt, 16) * 62;
  1069. console.log(fingerprintAllInfo);
  1070. console.log("length---" + fingerprintAllInfo.length);
  1071. let fingerprintList = [];
  1072. for (let i = 1; i <= fingerprintLength; i++) {
  1073. if (i % 62 == 0) {
  1074. let tempFingerPrints = fingerprintAllInfo.substring(
  1075. i - 62,
  1076. i
  1077. );
  1078. console.log(tempFingerPrints);
  1079. let data = {
  1080. fingerprintNo: tempFingerPrints.substring(0, 4),
  1081. belongsTo: tempFingerPrints.substring(4, 44),
  1082. isAlarmFinger: tempFingerPrints.substring(44, 46),
  1083. startTime: tempFingerPrints.substring(46, 54),
  1084. endTime: tempFingerPrints.substring(54, 62),
  1085. };
  1086. fingerprintList.push(data);
  1087. }
  1088. }
  1089. staticCallback({
  1090. status: status3030,
  1091. subCmdId: subCmdId,
  1092. fingerprintList: fingerprintList,
  1093. fingerprintTotalCnt: fingerprintTotalCnt,
  1094. currCnt: currCnt,
  1095. });
  1096. break;
  1097. case "10":
  1098. let rfCardTotalCnt = receData.substring(34, 36);
  1099. let rfCardAllInfo = receData.substring(36, receData.length);
  1100. let rfCardlength = parseInt(rfCardTotalCnt, 16) * 18;
  1101. console.log(rfCardAllInfo);
  1102. let rfCardList = [];
  1103. for (let i = 1; i <= rfCardlength; i++) {
  1104. if (i % 18 == 0) {
  1105. let tempCard = rfCardAllInfo.substring(i - 18, i);
  1106. let data = {
  1107. cardIndex: tempCard.substring(0, 2),
  1108. startTime: tempCard.substring(2, 10),
  1109. endTime: tempCard.substring(10, 18),
  1110. };
  1111. rfCardList.push(data);
  1112. }
  1113. }
  1114. staticCallback({
  1115. status: status3030,
  1116. subCmdId: subCmdId,
  1117. rfCardList: rfCardList,
  1118. totalCnt: rfCardTotalCnt,
  1119. });
  1120. break;
  1121. case "0f":
  1122. let tokenOfRfCard = receData.substring(34, 42);
  1123. if (status3030 == "06") {
  1124. ICINLocker.RfCardRegister(
  1125. deviceNameSaved,
  1126. authUserIdSaved,
  1127. userIdSaved,
  1128. keyIdSaved,
  1129. indexSaved,
  1130. tokenOfRfCard,
  1131. signKeySaved,
  1132. startTimeSaved,
  1133. endTimeSaved,
  1134. encKeySaved,
  1135. staticCallback
  1136. );
  1137. } else if (status3030 == "00") {
  1138. let cardNo = receData.substring(44, 84);
  1139. /**
  1140. * 第21-22位如果为010203为非安全卡其他则是安全卡
  1141. */
  1142. let safeCardFlag = cardNo.substring(20, 22);
  1143. // console.log('cardNo------------' + cardNo);
  1144. // console.log('safeCardFlag______' + safeCardFlag);
  1145. if (
  1146. safeCardFlag == "01" ||
  1147. safeCardFlag == "02" ||
  1148. safeCardFlag == "03"
  1149. ) {
  1150. ICINLocker.rFcardAddControl(
  1151. authUserIdSaved,
  1152. userIdSaved,
  1153. keyIdSaved,
  1154. indexSaved,
  1155. signKeySaved,
  1156. cardNo,
  1157. "01",
  1158. encKeySaved,
  1159. staticCallback
  1160. );
  1161. } else {
  1162. let completeFlag = receData.substring(42, 44);
  1163. staticCallback({
  1164. cmdId: cmdId,
  1165. status: status3030,
  1166. subCmdId: subCmdId,
  1167. cardNo: cardNo,
  1168. completeFlag: completeFlag,
  1169. });
  1170. }
  1171. } else {
  1172. staticCallback({
  1173. cmdId: cmdId,
  1174. status: status3030,
  1175. subCmdId: subCmdId,
  1176. });
  1177. }
  1178. break;
  1179. case "0b":
  1180. let tokenOfModifyFinger = receData.substring(34, 42);
  1181. if (status3030 == "06") {
  1182. ICINLocker.modifyFingerPrints(
  1183. deviceNameSaved,
  1184. authUserIdSaved,
  1185. userIdSaved,
  1186. keyIdSaved,
  1187. actionSaved,
  1188. fingerNoSaved,
  1189. tokenOfModifyFinger,
  1190. signKeySaved,
  1191. alarmFingerSaved,
  1192. startTimeSaved,
  1193. endTimeSaved,
  1194. encKeySaved,
  1195. staticCallback
  1196. );
  1197. } else if (status3030 == "00") {
  1198. staticCallback({
  1199. cmdId: cmdId,
  1200. status: status3030,
  1201. subCmdId: subCmdId,
  1202. });
  1203. } else {
  1204. staticCallback({
  1205. cmdId: cmdId,
  1206. status: status3030,
  1207. subCmdId: subCmdId,
  1208. });
  1209. }
  1210. break;
  1211. case "11":
  1212. if (status3030 == "06") {
  1213. let tokenOfModifyFinger = receData.substring(34, 42);
  1214. ICINLocker.modifyRFCard(
  1215. deviceNameSaved,
  1216. authUserIdSaved,
  1217. userIdSaved,
  1218. keyIdSaved,
  1219. indexSaved,
  1220. tokenOfModifyFinger,
  1221. actionSaved,
  1222. signKeySaved,
  1223. startTimeSaved,
  1224. endTimeSaved,
  1225. encKeySaved,
  1226. staticCallback
  1227. );
  1228. } else {
  1229. staticCallback({
  1230. cmdId: cmdId,
  1231. status: status3030,
  1232. subCmdId: subCmdId,
  1233. });
  1234. }
  1235. break;
  1236. case "12":
  1237. if (status3030 == "00") {
  1238. let tokenOfRFControl = receData.substring(34, 42);
  1239. let completeFlag = receData.substring(42, 44);
  1240. let cardNo = receData.substring(44, 84);
  1241. staticCallback({
  1242. cmdId: cmdId,
  1243. status: status3030,
  1244. subCmdId: subCmdId,
  1245. tokenOfRFControl: tokenOfRFControl,
  1246. completeFlag: completeFlag,
  1247. cardNo: cardNo,
  1248. });
  1249. }
  1250. break;
  1251. case "13":
  1252. staticCallback({
  1253. cmdId: cmdId,
  1254. status: status3030,
  1255. subCmdId: subCmdId,
  1256. });
  1257. break;
  1258. case "15":
  1259. let privateKey = receData.substring(34, 66);
  1260. staticCallback({
  1261. status: status3030,
  1262. privateKey: privateKey,
  1263. });
  1264. case "0c":
  1265. staticCallback({
  1266. status: status3030,
  1267. });
  1268. break;
  1269. case "14":
  1270. let completeFlagOfFiveKeys = receData.substring(34, 36);
  1271. staticCallback({
  1272. status: status3030,
  1273. completeFlag: completeFlagOfFiveKeys,
  1274. });
  1275. break;
  1276. case '17':
  1277. console.log('进17了')
  1278. let rfCmdId = receData.substring(34, 36);
  1279. let completeFlagOfBanOrLiftCard = receData.substring(36, 38);
  1280. staticCallback({
  1281. cmdId: cmdId,
  1282. status: status3030,
  1283. subCmdId: subCmdId,
  1284. rfCmdId: rfCmdId,
  1285. completeFlag: completeFlagOfBanOrLiftCard
  1286. })
  1287. break;
  1288. }
  1289. break;
  1290. }
  1291. receDataSaved = "";
  1292. });
  1293. }
  1294. static stopBluetoothDevicesDiscovery() {
  1295. wx.stopBluetoothDevicesDiscovery({
  1296. success: function (res) {
  1297. console.log("停止搜索设备" + JSON.stringify(res.errMsg));
  1298. },
  1299. });
  1300. }
  1301. /**
  1302. * *读取锁信息
  1303. * @param {门锁ID 最多40字节} _lockId
  1304. * @param {用户ID 最多20字节} usrID
  1305. * @param {回调} callback
  1306. */
  1307. static readLockerInfo(_lockId, usrID, callback) {
  1308. console.log("读取锁信息", [...arguments]);
  1309. deviceNameSaved = _lockId;
  1310. let head = makeHeader("300a");
  1311. let cmdID = "300a";
  1312. let lockerID = str2Hex(_lockId).PadRight(80, "0");
  1313. let userID = str2Hex(usrID).PadRight(40, "0");
  1314. let dataModular = (cmdID + lockerID + userID).replace("0x", "");
  1315. let dataModularLength = (dataModular.length / 2)
  1316. .toString(16)
  1317. .PadLeft(4, "0");
  1318. let templateResult = (
  1319. head +
  1320. dataModularLength +
  1321. dataModularLength +
  1322. dataModular
  1323. ).replace("0x", "");
  1324. let resultData = templateResult;
  1325. let crcData = crc
  1326. .crc16kermit(hex2Ab(resultData))
  1327. .toString(16)
  1328. .PadLeft(4, "0");
  1329. resultData += crcData;
  1330. console.log("读取锁信息发送的数据(300a)" + JSON.stringify(resultData));
  1331. sendDataTolock(resultData);
  1332. staticCallback = callback;
  1333. }
  1334. /**
  1335. * 加密读取锁信息
  1336. * @param {*} _lockId
  1337. * @param {*} usrID
  1338. * @param {*} callback
  1339. */
  1340. static readLockerInfoEnckey(_lockId, usrID, callback) {
  1341. console.log("读取锁信息", [...arguments]);
  1342. deviceNameSaved = _lockId;
  1343. let lockID4AuthCode = str2Hex(_lockId);
  1344. encKeySaved = lockID4AuthCode;
  1345. let head = makeHeader("300a");
  1346. let cmdID = "300a";
  1347. let lockerID = str2Hex(_lockId).PadRight(80, "0");
  1348. let userID = str2Hex(usrID).PadRight(40, "0");
  1349. let dataModular = (cmdID + lockerID + userID);
  1350. let dataModularLength = (dataModular.length / 2).toString(16).PadLeft(4, "0");
  1351. let dataModularSM4 = sm4.sm4_enc(
  1352. hexStr2Byte(dataModular),
  1353. dataModular.length / 2,
  1354. hexStr2Byte(lockID4AuthCode)
  1355. );
  1356. let dataModularSM4HexString = byte2HexStr(dataModularSM4);
  1357. let dataModularSM4Length = (dataModularSM4HexString.length / 2)
  1358. .toString(16)
  1359. .PadLeft(4, "0");
  1360. let resultData =
  1361. head + dataModularSM4Length + dataModularLength + dataModularSM4HexString;
  1362. console.log(
  1363. "加密前:" + head + dataModularSM4Length + dataModularLength + dataModular
  1364. );
  1365. let crcData = crc
  1366. .crc16kermit(hex2Ab(resultData))
  1367. .toString(16)
  1368. .PadLeft(4, "0");
  1369. resultData += crcData;
  1370. console.log("读取锁信息发送的数据(300a)" + JSON.stringify(resultData));
  1371. sendDataTolock(resultData);
  1372. staticCallback = callback;
  1373. }
  1374. /**
  1375. * *获取超管鉴权秘钥
  1376. * @param {门锁ID 最多40字节} _lockId
  1377. * @param {用户ID 最多20字节} usrID
  1378. * @param {钥匙ID 最多40字节} _keyId
  1379. * @param {回调} callback
  1380. */
  1381. static getSignKey(_lockId, usrID, _keyId, callback) {
  1382. deviceNameSaved = _lockId;
  1383. authUserIdSaved = usrID;
  1384. keyIdSaved = _keyId;
  1385. let head = makeHeader("3092");
  1386. let cmdId = "3092";
  1387. let lockID4AuthCode = str2Hex(_lockId);
  1388. encKeySaved = lockID4AuthCode;
  1389. let keyID4AuthCode = str2Hex(_keyId);
  1390. let authuserID4AuthCode = str2Hex(usrID);
  1391. let tempNowTime = new Date().getTime() / 1000;
  1392. let nowTime = parseInt(new Date().getTime() / 1000).toString(16);
  1393. let lockID = lockID4AuthCode.PadRight(80, "0");
  1394. let keyID = keyID4AuthCode.PadRight(80, "0");
  1395. let authUserID = authuserID4AuthCode.PadRight(40, "0");
  1396. let authCode = ab2hex(
  1397. md5.digest(
  1398. hexStr2Byte(
  1399. authuserID4AuthCode + keyID4AuthCode + nowTime + lockID4AuthCode
  1400. )
  1401. )
  1402. );
  1403. let authCodeLen = (authCode.length / 2).toString(16).PadLeft(2, "0");
  1404. let dataModular =
  1405. cmdId + lockID + keyID + authUserID + nowTime + authCodeLen + authCode;
  1406. let dataModularLength = (dataModular.length / 2)
  1407. .toString(16)
  1408. .PadLeft(4, "0");
  1409. let dataModularSM4 = sm4.sm4_enc(
  1410. hexStr2Byte(dataModular),
  1411. dataModular.length / 2,
  1412. hexStr2Byte(lockID4AuthCode)
  1413. );
  1414. let dataModularSM4HexString = byte2HexStr(dataModularSM4);
  1415. let dataModularSM4Length = (dataModularSM4HexString.length / 2)
  1416. .toString(16)
  1417. .PadLeft(4, "0");
  1418. let resultData =
  1419. head + dataModularSM4Length + dataModularLength + dataModularSM4HexString;
  1420. console.log(
  1421. "加密前:" + head + dataModularSM4Length + dataModularLength + dataModular
  1422. );
  1423. let crcData = crc
  1424. .crc16kermit(hexStr2Byte(resultData))
  1425. .toString(16)
  1426. .PadLeft(4, "0");
  1427. resultData += crcData;
  1428. sendDataTolock(resultData);
  1429. staticCallback = callback;
  1430. }
  1431. /**
  1432. * *协商秘钥
  1433. * @param {门锁ID 最多40字节} _lockId
  1434. * @param {用户ID 最多20字节} _usrID
  1435. * @param {钥匙ID 最多40字节} _keyId
  1436. * @param {用户的signKey} _userSignKey
  1437. * @param {回调函数} callback
  1438. */
  1439. static getCommonKey(_lockId, _usrID, _keyId, _userSignKey, callback) {
  1440. console.log("获取commonkey", [...arguments]);
  1441. userIdSaved = _usrID;
  1442. keyIdSaved = _keyId;
  1443. signKeySaved = _userSignKey;
  1444. let _signKey = _userSignKey;
  1445. let head = makeHeader("3093");
  1446. let cmdId = "3093";
  1447. let lockID4AuthCode = str2Hex(_lockId);
  1448. encKeySaved = lockID4AuthCode;
  1449. let keyID4AuthCode = str2Hex(_keyId);
  1450. let authuserID4AuthCode = str2Hex(_usrID);
  1451. let nowTime = parseInt(new Date().getTime() / 1000).toString(16);
  1452. let lockID = lockID4AuthCode.PadRight(80, "0");
  1453. let keyID = keyID4AuthCode.PadRight(80, "0");
  1454. let authUserID = authuserID4AuthCode.PadRight(40, "0");
  1455. let authCode = ab2hex(
  1456. md5.digest(
  1457. hexStr2Byte(authuserID4AuthCode + keyID4AuthCode + nowTime + _signKey)
  1458. )
  1459. );
  1460. let authCodeLen = (authCode.length / 2).toString(16).PadLeft(2, "0");
  1461. let dataModular =
  1462. cmdId + lockID + keyID + authUserID + nowTime + authCodeLen + authCode;
  1463. console.log(dataModular);
  1464. let dataModularLength = (dataModular.length / 2)
  1465. .toString(16)
  1466. .PadLeft(4, "0");
  1467. let dataModularSM4 = sm4.sm4_enc(
  1468. hexStr2Byte(dataModular),
  1469. dataModular.length / 2,
  1470. hexStr2Byte(lockID4AuthCode)
  1471. );
  1472. let dataModularSM4HexString = byte2HexStr(dataModularSM4);
  1473. let dataModularSM4Length = (dataModularSM4HexString.length / 2)
  1474. .toString(16)
  1475. .PadLeft(4, "0");
  1476. let resultData =
  1477. head + dataModularSM4Length + dataModularLength + dataModularSM4HexString;
  1478. console.log(
  1479. "包头信息 +++++++++++" + head + dataModularSM4Length + dataModularLength
  1480. );
  1481. let crcData = crc
  1482. .crc16kermit(hexStr2Byte(resultData))
  1483. .toString(16)
  1484. .PadLeft(4, "0");
  1485. resultData += crcData;
  1486. sendDataTolock(resultData);
  1487. staticCallback = callback;
  1488. }
  1489. /**
  1490. * *获取用户signkey
  1491. * @param {门锁ID 最多40字节} _lockId
  1492. * @param {超管ID} _superAdminId
  1493. * @param {用户ID 最多20字节} _usrID
  1494. * @param {超管的keyId 最多40字节} _keyId
  1495. * @param {超管的signkey 或者 authKey} _authUserSignKeyOrAuthKey
  1496. * @param {传入数据类型 String 01: 传入了authUserSignKey 02传入了authKey} _accessType
  1497. * @param {回调} callback
  1498. */
  1499. static getUserSignKey(
  1500. _lockId,
  1501. _superAdminId,
  1502. _userID,
  1503. _keyId,
  1504. _authUserSignKeyOrAuthKey,
  1505. _accessType,
  1506. callback
  1507. ) {
  1508. authUserIdSaved = _superAdminId;
  1509. userIdSaved = _userID;
  1510. keyIdSaved = _keyId;
  1511. let head = makeHeader("3094");
  1512. let cmdId = "3094";
  1513. let lockID4AuthCode = str2Hex(_lockId);
  1514. encKeySaved = lockID4AuthCode;
  1515. let keyID4AuthCode = str2Hex(_keyId);
  1516. let authuserID4AuthCode = str2Hex(_superAdminId);
  1517. let userID4AuthCode = str2Hex(_userID);
  1518. let nowTime = parseInt(new Date().getTime() / 1000).toString(16);
  1519. let lockID = lockID4AuthCode.PadRight(80, "0");
  1520. let keyID = keyID4AuthCode.PadRight(80, "0");
  1521. let authUserID = authuserID4AuthCode.PadRight(40, "0");
  1522. let userID = userID4AuthCode.PadRight(40, "0");
  1523. let authCode = "";
  1524. switch (_accessType) {
  1525. case "01":
  1526. authCode = ab2hex(
  1527. md5.digest(
  1528. hex2Ab(
  1529. authuserID4AuthCode +
  1530. keyID4AuthCode +
  1531. userID4AuthCode +
  1532. _authUserSignKeyOrAuthKey
  1533. )
  1534. )
  1535. );
  1536. break;
  1537. case "02":
  1538. authCode = _authUserSignKeyOrAuthKey;
  1539. break;
  1540. default:
  1541. break;
  1542. }
  1543. let authCodeLen = (authCode.length / 2).toString(16).PadLeft(2, "0");
  1544. let dataModular =
  1545. cmdId +
  1546. lockID +
  1547. authUserID +
  1548. keyID +
  1549. userID +
  1550. nowTime +
  1551. authCodeLen +
  1552. authCode;
  1553. console.log(dataModular);
  1554. let dataModularLength = (dataModular.length / 2)
  1555. .toString(16)
  1556. .PadLeft(4, "0");
  1557. let dataModularSM4 = sm4.sm4_enc(
  1558. hexStr2Byte(dataModular),
  1559. dataModular.length / 2,
  1560. hexStr2Byte(lockID4AuthCode)
  1561. );
  1562. let dataModularSM4HexString = byte2HexStr(dataModularSM4);
  1563. let dataModularSM4Length = (dataModularSM4HexString.length / 2)
  1564. .toString(16)
  1565. .PadLeft(4, "0");
  1566. let resultData =
  1567. head + dataModularSM4Length + dataModularLength + dataModularSM4HexString;
  1568. console.log(
  1569. "包头信息 +++++++++++" +
  1570. head +
  1571. dataModularSM4Length +
  1572. dataModularLength +
  1573. dataModular
  1574. );
  1575. let crcData = crc
  1576. .crc16kermit(hex2Ab(resultData))
  1577. .toString(16)
  1578. .PadLeft(4, "0");
  1579. resultData += crcData;
  1580. sendDataTolock(resultData);
  1581. staticCallback = callback;
  1582. }
  1583. /**
  1584. * *添加用户
  1585. * @param {锁ID} _lockId
  1586. * @param {超管ID} _superAdminId
  1587. * @param {添加的用户ID} usrId blueName
  1588. * @param {超管的keyId} _keyId
  1589. * @param {00普通用户 01管理员 FF超级管理员} _role
  1590. * @param {superAdmin的signkey} _signKey
  1591. * @param {传入空字符串即可} _token
  1592. * @param {通讯秘钥,getCommonKey命令可以获取} _commonKey
  1593. * @param {设置开始时间-8位timestamp字符串,eg:'00000000'} _startTime
  1594. * @param {设置结束时间-8位timestamp字符串,eg:'00000000'} _endTime
  1595. * @param {回调} callback
  1596. */
  1597. static addUserToLock(
  1598. _lockId,
  1599. _superAdminId,
  1600. _usrId,
  1601. _keyId,
  1602. _role,
  1603. _signKey,
  1604. _token,
  1605. _commonKey,
  1606. _startTime,
  1607. _endTime,
  1608. callback
  1609. ) {
  1610. console.log('添加用户参数', [...arguments])
  1611. deviceNameSaved = _lockId;
  1612. authUserIdSaved = _superAdminId;
  1613. userIdSaved = _usrId;
  1614. keyIdSaved = _keyId;
  1615. signKeySaved = _signKey;
  1616. roleSaved = _role;
  1617. encKeySaved = _commonKey;
  1618. startTimeSaved = _startTime;
  1619. endTimeSaved = _endTime;
  1620. let signKey = _signKey;
  1621. let head = makeHeader("3001");
  1622. let cmdId = "3001";
  1623. let lockId4AuthCode = str2Hex(_lockId);
  1624. let lockId = lockId4AuthCode.PadRight(80, "0");
  1625. let authuserID4AuthCode = str2Hex(_superAdminId);
  1626. let authUserID = authuserID4AuthCode.PadRight(40, "0");
  1627. let keyID4AuthCode = str2Hex(_keyId);
  1628. let keyID = keyID4AuthCode.PadRight(80, "0");
  1629. let userId = str2Hex(_usrId).PadRight(40, "0");
  1630. let openMode
  1631. if (blueKey) { //蓝牙扣
  1632. openMode = '04'
  1633. } else { //正常用户
  1634. openMode = "03";
  1635. }
  1636. let keyType = "00";
  1637. let startDate = _startTime;
  1638. let expireDate = _endTime;
  1639. var passWord = "".PadRight(40, "0");
  1640. var token = "".PadRight(8, "0");
  1641. var authCode = "";
  1642. var authCodeLen = "00";
  1643. if (_token != "") {
  1644. token = _token;
  1645. authCode = ab2hex(
  1646. md5.digest(
  1647. hexStr2Byte(authuserID4AuthCode + keyID4AuthCode + token + signKey)
  1648. )
  1649. );
  1650. authCodeLen = (authCode.length / 2).toString(16).PadLeft(2, "0");
  1651. }
  1652. let dataModular =
  1653. cmdId +
  1654. lockId +
  1655. authUserID +
  1656. keyID +
  1657. userId +
  1658. openMode +
  1659. keyType +
  1660. startDate +
  1661. expireDate +
  1662. _role +
  1663. passWord +
  1664. token +
  1665. authCodeLen +
  1666. authCode;
  1667. let dataModularLength = (dataModular.length / 2)
  1668. .toString(16)
  1669. .PadLeft(4, "0");
  1670. let dataModularSM4 = sm4.sm4_enc(
  1671. hexStr2Byte(dataModular),
  1672. dataModular.length / 2,
  1673. hexStr2Byte(_commonKey)
  1674. );
  1675. let dataModularSM4HexString = byte2HexStr(dataModularSM4);
  1676. console.log(
  1677. "dataModularSM4String---------------------" + dataModularSM4HexString
  1678. );
  1679. let dataModularSM4Length = (dataModularSM4HexString.length / 2)
  1680. .toString(16)
  1681. .PadLeft(4, "0");
  1682. let resultData =
  1683. head + dataModularSM4Length + dataModularLength + dataModularSM4HexString;
  1684. console.log(
  1685. "加密前:" + head + dataModularSM4Length + dataModularLength + dataModular
  1686. );
  1687. let crcData = crc
  1688. .crc16kermit(hexStr2Byte(resultData))
  1689. .toString(16)
  1690. .PadLeft(4, "0");
  1691. resultData += crcData;
  1692. sendDataTolock(resultData);
  1693. staticCallback = callback;
  1694. }
  1695. static addBlueKeyToLock(
  1696. _lockId,
  1697. _superAdminId,
  1698. _usrId,
  1699. _keyId,
  1700. _role,
  1701. _signKey,
  1702. _token,
  1703. _commonKey,
  1704. _startTime,
  1705. _endTime,
  1706. callback
  1707. ) {
  1708. console.log('添加用户参数', [...arguments])
  1709. deviceNameSaved = _lockId;
  1710. authUserIdSaved = _superAdminId;
  1711. userIdSaved = _usrId;
  1712. keyIdSaved = _keyId;
  1713. signKeySaved = _signKey;
  1714. roleSaved = _role;
  1715. encKeySaved = _commonKey;
  1716. startTimeSaved = _startTime;
  1717. endTimeSaved = _endTime;
  1718. let signKey = _signKey;
  1719. let head = makeHeader("3001");
  1720. let cmdId = "3001";
  1721. let lockId4AuthCode = str2Hex(_lockId);
  1722. let lockId = lockId4AuthCode.PadRight(80, "0");
  1723. let authuserID4AuthCode = str2Hex(_superAdminId);
  1724. let authUserID = authuserID4AuthCode.PadRight(40, "0");
  1725. let keyID4AuthCode = str2Hex(_keyId);
  1726. let keyID = keyID4AuthCode.PadRight(80, "0");
  1727. let userId = str2Hex(_usrId).PadRight(40, "0");
  1728. let openMode
  1729. if (blueKey) { //蓝牙扣
  1730. openMode = '04'
  1731. } else { //正常用户
  1732. openMode = "03";
  1733. }
  1734. let keyType = "00";
  1735. let startDate = _startTime;
  1736. let expireDate = _endTime;
  1737. var passWord = "".PadRight(40, "0");
  1738. var token = "".PadRight(8, "0");
  1739. var authCode = "";
  1740. var authCodeLen = "00";
  1741. if (_token != "") {
  1742. token = _token;
  1743. authCode = ab2hex(
  1744. md5.digest(
  1745. hexStr2Byte(authuserID4AuthCode + keyID4AuthCode + token + signKey)
  1746. )
  1747. );
  1748. authCodeLen = (authCode.length / 2).toString(16).PadLeft(2, "0");
  1749. }
  1750. let dataModular =
  1751. cmdId +
  1752. lockId +
  1753. authUserID +
  1754. keyID +
  1755. userId +
  1756. openMode +
  1757. keyType +
  1758. startDate +
  1759. expireDate +
  1760. _role +
  1761. passWord +
  1762. token +
  1763. authCodeLen +
  1764. authCode;
  1765. let dataModularLength = (dataModular.length / 2)
  1766. .toString(16)
  1767. .PadLeft(4, "0");
  1768. let dataModularSM4 = sm4.sm4_enc(
  1769. hexStr2Byte(dataModular),
  1770. dataModular.length / 2,
  1771. hexStr2Byte(_commonKey)
  1772. );
  1773. let dataModularSM4HexString = byte2HexStr(dataModularSM4);
  1774. console.log(
  1775. "dataModularSM4String---------------------" + dataModularSM4HexString
  1776. );
  1777. let dataModularSM4Length = (dataModularSM4HexString.length / 2)
  1778. .toString(16)
  1779. .PadLeft(4, "0");
  1780. let resultData =
  1781. head + dataModularSM4Length + dataModularLength + dataModularSM4HexString;
  1782. console.log(
  1783. "加密前:" + head + dataModularSM4Length + dataModularLength + dataModular
  1784. );
  1785. let crcData = crc
  1786. .crc16kermit(hexStr2Byte(resultData))
  1787. .toString(16)
  1788. .PadLeft(4, "0");
  1789. resultData += crcData;
  1790. sendDataTolock(resultData);
  1791. staticCallback = callback;
  1792. }
  1793. /**
  1794. * *开门
  1795. * @param {锁ID} _lockId
  1796. * @param {开门的用户} usrID
  1797. * @param {开门用户的signkey} _signKey
  1798. * @param {开门用户的keyId} _keyId
  1799. * @param {传入空字符串} _tokenOfOpenTheDoor
  1800. * @param {开锁方式 '00' 离线 '01'在线} _openMode
  1801. * @param {开门用户的commonKey} _commonKey
  1802. * @param {回调} callback
  1803. */
  1804. static openTheDoor(
  1805. _lockId,
  1806. usrID,
  1807. _signKey,
  1808. _keyId,
  1809. _tokenOfOpenTheDoor,
  1810. _openMode,
  1811. _commonKey,
  1812. callback
  1813. ) {
  1814. console.log("开门参数", [...arguments]);
  1815. deviceNameSaved = _lockId;
  1816. userIdSaved = usrID;
  1817. keyIdSaved = _keyId;
  1818. signKeySaved = _signKey;
  1819. encKeySaved = _commonKey;
  1820. openModeSaved = _openMode;
  1821. let authUserId = wx.getStorageSync("userSignKey");
  1822. let lockId4AuthCode = str2Hex(_lockId);
  1823. let lockId = lockId4AuthCode.PadRight(80, "0");
  1824. let head = makeHeader("3005");
  1825. let cmdId = "3005";
  1826. let keyID4AuthCode = str2Hex(_keyId);
  1827. let keyID = keyID4AuthCode.PadRight(80, "0");
  1828. let userID4AuthCode = str2Hex(usrID);
  1829. let userId = userID4AuthCode.PadRight(40, "0");
  1830. let openMode = _openMode;
  1831. let openTime = parseInt(new Date().getTime() / 1000).toString(16);
  1832. let token = "".PadRight(8, "0");
  1833. let authCode = "";
  1834. let authCodeLen = "00";
  1835. if (_tokenOfOpenTheDoor != "") {
  1836. token = _tokenOfOpenTheDoor;
  1837. console.log("authcode加密前为" + keyID4AuthCode + userID4AuthCode + token + _signKey)
  1838. authCode = ab2hex(
  1839. md5.digest(
  1840. hexStr2Byte(keyID4AuthCode + userID4AuthCode + token + _signKey)
  1841. )
  1842. );
  1843. authCodeLen = (authCode.length / 2).toString(16).PadLeft(2, "0");
  1844. }
  1845. let dataModular =
  1846. cmdId +
  1847. keyID +
  1848. userId +
  1849. openMode +
  1850. openTime +
  1851. token +
  1852. authCodeLen +
  1853. authCode;
  1854. let dataModularLength = (dataModular.length / 2)
  1855. .toString(16)
  1856. .PadLeft(4, "0");
  1857. let dataModularSM4 = sm4.sm4_enc(
  1858. hexStr2Byte(dataModular),
  1859. dataModular.length / 2,
  1860. hexStr2Byte(_commonKey)
  1861. );
  1862. let dataModularSM4HexString = byte2HexStr(dataModularSM4);
  1863. console.log(
  1864. "dataModularSM4String---------------------" + dataModularSM4HexString
  1865. );
  1866. let dataModularSM4Length = (dataModularSM4HexString.length / 2)
  1867. .toString(16)
  1868. .PadLeft(4, "0");
  1869. let resultData =
  1870. head + dataModularSM4Length + dataModularLength + dataModularSM4HexString;
  1871. console.log(
  1872. "加密前:" + head + dataModularSM4Length + dataModularLength + dataModular
  1873. );
  1874. let crcData = crc
  1875. .crc16kermit(hexStr2Byte(resultData))
  1876. .toString(16)
  1877. .PadLeft(4, "0");
  1878. resultData += crcData;
  1879. sendDataTolock(resultData);
  1880. staticCallback = callback;
  1881. }
  1882. /**
  1883. * *重置门锁
  1884. * @param {门锁ID 最多40字节} _lockId
  1885. * @param {用户ID 最多20字节} _usrID
  1886. * @param {传入空字符串即可} _resetLockToken
  1887. * @param {用户的signkey} _signkey
  1888. * @param {用户的commonKey} _commonKey
  1889. * @param {回调} callback
  1890. */
  1891. static resetLock(
  1892. _lockId,
  1893. _usrID,
  1894. _resetLockToken,
  1895. _signkey,
  1896. _commonKey,
  1897. callback
  1898. ) {
  1899. deviceNameSaved = _lockId;
  1900. userIdSaved = _usrID;
  1901. signKeySaved = _signkey;
  1902. encKeySaved = _commonKey;
  1903. let head = makeHeader("3004");
  1904. let cmdId = "3004";
  1905. let lockId4AuthCode = str2Hex(_lockId);
  1906. let lockId = lockId4AuthCode.PadRight(80, "0");
  1907. let userId = str2Hex(_usrID).PadRight(40, "0");
  1908. let token = "".PadRight(8, "0");
  1909. var authCodeLen = "00";
  1910. var authCode = "";
  1911. if (_resetLockToken != "") {
  1912. token = _resetLockToken;
  1913. authCode = ab2hex(md5.digest(hex2Ab(lockId4AuthCode + token + _signkey)));
  1914. authCodeLen = (authCode.length / 2).toString(16).PadLeft(2, "0");
  1915. }
  1916. let dataModular = cmdId + lockId + userId + token + authCodeLen + authCode;
  1917. let dataModularLength = (dataModular.length / 2)
  1918. .toString(16)
  1919. .PadLeft(4, "0");
  1920. let dataModularSM4 = sm4.sm4_enc(
  1921. hexStr2Byte(dataModular),
  1922. dataModular.length / 2,
  1923. hexStr2Byte(_commonKey)
  1924. );
  1925. let dataModularSM4HexString = byte2HexStr(dataModularSM4);
  1926. console.log(
  1927. "dataModularSM4String---------------------" + dataModularSM4HexString
  1928. );
  1929. let dataModularSM4Length = (dataModularSM4HexString.length / 2)
  1930. .toString(16)
  1931. .PadLeft(4, "0");
  1932. let resultData =
  1933. head + dataModularSM4Length + dataModularLength + dataModularSM4HexString;
  1934. console.log(
  1935. "加密前:" + head + dataModularSM4Length + dataModularLength + dataModular
  1936. );
  1937. let crcData = crc
  1938. .crc16kermit(hex2Ab(resultData))
  1939. .toString(16)
  1940. .PadLeft(4, "0");
  1941. resultData += crcData;
  1942. sendDataTolock(resultData);
  1943. staticCallback = callback;
  1944. }
  1945. /**
  1946. * 同步时间
  1947. * @param {门锁ID 最多40字节} _lockId
  1948. * @param {用户ID 最多20字节} usrId
  1949. * @param {时间- long型秒级时间戳} _time
  1950. * @param {NB接入域名IP} _NBDomainIP
  1951. * @param {用户的commonKey} _commonKey
  1952. * @param {回调函数} callback
  1953. */
  1954. static updateTime(_lockId, usrId, _time, _NBDomainIP, _commonKey, callback) {
  1955. encKeySaved = _commonKey;
  1956. let head = makeHeader("3009");
  1957. let cmdId = "3009";
  1958. let lockId4AuthCode = str2Hex(_lockId);
  1959. let lockerId = lockId4AuthCode.PadRight(80, "0");
  1960. let userId = str2Hex(usrId).PadRight(40, "0");
  1961. let time = parseInt(_time).toString(16);
  1962. let NBDomainIPHex = str2Hex(_NBDomainIP);
  1963. let NBDomainIP = NBDomainIPHex.PadRight(40, "0");
  1964. let dataModular = cmdId + lockerId + userId + time + NBDomainIP;
  1965. let dataModularLength = (dataModular.length / 2)
  1966. .toString(16)
  1967. .PadLeft(4, "0");
  1968. let dataModularSM4 = sm4.sm4_enc(
  1969. hexStr2Byte(dataModular),
  1970. dataModular.length / 2,
  1971. hexStr2Byte(_commonKey)
  1972. );
  1973. let dataModularSM4HexString = byte2HexStr(dataModularSM4);
  1974. console.log(
  1975. "dataModularSM4String---------------------" + dataModularSM4HexString
  1976. );
  1977. let dataModularSM4Length = (dataModularSM4HexString.length / 2)
  1978. .toString(16)
  1979. .PadLeft(4, "0");
  1980. let resultData =
  1981. head + dataModularSM4Length + dataModularLength + dataModularSM4HexString;
  1982. console.log(
  1983. "加密前:" + head + dataModularSM4Length + dataModularLength + dataModular
  1984. );
  1985. let crcData = crc
  1986. .crc16kermit(hex2Ab(resultData))
  1987. .toString(16)
  1988. .PadLeft(4, "0");
  1989. resultData += crcData;
  1990. sendDataTolock(resultData);
  1991. staticCallback = callback;
  1992. }
  1993. /**
  1994. * 更新固件确认token
  1995. * @param {门锁ID 最多40字节} _lockId
  1996. * @param {系统管理员ID - 最多20字节} _systemUserId
  1997. * @param {固件升级程序版本 - 4字节 16进制} _fileVersion
  1998. * @param {升级文件总字节数 - 4字节 16进制} _fileSize
  1999. * @param {升级文件crc校验码 - 4字节 16进制} _fileCRC
  2000. * @param {传入空字符串} _updateFirewareToken
  2001. * @param {系统管理员singkey} _signKey
  2002. * @param {通讯密钥} _commonKey
  2003. * @param {回调} callback
  2004. */
  2005. static updateFirmware(
  2006. _lockId,
  2007. _systemUserId,
  2008. _fileVersion,
  2009. _fileSize,
  2010. _fileCRC,
  2011. _updateFirewareToken,
  2012. _signKey,
  2013. _commonKey,
  2014. callback
  2015. ) {
  2016. console.log("更新固件确认token参数", [...arguments]);
  2017. deviceNameSaved = _lockId;
  2018. authUserIdSaved = _systemUserId;
  2019. fileVersionSaved = _fileVersion;
  2020. fileSizeSaved = _fileSize;
  2021. fileCRCSaved = _fileCRC;
  2022. signKeySaved = _signKey;
  2023. encKeySaved = _commonKey;
  2024. let head = makeHeader("300b");
  2025. let cmdId = "300b";
  2026. let lockId4AuthCode = str2Hex(_lockId);
  2027. let lockerId = lockId4AuthCode.PadRight(80, "0");
  2028. let userID4AuthCode = str2Hex(_systemUserId);
  2029. let userId = userID4AuthCode.PadRight(40, "0");
  2030. let fileVersion = _fileVersion;
  2031. let fileSize = _fileSize;
  2032. let fileCRC = _fileCRC;
  2033. let token = "00000000";
  2034. let authCodeLen = "00";
  2035. let authCode = "";
  2036. if (_updateFirewareToken != "") {
  2037. token = _updateFirewareToken;
  2038. authCode = ab2hex(md5.digest(hex2Ab(lockId4AuthCode + token + _signKey)));
  2039. authCodeLen = (authCode.length / 2).toString(16).PadLeft(2, "0");
  2040. }
  2041. let dataModular =
  2042. cmdId +
  2043. lockerId +
  2044. userId +
  2045. fileVersion +
  2046. fileSize +
  2047. fileCRC +
  2048. token +
  2049. authCodeLen +
  2050. authCode;
  2051. let dataModularLength = (dataModular.length / 2)
  2052. .toString(16)
  2053. .PadLeft(4, "0");
  2054. let dataModularSM4 = sm4.sm4_enc(
  2055. hexStr2Byte(dataModular),
  2056. dataModular.length / 2,
  2057. hexStr2Byte(_commonKey)
  2058. );
  2059. let dataModularSM4HexString = byte2HexStr(dataModularSM4);
  2060. console.log(
  2061. "dataModularSM4String---------------------" + dataModularSM4HexString
  2062. );
  2063. let dataModularSM4Length = (dataModularSM4HexString.length / 2)
  2064. .toString(16)
  2065. .PadLeft(4, "0");
  2066. let resultData =
  2067. head + dataModularSM4Length + dataModularLength + dataModularSM4HexString;
  2068. console.log(
  2069. "加密前:" + head + dataModularSM4Length + dataModularLength + dataModular
  2070. );
  2071. let crcData = crc
  2072. .crc16kermit(hex2Ab(resultData))
  2073. .toString(16)
  2074. .PadLeft(4, "0");
  2075. resultData += crcData;
  2076. sendDataTolock(resultData);
  2077. staticCallback = callback;
  2078. }
  2079. /**
  2080. * 发送更新文件
  2081. * @param {从更新固件获取的token} _token
  2082. * @param {文件大小 - 4字节 16进制} _fileSize
  2083. * @param {当前数据偏移量 - 4字节 16进制} _dataOffset
  2084. * @param {数据长度 - 4字节 16进制 每次最大发送1K} _dataLength
  2085. * @param {升级文件数据} _data
  2086. * @param {通讯密钥} _commonKey
  2087. */
  2088. static transforUpdateFile(
  2089. _token,
  2090. _fileSize,
  2091. _dataOffset,
  2092. _dataLength,
  2093. _data,
  2094. _commonKey,
  2095. callback
  2096. ) {
  2097. encKeySaved = _commonKey;
  2098. let head = makeHeader("300c");
  2099. let cmdId = "300c";
  2100. let dataModular =
  2101. cmdId + _token + _fileSize + _dataOffset + _dataLength + _data;
  2102. let dataModularLength = (dataModular.length / 2)
  2103. .toString(16)
  2104. .PadLeft(4, "0");
  2105. let dataModularSM4 = sm4.sm4_enc(
  2106. hexStr2Byte(dataModular),
  2107. dataModular.length / 2,
  2108. hexStr2Byte(_commonKey)
  2109. );
  2110. let dataModularSM4HexString = byte2HexStr(dataModularSM4);
  2111. let dataModularSM4Length = (dataModularSM4HexString.length / 2)
  2112. .toString(16)
  2113. .PadLeft(4, "0");
  2114. let resultData =
  2115. head + dataModularSM4Length + dataModularLength + dataModularSM4HexString;
  2116. console.log(
  2117. "加密前:" + head + dataModularSM4Length + dataModularLength + dataModular
  2118. );
  2119. let crcData = crc
  2120. .crc16kermit(hex2Ab(resultData))
  2121. .toString(16)
  2122. .PadLeft(4, "0");
  2123. resultData += crcData;
  2124. sendDataTolock(resultData);
  2125. staticCallback = callback;
  2126. }
  2127. /**
  2128. * *删除用户
  2129. * @param {门锁ID} _lockId
  2130. * @param {超管ID 最多20字节} _superAdminId
  2131. * @param {用户ID 最多20字节} usrId
  2132. * @param {超管keyId} _keyId
  2133. * @param {置空即可} delUserToken
  2134. * @param {超管的signkey} _signkey
  2135. * @param {用户的commonKey} _commonKey
  2136. * @param {回调} callback
  2137. */
  2138. static deleteUser(
  2139. _lockId,
  2140. _superAdminId,
  2141. usrId,
  2142. _keyId,
  2143. delUserToken,
  2144. _signkey,
  2145. _commonKey,
  2146. callback
  2147. ) {
  2148. deviceNameSaved = _lockId;
  2149. authUserIdSaved = _superAdminId;
  2150. userIdSaved = usrId;
  2151. keyIdSaved = _keyId;
  2152. signKeySaved = _signkey;
  2153. encKeySaved = _commonKey;
  2154. let head = makeHeader("3002");
  2155. let cmdId = "3002";
  2156. let lockId4AuthCode = str2Hex(_lockId);
  2157. let lockId = lockId4AuthCode.PadRight(80, "0");
  2158. let authuserID4AuthCode = str2Hex(_superAdminId);
  2159. let authUserId = authuserID4AuthCode.PadRight(40, "0");
  2160. let keyID4AuthCode = str2Hex(_keyId);
  2161. let keyId = keyID4AuthCode.PadRight(80, "0");
  2162. let delUserId = str2Hex(usrId).PadRight(40, "0");
  2163. let token = "".PadRight(8, "0");
  2164. let authCodeLen = "00";
  2165. let authCode = "";
  2166. if (delUserToken != "") {
  2167. token = delUserToken;
  2168. authCode = ab2hex(
  2169. md5.digest(
  2170. hex2Ab(authuserID4AuthCode + keyID4AuthCode + token + _signkey)
  2171. )
  2172. );
  2173. authCodeLen = (authCode.length / 2).toString(16).PadLeft(2, "0");
  2174. }
  2175. let dataModular =
  2176. cmdId +
  2177. lockId +
  2178. authUserId +
  2179. keyId +
  2180. delUserId +
  2181. token +
  2182. authCodeLen +
  2183. authCode;
  2184. let dataModularLength = (dataModular.length / 2)
  2185. .toString(16)
  2186. .PadLeft(4, "0");
  2187. let dataModularSM4 = sm4.sm4_enc(
  2188. hexStr2Byte(dataModular),
  2189. dataModular.length / 2,
  2190. hexStr2Byte(_commonKey)
  2191. );
  2192. let dataModularSM4HexString = byte2HexStr(dataModularSM4);
  2193. console.log(
  2194. "dataModularSM4String---------------------" + dataModularSM4HexString
  2195. );
  2196. let dataModularSM4Length = (dataModularSM4HexString.length / 2)
  2197. .toString(16)
  2198. .PadLeft(4, "0");
  2199. let resultData =
  2200. head + dataModularSM4Length + dataModularLength + dataModularSM4HexString;
  2201. console.log(
  2202. "加密前:" + head + dataModularSM4Length + dataModularLength + dataModular
  2203. );
  2204. let crcData = crc
  2205. .crc16kermit(hex2Ab(resultData))
  2206. .toString(16)
  2207. .PadLeft(4, "0");
  2208. resultData += crcData;
  2209. sendDataTolock(resultData);
  2210. staticCallback = callback;
  2211. }
  2212. /**
  2213. * *添加指纹
  2214. * @param {门锁ID} _lockId
  2215. * @param {超管ID 最多20字节} _superAdminId
  2216. * @param {用户ID 最多20字节} _userId
  2217. * @param {超管的keyId} _keyId
  2218. * @param {置空即可} _fingerToken
  2219. * @param {超管的signKey} _signKey
  2220. * @param {设置开始时间-8位timestamp字符串,eg:'00000000'} _startTime
  2221. * @param {设置结束时间-8位timestamp字符串,eg:'FFFFFFFF'} _endTime
  2222. * @param {是否为报警指纹'00'为否'01'为是} _alarmFinger
  2223. * @param {超管的commonKey} _commonKey
  2224. * @param {回调方法} callback
  2225. */
  2226. static addFingerPrints(
  2227. _lockId,
  2228. _superAdminId,
  2229. _userId,
  2230. _keyId,
  2231. _fingerToken,
  2232. _signKey,
  2233. _startTime,
  2234. _endTime,
  2235. _alarmFinger,
  2236. _commonKey,
  2237. callback
  2238. ) {
  2239. deviceNameSaved = _lockId;
  2240. authUserIdSaved = _superAdminId;
  2241. userIdSaved = _userId;
  2242. signKeySaved = _signKey;
  2243. startTimeSaved = _startTime;
  2244. endTimeSaved = _endTime;
  2245. alarmFingerSaved = _alarmFinger;
  2246. keyIdSaved = _keyId;
  2247. encKeySaved = _commonKey;
  2248. let head = makeHeader("3030");
  2249. let cmdId = "3030";
  2250. let subCmdId = "09";
  2251. let lockId4AuthCode = str2Hex(_lockId);
  2252. let authuserID4AuthCode = str2Hex(_superAdminId);
  2253. let authUserId = authuserID4AuthCode.PadRight(40, "0");
  2254. let keyID4AuthCode = str2Hex(_keyId);
  2255. let keyId = keyID4AuthCode.PadRight(80, "0");
  2256. let userID4AuthCode = str2Hex(_userId);
  2257. let userId = userID4AuthCode.PadRight(40, "0");
  2258. let alarmFinger = _alarmFinger;
  2259. let authCodeLen = "00";
  2260. let authCode = "";
  2261. let token = "00000000";
  2262. if (_fingerToken != "") {
  2263. token = _fingerToken;
  2264. authCode = ab2hex(
  2265. md5.digest(
  2266. hex2Ab(
  2267. authuserID4AuthCode +
  2268. keyID4AuthCode +
  2269. userID4AuthCode +
  2270. token +
  2271. _signKey
  2272. )
  2273. )
  2274. );
  2275. authCodeLen = (authCode.length / 2).toString(16).PadLeft(2, "0");
  2276. }
  2277. let reqData =
  2278. authUserId +
  2279. keyId +
  2280. userId +
  2281. _startTime +
  2282. _endTime +
  2283. alarmFinger +
  2284. token +
  2285. authCodeLen +
  2286. authCode;
  2287. let reqDataLength = (reqData.length / 2).toString(16).PadLeft(2, "0");
  2288. let dataModular = cmdId + subCmdId + reqDataLength + reqData;
  2289. let dataModularLength = (dataModular.length / 2)
  2290. .toString(16)
  2291. .PadLeft(4, "0");
  2292. let dataModularSM4 = sm4.sm4_enc(
  2293. hexStr2Byte(dataModular),
  2294. dataModular.length / 2,
  2295. hexStr2Byte(_commonKey)
  2296. );
  2297. let dataModularSM4HexString = byte2HexStr(dataModularSM4);
  2298. console.log(
  2299. "dataModularSM4String---------------------" + dataModularSM4HexString
  2300. );
  2301. let dataModularSM4Length = (dataModularSM4HexString.length / 2)
  2302. .toString(16)
  2303. .PadLeft(4, "0");
  2304. let resultData =
  2305. head + dataModularSM4Length + dataModularLength + dataModularSM4HexString;
  2306. console.log(
  2307. "加密前:" + head + dataModularSM4Length + dataModularLength + dataModular
  2308. );
  2309. let crcData = crc
  2310. .crc16kermit(hex2Ab(resultData))
  2311. .toString(16)
  2312. .PadLeft(4, "0");
  2313. resultData += crcData;
  2314. sendDataTolock(resultData);
  2315. staticCallback = callback;
  2316. }
  2317. /**
  2318. * *指纹管理
  2319. * @param {*门锁ID 最多40字节} _lockId
  2320. * @param {*超管ID 最多20字节} _superAdminId
  2321. * @param {*用户ID 最多20字节} _userID
  2322. * @param {*用户keyId} _keyId
  2323. * @param {* 01为删除 02修改 03设置报警指纹 } _action
  2324. * @param {*指纹编号 eg'0001'} _fingerNo
  2325. * @param {*置空即可} _fingerToken
  2326. * @param {*superAdmin的signKey} _signKey
  2327. * @param {是否为报警指纹'00'为否'01'为是} _alarmFinger
  2328. * @param {设置开始时间-8位timestamp字符串,eg:'00000000'} _startTime
  2329. * @param {设置结束时间-8位timestamp字符串,eg:'FFFFFFFF'} _endTime
  2330. * @param {超管的commonKey} _commonKey
  2331. * @param {*回调} callback
  2332. */
  2333. static modifyFingerPrints(
  2334. _lockId,
  2335. _superAdminId,
  2336. _userID,
  2337. _keyId,
  2338. _action,
  2339. _fingerNo,
  2340. _fingerToken,
  2341. _signKey,
  2342. _alarmFinger,
  2343. _startTime,
  2344. _endTime,
  2345. _commonKey,
  2346. callback
  2347. ) {
  2348. deviceNameSaved = _lockId;
  2349. authUserIdSaved = _superAdminId;
  2350. userIdSaved = _userID;
  2351. fingerNoSaved = _fingerNo;
  2352. signKeySaved = _signKey;
  2353. keyIdSaved = _keyId;
  2354. actionSaved = _action;
  2355. alarmFingerSaved = _alarmFinger;
  2356. startTimeSaved = _startTime;
  2357. endTimeSaved = _endTime;
  2358. encKeySaved = _commonKey;
  2359. let head = makeHeader("3030");
  2360. let cmdId = "3030";
  2361. let subCmdId = "0b";
  2362. let lockId4AuthCode = str2Hex(_lockId);
  2363. let authuserID4AuthCode = str2Hex(_superAdminId);
  2364. let authUserId = authuserID4AuthCode.PadRight(40, "0");
  2365. let keyID4AuthCode = str2Hex(_keyId);
  2366. let keyId = keyID4AuthCode.PadRight(80, "0");
  2367. let userID4AuthCode = str2Hex(_userID);
  2368. let userId = userID4AuthCode.PadRight(40, "0");
  2369. let alarmFinger = _alarmFinger;
  2370. let startTime = _startTime;
  2371. let endTime = _endTime;
  2372. let authCodeLen = "00";
  2373. let authCode = "";
  2374. let fingerNo = _fingerNo;
  2375. let token = "00000000";
  2376. if (_fingerToken != "") {
  2377. token = _fingerToken;
  2378. authCode = ab2hex(
  2379. md5.digest(
  2380. hex2Ab(
  2381. authuserID4AuthCode +
  2382. keyID4AuthCode +
  2383. userID4AuthCode +
  2384. token +
  2385. _signKey
  2386. )
  2387. )
  2388. );
  2389. authCodeLen = (authCode.length / 2).toString(16).PadLeft(2, "0");
  2390. }
  2391. let reqData =
  2392. authUserId +
  2393. keyId +
  2394. userId +
  2395. startTime +
  2396. endTime +
  2397. fingerNo +
  2398. alarmFinger +
  2399. _action +
  2400. token +
  2401. authCodeLen +
  2402. authCode;
  2403. let reqDataLength = (reqData.length / 2).toString(16).PadLeft(2, "0");
  2404. let dataModular = cmdId + subCmdId + reqDataLength + reqData;
  2405. let dataModularLength = (dataModular.length / 2)
  2406. .toString(16)
  2407. .PadLeft(4, "0");
  2408. let dataModularSM4 = sm4.sm4_enc(
  2409. hexStr2Byte(dataModular),
  2410. dataModular.length / 2,
  2411. hexStr2Byte(_commonKey)
  2412. );
  2413. let dataModularSM4HexString = byte2HexStr(dataModularSM4);
  2414. console.log(
  2415. "dataModularSM4String---------------------" + dataModularSM4HexString
  2416. );
  2417. let dataModularSM4Length = (dataModularSM4HexString.length / 2)
  2418. .toString(16)
  2419. .PadLeft(4, "0");
  2420. let resultData =
  2421. head + dataModularSM4Length + dataModularLength + dataModularSM4HexString;
  2422. console.log(
  2423. "加密前:" + head + dataModularSM4Length + dataModularLength + dataModular
  2424. );
  2425. let crcData = crc
  2426. .crc16kermit(hex2Ab(resultData))
  2427. .toString(16)
  2428. .PadLeft(4, "0");
  2429. resultData += crcData;
  2430. sendDataTolock(resultData);
  2431. staticCallback = callback;
  2432. }
  2433. /**
  2434. * *查询指纹列表
  2435. * @param {*门锁ID 最多40字节} _lockId
  2436. * @param {*用户ID 最多20字节} _userID
  2437. * @param {*用户keyId} _keyId
  2438. * @param {超管的commonKey} _commonKey
  2439. * @param {回调函数} callback
  2440. */
  2441. static queryFingerprintsList(_lockId, _userID, _keyId, _commonKey, callback) {
  2442. encKeySaved = _commonKey;
  2443. let cmdId = "3030";
  2444. let head = makeHeader("3030");
  2445. let lockId4AuthCode = str2Hex(_lockId);
  2446. let lockId = lockId4AuthCode.PadRight(80, "0");
  2447. let subCmdId = "0a";
  2448. let keyID4AuthCode = _keyId;
  2449. let keyId = keyID4AuthCode.PadRight(80, "0");
  2450. let userId = str2Hex(_userID).PadRight(40, "0");
  2451. let reqData = keyId + userId;
  2452. let reqDataLength = (reqData.length / 2).toString(16).PadLeft(2, "0");
  2453. let dataModular = cmdId + subCmdId + reqDataLength + reqData;
  2454. let dataModularLength = (dataModular.length / 2)
  2455. .toString(16)
  2456. .PadLeft(4, "0");
  2457. let dataModularSM4 = sm4.sm4_enc(
  2458. hexStr2Byte(dataModular),
  2459. dataModular.length / 2,
  2460. hexStr2Byte(_commonKey)
  2461. );
  2462. let dataModularSM4HexString = byte2HexStr(dataModularSM4);
  2463. console.log(
  2464. "dataModularSM4String---------------------" + dataModularSM4HexString
  2465. );
  2466. let dataModularSM4Length = (dataModularSM4HexString.length / 2)
  2467. .toString(16)
  2468. .PadLeft(4, "0");
  2469. let resultData =
  2470. head + dataModularSM4Length + dataModularLength + dataModularSM4HexString;
  2471. console.log(
  2472. "加密前:" + head + dataModularSM4Length + dataModularLength + dataModular
  2473. );
  2474. let crcData = crc
  2475. .crc16kermit(hex2Ab(resultData))
  2476. .toString(16)
  2477. .PadLeft(4, "0");
  2478. resultData += crcData;
  2479. sendDataTolock(resultData);
  2480. staticCallback = callback;
  2481. }
  2482. /**
  2483. * *密码管理
  2484. * @param {门锁ID 最多40字节} _lockId
  2485. * @param {超管ID 最多20字节} _superAdminId
  2486. * @param {用户ID 最多20字节} _userID
  2487. * @param {开锁密码 - 6位数字的字符串} _pwd
  2488. * @param {密码编号 - 取值范围 00-FF } _pwdNo
  2489. * @param {超管的keyId} _keyId
  2490. * @param {superAdmin的signKey} _signKey
  2491. * @param {传入空字符串即可} _pwdToken
  2492. * @param {设置开始时间-8位timestamp字符串,eg:'00000000'} _startTime
  2493. * @param {设置结束时间-8位timestamp字符串,eg:'FFFFFFFF'} _endTime
  2494. * @param {使用次数限制 - 整型取值范围是0-2550表示删除密码} _useCountLimit
  2495. * @param {超管的commonKey} _commonKey
  2496. * @param {回调} callback
  2497. */
  2498. static handlePwd(
  2499. _lockId,
  2500. _superAdminId,
  2501. _userID,
  2502. _pwd,
  2503. _pwdNo,
  2504. _keyId,
  2505. _signKey,
  2506. _pwdToken,
  2507. _startTime,
  2508. _endTime,
  2509. _useCountLimit,
  2510. _commonKey,
  2511. callback
  2512. ) {
  2513. console.log("密码参数", [...arguments]);
  2514. deviceNameSaved = _lockId;
  2515. authUserIdSaved = _superAdminId;
  2516. userIdSaved = _userID;
  2517. pwdSaved = _pwd;
  2518. startTimeSaved = _startTime;
  2519. endTimeSaved = _endTime;
  2520. useCountLimitSaved = _useCountLimit;
  2521. keyIdSaved = _keyId;
  2522. signKeySaved = _signKey;
  2523. pwdNoSaved = _pwdNo;
  2524. encKeySaved = _commonKey;
  2525. let head = makeHeader("3030");
  2526. let cmdId = "3030";
  2527. let subCmdId = "03";
  2528. let lockId4AuthCode = str2Hex(_lockId);
  2529. let authuserID4AuthCode = str2Hex(_superAdminId);
  2530. let authUserId = authuserID4AuthCode.PadRight(40, "0");
  2531. let keyID4AuthCode = str2Hex(_keyId);
  2532. let keyId = keyID4AuthCode.PadRight(80, "0");
  2533. let userID4AuthCode = str2Hex(_userID);
  2534. let userId = userID4AuthCode.PadRight(40, "0");
  2535. let pwd = str2Hex(_pwd).PadRight(40, "0");
  2536. let useCountLimit = _useCountLimit.toString(16).PadLeft(2, "0");
  2537. let token = "00000000".PadRight(8, "0");
  2538. let startTime = _startTime.PadRight(8, "0");
  2539. let endTime = _endTime.PadRight(8, "0");
  2540. let authCodeLen = "00";
  2541. let authCode = "";
  2542. if (_pwdToken != "") {
  2543. token = _pwdToken;
  2544. authCode = ab2hex(
  2545. md5.digest(
  2546. hex2Ab(
  2547. authuserID4AuthCode +
  2548. keyID4AuthCode +
  2549. userID4AuthCode +
  2550. token +
  2551. _signKey
  2552. )
  2553. )
  2554. );
  2555. console.log("2656", authCode);
  2556. authCodeLen = (authCode.length / 2).toString(16).PadLeft(2, "0");
  2557. }
  2558. let reqData =
  2559. authUserId +
  2560. keyId +
  2561. userId +
  2562. _pwdNo +
  2563. pwd +
  2564. useCountLimit +
  2565. token +
  2566. startTime +
  2567. endTime +
  2568. authCodeLen +
  2569. authCode;
  2570. console.log("2672", authUserId+"---",
  2571. keyId+"---",
  2572. userId+"---",
  2573. _pwdNo+"---",
  2574. pwd+"---",
  2575. useCountLimit+"---",
  2576. token+"---",
  2577. startTime+"---",
  2578. endTime+"---",
  2579. authCodeLen+"---",
  2580. authCode);
  2581. console.log("2673", reqData, reqData.length, reqData.length / 2, (reqData.length / 2).toString(16), typeof ((reqData.length / 2).toString(16)));
  2582. let reqDataLength = (reqData.length / 2).toString(16).PadLeft(2, "0");
  2583. let dataModular = cmdId + subCmdId + reqDataLength + reqData;
  2584. let dataModularLength = (dataModular.length / 2)
  2585. .toString(16)
  2586. .PadLeft(4, "0");
  2587. let dataModularSM4 = sm4.sm4_enc(
  2588. hexStr2Byte(dataModular),
  2589. dataModular.length / 2,
  2590. hexStr2Byte(_commonKey)
  2591. );
  2592. let dataModularSM4HexString = byte2HexStr(dataModularSM4);
  2593. console.log(
  2594. "dataModularSM4String---------------------" + dataModularSM4HexString
  2595. );
  2596. let dataModularSM4Length = (dataModularSM4HexString.length / 2)
  2597. .toString(16)
  2598. .PadLeft(4, "0");
  2599. let resultData =
  2600. head + dataModularSM4Length + dataModularLength + dataModularSM4HexString;
  2601. console.log(
  2602. "加密前:" + head + dataModularSM4Length + dataModularLength + dataModular
  2603. );
  2604. let crcData = crc
  2605. .crc16kermit(hex2Ab(resultData))
  2606. .toString(16)
  2607. .PadLeft(4, "0");
  2608. resultData += crcData;
  2609. sendDataTolock(resultData);
  2610. staticCallback = callback;
  2611. }
  2612. /**
  2613. * *密码列表
  2614. * @param {门锁ID 最多40字节} _lockId
  2615. * @param {用户ID 最多20字节} _userID
  2616. * @param {用户keyId} _keyId
  2617. * @param {用户的commonKey} _commonKey
  2618. * @param {回调} callback
  2619. */
  2620. static queryPwdList(_lockId, _userID, _keyId, _commonKey, callback) {
  2621. encKeySaved = _commonKey;
  2622. let cmdId = "3030";
  2623. let head = makeHeader("3030");
  2624. let lockId4AuthCode = str2Hex(_lockId);
  2625. let lockId = lockId4AuthCode.PadRight(80, "0");
  2626. let userId = str2Hex(_userID).PadRight(40, "0");
  2627. let keyID4AuthCode = str2Hex(_keyId);
  2628. let keyId = keyID4AuthCode.PadRight(80, "0");
  2629. let subCmdId = "05";
  2630. let reqData = keyId + userId;
  2631. let reqDataLength = (reqData.length / 2).toString(16).PadLeft(2, "0");
  2632. let dataModular = cmdId + subCmdId + reqDataLength + reqData;
  2633. let dataModularLength = (dataModular.length / 2)
  2634. .toString(16)
  2635. .PadLeft(4, "0");
  2636. let dataModularSM4 = sm4.sm4_enc(
  2637. hexStr2Byte(dataModular),
  2638. dataModular.length / 2,
  2639. hexStr2Byte(_commonKey)
  2640. );
  2641. let dataModularSM4HexString = byte2HexStr(dataModularSM4);
  2642. console.log(
  2643. "dataModularSM4String---------------------" + dataModularSM4HexString
  2644. );
  2645. let dataModularSM4Length = (dataModularSM4HexString.length / 2)
  2646. .toString(16)
  2647. .PadLeft(4, "0");
  2648. let resultData =
  2649. head + dataModularSM4Length + dataModularLength + dataModularSM4HexString;
  2650. console.log(
  2651. "加密前:" + head + dataModularSM4Length + dataModularLength + dataModular
  2652. );
  2653. let crcData = crc
  2654. .crc16kermit(hex2Ab(resultData))
  2655. .toString(16)
  2656. .PadLeft(4, "0");
  2657. resultData += crcData;
  2658. sendDataTolock(resultData);
  2659. staticCallback = callback;
  2660. }
  2661. /**
  2662. * 读取开锁记录
  2663. * @param {门锁ID 最多40字节} _lockId
  2664. * @param {用户ID 最多20字节} _userID
  2665. * @param {2个字节 0000: 启动读取记录 0001上条记录操作同步正常读取下一条记录确认存储完上次的记录,再传入0001,会删除上次记录 } _openStatus
  2666. * @param {用户的commonKey} _commonKey
  2667. * @param {回调函数} callback
  2668. */
  2669. static queryLockRecord(_lockId, _userID, _openStatus, _commonKey, callback) {
  2670. encKeySaved = _commonKey;
  2671. let cmdId = "3008";
  2672. let head = makeHeader("3008");
  2673. let lockId4AuthCode = str2Hex(_lockId);
  2674. let lockId = lockId4AuthCode.PadRight(80, "0");
  2675. let userId = str2Hex(_userID).PadRight(40, "0");
  2676. let lockRecordCount = "0004";
  2677. let dataModular = cmdId + lockId + userId + _openStatus + lockRecordCount;
  2678. let dataModularLength = (dataModular.length / 2)
  2679. .toString(16)
  2680. .PadLeft(4, "0");
  2681. let dataModularSM4 = sm4.sm4_enc(
  2682. hexStr2Byte(dataModular),
  2683. dataModular.length / 2,
  2684. hexStr2Byte(_commonKey)
  2685. );
  2686. let dataModularSM4HexString = byte2HexStr(dataModularSM4);
  2687. console.log(
  2688. "dataModularSM4String---------------------" + dataModularSM4HexString
  2689. );
  2690. let dataModularSM4Length = (dataModularSM4HexString.length / 2)
  2691. .toString(16)
  2692. .PadLeft(4, "0");
  2693. let resultData =
  2694. head + dataModularSM4Length + dataModularLength + dataModularSM4HexString;
  2695. console.log(
  2696. "加密前:" + head + dataModularSM4Length + dataModularLength + dataModular
  2697. );
  2698. let crcData = crc
  2699. .crc16kermit(hex2Ab(resultData))
  2700. .toString(16)
  2701. .PadLeft(4, "0");
  2702. resultData += crcData;
  2703. sendDataTolock(resultData);
  2704. staticCallback = callback;
  2705. }
  2706. /**
  2707. * 读取蓝扣ID
  2708. * @param {门锁ID 最多40字节} _lockId
  2709. * @param {用户的commonKey} _commonKey
  2710. * @param {回调函数} callback
  2711. */
  2712. static readBluetoothBuckle(_lockId, _commonKey, callback) {
  2713. console.log('读取蓝牙扣id参数', [...arguments])
  2714. encKeySaved = _commonKey.substring(0, 32)
  2715. let cmdId = "3010";
  2716. let head = makeHeader("3010");
  2717. console.log('head', head)
  2718. let lockId4AuthCode = str2Hex(_lockId);
  2719. let lockId = lockId4AuthCode.PadRight(80, "0");
  2720. let dataModular = cmdId + lockId;
  2721. let dataModularLength = (dataModular.length / 2)
  2722. .toString(16)
  2723. .PadLeft(4, "0");
  2724. let dataModularSM4 = sm4.sm4_enc(
  2725. hexStr2Byte(dataModular),
  2726. dataModular.length / 2,
  2727. hexStr2Byte(encKeySaved)
  2728. );
  2729. let dataModularSM4HexString = byte2HexStr(dataModularSM4);
  2730. console.log(
  2731. "dataModularSM4String---------------------" + dataModularSM4HexString
  2732. );
  2733. let dataModularSM4Length = (dataModularSM4HexString.length / 2)
  2734. .toString(16)
  2735. .PadLeft(4, "0");
  2736. let resultData =
  2737. head + dataModularSM4Length + dataModularLength + dataModularSM4HexString;
  2738. console.log(
  2739. "加密前:" + head + dataModularSM4Length + dataModularLength + dataModular
  2740. );
  2741. let crcData = crc
  2742. .crc16kermit(hex2Ab(resultData))
  2743. .toString(16)
  2744. .PadLeft(4, "0");
  2745. resultData += crcData;
  2746. sendDataTolock(resultData);
  2747. staticCallback = callback;
  2748. }
  2749. /**
  2750. * 注册蓝牙扣
  2751. * @param {授权用户ID 最多20字节 } _authUserId
  2752. * @param {蓝牙钥匙扣ID - 最多20字节 返回的蓝牙扣名称} _blueKeyId
  2753. * @param {授权用户keyID - 最多40字节 '0o01weit9ti9ti' } _keyId
  2754. * @param {密码 - 最多20字节 '3030000000000000000000000000000000000000'} _password
  2755. * @param {传入空字符串即可} _bluetoothBuckleToken
  2756. * @param {授权者的signkey } _singkey
  2757. * @param {通讯密钥} _commonKey
  2758. * @param {回调方法} callback
  2759. */
  2760. static registeBluetoothBuckle(
  2761. _authUserId,
  2762. _blueKeyId,
  2763. _keyId,
  2764. _password,
  2765. _bluetoothBuckleToken,
  2766. _signKey,
  2767. _commonKey,
  2768. callback
  2769. ) {
  2770. console.log('注册蓝牙扣参数', [...arguments])
  2771. authUserIdSaved = _authUserId;
  2772. blueKeyIdSaved = _blueKeyId;
  2773. keyIdSaved = _keyId;
  2774. pwdSaved = _password;
  2775. signKeySaved = _signKey;
  2776. encKeySaved = _commonKey.substring(0, 32)
  2777. let cmdId = "3011";
  2778. let head = makeHeader("3011");
  2779. let authuserID4AuthCode = str2Hex(_authUserId);
  2780. let authUserId = str2Hex(_authUserId).PadRight(40, "0");
  2781. let blueKeyId = str2Hex(_blueKeyId).PadRight(40, '0')
  2782. let keyID4AuthCode = str2Hex(_keyId);
  2783. let keyId = str2Hex(_keyId).PadRight(80, "0");
  2784. let password = _password;
  2785. let token = "00000000";
  2786. let authCodeLen = "00";
  2787. let authCode = "";
  2788. if (_bluetoothBuckleToken != "") {
  2789. token = _bluetoothBuckleToken;
  2790. let ii = getIIData();
  2791. console.log('ii', ii)
  2792. authCode = ab2hex(
  2793. md5.digest(
  2794. hex2Ab(
  2795. authuserID4AuthCode + keyID4AuthCode + token + '7569594d4d5377657277313232405e23264038370a0d1112151A212A3542'
  2796. )
  2797. )
  2798. );
  2799. authCodeLen = (authCode.length / 2).toString(16).PadLeft(2, "0");
  2800. }
  2801. let authSingkey = ab2hex(md5.digest(hex2Ab(authuserID4AuthCode + keyID4AuthCode + str2Hex(_blueKeyId) + _signKey)))
  2802. console.log(_authUserId, _keyId, _blueKeyId, _signKey)
  2803. let dataModular =
  2804. cmdId +
  2805. authUserId +
  2806. blueKeyId +
  2807. keyId +
  2808. password +
  2809. token +
  2810. authCodeLen +
  2811. authCode +
  2812. authSingkey;
  2813. console.log('authcode', authCode)
  2814. let dataModularLength = (dataModular.length / 2)
  2815. .toString(16)
  2816. .PadLeft(4, "0");
  2817. let dataModularSM4 = sm4.sm4_enc(
  2818. hexStr2Byte(dataModular),
  2819. dataModular.length / 2,
  2820. hexStr2Byte(encKeySaved)
  2821. );
  2822. let dataModularSM4HexString = byte2HexStr(dataModularSM4);
  2823. console.log(
  2824. "dataModularSM4String---------------------" + dataModularSM4HexString
  2825. );
  2826. let dataModularSM4Length = (dataModularSM4HexString.length / 2)
  2827. .toString(16)
  2828. .PadLeft(4, "0");
  2829. let resultData =
  2830. head + dataModularSM4Length + dataModularLength + dataModularSM4HexString;
  2831. console.log(
  2832. "加密前:" + head + dataModularSM4Length + dataModularLength + dataModular
  2833. );
  2834. let crcData = crc
  2835. .crc16kermit(hex2Ab(resultData))
  2836. .toString(16)
  2837. .PadLeft(4, "0");
  2838. resultData += crcData;
  2839. sendDataTolock(resultData);
  2840. staticCallback = callback;
  2841. }
  2842. /**
  2843. * *查询RF卡列表
  2844. * @param {门锁ID 最多40字节} _lockId
  2845. * @param {用户ID 最多20字节} _userID
  2846. * @param {用户的commonKey} _commonKey
  2847. * @param {回调函数} callback
  2848. */
  2849. static queryRFCard(_lockId, _userID, _keyId, _commonKey, callback) {
  2850. encKeySaved = _commonKey;
  2851. let cmdId = "3030";
  2852. let head = makeHeader("3030");
  2853. let lockId4AuthCode = str2Hex(_lockId);
  2854. let lockId = lockId4AuthCode.PadRight(80, "0");
  2855. let subCmdId = "10";
  2856. let keyID4AuthCode = str2Hex(_keyId);
  2857. let keyId = keyID4AuthCode.PadRight(80, "0");
  2858. let userId = str2Hex(_userID).PadRight(40, "0");
  2859. let reqData = keyId + userId;
  2860. let reqDataLength = (reqData.length / 2).toString(16).PadLeft(2, "0");
  2861. let dataModular = cmdId + subCmdId + reqDataLength + reqData;
  2862. let dataModularLength = (dataModular.length / 2)
  2863. .toString(16)
  2864. .PadLeft(4, "0");
  2865. let dataModularSM4 = sm4.sm4_enc(
  2866. hexStr2Byte(dataModular),
  2867. dataModular.length / 2,
  2868. hexStr2Byte(_commonKey)
  2869. );
  2870. let dataModularSM4HexString = byte2HexStr(dataModularSM4);
  2871. console.log(
  2872. "dataModularSM4String---------------------" + dataModularSM4HexString
  2873. );
  2874. let dataModularSM4Length = (dataModularSM4HexString.length / 2)
  2875. .toString(16)
  2876. .PadLeft(4, "0");
  2877. let resultData =
  2878. head + dataModularSM4Length + dataModularLength + dataModularSM4HexString;
  2879. console.log(
  2880. "加密前:" + head + dataModularSM4Length + dataModularLength + dataModular
  2881. );
  2882. let crcData = crc
  2883. .crc16kermit(hex2Ab(resultData))
  2884. .toString(16)
  2885. .PadLeft(4, "0");
  2886. resultData += crcData;
  2887. sendDataTolock(resultData);
  2888. staticCallback = callback;
  2889. }
  2890. /**
  2891. * *录制RF卡
  2892. * @param {门锁ID 最多40字节} _lockId
  2893. * @param {超管ID 最多20字节} _superAdminId
  2894. * @param {用户ID 最多20字节} _userID
  2895. * @param {超管的keyId} _keyId
  2896. * @param {RF卡的索引号-取值范围 00-FF} _index
  2897. * @param {传入空字符串即可} _rfToken
  2898. * @param {superAdmin的signKey} _signKey
  2899. * @param {设置开始时间-8位timestamp字符串,eg:'00000000'} _startTime
  2900. * @param {设置结束时间-8位timestamp字符串,eg:'FFFFFFFF'} _endTime
  2901. * @param {超管的commonKey} _commonKey
  2902. * @param {回调函数} callback
  2903. */
  2904. static RfCardRegister(
  2905. _lockId,
  2906. _superAdminId,
  2907. _userID,
  2908. _keyId,
  2909. _index,
  2910. _rfToken,
  2911. _signKey,
  2912. _startTime,
  2913. _endTime,
  2914. _commonKey,
  2915. callback
  2916. ) {
  2917. deviceNameSaved = _lockId;
  2918. authUserIdSaved = _superAdminId;
  2919. userIdSaved = _userID;
  2920. keyIdSaved = _keyId;
  2921. indexSaved = _index;
  2922. signKeySaved = _signKey;
  2923. startTimeSaved = _startTime;
  2924. endTimeSaved = _endTime;
  2925. encKeySaved = _commonKey;
  2926. let cmdId = "3030";
  2927. let head = makeHeader("3030");
  2928. let lockId4AuthCode = str2Hex(_lockId);
  2929. let lockId = lockId4AuthCode.PadRight(80, "0");
  2930. let subCmdId = "0F";
  2931. let authuserID4AuthCode = str2Hex(_superAdminId);
  2932. let authUserId = authuserID4AuthCode.PadRight(40, "0");
  2933. let keyID4AuthCode = str2Hex(_keyId);
  2934. let keyId = keyID4AuthCode.PadRight(80, "0");
  2935. let userID4AuthCode = str2Hex(_userID);
  2936. let userId = userID4AuthCode.PadRight(40, "0");
  2937. let index = _index;
  2938. let token = "00000000";
  2939. let authCodeLen = "00";
  2940. let authCode = "";
  2941. if (_rfToken != "") {
  2942. token = _rfToken;
  2943. authCode = ab2hex(
  2944. md5.digest(
  2945. hex2Ab(
  2946. authuserID4AuthCode +
  2947. keyID4AuthCode +
  2948. userID4AuthCode +
  2949. token +
  2950. _signKey
  2951. )
  2952. )
  2953. );
  2954. authCodeLen = (authCode.length / 2).toString(16).PadLeft(2, "0");
  2955. }
  2956. let reqData =
  2957. authUserId +
  2958. keyId +
  2959. userId +
  2960. index +
  2961. _startTime +
  2962. _endTime +
  2963. token +
  2964. authCodeLen +
  2965. authCode;
  2966. let reqDataLength = (reqData.length / 2).toString(16).PadLeft(2, "0");
  2967. let dataModular = cmdId + subCmdId + reqDataLength + reqData;
  2968. let dataModularLength = (dataModular.length / 2)
  2969. .toString(16)
  2970. .PadLeft(4, "0");
  2971. let dataModularSM4 = sm4.sm4_enc(
  2972. hexStr2Byte(dataModular),
  2973. dataModular.length / 2,
  2974. hexStr2Byte(_commonKey)
  2975. );
  2976. let dataModularSM4HexString = byte2HexStr(dataModularSM4);
  2977. console.log(
  2978. "dataModularSM4String---------------------" + dataModularSM4HexString
  2979. );
  2980. let dataModularSM4Length = (dataModularSM4HexString.length / 2)
  2981. .toString(16)
  2982. .PadLeft(4, "0");
  2983. let resultData =
  2984. head + dataModularSM4Length + dataModularLength + dataModularSM4HexString;
  2985. console.log(
  2986. "加密前:" + head + dataModularSM4Length + dataModularLength + dataModular
  2987. );
  2988. let crcData = crc
  2989. .crc16kermit(hex2Ab(resultData))
  2990. .toString(16)
  2991. .PadLeft(4, "0");
  2992. resultData += crcData;
  2993. sendDataTolock(resultData);
  2994. staticCallback = callback;
  2995. }
  2996. /**
  2997. * *RF卡管理
  2998. * @param {门锁ID 最多40字节} _lockId
  2999. * @param {超管ID 最多20字节} _superAdminId
  3000. * @param {用户ID 最多20字节} _userID
  3001. * @param {用户keyId} _keyId
  3002. * @param {RF卡的索引号-取值范围 00-FF} _index
  3003. * @param {置空即可} _rfToken
  3004. * @param {01删除 02修改} _action
  3005. * @param {superAdmin的signKey} _signKey
  3006. * @param {设置开始时间-8位timestamp字符串,eg:'00000000'} _startTime
  3007. * @param {设置结束时间-8位timestamp字符串,eg:'FFFFFFFF'} _endTime
  3008. * @param {超管的commonKey} _commonKey
  3009. * @param {回调函数} callback
  3010. */
  3011. static modifyRFCard(
  3012. _lockId,
  3013. _superAdminId,
  3014. _userID,
  3015. _keyId,
  3016. _index,
  3017. _rfToken,
  3018. _action,
  3019. _signKey,
  3020. _startTime,
  3021. _endTime,
  3022. _commonKey,
  3023. callback
  3024. ) {
  3025. deviceNameSaved = _lockId;
  3026. authUserIdSaved = _superAdminId;
  3027. userIdSaved = _userID;
  3028. keyIdSaved = _keyId;
  3029. indexSaved = _index;
  3030. signKeySaved = _signKey;
  3031. startTimeSaved = _startTime;
  3032. endTimeSaved = _endTime;
  3033. actionSaved = _action;
  3034. encKeySaved = _commonKey;
  3035. let cmdId = "3030";
  3036. let head = makeHeader("3030");
  3037. let lockId4AuthCode = str2Hex(_lockId);
  3038. let lockId = lockId4AuthCode.PadRight(80, "0");
  3039. let subCmdId = "11";
  3040. let authuserID4AuthCode = str2Hex(_superAdminId);
  3041. let authUserId = authuserID4AuthCode.PadRight(40, "0");
  3042. let keyID4AuthCode = str2Hex(_keyId);
  3043. let keyId = keyID4AuthCode.PadRight(80, "0");
  3044. let userID4AuthCode = str2Hex(_userID);
  3045. let userId = userID4AuthCode.PadRight(40, "0");
  3046. let index = _index;
  3047. let token = "00000000";
  3048. let authCodeLen = "00";
  3049. let authCode = "";
  3050. if (_rfToken != "") {
  3051. token = _rfToken;
  3052. authCode = ab2hex(
  3053. md5.digest(
  3054. hex2Ab(
  3055. authuserID4AuthCode +
  3056. keyID4AuthCode +
  3057. userID4AuthCode +
  3058. token +
  3059. _signKey
  3060. )
  3061. )
  3062. );
  3063. authCodeLen = (authCode.length / 2).toString(16).PadLeft(2, "0");
  3064. }
  3065. let reqData =
  3066. authUserId +
  3067. keyId +
  3068. userId +
  3069. index +
  3070. _action +
  3071. _startTime +
  3072. _endTime +
  3073. token +
  3074. authCodeLen +
  3075. authCode;
  3076. let reqDataLength = (reqData.length / 2).toString(16).PadLeft(2, "0");
  3077. let dataModular = cmdId + subCmdId + reqDataLength + reqData;
  3078. let dataModularLength = (dataModular.length / 2)
  3079. .toString(16)
  3080. .PadLeft(4, "0");
  3081. let dataModularSM4 = sm4.sm4_enc(
  3082. hexStr2Byte(dataModular),
  3083. dataModular.length / 2,
  3084. hexStr2Byte(_commonKey)
  3085. );
  3086. let dataModularSM4HexString = byte2HexStr(dataModularSM4);
  3087. console.log(
  3088. "dataModularSM4String---------------------" + dataModularSM4HexString
  3089. );
  3090. let dataModularSM4Length = (dataModularSM4HexString.length / 2)
  3091. .toString(16)
  3092. .PadLeft(4, "0");
  3093. let resultData =
  3094. head + dataModularSM4Length + dataModularLength + dataModularSM4HexString;
  3095. console.log(
  3096. "加密前:" + head + dataModularSM4Length + dataModularLength + dataModular
  3097. );
  3098. let crcData = crc
  3099. .crc16kermit(hex2Ab(resultData))
  3100. .toString(16)
  3101. .PadLeft(4, "0");
  3102. resultData += crcData;
  3103. sendDataTolock(resultData);
  3104. staticCallback = callback;
  3105. }
  3106. /**
  3107. * rf卡添加非安全卡控制
  3108. * @param {*} _superAdminId
  3109. * @param {*} _userID
  3110. * @param {*} _keyId
  3111. * @param {*} _index
  3112. * @param {*} _signKey
  3113. * @param {*} _cardNo
  3114. * @param {*} _cardFlag
  3115. * @param {*} _commonKey
  3116. * @param {*} callback
  3117. */
  3118. static rFcardAddControl(
  3119. _superAdminId,
  3120. _userID,
  3121. _keyId,
  3122. _index,
  3123. _signKey,
  3124. _cardNo,
  3125. _cardFlag,
  3126. _commonKey,
  3127. callback
  3128. ) {
  3129. let cmdId = "3030";
  3130. let head = makeHeader("3030");
  3131. let subCmdId = "12";
  3132. let authuserID4AuthCode = str2Hex(_superAdminId);
  3133. let authUserId = authuserID4AuthCode.PadRight(40, "0");
  3134. let keyID4AuthCode = str2Hex(_keyId);
  3135. let keyId = keyID4AuthCode.PadRight(80, "0");
  3136. let userID4AuthCode = str2Hex(_userID);
  3137. let userId = userID4AuthCode.PadRight(40, "0");
  3138. let index = _index;
  3139. let cardNo = _cardNo;
  3140. let cardFlag = _cardFlag;
  3141. let reqData = authUserId + keyId + userId + index + cardNo + cardFlag;
  3142. let reqDataLength = (reqData.length / 2).toString(16).PadLeft(2, "0");
  3143. let dataModular = cmdId + subCmdId + reqDataLength + reqData;
  3144. let dataModularLength = (dataModular.length / 2)
  3145. .toString(16)
  3146. .PadLeft(4, "0");
  3147. let dataModularSM4 = sm4.sm4_enc(
  3148. hexStr2Byte(dataModular),
  3149. dataModular.length / 2,
  3150. hexStr2Byte(_commonKey)
  3151. );
  3152. let dataModularSM4HexString = byte2HexStr(dataModularSM4);
  3153. console.log(
  3154. "dataModularSM4String---------------------" + dataModularSM4HexString
  3155. );
  3156. let dataModularSM4Length = (dataModularSM4HexString.length / 2)
  3157. .toString(16)
  3158. .PadLeft(4, "0");
  3159. let resultData =
  3160. head + dataModularSM4Length + dataModularLength + dataModularSM4HexString;
  3161. console.log(
  3162. "加密前:" + head + dataModularSM4Length + dataModularLength + dataModular
  3163. );
  3164. let crcData = crc
  3165. .crc16kermit(hex2Ab(resultData))
  3166. .toString(16)
  3167. .PadLeft(4, "0");
  3168. resultData += crcData;
  3169. sendDataTolock(resultData);
  3170. staticCallback = callback;
  3171. }
  3172. /**
  3173. * *设置音量
  3174. * @param {门锁ID 最多40字节} _lockId
  3175. * @param {用户ID 最多20字节} _userId
  3176. * @param {声音选项(string) 00 静音 01 低音 02 高音} _volume
  3177. * @param {用户的commonKey} _commonKey
  3178. * @param {回调} callback
  3179. */
  3180. static setVolume(_lockId, _userId, _volume, _commonKey, callback) {
  3181. encKeySaved = _commonKey;
  3182. let cmdId = "3030";
  3183. let head = makeHeader("3030");
  3184. let lockId4AuthCode = str2Hex(_lockId);
  3185. let lockId = lockId4AuthCode.PadRight(80, "0");
  3186. let subCmdId = "0C";
  3187. let userId = str2Hex(_userId).PadRight(40, "0");
  3188. let reqData = lockId + userId + _volume;
  3189. let reqDataLength = (reqData.length / 2).toString(16).PadLeft(2, "0");
  3190. let dataModular = cmdId + subCmdId + reqDataLength + reqData;
  3191. let dataModularLength = (dataModular.length / 2)
  3192. .toString(16)
  3193. .PadLeft(4, "0");
  3194. let dataModularSM4 = sm4.sm4_enc(
  3195. hexStr2Byte(dataModular),
  3196. dataModular.length / 2,
  3197. hexStr2Byte(_commonKey)
  3198. );
  3199. let dataModularSM4HexString = byte2HexStr(dataModularSM4);
  3200. console.log(
  3201. "dataModularSM4String---------------------" + dataModularSM4HexString
  3202. );
  3203. let dataModularSM4Length = (dataModularSM4HexString.length / 2)
  3204. .toString(16)
  3205. .PadLeft(4, "0");
  3206. let resultData =
  3207. head + dataModularSM4Length + dataModularLength + dataModularSM4HexString;
  3208. console.log(
  3209. "加密前:" + head + dataModularSM4Length + dataModularLength + dataModular
  3210. );
  3211. let crcData = crc
  3212. .crc16kermit(hex2Ab(resultData))
  3213. .toString(16)
  3214. .PadLeft(4, "0");
  3215. resultData += crcData;
  3216. sendDataTolock(resultData);
  3217. staticCallback = callback;
  3218. }
  3219. /**
  3220. * *设置 NB 物联网关 IP
  3221. * @param {NB物联网关IP地址 String 20字节} _NbGwIp
  3222. * @param {端口号 String 6字节} _NbGwPort
  3223. * @param {超管的commonKey} _commonKey
  3224. * @param {回调函数} callback
  3225. */
  3226. static setIpOfNb(_NbGwIp, _NbGwPort, _commonKey, callback) {
  3227. console.log("设置 ip参数", [...arguments]);
  3228. encKeySaved = _commonKey;
  3229. let cmdId = "3030";
  3230. let head = makeHeader("3030");
  3231. let subCmdId = "01";
  3232. let NbGwIp = str2Hex(_NbGwIp).PadRight(40, "0");
  3233. let NbGwPort = str2Hex(_NbGwPort).PadRight(12, "0");
  3234. let reqData = NbGwIp + NbGwPort;
  3235. let reqDataLength = (reqData.length / 2).toString(16).PadLeft(2, "0");
  3236. let dataModular = cmdId + subCmdId + reqDataLength + reqData;
  3237. let dataModularLength = (dataModular.length / 2)
  3238. .toString(16)
  3239. .PadLeft(4, "0");
  3240. let dataModularSM4 = sm4.sm4_enc(
  3241. hexStr2Byte(dataModular),
  3242. dataModular.length / 2,
  3243. hexStr2Byte(_commonKey)
  3244. );
  3245. let dataModularSM4HexString = byte2HexStr(dataModularSM4);
  3246. console.log(
  3247. "dataModularSM4String---------------------" + dataModularSM4HexString
  3248. );
  3249. let dataModularSM4Length = (dataModularSM4HexString.length / 2)
  3250. .toString(16)
  3251. .PadLeft(4, "0");
  3252. let resultData =
  3253. head + dataModularSM4Length + dataModularLength + dataModularSM4HexString;
  3254. console.log(
  3255. "加密前:" + head + dataModularSM4Length + dataModularLength + dataModular
  3256. );
  3257. let crcData = crc
  3258. .crc16kermit(hex2Ab(resultData))
  3259. .toString(16)
  3260. .PadLeft(4, "0");
  3261. resultData += crcData;
  3262. sendDataTolock(resultData);
  3263. staticCallback = callback;
  3264. }
  3265. //读取 NB IMEI 号码
  3266. // Todo: 回复02 密码错误 暂不使用此指令
  3267. static getNbImeiNumber(_lockId, _commonKey, callback) {
  3268. encKeySaved = _commonKey;
  3269. let cmdId = "3030";
  3270. let head = makeHeader("3030");
  3271. let subCmdId = "02";
  3272. let lockId = str2Hex(_lockId).PadRight(80, "0");
  3273. let reqData = lockId;
  3274. let reqDataLength = (reqData.length / 2).toString(16).PadLeft(2, "0");
  3275. let dataModular = cmdId + subCmdId + reqDataLength + reqData;
  3276. let dataModularLength = (dataModular.length / 2)
  3277. .toString(16)
  3278. .PadLeft(4, "0");
  3279. let dataModularSM4 = sm4.sm4_enc(
  3280. hexStr2Byte(dataModular),
  3281. dataModular.length / 2,
  3282. hexStr2Byte(_commonKey)
  3283. );
  3284. let dataModularSM4HexString = byte2HexStr(dataModularSM4);
  3285. console.log(
  3286. "dataModularSM4String---------------------" + dataModularSM4HexString
  3287. );
  3288. let dataModularSM4Length = (dataModularSM4HexString.length / 2)
  3289. .toString(16)
  3290. .PadLeft(4, "0");
  3291. let resultData =
  3292. head + dataModularSM4Length + dataModularLength + dataModularSM4HexString;
  3293. console.log(
  3294. "加密前:" + head + dataModularSM4Length + dataModularLength + dataModular
  3295. );
  3296. let crcData = crc
  3297. .crc16kermit(hex2Ab(resultData))
  3298. .toString(16)
  3299. .PadLeft(4, "0");
  3300. resultData += crcData;
  3301. sendDataTolock(resultData);
  3302. staticCallback = callback;
  3303. }
  3304. /**
  3305. * *设置 WIFI AP 信息
  3306. * @param {wifi的名称} _wifiApSsid
  3307. * @param {wifi密码} _wifiApPwd
  3308. * @param {超管的commonKey} _commonKey
  3309. * @param {回调} callback
  3310. */
  3311. static setWifiApInfo(_wifiApSsid, _wifiApPwd, _commonKey, callback) {
  3312. console.log("设置wifi信息", [...arguments]);
  3313. encKeySaved = _commonKey;
  3314. let cmdId = "3030";
  3315. let head = makeHeader("3030");
  3316. let subCmdId = "13";
  3317. let wifiApSsid = str2Hex(_wifiApSsid).PadRight(64, "0");
  3318. let wifiApPwd = str2Hex(_wifiApPwd).PadRight(40, "0");
  3319. let reqData = wifiApSsid + wifiApPwd;
  3320. let reqDataLength = (reqData.length / 2).toString(16).PadLeft(2, "0");
  3321. let dataModular = cmdId + subCmdId + reqDataLength + reqData;
  3322. let dataModularLength = (dataModular.length / 2)
  3323. .toString(16)
  3324. .PadLeft(4, "0");
  3325. let dataModularSM4 = sm4.sm4_enc(
  3326. hexStr2Byte(dataModular),
  3327. dataModular.length / 2,
  3328. hexStr2Byte(_commonKey)
  3329. );
  3330. let dataModularSM4HexString = byte2HexStr(dataModularSM4);
  3331. console.log(
  3332. "dataModularSM4String---------------------" + dataModularSM4HexString
  3333. );
  3334. let dataModularSM4Length = (dataModularSM4HexString.length / 2)
  3335. .toString(16)
  3336. .PadLeft(4, "0");
  3337. let resultData =
  3338. head + dataModularSM4Length + dataModularLength + dataModularSM4HexString;
  3339. console.log(
  3340. "加密前:" + head + dataModularSM4Length + dataModularLength + dataModular
  3341. );
  3342. let crcData = crc
  3343. .crc16kermit(hex2Ab(resultData))
  3344. .toString(16)
  3345. .PadLeft(4, "0");
  3346. resultData += crcData;
  3347. sendDataTolock(resultData);
  3348. staticCallback = callback;
  3349. }
  3350. // 用户权限移交
  3351. // Todo: 移交权限signkey
  3352. static transferAuth(
  3353. _lockId,
  3354. _transferFromUserId,
  3355. _transferToUserId,
  3356. _transferFromkeyId,
  3357. _transferFromUserRole,
  3358. _transferToUserRole,
  3359. _signKey,
  3360. _commonKey,
  3361. callback
  3362. ) {
  3363. encKeySaved = _commonKey;
  3364. let head = makeHeader("3012");
  3365. let cmdId = "3012";
  3366. let lockId = str2Hex(_lockId).PadRight(80, "0");
  3367. let authuserID4AuthCode = str2Hex(_transferFromUserId);
  3368. let authUserId = authuserID4AuthCode.PadRight(40, "0");
  3369. let keyID4AuthCode = str2Hex(_transferFromkeyId);
  3370. let keyId = keyID4AuthCode.PadRight(80, "0");
  3371. let userID4AuthCode = str2Hex(_transferToUserId);
  3372. let userId = userID4AuthCode.PadRight(40, "0");
  3373. let authUserNewRole = _transferFromUserRole;
  3374. let userNewRole = _transferToUserRole;
  3375. let token = _lockId.substring(4, 12);
  3376. let authCode = ab2hex(
  3377. md5.digest(
  3378. hex2Ab(
  3379. authuserID4AuthCode +
  3380. keyID4AuthCode +
  3381. userID4AuthCode +
  3382. token +
  3383. _signKey
  3384. )
  3385. )
  3386. );
  3387. let authCodeLen = (authCode.length / 2).toString(16).PadLeft(2, "0");
  3388. let dataModular =
  3389. cmdId +
  3390. lockId +
  3391. authUserId +
  3392. keyId +
  3393. userId +
  3394. authUserNewRole +
  3395. userNewRole +
  3396. token +
  3397. authCodeLen +
  3398. authCode;
  3399. let dataModularLength = (dataModular.length / 2)
  3400. .toString(16)
  3401. .PadLeft(4, "0");
  3402. let dataModularSM4 = sm4.sm4_enc(
  3403. hexStr2Byte(dataModular),
  3404. dataModular.length / 2,
  3405. hexStr2Byte(_commonKey)
  3406. );
  3407. let dataModularSM4HexString = byte2HexStr(dataModularSM4);
  3408. console.log(
  3409. "dataModularSM4String---------------------" + dataModularSM4HexString
  3410. );
  3411. let dataModularSM4Length = (dataModularSM4HexString.length / 2)
  3412. .toString(16)
  3413. .PadLeft(4, "0");
  3414. let resultData =
  3415. head + dataModularSM4Length + dataModularLength + dataModularSM4HexString;
  3416. console.log(
  3417. "加密前:" + head + dataModularSM4Length + dataModularLength + dataModular
  3418. );
  3419. let crcData = crc
  3420. .crc16kermit(hex2Ab(resultData))
  3421. .toString(16)
  3422. .PadLeft(4, "0");
  3423. resultData += crcData;
  3424. sendDataTolock(resultData);
  3425. staticCallback = callback;
  3426. }
  3427. /**
  3428. * 获取锁端私钥
  3429. * @param {门锁ID 最多40字节} _lockId
  3430. * @param {通讯密钥} _commonKey
  3431. * @param {回调} callback
  3432. */
  3433. static getPirvateKey(_lockId, _commonKey, callback) {
  3434. encKeySaved = _commonKey;
  3435. let cmdId = "3030";
  3436. let head = makeHeader("3030");
  3437. let subCmdId = "15";
  3438. let lockId = str2Hex(_lockId).PadRight(80, "0");
  3439. let reqData = lockId;
  3440. let reqDataLength = (reqData.length / 2).toString(16).PadLeft(2, "0");
  3441. let dataModular = cmdId + subCmdId + reqDataLength + reqData;
  3442. let dataModularLength = (dataModular.length / 2)
  3443. .toString(16)
  3444. .PadLeft(4, "0");
  3445. let dataModularSM4 = sm4.sm4_enc(
  3446. hexStr2Byte(dataModular),
  3447. dataModular.length / 2,
  3448. hexStr2Byte(_commonKey)
  3449. );
  3450. let dataModularSM4HexString = byte2HexStr(dataModularSM4);
  3451. console.log(
  3452. "dataModularSM4String---------------------" + dataModularSM4HexString
  3453. );
  3454. let dataModularSM4Length = (dataModularSM4HexString.length / 2)
  3455. .toString(16)
  3456. .PadLeft(4, "0");
  3457. let resultData =
  3458. head + dataModularSM4Length + dataModularLength + dataModularSM4HexString;
  3459. console.log(
  3460. "加密前:" + head + dataModularSM4Length + dataModularLength + dataModular
  3461. );
  3462. let crcData = crc
  3463. .crc16kermit(hex2Ab(resultData))
  3464. .toString(16)
  3465. .PadLeft(4, "0");
  3466. resultData += crcData;
  3467. sendDataTolock(resultData);
  3468. staticCallback = callback;
  3469. }
  3470. /**
  3471. * 添加五组密钥
  3472. * @param {门锁ID 最多40字节} _lockId
  3473. * @param {写入时间-8位timestamp字符串 eg: "00000000"} _writeTime
  3474. * @param {是否写入通讯密钥'01'写入'00'不写入} _isComKey
  3475. * @param {通讯密钥} _comKey
  3476. * @param {是否写入园区密钥'01'写入'00'不写入} _isParkKey
  3477. * @param {园区密钥} _parkKey
  3478. * @param {是否写入楼号密钥'01'写入'00'不写入} _isTowerKey
  3479. * @param {楼号密钥} _towerKey
  3480. * @param {是否写入层号密钥'01'写入'00'不写入} _isFloorKey
  3481. * @param {层号密钥} _floorKey
  3482. * @param {是否写入同层区域密钥'01'写入'00'不写入} _isAreaKey
  3483. * @param {同层区域密钥} _areaKey
  3484. * @param {通信密钥} _commonKey
  3485. */
  3486. static addFiveKeys(
  3487. _lockId,
  3488. _writeTime,
  3489. _isComKey,
  3490. _comKey,
  3491. _isParkKey,
  3492. _parkKey,
  3493. _isTowerKey,
  3494. _towerKey,
  3495. _isFloorKey,
  3496. _floorKey,
  3497. _isAreaKey,
  3498. _areaKey,
  3499. _commonKey,
  3500. callback
  3501. ) {
  3502. encKeySaved = _commonKey;
  3503. let cmdId = "3030";
  3504. let head = makeHeader("3030");
  3505. let subCmdId = "14";
  3506. let lockId = str2Hex(_lockId).PadRight(80, "0");
  3507. let comKey = _comKey.PadRight(32, "0");
  3508. let parkKey = _parkKey.PadRight(32, "0");
  3509. let towerKey = _towerKey.PadRight(32, "0");
  3510. let floorKey = _floorKey.PadRight(32, "0");
  3511. let areaKey = _areaKey.PadRight(32, "0");
  3512. let reqData =
  3513. lockId +
  3514. _writeTime +
  3515. _isComKey +
  3516. comKey +
  3517. _isParkKey +
  3518. parkKey +
  3519. _isTowerKey +
  3520. towerKey +
  3521. _isFloorKey +
  3522. floorKey +
  3523. _isAreaKey +
  3524. areaKey;
  3525. let reqDataLength = (reqData.length / 2).toString(16).PadLeft(2, "0");
  3526. let dataModular = cmdId + subCmdId + reqDataLength + reqData;
  3527. let dataModularLength = (dataModular.length / 2)
  3528. .toString(16)
  3529. .PadLeft(4, "0");
  3530. let dataModularSM4 = sm4.sm4_enc(
  3531. hexStr2Byte(dataModular),
  3532. dataModular.length / 2,
  3533. hexStr2Byte(_commonKey)
  3534. );
  3535. let dataModularSM4HexString = byte2HexStr(dataModularSM4);
  3536. console.log(
  3537. "dataModularSM4String---------------------" + dataModularSM4HexString
  3538. );
  3539. let dataModularSM4Length = (dataModularSM4HexString.length / 2)
  3540. .toString(16)
  3541. .PadLeft(4, "0");
  3542. let resultData =
  3543. head + dataModularSM4Length + dataModularLength + dataModularSM4HexString;
  3544. console.log(
  3545. "加密前:" + head + dataModularSM4Length + dataModularLength + dataModular
  3546. );
  3547. let crcData = crc
  3548. .crc16kermit(hex2Ab(resultData))
  3549. .toString(16)
  3550. .PadLeft(4, "0");
  3551. resultData += crcData;
  3552. sendDataTolock(resultData);
  3553. staticCallback = callback;
  3554. }
  3555. /**
  3556. * NBIOT通道转发
  3557. * @param {*} _nbIotCmd
  3558. * @param {*} _commonKey
  3559. */
  3560. static nbIotTransmit(_nbIotCmd, _commonKey, callback) {
  3561. encKeySaved = _commonKey;
  3562. let head = makeHeader('3095');
  3563. let cmdId = "3095";
  3564. let dataModular = cmdId + _nbIotCmd;
  3565. let dataModularLength = (dataModular.length / 2).toString(16).PadLeft(4, '0');
  3566. let dataModularSM4 = sm4.sm4_enc(hexStr2Byte(dataModular), dataModular.length / 2, hexStr2Byte(_commonKey));
  3567. let dataModularSM4HexString = byte2HexStr(dataModularSM4);
  3568. console.log('dataModularSM4String---------------------' + dataModularSM4HexString)
  3569. let dataModularSM4Length = (dataModularSM4HexString.length / 2).toString(16).PadLeft(4, '0');
  3570. let resultData = head + dataModularSM4Length + dataModularLength + dataModularSM4HexString;
  3571. console.log('加密前:' + head + dataModularSM4Length + dataModularLength + dataModular)
  3572. let crcData = (crc.crc16kermit(hexStr2Byte(resultData)).toString(16)).PadLeft(4, '0');
  3573. resultData += crcData;
  3574. sendDataTolock(resultData);
  3575. staticCallback = callback;
  3576. }
  3577. /**
  3578. * 禁用/解禁离线卡
  3579. * @param {锁ID} _lockId
  3580. * @param {8位字符串 eg:parseInt(new Date().getTime() / 1000).toString(16)} _writeTime
  3581. * @param {禁用卡ID} _rfCardId
  3582. * @param {String类型 '0': 解禁 '1': 禁用} _banOrLift
  3583. * @param {用户的commonKey} _commonKey
  3584. * @param {回调} callback
  3585. */
  3586. static banOrLiftCard(_lockId, _writeTime, _rfCardId, _banOrLift, _commonKey, callback) {
  3587. console.log('解禁、禁用离线卡参数为' + [...arguments])
  3588. let cmdId = "3030";
  3589. let head = makeHeader("3030");
  3590. let subCmdId = "17";
  3591. let lockId = str2Hex(_lockId).PadRight(80, "0");
  3592. let writeTime = _writeTime;
  3593. let rfCardId = _rfCardId;
  3594. let rfCmdId;
  3595. if (_banOrLift == '0') {
  3596. // 解禁
  3597. rfCmdId = 'ee'
  3598. } else if (_banOrLift == '1') {
  3599. // 禁用
  3600. rfCmdId = '55'
  3601. }
  3602. // else {
  3603. // staticCallback = callback({
  3604. // cmdId: cmdId,
  3605. // status: '99',
  3606. // statusReason: '参数错误'
  3607. // })
  3608. // return;
  3609. // }
  3610. let reqData = lockId + writeTime + rfCardId + rfCmdId;
  3611. let reqDataLength = (reqData.length / 2).toString(16).PadLeft(2, "0");
  3612. let dataModular = cmdId + subCmdId + reqDataLength + reqData;
  3613. let dataModularLength = (dataModular.length / 2).toString(16).PadLeft(4, '0');
  3614. let dataModularSM4 = sm4.sm4_enc(hexStr2Byte(dataModular), dataModular.length / 2, hexStr2Byte(_commonKey));
  3615. let dataModularSM4HexString = byte2HexStr(dataModularSM4);
  3616. console.log('dataModularSM4String---------------------' + dataModularSM4HexString)
  3617. let dataModularSM4Length = (dataModularSM4HexString.length / 2).toString(16).PadLeft(4, '0');
  3618. let resultData = head + dataModularSM4Length + dataModularLength + dataModularSM4HexString;
  3619. console.log('加密前:' + head + dataModularSM4Length + dataModularLength + dataModular);
  3620. let crcData = (crc.crc16kermit(hexStr2Byte(resultData)).toString(16)).PadLeft(4, '0');
  3621. resultData += crcData;
  3622. sendDataTolock(resultData);
  3623. staticCallback = callback;
  3624. }
  3625. /**
  3626. * 获取锁当前用户信息
  3627. * TODO 鉴权方式
  3628. * @param {*} _lockId
  3629. * @param {*} _superAdminId
  3630. * @param {*} _userIndex
  3631. * @param {*} _tokenOfGetUserInfo
  3632. * @param {*} _commonKey
  3633. * @param {*} callback
  3634. */
  3635. static getUserInfo(_lockId, _superAdminId, _userIndex, _tokenOfGetUserInfo, _commonKey, callback) {
  3636. let cmdId = "3007";
  3637. let head = makeHeader("3007");
  3638. let lockId = str2Hex(_lockId).PadLeft(80, '0');
  3639. let userId = str2Hex(_userId).PadLeft(40, '0');
  3640. let userIndex = parseInt(_userIndex);
  3641. let token = "00000000";
  3642. let authCode = "";
  3643. let authCodeLen = "00";
  3644. if (_tokenOfGetUserInfo != "") {
  3645. token = _tokenOfGetUserInfo;
  3646. authCode = ab2hex(
  3647. md5.digest(
  3648. hexStr2Byte(lockId + userId + token + _commonKey)
  3649. )
  3650. );
  3651. authCodeLen = (authCode.length / 2).toString(16).PadLeft(2, "0");
  3652. }
  3653. let dataModular = cmdId + lockId + userId + userIndex + token + authCodeLen + authCode;
  3654. let dataModularLength = (dataModular.length / 2).toString(16).PadLeft(4, "0");
  3655. let dataModularSM4 = sm4.sm4_enc(hexStr2Byte(dataModular), dataModular.length / 2, hexStr2Byte(_commonKey));
  3656. let dataModularSM4HexString = byte2HexStr(dataModularSM4);
  3657. console.log("dataModularSM4String---------------------" + dataModularSM4HexString);
  3658. let dataModularSM4Length = (dataModularSM4HexString.length / 2).toString(16).PadLeft(4, "0");
  3659. let resultData = head + dataModularSM4Length + dataModularLength + dataModularSM4HexString;
  3660. console.log("加密前:" + head + dataModularSM4Length + dataModularLength + dataModular);
  3661. let crcData = crc.crc16kermit(hex2Ab(resultData)).toString(16).PadLeft(4, "0");
  3662. resultData += crcData;
  3663. sendDataTolock(resultData);
  3664. staticCallback = callback;
  3665. }
  3666. }
  3667. String.prototype.PadLeft = function (len, charStr) {
  3668. var s = this + "";
  3669. console.log("3794", s);
  3670. return new Array(len - s.length + 1).join(charStr, "") + s;
  3671. };
  3672. String.prototype.PadRight = function (len, charStr) {
  3673. var s = this + "";
  3674. return s + new Array(len - s.length + 1).join(charStr, "");
  3675. };
  3676. /**
  3677. * String转16进制
  3678. * @param {字符串} str
  3679. */
  3680. function str2Hex(str) {
  3681. var val = "";
  3682. for (var i = 0; i < str.length; i++) {
  3683. if (val == "") val = str.charCodeAt(i).toString(16);
  3684. else val += str.charCodeAt(i).toString(16);
  3685. }
  3686. return val;
  3687. }
  3688. /**
  3689. * 16进制转String
  3690. */
  3691. function hexToString(hex) {
  3692. var arr = hex.split("");
  3693. var out = "";
  3694. for (var i = 0; i < arr.length / 2; i++) {
  3695. var tmp = "0x" + arr[i * 2] + arr[i * 2 + 1];
  3696. var charValue = String.fromCharCode(tmp);
  3697. out += charValue;
  3698. }
  3699. return out;
  3700. }
  3701. function ab2hex(buffer) {
  3702. var hexArr = Array.prototype.map.call(new Uint8Array(buffer), function (bit) {
  3703. return ("00" + bit.toString(16)).slice(-2);
  3704. });
  3705. return hexArr.join("");
  3706. }
  3707. function getIIData() {
  3708. let ii = 0;
  3709. for (let i = 0; i < 8; i++) {
  3710. ii = 0x11 + i * i;
  3711. }
  3712. return ii;
  3713. }
  3714. //分包
  3715. function datasubpck(datahex) {
  3716. var arrayObj = new Array();
  3717. let dataPackageLength = packageSize
  3718. console.log('分包大小', dataPackageLength, datahex)
  3719. for (var i = 0; i < datahex.length; i += dataPackageLength) {
  3720. // 预加 最大包长度,如果依然小于总数据长度,可以取最大包数据大小
  3721. if (i + dataPackageLength < datahex.length) {
  3722. arrayObj.push(datahex.substring(i, i + dataPackageLength));
  3723. } else {
  3724. arrayObj.push(datahex.substring(i));
  3725. }
  3726. }
  3727. return arrayObj;
  3728. }
  3729. /**
  3730. * 向锁端写入数据
  3731. * @param {*} connectedDeviceId
  3732. * @param {*} writeServicweId
  3733. * @param {*} writeCharacteristicsId
  3734. * @param {*} dataArray
  3735. * @param {*} index
  3736. */
  3737. var totalLength = 0;
  3738. function deviceWrite(
  3739. connectedDeviceId,
  3740. writeServicweId,
  3741. writeCharacteristicsId,
  3742. dataArray,
  3743. index
  3744. ) {
  3745. totalLength = dataArray.length;
  3746. var subhex = "";
  3747. subhex = dataArray[index];
  3748. index = index + 1;
  3749. var bufferSuplied = hex2Ab(subhex);
  3750. try {
  3751. wx.writeBLECharacteristicValue({
  3752. // 这里的 deviceId 需要在上面的 getBluetoothDevices 或 onBluetoothDeviceFound 接口中获取
  3753. deviceId: connectedDeviceId,
  3754. // 这里的 serviceId 需要在上面的 getBLEDeviceServices 接口中获取
  3755. serviceId: writeServicweId,
  3756. // 这里的 characteristicId 需要在上面的 getBLEDeviceCharacteristics 接口中获取
  3757. characteristicId: writeCharacteristicsId,
  3758. value: bufferSuplied,
  3759. success: function (res) {
  3760. //递归发送
  3761. if (index < totalLength) {
  3762. deviceWrite(
  3763. connectedDeviceId,
  3764. writeServicweId,
  3765. writeCharacteristicsId,
  3766. dataArray,
  3767. index
  3768. );
  3769. }
  3770. },
  3771. fail: function (res) {
  3772. console.log("写入失败", res);
  3773. //如果写入事变
  3774. ICINLocker.connect(connectedDeviceId);
  3775. throw new Error(res);
  3776. },
  3777. });
  3778. } catch (e) {
  3779. throw e;
  3780. }
  3781. }
  3782. function calculateAuthKey(_superAdminId, _keyId, _userId, _signKey) {
  3783. return ab2hex(
  3784. md5.digest(hex2Ab(_superAdminId + _keyId + _userId + _signkey))
  3785. );
  3786. }
  3787. /**
  3788. * 向锁发送数据
  3789. * @param {*} _hexData
  3790. */
  3791. function sendDataTolock(_hexData) {
  3792. var receData = "";
  3793. wx.setStorage({
  3794. data: receData,
  3795. key: "receData",
  3796. });
  3797. var hex = _hexData;
  3798. console.log("发送的指令" + hex);
  3799. var connectedDeviceId = wx.getStorageSync("connectedDeviceId");
  3800. var writeServicweId = wx.getStorageSync("writeServicweId");
  3801. var writeCharacteristicsId = wx.getStorageSync("writeCharacteristicsId");
  3802. var dataArray = datasubpck(hex);
  3803. deviceWrite(
  3804. connectedDeviceId,
  3805. writeServicweId,
  3806. writeCharacteristicsId,
  3807. dataArray,
  3808. 0
  3809. );
  3810. }
  3811. function makeHeader(type) {
  3812. let normalHead = "EF01EE02";
  3813. let headType = "01";
  3814. let headNo = "0001";
  3815. var headSta = "";
  3816. if (type == "300a") {
  3817. headSta = "10";
  3818. } else if (type == "3093" || type == "3092" || type == "3094" || type == "3010" || type == "3011") {
  3819. headSta = "12";
  3820. } else {
  3821. headSta = "13";
  3822. }
  3823. let result = normalHead + headType + headNo + headSta;
  3824. return result;
  3825. }
  3826. function isLockID(value) {
  3827. var rule = "^ICIN_[\\da-zA-Z]{12}$";
  3828. var re = new RegExp(rule);
  3829. if (re.test(value)) {
  3830. return true;
  3831. } else {
  3832. return false;
  3833. }
  3834. }
  3835. function str2Ascii(data) {
  3836. var result = "";
  3837. for (var i = data.length - 1; i >= 0; i--) {
  3838. var str = data.charAt(i);
  3839. var code = str.charCodeAt();
  3840. result += code;
  3841. }
  3842. return result;
  3843. }
  3844. function hex2Ab(value) {
  3845. var typedArray = new Uint8Array(
  3846. value.match(/[\da-f]{2}/gi).map(function (h) {
  3847. return parseInt(h, 16);
  3848. })
  3849. );
  3850. return typedArray.buffer;
  3851. }
  3852. function hexStr2Byte(hexStr) {
  3853. var pos = 0;
  3854. var len = hexStr.length;
  3855. if (len % 2 !== 0) {
  3856. return null;
  3857. }
  3858. len /= 2;
  3859. var hexA = [];
  3860. for (var i = 0; i < len; i++) {
  3861. var s = hexStr.substr(pos, 2);
  3862. var v = parseInt(s, 16);
  3863. hexA.push(v);
  3864. pos += 2;
  3865. }
  3866. return hexA;
  3867. }
  3868. function ip2hex(_ip) {
  3869. var REG =
  3870. /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/;
  3871. var xH = "",
  3872. result = REG.exec(_ip);
  3873. if (!result) return -1;
  3874. for (var i = 1; i <= 4; i++) {
  3875. var h = parseInt(result[i]);
  3876. xH += (h > 15 ? "" : "0") + h.toString(16);
  3877. }
  3878. return parseInt(xH, 16).toString();
  3879. }
  3880. function timeConvert(timestamp, num) {
  3881. //num:0 YYYY-MM-DD num:1 YYYY-MM-DD hh:mm:ss // timestamp:时间戳
  3882. timestamp = timestamp + "";
  3883. timestamp = timestamp * 1000;
  3884. var date = new Date(timestamp);
  3885. var y = date.getFullYear();
  3886. var m = date.getMonth() + 1;
  3887. m = m < 10 ? "0" + m : m;
  3888. var d = date.getDate();
  3889. d = d < 10 ? "0" + d : d;
  3890. var h = date.getHours();
  3891. h = h < 10 ? "0" + h : h;
  3892. var minute = date.getMinutes();
  3893. var second = date.getSeconds();
  3894. minute = minute < 10 ? "0" + minute : minute;
  3895. second = second < 10 ? "0" + second : second;
  3896. if (num == 0) {
  3897. return y + "-" + m + "-" + d;
  3898. } else {
  3899. return y + "-" + m + "-" + d + " " + h + ":" + minute + ":" + second;
  3900. }
  3901. }
  3902. // 16进制数转10进制
  3903. var ex16hex = function (value) {
  3904. value = stripscript(value);
  3905. value = value.replace("0x", "");
  3906. var arr = value.split("");
  3907. arr = arr.reverse();
  3908. var len = arr.length;
  3909. var res = 0;
  3910. $.each(arr, function (i, v) {
  3911. var num = hex_change(v);
  3912. console.log(num);
  3913. res += muti16(num, i);
  3914. });
  3915. return res;
  3916. };
  3917. function hex2int(hex) {
  3918. var len = hex.length,
  3919. a = new Array(len),
  3920. code;
  3921. for (var i = 0; i < len; i++) {
  3922. code = hex.charCodeAt(i);
  3923. if (48 <= code && code < 58) {
  3924. code -= 48;
  3925. } else {
  3926. code = (code & 0xdf) - 65 + 10;
  3927. }
  3928. a[i] = code;
  3929. }
  3930. return a.reduce(function (acc, c) {
  3931. acc = 16 * acc + c;
  3932. return acc;
  3933. }, 0);
  3934. }
  3935. function byte2HexStr(byteArray) {
  3936. if (byteArray) {
  3937. var str = "";
  3938. for (var i = 0; i < byteArray.length; i++) {
  3939. var tmp = byteArray[i].toString(16);
  3940. if (tmp.length === 1) {
  3941. tmp = "0" + tmp;
  3942. }
  3943. str += tmp;
  3944. }
  3945. return str;
  3946. } else {
  3947. return null;
  3948. }
  3949. }