فهرست منبع

fix(devices): apply manual overrides to offline (ghost) devices too

A retained client lost its manual icon, name and type as soon as it went offline, because ghostDevices built the device without the overrides. The load now lives in one helper used by both buildDevices and ghostDevices, so an override looks the same online and offline.

Does not touch the online path and ghostDevices emits no alerts.

Closes #2088
Co-authored-by: Bann-DEV <hef.fangyuan@gmail.com>
Bann-DEV 2 هفته پیش
والد
کامیت
a7b9008f6d

+ 18 - 0
server-go/internal/adapters/device_seen.go

@@ -494,6 +494,7 @@ func (l *Live) ghostDevices(devices []Device) []Device {
 		tomb = map[string]bool{}
 	}
 	now := l.seenNow()
+	overrides := l.loadDeviceOverrides()
 	var out []Device
 	for rawMac, s := range byMac {
 		mac := normSeenMAC(rawMac)
@@ -531,6 +532,23 @@ func (l *Live) ghostDevices(devices []Device) []Device {
 			d.Hostname = s.name
 			d.Name = s.name
 		}
+		// #437/#797: el override manual manda también en el fantasma offline
+		// (icono, nombre y tipo). Sin esto un cliente retenido perdía su icono
+		// al caer offline y no reflejaba los cambios hechos estando offline.
+		if ov, ok := overrides[strings.ToLower(mac)]; ok {
+			if ov.Icon != "" {
+				d.IconOverride = ov.Icon
+			}
+			if ov.Name != "" {
+				d.Name = ov.Name
+				d.NameOverride = ov.Name
+			}
+			if ov.DeviceType != "" {
+				d.Type = ov.DeviceType
+				d.TypeOverride = ov.DeviceType
+			}
+			d.NotifyOffline = ov.NotifyOffline
+		}
 		if aliases := revAlias[mac]; len(aliases) > 0 {
 			d.AliasMacs = aliases
 		}

+ 35 - 24
server-go/internal/adapters/live.go

@@ -1815,6 +1815,40 @@ func hostLikePorts(ports []EthPort) bool {
 	return false
 }
 
+// overrideInfo es el override manual persistido por MAC (#437/#797).
+type overrideInfo struct {
+	Icon          string
+	Name          string
+	DeviceType    string
+	BannedBands   string
+	NotifyOffline bool
+}
+
+// loadDeviceOverrides lee los overrides manuales, con clave en minúsculas
+// para que el lookup sea robusto al caso de la MAC (#797). Lo usan tanto
+// buildDevices (clientes vivos) como ghostDevices (retenidos offline):
+// un override debe verse igual esté el cliente online u offline.
+func (l *Live) loadDeviceOverrides() map[string]overrideInfo {
+	out := map[string]overrideInfo{}
+	if l.db == nil {
+		return out
+	}
+	rows, err := l.db.Query("SELECT mac, icon, name, device_type, banned_bands, notify_offline FROM device_overrides")
+	if err != nil {
+		return out
+	}
+	defer rows.Close()
+	for rows.Next() {
+		var mac, name, deviceType, bannedBands string
+		var icon sql.NullString
+		var notifyOffline bool
+		if err := rows.Scan(&mac, &icon, &name, &deviceType, &bannedBands, &notifyOffline); err == nil {
+			out[strings.ToLower(mac)] = overrideInfo{Icon: icon.String, Name: name, DeviceType: deviceType, BannedBands: bannedBands, NotifyOffline: notifyOffline}
+		}
+	}
+	return out
+}
+
 func (l *Live) buildRouter(p *routerPolled, history []histPoint) Router {
 	l.mu.Lock()
 	gw := l.gatewayCfg
@@ -3873,30 +3907,7 @@ func (l *Live) buildDevices(polled map[string]*routerPolled, wanPort string) []D
 	}
 	// issue #437/#797: overrides manuales de dispositivo (icono, nombre
 	// visible y tipo) sobre el dispositivo auto-descubierto.
-	type overrideInfo struct {
-		Icon          string
-		Name          string
-		DeviceType    string
-		BannedBands   string
-		NotifyOffline bool
-	}
-	deviceOverrides := map[string]overrideInfo{}
-	if l.db != nil {
-		if rows, err := l.db.Query("SELECT mac, icon, name, device_type, banned_bands, notify_offline FROM device_overrides"); err == nil {
-			for rows.Next() {
-				var mac, name, deviceType, bannedBands string
-				var icon sql.NullString
-				var notifyOffline bool
-				if err := rows.Scan(&mac, &icon, &name, &deviceType, &bannedBands, &notifyOffline); err == nil {
-					// Clave en minúsculas: las MACs de los sondeos pueden
-					// llegar en cualquier caso y el lookup debe ser robusto
-					// (#797: icon overrides que "desaparecían" por caso).
-					deviceOverrides[strings.ToLower(mac)] = overrideInfo{Icon: icon.String, Name: name, DeviceType: deviceType, BannedBands: bannedBands, NotifyOffline: notifyOffline}
-				}
-			}
-			rows.Close()
-		}
-	}
+	deviceOverrides := l.loadDeviceOverrides()
 	lldpCapsByMac := map[string]string{}
 	for _, p := range polled {
 		for _, nb := range p.lldp {

+ 34 - 0
server-go/internal/adapters/override_builddevices_test.go

@@ -41,3 +41,37 @@ func TestBuildDevicesNameTypeIconOverride(t *testing.T) {
 		t.Errorf("icon override no aplicado: %+v", got)
 	}
 }
+
+// Los mismos overrides deben verse en el cliente retenido offline (fantasma):
+// sin esto el icono cambiaba al caer offline y editar icono/tipo estando
+// offline no se reflejaba nunca.
+func TestGhostDevicesApplyManualOverrides(t *testing.T) {
+	d := openLiveTestDB(t)
+	l := &Live{db: d}
+	l.noteDevicesSeen([]Device{
+		{MAC: "aa:bb:cc:dd:ee:ff", Online: true, Hostname: "viewsonic-proj", Type: "desconocido"},
+	}, 1000)
+	now := time.Now().UnixMilli()
+	if _, err := d.Exec(
+		`INSERT INTO device_overrides (mac, icon, name, device_type, banned_bands, created_at, updated_at)
+		 VALUES ('aa:bb:cc:dd:ee:ff', 'projector', '投影', 'tv', '', ?, ?)`, now, now); err != nil {
+		t.Fatalf("seed override: %v", err)
+	}
+	ghosts := l.ghostDevices(nil)
+	if len(ghosts) != 1 {
+		t.Fatalf("esperado 1 fantasma, got %d", len(ghosts))
+	}
+	g := ghosts[0]
+	if g.Online {
+		t.Fatalf("el fantasma debe ser offline")
+	}
+	if g.IconOverride != "projector" {
+		t.Errorf("icon override no aplicado en offline: %+v", g)
+	}
+	if g.Name != "投影" || g.NameOverride != "投影" {
+		t.Errorf("name override no aplicado en offline: %+v", g)
+	}
+	if g.Type != "tv" || g.TypeOverride != "tv" {
+		t.Errorf("type override no aplicado en offline: %+v", g)
+	}
+}