2025-06-10 17:47:09 +08:00
|
|
|
|
import {
|
|
|
|
|
|
checkRequiredFields,
|
2025-07-17 09:34:16 +08:00
|
|
|
|
createPackageEnd, generateRandomString,
|
2025-06-10 17:47:09 +08:00
|
|
|
|
} from "../format.js";
|
2025-06-11 17:29:21 +08:00
|
|
|
|
import {cmdIds, Result} from "../constant.js";
|
2025-06-10 17:47:09 +08:00
|
|
|
|
import {searchAndConnectDevice, writeBLECharacteristicValue} from "../uni/basic.js";
|
2025-07-01 11:46:05 +08:00
|
|
|
|
import {getDeviceNetworkInfo, getGatewayConfig} from "../api.js";
|
2025-06-10 17:47:09 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 开始搜索wifi
|
|
|
|
|
|
* @returns {Promise<unknown>}
|
|
|
|
|
|
*/
|
2025-06-11 18:04:49 +08:00
|
|
|
|
export async function startSearchWiFi(params = {}) {
|
|
|
|
|
|
params.connected = typeof params.connected === 'boolean' ? params.connected : false;
|
|
|
|
|
|
params.disconnect = typeof params.disconnect === 'boolean' ? params.disconnect : false;
|
2025-06-10 17:47:09 +08:00
|
|
|
|
const uid = this.accountInfo.uid.toString()
|
|
|
|
|
|
// 设置执行账号
|
2025-06-11 18:04:49 +08:00
|
|
|
|
const result = await this.login(uid)
|
2025-06-10 17:47:09 +08:00
|
|
|
|
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()
|
2025-06-11 17:29:21 +08:00
|
|
|
|
if (checkResult.code !== Result.Success.code) {
|
|
|
|
|
|
return checkResult
|
2025-06-10 17:47:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-11 17:29:21 +08:00
|
|
|
|
// 判断锁是否支持wifi功能
|
|
|
|
|
|
if (!this.lockInfo.lockFeature || this.lockInfo.lockFeature.wifi !== 1) {
|
|
|
|
|
|
return new Result(Result.codes.Fail, null, '该锁不支持wifi配置');
|
2025-06-10 17:47:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-06-11 17:29:21 +08:00
|
|
|
|
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
|
2025-06-10 17:47:09 +08:00
|
|
|
|
for (let i = 0; i < uid.length; i++) {
|
2025-06-11 17:29:21 +08:00
|
|
|
|
contentArray[i + 2] = i < uid.length ? uid.charCodeAt(i) : 0;
|
2025-06-10 17:47:09 +08:00
|
|
|
|
}
|
2025-06-11 17:29:21 +08:00
|
|
|
|
const packageArray = createPackageEnd(headArray, contentArray)
|
2025-06-10 17:47:09 +08:00
|
|
|
|
|
2025-06-11 17:29:21 +08:00
|
|
|
|
const writeResult = await writeBLECharacteristicValue(this.lockInfo.deviceId,
|
|
|
|
|
|
this.lockInfo.serviceId,
|
|
|
|
|
|
this.lockInfo.writeCharacteristicId,
|
|
|
|
|
|
packageArray)
|
|
|
|
|
|
if (writeResult.code !== Result.Success.code) {
|
|
|
|
|
|
return writeResult
|
2025-06-10 17:47:09 +08:00
|
|
|
|
}
|
2025-06-11 17:29:21 +08:00
|
|
|
|
return this.getWriteResult(this.startSearchWiFi, params)
|
|
|
|
|
|
}
|
2025-06-10 17:47:09 +08:00
|
|
|
|
|
2025-06-11 17:29:21 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 连接wifi
|
|
|
|
|
|
* @param params
|
|
|
|
|
|
* @returns {Promise<unknown>}
|
|
|
|
|
|
*/
|
|
|
|
|
|
export async function connectWiFi(params) {
|
2025-07-17 10:08:31 +08:00
|
|
|
|
const cardRequiredFields = ['ssid', 'password', 'clientId', 'starLockPeerId'];
|
2025-06-11 17:29:21 +08:00
|
|
|
|
const missingField = checkRequiredFields(params, cardRequiredFields);
|
|
|
|
|
|
if (missingField) {
|
|
|
|
|
|
return new Result(Result.codes.NotMoreData, null, `参数信息不完整: ${missingField}`);
|
|
|
|
|
|
}
|
2025-06-10 17:47:09 +08:00
|
|
|
|
|
2025-07-01 10:28:43 +08:00
|
|
|
|
// 获取网关配置
|
|
|
|
|
|
const config = await getGatewayConfig();
|
|
|
|
|
|
if (config.code !== Result.Success.code) {
|
|
|
|
|
|
return new Result(Result.codes.Fail, null, `获取网关配置失败: ${config.message}`);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-17 09:34:16 +08:00
|
|
|
|
const clientId = params.clientId;
|
2025-07-17 09:44:24 +08:00
|
|
|
|
const starLockPeerId = params.starLockPeerId;
|
2025-06-11 17:29:21 +08:00
|
|
|
|
let {
|
|
|
|
|
|
ssid,
|
|
|
|
|
|
password,
|
|
|
|
|
|
configureJson = JSON.stringify({
|
2025-07-01 11:46:05 +08:00
|
|
|
|
starcloudRpcPeerId: config.data.starcloudRpcPeerId,
|
|
|
|
|
|
starcloudReportPeerId: config.data.starcloudReportPeerId,
|
|
|
|
|
|
starcloudUrl: config.data.starcloudUrl,
|
2025-07-17 10:08:31 +08:00
|
|
|
|
userPeerId: config.data.userPeerId,
|
2025-07-01 11:46:05 +08:00
|
|
|
|
scdUrl: config.data.scdUrl,
|
2025-07-17 09:44:24 +08:00
|
|
|
|
starlockPeerId: starLockPeerId,
|
2025-07-17 09:34:16 +08:00
|
|
|
|
clientId: clientId,
|
|
|
|
|
|
secretKey: generateRandomString(18),
|
2025-06-11 17:29:21 +08:00
|
|
|
|
})
|
|
|
|
|
|
} = params
|
2025-06-10 17:47:09 +08:00
|
|
|
|
|
|
|
|
|
|
|
2025-06-11 17:29:21 +08:00
|
|
|
|
const uid = this.accountInfo.uid.toString()
|
|
|
|
|
|
// 设置执行账号
|
2025-07-01 10:28:43 +08:00
|
|
|
|
const result = await this.login(uid)
|
2025-06-11 17:29:21 +08:00
|
|
|
|
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)
|
2025-06-10 17:47:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-11 17:29:21 +08:00
|
|
|
|
// 检查是否已添加为用户
|
|
|
|
|
|
const checkResult = await this.checkLockUser()
|
|
|
|
|
|
if (checkResult.code !== Result.Success.code) {
|
|
|
|
|
|
return checkResult
|
|
|
|
|
|
}
|
2025-06-10 17:47:09 +08:00
|
|
|
|
|
2025-06-11 17:29:21 +08:00
|
|
|
|
// 判断锁是否支持wifi功能
|
|
|
|
|
|
if (!this.lockInfo.lockFeature || this.lockInfo.lockFeature.wifi !== 1) {
|
|
|
|
|
|
return new Result(Result.codes.Fail, null, '该锁不支持wifi配置');
|
|
|
|
|
|
}
|
2025-06-10 17:47:09 +08:00
|
|
|
|
|
2025-06-11 17:29:21 +08:00
|
|
|
|
// 处理 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];
|
|
|
|
|
|
}
|
2025-06-10 17:47:09 +08:00
|
|
|
|
|
2025-06-11 17:29:21 +08:00
|
|
|
|
console.log("配网命令:", Array.from(contentArray))
|
|
|
|
|
|
const packageArray = createPackageEnd(headArray, contentArray)
|
|
|
|
|
|
console.log("蓝牙包:", Array.from(packageArray))
|
2025-06-10 17:47:09 +08:00
|
|
|
|
const writeResult = await writeBLECharacteristicValue(this.lockInfo.deviceId,
|
|
|
|
|
|
this.lockInfo.serviceId,
|
|
|
|
|
|
this.lockInfo.writeCharacteristicId,
|
|
|
|
|
|
packageArray)
|
|
|
|
|
|
if (writeResult.code !== Result.Success.code) {
|
|
|
|
|
|
return writeResult
|
|
|
|
|
|
}
|
2025-06-11 17:29:21 +08:00
|
|
|
|
return this.getWriteResult(this.connectWiFi, params)
|
2025-07-01 11:46:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 获取锁配网信息
|
|
|
|
|
|
* @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,
|
|
|
|
|
|
})
|
2025-06-11 17:29:21 +08:00
|
|
|
|
}
|