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.

806 lines
27 KiB

1 year ago
  1. const config = require('../config')
  2. const util = require('./util1.js')
  3. const plugin = requirePlugin("myPlugin")
  4. var isConnected = false
  5. var basecode = 29283991
  6. class LwJ{
  7. constructor(lockDevice,isConnected,connectedDeviceId,isInitReady,lockModel){
  8. this.isConnected = isConnected
  9. this.lockDevice = lockDevice
  10. this.connectedDeviceId = connectedDeviceId
  11. this.isInitReady = isInitReady
  12. this.lockModel = lockModel
  13. this.taskId = 0
  14. this.isJack = false
  15. }
  16. getData(){
  17. return {
  18. isConnected:this.isConnected,
  19. isConnected:this.lockDevice,
  20. isConnected:this.connectedDeviceId,
  21. isConnected:this.isInitReady,
  22. isConnected:this.lockModel,
  23. isConnected:this.taskId
  24. }
  25. }
  26. initBluetooth () {
  27. var that = this
  28. wx.getConnectedBluetoothDevices({
  29. services: [config.uuid.serviceUuid],
  30. success: function (res) {
  31. res.devices.map(function (it) {
  32. if (it.deviceId == connectedDeviceId || it.name == name) {
  33. this.isConnected = true
  34. }
  35. })
  36. // that.setData({
  37. // isConnected: this.isConnected
  38. // })
  39. console.log('isConnected', isConnected)
  40. if (isConnected) {
  41. if (that.isInitReady) {
  42. that.doWork()
  43. } else {
  44. that.setupConnection()
  45. }
  46. } else {
  47. that.connectTo()
  48. }
  49. },
  50. })
  51. }
  52. connectTo () {
  53. var that = this
  54. wx.showLoading({
  55. title: '连接中',
  56. })
  57. wx.createBLEConnection({
  58. deviceId: this.lockDevice.deviceId,
  59. timeout: config.ble.connectTimeOut,
  60. success: function (res) {
  61. console.debug('connectTo', res)
  62. isConnected = true
  63. wx.hideLoading()
  64. setTimeout(function () {
  65. that.setupConnection()
  66. }, 500)
  67. },
  68. fail: function (e) {
  69. console.debug('connectTo', e)
  70. wx.hideLoading()
  71. switch (e.errCode) {
  72. case 10003: {
  73. wx.showModal({
  74. title: '连接失败',
  75. content: '是否重新连接?',
  76. success: function (res) {
  77. if (res.confirm) {
  78. that.connectTo()
  79. }
  80. }
  81. })
  82. break
  83. }
  84. case 10012: {
  85. wx.showModal({
  86. title: '连接失败',
  87. content: '蓝牙连接超时',
  88. showCancel: false
  89. })
  90. break
  91. }
  92. case 10002: {
  93. wx.showModal({
  94. title: '连接失败',
  95. content: '没有找到指定设备',
  96. showCancel: false
  97. })
  98. break
  99. }
  100. }
  101. }
  102. })
  103. }
  104. setupConnection () {
  105. var that = this
  106. wx.showLoading({
  107. title: '初始化连接中',
  108. })
  109. wx.getBLEDeviceServices({
  110. deviceId: that.lockDevice.deviceId,
  111. success: function (res) {
  112. console.log('Services', res)
  113. wx.getBLEDeviceCharacteristics({
  114. deviceId: that.lockDevice.deviceId,
  115. serviceId: config.uuid.serviceUuid.toUpperCase(),
  116. success: function (res) {
  117. console.log('Characteristics', res)
  118. wx.onBLECharacteristicValueChange(function (res) {
  119. var bytes = new Uint8Array(res.value)
  120. console.log('bytes', bytes)
  121. var data = plugin.parseBytes(that.lockDevice.name, basecode, bytes)
  122. console.log('data', data)
  123. switch (data.cmd) {
  124. case 'reportLockBattery': {
  125. if (data.data.battery < 10) {
  126. wx.showModal({
  127. title: '电量报警',
  128. content: `门锁当前电量${data.data.battery}%!\n电量严重不足,请立即更换电池!`,
  129. showCancel: false
  130. })
  131. } else if (data.data.battery < 30) {
  132. wx.showModal({
  133. title: '电量预警',
  134. content: `门锁当前电量${data.data.battery}%!\n请及时更换电池!`,
  135. showCancel: false
  136. })
  137. }
  138. break
  139. }
  140. case 'reportRfCardResult': {
  141. if (that.taskId == 61) {
  142. wx.showModal({
  143. title: '检测到新卡',
  144. content: `是否添加此房卡,卡号:${data.data.cardId}`,
  145. success: function (res) {
  146. if (res.confirm) {
  147. rfCardId = data.data.cardId
  148. that.onAddRfCard()
  149. }
  150. }
  151. })
  152. } else {
  153. if (that.lockModel > 70) {
  154. if (data.data.isValid) {
  155. wx.showModal({
  156. title: '提示',
  157. content: `房卡开锁成功,序号:${data.data.index}`,
  158. showCancel: false
  159. })
  160. } else {
  161. wx.showModal({
  162. title: '提示',
  163. content: `多次刷房卡开锁失败!`,
  164. showCancel: false
  165. })
  166. }
  167. } else {
  168. if (data.data.isValid) {
  169. wx.showModal({
  170. title: '提示',
  171. content: `房卡开锁成功,卡号:${data.data.cardId}`,
  172. showCancel: false
  173. })
  174. } else {
  175. wx.showModal({
  176. title: '提示',
  177. content: `房卡未授权,卡号:${data.data.cardId}`,
  178. showCancel: false
  179. })
  180. }
  181. }
  182. }
  183. break
  184. }
  185. case 'reportPincodeResult': {
  186. if (that.lockModel > 70) {
  187. if (data.data.isValid) {
  188. wx.showModal({
  189. title: '提示',
  190. content: `密码开锁成功,序号:${data.data.index}`,
  191. showCancel: false
  192. })
  193. } else {
  194. wx.showModal({
  195. title: '提示',
  196. content: `多次密码开锁失败!`,
  197. showCancel: false
  198. })
  199. }
  200. } else {
  201. if (data.data.isValid) {
  202. wx.showModal({
  203. title: '提示',
  204. content: `密码:${data.data.pincode}开锁成功!`,
  205. showCancel: false
  206. })
  207. } else {
  208. wx.showModal({
  209. title: '提示',
  210. content: `密码:${data.data.pincode}未授权!`,
  211. showCancel: false
  212. })
  213. }
  214. }
  215. break
  216. }
  217. case 'reportFingerprintResult': {
  218. if (data.data.isValid) {
  219. wx.showModal({
  220. title: '提示',
  221. content: `指纹开锁成功,序号:${data.data.index}`,
  222. showCancel: false
  223. })
  224. } else {
  225. wx.showModal({
  226. title: '提示',
  227. content: `多次指纹开锁失败!`,
  228. showCancel: false
  229. })
  230. }
  231. break
  232. }
  233. case 'queryLockState': {
  234. wx.hideLoading()
  235. if (data.code != 200) {
  236. wx.showModal({
  237. title: '提示',
  238. content: '查询失败!',
  239. showCancel: false,
  240. success: function (res) {}
  241. })
  242. break
  243. }
  244. // that.setData({
  245. // lock: {
  246. // mac: data.data.mac,
  247. // lockType: util.getLockType(that.lockModel),
  248. // },
  249. // isBindLock: data.data.isBind
  250. // })
  251. // lockMac = data.data.mac
  252. if (that.taskId > 0) {
  253. if (data.data.isBind) {
  254. wx.showModal({
  255. title: data.data.mac,
  256. content: '此设备已经硬件绑定!',
  257. showCancel: false,
  258. success: function (res) {}
  259. })
  260. } else {
  261. wx.showModal({
  262. title: data.data.mac,
  263. content: '此设备硬件未绑定!',
  264. showCancel: false,
  265. success: function (res) {}
  266. })
  267. }
  268. }
  269. that.taskId = 0
  270. break
  271. }
  272. case 'queryLockBattery': {
  273. wx.hideLoading()
  274. if (data.code != 200) {
  275. wx.showModal({
  276. title: '提示',
  277. content: '查询失败!',
  278. showCancel: false,
  279. success: function (res) {}
  280. })
  281. break
  282. }
  283. that.taskId = 0
  284. wx.showModal({
  285. title: '提示',
  286. content: `此门锁电量为${data.data.battery}%`,
  287. showCancel: false,
  288. success: function (res) {}
  289. })
  290. break
  291. }
  292. case 'queryNbImei': {
  293. wx.hideLoading()
  294. if (data.code != 200) {
  295. wx.showModal({
  296. title: '提示',
  297. content: '查询失败!',
  298. showCancel: false,
  299. success: function (res) {}
  300. })
  301. break
  302. }
  303. that.taskId = 0
  304. wx.showModal({
  305. title: '提示',
  306. content: `此NB设备IMEI为${data.data.imei}`,
  307. showCancel: false,
  308. success: function (res) {}
  309. })
  310. break
  311. }
  312. case 'setNbFun': {
  313. wx.hideLoading()
  314. if (data.code != 200) {
  315. wx.showModal({
  316. title: '提示',
  317. content: '设置Nb功能失败!',
  318. showCancel: false,
  319. success: function (res) {}
  320. })
  321. break
  322. }
  323. that.taskId = 0
  324. wx.showModal({
  325. title: '提示',
  326. content: that.data.isNbEnable ? 'NB功能已禁用' : 'NB功能已打开',
  327. showCancel: false,
  328. })
  329. that.setData({
  330. isNbEnable: !that.data.isNbEnable
  331. })
  332. break
  333. }
  334. case 'sendBindLock': {
  335. wx.hideLoading()
  336. if (data.code != 200) {
  337. wx.showModal({
  338. title: '提示',
  339. content: '绑定失败!',
  340. showCancel: false,
  341. success: function (res) {}
  342. })
  343. break
  344. }
  345. that.taskId = 0
  346. that.setData({
  347. isBindLock: data.data.isBind
  348. })
  349. if (data.data.isBind) {
  350. wx.showToast({
  351. title: '绑定成功!',
  352. })
  353. } else {
  354. wx.showToast({
  355. title: '绑定失败!',
  356. })
  357. }
  358. break
  359. }
  360. case 'sendUnbindLock': {
  361. wx.hideLoading()
  362. if (data.code != 200) {
  363. wx.showModal({
  364. title: '提示',
  365. content: '解绑失败!',
  366. showCancel: false,
  367. success: function (res) {}
  368. })
  369. break
  370. }
  371. that.taskId = 0
  372. that.setData({
  373. isBindLock: data.data.isBind
  374. })
  375. if (data.data.isBind) {
  376. wx.showToast({
  377. title: '解绑失败!',
  378. })
  379. } else {
  380. wx.showToast({
  381. title: '解绑成功!',
  382. })
  383. }
  384. break
  385. }
  386. case 'login1':
  387. case 'sendOpenLockP1': {
  388. if (data.code != 200) {
  389. wx.hideLoading()
  390. if (that.taskId == 41) {
  391. wx.showModal({
  392. title: '提示',
  393. content: that.data.isJack ? '上电失败!' : '解锁失败!',
  394. showCancel: false,
  395. success: function (res) {}
  396. })
  397. } else if (that.taskId == 32) {
  398. wx.showModal({
  399. title: '提示',
  400. content: '登录失败!',
  401. showCancel: false,
  402. success: function (res) {}
  403. })
  404. }
  405. break
  406. }
  407. if (that.taskId == 41) {
  408. var bytes = plugin.sendOpenLockP2(that.lockDevice.name, basecode, data.data.randomN)
  409. console.debug('bytes', bytes)
  410. that.sendBytes(bytes)
  411. } else if (that.taskId == 32) {
  412. var bytes = plugin.login2(that.lockDevice.name, basecode, data.data.randomN)
  413. console.debug('bytes', bytes)
  414. that.sendBytes(bytes)
  415. }
  416. break;
  417. }
  418. case 'login':
  419. case 'sendOpenLockP2': {
  420. wx.hideLoading()
  421. if (data.code != 200) {
  422. if (that.taskId == 41) {
  423. wx.showModal({
  424. title: '提示',
  425. content: that.data.isJack ? '上电失败!' : '解锁失败!',
  426. showCancel: false,
  427. success: function (res) {}
  428. })
  429. } else if (that.taskId == 32) {
  430. wx.showModal({
  431. title: '提示',
  432. content: '登录失败!',
  433. showCancel: false,
  434. success: function (res) {}
  435. })
  436. }
  437. break
  438. }
  439. isLockLogin = true
  440. if (that.taskId == 41) {
  441. wx.showToast({
  442. title: that.data.isJack ? '上电成功!' : '解锁成功!',
  443. })
  444. } else if (that.taskId == 32) {
  445. wx.showToast({
  446. title: '登录成功!',
  447. })
  448. }
  449. that.taskId = 0
  450. break;
  451. }
  452. case 'syncClock': {
  453. wx.hideLoading()
  454. if (data.code != 200) {
  455. wx.showModal({
  456. title: '提示',
  457. content: '同步时间失败!' + data.data.msg,
  458. showCancel: false,
  459. success: function (res) {}
  460. })
  461. break
  462. }
  463. that.taskId = 0
  464. wx.showModal({
  465. title: '提示',
  466. content: '时间更新成功!',
  467. showCancel: false
  468. })
  469. break;
  470. }
  471. case 'addPincode': {
  472. wx.hideLoading()
  473. if (data.code != 200) {
  474. wx.showModal({
  475. title: '提示',
  476. content: '添加密码失败!' + data.data.msg,
  477. showCancel: false,
  478. success: function (res) {}
  479. })
  480. break
  481. }
  482. that.taskId = 0
  483. that.setData({
  484. isPincodeAdd: true
  485. })
  486. wx.showModal({
  487. title: '提示',
  488. content: `添加密码${pincode}成功!`,
  489. showCancel: false
  490. })
  491. break
  492. }
  493. case 'delPincode': {
  494. wx.hideLoading()
  495. if (data.code != 200) {
  496. wx.showModal({
  497. title: '提示',
  498. content: '删除密码失败!',
  499. showCancel: false,
  500. success: function (res) {}
  501. })
  502. break
  503. }
  504. that.taskId = 0
  505. wx.showModal({
  506. title: '提示',
  507. content: `删除密码${pincode}成功!`,
  508. showCancel: false,
  509. })
  510. that.setData({
  511. isPincodeAdd: false
  512. })
  513. pincode = -1
  514. pincodeIndex = -1
  515. break
  516. }
  517. case 'addRfCard': {
  518. wx.hideLoading()
  519. if (data.code == 100) {
  520. wx.showLoading({
  521. title: data.data.msg,
  522. })
  523. break
  524. } else if (data.code != 200) {
  525. wx.showModal({
  526. title: '提示',
  527. content: '添加房卡失败!' + data.data.msg,
  528. showCancel: false,
  529. success: function (res) {}
  530. })
  531. break
  532. }
  533. that.taskId = 0
  534. that.setData({
  535. isRfCardAdd: true
  536. })
  537. if (that.lockModel > 70) {
  538. wx.showModal({
  539. title: '提示',
  540. content: `添加房卡成功,序号${rfCardIndex}`,
  541. showCancel: false
  542. })
  543. } else {
  544. wx.showModal({
  545. title: '提示',
  546. content: `添加房卡成功,卡号${rfCardId}`,
  547. showCancel: false
  548. })
  549. }
  550. break
  551. }
  552. case 'delRfCard': {
  553. wx.hideLoading()
  554. if (data.code != 200) {
  555. wx.showModal({
  556. title: '提示',
  557. content: '删除房卡失败!',
  558. showCancel: false,
  559. success: function (res) {}
  560. })
  561. break
  562. }
  563. that.taskId = 0
  564. that.setData({
  565. isRfCardAdd: false
  566. })
  567. if (that.lockModel > 70) {
  568. wx.showModal({
  569. title: '提示',
  570. content: `删除房卡成功,序号${rfCardIndex}`,
  571. showCancel: false,
  572. })
  573. } else {
  574. wx.showModal({
  575. title: '提示',
  576. content: `删除房卡成功,卡号${rfCardId}`,
  577. showCancel: false,
  578. })
  579. }
  580. rfCardId = -1
  581. rfCardIndex = -1
  582. break
  583. }
  584. case 'addFingerprint': {
  585. wx.hideLoading()
  586. if (data.code == 100) {
  587. wx.showLoading({
  588. title: data.data.msg,
  589. })
  590. break
  591. } else if (data.code != 200) {
  592. wx.showModal({
  593. title: '提示',
  594. content: '添加指纹失败!' + data.data.msg,
  595. showCancel: false,
  596. success: function (res) {}
  597. })
  598. break
  599. }
  600. that.taskId = 0
  601. that.setData({
  602. isFingerprintAdd: true
  603. })
  604. wx.showModal({
  605. title: '提示',
  606. content: `添加指纹成功,序号${fingerprintIndex}`,
  607. showCancel: false
  608. })
  609. break
  610. }
  611. case 'delFingerprint': {
  612. wx.hideLoading()
  613. if (data.code != 200) {
  614. wx.showModal({
  615. title: '提示',
  616. content: '删除指纹失败!',
  617. showCancel: false,
  618. success: function (res) {}
  619. })
  620. break
  621. }
  622. that.taskId = 0
  623. wx.showModal({
  624. title: '提示',
  625. content: `删除指纹成功,序号${fingerprintIndex}`,
  626. showCancel: false,
  627. })
  628. that.setData({
  629. isFingerprintAdd: false
  630. })
  631. fingerprintIndex = -1
  632. break
  633. }
  634. case 'changeAdminPincode': {
  635. wx.hideLoading()
  636. if (data.code != 200) {
  637. wx.showModal({
  638. title: '提示',
  639. content: '修改管理密码失败!',
  640. showCancel: false,
  641. success: function (res) {}
  642. })
  643. break
  644. }
  645. that.taskId = 0
  646. wx.showToast({
  647. title: '修改管理密码成功!',
  648. })
  649. }
  650. case 'queryLockSlotState': {
  651. wx.hideLoading()
  652. if (data.code != 200) {
  653. wx.showModal({
  654. title: '提示',
  655. content: '查询失败!',
  656. showCancel: false,
  657. success: function (res) {}
  658. })
  659. break
  660. }
  661. that.taskId = 0
  662. wx.showModal({
  663. title: '提示',
  664. content: data.data.state == 0 ? '此门锁方舌关闭' : '此门锁方舌打开',
  665. showCancel: false,
  666. success: function (res) {}
  667. })
  668. break
  669. }
  670. case 'queryLockUnlockState': {
  671. wx.hideLoading()
  672. if (data.code != 200) {
  673. wx.showModal({
  674. title: '提示',
  675. content: '查询失败!',
  676. showCancel: false,
  677. success: function (res) {}
  678. })
  679. break
  680. }
  681. that.taskId = 0
  682. wx.showModal({
  683. title: '提示',
  684. content: data.data.state == 0 ? '此门锁已关闭' : '此门锁已打开',
  685. showCancel: false,
  686. success: function (res) {}
  687. })
  688. break
  689. }
  690. case 'setLockUnlockState': {
  691. wx.hideLoading()
  692. if (data.code != 200) {
  693. wx.showModal({
  694. title: '提示',
  695. content: '设置常开指令失败!',
  696. showCancel: false,
  697. success: function (res) {}
  698. })
  699. break
  700. }
  701. that.taskId = 0
  702. wx.showModal({
  703. title: '提示',
  704. content: data.data.state == 0 ? '常开模式已退出' : '常开模式已开启',
  705. showCancel: false,
  706. })
  707. that.setData({
  708. isUnloked: data.data.state == 1
  709. })
  710. break
  711. }
  712. case 'setLockMuteState': {
  713. wx.hideLoading()
  714. if (data.code != 200) {
  715. wx.showModal({
  716. title: '提示',
  717. content: '设置静音指令失败!',
  718. showCancel: false,
  719. success: function (res) {}
  720. })
  721. break
  722. }
  723. that.taskId = 0
  724. wx.showModal({
  725. title: '提示',
  726. content: data.data.state == 1 ? '静音模式已退出' : '静音模式已开启',
  727. showCancel: false,
  728. })
  729. that.setData({
  730. isMuted: data.data.state == 0
  731. })
  732. break
  733. }
  734. }
  735. })
  736. wx.notifyBLECharacteristicValueChange({
  737. deviceId: that.lockDevice.deviceId,
  738. serviceId: config.uuid.serviceUuid.toUpperCase(),
  739. characteristicId: config.uuid.notifyUuid.toUpperCase(),
  740. state: true,
  741. success: function (res) {
  742. console.log('setupNotify', res)
  743. that.isInitReady = true
  744. setTimeout(function () {
  745. wx.hideLoading()
  746. that.onQueryBindState()
  747. }, 500)
  748. },
  749. })
  750. },
  751. })
  752. },
  753. })
  754. }
  755. onQueryBindState () {
  756. var that = this
  757. wx.showLoading({
  758. title: '查询中',
  759. })
  760. var bytes = plugin.queryLockState(this.lockDevice.name)
  761. console.debug('bytes', bytes)
  762. that.sendBytes(bytes)
  763. }
  764. openLock (taskId) {
  765. var that = this
  766. this.taskId = taskId
  767. wx.showLoading({
  768. title: that.isJack ? '上电中' : '解锁中',
  769. })
  770. var bytes = plugin.sendOpenLockP1(that.lockDevice.name, basecode)
  771. console.debug('bytes', bytes)
  772. that.sendBytes(bytes)
  773. }
  774. sendBytes(bytes) {
  775. var that = this
  776. setTimeout(function () {
  777. wx.writeBLECharacteristicValue({
  778. deviceId: that.lockDevice.deviceId,
  779. serviceId: config.uuid.serviceUuid.toUpperCase(),
  780. characteristicId: config.uuid.writeUuid.toUpperCase(),
  781. value: bytes,
  782. success: function (res) {
  783. },
  784. })
  785. }, 50)
  786. }
  787. }
  788. module.exports = LwJ