@improv-wifi/serial-sdk 3.0.0 → 3.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md
CHANGED
|
@@ -84,7 +84,7 @@ await improv.initialize();
|
|
|
84
84
|
|
|
85
85
|
improv.addEventListener("disconnect", console.log);
|
|
86
86
|
|
|
87
|
-
// Optional: keeps `improv.networkState` fresh (e.g. an Ethernet link coming up).
|
|
87
|
+
// Optional: keeps `improv.networkState` and `improv.state` fresh (e.g. an Ethernet link coming up).
|
|
88
88
|
improv.startNetworkStatePolling();
|
|
89
89
|
|
|
90
90
|
console.log({
|
package/dist/serial.d.ts
CHANGED
|
@@ -58,6 +58,7 @@ export declare class ImprovSerial extends EventTarget {
|
|
|
58
58
|
* so each poll settles quickly.
|
|
59
59
|
*/
|
|
60
60
|
requestCurrentState(timeout?: number): Promise<void>;
|
|
61
|
+
private _requestCurrentState;
|
|
61
62
|
requestInfo(timeout?: number): Promise<void>;
|
|
62
63
|
provision(ssid: string, password: string, timeout?: number): Promise<void>;
|
|
63
64
|
scan(timeout?: number): Promise<Ssid[]>;
|
|
@@ -114,10 +115,10 @@ export declare class ImprovSerial extends EventTarget {
|
|
|
114
115
|
*/
|
|
115
116
|
requestNetworkState(timeout?: number): Promise<NetworkState>;
|
|
116
117
|
/**
|
|
117
|
-
* Poll the device's network state, keeping `networkState` fresh.
|
|
118
|
-
* stopped by `stopNetworkStatePolling()`, `close()`, a disconnect,
|
|
119
|
-
* device reporting the command unsupported. A failed poll keeps the
|
|
120
|
-
* known state, leaves `error` untouched and retries on the next tick.
|
|
118
|
+
* Poll the device's network state, keeping `networkState` and `state` fresh.
|
|
119
|
+
* Idempotent; stopped by `stopNetworkStatePolling()`, `close()`, a disconnect,
|
|
120
|
+
* or the device reporting the command unsupported. A failed poll keeps the
|
|
121
|
+
* last known state, leaves `error` untouched and retries on the next tick.
|
|
121
122
|
*/
|
|
122
123
|
startNetworkStatePolling(interval?: number, timeout?: number): void;
|
|
123
124
|
stopNetworkStatePolling(): void;
|
package/dist/serial.js
CHANGED
|
@@ -133,23 +133,32 @@ export class ImprovSerial extends EventTarget {
|
|
|
133
133
|
* to switch network interfaces) can pass a timeout shorter than the default
|
|
134
134
|
* so each poll settles quickly.
|
|
135
135
|
*/
|
|
136
|
-
|
|
136
|
+
requestCurrentState(timeout) {
|
|
137
|
+
return this._requestCurrentState(timeout, false);
|
|
138
|
+
}
|
|
139
|
+
async _requestCurrentState(timeout, quiet) {
|
|
137
140
|
var _a;
|
|
138
141
|
// Every device reports its state; a provisioned one also returns the next
|
|
139
142
|
// URL. Attach the listener before sending, then wait for the state change;
|
|
140
143
|
// the RPC promise is only used to surface a command error.
|
|
144
|
+
const command = 2 /* ImprovSerialRPCCommand.REQUEST_CURRENT_STATE */;
|
|
141
145
|
const abort = new AbortController();
|
|
142
146
|
let rpcResult;
|
|
143
147
|
try {
|
|
144
148
|
await new Promise((resolve, reject) => {
|
|
145
|
-
this.
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
149
|
+
rpcResult = this._enqueueRPC(() => new Promise((resolveRPC, rejectRPC) => {
|
|
150
|
+
// Not up front: a reply to an earlier state request isn't ours.
|
|
151
|
+
this.addEventListener("state-changed", () => resolve(), {
|
|
152
|
+
signal: abort.signal,
|
|
153
|
+
});
|
|
154
|
+
this._rpcFeedback = {
|
|
155
|
+
command,
|
|
156
|
+
resolve: resolveRPC,
|
|
157
|
+
reject: rejectRPC,
|
|
158
|
+
quiet,
|
|
159
|
+
};
|
|
160
|
+
this._sendRPC(command, []);
|
|
161
|
+
}), timeout !== null && timeout !== void 0 ? timeout : DEFAULT_RPC_TIMEOUT);
|
|
153
162
|
rpcResult.catch(reject);
|
|
154
163
|
});
|
|
155
164
|
}
|
|
@@ -312,10 +321,10 @@ export class ImprovSerial extends EventTarget {
|
|
|
312
321
|
return this._requestNetworkState(timeout, false);
|
|
313
322
|
}
|
|
314
323
|
/**
|
|
315
|
-
* Poll the device's network state, keeping `networkState` fresh.
|
|
316
|
-
* stopped by `stopNetworkStatePolling()`, `close()`, a disconnect,
|
|
317
|
-
* device reporting the command unsupported. A failed poll keeps the
|
|
318
|
-
* known state, leaves `error` untouched and retries on the next tick.
|
|
324
|
+
* Poll the device's network state, keeping `networkState` and `state` fresh.
|
|
325
|
+
* Idempotent; stopped by `stopNetworkStatePolling()`, `close()`, a disconnect,
|
|
326
|
+
* or the device reporting the command unsupported. A failed poll keeps the
|
|
327
|
+
* last known state, leaves `error` untouched and retries on the next tick.
|
|
319
328
|
*/
|
|
320
329
|
startNetworkStatePolling(interval = NETWORK_STATE_POLL_INTERVAL, timeout = NETWORK_STATE_POLL_TIMEOUT) {
|
|
321
330
|
if (this._stopNetworkStatePolling ||
|
|
@@ -333,12 +342,17 @@ export class ImprovSerial extends EventTarget {
|
|
|
333
342
|
while (active) {
|
|
334
343
|
try {
|
|
335
344
|
await this._requestNetworkState(timeout, true);
|
|
345
|
+
if (!active) {
|
|
346
|
+
break;
|
|
347
|
+
}
|
|
348
|
+
// A reboot onto another interface silently changes the Wi-Fi state.
|
|
349
|
+
await this._requestCurrentState(timeout, true);
|
|
336
350
|
}
|
|
337
351
|
catch (err) {
|
|
338
352
|
if (this.networkState === null) {
|
|
339
353
|
break;
|
|
340
354
|
}
|
|
341
|
-
this.logger.debug(`
|
|
355
|
+
this.logger.debug(`Network state poll failed: ${err}`);
|
|
342
356
|
}
|
|
343
357
|
if (!active) {
|
|
344
358
|
break;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
const e=(e,o,t,n)=>{n=n||{};const r=new CustomEvent(o,{bubbles:void 0===n.bubbles||n.bubbles,cancelable:Boolean(n.cancelable),composed:void 0===n.composed||n.composed,detail:t});e.dispatchEvent(r)};class o extends HTMLElement{connectedCallback(){if(this.renderRoot)return;if(this.renderRoot=this.attachShadow({mode:"open"}),!o.isSupported||!o.isAllowed)return this.toggleAttribute("not-supported",!0),void(this.renderRoot.innerHTML=o.isAllowed?"<slot name='unsupported'>Your browser does not support serial provisioning. Use Google Chrome, Microsoft Edge or Mozilla Firefox.</slot>":"<slot name='not-allowed'>You can only use Improv on HTTPS sites or localhost.</slot>");this.toggleAttribute("supported",!0);const t=document.createElement("slot");t.name="activate";const n=document.createElement("button");if(n.innerText="Connect device to Wi-Fi",t.append(n),t.addEventListener("click",async o=>{o.preventDefault(),(async o=>{let t;import("./serial-provision-dialog-
|
|
1
|
+
const e=(e,o,t,n)=>{n=n||{};const r=new CustomEvent(o,{bubbles:void 0===n.bubbles||n.bubbles,cancelable:Boolean(n.cancelable),composed:void 0===n.composed||n.composed,detail:t});e.dispatchEvent(r)};class o extends HTMLElement{connectedCallback(){if(this.renderRoot)return;if(this.renderRoot=this.attachShadow({mode:"open"}),!o.isSupported||!o.isAllowed)return this.toggleAttribute("not-supported",!0),void(this.renderRoot.innerHTML=o.isAllowed?"<slot name='unsupported'>Your browser does not support serial provisioning. Use Google Chrome, Microsoft Edge or Mozilla Firefox.</slot>":"<slot name='not-allowed'>You can only use Improv on HTTPS sites or localhost.</slot>");this.toggleAttribute("supported",!0);const t=document.createElement("slot");t.name="activate";const n=document.createElement("button");if(n.innerText="Connect device to Wi-Fi",t.append(n),t.addEventListener("click",async o=>{o.preventDefault(),(async o=>{let t;import("./serial-provision-dialog-BHc0Gznm.js");try{t=await navigator.serial.requestPort()}catch(e){if("NotFoundError"===e.name)return;return void alert(`Error: ${e.message}`)}if(!t)return;await t.open({baudRate:115200});const n=document.createElement("improv-wifi-serial-provision-dialog");n.port=t,n.addEventListener("closed",async n=>{await t.close(),e(o,"closed",n.detail)},{once:!0}),document.body.appendChild(n)})(this)}),"adoptedStyleSheets"in Document.prototype&&"replaceSync"in CSSStyleSheet.prototype){const e=new CSSStyleSheet;e.replaceSync(o.style),this.renderRoot.adoptedStyleSheets=[e]}else{const e=document.createElement("style");e.innerText=o.style,this.renderRoot.append(e)}this.renderRoot.append(t)}}o.isSupported="serial"in navigator,o.isAllowed=window.isSecureContext,o.style='\n button {\n position: relative;\n cursor: pointer;\n font-size: 14px;\n padding: 8px 28px;\n color: var(--improv-on-primary-color, #fff);\n background-color: var(--improv-primary-color, #03a9f4);\n border: none;\n border-radius: 4px;\n box-shadow: 0 2px 2px 0 rgba(0,0,0,.14), 0 3px 1px -2px rgba(0,0,0,.12), 0 1px 5px 0 rgba(0,0,0,.2);\n }\n button::before {\n content: " ";\n position: absolute;\n top: 0;\n bottom: 0;\n left: 0;\n right: 0;\n opacity: 0.2;\n border-radius: 4px;\n }\n button:hover {\n box-shadow: 0 4px 8px 0 rgba(0,0,0,.14), 0 1px 7px 0 rgba(0,0,0,.12), 0 3px 1px -1px rgba(0,0,0,.2);\n }\n button:hover::before {\n background-color: rgba(255,255,255,.8);\n }\n button:focus {\n outline: none;\n }\n button:focus::before {\n background-color: white;\n }\n button:active::before {\n background-color: grey;\n }\n',customElements.define("improv-wifi-serial-launch-button",o);export{e as f};
|
package/dist/web/{serial-provision-dialog-DR-raZEr.js → serial-provision-dialog-BHc0Gznm.js}
RENAMED
|
@@ -490,78 +490,78 @@ import{f as e}from"./serial-launch-button.js";function t(e,t,o,i){var r,n=argume
|
|
|
490
490
|
name="trailing-supporting-text"
|
|
491
491
|
slot="trailing-supporting-text"></slot>
|
|
492
492
|
`}onFocus(){-1===this.tabIndex&&this.dispatchEvent(new Event("request-activation",{bubbles:!0,composed:!0}))}focus(){this.listItemRoot?.focus()}click(){this.listItemRoot?this.listItemRoot.click():super.click()}}Di.shadowRootOptions={...he.shadowRootOptions,delegatesFocus:!0},t([fe({type:Boolean,reflect:!0})],Di.prototype,"disabled",void 0),t([fe({reflect:!0})],Di.prototype,"type",void 0),t([fe({type:Boolean,attribute:"md-list-item",reflect:!0})],Di.prototype,"isListItem",void 0),t([fe()],Di.prototype,"href",void 0),t([fe()],Di.prototype,"target",void 0),t([ye(".list-item")],Di.prototype,"listItemRoot",void 0);const Ni=a`:host{display:flex;gap:16px;-webkit-tap-highlight-color:rgba(0,0,0,0);--md-ripple-hover-color: var(--md-list-item-hover-state-layer-color, var(--md-sys-color-on-surface, #1d1b20));--md-ripple-hover-opacity: var(--md-list-item-hover-state-layer-opacity, 0.08);--md-ripple-pressed-color: var(--md-list-item-pressed-state-layer-color, var(--md-sys-color-on-surface, #1d1b20));--md-ripple-pressed-opacity: var(--md-list-item-pressed-state-layer-opacity, 0.12)}:host(:is([type=button]:not([disabled]),[type=link])){cursor:pointer}md-focus-ring{z-index:1;--md-focus-ring-shape: 8px}a,button,li{background:none;border:none;cursor:inherit;padding:0;margin:0;text-align:unset;text-decoration:none}.list-item{border-radius:inherit;display:flex;flex:1;gap:inherit;max-width:inherit;min-width:inherit;outline:none;-webkit-tap-highlight-color:rgba(0,0,0,0);width:100%}.list-item.interactive{cursor:pointer}.list-item.disabled{opacity:var(--md-list-item-disabled-opacity, 0.3);pointer-events:none}[slot=container]{pointer-events:none}md-ripple{border-radius:inherit}md-item{border-radius:inherit;flex:1;height:100%;color:var(--md-list-item-label-text-color, var(--md-sys-color-on-surface, #1d1b20));font-family:var(--md-list-item-label-text-font, var(--md-sys-typescale-body-large-font, var(--md-ref-typeface-plain, Roboto)));font-size:var(--md-list-item-label-text-size, var(--md-sys-typescale-body-large-size, 1rem));line-height:var(--md-list-item-label-text-line-height, var(--md-sys-typescale-body-large-line-height, 1.5rem));font-weight:var(--md-list-item-label-text-weight, var(--md-sys-typescale-body-large-weight, var(--md-ref-typeface-weight-regular, 400)));min-height:var(--md-list-item-one-line-container-height, 56px);padding-top:var(--md-list-item-top-space, 12px);padding-bottom:var(--md-list-item-bottom-space, 12px);padding-inline-start:var(--md-list-item-leading-space, 16px);padding-inline-end:var(--md-list-item-trailing-space, 16px);gap:inherit}md-item[multiline]{min-height:var(--md-list-item-two-line-container-height, 72px)}[slot=supporting-text]{color:var(--md-list-item-supporting-text-color, var(--md-sys-color-on-surface-variant, #49454f));font-family:var(--md-list-item-supporting-text-font, var(--md-sys-typescale-body-medium-font, var(--md-ref-typeface-plain, Roboto)));font-size:var(--md-list-item-supporting-text-size, var(--md-sys-typescale-body-medium-size, 0.875rem));line-height:var(--md-list-item-supporting-text-line-height, var(--md-sys-typescale-body-medium-line-height, 1.25rem));font-weight:var(--md-list-item-supporting-text-weight, var(--md-sys-typescale-body-medium-weight, var(--md-ref-typeface-weight-regular, 400)))}[slot=trailing-supporting-text]{color:var(--md-list-item-trailing-supporting-text-color, var(--md-sys-color-on-surface-variant, #49454f));font-family:var(--md-list-item-trailing-supporting-text-font, var(--md-sys-typescale-label-small-font, var(--md-ref-typeface-plain, Roboto)));font-size:var(--md-list-item-trailing-supporting-text-size, var(--md-sys-typescale-label-small-size, 0.6875rem));line-height:var(--md-list-item-trailing-supporting-text-line-height, var(--md-sys-typescale-label-small-line-height, 1rem));font-weight:var(--md-list-item-trailing-supporting-text-weight, var(--md-sys-typescale-label-small-weight, var(--md-ref-typeface-weight-medium, 500)))}:is([slot=start],[slot=end])::slotted(*){fill:currentColor}[slot=start]{color:var(--md-list-item-leading-icon-color, var(--md-sys-color-on-surface-variant, #49454f))}[slot=end]{color:var(--md-list-item-trailing-icon-color, var(--md-sys-color-on-surface-variant, #49454f))}@media(forced-colors: active){.disabled slot{color:GrayText}.list-item.disabled{color:GrayText;opacity:1}}
|
|
493
|
-
`;Ni.styleSheet;let Fi=class extends Di{};Fi.styles=[Ni],Fi=t([ue("md-list-item")],Fi);const Ui=["I".charCodeAt(0),"M".charCodeAt(0),"P".charCodeAt(0),"R".charCodeAt(0),"O".charCodeAt(0),"V".charCodeAt(0),1];var Bi,Mi;!function(e){e[e.CURRENT_STATE=1]="CURRENT_STATE",e[e.ERROR_STATE=2]="ERROR_STATE",e[e.RPC=3]="RPC",e[e.RPC_RESULT=4]="RPC_RESULT"}(Bi||(Bi={})),function(e){e[e.STOPPED=0]="STOPPED",e[e.READY=2]="READY",e[e.PROVISIONING=3]="PROVISIONING",e[e.PROVISIONED=4]="PROVISIONED"}(Mi||(Mi={}));const Vi={0:"NO_ERROR",1:"INVALID_RPC_PACKET",2:"UNKNOWN_RPC_COMMAND",3:"UNABLE_TO_CONNECT",5:"BAD_HOSTNAME",254:"TIMEOUT",255:"UNKNOWN_ERROR"};class Hi extends Error{constructor(){super("Port is not ready")}}const Wi=e=>"["+e.map(e=>((e,t=2)=>{let o=e.toString(16).toUpperCase();return o.startsWith("-")?"-0x"+o.substring(1).padStart(t,"0"):"0x"+o.padStart(t,"0")})(e)).join(", ")+"]",ji=e=>e.sort((e,t)=>e.name.toLocaleLowerCase().localeCompare(t.name.toLocaleLowerCase())),Zi=(e,t)=>{const o=new Map;for(const t of e)o.set(t.name,t);for(const e of t)o.set(e.name,e);return ji(Array.from(o.values()))},Ki=(e,t)=>e.length!==t.length||e.some((e,o)=>e.name!==t[o].name||e.rssi!==t[o].rssi||e.secured!==t[o].secured);class
|
|
493
|
+
`;Ni.styleSheet;let Fi=class extends Di{};Fi.styles=[Ni],Fi=t([ue("md-list-item")],Fi);const Ui=["I".charCodeAt(0),"M".charCodeAt(0),"P".charCodeAt(0),"R".charCodeAt(0),"O".charCodeAt(0),"V".charCodeAt(0),1];var Bi,Mi;!function(e){e[e.CURRENT_STATE=1]="CURRENT_STATE",e[e.ERROR_STATE=2]="ERROR_STATE",e[e.RPC=3]="RPC",e[e.RPC_RESULT=4]="RPC_RESULT"}(Bi||(Bi={})),function(e){e[e.STOPPED=0]="STOPPED",e[e.READY=2]="READY",e[e.PROVISIONING=3]="PROVISIONING",e[e.PROVISIONED=4]="PROVISIONED"}(Mi||(Mi={}));const Vi={0:"NO_ERROR",1:"INVALID_RPC_PACKET",2:"UNKNOWN_RPC_COMMAND",3:"UNABLE_TO_CONNECT",5:"BAD_HOSTNAME",254:"TIMEOUT",255:"UNKNOWN_ERROR"};class Hi extends Error{constructor(){super("Port is not ready")}}const Wi=e=>"["+e.map(e=>((e,t=2)=>{let o=e.toString(16).toUpperCase();return o.startsWith("-")?"-0x"+o.substring(1).padStart(t,"0"):"0x"+o.padStart(t,"0")})(e)).join(", ")+"]",ji=e=>e.sort((e,t)=>e.name.toLocaleLowerCase().localeCompare(t.name.toLocaleLowerCase())),Zi=(e,t)=>{const o=new Map;for(const t of e)o.set(t.name,t);for(const e of t)o.set(e.name,e);return ji(Array.from(o.values()))},Ki=3e4,Gi=(e,t)=>e.length!==t.length||e.some((e,o)=>e.name!==t[o].name||e.rssi!==t[o].rssi||e.secured!==t[o].secured);class Yi extends EventTarget{get error(){return this._error}set error(e){this._error=e,this.dispatchEvent(new CustomEvent("error-changed",{detail:this._error}))}get networkState(){return this._networkState}constructor(e,t){if(super(),this.port=e,this.logger=t,this._error=0,this._rpcLock=Promise.resolve(),null===e.readable)throw new Error("Port is not readable");if(null===e.writable)throw new Error("Port is not writable")}async initialize(e=1e3){if(this.logger.log("Initializing Improv Serial"),this._processInput(),void 0===this._reader)throw new Hi;let t;try{await new Promise(async(o,i)=>{setTimeout(()=>i(new Error("Improv Wi-Fi Serial not detected")),e),t=setInterval(()=>this._sendRPC(2,[]),1e3),await this.requestCurrentState(),o(void 0)}),clearInterval(t),await this.requestInfo()}catch(e){throw await this.close(),e}finally{clearInterval(t)}return this.info}async close(){this.stopNetworkStatePolling(),this._reader&&await new Promise(e=>{this._reader.cancel(),this.addEventListener("disconnect",e,{once:!0})})}requestCurrentState(e){return this._requestCurrentState(e,!1)}async _requestCurrentState(e,t){var o;const i=new AbortController;let r;try{await new Promise((o,n)=>{r=this._enqueueRPC(()=>new Promise((e,r)=>{this.addEventListener("state-changed",()=>o(),{signal:i.signal}),this._rpcFeedback={command:2,resolve:e,reject:r,quiet:t},this._sendRPC(2,[])}),null!=e?e:Ki),r.catch(n)})}catch(e){throw new Error(`Error fetching current state: ${e}`)}finally{i.abort()}this.state===Mi.PROVISIONED?this.nextUrl=(await r)[0]:null===(o=this._rpcFeedback)||void 0===o||o.resolve([])}async requestInfo(e){const t=await this._sendRPCWithResponse(3,[],e);this.info={firmware:t[0],version:t[1],name:t[3],chipFamily:t[2],osName:t.length>4?t[4]:null,osVersion:t.length>5?t[5]:null}}async provision(e,t,o){const i=new TextEncoder,r=i.encode(e),n=i.encode(t),s=[r.length,...r,n.length,...n],a=await this._sendRPCWithResponse(1,s,o);this.nextUrl=a[0]}async scan(e){const t=(await this._sendRPCWithMultipleResponses(4,[],e)).map(([e,t,o])=>({name:e,rssi:parseInt(t),secured:"NO"!==o}));return ji(t)}subscribeSSIDs(e){let t,o,i=!0;const r=(async()=>{for(;i;){let r;try{r=await this.scan(3e4)}catch(o){this.logger.error("Error while scanning for Wi-Fi networks",o),i&&void 0===t&&e(null);break}if(!i)break;const n=void 0===t?r:Zi(t,r);(void 0===t||Gi(t,n))&&(t=n,e(n)),await new Promise(e=>{o=e,setTimeout(e,3e3)})}})();return()=>(i=!1,null==o||o(),r)}async getHostname(e){return(await this._sendRPCWithResponse(5,[],e))[0]}async setHostname(e,t){const o=new TextEncoder;return(await this._sendRPCWithResponse(5,[...o.encode(e)],t))[0]}async getDeviceName(e){return(await this._sendRPCWithResponse(6,[],e))[0]}async setDeviceName(e,t){const o=new TextEncoder,i=await this._sendRPCWithResponse(6,[...o.encode(e)],t);return this.info&&(this.info.name=i[0]),i[0]}requestNetworkState(e){return this._requestNetworkState(e,!1)}startNetworkStatePolling(e=2500,t=500){if(this._stopNetworkStatePolling||null===this.networkState||!this._reader)return;let o,i=!0;this._stopNetworkStatePolling=()=>{i=!1,null==o||o()},(async()=>{for(;i;){try{if(await this._requestNetworkState(t,!0),!i)break;await this._requestCurrentState(t,!0)}catch(e){if(null===this.networkState)break;this.logger.debug(`Network state poll failed: ${e}`)}if(!i)break;await new Promise(t=>{o=t,setTimeout(t,e)})}i&&(this._stopNetworkStatePolling=void 0)})()}stopNetworkStatePolling(){var e;null===(e=this._stopNetworkStatePolling)||void 0===e||e.call(this),this._stopNetworkStatePolling=void 0}async _requestNetworkState(e,t){let o;try{o=await this._sendRPCWithResponse(7,[],e,t)}catch(e){throw e===Vi[2]&&this._setNetworkState(null),e}const i=parseInt(o[0]),r={online:!!(1&i),supportsWifi:!!(2&i),supportsEthernet:!!(4&i),supportsThread:!!(8&i),supportsModem:!!(16&i),urls:o.slice(1)};return this._setNetworkState(r),r}_setNetworkState(e){const t=this._networkState;this._networkState=e,t===e||null!=t&&null!=e&&((e,t)=>e.online===t.online&&e.supportsWifi===t.supportsWifi&&e.supportsEthernet===t.supportsEthernet&&e.supportsThread===t.supportsThread&&e.supportsModem===t.supportsModem&&e.urls.length===t.urls.length&&e.urls.every((e,o)=>e===t.urls[o]))(t,e)||this.dispatchEvent(new CustomEvent("network-state-changed",{detail:e}))}_sendRPC(e,t){this.writePacketToStream(Bi.RPC,[e,t.length,...t])}_enqueueRPC(e,t){const o=()=>this._awaitRPCResultWithTimeout(e(),t).finally(()=>{this._rpcFeedback=void 0}),i=this._rpcLock.then(o,o);return this._rpcLock=i.catch(()=>{}),i}_sendRPCWithResponse(e,t,o=3e4,i=!1){return this._enqueueRPC(()=>new Promise((o,r)=>{this._rpcFeedback={command:e,resolve:o,reject:r,quiet:i},this._sendRPC(e,t)}),o)}_sendRPCWithMultipleResponses(e,t,o=3e4){return this._enqueueRPC(()=>new Promise((o,i)=>{this._rpcFeedback={command:e,resolve:o,reject:i,receivedData:[]},this._sendRPC(e,t)}),o)}async _awaitRPCResultWithTimeout(e,t){if(!t)return await e;const o=setTimeout(()=>this._setError(254),t);try{return await e}finally{clearTimeout(o)}}async _processInput(){this.logger.debug("Starting read loop"),this._reader=this.port.readable.getReader();try{let e,t=[],o=0;for(;;){const{value:i,done:r}=await this._reader.read();if(r)break;if(i&&0!==i.length)for(const r of i){if(!1===e){10===r&&(e=void 0);continue}if(!0===e){t.push(r),t.length===o&&(this._handleIncomingPacket(t),e=void 0,t=[]);continue}if(10===r){t=[];continue}if(t.push(r),9!==t.length)continue;if(e="IMPROV"===String.fromCharCode(...t.slice(0,6)),!e){t=[];continue}o=9+t[8]+1}}}catch(e){this.logger.error("Error while reading serial port",e)}finally{this._reader.releaseLock(),this._reader=void 0}this.logger.debug("Finished read loop"),this.stopNetworkStatePolling(),this.dispatchEvent(new Event("disconnect"))}_handleIncomingPacket(e){const t=e.slice(6),o=t[0],i=t[1],r=t[2],n=t.slice(3,3+r);if(this.logger.debug("PROCESS",{version:o,packetType:i,packetLength:r,data:Wi(n)}),1!==o)return void this.logger.error("Received unsupported version",o);let s=t[3+r],a=0;for(let t=0;t<e.length-1;t++)a+=e[t];if(a&=255,a===s)if(i===Bi.CURRENT_STATE)this.state=n[0],this.state!==Mi.PROVISIONED&&(this.nextUrl=void 0),this.dispatchEvent(new CustomEvent("state-changed",{detail:this.state}));else if(i===Bi.ERROR_STATE)this._setError(n[0]);else if(i===Bi.RPC_RESULT){if(!this._rpcFeedback)return void this.logger.error("Received result while not waiting for one");const e=n[0];if(e!==this._rpcFeedback.command)return void this.logger.error(`Received result for command ${e} but expected ${this._rpcFeedback.command}`);const t=[],o=n[1],i=new TextDecoder("utf-8");let r=2;for(;r<2+o;)t.push(i.decode(new Uint8Array(n.slice(r+1,r+n[r]+1)))),r+=n[r]+1;"receivedData"in this._rpcFeedback?t.length>0?this._rpcFeedback.receivedData.push(t):this._rpcFeedback.resolve(this._rpcFeedback.receivedData):this._rpcFeedback.resolve(t)}else this.logger.error("Unable to handle packet",t);else this.logger.error(`Received invalid checksum ${s}. Expected ${a}`)}async writePacketToStream(e,t){const o=new Uint8Array([...Ui,e,t.length,...t,0,0]);o[o.length-2]=255&o.reduce((e,t)=>e+t,0),o[o.length-1]=10,this.logger.debug("Writing to stream:",Wi(new Array(...o)));const i=this.port.writable.getWriter();await i.write(o);try{i.releaseLock()}catch(e){console.error("Ignoring release lock error",e)}}_setError(e){e>0&&this._rpcFeedback&&(this._rpcFeedback.reject(Vi[e]||`UNKNOWN_ERROR (${e})`),this._rpcFeedback.quiet)||(this.error=e)}}const Xi="⚠️",Ji=j`
|
|
494
494
|
<svg viewBox="0 -960 960 960" width="24px">
|
|
495
495
|
<path
|
|
496
496
|
fill="currentColor"
|
|
497
497
|
d="M440-280h80v-240h-80v240Zm40-320q17 0 28.5-11.5T520-640q0-17-11.5-28.5T480-680q-17 0-28.5 11.5T440-640q0 17 11.5 28.5T480-600Zm0 520q-83 0-156-31.5T197-197q-54-54-85.5-127T80-480q0-83 31.5-156T197-763q54-54 127-85.5T480-880q83 0 156 31.5T763-763q54 54 85.5 127T880-480q0 83-31.5 156T763-197q-54 54-127 85.5T480-80Zm0-80q134 0 227-93t93-227q0-134-93-227t-227-93q-134 0-227 93t-93 227q0 134 93 227t227 93Zm0-320Z"
|
|
498
498
|
/>
|
|
499
499
|
</svg>
|
|
500
|
-
`,
|
|
500
|
+
`,Qi=j`
|
|
501
501
|
<svg viewBox="0 -960 960 960">
|
|
502
502
|
<path
|
|
503
503
|
fill="currentColor"
|
|
504
504
|
d="M480-120q-42 0-71-29t-29-71q0-42 29-71t71-29q42 0 71 29t29 71q0 42-29 71t-71 29ZM254-346l-84-86q59-59 138.5-93.5T480-560q92 0 171.5 35T790-430l-84 84q-44-44-102-69t-124-25q-66 0-124 25t-102 69ZM84-516 0-600q92-94 215-147t265-53q142 0 265 53t215 147l-84 84q-77-77-178.5-120.5T480-680q-116 0-217.5 43.5T84-516Z"
|
|
505
505
|
/>
|
|
506
506
|
</svg>
|
|
507
|
-
`,
|
|
507
|
+
`,er=j`
|
|
508
508
|
<svg viewBox="0 -960 960 960" width="24px">
|
|
509
509
|
<path
|
|
510
510
|
fill="currentColor"
|
|
511
511
|
d="M480-120 0-600q95-97 219.5-148.5T480-800q137 0 261 51t219 149L480-120ZM174-540q67-48 145-74t161-26q83 0 161 26t145 74l58-58q-79-60-172-91t-192-31q-99 0-192 31t-172 91l58 58Z"
|
|
512
512
|
/>
|
|
513
513
|
</svg>
|
|
514
|
-
`,
|
|
514
|
+
`,tr=j`
|
|
515
515
|
<svg viewBox="0 -960 960 960" width="24px">
|
|
516
516
|
<path
|
|
517
517
|
fill="currentColor"
|
|
518
518
|
d="M480-120 0-600q96-98 220-149t260-51q137 0 261 51t219 149L480-120ZM232-482q53-38 116-59.5T480-563q69 0 132 21.5T728-482l116-116q-78-59-170.5-90.5T480-720q-101 0-193.5 31.5T116-598l116 116Z"
|
|
519
519
|
/>
|
|
520
520
|
</svg>
|
|
521
|
-
`,
|
|
521
|
+
`,or=j`
|
|
522
522
|
<svg viewBox="0 -960 960 960" width="24px">
|
|
523
523
|
<path
|
|
524
524
|
fill="currentColor"
|
|
525
525
|
d="M480-120 0-600q96-98 220-149t260-51q137 0 261 51t219 149L480-120ZM299-415q38-28 84-43.5t97-15.5q51 0 97 15.5t84 43.5l183-183q-78-59-170.5-90.5T480-720q-101 0-193.5 31.5T116-598l183 183Z"
|
|
526
526
|
/>
|
|
527
527
|
</svg>
|
|
528
|
-
`,
|
|
528
|
+
`,ir=j`
|
|
529
529
|
<svg viewBox="0 -960 960 960" width="24px">
|
|
530
530
|
<path
|
|
531
531
|
fill="currentColor"
|
|
532
532
|
d="M480-120 0-600q96-98 220-149t260-51q137 0 261 51t219 149L480-120ZM361-353q25-18 55.5-28t63.5-10q33 0 63.5 10t55.5 28l245-245q-78-59-170.5-90.5T480-720q-101 0-193.5 31.5T116-598l245 245Z"
|
|
533
533
|
/>
|
|
534
534
|
</svg>
|
|
535
|
-
`,
|
|
535
|
+
`,rr=j`
|
|
536
536
|
<svg viewBox="0 -960 960 960" width="24px">
|
|
537
537
|
<path
|
|
538
538
|
fill="currentColor"
|
|
539
539
|
d="M240-80q-33 0-56.5-23.5T160-160v-400q0-33 23.5-56.5T240-640h40v-80q0-83 58.5-141.5T480-920q83 0 141.5 58.5T680-720v80h40q33 0 56.5 23.5T800-560v400q0 33-23.5 56.5T720-80H240Zm0-80h480v-400H240v400Zm240-120q33 0 56.5-23.5T560-360q0-33-23.5-56.5T480-440q-33 0-56.5 23.5T400-360q0 33 23.5 56.5T480-280ZM360-640h240v-80q0-50-35-85t-85-35q-50 0-85 35t-35 85v80ZM240-160v-400 400Z"
|
|
540
540
|
/>
|
|
541
541
|
</svg>
|
|
542
|
-
`,
|
|
542
|
+
`,nr=j`
|
|
543
543
|
<svg viewBox="0 -960 960 960" width="24px">
|
|
544
544
|
<path
|
|
545
545
|
fill="currentColor"
|
|
546
546
|
d="M240-160h480v-400H240v400Zm240-120q33 0 56.5-23.5T560-360q0-33-23.5-56.5T480-440q-33 0-56.5 23.5T400-360q0 33 23.5 56.5T480-280ZM240-160v-400 400Zm0 80q-33 0-56.5-23.5T160-160v-400q0-33 23.5-56.5T240-640h280v-80q0-83 58.5-141.5T720-920q83 0 141.5 58.5T920-720h-80q0-50-35-85t-85-35q-50 0-85 35t-35 85v80h120q33 0 56.5 23.5T800-560v400q0 33-23.5 56.5T720-80H240Z"
|
|
547
547
|
/>
|
|
548
548
|
</svg>
|
|
549
|
-
`,
|
|
549
|
+
`,sr=j`
|
|
550
550
|
<svg viewBox="0 -960 960 960">
|
|
551
551
|
<path
|
|
552
552
|
fill="currentColor"
|
|
553
553
|
d="M480-320q75 0 127.5-52.5T660-500q0-75-52.5-127.5T480-680q-75 0-127.5 52.5T300-500q0 75 52.5 127.5T480-320Zm0-72q-45 0-76.5-31.5T372-500q0-45 31.5-76.5T480-608q45 0 76.5 31.5T588-500q0 45-31.5 76.5T480-392Zm0 192q-146 0-266-81.5T40-500q54-137 174-218.5T480-800q146 0 266 81.5T920-500q-54 137-174 218.5T480-200Zm0-300Zm0 220q113 0 207.5-59.5T832-500q-50-101-144.5-160.5T480-720q-113 0-207.5 59.5T128-500q50 101 144.5 160.5T480-280Z"
|
|
554
554
|
/>
|
|
555
555
|
</svg>
|
|
556
|
-
`,
|
|
556
|
+
`,ar=j`
|
|
557
557
|
<svg viewBox="0 -960 960 960">
|
|
558
558
|
<path
|
|
559
559
|
fill="currentColor"
|
|
560
560
|
d="m644-428-58-58q9-47-27-88t-93-32l-58-58q17-8 34.5-12t37.5-4q75 0 127.5 52.5T660-500q0 20-4 37.5T644-428Zm128 126-58-56q38-29 67.5-63.5T832-500q-50-101-143.5-160.5T480-720q-29 0-57 4t-55 12l-62-62q41-17 84-25.5t90-8.5q151 0 269 83.5T920-500q-23 59-60.5 109.5T772-302Zm20 246L624-222q-35 11-70.5 16.5T480-200q-151 0-269-83.5T40-500q21-53 53-98.5t73-81.5L56-792l56-56 736 736-56 56ZM222-624q-29 26-53 57t-41 67q50 101 143.5 160.5T480-280q20 0 39-2.5t39-5.5l-36-38q-11 3-21 4.5t-21 1.5q-75 0-127.5-52.5T300-500q0-11 1.5-21t4.5-21l-84-82Zm319 93Zm-151 75Z"
|
|
561
561
|
/>
|
|
562
562
|
</svg>
|
|
563
|
-
`;function
|
|
564
|
-
>${
|
|
563
|
+
`;function lr(e){return e.length?e.reduce((e,t)=>t.rssi>e.rssi?t:e).name:null}let dr=class extends he{constructor(){super(...arguments),this.logger=console,this._state="CONNECTING",this._busy=!1,this._selectedSsid=null,this._showPassword=!1,this._reprovision=!1}render(){var e,t,o,i;if(!this.port)return W``;let r,n,s=W`${null!==(i=null===(t=null===(e=this._client)||void 0===e?void 0:e.info)||void 0===t?void 0:t.name)&&void 0!==i?i:K}`;return"CONNECTING"!==this._state&&this._client?"ERROR"===this._state?(r=this._renderMessage(Xi,`An error occurred. ${this._error}`),n=this._renderCloseAction()):this._client.state===Mi.STOPPED?(r=this._renderMessage(Xi,"The connected device has Wi-Fi turned off, so it can't be configured right now. Enable the device's Wi-Fi, then try again."),n=this._renderCloseAction()):this._showConfigureForm?this._busy?r=this._renderProgress("Provisioning"):(s=W`<md-filled-tonal-icon-button
|
|
564
|
+
>${Qi}</md-filled-tonal-icon-button
|
|
565
565
|
>Configure Wi-Fi`,void 0===this._ssids?r=this._renderProgress("Searching for networks"):(r=this._renderImprovReady(),n=W`${this._renderCloseAction()}
|
|
566
566
|
<md-filled-button @click=${this._provision}
|
|
567
567
|
>Connect</md-filled-button
|
|
@@ -579,7 +579,7 @@ import{f as e}from"./serial-launch-button.js";function t(e,t,o,i){var r,n=argume
|
|
|
579
579
|
href=${this._client.nextUrl}
|
|
580
580
|
form="improv-form"
|
|
581
581
|
>Visit Device</md-filled-button
|
|
582
|
-
>`}`):(r=this._renderMessage(
|
|
582
|
+
>`}`):(r=this._renderMessage(Xi,`Unexpected state: ${this._state} - ${this._client.state}`),n=this._renderCloseAction()):r=this._renderProgress("Connecting"),W`
|
|
583
583
|
<md-dialog open @close=${this._handleClose}>
|
|
584
584
|
<div slot="headline">${s}</div>
|
|
585
585
|
<form slot="content" id="improv-form" method="dialog">${r}</form>
|
|
@@ -624,14 +624,14 @@ import{f as e}from"./serial-launch-button.js";function t(e,t,o,i){var r,n=argume
|
|
|
624
624
|
<span
|
|
625
625
|
slot="start"
|
|
626
626
|
class=${o=e.rssi,o>=-50?"signal-excellent":o>=-60?"signal-good":o>=-70?"signal-fair":"signal-weak"}
|
|
627
|
-
>${function(e){return e>=-50?
|
|
627
|
+
>${function(e){return e>=-50?er:e>=-60?tr:e>=-70?or:ir}(e.rssi)}</span
|
|
628
628
|
>
|
|
629
629
|
<span slot="headline">${e.name}</span>
|
|
630
630
|
<span slot="end" class="network-details">
|
|
631
631
|
<span class="signal-strength">${e.rssi}dB</span>
|
|
632
632
|
<span
|
|
633
633
|
class="lock-icon ${e.secured?"lock-secured":"lock-unsecured"}"
|
|
634
|
-
>${e.secured?
|
|
634
|
+
>${e.secured?rr:nr}</span
|
|
635
635
|
>
|
|
636
636
|
</span>
|
|
637
637
|
</md-select-option>
|
|
@@ -662,12 +662,12 @@ import{f as e}from"./serial-launch-button.js";function t(e,t,o,i){var r,n=argume
|
|
|
662
662
|
toggle
|
|
663
663
|
.selected=${this._showPassword}
|
|
664
664
|
>
|
|
665
|
-
${this._showPassword?sr
|
|
665
|
+
${this._showPassword?ar:sr}
|
|
666
666
|
</md-icon-button>
|
|
667
667
|
</md-outlined-text-field>
|
|
668
668
|
`:K}
|
|
669
669
|
`}_renderDeviceInfo(){return W`<div class="device-info">
|
|
670
|
-
<div>${
|
|
670
|
+
<div>${Ji}Device Info</div>
|
|
671
671
|
<div>Name<span>${this._client.info.name}</span></div>
|
|
672
672
|
<div>Firmware<span>${this._client.info.firmware}</span></div>
|
|
673
673
|
<div>Version<span>${this._client.info.version}</span></div>
|
|
@@ -676,12 +676,12 @@ import{f as e}from"./serial-launch-button.js";function t(e,t,o,i){var r,n=argume
|
|
|
676
676
|
${this._client.info.osVersion?W`<div>
|
|
677
677
|
OS Version<span>${this._client.info.osVersion}</span>
|
|
678
678
|
</div>`:K}
|
|
679
|
-
</div>`}_togglePasswordVisibility(e){e.preventDefault(),e.stopPropagation(),this._showPassword=!this._showPassword}get _showConfigureForm(){var e,t;return(null===(e=this._client)||void 0===e?void 0:e.state)===Mi.READY||(null===(t=this._client)||void 0===t?void 0:t.state)===Mi.PROVISIONED&&this._reprovision}_syncScanning(){const e="IMPROV-STATE"===this._state&&!this._busy&&this._showConfigureForm;e!==!!this._unsubSSIDs&&(e?this._unsubSSIDs=this._client.subscribeSSIDs(e=>{null===e?this._selectedSsid=null:null==this._ssids?this._selectedSsid=
|
|
679
|
+
</div>`}_togglePasswordVisibility(e){e.preventDefault(),e.stopPropagation(),this._showPassword=!this._showPassword}get _showConfigureForm(){var e,t;return(null===(e=this._client)||void 0===e?void 0:e.state)===Mi.READY||(null===(t=this._client)||void 0===t?void 0:t.state)===Mi.PROVISIONED&&this._reprovision}_syncScanning(){const e="IMPROV-STATE"===this._state&&!this._busy&&this._showConfigureForm;e!==!!this._unsubSSIDs&&(e?this._unsubSSIDs=this._client.subscribeSSIDs(e=>{null===e?this._selectedSsid=null:null==this._ssids?this._selectedSsid=lr(e):null===this._selectedSsid||e.some(e=>e.name===this._selectedSsid)||(this._selectedSsid=lr(e)),this._ssids=e}):this._stopScanning())}async _stopScanning(){if(!this._unsubSSIDs)return;const e=this._unsubSSIDs;this._unsubSSIDs=void 0,await e()}async _provision(){var e;const t=null===this._selectedSsid?this._inputSSID.value:this._selectedSsid,o=(null===(e=this._inputPassword)||void 0===e?void 0:e.value)||"";this._busy=!0,await this._stopScanning();try{await this._client.provision(t,o,45e3),this._reprovision=!1}catch(e){console.log(e)}finally{this._busy=!1}}_changeWifi(){this._reprovision=!0}updated(e){let t;super.updated(e),e.has("port")&&this.port&&this._connect(),this._syncScanning(),e.has("_ssids")&&void 0===e.get("_ssids")&&null!=this._ssids?t=this._selectSSID:e.has("_selectedSsid")&&null===this._selectedSsid&&(t=this._inputSSID),t&&t.updateComplete.then(()=>t.focus())}async _connect(){let e;try{e=new Yi(this.port,this.logger)}catch(e){return this._state="ERROR",void(this._error=e.message||e||"Unknown error")}e.addEventListener("state-changed",()=>{this._state="IMPROV-STATE",this.requestUpdate()}),e.addEventListener("error-changed",()=>this.requestUpdate());try{await e.initialize(1e4)}catch(e){return this._state="ERROR",void(this._error=this.learnMoreUrl?W`
|
|
680
680
|
Unable to detect Improv service on connected device.
|
|
681
681
|
<a href=${this.learnMoreUrl} target="_blank"
|
|
682
682
|
>Learn how to resolve this</a
|
|
683
683
|
>
|
|
684
|
-
`:e.message)}e.addEventListener("disconnect",()=>{this._state="ERROR",this._error="Disconnected"}),e.nextUrl&&this.requestUpdate(),this._client=e}async _handleClose(){const t={improv:!1,provisioned:!1};this._client&&(t.improv=!0,t.provisioned=this._client.state===Mi.PROVISIONED,await this._client.close(),this._client=void 0),e(this,"closed",t),this.parentNode.removeChild(this)}disconnectedCallback(){super.disconnectedCallback(),this._stopScanning()}};
|
|
684
|
+
`:e.message)}e.addEventListener("disconnect",()=>{this._state="ERROR",this._error="Disconnected"}),e.nextUrl&&this.requestUpdate(),this._client=e}async _handleClose(){const t={improv:!1,provisioned:!1};this._client&&(t.improv=!0,t.provisioned=this._client.state===Mi.PROVISIONED,await this._client.close(),this._client=void 0),e(this,"closed",t),this.parentNode.removeChild(this)}disconnectedCallback(){super.disconnectedCallback(),this._stopScanning()}};dr.styles=a`
|
|
685
685
|
:host {
|
|
686
686
|
--md-dialog-max-width: 390px;
|
|
687
687
|
--md-dialog-container-max-block-size: none !important;
|
|
@@ -800,4 +800,4 @@ import{f as e}from"./serial-launch-button.js";function t(e,t,o,i){var r,n=argume
|
|
|
800
800
|
.signal-weak {
|
|
801
801
|
color: #ea4335;
|
|
802
802
|
}
|
|
803
|
-
`,t([fe()],
|
|
803
|
+
`,t([fe()],dr.prototype,"port",void 0),t([ge()],dr.prototype,"_state",void 0),t([ge()],dr.prototype,"_client",void 0),t([ge()],dr.prototype,"_busy",void 0),t([ge()],dr.prototype,"_error",void 0),t([ge()],dr.prototype,"_selectedSsid",void 0),t([ge()],dr.prototype,"_ssids",void 0),t([ge()],dr.prototype,"_showPassword",void 0),t([ge()],dr.prototype,"_reprovision",void 0),t([ye("md-outlined-select")],dr.prototype,"_selectSSID",void 0),t([ye("md-outlined-text-field[name=ssid]")],dr.prototype,"_inputSSID",void 0),t([ye("md-outlined-text-field[name=password]")],dr.prototype,"_inputPassword",void 0),dr=t([ue("improv-wifi-serial-provision-dialog")],dr);
|