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.

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