Refactor NetworkDevices widget and GetDevices API to simplify device handling and remove interface-based grouping.

This commit is contained in:
Arindy 2025-12-28 12:49:18 +01:00
parent 79166b344c
commit 598e93c9d2
2 changed files with 23 additions and 43 deletions

View File

@ -21,35 +21,29 @@ type Device struct {
Online bool `json:"online"` Online bool `json:"online"`
} }
func GetDevices() map[string]map[string]Device { func GetDevices() map[string]Device {
result := make(map[string]map[string]Device) result := make(map[string]Device)
devices := make(map[string]Device) devices := make(map[string]Device)
arpDevices := arpDevices() arpDevices := arpDevices()
knownDevices := knownDevices() knownDevices := knownDevices()
for mac, device := range arpDevices { for mac, device := range arpDevices {
if strings.HasPrefix(device.Interface, "macvlan") {
continue
}
if strings.HasPrefix(device.Interface, "br") { if strings.HasPrefix(device.Interface, "br") {
continue continue
} }
if known, ok := knownDevices[mac]; ok { if known, ok := knownDevices[mac]; ok {
devices[mac] = Device{Name: known.Name, IP: device.IP, Interface: device.Interface, Icon: known.Icon} devices[mac] = Device{Name: known.Name, IP: device.IP, Icon: known.Icon}
} else { } else {
devices[mac] = device devices[mac] = device
} }
} }
for mac, device := range knownDevices { for mac, device := range knownDevices {
if _, ok := devices[mac]; !ok { if _, ok := devices[mac]; !ok {
devices[mac] = Device{Name: device.Name, IP: device.IP, Interface: "_unknown_", Icon: device.Icon} devices[mac] = Device{Name: device.Name, IP: device.IP, Icon: device.Icon}
} }
} }
writeDevices(devices) writeDevices(devices)
for mac, device := range devices { for mac, device := range devices {
if _, ok := result[device.Interface]; !ok {
result[device.Interface] = make(map[string]Device)
}
if device.Icon == "" { if device.Icon == "" {
device.Icon = "\U000F0C8A" device.Icon = "\U000F0C8A"
} }
@ -63,7 +57,7 @@ func GetDevices() map[string]map[string]Device {
} }
} }
} }
result[device.Interface][mac] = device result[mac] = device
} }
return result return result

View File

@ -26,35 +26,30 @@ func NetworkDevices() NetworkDevicesOptions {
func createNetworkDevicesList(ctx context.Context, _ terminalapi.Terminal, _ interface{}) widgetapi.Widget { func createNetworkDevicesList(ctx context.Context, _ terminalapi.Terminal, _ interface{}) widgetapi.Widget {
list := util.PanicOnErrorWithResult(text.New()) list := util.PanicOnErrorWithResult(text.New())
go util.Periodic(ctx, 20*time.Second, func() error { go util.Periodic(ctx, 20*time.Second, func() error {
interfaces := networking.GetDevices() devices := networking.GetDevices()
list.Reset() list.Reset()
for _, iface := range sortedInterfaces(interfaces) { for _, mac := range sortedKeys(devices) {
if err := list.Write(fmt.Sprintf("=== %s ===\n", iface)); err != nil {
return err
}
for _, mac := range sortedKeys(interfaces[iface]) {
status := cell.ColorGray status := cell.ColorGray
if interfaces[iface][mac].Online { if devices[mac].Online {
status = cell.ColorGreen status = cell.ColorGreen
} }
if err := list.Write(fmt.Sprintf("|%-15s|", mac), text.WriteCellOpts(cell.FgColor(cell.ColorGray))); err != nil { if err := list.Write(fmt.Sprintf("|%-15s|", mac), text.WriteCellOpts(cell.FgColor(cell.ColorGray))); err != nil {
return err return err
} }
if err := list.Write(fmt.Sprintf("%2s ", interfaces[iface][mac].Icon), text.WriteCellOpts(cell.BgColor(status), cell.FgColor(cell.ColorBlack))); err != nil { if err := list.Write(fmt.Sprintf("%2s ", devices[mac].Icon), text.WriteCellOpts(cell.BgColor(status), cell.FgColor(cell.ColorBlack))); err != nil {
return err return err
} }
if err := list.Write(fmt.Sprint("|"), text.WriteCellOpts(cell.FgColor(cell.ColorGray))); err != nil { if err := list.Write(fmt.Sprint("|"), text.WriteCellOpts(cell.FgColor(cell.ColorGray))); err != nil {
return err return err
} }
if err := list.Write(fmt.Sprintf(" %s", interfaces[iface][mac].Name), text.WriteCellOpts(cell.FgColor(cell.ColorWhite))); err != nil { if err := list.Write(fmt.Sprintf(" %s", devices[mac].Name), text.WriteCellOpts(cell.FgColor(cell.ColorWhite))); err != nil {
return err return err
} }
if err := list.Write(fmt.Sprintf(" (%s)\n", interfaces[iface][mac].IP), text.WriteCellOpts(cell.FgColor(cell.ColorGray))); err != nil { if err := list.Write(fmt.Sprintf(" (%s)\n", devices[mac].IP), text.WriteCellOpts(cell.FgColor(cell.ColorGray))); err != nil {
return err return err
} }
} }
}
return nil return nil
}) })
@ -62,15 +57,6 @@ func createNetworkDevicesList(ctx context.Context, _ terminalapi.Terminal, _ int
return list return list
} }
func sortedInterfaces(interfaces map[string]map[string]networking.Device) []string {
keys := make([]string, 0, len(interfaces))
for key := range interfaces {
keys = append(keys, key)
}
sort.Strings(keys)
return keys
}
func sortedKeys(devices map[string]networking.Device) []string { func sortedKeys(devices map[string]networking.Device) []string {
macs := make([]string, 0, len(devices)) macs := make([]string, 0, len(devices))
for mac := range devices { for mac := range devices {