internal: format network

Also use exec, onExited and StdioCollector
This commit is contained in:
2 * r + 2 * t 2025-08-04 23:15:55 +10:00
parent 5405400bd1
commit 1f4ec53f8f

View file

@ -10,42 +10,37 @@ Singleton {
readonly property list<AccessPoint> networks: [] readonly property list<AccessPoint> networks: []
readonly property AccessPoint active: networks.find(n => n.active) ?? null readonly property AccessPoint active: networks.find(n => n.active) ?? null
property bool wifiEnabled: true property bool wifiEnabled: true
property bool scanning: false readonly property bool scanning: rescanProc.running
reloadableId: "network" reloadableId: "network"
function enableWifi(enabled: bool): void { function enableWifi(enabled: bool): void {
const cmd = enabled ? "on" : "off"; const cmd = enabled ? "on" : "off";
enableWifiProcess.command = ["nmcli", "radio", "wifi", cmd]; enableWifiProc.exec(["nmcli", "radio", "wifi", cmd]);
enableWifiProcess.running = true;
} }
function toggleWifi(): void { function toggleWifi(): void {
const cmd = wifiEnabled ? "off" : "on"; const cmd = wifiEnabled ? "off" : "on";
enableWifiProcess.command = ["nmcli", "radio", "wifi", cmd]; enableWifiProc.exec(["nmcli", "radio", "wifi", cmd]);
enableWifiProcess.running = true;
} }
function rescanWifi(): void { function rescanWifi(): void {
scanning = true; rescanProc.running = true;
rescanProcess.running = true;
} }
function connectToNetwork(ssid: string, password: string): void { function connectToNetwork(ssid: string, password: string): void {
// TODO: Implement password // TODO: Implement password
connectProcess.command = ["nmcli", "conn", "up", ssid]; connectProc.exec(["nmcli", "conn", "up", ssid]);
connectProcess.running = true;
} }
function disconnectFromNetwork(): void { function disconnectFromNetwork(): void {
if (active) { if (active) {
disconnectProcess.command = ["nmcli", "connection", "down", active.ssid]; disconnectProc.exec(["nmcli", "connection", "down", active.ssid]);
disconnectProcess.running = true;
} }
} }
function getWifiStatus(): void { function getWifiStatus(): void {
wifiStatusProcess.running = true; wifiStatusProc.running = true;
} }
Process { Process {
@ -57,53 +52,53 @@ Singleton {
} }
Process { Process {
id: wifiStatusProcess id: wifiStatusProc
running: true
command: ["nmcli", "radio", "wifi"] command: ["nmcli", "radio", "wifi"]
environment: ({ environment: ({
LANG: "C", LANG: "C",
LC_ALL: "C" LC_ALL: "C"
}) })
stdout: StdioCollector { stdout: StdioCollector {
onStreamFinished: { onStreamFinished: {
root.wifiEnabled = text.trim() === "enabled"; root.wifiEnabled = text.trim() === "enabled";
} }
} }
Component.onCompleted: running = true
} }
Process { Process {
id: enableWifiProcess id: enableWifiProc
stdout: SplitParser {
onRead: { onExited: {
getWifiStatus(); root.getWifiStatus();
getNetworks.running = true; getNetworks.running = true;
}
} }
} }
Process { Process {
id: rescanProcess id: rescanProc
command: ["nmcli", "dev", "wifi", "list", "--rescan", "yes"] command: ["nmcli", "dev", "wifi", "list", "--rescan", "yes"]
stdout: SplitParser { onExited: {
onRead: { getNetworks.running = true;
scanning = false;
getNetworks.running = true;
}
} }
} }
Process { Process {
id: connectProcess id: connectProc
stdout: SplitParser { stdout: SplitParser {
onRead: getNetworks.running = true onRead: getNetworks.running = true
} }
stderr: SplitParser { stderr: StdioCollector {
onRead: console.warn("Network connection error:", data) onStreamFinished: console.warn("Network connection error:", text)
} }
} }
Process { Process {
id: disconnectProcess id: disconnectProc
stdout: SplitParser { stdout: SplitParser {
onRead: getNetworks.running = true onRead: getNetworks.running = true
} }
@ -111,6 +106,7 @@ Singleton {
Process { Process {
id: getNetworks id: getNetworks
running: true running: true
command: ["nmcli", "-g", "ACTIVE,SIGNAL,FREQ,SSID,BSSID,SECURITY", "d", "w"] command: ["nmcli", "-g", "ACTIVE,SIGNAL,FREQ,SSID,BSSID,SECURITY", "d", "w"]
environment: ({ environment: ({
@ -134,7 +130,7 @@ Singleton {
security: net[5] || "" security: net[5] || ""
}; };
}).filter(n => n.ssid && n.ssid.length > 0); }).filter(n => n.ssid && n.ssid.length > 0);
// Group networks by SSID and prioritize connected ones // Group networks by SSID and prioritize connected ones
const networkMap = new Map(); const networkMap = new Map();
for (const network of allNetworks) { for (const network of allNetworks) {
@ -154,9 +150,9 @@ Singleton {
// If existing is active and new is not, keep existing // If existing is active and new is not, keep existing
} }
} }
const networks = Array.from(networkMap.values()); const networks = Array.from(networkMap.values());
const rNetworks = root.networks; const rNetworks = root.networks;
const destroyed = rNetworks.filter(rn => !networks.find(n => n.frequency === rn.frequency && n.ssid === rn.ssid && n.bssid === rn.bssid)); const destroyed = rNetworks.filter(rn => !networks.find(n => n.frequency === rn.frequency && n.ssid === rn.ssid && n.bssid === rn.bssid));