1
0
Fork 0
mirror of https://github.com/betaflight/betaflight-configurator.git synced 2025-07-18 13:55:14 +03:00

Fix elrs connection (#3095)

fix elrs connection

arrange mdns to class
This commit is contained in:
ASDosjani 2022-12-02 04:59:48 +01:00 committed by GitHub
parent 09355c25d0
commit 88f5c7d863
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 150 additions and 98 deletions

View file

@ -1,9 +1,6 @@
'use strict';
const TIMEOUT_CHECK = 500 ; // With 250 it seems that it produces a memory leak and slowdown in some versions, reason unknown
const MDNS_INTERVAL = 10000;
const TCP_CHECK_INTERVAL = 5000;
const TCP_TIMEOUT = 2000;
const usbDevices = { filters: [
{'vendorId': 1155, 'productId': 57105}, // STM Device in DFU Mode || Digital Radio in USB mode
@ -21,102 +18,16 @@ const PortHandler = new function () {
};
PortHandler.initialize = function () {
const self = this;
const portPickerElementSelector = "div#port-picker #port";
this.portPickerElement = $(portPickerElementSelector);
this.selectList = document.querySelector(portPickerElementSelector);
this.initialWidth = this.selectList.offsetWidth + 12;
self.portPickerElement = $(portPickerElementSelector);
self.selectList = document.querySelector(portPickerElementSelector);
self.initialWidth = self.selectList.offsetWidth + 12;
// fill dropdown with version numbers
generateVirtualApiVersions();
const self = this;
self.mdnsBrowser = {
services: [],
browser: null,
init: false,
};
let bonjour = {};
if (!self.mdnsBrowser.init) {
if (GUI.isCordova()) {
const zeroconf = cordova.plugins.zeroconf;
zeroconf.registerAddressFamily = 'ipv4'; // or 'ipv6' ('any' by default)
zeroconf.watchAddressFamily = 'ipv4'; // or 'ipv6' ('any' by default)
zeroconf.watch("_http._tcp.", "local.", (result) => {
const action = result.action;
const service = result.service;
if (['added', 'resolved'].includes(action)) {
console.log("Zeroconf Service Changed", service);
self.mdnsBrowser.services.push({
addresses: service.ipv4Addresses,
txt: service.txtRecord,
fqdn: service.hostname,
});
} else {
console.log("Zeroconf Service Removed", service);
self.mdnsBrowser.services = mdnsBrowser.services.filter(s => s.fqdn !== service.hostname);
}
});
} else {
bonjour = require('bonjour')();
self.mdnsBrowser.browser = bonjour.find({ type: 'http' }, function(service) {
console.log("Found HTTP service", service);
self.mdnsBrowser.services.push({
addresses: service.addresses,
txt: service.txt,
fqdn: service.host,
});
});
}
self.mdnsBrowser.init = true;
}
const tcpCheck = function() {
if (!self.tcpCheckLock) {
self.tcpCheckLock = true;
if (self.initialPorts?.length > 0) {
const tcpPorts = self.initialPorts.filter(p => p.path.startsWith('tcp://'));
tcpPorts.forEach(function (port) {
const removePort = () => {
self.mdnsBrowser.services = self.mdnsBrowser.services.filter(s => s.fqdn !== port.fqdn);
};
$.get({
host: port.path.split('//').pop(),
port: 80,
timeout: TCP_TIMEOUT,
}, (res) => res.destroy())
.fail(removePort);
});
//timeout is 2000ms for every found port, so wait that time before checking again
setTimeout(() => {
self.tcpCheckLock = false;
}, Math.min(tcpPorts.length, 1) * (TCP_TIMEOUT + 1));
} else {
self.tcpCheckLock = false;
}
}
setTimeout(() => {
tcpCheck();
}, TCP_CHECK_INTERVAL);
};
tcpCheck();
if (self.mdns_timer) {
clearInterval(self.mdns_timer);
}
self.mdns_timer = setInterval(() => {
if (!GUI.connected_to && !GUI.isCordova() && self.mdnsBrowser.browser) {
self.mdnsBrowser.browser.update();
}
}, MDNS_INTERVAL);
this.reinitialize(); // just to prevent code redundancy
};
@ -127,6 +38,7 @@ PortHandler.reinitialize = function () {
}
this.showVirtualMode = ConfigStorage.get('showVirtualMode').showVirtualMode;
this.showAllSerialDevices = ConfigStorage.get('showAllSerialDevices').showAllSerialDevices;
this.check(); // start listening, check after TIMEOUT_CHECK ms
};
@ -141,7 +53,7 @@ PortHandler.check = function () {
self.check_serial_devices();
}
self.usbCheckLoop = setTimeout(function () {
self.usbCheckLoop = setTimeout(() => {
self.check();
}, TIMEOUT_CHECK);
};
@ -153,7 +65,7 @@ PortHandler.check_serial_devices = function () {
let currentPorts = [
...cp,
...(self.mdnsBrowser?.services?.filter(s => s.txt.vendor === 'elrs' && s.txt.type === 'rx')
...(MdnsDiscovery.mdnsBrowser.services?.filter(s => s.txt.vendor === 'elrs' && s.txt.type === 'rx' && s.ready === true)
.map(s => s.addresses.map(a => ({
path: `tcp://${a}`,
displayName: `${s.txt.target} - ${s.txt.version}`,
@ -239,7 +151,8 @@ PortHandler.removePort = function(currentPorts) {
if (GUI.connected_to) {
for (let i = 0; i < removePorts.length; i++) {
if (removePorts[i].path === GUI.connected_to) {
$('div#header_btns a.connect').click();
$('div.connect_controls a.connect').click();
$('div.connect_controls a.connect.active').click();
}
}
}
@ -293,7 +206,7 @@ PortHandler.detectPort = function(currentPorts) {
if (GUI.auto_connect && !GUI.connecting_to && !GUI.connected_to) {
// start connect procedure. We need firmware flasher protection over here
if (GUI.active_tab !== 'firmware_flasher') {
$('div#header_btns a.connect').click();
$('div.connect_controls a.connect').click();
}
}
// trigger callbacks