Add online status detection and update dependencies

This commit is contained in:
2025-12-28 12:24:23 +01:00
parent 0b992a6a11
commit 79166b344c
4 changed files with 38 additions and 12 deletions

View File

@@ -6,6 +6,9 @@ import (
"os"
"os/exec"
"strings"
"time"
"github.com/prometheus-community/pro-bing"
)
const deviceFile = "devices.json"
@@ -15,6 +18,7 @@ type Device struct {
IP string `json:"ip"`
Interface string `json:"interface"`
Icon string `json:"icon"`
Online bool `json:"online"`
}
func GetDevices() map[string]map[string]Device {
@@ -26,6 +30,9 @@ func GetDevices() map[string]map[string]Device {
if strings.HasPrefix(device.Interface, "macvlan") {
continue
}
if strings.HasPrefix(device.Interface, "br") {
continue
}
if known, ok := knownDevices[mac]; ok {
devices[mac] = Device{Name: known.Name, IP: device.IP, Interface: device.Interface, Icon: known.Icon}
} else {
@@ -34,22 +41,28 @@ func GetDevices() map[string]map[string]Device {
}
for mac, device := range knownDevices {
if _, ok := devices[mac]; !ok {
devices[mac] = Device{Name: device.Name, IP: device.IP, Interface: "_offline_", Icon: device.Icon}
devices[mac] = Device{Name: device.Name, IP: device.IP, Interface: "_unknown_", Icon: device.Icon}
}
}
writeDevices(devices)
for mac, device := range devices {
if strings.HasPrefix(device.Interface, "br") {
device.Icon = "\uE7B0"
device.Interface = "bridge"
}
if _, ok := result[device.Interface]; !ok {
result[device.Interface] = make(map[string]Device)
}
if device.Icon == "" {
device.Icon = "\U000F0C8A"
}
pinger, err := probing.NewPinger(device.IP)
if err == nil {
pinger.Count = 1
pinger.Timeout = 500 * time.Millisecond
if err := pinger.Run(); err == nil {
if pinger.Statistics().PacketsRecv > 0 {
device.Online = true
}
}
}
result[device.Interface][mac] = device
}