1
0
Fork 0
mirror of https://gitlab.com/futo-org/fcast.git synced 2025-07-15 10:28:46 +00:00

Fixing hostname detection on Raspberry PI

This commit is contained in:
Michael Hollister 2024-11-11 10:45:15 -06:00
parent 849988634d
commit fcf13b1fe5

View file

@ -1,9 +1,11 @@
import mdns = require('mdns-js'); import mdns from 'mdns-js';
const cp = require('child_process'); const cp = require('child_process');
const os = require('os'); const os = require('os');
export class DiscoveryService { export class DiscoveryService {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
private serviceTcp: any; private serviceTcp: any;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
private serviceWs: any; private serviceWs: any;
private static getComputerName() { private static getComputerName() {
@ -12,8 +14,31 @@ export class DiscoveryService {
return process.env.COMPUTERNAME; return process.env.COMPUTERNAME;
case "darwin": case "darwin":
return cp.execSync("scutil --get ComputerName").toString().trim(); return cp.execSync("scutil --get ComputerName").toString().trim();
case "linux": case "linux": {
return os.hostname(); let hostname: string;
// Some distro's don't work with `os.hostname()`, but work with `hostnamectl` and vice versa...
try {
hostname = os.hostname();
}
catch (err) {
console.warn('Error fetching hostname, trying different method...');
console.warn(err);
try {
hostname = cp.execSync("hostnamectl hostname").toString().trim();
}
catch (err2) {
console.warn('Error fetching hostname again, using generic name...');
console.warn(err2);
hostname = 'linux device';
}
}
return hostname;
}
default: default:
return os.hostname(); return os.hostname();
} }