Browse Source

Allow specifying checkFirmware override (#216)

Paulus Schoutsen 3 years ago
parent
commit
95b8efa500
4 changed files with 40 additions and 3 deletions
  1. 22 0
      index.html
  2. 1 0
      src/connect.ts
  3. 4 1
      src/install-button.ts
  4. 13 2
      src/install-dialog.ts

+ 22 - 0
index.html

@@ -403,6 +403,28 @@
         is visible in the ESP Web Tools menu when connected to a device running
         your firmware (as detected via Improv).
       </p>
+      <p>
+        ESP Web Tools allows you to provide your own check if the device is
+        running the same firmware as specified in the manifest. This check can
+        be setting the <code>overrides</code> property on
+        <code>&lt;esp-web-install-button&gt;</code>. The value is an object
+        containing a
+        <code>checkSameFirmwareVersion(manifest, improvInfo)</code> function.
+        The <code>manifest</code> parameter is your manifest and
+        <code>improvInfo</code> is the information returned from Improv:
+        <code>{ name, firmware, version, chipFamily }</code>. This check is only
+        called if the device firmware was detected via Improv.
+      </p>
+      <pre>
+const button = document.querySelector('esp-web-install-button');
+button.overrides = {
+  checkSameFirmwareVersion(manifest, improvInfo) {
+    const manifestFirmware = manifest.name.toLowerCase();
+    const deviceFirmware = improvInfo.firmware.toLowerCase();
+    return manifestFirmware.includes(deviceFirmware);
+  }
+};</pre
+      >
 
       <h3 id="customize">Customizing the look and feel</h3>
       <p>

+ 1 - 0
src/connect.ts

@@ -30,6 +30,7 @@ export const connect = async (button: InstallButton) => {
   const el = document.createElement("ewt-install-dialog");
   el.port = port;
   el.manifestPath = button.manifest || button.getAttribute("manifest")!;
+  el.overrides = button.overrides;
   el.addEventListener(
     "closed",
     () => {

+ 4 - 1
src/install-button.ts

@@ -1,4 +1,5 @@
-import { FlashState } from "./const";
+import type { FlashState } from "./const";
+import type { EwtInstallDialog } from "./install-dialog";
 
 export class InstallButton extends HTMLElement {
   public static isSupported = "serial" in navigator;
@@ -71,6 +72,8 @@ export class InstallButton extends HTMLElement {
 
   public renderRoot?: ShadowRoot;
 
+  public overrides: EwtInstallDialog["overrides"];
+
   public static preload() {
     import("./connect");
   }

+ 13 - 2
src/install-dialog.ts

@@ -30,13 +30,20 @@ import { dialogStyles } from "./styles";
 const ERROR_ICON = "⚠️";
 const OK_ICON = "🎉";
 
-class EwtInstallDialog extends LitElement {
+export class EwtInstallDialog extends LitElement {
   public port!: SerialPort;
 
   public manifestPath!: string;
 
   public logger: Logger = console;
 
+  public overrides?: {
+    checkSameFirmwareVersion?: (
+      manifest: Manifest,
+      deviceImprov: ImprovSerial["info"]
+    ) => boolean;
+  };
+
   private _manifest!: Manifest;
 
   private _info?: ImprovSerial["info"];
@@ -877,7 +884,11 @@ class EwtInstallDialog extends LitElement {
    * Return if the device runs same firmware as manifest.
    */
   private get _isSameFirmware() {
-    return this._info?.firmware === this._manifest!.name;
+    return !this._info
+      ? false
+      : this.overrides?.checkSameFirmwareVersion
+      ? this.overrides.checkSameFirmwareVersion(this._manifest, this._info)
+      : this._info.firmware === this._manifest.name;
   }
 
   /**