Network: Scanning use a more reliable backward parsing + added logs to figure potential bug.
This commit is contained in:
parent
835f88d71e
commit
4ba0f8d958
1 changed files with 87 additions and 79 deletions
|
|
@ -92,9 +92,6 @@ Singleton {
|
||||||
|
|
||||||
function setWifiEnabled(enabled) {
|
function setWifiEnabled(enabled) {
|
||||||
Settings.data.network.wifiEnabled = enabled
|
Settings.data.network.wifiEnabled = enabled
|
||||||
|
|
||||||
wifiToggleProcess.action = enabled ? "on" : "off"
|
|
||||||
wifiToggleProcess.running = true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function scan() {
|
function scan() {
|
||||||
|
|
@ -103,7 +100,9 @@ Singleton {
|
||||||
|
|
||||||
scanning = true
|
scanning = true
|
||||||
lastError = ""
|
lastError = ""
|
||||||
scanProcess.running = true
|
|
||||||
|
// Get existing profiles first, then scan
|
||||||
|
profileCheckProcess.running = true
|
||||||
Logger.log("Network", "Wi-Fi scan in progress...")
|
Logger.log("Network", "Wi-Fi scan in progress...")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -176,8 +175,7 @@ Singleton {
|
||||||
"ssid": ssid,
|
"ssid": ssid,
|
||||||
"security": "--",
|
"security": "--",
|
||||||
"signal": 100,
|
"signal": 100,
|
||||||
"connected"// Default to good signal until real scan
|
"connected": true,
|
||||||
: true,
|
|
||||||
"existing": true,
|
"existing": true,
|
||||||
"cached": true
|
"cached": true
|
||||||
}
|
}
|
||||||
|
|
@ -241,30 +239,23 @@ Singleton {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Helper process to get existing profiles
|
||||||
Process {
|
Process {
|
||||||
id: wifiToggleProcess
|
id: profileCheckProcess
|
||||||
property string action: "on"
|
|
||||||
running: false
|
running: false
|
||||||
command: ["nmcli", "radio", "wifi", action]
|
command: ["nmcli", "-t", "-f", "NAME", "connection", "show"]
|
||||||
|
|
||||||
onRunningChanged: {
|
stdout: StdioCollector {
|
||||||
if (!running) {
|
|
||||||
if (action === "on") {
|
|
||||||
// Clear networks immediately and start delayed scan
|
|
||||||
root.networks = ({})
|
|
||||||
delayedScanTimer.interval = 8000
|
|
||||||
delayedScanTimer.restart()
|
|
||||||
} else {
|
|
||||||
root.networks = ({})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
stderr: StdioCollector {
|
|
||||||
onStreamFinished: {
|
onStreamFinished: {
|
||||||
if (text.trim()) {
|
const profiles = {}
|
||||||
Logger.warn("Network", "WiFi toggle error: " + text)
|
const lines = text.split("\n").filter(l => l.trim())
|
||||||
|
for (const line of lines) {
|
||||||
|
profiles[line.trim()] = true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Logger.log("Network", "Got profiles", JSON.stringify(profiles))
|
||||||
|
scanProcess.existingProfiles = profiles
|
||||||
|
scanProcess.running = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -272,74 +263,90 @@ Singleton {
|
||||||
Process {
|
Process {
|
||||||
id: scanProcess
|
id: scanProcess
|
||||||
running: false
|
running: false
|
||||||
command: ["sh", "-c", `
|
command: ["nmcli", "-t", "-f", "SSID,SECURITY,SIGNAL,IN-USE", "device", "wifi", "list", "--rescan", "yes"]
|
||||||
# Get list of saved connection profiles (just the names)
|
|
||||||
profiles=$(nmcli -t -f NAME connection show | tr '\n' '|')
|
|
||||||
|
|
||||||
# Get WiFi networks
|
// Store existing profiles
|
||||||
nmcli -t -f SSID,SECURITY,SIGNAL,IN-USE device wifi list --rescan yes | while read line; do
|
property var existingProfiles: ({})
|
||||||
ssid=$(echo "$line" | cut -d: -f1)
|
|
||||||
security=$(echo "$line" | cut -d: -f2)
|
|
||||||
signal=$(echo "$line" | cut -d: -f3)
|
|
||||||
in_use=$(echo "$line" | cut -d: -f4)
|
|
||||||
|
|
||||||
# Skip empty SSIDs
|
|
||||||
if [ -z "$ssid" ]; then
|
|
||||||
continue
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Check if SSID matches any profile name (simple check)
|
|
||||||
# This covers most cases where profile name equals or contains the SSID
|
|
||||||
existing=false
|
|
||||||
if echo "$profiles" | grep -qF "$ssid|"; then
|
|
||||||
existing=true
|
|
||||||
fi
|
|
||||||
|
|
||||||
echo "$ssid|$security|$signal|$in_use|$existing"
|
|
||||||
done
|
|
||||||
`]
|
|
||||||
|
|
||||||
stdout: StdioCollector {
|
stdout: StdioCollector {
|
||||||
onStreamFinished: {
|
onStreamFinished: {
|
||||||
const nets = {}
|
const lines = text.split("\n")
|
||||||
const lines = text.split("\n").filter(l => l.trim())
|
const networksMap = {}
|
||||||
|
|
||||||
for (const line of lines) {
|
for (var i = 0; i < lines.length; ++i) {
|
||||||
const parts = line.split("|")
|
const line = lines[i].trim()
|
||||||
if (parts.length < 5)
|
if (!line)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
const ssid = parts[0]
|
// Parse from the end to handle SSIDs with colons
|
||||||
if (!ssid || ssid.trim() === "")
|
// Format is SSID:SECURITY:SIGNAL:IN-USE
|
||||||
continue
|
// We know the last 3 fields, so everything else is SSID
|
||||||
|
const lastColonIdx = line.lastIndexOf(":")
|
||||||
const network = {
|
if (lastColonIdx === -1) {
|
||||||
"ssid": ssid,
|
Logger.warn("Network", "Malformed nmcli output line:", line)
|
||||||
"security": parts[1] || "--",
|
continue
|
||||||
"signal": parseInt(parts[2]) || 0,
|
|
||||||
"connected": parts[3] === "*",
|
|
||||||
"existing": parts[4] === "true",
|
|
||||||
"cached": ssid in cacheAdapter.knownNetworks
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Track connected network
|
const inUse = line.substring(lastColonIdx + 1)
|
||||||
if (network.connected && cacheAdapter.lastConnected !== ssid) {
|
const remainingLine = line.substring(0, lastColonIdx)
|
||||||
cacheAdapter.lastConnected = ssid
|
|
||||||
saveCache()
|
const secondLastColonIdx = remainingLine.lastIndexOf(":")
|
||||||
|
if (secondLastColonIdx === -1) {
|
||||||
|
Logger.warn("Network", "Malformed nmcli output line:", line)
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// Keep best signal for duplicate SSIDs
|
const signal = remainingLine.substring(secondLastColonIdx + 1)
|
||||||
if (!nets[ssid] || network.signal > nets[ssid].signal) {
|
const remainingLine2 = remainingLine.substring(0, secondLastColonIdx)
|
||||||
nets[ssid] = network
|
|
||||||
|
const thirdLastColonIdx = remainingLine2.lastIndexOf(":")
|
||||||
|
if (thirdLastColonIdx === -1) {
|
||||||
|
Logger.warn("Network", "Malformed nmcli output line:", line)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
const security = remainingLine2.substring(thirdLastColonIdx + 1)
|
||||||
|
const ssid = remainingLine2.substring(0, thirdLastColonIdx)
|
||||||
|
|
||||||
|
if (ssid) {
|
||||||
|
const signalInt = parseInt(signal) || 0
|
||||||
|
const connected = inUse === "*"
|
||||||
|
|
||||||
|
// Track connected network in cache
|
||||||
|
if (connected && cacheAdapter.lastConnected !== ssid) {
|
||||||
|
cacheAdapter.lastConnected = ssid
|
||||||
|
saveCache()
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!networksMap[ssid]) {
|
||||||
|
networksMap[ssid] = {
|
||||||
|
"ssid": ssid,
|
||||||
|
"security": security || "--",
|
||||||
|
"signal": signalInt,
|
||||||
|
"connected": connected,
|
||||||
|
"existing": ssid in scanProcess.existingProfiles,
|
||||||
|
"cached": ssid in cacheAdapter.knownNetworks
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Keep the best signal for duplicate SSIDs
|
||||||
|
const existingNet = networksMap[ssid]
|
||||||
|
if (connected) {
|
||||||
|
existingNet.connected = true
|
||||||
|
}
|
||||||
|
if (signalInt > existingNet.signal) {
|
||||||
|
existingNet.signal = signalInt
|
||||||
|
existingNet.security = security || "--"
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// For logging purpose only
|
// Logging
|
||||||
Logger.log("Network", "Wi-Fi scan completed")
|
|
||||||
const oldSSIDs = Object.keys(root.networks)
|
const oldSSIDs = Object.keys(root.networks)
|
||||||
const newSSIDs = Object.keys(nets)
|
const newSSIDs = Object.keys(networksMap)
|
||||||
const newNetworks = newSSIDs.filter(ssid => !oldSSIDs.includes(ssid))
|
const newNetworks = newSSIDs.filter(ssid => !oldSSIDs.includes(ssid))
|
||||||
const lostNetworks = oldSSIDs.filter(ssid => !newSSIDs.includes(ssid))
|
const lostNetworks = oldSSIDs.filter(ssid => !newSSIDs.includes(ssid))
|
||||||
|
|
||||||
if (newNetworks.length > 0 || lostNetworks.length > 0) {
|
if (newNetworks.length > 0 || lostNetworks.length > 0) {
|
||||||
if (newNetworks.length > 0) {
|
if (newNetworks.length > 0) {
|
||||||
Logger.log("Network", "New Wi-Fi SSID discovered:", newNetworks.join(", "))
|
Logger.log("Network", "New Wi-Fi SSID discovered:", newNetworks.join(", "))
|
||||||
|
|
@ -347,11 +354,12 @@ Singleton {
|
||||||
if (lostNetworks.length > 0) {
|
if (lostNetworks.length > 0) {
|
||||||
Logger.log("Network", "Wi-Fi SSID disappeared:", lostNetworks.join(", "))
|
Logger.log("Network", "Wi-Fi SSID disappeared:", lostNetworks.join(", "))
|
||||||
}
|
}
|
||||||
Logger.log("Network", "Total Wi-Fi SSIDs:", Object.keys(nets).length)
|
Logger.log("Network", "Total Wi-Fi SSIDs:", Object.keys(networksMap).length)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Assign the results
|
Logger.log("Network", "Wi-Fi scan completed")
|
||||||
root.networks = nets
|
Logger.log("Network", JSON.stringify(networksMap))
|
||||||
|
root.networks = networksMap
|
||||||
root.scanning = false
|
root.scanning = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue