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.

4091 lines
156 KiB

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