Skip to content

Commit 01ed6ef

Browse files
committed
Add new control request to set wifi creds
1 parent 6a0eb44 commit 01ed6ef

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

src/cmd/serial.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,7 @@ module.exports = class SerialCommand extends CLICommandBase {
534534

535535
// if device's firmware version is less than 6.0.0, use the old way
536536
const fwVer = device.firmwareVersion;
537+
// FIXME: get the correct version number
537538
if (semver.lt(fwVer, '6.0.0')) {
538539
// configure serial
539540
if (file){

src/lib/wifi-control-request.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ module.exports = class WiFiControlRequest {
3131
} else {
3232
network = await this.getNetworkToConnect();
3333
}
34-
await this.joinWifi(network);
34+
await this.setWifiCredentials(network);
35+
console.log('Done! WiFi credentials have been set.');
3536
}
3637

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

209+
async setWifiCredentials({ ssid, password }) {
210+
// open device by id
211+
let retries = RETRY_COUNT;
212+
const spin = this.newSpin(`Setting Wi-Fi credentials for '${ssid}'`).start();
213+
let lastError;
214+
while (retries > 0) {
215+
try {
216+
if (!this.device || this.device.isOpen === false) {
217+
this.device = await usbUtils.getOneUsbDevice({ api: this.api, idOrName: this.deviceId });
218+
}
219+
await this.device.setWifiCredentials({ ssid, password });
220+
this.stopSpin();
221+
return;
222+
} catch (error) {
223+
lastError = error;
224+
await utilities.delay(TIME_BETWEEN_RETRIES);
225+
retries--;
226+
} finally {
227+
if (this.device && this.device.isOpen) {
228+
await this.device.close();
229+
}
230+
}
231+
}
232+
this.stopSpin();
233+
throw this._handleDeviceError(lastError, { action: 'set Wi-Fi credentials' });
234+
}
235+
208236
async pickNetworkManually() {
209237
const ssid = await this._promptForSSID();
210238
const password = await this._promptForPassword();

0 commit comments

Comments
 (0)