Skip to content

Commit

Permalink
Add new control request to set wifi creds
Browse files Browse the repository at this point in the history
  • Loading branch information
keeramis committed May 16, 2024
1 parent 6a0eb44 commit 01ed6ef
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/cmd/serial.js
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,7 @@ module.exports = class SerialCommand extends CLICommandBase {

// if device's firmware version is less than 6.0.0, use the old way
const fwVer = device.firmwareVersion;
// FIXME: get the correct version number
if (semver.lt(fwVer, '6.0.0')) {
// configure serial
if (file){
Expand Down
30 changes: 29 additions & 1 deletion src/lib/wifi-control-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ module.exports = class WiFiControlRequest {
} else {
network = await this.getNetworkToConnect();
}
await this.joinWifi(network);
await this.setWifiCredentials(network);
console.log('Done! WiFi credentials have been set.');
}

async getNetworkToConnectFromJson() {
Expand Down Expand Up @@ -205,6 +206,33 @@ module.exports = class WiFiControlRequest {
throw this._handleDeviceError(lastError, { action: 'join Wi-Fi network' });
}

async setWifiCredentials({ ssid, password }) {
// open device by id
let retries = RETRY_COUNT;
const spin = this.newSpin(`Setting Wi-Fi credentials for '${ssid}'`).start();
let lastError;
while (retries > 0) {
try {
if (!this.device || this.device.isOpen === false) {
this.device = await usbUtils.getOneUsbDevice({ api: this.api, idOrName: this.deviceId });
}
await this.device.setWifiCredentials({ ssid, password });
this.stopSpin();
return;
} catch (error) {
lastError = error;
await utilities.delay(TIME_BETWEEN_RETRIES);
retries--;
} finally {
if (this.device && this.device.isOpen) {
await this.device.close();
}
}
}
this.stopSpin();
throw this._handleDeviceError(lastError, { action: 'set Wi-Fi credentials' });
}

async pickNetworkManually() {
const ssid = await this._promptForSSID();
const password = await this._promptForPassword();
Expand Down

0 comments on commit 01ed6ef

Please sign in to comment.