diff --git a/README.md b/README.md index 02e5a99..0ffa075 100644 --- a/README.md +++ b/README.md @@ -139,4 +139,23 @@ const data = await $starCloud.removeBadLock(params) * @param {AccountInfo} params.accountInfo 账号信息 */ const data = await $starCloud.deleteLock(params) + +/** + * 修改管理员密码 + * @param params + * @param {AccountInfo} params.accountInfo 账号信息 + * @param {String} params.adminPwd 管理员密码 + * @param {Boolean} params.disconnect 操作后是否断开连接 + * @returns {Promise} + */ +const data = await $starCloud.updateAdminPassword(params) + +/** + * 同步全部开门记录 + * @param params + * @param {AccountInfo} params.accountInfo 账号信息 + * @param {Boolean} params.disconnect 操作后是否断开连接 + * @returns {Promise} + */ +const data = await $starCloud.syncAllOpenRecord(params) ``` diff --git a/api.js b/api.js index 0c262d3..4d31d43 100644 --- a/api.js +++ b/api.js @@ -143,3 +143,21 @@ export function changeAdminKeyboardPwdRequest(data) { data }) } + +// 获取操作记录的最后上传时间 +export function getLastRecordTimeRequest(data) { + return request({ + url: '/v1/lockRecord/getLastRecordTime', + method: 'POST', + data + }) +} + +// 上传操作记录 +export function uploadRecordRequest(data) { + return request({ + url: '/v1/lockRecord/upload', + method: 'POST', + data + }) +} diff --git a/starCloud.js b/starCloud.js index 4eed516..9e39b3b 100644 --- a/starCloud.js +++ b/starCloud.js @@ -7,6 +7,7 @@ import { changeAdminKeyboardPwdRequest, deleteLockRequest, deletePasswordRequest, + getLastRecordTimeRequest, getLockDetailRequest, getLockNetTokenRequest, getOfflinePasswordRequest, @@ -17,7 +18,8 @@ import { starCloudCreateUser, updateElectricQuantityRequest, updateLockUserNoRequest, - updatePasswordRequest + updatePasswordRequest, + uploadRecordRequest } from '@/starCloud/api' import { getStorage, removeStorage, setStorage } from '@/starCloud/storage' import { @@ -164,7 +166,9 @@ const subCmdIds = { // 设置开锁密码 setLockPassword: 3, // 重置开锁密码 - resetLockPassword: 19 + resetLockPassword: 19, + // 同步开门记录 + syncOpenRecord: 41 } // 特性值回调 @@ -895,6 +899,139 @@ export const useStarCloudStore = defineStore('starCloud', { return this.getWriteResult(this.updateAdminPassword, params) }, + /** + * 同步全部开门记录 + * @param params + * @param {AccountInfo} params.accountInfo 账号信息 + * @param {Boolean} params.disconnect 操作后是否断开连接 + * @returns {Promise} + */ + async syncAllOpenRecord(params) { + const { accountInfo, disconnect } = params + + const { code, data, message } = await this.syncOpenRecord({ + accountInfo, + disconnect: false + }) + + if (code === Result.Success.code) { + if (data.count === 10) { + return await this.syncAllOpenRecord({ + accountInfo, + disconnect + }) + } else { + if (disconnect) { + await this.disconnectDevice() + } + return new Result(code, data, message) + } + } else { + return new Result(code, data, message) + } + }, + + /** + * 同步开门记录 + * @param params + * @param {AccountInfo} params.accountInfo 账号信息 + * @param {Boolean} params.disconnect 操作后是否断开连接 + * @returns {Promise} + */ + async syncOpenRecord(params) { + const { accountInfo } = params + + // 设置执行账号 + const result = await this.login(accountInfo) + if (result.code !== Result.Success.code) { + return result + } + + // 确认设备连接正常 + 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() + if (checkResult.code !== Result.Success.code) { + return checkResult + } + + const uid = this.lockInfo.uid.toString() + const keyId = this.lockInfo.keyId.toString() + const logsCount = 10 + + const timeResult = await getLastRecordTimeRequest({ + lockId: this.lockInfo.lockId + }) + + if (timeResult.code !== Result.Success.code) { + return timeResult + } + + const operateDate = Math.ceil(timeResult.data.operateDate / 1000) + const currentDate = Math.ceil(timeResult.data.currentDate / 1000) + + const length = 2 + 1 + 1 + 40 + 20 + 2 + 4 + 4 + 1 + 16 + const headArray = this.createPackageHeader(3, length) + + const contentArray = new Uint8Array(length) + contentArray[0] = cmdIds.expandCmd / 256 + contentArray[1] = cmdIds.expandCmd % 256 + + // 子命令 + contentArray[2] = subCmdIds.syncOpenRecord + + contentArray[3] = length - 3 + + for (let i = 0; i < keyId.length; i++) { + contentArray[i + 4] = keyId.charCodeAt(i) + } + + for (let i = 0; i < uid.length; i++) { + contentArray[i + 44] = uid.charCodeAt(i) + } + + contentArray[64] = logsCount / 256 + contentArray[65] = logsCount % 256 + + contentArray.set(timestampToArray(operateDate), 66) + contentArray.set(timestampToArray(currentDate), 70) + + contentArray[74] = 16 + + const md5Array = md5Encrypt( + uid + keyId, + this.lockInfo.token || new Uint8Array([0, 0, 0, 0]), + this.lockInfo.bluetooth.publicKey + ) + + contentArray.set(md5Array, 75) + + const cebArray = sm4.encrypt(contentArray, this.lockInfo.bluetooth.privateKey, { + mode: 'ecb', + output: 'array' + }) + + const packageArray = createPackageEnd(headArray, cebArray) + + const writeResult = await writeBLECharacteristicValue( + this.lockInfo.deviceId, + this.lockInfo.serviceId, + this.lockInfo.writeCharacteristicId, + packageArray + ) + + if (writeResult.code !== Result.Success.code) { + return writeResult + } + + return this.getWriteResult(this.syncOpenRecord, params) + }, + /** * 清理用户 * @returns {Promise} @@ -1609,6 +1746,43 @@ export const useStarCloudStore = defineStore('starCloud', { characteristicValueCallback(new Result(decrypted[2])) } break + case subCmdIds.syncOpenRecord: + if (decrypted[2] === Result.Success.code && decrypted[6] > 0) { + const records = [] + const count = decrypted[6] || 0 + for (let i = 0; i < count; i++) { + let password = decrypted.slice(14 + 17 * i, 14 + 17 * i + 10) + if (password.every(item => item === 0)) { + password = null + } else { + password = uint8ArrayToString(password) + } + const record = { + type: decrypted[7 + 17 * i], + user: decrypted[8 + 17 * i] * 256 + decrypted[9 + 17 * i], + date: arrayToTimestamp(decrypted.slice(10 + 17 * i, 14 + 17 * i)) * 1000, + success: 1, + password: password + } + records.push(record) + } + const { code, message } = await uploadRecordRequest({ + records, + lockId: this.lockInfo.lockId + }) + characteristicValueCallback( + new Result( + code, + { + count + }, + message + ) + ) + } else { + characteristicValueCallback(new Result(decrypted[2])) + } + break default: break }