完成除清理用户外全部蓝牙API的写入与监听

This commit is contained in:
范鹏 2024-08-16 15:10:58 +08:00
parent 1130b198b9
commit 680bfd4b15
2 changed files with 184 additions and 2 deletions

View File

@ -11,6 +11,9 @@
<button class="button" @click="closeDoorOperate">关门</button>
<button class="button" @click="reset">恢复出厂设置</button>
<button class="button" @click="getLockStatusResult">获取锁状态</button>
<button class="button" @click="resetPassword">重置锁密码</button>
<button class="button" @click="setPassword">设置密码</button>
<button class="button" @click="deletePassword">删除密码</button>
<view>名称{{currentLockInfo.name}}</view>
<view>设备Id{{currentLockInfo.deviceId}}</view>
</view>
@ -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,

View File

@ -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)
}
}
})