Add wifi scan ability and prepared connecting

This commit is contained in:
2024-03-28 04:17:53 +01:00
parent 900b07f088
commit 2d7186c7c9
12 changed files with 429 additions and 41 deletions

114
src/OSHandler.ts Normal file
View File

@ -0,0 +1,114 @@
import {WiFiNetwork} from "./IPCConstants";
import {spawn, exec} from 'node:child_process';
const wifiScan = require("node-wifi-scanner");
export default class OSHandler {
static getKnownWifis(): Promise<WiFiNetwork[]> {
return new Promise<WiFiNetwork[]>((resolve, reject) => {
});
}
static addWifi(wifi: WiFiNetwork) {
return new Promise<WiFiNetwork>((resolve, reject) => {
});
}
static scanWifis() {
return new Promise<WiFiNetwork[]>((resolve, reject) => {
wifiScan.scan(true).then((result: {
ssid: string,
mac: string,
channel: number,
rssi: number,
encrypted: boolean
}[]) => {
if (!result)
return {status: false};
let networks: WiFiNetwork[] = [];
for (let wifi of result) {
networks.push({
ssid: wifi.ssid,
isKnown: true,
isSecured: wifi.encrypted,
rssi: wifi.rssi
})
}
// Sort best rssi to top
networks.sort((a, b) => {
return b.rssi - a.rssi;
});
// Sort best rssi to top
networks.sort((a, b) => {
if (b.isSecured && !a.isSecured)
return 1;
else if (b.isSecured == a.isSecured)
return 0;
else
return -1;
});
console.log(networks);
networks = networks.filter((ele, index) => {
// If empty ssid
if (!ele.ssid || ele.ssid == "")
return false;
// Remove duplicates
let i = 0;
for (let x of networks) {
if (x.ssid == ele.ssid) {
return i == index;
}
i++;
}
return true;
});
resolve(networks);
}).catch(reject);
})
}
static disableAllWifis() {
return new Promise<void>((resolve, reject) => {
});
}
static checkForSudo() {
return new Promise<boolean>((resolve) => {
const test = spawn("sudo", ["-v"], {detached: true, stdio: ["ignore", "pipe", "pipe"]});
test.on("close", (code: number) => {
if (code != 0)
resolve(false);
else
resolve(true);
});
test.on("error", (err) => {
resolve(false);
})
})
}
static checkForInternet() {
return new Promise<boolean>((resolve) => {
exec("ping -q -w 1 -c 1 `ip r | grep default | cut -d ' ' -f 3` > /dev/null && echo ok || echo error", (error, stdout, stderr) => {
if (error)
resolve(false);
else
resolve(stdout == "1");
});
});
}
}