Refactor NetworkDevices widget and GetDevices API to simplify device handling and remove interface-based grouping.
This commit is contained in:
parent
79166b344c
commit
598e93c9d2
@ -21,35 +21,29 @@ type Device struct {
|
||||
Online bool `json:"online"`
|
||||
}
|
||||
|
||||
func GetDevices() map[string]map[string]Device {
|
||||
result := make(map[string]map[string]Device)
|
||||
func GetDevices() map[string]Device {
|
||||
result := make(map[string]Device)
|
||||
devices := make(map[string]Device)
|
||||
arpDevices := arpDevices()
|
||||
knownDevices := knownDevices()
|
||||
for mac, device := range arpDevices {
|
||||
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}
|
||||
devices[mac] = Device{Name: known.Name, IP: device.IP, Icon: known.Icon}
|
||||
} else {
|
||||
devices[mac] = device
|
||||
}
|
||||
}
|
||||
for mac, device := range knownDevices {
|
||||
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)
|
||||
|
||||
for mac, device := range devices {
|
||||
if _, ok := result[device.Interface]; !ok {
|
||||
result[device.Interface] = make(map[string]Device)
|
||||
}
|
||||
if device.Icon == "" {
|
||||
device.Icon = "\U000F0C8A"
|
||||
}
|
||||
@ -63,7 +57,7 @@ func GetDevices() map[string]map[string]Device {
|
||||
}
|
||||
}
|
||||
}
|
||||
result[device.Interface][mac] = device
|
||||
result[mac] = device
|
||||
}
|
||||
|
||||
return result
|
||||
|
||||
@ -26,33 +26,28 @@ func NetworkDevices() NetworkDevicesOptions {
|
||||
func createNetworkDevicesList(ctx context.Context, _ terminalapi.Terminal, _ interface{}) widgetapi.Widget {
|
||||
list := util.PanicOnErrorWithResult(text.New())
|
||||
go util.Periodic(ctx, 20*time.Second, func() error {
|
||||
interfaces := networking.GetDevices()
|
||||
devices := networking.GetDevices()
|
||||
list.Reset()
|
||||
for _, iface := range sortedInterfaces(interfaces) {
|
||||
if err := list.Write(fmt.Sprintf("=== %s ===\n", iface)); err != nil {
|
||||
for _, mac := range sortedKeys(devices) {
|
||||
status := cell.ColorGray
|
||||
if devices[mac].Online {
|
||||
status = cell.ColorGreen
|
||||
}
|
||||
if err := list.Write(fmt.Sprintf("|%-15s|", mac), text.WriteCellOpts(cell.FgColor(cell.ColorGray))); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := list.Write(fmt.Sprintf("%2s ", devices[mac].Icon), text.WriteCellOpts(cell.BgColor(status), cell.FgColor(cell.ColorBlack))); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := list.Write(fmt.Sprint("|"), text.WriteCellOpts(cell.FgColor(cell.ColorGray))); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, mac := range sortedKeys(interfaces[iface]) {
|
||||
status := cell.ColorGray
|
||||
if interfaces[iface][mac].Online {
|
||||
status = cell.ColorGreen
|
||||
}
|
||||
if err := list.Write(fmt.Sprintf("|%-15s|", mac), text.WriteCellOpts(cell.FgColor(cell.ColorGray))); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := list.Write(fmt.Sprintf("%2s ", interfaces[iface][mac].Icon), text.WriteCellOpts(cell.BgColor(status), cell.FgColor(cell.ColorBlack))); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := list.Write(fmt.Sprint("|"), text.WriteCellOpts(cell.FgColor(cell.ColorGray))); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := list.Write(fmt.Sprintf(" %s", interfaces[iface][mac].Name), text.WriteCellOpts(cell.FgColor(cell.ColorWhite))); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := list.Write(fmt.Sprintf(" (%s)\n", interfaces[iface][mac].IP), text.WriteCellOpts(cell.FgColor(cell.ColorGray))); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := list.Write(fmt.Sprintf(" %s", devices[mac].Name), text.WriteCellOpts(cell.FgColor(cell.ColorWhite))); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := list.Write(fmt.Sprintf(" (%s)\n", devices[mac].IP), text.WriteCellOpts(cell.FgColor(cell.ColorGray))); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
@ -62,15 +57,6 @@ func createNetworkDevicesList(ctx context.Context, _ terminalapi.Terminal, _ int
|
||||
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 {
|
||||
macs := make([]string, 0, len(devices))
|
||||
for mac := range devices {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user