Skip to content

Commit

Permalink
scanning for elrs network devices with crsf msp frame capability
Browse files Browse the repository at this point in the history
only setup port and connect if its valid and ready

change mdsn detection and made remove ports working for tcp ports

used the bonjour package and made it event based

removed unused constant

chore: code quality

fix self reference for mdns handler functions

adding port removal on not reachable

fix tcpcheck for resolved addresses

fixing mdns and polling

fixed mdns browser init (thanks to CapnBry), events not getting registered before the discovery starts

tcp polling was to fast and now doesnt check if the gui is connected to reduce ESP stress.

chore: remove commented code

added mdns requery interval

added andoird zeroconf for mdns discovery
  • Loading branch information
freasy committed Jun 27, 2022
1 parent 863dd3a commit 66521c3
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 3 deletions.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
"@fortawesome/fontawesome-free": "^5.13.0",
"@panter/vue-i18next": "^0.15.2",
"bluebird": "^3.7.2",
"bonjour": "^3.5.0",
"cordova-plugin-zeroconf": "^1.4.2",
"djv": "^2.1.4",
"dompurify": "^2.3.6",
"i18next": "^19.0.0",
Expand All @@ -66,6 +68,7 @@
"jquery-ui-npm": "^1.12.0",
"lru_map": "^0.3.3",
"marked": "^4.0.10",
"multicast-dns": "^7.2.4",
"multiple-select": "^1.5.2",
"nw-vue-devtools-prebuilt": "^0.0.10",
"object-hash": "^2.0.3",
Expand Down
120 changes: 117 additions & 3 deletions src/js/port_handler.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,40 @@
'use strict';

const http = require("http");
const bonjour = require('bonjour')();

const MDNS_INTERVAL = 10000;
const TCP_CHECK_INTERVAL = 5000;
const TCP_TIMEOUT = 2000;

let mdnsBrowser = null;
if (GUI.isCordova()) {
mdnsBrowser = {
services: [],
};

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.").subscribe(result => {
const action = result.action;
const service = result.service;
if (['added', 'resolved'].includes(action)) {
console.log("Zeroconf Service Changed", service);
mdnsBrowser.services.push({
addresses: service.ipv4Addresses,
txt: service.txtRecord,
fqdn: service.hostname,
});
} else {
console.log("Zeroconf Service Removed", service);
mdnsBrowser.services = mdnsBrowser.services.filter(s => s.fqdn !== service.hostname);
}
});
} else {
bonjour.find({ type: 'http' });
}

const TIMEOUT_CHECK = 500 ; // With 250 it seems that it produces a memory leak and slowdown in some versions, reason unknown

const usbDevices = { filters: [
Expand Down Expand Up @@ -29,6 +64,69 @@ PortHandler.initialize = function () {
// fill dropdown with version numbers
generateVirtualApiVersions();

const self = this;

mdnsBrowser?.on('down', function (service) {
console.log('Lost an HTTP server:', service);
let currentPorts = [];
if (self.initialPorts && self.initialPorts.length > 0) {
currentPorts = self.initialPorts.filter(p => !service.addresses.some(a => p.path.startsWith(`tcp://${a}`)));

if (currentPorts.length < self.initialPorts.length) {
self.removePort(currentPorts, false);
self.detectPort(currentPorts);
}
}
});

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 = () => {
mdnsBrowser?._removeService(port.fqdn);

if (GUI.isCordova()) {
mdnsBrowser.services = mdnsBrowser.services.filter(s => s.fqdn !== port.fqdn);
}
};
http.get({
host: port.path.split('//').pop(),
port: 80,
timeout: TCP_TIMEOUT,
}, (res) => res.destroy())
.on('timeout', removePort)
.on('error', 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) {
mdnsBrowser?.update();
}
}, MDNS_INTERVAL);

// start listening, check after TIMEOUT_CHECK ms
this.check();
};
Expand All @@ -44,6 +142,10 @@ PortHandler.check = function () {
self.check_serial_devices();
}

self.check_elrs_devices();

GUI.updateManualPortVisibility();

setTimeout(function () {
self.check();
}, TIMEOUT_CHECK);
Expand All @@ -52,7 +154,19 @@ PortHandler.check = function () {
PortHandler.check_serial_devices = function () {
const self = this;

serial.getDevices(function(currentPorts) {
serial.getDevices(function(cp) {

let currentPorts = [
...cp,
...(mdnsBrowser?.services?.filter(s => s.txt.vendor === 'elrs' && s.txt.type === 'rx')
.map(s => s.addresses.map(a => ({
path: `tcp://${a}`,
displayName: `${s.txt.target} - ${s.txt.version}`,
fqdn: s.fqdn,
vendorId: 0,
productId: 0,
}))).flat() ?? []),
].filter(Boolean);

// auto-select port (only during initialization)
if (!self.initialPorts) {
Expand Down Expand Up @@ -120,15 +234,15 @@ PortHandler.check_usb_devices = function (callback) {

PortHandler.removePort = function(currentPorts) {
const self = this;
const removePorts = self.array_difference(self.initialPorts, currentPorts);
let removePorts = self.array_difference(self.initialPorts, currentPorts);

if (removePorts.length) {
console.log(`PortHandler - Removed: ${JSON.stringify(removePorts)}`);
self.port_available = false;
// disconnect "UI" - routine can't fire during atmega32u4 reboot procedure !!!
if (GUI.connected_to) {
for (let i = 0; i < removePorts.length; i++) {
if (removePorts[i] === GUI.connected_to) {
if (removePorts[i].path === GUI.connected_to) {
$('div#header_btns a.connect').click();
}
}
Expand Down

0 comments on commit 66521c3

Please sign in to comment.