|
75 | 75 |
|
76 | 76 | // We'll drop these into the Window object so we can play with them in |
77 | 77 | // the DevTools console if we want to. |
78 | | - window.WebLabel = WebLabel; |
79 | | - window.WebDevices = WebDevices; |
| 78 | + (window as any).WebLabel = WebLabel; |
| 79 | + (window as any).WebDevices = WebDevices; |
80 | 80 |
|
81 | 81 | // For this demo we're going to make use of the USB printer manager |
82 | 82 | // so it can take care of concerns like the USB connect and disconnect events. |
|
104 | 104 | // We'll wire up some basic event listeners to the printer manager. |
105 | 105 | // First, a button to prompt a user to add a printer. |
106 | 106 | const addPrinterBtn = document.getElementById('addprinter')!; |
107 | | - addPrinterBtn.addEventListener('click', async () => printerMgr.promptForNewDevice()); |
| 107 | + addPrinterBtn.addEventListener('click', async () => { |
| 108 | + try { |
| 109 | + await printerMgr.promptForNewDevice(); |
| 110 | + } catch (e) { |
| 111 | + if (e instanceof WebDevices.DriverAccessDeniedError) { |
| 112 | + deviceErrorAlert(); |
| 113 | + } else { |
| 114 | + throw e; |
| 115 | + } |
| 116 | + } |
| 117 | + }); |
| 118 | + |
| 119 | + // And a function to call if it fails |
| 120 | + function deviceErrorAlert() { |
| 121 | + // This happens when the operating system didn't let Chrome connect. |
| 122 | + // Usually either another tab is open talking to the device, or the driver |
| 123 | + // is already loaded by another application. |
| 124 | + showAlert( |
| 125 | + 'danger', |
| 126 | + 'alert-printer-comm-error', |
| 127 | + `Operating system denied device access`, |
| 128 | + `<p>Chrome wasn't allowed to connect to a device. This usually happens because: |
| 129 | + <ul> |
| 130 | + <li>Another browser tab is already connected to that device. |
| 131 | + <li>You're on Windows and <a href="https://cellivar.github.io/WebZLP/docs/windows_driver">need to replace the driver for the device</a>. |
| 132 | + <li>Another application loaded a driver to talk to the device. |
| 133 | + </ul> |
| 134 | + Fix the issue and re-connect to the device.</p>` |
| 135 | + ); |
| 136 | + } |
108 | 137 |
|
109 | 138 | // Next a button to manually refresh all printers, just in case. |
110 | 139 | const refreshPrinterBtn = document.getElementById('refreshPrinters')!; |
111 | | - refreshPrinterBtn.addEventListener('click', async () => printerMgr.forceReconnect()); |
| 140 | + refreshPrinterBtn.addEventListener('click', async () => { |
| 141 | + try { |
| 142 | + await printerMgr.forceReconnect(); |
| 143 | + } catch (e) { |
| 144 | + if (e instanceof WebDevices.DriverAccessDeniedError) { |
| 145 | + deviceErrorAlert(); |
| 146 | + } else { |
| 147 | + throw e; |
| 148 | + } |
| 149 | + } |
| 150 | + }); |
112 | 151 |
|
113 | 152 | // Next we wire up some events on the UsbDeviceManager itself. |
114 | 153 | printerMgr.addEventListener('connectedDevice', ({ detail }) => { |
|
174 | 213 | // commands or configs specific to their printers. |
175 | 214 | // Sensors |
176 | 215 | modalZplRibbonTHold: HTMLInputElement |
177 | | - modalZplRibbonGain : HTMLInputElement |
| 216 | + modalZplRibbonGain : HTMLInputElement |
178 | 217 | modalZplWebTHold : HTMLInputElement |
179 | 218 | modalZplWebMedia : HTMLInputElement |
180 | | - modalZplTransGain : HTMLInputElement |
| 219 | + modalZplTransGain : HTMLInputElement |
181 | 220 | modalZplMarkTHold : HTMLInputElement |
182 | 221 | modalZplMarkMedia : HTMLInputElement |
183 | | - modalZplMarkGain : HTMLInputElement |
| 222 | + modalZplMarkGain : HTMLInputElement |
184 | 223 |
|
185 | 224 | modalZplWithSensorGraph: HTMLInputElement |
186 | 225 |
|
@@ -748,33 +787,17 @@ <h4>${titleHtml}</h4> |
748 | 787 | // and let it take over the UI. |
749 | 788 | await app.init(); |
750 | 789 |
|
751 | | - // Make the TypeScript type system happy by adding a property to the Window object. |
752 | | - declare global { |
753 | | - interface Window { label_app: BasicLabelDesignerApp } |
754 | | - } |
755 | 790 | // Now we can access our printer in the dev console if we want to mess with it! |
756 | | - window.label_app = app; |
| 791 | + (window as any).label_app = app; |
757 | 792 |
|
758 | 793 | // Now we'll fire the reconnect since our UI is wired up. |
759 | 794 | try { |
760 | 795 | await printerMgr.forceReconnect(); |
761 | 796 | } catch (e) { |
762 | 797 | if (e instanceof WebDevices.DriverAccessDeniedError) { |
763 | | - // This happens when the operating system didn't let Chrome connect. |
764 | | - // Usually either another tab is open talking to the device, or the driver |
765 | | - // is already loaded by another application. |
766 | | - showAlert( |
767 | | - 'danger', |
768 | | - 'alert-printer-comm-error', |
769 | | - `Operating system refused device access`, |
770 | | - `<p>This usually happens for one of these reasons: |
771 | | - <ul> |
772 | | - <li>Another browser tab is already connected. |
773 | | - <li>Another application loaded a driver to talk to the device. |
774 | | - <li>You're on Windows and need to replace the driver. |
775 | | - </ul> |
776 | | - Fix the issue and re-connect to the device.</p>` |
777 | | - ); |
| 798 | + deviceErrorAlert(); |
| 799 | + } else { |
| 800 | + throw e; |
778 | 801 | } |
779 | 802 | } |
780 | 803 |
|
|
0 commit comments