diff --git a/locales/en/messages.json b/locales/en/messages.json index a956439b537..d7aa9d6597b 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 da1cb876781..70d69378c34 100644 --- a/src/js/port_handler.js +++ b/src/js/port_handler.js @@ -3,9 +3,11 @@ 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 { 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 @@ -32,13 +34,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; @@ -79,15 +75,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) { @@ -116,10 +126,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) { @@ -321,10 +342,35 @@ PortHandler.updatePortSelect = function (ports) { this.addNoPortSelection(); } + if (isWeb()) { + const askpermission = 'askpermission'; + this.portPickerElement.append($("