starcloud-sdk-uniapp/star-cloud/configureNetwork.js

143 lines
4.6 KiB
JavaScript

import {
_convertToMilliseconds,
checkRequiredFields,
convertWeekdaysToNumber, createPackageEnd, md5Encrypt,
parseTimeToList,
timestampToArray
} from "../format.js";
import {cmdIds, Result, subCmdIds} from "../constant.js";
import {
checkRepeatCardName,
checkRepeatFaceName,
checkRepeatFingerprintName,
checkRepeatPalmVeinName,
checkRepeatRemoteName
} from "../api.js";
import {searchAndConnectDevice, writeBLECharacteristicValue} from "../uni/basic.js";
import {sm4} from "sm-crypto";
/**
* 开始搜索wifi
* @returns {Promise<unknown>}
*/
export async function startSearchWiFi(params) {
const uid = this.accountInfo.uid.toString()
// 设置执行账号
const result = await this.login(uid)
if (result.code !== Result.Success.code) {
return result
}
// 确认设备连接正常
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()
if (!checkResult) {
return {
code: -1
}
}
const length = 2 + 1 + 1 + 40 + 20 + 2 + 2 + 1 + 1 + 1 + 4 + 1 + 1 + 4 + 4 + 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
// 子命令
if (type === 'card') {
contentArray[2] = subCmdIds.registerCard
} else if (type === 'fingerprint') {
contentArray[2] = subCmdIds.registerFingerprint
} else if (type === 'face') {
contentArray[2] = subCmdIds.registerFace
} else if (type === 'remote') {
contentArray[2] = subCmdIds.registerRemote
} else if (type === 'palmVein') {
contentArray[2] = subCmdIds.registerPalmVein
}
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)
}
// 子命令
if (type === 'card') {
contentArray[64] = (params.cardNumber || 0) / 256
contentArray[65] = (params.cardNumber || 0) % 256
} else if (type === 'fingerprint') {
contentArray[64] = (params.fingerprintNumber || 0) / 256
contentArray[65] = (params.fingerprintNumber || 0) % 256
} else if (type === 'face') {
contentArray[64] = (params.faceNumber || 0) / 256
contentArray[65] = (params.faceNumber || 0) % 256
} else if (type === 'remote') {
contentArray[64] = (params.remoteNumber || 0) / 256
contentArray[65] = (params.remoteNumber || 0) % 256
} else if (type === 'palmVein') {
contentArray[64] = (params.palmVeinNumber || 0) / 256
contentArray[65] = (params.palmVeinNumber || 0) % 256
}
contentArray[66] = (userCountLimit || 0xffff) / 256
contentArray[67] = (userCountLimit || 0xffff) % 256
contentArray[68] = operate
contentArray[69] = isAdmin || 0
contentArray[70] = isForce || 0
contentArray.set(this.lockInfo.token || new Uint8Array([0, 0, 0, 0]), 71)
contentArray[75] = isRound
contentArray[76] = convertWeekdaysToNumber(weekDay)
contentArray.set(timestampToArray(startDate), 77)
contentArray.set(timestampToArray(endDate), 81)
if (isRound) {
contentArray.set(parseTimeToList(startTime), 85)
contentArray.set(parseTimeToList(endTime), 89)
} else {
contentArray.set(new Uint8Array([0, 0, 0, 0]), 85)
contentArray.set(new Uint8Array([0, 0, 0, 0]), 89)
}
contentArray[93] = 16
const md5Array = md5Encrypt(
keyId + uid,
this.lockInfo.token || new Uint8Array([0, 0, 0, 0]),
this.lockInfo.bluetooth.signKey
)
contentArray.set(md5Array, 94)
console.log('加密前:', Array.from(contentArray))
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.registerExtendedProducts, params)
}