Enter credentials of the Wi-Fi network that you want your device to
connect to.
${error ? html`
${infoIcon}Device Info
Name${this._client!.info!.name}
Firmware${this._client!.info!.firmware}
Version${this._client!.info!.version}
Chip${this._client!.info!.chipFamily}
${
this._client!.info!.osName
? html`
OS${this._client!.info!.osName}
`
: nothing
}
${
this._client!.info!.osVersion
? html`
OS Version${this._client!.info!.osVersion}
`
: nothing
}
`;
}
private _togglePasswordVisibility(event: Event) {
event.preventDefault();
event.stopPropagation();
this._showPassword = !this._showPassword;
}
// Whether the network configuration form is the active view.
private get _showConfigureForm(): boolean {
return (
this._client?.state === ImprovSerialCurrentState.READY ||
(this._client?.state === ImprovSerialCurrentState.PROVISIONED &&
this._reprovision)
);
}
// Scan while (and only while) the configure form is shown. Driven from
// `updated()`, so entering the form starts scanning and leaving it stops.
private _syncScanning() {
const shouldScan =
this._state === "IMPROV-STATE" && !this._busy && this._showConfigureForm;
if (shouldScan === !!this._unsubSSIDs) {
return;
}
if (shouldScan) {
this._unsubSSIDs = this._client!.subscribeSSIDs((networks) => {
if (networks === null) {
// Scanning isn't available; fall back to manual network entry.
this._selectedSsid = null;
} else if (this._ssids == null) {
// First result: default to the network with the strongest signal.
this._selectedSsid = strongestSsid(networks);
} else if (
this._selectedSsid !== null &&
!networks.some((s) => s.name === this._selectedSsid)
) {
// A merged scan never drops networks, but the selection persists
// across scan restarts (after a failed provision, or reopening to
// change Wi-Fi), and the first fresh scan may not include it yet.
// Re-default when that happens; keep "Join other" (null) as-is.
this._selectedSsid = strongestSsid(networks);
}
this._ssids = networks;
});
} else {
this._stopScanning();
}
}
private async _stopScanning() {
if (!this._unsubSSIDs) {
return;
}
const unsubscribe = this._unsubSSIDs;
this._unsubSSIDs = undefined;
await unsubscribe();
}
private async _provision() {
// Read the form before setting `_busy`: that swaps the form for a progress
// view, and the render happens during any await below — after which the
// fields no longer exist and we'd provision with an empty password.
const ssid =
this._selectedSsid === null ? this._inputSSID.value : this._selectedSsid;
const password = this._inputPassword?.value || "";
this._busy = true;
// Wait for any in-flight scan to settle before provisioning so we don't
// have two RPC commands in flight at once. Marking busy above already tells
// `_syncScanning` to stop, but the provision RPC needs it stopped *now*.
await this._stopScanning();
try {
await this._client!.provision(
ssid,
password,
// Devices try to connect for ~30s before reporting failure. Our
// timeout must comfortably exceed that: it's only a safety net, and if
// it fires first the device's late failure packet rejects the *next*
// RPC we send (the resumed network scan) instead of this one.
45000,
);
// Success: leave the change-Wi-Fi form and show the provisioned screen.
this._reprovision = false;
} catch (err) {
// No need to do error handling because we listen for `error-changed`
// events. Clearing busy resumes scanning via `_syncScanning`.
console.log(err);
} finally {
this._busy = false;
}
}
private _changeWifi() {
this._reprovision = true;
}
protected updated(changedProps: PropertyValues) {
super.updated(changedProps);
if (changedProps.has("port") && this.port) {
this._connect();
}
this._syncScanning();
let toFocus: LitElement | undefined;
if (
changedProps.has("_ssids") &&
changedProps.get("_ssids") === undefined &&
this._ssids != null
) {
// Only focus on the first result, not on every background scan update.
toFocus = this._selectSSID;
} else if (
changedProps.has("_selectedSsid") &&
this._selectedSsid === null
) {
toFocus = this._inputSSID;
}
if (toFocus) {
toFocus.updateComplete.then(() => toFocus!.focus());
}
}
private async _connect() {
let client: ImprovSerial;
try {
client = new ImprovSerial(this.port!, this.logger);
} catch (err) {
this._state = "ERROR";
this._error = (err as any).message || err || "Unknown error";
return;
}
client.addEventListener("state-changed", () => {
this._state = "IMPROV-STATE";
this.requestUpdate();
});
client.addEventListener("error-changed", () => this.requestUpdate());
try {
await client.initialize(10000);
} catch (err: any) {
this._state = "ERROR";
this._error = this.learnMoreUrl
? html`
Unable to detect Improv service on connected device.