From 494083917cad194bc059b7f34b4810090bbd2636 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 | 189 ++++++++++++++++++++------------ src/js/serial_backend.js | 123 +++++++++++---------- src/js/tabs/firmware_flasher.js | 29 +++-- src/js/tabs/options.js | 12 ++ src/tabs/options.html | 6 + 6 files changed, 225 insertions(+), 138 deletions(-) diff --git a/locales/en/messages.json b/locales/en/messages.json index 77d9fe3cd6d..e3a9d1d2a2e 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 8ed57b7a8c2..c54d8c0e8c7 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,64 +103,61 @@ 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(); + + const usbText = (result[0].productName) ? (`DFU - ${result[0].productName}`) : "DFU"; + const dfuOption = document.createElement('option'); - self.portPickerElement.append($('