From f3c5a1dec63b07763d0d0556ad0e03d65c66d8e0 Mon Sep 17 00:00:00 2001 From: Mark Haslinghuis Date: Fri, 29 Dec 2023 01:31:02 +0100 Subject: [PATCH] Hide manual connection and refactor port_handler --- locales/en/messages.json | 4 + src/js/port_handler.js | 257 +++++++++++++++++++++----------- src/js/serial_backend.js | 93 ++++++++---- src/js/tabs/firmware_flasher.js | 21 ++- src/js/tabs/options.js | 12 ++ src/tabs/options.html | 6 + 6 files changed, 278 insertions(+), 115 deletions(-) diff --git a/locales/en/messages.json b/locales/en/messages.json index 476e9230f3..af923fdb30 100755 --- a/locales/en/messages.json +++ b/locales/en/messages.json @@ -113,6 +113,10 @@ "message": "Use mDNS Browser Device discovery on network (experimental)", "description": "Enable mDNS Browser Device discovery in PortHandler (experimental)" }, + "showManualMode": { + "message": "Enable manual connection mode", + "description": "Text for the option to enable or disable manual connection mode" + }, "showVirtualMode": { "message": "Enable virtual connection mode", "description": "Text for the option to enable or disable the virtual FC" diff --git a/src/js/port_handler.js b/src/js/port_handler.js index 40998156ef..29b51eefa3 100644 --- a/src/js/port_handler.js +++ b/src/js/port_handler.js @@ -5,7 +5,6 @@ import { generateVirtualApiVersions, getTextWidth } from './utils/common'; import { get as getConfig } from "./ConfigStorage"; import serial from "./serial"; import MdnsDiscovery from "./mdns_discovery"; -import $ from 'jquery'; import { isWeb } from "./utils/isWeb"; const TIMEOUT_CHECK = 500 ; // With 250 it seems that it produces a memory leak and slowdown in some versions, reason unknown @@ -18,6 +17,7 @@ export const usbDevices = { filters: [ ] }; const PortHandler = new function () { + this.currentPorts = []; this.initialPorts = false; this.port_detected_callbacks = []; this.port_removed_callbacks = []; @@ -26,6 +26,7 @@ const PortHandler = new function () { this.showAllSerialDevices = false; this.useMdnsBrowser = false; this.showVirtualMode = false; + this.showManualMode = false; }; PortHandler.initialize = function () { @@ -56,6 +57,7 @@ PortHandler.reinitialize = function () { } this.showVirtualMode = getConfig('showVirtualMode').showVirtualMode; + this.showManualMode = getConfig('showManualMode').showManualMode; this.showAllSerialDevices = getConfig('showAllSerialDevices').showAllSerialDevices; this.useMdnsBrowser = getConfig('useMdnsBrowser').useMdnsBrowser; @@ -86,10 +88,10 @@ PortHandler.check_serial_devices = function () { const self = this; serial.getDevices(function(cp) { - let currentPorts = []; + self.currentPorts = []; if (self.useMdnsBrowser) { - currentPorts = [ + self.currentPorts = [ ...cp, ...(MdnsDiscovery.mdnsBrowser.services?.filter(s => s.txt?.vendor === 'elrs' && s.txt?.type === 'rx' && s.ready === true) .map(s => s.addresses.map(a => ({ @@ -101,65 +103,82 @@ PortHandler.check_serial_devices = function () { }))).flat() ?? []), ].filter(Boolean); } else { - currentPorts = cp; + self.currentPorts = cp; } // auto-select port (only during initialization) if (!self.initialPorts) { - currentPorts = self.updatePortSelect(currentPorts); - self.selectPort(currentPorts); - self.initialPorts = currentPorts; + self.currentPorts = self.updatePortSelect(self.currentPorts); + self.selectPort(self.currentPorts); + self.initialPorts = self.currentPorts; GUI.updateManualPortVisibility(); } else { - self.removePort(currentPorts); - self.detectPort(currentPorts); + self.removePort(); + self.detectPort(); } }); }; PortHandler.check_usb_devices = function (callback) { const self = this; + const portSelect = document.querySelector('#port'); + chrome.usb.getDevices(usbDevices, function (result) { + const dfuActive = !!portSelect.value.startsWith('DFU'); - const dfuElement = self.portPickerElement.children("[value='DFU']"); if (result.length) { - if (!dfuElement.length) { - self.portPickerElement.empty(); - let usbText; - if (result[0].productName) { - usbText = (`DFU - ${result[0].productName}`); - } else { - usbText = "DFU"; - } + // Found device in DFU mode, add it to the list + if (!dfuActive) { + self.clearOptions(); + + // self.portPickerElement.append($('