From a35e63079bd1d9d01847d0a59c9617ce3a2429a1 Mon Sep 17 00:00:00 2001 From: Miguel Angel Mulero Martinez Date: Mon, 13 May 2024 16:59:20 +0200 Subject: [PATCH 1/9] Make port_handler work with PWA --- locales/en/messages.json | 4 +++ src/js/port_handler.js | 74 ++++++++++++++++++++++++++++++++-------- src/js/serial_backend.js | 16 ++++----- src/js/webSerial.js | 44 ++++++++++++++++++++++-- 4 files changed, 113 insertions(+), 25 deletions(-) diff --git a/locales/en/messages.json b/locales/en/messages.json index a956439b53..d7aa9d6597 100755 --- a/locales/en/messages.json +++ b/locales/en/messages.json @@ -62,6 +62,10 @@ "message": "Virtual Mode (Experimental)", "description": "Configure a Virtual Flight Controller without the need of a physical FC." }, + "portsSelectPermission": { + "message": "--- I can't find my device ---", + "description": "Option in the port selection dropdown to allow the user to give permissions to the system to access the device." + }, "virtualMSPVersion": { "message": "Virtual Firmware Version" }, diff --git a/src/js/port_handler.js b/src/js/port_handler.js index 7a4fbe8497..736db850b5 100644 --- a/src/js/port_handler.js +++ b/src/js/port_handler.js @@ -3,10 +3,12 @@ import FC from "./fc"; import { i18n } from "./localization"; import { generateVirtualApiVersions, getTextWidth } from './utils/common'; import { get as getConfig } from "./ConfigStorage"; -import serial from "./serial"; import MdnsDiscovery from "./mdns_discovery"; import { isWeb } from "./utils/isWeb"; import { usbDevices } from "./usb_devices"; +import { serialShim } from "./serial_shim.js"; + +const serial = serialShim(); const TIMEOUT_CHECK = 500 ; // With 250 it seems that it produces a memory leak and slowdown in some versions, reason unknown @@ -26,13 +28,7 @@ const PortHandler = new function () { PortHandler.initialize = function () { const self = this; - // currently web build doesn't need port handler, - // so just bail out. - if (isWeb()) { - return 'not implemented'; - } - - const portPickerElementSelector = "div#port-picker #port"; + const portPickerElementSelector = "div#port-picker #port, div.web-port-picker #port"; self.portPickerElement = $(portPickerElementSelector); self.selectList = document.querySelector(portPickerElementSelector); self.initialWidth = self.selectList.offsetWidth + 12; @@ -73,15 +69,29 @@ PortHandler.check = function () { self.check_serial_devices(); } - self.usbCheckLoop = setTimeout(() => { - self.check(); - }, TIMEOUT_CHECK); + if (isWeb()) { + serial.addEventListener("addedDevice", (e) => { + this.check_serial_devices(); + }); + + serial.addEventListener("removedDevice", (e) => { + this.check_serial_devices(); + }); + + this.check_serial_devices(); + + } else { + self.usbCheckLoop = setTimeout(() => { + self.check(); + }, TIMEOUT_CHECK); + } + }; PortHandler.check_serial_devices = function () { const self = this; - serial.getDevices(function(cp) { + const updatePorts = function(cp) { self.currentPorts = []; if (self.useMdnsBrowser) { @@ -110,10 +120,21 @@ PortHandler.check_serial_devices = function () { self.removePort(); self.detectPort(); } - }); + }; + + if (isWeb()) { + serial.getDevices().then(updatePorts); + } else { + serial.getDevices(updatePorts); + } }; PortHandler.check_usb_devices = function (callback) { + + if (isWeb()) { + return; + } + const self = this; chrome.usb.getDevices(usbDevices, function (result) { @@ -315,10 +336,35 @@ PortHandler.updatePortSelect = function (ports) { this.addNoPortSelection(); } + if (isWeb()) { + const askpermission = 'askpermission'; + this.portPickerElement.append($("