update to network updates
This commit is contained in:
@@ -34,6 +34,7 @@ QtObject {
|
|||||||
property real _prevTxBytes: 0
|
property real _prevTxBytes: 0
|
||||||
property var _prevUpdateTime: 0
|
property var _prevUpdateTime: 0
|
||||||
property string _trafficActiveDevice: ""
|
property string _trafficActiveDevice: ""
|
||||||
|
property string _lastNetworkSignature: ""
|
||||||
|
|
||||||
readonly property var networking: Networking
|
readonly property var networking: Networking
|
||||||
|
|
||||||
@@ -53,11 +54,32 @@ QtObject {
|
|||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
property var _pendingDevices: []
|
|
||||||
|
|
||||||
function updateAll() {
|
function updateAll() {
|
||||||
|
updateNetworks(true)
|
||||||
|
updateStats()
|
||||||
|
}
|
||||||
|
|
||||||
|
function updateNetworks(force) {
|
||||||
var connected = findAllConnectedDevices()
|
var connected = findAllConnectedDevices()
|
||||||
_pendingDevices = connected
|
|
||||||
|
var sig = ""
|
||||||
|
for (var i = 0; i < connected.length; i++) {
|
||||||
|
var dev = connected[i]
|
||||||
|
sig += dev.name + ":" + dev.type + ":" + dev.connected
|
||||||
|
if (dev.type === DeviceType.Wifi) {
|
||||||
|
var nets = dev.networks.values
|
||||||
|
for (var j = 0; j < nets.length; j++) {
|
||||||
|
if (nets[j].connected) {
|
||||||
|
sig += ":" + nets[j].name
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sig += ";"
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!force && sig === _lastNetworkSignature) return
|
||||||
|
_lastNetworkSignature = sig
|
||||||
|
|
||||||
activeConnections.clear()
|
activeConnections.clear()
|
||||||
var hasWifi = false
|
var hasWifi = false
|
||||||
@@ -109,7 +131,6 @@ QtObject {
|
|||||||
|
|
||||||
if (trafficDev) {
|
if (trafficDev) {
|
||||||
_trafficActiveDevice = trafficDev.name
|
_trafficActiveDevice = trafficDev.name
|
||||||
trafficScript.running = true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
root.computeStatus(hasWired, hasWifi, bestWifiSignal)
|
root.computeStatus(hasWired, hasWifi, bestWifiSignal)
|
||||||
@@ -117,13 +138,27 @@ QtObject {
|
|||||||
if (connected.length > 0) {
|
if (connected.length > 0) {
|
||||||
ipv4Script.running = true
|
ipv4Script.running = true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
networkDetailScript.running = true
|
||||||
|
}
|
||||||
|
|
||||||
|
function updateStats() {
|
||||||
|
if (!_trafficActiveDevice) return
|
||||||
|
statsScript.running = true
|
||||||
}
|
}
|
||||||
|
|
||||||
property Timer networkTimer: Timer {
|
property Timer networkTimer: Timer {
|
||||||
interval: 10000
|
interval: 5000
|
||||||
repeat: true
|
repeat: true
|
||||||
running: true
|
running: true
|
||||||
onTriggered: root.updateAll()
|
onTriggered: root.updateNetworks()
|
||||||
|
}
|
||||||
|
|
||||||
|
property Timer statsTimer: Timer {
|
||||||
|
interval: 1000
|
||||||
|
repeat: true
|
||||||
|
running: true
|
||||||
|
onTriggered: root.updateStats()
|
||||||
}
|
}
|
||||||
|
|
||||||
property Process ipv4Script: Process {
|
property Process ipv4Script: Process {
|
||||||
@@ -161,16 +196,8 @@ QtObject {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
property Process trafficScript: Process {
|
property Process networkDetailScript: Process {
|
||||||
command: ["sh", "-c",
|
command: ["sh", "-c",
|
||||||
"echo '===TRAFFIC==='; " +
|
|
||||||
"DEV=\"" + root._trafficActiveDevice + "\"; " +
|
|
||||||
"if [ -n \"$DEV\" ]; then " +
|
|
||||||
" echo \"RX_BYTES=$(cat /sys/class/net/$DEV/statistics/rx_bytes 2>/dev/null || echo 0)\"; " +
|
|
||||||
" echo \"TX_BYTES=$(cat /sys/class/net/$DEV/statistics/tx_bytes 2>/dev/null || echo 0)\"; " +
|
|
||||||
"fi; " +
|
|
||||||
"echo '===PING==='; " +
|
|
||||||
"ping -c 1 -W 2 1.1.1.1 2>/dev/null | awk -F/ '/rtt/{print $5}' || echo ''; " +
|
|
||||||
"echo '===VPN_ALL==='; " +
|
"echo '===VPN_ALL==='; " +
|
||||||
"nmcli -t -f NAME,TYPE connection show 2>/dev/null | grep -i vpn || true; " +
|
"nmcli -t -f NAME,TYPE connection show 2>/dev/null | grep -i vpn || true; " +
|
||||||
"echo '===VPN_ACTIVE==='; " +
|
"echo '===VPN_ACTIVE==='; " +
|
||||||
@@ -182,39 +209,11 @@ QtObject {
|
|||||||
]
|
]
|
||||||
|
|
||||||
stdout: StdioCollector {
|
stdout: StdioCollector {
|
||||||
onStreamFinished: root.parseOutput(text.trim())
|
onStreamFinished: root.parseNetworkDetails(text.trim())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function computeStatus(hasWired, hasWifi, bestSignal) {
|
function parseNetworkDetails(output) {
|
||||||
if (!hasWired && !hasWifi) {
|
|
||||||
root.status = "/"
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
var parts = []
|
|
||||||
if (hasWired) parts.push("")
|
|
||||||
if (hasWifi) {
|
|
||||||
var sig = Math.round(bestSignal * 100)
|
|
||||||
if (sig > 80) parts.push("")
|
|
||||||
else if (sig > 60) parts.push("")
|
|
||||||
else if (sig > 40) parts.push("")
|
|
||||||
else if (sig > 20) parts.push("")
|
|
||||||
else if (sig > 0) parts.push("")
|
|
||||||
else parts.push("")
|
|
||||||
}
|
|
||||||
|
|
||||||
root.status = parts.join(" ")
|
|
||||||
}
|
|
||||||
|
|
||||||
function formatSpeed(bytesPerSec) {
|
|
||||||
if (bytesPerSec >= 1073741824) return (bytesPerSec / 1073741824).toFixed(1) + " GB/s"
|
|
||||||
if (bytesPerSec >= 1048576) return (bytesPerSec / 1048576).toFixed(1) + " MB/s"
|
|
||||||
if (bytesPerSec >= 1024) return (bytesPerSec / 1024).toFixed(1) + " KB/s"
|
|
||||||
return bytesPerSec.toFixed(0) + " B/s"
|
|
||||||
}
|
|
||||||
|
|
||||||
function parseOutput(output) {
|
|
||||||
var sections = output.split("===TAILSCALE_IP===")
|
var sections = output.split("===TAILSCALE_IP===")
|
||||||
var tailscaleIpSection = sections.length > 1 ? sections[1].trim() : ""
|
var tailscaleIpSection = sections.length > 1 ? sections[1].trim() : ""
|
||||||
|
|
||||||
@@ -230,8 +229,48 @@ QtObject {
|
|||||||
sections = beforeVpnActive.split("===VPN_ALL===")
|
sections = beforeVpnActive.split("===VPN_ALL===")
|
||||||
var vpnAllSection = sections.length > 1 ? sections[1].trim() : ""
|
var vpnAllSection = sections.length > 1 ? sections[1].trim() : ""
|
||||||
|
|
||||||
var beforeVpnAll = sections[0]
|
root.vpnExists = vpnAllSection.length > 0
|
||||||
sections = beforeVpnAll.split("===PING===")
|
if (vpnActiveSection) {
|
||||||
|
root.vpnConnected = true
|
||||||
|
var vpnLines = vpnActiveSection.split("\n")
|
||||||
|
if (vpnLines.length > 0) {
|
||||||
|
root.vpnName = vpnLines[0].split(":")[0] || ""
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
root.vpnConnected = false
|
||||||
|
root.vpnName = ""
|
||||||
|
root.vpnIp = ""
|
||||||
|
}
|
||||||
|
|
||||||
|
root.tailscaleExists = tailscaleExistsSection.indexOf("EXISTS") >= 0
|
||||||
|
if (tailscaleIpSection) {
|
||||||
|
root.tailscaleConnected = true
|
||||||
|
root.tailscaleIp = tailscaleIpSection
|
||||||
|
} else {
|
||||||
|
root.tailscaleConnected = false
|
||||||
|
root.tailscaleIp = ""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
property Process statsScript: Process {
|
||||||
|
command: ["sh", "-c",
|
||||||
|
"echo '===TRAFFIC==='; " +
|
||||||
|
"DEV=\"" + root._trafficActiveDevice + "\"; " +
|
||||||
|
"if [ -n \"$DEV\" ]; then " +
|
||||||
|
" echo \"RX_BYTES=$(cat /sys/class/net/$DEV/statistics/rx_bytes 2>/dev/null || echo 0)\"; " +
|
||||||
|
" echo \"TX_BYTES=$(cat /sys/class/net/$DEV/statistics/tx_bytes 2>/dev/null || echo 0)\"; " +
|
||||||
|
"fi; " +
|
||||||
|
"echo '===PING==='; " +
|
||||||
|
"ping -c 1 -W 2 1.1.1.1 2>/dev/null | awk -F/ '/rtt/{print $5}' || echo ''"
|
||||||
|
]
|
||||||
|
|
||||||
|
stdout: StdioCollector {
|
||||||
|
onStreamFinished: root.parseStats(text.trim())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function parseStats(output) {
|
||||||
|
var sections = output.split("===PING===")
|
||||||
root.ping = sections.length > 1 ? sections[1].trim() : ""
|
root.ping = sections.length > 1 ? sections[1].trim() : ""
|
||||||
|
|
||||||
var pingVal = parseFloat(root.ping)
|
var pingVal = parseFloat(root.ping)
|
||||||
@@ -242,9 +281,7 @@ QtObject {
|
|||||||
root.pingHistory = pingHist
|
root.pingHistory = pingHist
|
||||||
}
|
}
|
||||||
|
|
||||||
var beforePing = sections[0]
|
var trafficSection = sections[0].replace("===TRAFFIC===", "").trim()
|
||||||
sections = beforePing.split("===TRAFFIC===")
|
|
||||||
var trafficSection = sections.length > 1 ? sections[1].trim() : ""
|
|
||||||
|
|
||||||
if (trafficSection) {
|
if (trafficSection) {
|
||||||
var trafficLines = trafficSection.split("\n")
|
var trafficLines = trafficSection.split("\n")
|
||||||
@@ -284,28 +321,34 @@ QtObject {
|
|||||||
root._prevTxBytes = txBytes
|
root._prevTxBytes = txBytes
|
||||||
root._prevUpdateTime = Date.now()
|
root._prevUpdateTime = Date.now()
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
root.vpnExists = vpnAllSection.length > 0
|
function computeStatus(hasWired, hasWifi, bestSignal) {
|
||||||
if (vpnActiveSection) {
|
if (!hasWired && !hasWifi) {
|
||||||
root.vpnConnected = true
|
root.status = "/"
|
||||||
var vpnLines = vpnActiveSection.split("\n")
|
return
|
||||||
if (vpnLines.length > 0) {
|
|
||||||
root.vpnName = vpnLines[0].split(":")[0] || ""
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
root.vpnConnected = false
|
|
||||||
root.vpnName = ""
|
|
||||||
root.vpnIp = ""
|
|
||||||
}
|
}
|
||||||
|
|
||||||
root.tailscaleExists = tailscaleExistsSection.indexOf("EXISTS") >= 0
|
var parts = []
|
||||||
if (tailscaleIpSection) {
|
if (hasWired) parts.push("")
|
||||||
root.tailscaleConnected = true
|
if (hasWifi) {
|
||||||
root.tailscaleIp = tailscaleIpSection
|
var sig = Math.round(bestSignal * 100)
|
||||||
} else {
|
if (sig > 80) parts.push("")
|
||||||
root.tailscaleConnected = false
|
else if (sig > 60) parts.push("")
|
||||||
root.tailscaleIp = ""
|
else if (sig > 40) parts.push("")
|
||||||
|
else if (sig > 20) parts.push("")
|
||||||
|
else if (sig > 0) parts.push("")
|
||||||
|
else parts.push("")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
root.status = parts.join(" ")
|
||||||
|
}
|
||||||
|
|
||||||
|
function formatSpeed(bytesPerSec) {
|
||||||
|
if (bytesPerSec >= 1073741824) return (bytesPerSec / 1073741824).toFixed(1) + " GB/s"
|
||||||
|
if (bytesPerSec >= 1048576) return (bytesPerSec / 1048576).toFixed(1) + " MB/s"
|
||||||
|
if (bytesPerSec >= 1024) return (bytesPerSec / 1024).toFixed(1) + " KB/s"
|
||||||
|
return bytesPerSec.toFixed(0) + " B/s"
|
||||||
}
|
}
|
||||||
|
|
||||||
Component.onCompleted: {
|
Component.onCompleted: {
|
||||||
|
|||||||
Reference in New Issue
Block a user