fix: 优化开锁速度,添加开锁实时日志

This commit is contained in:
范鹏 2024-11-28 13:45:45 +08:00
parent f92782e137
commit a888ce7415
3 changed files with 235 additions and 41 deletions

View File

@ -46,6 +46,7 @@ const $starCloud = useStarCloudStore()
* @param {String} params.clientId 客户端Id
* @param {String} params.clientSecret 客户端密码
* @param {String} params.env 环境
* @param {Boolean} params.isReportLog 是否上报日志
*/
$starCloud.initStarCloud(params)

40
log.js Normal file
View File

@ -0,0 +1,40 @@
import { isReportLog } from '@/starCloud/starCloud'
const log = wx.getRealtimeLogManager ? wx.getRealtimeLogManager() : null
export default {
debug() {
if (!log) return
if (!isReportLog) return
log.debug.apply(log, arguments)
},
info() {
if (!log) return
if (!isReportLog) return
log.info.apply(log, arguments)
},
warn() {
if (!log) return
if (!isReportLog) return
log.warn.apply(log, arguments)
},
error() {
if (!log) return
if (!isReportLog) return
log.error.apply(log, arguments)
},
setFilterMsg(msg) {
// 从基础库2.7.3开始支持
if (!log || !log.setFilterMsg) return
if (typeof msg !== 'string') return
if (!isReportLog) return
log.setFilterMsg(msg)
},
addFilterMsg(msg) {
// 从基础库2.8.1开始支持
if (!log || !log.addFilterMsg) return
if (typeof msg !== 'string') return
if (!isReportLog) return
log.addFilterMsg(msg)
}
}

View File

@ -43,6 +43,7 @@ import {
timestampToArray,
uint8ArrayToString
} from '@/starCloud/format'
import log from '@/starCloud/log'
/**
* 离线密码
@ -139,6 +140,9 @@ import {
* @property {String} userNos - 设备ID
*/
// 是否上报日志
export const isReportLog = false
// 命令ID
const cmdIds = {
// 获取公钥
@ -216,12 +220,15 @@ export const useStarCloudStore = defineStore('starCloud', {
* @param {String} params.clientId 客户端Id
* @param {String} params.clientSecret 客户端密码
* @param {String} params.env 环境
* @param {Boolean} params.isReportLog 是否上报日志
*/
initStarCloud(params) {
const { clientId, clientSecret, env } = params
const appInfo = uni.getAccountInfoSync()
this.envVersion = appInfo.miniProgram.envVersion
isReportLog = params.isReportLog
this.env = env || 'XHJ'
this.clientId = clientId
this.clientSecret = clientSecret
@ -271,25 +278,26 @@ export const useStarCloudStore = defineStore('starCloud', {
lockId
})
if (code === Result.Success.code) {
let lockList = getStorage('lockList')
this.lockInfo = data
let lockList = getStorage('starLockList')
if (!lockList) {
lockList = {}
}
if (lockList[accountInfo.uid]) {
const index = lockList[accountInfo.uid].findIndex(item => item.lockId === lockId)
if (index === -1) {
lockList[accountInfo.uid].push(data)
lockList[accountInfo.uid].push(this.lockInfo)
} else {
lockList[accountInfo.uid][index] = data
this.lockInfo.token = lockList[accountInfo.uid][index].token
lockList[accountInfo.uid][index] = this.lockInfo
}
setStorage('lockList', lockList)
setStorage('starLockList', lockList)
} else {
lockList[accountInfo.uid] = [data]
setStorage('lockList', lockList)
lockList[accountInfo.uid] = [this.lockInfo]
setStorage('starLockList', lockList)
}
this.lockInfo = data
} else {
const lockList = getStorage('lockList')
const lockList = getStorage('starLockList')
if (lockList[accountInfo.uid]) {
const index = lockList[accountInfo.uid].findIndex(item => item.lockId === lockId)
if (index !== -1) {
@ -310,18 +318,61 @@ export const useStarCloudStore = defineStore('starCloud', {
*/
async openDoor(params) {
const { accountInfo, type } = params
log.info(
new Result(
Result.Success.code,
{
lockName: this.lockInfo.bluetooth.bluetoothDeviceName,
lockId: this.lockInfo.lockId,
uid: accountInfo.uid,
time: new Date().getTime()
},
`开始开门`
)
)
// 设置执行账号
const result = await this.login(accountInfo)
if (result.code !== Result.Success.code) {
return result
}
log.info(
new Result(
result.code,
{
lockName: this.lockInfo.bluetooth.bluetoothDeviceName,
lockId: this.lockInfo.lockId,
uid: accountInfo.uid,
time: new Date().getTime()
},
`登录星云账号: ${result.message}`
)
)
// 确认设备连接正常
const searchResult = await searchAndConnectDevice(this.lockInfo.bluetooth.bluetoothDeviceName)
if (!params.connected) {
const searchResult = await searchAndConnectDevice(
this.lockInfo.bluetooth.bluetoothDeviceName
)
log.info(
new Result(
searchResult.code,
{
lockName: this.lockInfo.bluetooth.bluetoothDeviceName,
lockId: this.lockInfo.lockId,
uid: accountInfo.uid,
time: new Date().getTime()
},
`连接设备: ${searchResult.message}`
)
)
if (searchResult.code !== Result.Success.code) {
return searchResult
}
this.updateLockInfo(searchResult.data)
}
// 检查是否已添加为用户
const checkResult = await this.checkLockUser()
@ -329,6 +380,19 @@ export const useStarCloudStore = defineStore('starCloud', {
return checkResult
}
log.info(
new Result(
checkResult.code,
{
lockName: this.lockInfo.bluetooth.bluetoothDeviceName,
lockId: this.lockInfo.lockId,
uid: accountInfo.uid,
time: new Date().getTime()
},
`确认是否为锁用户: ${checkResult.message}`
)
)
// 是否需要联网
let onlineToken = ''
if (this.lockInfo.lockSetting.appUnlockOnline) {
@ -340,6 +404,19 @@ export const useStarCloudStore = defineStore('starCloud', {
}
}
log.info(
new Result(
checkResult.code,
{
lockName: this.lockInfo.bluetooth.bluetoothDeviceName,
lockId: this.lockInfo.lockId,
uid: accountInfo.uid,
time: new Date().getTime()
},
`判断是否需要联网token: ${this.lockInfo.lockSetting.appUnlockOnline}`
)
)
// 开门方式
let openMode
if (type === 'close') {
@ -396,6 +473,19 @@ export const useStarCloudStore = defineStore('starCloud', {
const packageArray = createPackageEnd(headArray, cebArray)
log.info(
new Result(
Result.Success.code,
{
lockName: this.lockInfo.bluetooth.bluetoothDeviceName,
lockId: this.lockInfo.lockId,
uid: accountInfo.uid,
time: new Date().getTime()
},
`开始写入`
)
)
const writeResult = await writeBLECharacteristicValue(
this.lockInfo.deviceId,
this.lockInfo.serviceId,
@ -407,6 +497,19 @@ export const useStarCloudStore = defineStore('starCloud', {
return writeResult
}
log.info(
new Result(
writeResult.code,
{
lockName: this.lockInfo.bluetooth.bluetoothDeviceName,
lockId: this.lockInfo.lockId,
uid: accountInfo.uid,
time: new Date().getTime()
},
`写入完成:${writeResult.message}`
)
)
return this.getWriteResult(this.openDoor, params)
},
/**
@ -442,11 +545,15 @@ export const useStarCloudStore = defineStore('starCloud', {
return result
}
// 确认设备连接正常
const searchResult = await searchAndConnectDevice(this.lockInfo.bluetooth.bluetoothDeviceName)
if (!params.connected) {
const searchResult = await searchAndConnectDevice(
this.lockInfo.bluetooth.bluetoothDeviceName
)
if (searchResult.code !== Result.Success.code) {
return searchResult
}
this.updateLockInfo(searchResult.data)
}
// 检查是否已添加为用户
const checkResult = await this.checkLockUser()
@ -707,7 +814,7 @@ export const useStarCloudStore = defineStore('starCloud', {
lockIds: params.lockIds
})
if (code === Result.Success.code) {
const lockList = getStorage('lockList')
const lockList = getStorage('starLockList')
if (lockList[accountInfo.uid]) {
lockIds.forEach(lockId => {
const index = lockList[accountInfo.uid].findIndex(item => item.lockId === lockId)
@ -715,7 +822,7 @@ export const useStarCloudStore = defineStore('starCloud', {
lockList[accountInfo.uid].splice(index, 1)
}
})
setStorage('lockList', lockList)
setStorage('starLockList', lockList)
}
}
@ -734,11 +841,15 @@ export const useStarCloudStore = defineStore('starCloud', {
return result
}
// 确认设备连接正常
const searchResult = await searchAndConnectDevice(this.lockInfo.bluetooth.bluetoothDeviceName)
if (!params.connected) {
const searchResult = await searchAndConnectDevice(
this.lockInfo.bluetooth.bluetoothDeviceName
)
if (searchResult.code !== Result.Success.code) {
return searchResult
}
this.updateLockInfo(searchResult.data)
}
// 检查是否已添加为用户
const checkResult = await this.checkLockUser()
@ -812,11 +923,15 @@ export const useStarCloudStore = defineStore('starCloud', {
}
// 确认设备连接正常
const searchResult = await searchAndConnectDevice(this.lockInfo.bluetooth.bluetoothDeviceName)
if (!params.connected) {
const searchResult = await searchAndConnectDevice(
this.lockInfo.bluetooth.bluetoothDeviceName
)
if (searchResult.code !== Result.Success.code) {
return searchResult
}
this.updateLockInfo(searchResult.data)
}
// 检查是否已添加为用户
const checkResult = await this.checkLockUser()
@ -948,11 +1063,15 @@ export const useStarCloudStore = defineStore('starCloud', {
}
// 确认设备连接正常
const searchResult = await searchAndConnectDevice(this.lockInfo.bluetooth.bluetoothDeviceName)
if (!params.connected) {
const searchResult = await searchAndConnectDevice(
this.lockInfo.bluetooth.bluetoothDeviceName
)
if (searchResult.code !== Result.Success.code) {
return searchResult
}
this.updateLockInfo(searchResult.data)
}
// 检查是否已添加为用户
const checkResult = await this.checkLockUser()
@ -1337,7 +1456,9 @@ export const useStarCloudStore = defineStore('starCloud', {
// 添加用户
async addLockUser(params) {
const { params: data } = params
// 确认设备连接正常
if (!params.connected) {
const searchResult = await searchAndConnectDevice(
this.lockInfo.bluetooth.bluetoothDeviceName,
data.role !== 0xff
@ -1346,6 +1467,7 @@ export const useStarCloudStore = defineStore('starCloud', {
return searchResult
}
this.updateLockInfo(searchResult.data)
}
const {
name,
@ -1451,11 +1573,22 @@ export const useStarCloudStore = defineStore('starCloud', {
characteristicValueCallback = async data => {
// code 6 token过期,重新获取
if (data.code === Result.NotTokenLock.code) {
resolve(await request(params))
log.info(
new Result(
data.code,
{
lockName: this.lockInfo.bluetooth.bluetoothDeviceName,
lockId: this.lockInfo.lockId,
time: new Date().getTime()
},
`token过期${data.message}`
)
)
resolve(await request({ ...params, connected: true }))
} else if (data.code === Result.NotRegisteredLock.code) {
const checkResult = await this.checkLockUser(true)
if (checkResult.code === Result.Success.code) {
resolve(await request(params))
resolve(await request({ ...params, connected: true }))
} else {
clearTimeout(getWriteResultTimer)
resolve(checkResult)
@ -1466,6 +1599,17 @@ export const useStarCloudStore = defineStore('starCloud', {
await this.disconnectDevice()
}
console.log('写入结果', data, request, params)
log.info(
new Result(
data.code,
{
lockName: this.lockInfo.bluetooth.bluetoothDeviceName,
lockId: this.lockInfo.lockId,
time: new Date().getTime()
},
`开门结果:${data.message}`
)
)
resolve(data)
}
}
@ -1535,6 +1679,15 @@ export const useStarCloudStore = defineStore('starCloud', {
...this.lockInfo,
...lockInfo
}
const lockList = getStorage('starLockList')
const index = lockList[this.accountInfo.uid].findIndex(
item => item.lockId === this.lockInfo.lockId
)
if (index !== -1) {
lockList[this.accountInfo.uid][index] = this.lockInfo
}
setStorage('starLockList', lockList)
},
// 特征值变化回调
listenCharacteristicValue(res) {
@ -1811,7 +1964,7 @@ export const useStarCloudStore = defineStore('starCloud', {
lockId: this.lockInfo.lockId
})
if (code === Result.Success.code) {
const lockList = getStorage('lockList')
const lockList = getStorage('starLockList')
if (lockList[this.accountInfo.uid]) {
const index = lockList[this.accountInfo.uid].findIndex(
item => item.lockId === this.lockInfo.lockId
@ -1819,7 +1972,7 @@ export const useStarCloudStore = defineStore('starCloud', {
if (index !== -1) {
lockList[this.accountInfo.uid].splice(index, 1)
}
setStorage('lockList', lockList)
setStorage('starLockList', lockList)
}
}
characteristicValueCallback(new Result(code, {}, message))