1. 修复新普通用户操作蓝牙bug

This commit is contained in:
范鹏 2024-08-30 13:54:44 +08:00
parent 2a55f76d6a
commit 3355a68007

View File

@ -288,6 +288,7 @@ export const useBluetoothStore = defineStore('ble', {
token: decrypted.slice(42,46),
lockUserNo: decrypted[47] * 256 + decrypted[48]
})
console.log('添加用户结果', decrypted[46], that.currentLockInfo.token)
characteristicValueCallback({
code: decrypted[46]
})
@ -329,6 +330,7 @@ export const useBluetoothStore = defineStore('ble', {
...that.currentLockInfo,
token: decrypted.slice(2,6)
})
console.log('默认结果', decrypted[6], that.currentLockInfo.token)
characteristicValueCallback({
code: decrypted[6]
})
@ -650,6 +652,7 @@ export const useBluetoothStore = defineStore('ble', {
timer2 = setTimeout(() => {
uni.stopBluetoothDevicesDiscovery()
clearInterval(timer1)
resolve(false)
}, 10500)
timer1 = setInterval(() => {
uni.getBluetoothDevices({
@ -693,7 +696,7 @@ export const useBluetoothStore = defineStore('ble', {
},
// 检查是否已添加为用户
async checkLockUser() {
console.log('检查是否已添加为用户', this.currentLockInfo.lockUserNo === 0)
console.log('检查是否已添加为用户', this.currentLockInfo.lockUserNo)
if(this.currentLockInfo.lockUserNo === 0) {
const timestamp = parseInt(new Date().getTime() / 1000)
const password = (Math.floor(Math.random() * 900000) + 100000).toString()
@ -717,17 +720,20 @@ export const useBluetoothStore = defineStore('ble', {
role: 0,
password
})
console.log('添加结果', addUserCode)
console.log('添加用户蓝牙结果', addUserCode)
if(addUserCode === 0) {
const { code } = await updateLockUserNoRequest({
keyId: this.keyId,
lockUserNo: this.currentLockInfo.lockUserNo
})
console.log('添加用户请求结果', code)
return true
} else if(addUserCode === 0x0c) {
console.log('用户达上限,开始清理用户')
const { code: requestCode, data: requestData } = await getUserNoListRequest({
lockId: this.currentLockInfo.lockId
})
console.log('获取用户列表请求结果', requestCode, requestData)
if(requestCode !== 0) return false
const userNoList = requestData.userNos
const { code: cleanCode } = await this.cleanLockUser({
@ -737,6 +743,7 @@ export const useBluetoothStore = defineStore('ble', {
uid: this.currentLockInfo.uid.toString(),
userNoList: userNoList
})
console.log('清理用户蓝牙结果', cleanCode)
if(cleanCode === 0) {
return await this.checkLockUser()
} else {
@ -752,35 +759,9 @@ export const useBluetoothStore = defineStore('ble', {
}
},
// 写入特征值
async writeBLECharacteristicValue(binaryData, isCheckUser = true) {
async writeBLECharacteristicValue(binaryData) {
const that = this
// 确认蓝牙状态正常
if(this.bluetoothStatus !== 0) {
console.log('写入未执行', this.bluetoothStatus)
this.getBluetoothStatus()
return
}
// 确认设备连接正常
if(!that.currentLockInfo.connected) {
const srerchResult = await that.searchAndConnectDevice()
that.updateCurrentLockInfo({
...that.currentLockInfo,
deviceId: srerchResult
})
console.log('设备ID', that.currentLockInfo.deviceId)
if(!srerchResult) return
const result = await that.connectBluetoothDevice()
if(!result) return
}
// 检查是否已添加为用户
if(isCheckUser) {
const checkResult = await that.checkLockUser()
if (!checkResult) return
}
console.log('设备ID', that.currentLockInfo.deviceId)
console.log('设备名称:', that.currentLockInfo.name)
console.log('设备主服务:', that.currentLockInfo.serviceId)
@ -868,6 +849,37 @@ export const useBluetoothStore = defineStore('ble', {
},
// 获取公钥
async getPublicKey(name) {
// 确认蓝牙状态正常
if(this.bluetoothStatus !== 0) {
console.log('写入未执行', this.bluetoothStatus)
this.getBluetoothStatus()
return {
code: -1
}
}
// 确认设备连接正常
if(!this.currentLockInfo.connected) {
const srerchResult = await this.searchAndConnectDevice()
this.updateCurrentLockInfo({
...this.currentLockInfo,
deviceId: srerchResult
})
console.log('设备ID', this.currentLockInfo.deviceId)
if(!srerchResult) {
return {
code: -1
}
}
const result = await this.connectBluetoothDevice()
console.log('连接结果', result)
if(!result) {
return {
code: -1
}
}
}
const headArray = this.createPackageHeader(0, 42)
const conentArray = new Uint8Array(42)
@ -885,6 +897,37 @@ export const useBluetoothStore = defineStore('ble', {
},
// 获取私钥
async getCommKey(name, keyId, authUid, nowTime) {
// 确认蓝牙状态正常
if(this.bluetoothStatus !== 0) {
console.log('写入未执行', this.bluetoothStatus)
this.getBluetoothStatus()
return {
code: -1
}
}
// 确认设备连接正常
if(!this.currentLockInfo.connected) {
const srerchResult = await this.searchAndConnectDevice()
this.updateCurrentLockInfo({
...this.currentLockInfo,
deviceId: srerchResult
})
console.log('设备ID', this.currentLockInfo.deviceId)
if(!srerchResult) {
return {
code: -1
}
}
const result = await this.connectBluetoothDevice()
console.log('连接结果', result)
if(!result) {
return {
code: -1
}
}
}
const length = 2 + 40 + 40 + 20 + 4 + 1 + 16
const headArray = this.createPackageHeader(2, length)
const conentArray = new Uint8Array(length)
@ -936,6 +979,37 @@ export const useBluetoothStore = defineStore('ble', {
},
// 获取锁状态
async getLockStatus(data) {
// 确认蓝牙状态正常
if(this.bluetoothStatus !== 0) {
console.log('写入未执行', this.bluetoothStatus)
this.getBluetoothStatus()
return {
code: -1
}
}
// 确认设备连接正常
if(!this.currentLockInfo.connected) {
const srerchResult = await this.searchAndConnectDevice()
this.updateCurrentLockInfo({
...this.currentLockInfo,
deviceId: srerchResult
})
console.log('设备ID', this.currentLockInfo.deviceId)
if(!srerchResult) {
return {
code: -1
}
}
const result = await this.connectBluetoothDevice()
console.log('连接结果', result)
if(!result) {
return {
code: -1
}
}
}
const { name, uid, nowTime, localTime } = data
const length = 2 + 40 + 20 + 4 + 4
const headArray = this.createPackageHeader(3, length)
@ -976,6 +1050,37 @@ export const useBluetoothStore = defineStore('ble', {
},
// 添加用户
async addLockUser(data) {
// 确认蓝牙状态正常
if(this.bluetoothStatus !== 0) {
console.log('写入未执行', this.bluetoothStatus)
this.getBluetoothStatus()
return {
code: -1
}
}
// 确认设备连接正常
if(!this.currentLockInfo.connected) {
const srerchResult = await this.searchAndConnectDevice()
this.updateCurrentLockInfo({
...this.currentLockInfo,
deviceId: srerchResult
})
console.log('设备ID', this.currentLockInfo.deviceId)
if(!srerchResult) {
return {
code: -1
}
}
const result = await this.connectBluetoothDevice()
console.log('连接结果', result)
if(!result) {
return {
code: -1
}
}
}
const { name, authUid, uid, keyId, openMode, keyType, startDate, expireDate, useCountLimit, isRound, weekRound,
startHour, startMin, endHour, endMin, role, password } = data
const length = 2 + 40 + 20 + 40 + 20 + 1 + 1 + 4 + 4 + 2 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 20 + 4 + 1 + 16
@ -1039,15 +1144,17 @@ export const useBluetoothStore = defineStore('ble', {
// 获取写入结果
getWriteResult(request, params) {
return new Promise(resolve => {
const timer = setTimeout(() => {
const getWriteResultTimer = setTimeout(() => {
resolve({ code: -1 })
}, 10000)
}, 20000)
characteristicValueCallback = async (data) => {
// code 6 token过期,重新获取
if(data.code === 6) {
console.log('token过期', request.toString())
console.log('token过期', params)
resolve(await request(params))
} else {
clearTimeout(timer)
clearTimeout(getWriteResultTimer)
resolve(data)
}
}
@ -1055,6 +1162,45 @@ export const useBluetoothStore = defineStore('ble', {
},
// 开门
async openDoor(data) {
// 确认蓝牙状态正常
if(this.bluetoothStatus !== 0) {
console.log('写入未执行', this.bluetoothStatus)
this.getBluetoothStatus()
return {
code: -1
}
}
// 确认设备连接正常
if(!this.currentLockInfo.connected) {
const srerchResult = await this.searchAndConnectDevice()
this.updateCurrentLockInfo({
...this.currentLockInfo,
deviceId: srerchResult
})
console.log('设备ID', this.currentLockInfo.deviceId)
if(!srerchResult) {
return {
code: -1
}
}
const result = await this.connectBluetoothDevice()
console.log('连接结果', result)
if(!result) {
return {
code: -1
}
}
}
// 检查是否已添加为用户
const checkResult = await this.checkLockUser()
if (!checkResult) {
return {
code: -1
}
}
const { name, uid, openMode, openTime, onlineToken } = data
const length = 2 + 40 + 20 + 1 + 4 + 4 + 1 + 16 + 16
const headArray = this.createPackageHeader(3, length)
@ -1075,6 +1221,8 @@ export const useBluetoothStore = defineStore('ble', {
conentArray.set(this.timestampToArray(openTime), 63)
console.log('开门时token', this.currentLockInfo.token)
conentArray.set(this.currentLockInfo.token || this.timestampToArray(openTime), 67)
conentArray[71] = 16
@ -1091,12 +1239,43 @@ export const useBluetoothStore = defineStore('ble', {
const cebArray = sm4.encrypt(conentArray, this.currentLockInfo.commKey, { mode: 'ecb', output: 'array' })
const packageArray = this.createPackageEnd(headArray, cebArray)
await this.writeBLECharacteristicValue(packageArray)
this.writeBLECharacteristicValue(packageArray)
return this.getWriteResult(this.openDoor, data)
},
// 清理用户
async cleanLockUser(data) {
// 确认蓝牙状态正常
if(this.bluetoothStatus !== 0) {
console.log('写入未执行', this.bluetoothStatus)
this.getBluetoothStatus()
return {
code: -1
}
}
// 确认设备连接正常
if(!this.currentLockInfo.connected) {
const srerchResult = await this.searchAndConnectDevice()
this.updateCurrentLockInfo({
...this.currentLockInfo,
deviceId: srerchResult
})
console.log('设备ID', this.currentLockInfo.deviceId)
if(!srerchResult) {
return {
code: -1
}
}
const result = await this.connectBluetoothDevice()
console.log('连接结果', result)
if(!result) {
return {
code: -1
}
}
}
const { name, authUid, keyId, uid, userNoList } = data
const length = 2 + 40 + 20 + 40 + 20 + 2 + userNoList.length + 4 + 1 + 16
@ -1141,12 +1320,51 @@ export const useBluetoothStore = defineStore('ble', {
const packageArray = this.createPackageEnd(headArray, cebArray)
await this.writeBLECharacteristicValue(packageArray, false)
await this.writeBLECharacteristicValue(packageArray)
return this.getWriteResult(this.cleanLockUser, data)
},
// 恢复出厂设置
async resetDevice(data) {
// 确认蓝牙状态正常
if(this.bluetoothStatus !== 0) {
console.log('写入未执行', this.bluetoothStatus)
this.getBluetoothStatus()
return {
code: -1
}
}
// 确认设备连接正常
if(!this.currentLockInfo.connected) {
const srerchResult = await this.searchAndConnectDevice()
this.updateCurrentLockInfo({
...this.currentLockInfo,
deviceId: srerchResult
})
console.log('设备ID', this.currentLockInfo.deviceId)
if(!srerchResult) {
return {
code: -1
}
}
const result = await this.connectBluetoothDevice()
console.log('连接结果', result)
if(!result) {
return {
code: -1
}
}
}
// 检查是否已添加为用户
const checkResult = await this.checkLockUser()
if (!checkResult) {
return {
code: -1
}
}
const { name, authUid, keyId } = data
const length = 2 + 40 + 20 + 4 + 1 + 16
const headArray = this.createPackageHeader(3, length)
@ -1178,6 +1396,45 @@ export const useBluetoothStore = defineStore('ble', {
},
// 重置开锁密码
async resetLockPassword(data) {
// 确认蓝牙状态正常
if(this.bluetoothStatus !== 0) {
console.log('写入未执行', this.bluetoothStatus)
this.getBluetoothStatus()
return {
code: -1
}
}
// 确认设备连接正常
if(!this.currentLockInfo.connected) {
const srerchResult = await this.searchAndConnectDevice()
this.updateCurrentLockInfo({
...this.currentLockInfo,
deviceId: srerchResult
})
console.log('设备ID', this.currentLockInfo.deviceId)
if(!srerchResult) {
return {
code: -1
}
}
const result = await this.connectBluetoothDevice()
console.log('连接结果', result)
if(!result) {
return {
code: -1
}
}
}
// 检查是否已添加为用户
const checkResult = await this.checkLockUser()
if (!checkResult) {
return {
code: -1
}
}
const { keyId, uid } = data
const length = 2 + 1 + 1 + 40 + 20 + 4 + 1 + 16
const headArray = this.createPackageHeader(3, length)
@ -1215,13 +1472,47 @@ export const useBluetoothStore = defineStore('ble', {
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) {
// 确认蓝牙状态正常
if(this.bluetoothStatus !== 0) {
console.log('写入未执行', this.bluetoothStatus)
this.getBluetoothStatus()
return {
code: -1
}
}
// 确认设备连接正常
if(!this.currentLockInfo.connected) {
const srerchResult = await this.searchAndConnectDevice()
this.updateCurrentLockInfo({
...this.currentLockInfo,
deviceId: srerchResult
})
console.log('设备ID', this.currentLockInfo.deviceId)
if(!srerchResult) {
return {
code: -1
}
}
const result = await this.connectBluetoothDevice()
console.log('连接结果', result)
if(!result) {
return {
code: -1
}
}
}
// 检查是否已添加为用户
const checkResult = await this.checkLockUser()
if (!checkResult) {
return {
code: -1
}
}
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)