205 lines
7.0 KiB
JavaScript
205 lines
7.0 KiB
JavaScript
import {
|
||
checkRequiredFields,
|
||
createPackageEnd, generateRandomString,
|
||
} from "../format.js";
|
||
import {cmdIds, Result} from "../constant.js";
|
||
import {searchAndConnectDevice, writeBLECharacteristicValue} from "../uni/basic.js";
|
||
import {getDeviceNetworkInfo, getGatewayConfig} from "../api.js";
|
||
|
||
/**
|
||
* 开始搜索wifi
|
||
* @returns {Promise<unknown>}
|
||
*/
|
||
export async function startSearchWiFi(params = {}) {
|
||
params.connected = typeof params.connected === 'boolean' ? params.connected : false;
|
||
params.disconnect = typeof params.disconnect === 'boolean' ? params.disconnect : false;
|
||
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.code !== Result.Success.code) {
|
||
return checkResult
|
||
}
|
||
|
||
// 判断锁是否支持wifi功能
|
||
if (!this.lockInfo.lockFeature || this.lockInfo.lockFeature.wifi !== 1) {
|
||
return new Result(Result.codes.Fail, null, '该锁不支持wifi配置');
|
||
}
|
||
|
||
|
||
const length = 2 + 20
|
||
const headArray = this.createPackageHeader(0, length)
|
||
const contentArray = new Uint8Array(length)
|
||
|
||
contentArray[0] = cmdIds.searchWiFi / 256
|
||
contentArray[1] = cmdIds.searchWiFi % 256
|
||
for (let i = 0; i < uid.length; i++) {
|
||
contentArray[i + 2] = i < uid.length ? uid.charCodeAt(i) : 0;
|
||
}
|
||
const packageArray = createPackageEnd(headArray, contentArray)
|
||
|
||
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.startSearchWiFi, params)
|
||
}
|
||
|
||
/**
|
||
* 连接wifi
|
||
* @param params
|
||
* @returns {Promise<unknown>}
|
||
*/
|
||
export async function connectWiFi(params) {
|
||
const cardRequiredFields = ['ssid', 'password', 'clientId', 'starLockPeerId'];
|
||
const missingField = checkRequiredFields(params, cardRequiredFields);
|
||
if (missingField) {
|
||
return new Result(Result.codes.NotMoreData, null, `参数信息不完整: ${missingField}`);
|
||
}
|
||
|
||
// 获取网关配置
|
||
const config = await getGatewayConfig();
|
||
if (config.code !== Result.Success.code) {
|
||
return new Result(Result.codes.Fail, null, `获取网关配置失败: ${config.message}`);
|
||
}
|
||
|
||
const clientId = params.clientId;
|
||
const starLockPeerId = params.starLockPeerId;
|
||
let {
|
||
ssid,
|
||
password,
|
||
configureJson = JSON.stringify({
|
||
starcloudRpcPeerId: config.data.starcloudRpcPeerId,
|
||
starcloudReportPeerId: config.data.starcloudReportPeerId,
|
||
starcloudUrl: config.data.starcloudUrl,
|
||
userPeerId: config.data.userPeerId,
|
||
scdUrl: config.data.scdUrl,
|
||
starlockPeerId: starLockPeerId,
|
||
clientId: clientId,
|
||
secretKey: generateRandomString(18),
|
||
})
|
||
} = 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.code !== Result.Success.code) {
|
||
return checkResult
|
||
}
|
||
|
||
// 判断锁是否支持wifi功能
|
||
if (!this.lockInfo.lockFeature || this.lockInfo.lockFeature.wifi !== 1) {
|
||
return new Result(Result.codes.Fail, null, '该锁不支持wifi配置');
|
||
}
|
||
|
||
// 处理 configureJson
|
||
let configureJsonBytes = [];
|
||
if (configureJson) {
|
||
for (let i = 0; i < configureJson.length; i++) {
|
||
const code = configureJson.charCodeAt(i);
|
||
if (code < 0x80) {
|
||
configureJsonBytes.push(code);
|
||
} else if (code < 0x800) {
|
||
configureJsonBytes.push(0xc0 | (code >> 6));
|
||
configureJsonBytes.push(0x80 | (code & 0x3f));
|
||
} else {
|
||
configureJsonBytes.push(0xe0 | (code >> 12));
|
||
configureJsonBytes.push(0x80 | ((code >> 6) & 0x3f));
|
||
configureJsonBytes.push(0x80 | (code & 0x3f));
|
||
}
|
||
}
|
||
}
|
||
const configureJsonLength = configureJsonBytes.length;
|
||
|
||
// 计算总长度
|
||
const length = 2 + 30 + 20 + 2 + configureJsonLength;
|
||
const headArray = this.createPackageHeader(0, length);
|
||
const contentArray = new Uint8Array(length);
|
||
|
||
contentArray[0] = cmdIds.configureNetwork / 256;
|
||
contentArray[1] = cmdIds.configureNetwork % 256;
|
||
// SSID: 30字节,补0
|
||
for (let i = 0; i < 30; i++) {
|
||
contentArray[i + 2] = i < ssid.length ? ssid.charCodeAt(i) : 0;
|
||
}
|
||
// Password: 20字节,补0
|
||
for (let i = 0; i < 20; i++) {
|
||
contentArray[i + 32] = i < password.length ? password.charCodeAt(i) : 0;
|
||
}
|
||
// configureJsonLength: 2字节(大端序)
|
||
contentArray[52] = (configureJsonLength >> 8) & 0xff;
|
||
contentArray[53] = configureJsonLength & 0xff;
|
||
// configureJson内容
|
||
for (let i = 0; i < configureJsonLength; i++) {
|
||
contentArray[54 + i] = configureJsonBytes[i];
|
||
}
|
||
|
||
console.log("配网命令:", Array.from(contentArray))
|
||
const packageArray = createPackageEnd(headArray, contentArray)
|
||
console.log("蓝牙包:", Array.from(packageArray))
|
||
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.connectWiFi, params)
|
||
}
|
||
|
||
|
||
/**
|
||
* 获取锁配网信息
|
||
* @param params
|
||
* @param {Number} params.uid uid
|
||
* @param {Number} params.deviceMac 锁mac地址
|
||
* @returns {Promise<void>}
|
||
*/
|
||
export async function getLockNetworkInfo(params) {
|
||
const cardRequiredFields = ['uid', 'deviceMac'];
|
||
const missingField = checkRequiredFields(params, cardRequiredFields);
|
||
if (missingField) {
|
||
return new Result(Result.codes.NotMoreData, null, `参数信息不完整: ${missingField}`);
|
||
}
|
||
// 设置执行账号
|
||
const result = await this.login(params.uid)
|
||
if (result.code !== Result.Success.code) {
|
||
return result
|
||
}
|
||
|
||
return await getDeviceNetworkInfo({
|
||
deviceType: 2,
|
||
deviceMac: params.deviceMac,
|
||
})
|
||
} |