180 lines
6.4 KiB
JavaScript
180 lines
6.4 KiB
JavaScript
import {
|
||
checkRequiredFields,
|
||
createPackageEnd,
|
||
} from "../format.js";
|
||
import {cmdIds, Result} from "../constant.js";
|
||
import {searchAndConnectDevice, writeBLECharacteristicValue} from "../uni/basic.js";
|
||
import {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'];
|
||
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}`);
|
||
}
|
||
|
||
console.log(config)
|
||
|
||
let {
|
||
ssid,
|
||
password,
|
||
configureJson = JSON.stringify({
|
||
starcloudRpcPeerId: "6HnEcGnXMUcLQoE7rnC4aXMVJmojMnKAjqKHrt4TmN1U",
|
||
starcloudReportPeerId: "G3ehYz8djE35CTE2LWn5xe2nD51UpjC4hWd3vqVmXViE",
|
||
starcloudUrl: "http://cloud.skychip.top",
|
||
userPeerId: "C2HjHNy9LsjxW2QEmceiNDTN6XSXFDUZ3fYsnBigVQXA",
|
||
scdUrl: "http://scd.skychip.top:8710",
|
||
starlockPeerId: "0b3bd6327daafe2da24fdd0cae76c71477f32e3ef8ab",
|
||
clientId: "sBfWAwdMqVKIMBj4dPuRextHViC266aE",
|
||
secretKey: "zNn1AluC6sTVAtA4dX",
|
||
userPeerld: "zC2HjHNy9LsjxW2QEmceiNDTN6XSXFDUZ3fYsnBigVQX"
|
||
})
|
||
} = 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)
|
||
} |