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

169 lines
5.9 KiB
JavaScript
Raw Normal View History

2025-06-10 17:47:09 +08:00
import {
checkRequiredFields,
2025-06-11 17:29:21 +08:00
createPackageEnd,
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";
/**
* 开始搜索wifi
* @returns {Promise<unknown>}
*/
export async function startSearchWiFi(params) {
const uid = this.accountInfo.uid.toString()
// 设置执行账号
2025-06-11 17:29:21 +08:00
const result = await this.login(params.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) {
const cardRequiredFields = ['ssid', 'password'];
const missingField = checkRequiredFields(params, cardRequiredFields);
if (missingField) {
return new Result(Result.codes.NotMoreData, null, `参数信息不完整: ${missingField}`);
}
2025-06-10 17:47:09 +08:00
2025-06-11 17:29:21 +08:00
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
2025-06-10 17:47:09 +08:00
2025-06-11 17:29:21 +08:00
const uid = this.accountInfo.uid.toString()
// 设置执行账号
const result = await this.login(params.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)
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)
}