From 680bfd4b1563f71339929c8e34cc18476c906942 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8C=83=E9=B9=8F?= Date: Fri, 16 Aug 2024 15:10:58 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E9=99=A4=E6=B8=85=E7=90=86?= =?UTF-8?q?=E7=94=A8=E6=88=B7=E5=A4=96=E5=85=A8=E9=83=A8=E8=93=9D=E7=89=99?= =?UTF-8?q?API=E7=9A=84=E5=86=99=E5=85=A5=E4=B8=8E=E7=9B=91=E5=90=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/index/index.vue | 43 ++++++++++++- stores/bluetooth.js | 143 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 184 insertions(+), 2 deletions(-) diff --git a/pages/index/index.vue b/pages/index/index.vue index c7c1a05..c4ed272 100644 --- a/pages/index/index.vue +++ b/pages/index/index.vue @@ -11,6 +11,9 @@ + + + 名称:{{currentLockInfo.name}} 设备Id:{{currentLockInfo.deviceId}} @@ -38,7 +41,45 @@ methods: { ...mapActions(useBluetoothStore, ['getBluetoothDevices', 'stopGetBluetoothDevices', 'updateCurrentLockInfo', 'connectBluetoothDevice', 'getPublicKey', 'getCommKey', 'getLockStatus', 'addLockUser', 'timestampToArray', - 'openDoor', 'resetDevice']), + 'openDoor', 'resetDevice', 'resetLockPassword', 'setLockPassword']), + async deletePassword() { + const timestamp = parseInt(new Date().getTime() / 1000) + const data = await this.setLockPassword({ + keyId: this.keyId, + uid: this.authUid, + pwdNo: 2, + operate: 2, + isAdmin: 1, + pwd: '000000', + userCountLimit: 0, + startTime: timestamp, + endTime: timestamp + }) + console.log('设置密码返回', data) + }, + async setPassword() { + const timestamp = parseInt(new Date().getTime() / 1000) + const endTimestamp = timestamp + 3600 * 24 * 365 + const data = await this.setLockPassword({ + keyId: this.keyId, + uid: this.authUid, + pwdNo: 1, + operate: 0, + isAdmin: 1, + pwd: '000000', + userCountLimit: 0xffff, + startTime: timestamp, + endTime: endTimestamp + }) + console.log('设置密码返回', data) + }, + async resetPassword() { + const { code } = await this.resetLockPassword({ + uid: this.authUid, + keyId: this.keyId + }) + console.log('重置密码返回', code) + }, async openDoorOperate() { const { code } = await this.openDoor({ name: this.currentLockInfo.name, diff --git a/stores/bluetooth.js b/stores/bluetooth.js index 63a39b7..52870ef 100644 --- a/stores/bluetooth.js +++ b/stores/bluetooth.js @@ -27,6 +27,16 @@ const cmdIds = { resetDevice: 0x3004, // 清理用户 cleanUser: 0x300C, + // 扩展命令 + expandCmd: 0x3030 +} + +// 子命令ID +const subCmdIds = { + // 设置开锁密码 + setLockPassword: 3, + // 重置开锁密码 + resetLockPassword: 19 } export const useBluetoothStore = defineStore('ble', { @@ -210,6 +220,38 @@ export const useBluetoothStore = defineStore('ble', { code: decrypted[46] }) break + case cmdIds.expandCmd: + const subCmdId = decrypted[3] + switch (subCmdId) { + case subCmdIds.resetLockPassword: + that.updateCurrentLockInfo({ + ...that.currentLockInfo, + token: decrypted.slice(5,9) + }) + characteristicValueCallback({ + code: decrypted[2] + }) + if(decrypted[2] === 0) { + that.updateCurrentLockInfo({ + ...that.currentLockInfo, + encrpyKey: decrypted.slice(9, 17) + }) + } + break + case subCmdIds.setLockPassword: + that.updateCurrentLockInfo({ + ...that.currentLockInfo, + token: decrypted.slice(5,9) + }) + characteristicValueCallback({ + code: decrypted[2], + data: { + status: decrypted[11] + } + }) + break + } + break default: that.updateCurrentLockInfo({ ...that.currentLockInfo, @@ -758,7 +800,7 @@ export const useBluetoothStore = defineStore('ble', { return new Promise(resolve => { const timer = setTimeout(() => { resolve({ code: -1 }) - }, 20000) + }, 10000) characteristicValueCallback = async (data) => { // code 6 token过期,重新获取 if(data.code === 6) { @@ -842,6 +884,105 @@ export const useBluetoothStore = defineStore('ble', { await this.writeBLECharacteristicValue(packageArray) return this.getWriteResult(this.resetDevice, data) + }, + // 重置开锁密码 + async resetLockPassword(data) { + const { keyId, uid } = data + const length = 2 + 1 + 1 + 40 + 20 + 4 + 1 + 16 + const headArray = this.createPackageHeader(3, length) + const conentArray = new Uint8Array(length) + + conentArray[0] = cmdIds.expandCmd / 256 + conentArray[1] = cmdIds.expandCmd % 256 + + // 子命令 + conentArray[2] = subCmdIds.resetLockPassword + + conentArray[3] = length - 4 + + for (let i = 0; i < keyId.length; i++) { + conentArray[i + 4] = keyId.charCodeAt(i) + } + + for (let i = 0; i < uid.length; i++) { + conentArray[i + 44] = uid.charCodeAt(i) + } + + conentArray.set(this.currentLockInfo.token, 64) + + conentArray[68] = 16 + + const md5Array = this.md5Encrypte(keyId + uid, this.currentLockInfo.token, this.currentLockInfo.signKey) + + conentArray.set(md5Array, 69) + + const cebArray = sm4.encrypt(conentArray, this.currentLockInfo.commKey, { mode: 'ecb', output: 'array' }) + + const packageArray = this.createPackageEnd(headArray, cebArray) + + await this.writeBLECharacteristicValue(packageArray) + + return this.getWriteResult(this.resetLockPassword, data) + }, + // 清理用户 + async cleanUser(data) { + const { name, authUid, keyId, uid, userNoLength, userNoList } = data + // const length = 2 + 40 + 20 + 40 + 20 + 2 + userNoLength * 4 + 1 + 16 + }, + // 设置密码 + async setLockPassword(data) { + const { keyId, uid, pwdNo, operate, isAdmin, pwd, userCountLimit, startTime, endTime} = data + const length = 2 + 1 + 1 + 40 + 20 + 2 + 1 + 1 + 20 + 2 + 4 + 4 + 4 + 1 + 16 + const headArray = this.createPackageHeader(3, length) + const conentArray = new Uint8Array(length) + + conentArray[0] = cmdIds.expandCmd / 256 + conentArray[1] = cmdIds.expandCmd % 256 + + // 子命令 + conentArray[2] = subCmdIds.setLockPassword + + conentArray[3] = length - 3 + + for (let i = 0; i < keyId.length; i++) { + conentArray[i + 4] = keyId.charCodeAt(i) + } + + for (let i = 0; i < uid.length; i++) { + conentArray[i + 44] = uid.charCodeAt(i) + } + + conentArray[64] = pwdNo / 256 + conentArray[65] = pwdNo % 256 + + conentArray[66] = operate + conentArray[67] = isAdmin + + for (let i = 0; i < pwd.length; i++) { + conentArray[i + 68] = pwd.charCodeAt(i) + } + + conentArray[88] = userCountLimit / 256 + conentArray[89] = userCountLimit % 256 + + conentArray.set(this.currentLockInfo.token, 90) + + conentArray.set(this.timestampToArray(startTime), 94) + conentArray.set(this.timestampToArray(endTime), 98) + + conentArray[102] = 16 + + const md5Array = this.md5Encrypte(keyId + uid, this.currentLockInfo.token, this.currentLockInfo.signKey) + + conentArray.set(md5Array, 103) + + const cebArray = sm4.encrypt(conentArray, this.currentLockInfo.commKey, { mode: 'ecb', output: 'array' }) + + const packageArray = this.createPackageEnd(headArray, cebArray) + + await this.writeBLECharacteristicValue(packageArray) + + return this.getWriteResult(this.setLockPassword, data) } } })