Просмотр исходного кода

feat(devices): tipo estimado por hostname en live (iconos reales en el mapa)

Live hardcodeaba type=desconocido (heredado del backend Node): todos los
chips wifi salian con '?'. Clasificador por patrones de hostname (+ OUI
IoT de refuerzo) con fallback conservador a desconocido. Icono neutro
CircuitBoard para los que sigan sin clasificar (el '?' era ruido visual).
gnacho 4 месяцев назад
Родитель
Сommit
9dc3d3258c

+ 3 - 3
app/src/components/DeviceRow.tsx

@@ -1,7 +1,7 @@
 import {
   Camera,
+  CircuitBoard,
   Gamepad2,
-  HelpCircle,
   Laptop,
   Lightbulb,
   Monitor,
@@ -38,7 +38,7 @@ export const DEVICE_ICONS: Record<DeviceType, LucideIcon> = {
   altavoz: Speaker,
   servidor: Server,
   switch: Network,
-  desconocido: HelpCircle,
+  desconocido: CircuitBoard,
 }
 
 const SIGNAL_ICONS = {
@@ -83,7 +83,7 @@ export function DeviceRow({ device, variant = 'compact', className, onClick }: D
   const { t } = useTranslation()
   const { routers } = useNetPulse()
   const routerName = (id: string) => routers.find((r) => r.id === id)?.name ?? id
-  const Icon = DEVICE_ICONS[device.type] ?? HelpCircle
+  const Icon = DEVICE_ICONS[device.type] ?? CircuitBoard
   return (
     <div
       onClick={onClick}

+ 56 - 0
server-go/internal/adapters/classify.go

@@ -0,0 +1,56 @@
+package adapters
+
+import "strings"
+
+// ---------------------------------------------------------------------------
+// Clasificación de tipo de dispositivo live (2-Ago-2026). El FDB/DHCP no dice
+// QUÉ es cada cliente: se estima por patrones de hostname (y en el futuro por
+// OUI). Solo afecta a presentación (icono/grupo); ante la duda, "desconocido"
+// — nunca afirmar un tipo sin evidencia razonable.
+// ---------------------------------------------------------------------------
+
+// typeRule: substrings (minúsculas) del hostname → tipo. El orden importa:
+// la primera regla que casa gana (de más específica a más genérica).
+var typeRules = []struct {
+	typ  string
+	subs []string
+}{
+	// Consola antes que tv/iot: "switch" a secas es switch de red, pero
+	// "switch OLED" no aparece en hostnames reales; Nintendo suele anunciarse
+	// como "nintendo-switch".
+	{"consola", []string{"playstation", "ps4", "ps5", "xbox", "nintendo", "steamdeck", "steam-deck", "consola"}},
+	{"tv", []string{"appletv", "apple-tv", "bravia", "webos", "firetv", "fire-tv", "chromecast", "mibox", "mi-box", "shield", "television", "smart-tv", "smarttv", "-tv", "tv-"}},
+	{"camara", []string{"camara", "camera", "doorbell", "timbre", "ezviz", "tapo-c", "-cam", "cam-"}},
+	{"altavoz", []string{"sonos", "heos", "homepod", "echo-", "altavoz", "speaker", "soundbar", "marantz", "denon", "home-mini", "nest-mini", "nest-audio", "amplificador", "receiver"}},
+	{"tablet", []string{"ipad", "tablet", "kindle", "kobo"}},
+	{"movil", []string{"iphone", "android", "pixel", "galaxy-s", "galaxy-a", "redmi-note", "oneplus", "xiaomi-1", "mi-1", "phone", "movil"}},
+	{"portatil", []string{"macbook", "laptop", "thinkpad", "ideapad", "portatil", "notebook", "surface", "hp-laptop", "lenovo-yoga"}},
+	{"servidor", []string{"proxmox", "pve", "jellyfin", "transmission", "helios", "homeassistant", "home-assistant", "haos", "servidor", "server", "nas", "citadel", "omv", "truenas", "pihole", "pi-hole", "adguard", "raspberry", "rpi", "docker", "keynest", "deltos", "nido", "netpulse"}},
+	{"ordenador", []string{"imac", "mac-mini", "macstudio", "mac-studio", "desktop", "sobremesa", "workstation", "nuc", "ser9", "pc-", "-pc", "tower", "minipc", "mini-pc"}},
+	{"switch", []string{"gs308", "gs305", "tl-sg", "switch"}},
+	{"iot", []string{"roomba", "irobot", "roborock", "aspirador", "robot", "tasmota", "sonoff", "shelly", "esphome", "tuya", "smartlife", "meross", "gosund", "switchbot", "aqara", "lumi", "zigbee", "zhirui", "osram", "ikea", "tradfri", "hue", "wled", "athom", "cargador", "wallbox", "feyree", "tedee", "cerradura", "enchufe", "plug", "bombilla", "downlight", "persiana", "curtain", "riego", "sprinkler", "termo", "termostato", "caldera", "aire", "ac-", "slzb", "impresora", "printer", "epson", "brother", "canon"}},
+}
+
+// GuessDeviceType estima el DeviceType a partir del hostname (DHCP) y, si se
+// conoce, del fabricante OUI. Vacío/desconocido → "desconocido".
+func GuessDeviceType(hostname, manufacturer string) string {
+	h := strings.ToLower(strings.TrimSpace(hostname))
+	m := strings.ToLower(strings.TrimSpace(manufacturer))
+	// Hostnames basura frecuentes (MAC como nombre, "unknown-…", "android-…"
+	// genérico de DHCP) solo aportan si casan con una regla clara.
+	for _, r := range typeRules {
+		for _, s := range r.subs {
+			if h != "" && strings.Contains(h, s) {
+				return r.typ
+			}
+		}
+	}
+	// Fabricante como refuerzo cuando el hostname no dice nada.
+	ouiIoT := []string{"espressif", "tuya", "shenzhen", "sonoff", "shelly", "tasmota", "meross", "gosund", "lumi", "aqara", "tuya smart"}
+	for _, s := range ouiIoT {
+		if m != "" && strings.Contains(m, s) {
+			return "iot"
+		}
+	}
+	return "desconocido"
+}

+ 46 - 0
server-go/internal/adapters/classify_test.go

@@ -0,0 +1,46 @@
+package adapters
+
+import "testing"
+
+func TestGuessDeviceType(t *testing.T) {
+	cases := []struct {
+		hostname, manufacturer, want string
+	}{
+		{"iPhone-de-Nacho", "", "movil"},
+		{"pixel-8-pro", "", "movil"},
+		{"MacBook-Air", "", "portatil"},
+		{"thinkpad-x1", "", "portatil"},
+		{"kindle-paperwhite", "", "tablet"},
+		{"iPad-de-casa", "", "tablet"},
+		{"shield", "", "tv"},
+		{"lg-webos-tv", "", "tv"},
+		{"ps5-salon", "", "consola"},
+		{"heos5", "", "altavoz"},
+		{"sonos-cocina", "", "altavoz"},
+		{"marantz-av", "", "altavoz"},
+		{"jellyfin", "", "servidor"},
+		{"transmission", "", "servidor"},
+		{"homeassistant", "", "servidor"},
+		{"raspberry-pi", "", "servidor"},
+		{"pc-sobremesa", "", "ordenador"},
+		{"imac-estudio", "", "ordenador"},
+		{"switch-netgear", "", "switch"},
+		{"gs308e", "", "switch"},
+		{"roomba960", "", "iot"},
+		{"zhirui-4c0268", "", "iot"},
+		{"cargador-coche", "", "iot"},
+		{"slzb-06m", "", "iot"},
+		{"camara-porche", "", "camara"},
+		{"esp32-cam-taller", "", "camara"},
+		{"sonoff-mini", "", "iot"},
+		{"A4:CF:12:9A:01:02", "", "desconocido"}, // MAC como nombre
+		{"", "", "desconocido"},
+		{"", "Espressif Inc.", "iot"},
+		{"", "Tuya Smart Inc.", "iot"},
+	}
+	for _, c := range cases {
+		if got := GuessDeviceType(c.hostname, c.manufacturer); got != c.want {
+			t.Errorf("GuessDeviceType(%q, %q) = %q, esperaba %q", c.hostname, c.manufacturer, got, c.want)
+		}
+	}
+}

+ 4 - 1
server-go/internal/adapters/live.go

@@ -791,7 +791,7 @@ func (l *Live) buildDevices(polled map[string]*routerPolled) []Device {
 		s, isSeen := seen[mac]
 		d := Device{
 			ID: strings.ToLower(strings.ReplaceAll(mac, ":", "-")),
-			MAC: mac, Type: "desconocido", Manufacturer: "Desconocido",
+			MAC: mac, Manufacturer: "Desconocido",
 			TrafficMbps: 0, Sparkline: []float64{},
 			RouterID: gwID, Band: "—",
 			Online: isSeen,
@@ -806,6 +806,9 @@ func (l *Live) buildDevices(polled map[string]*routerPolled) []Device {
 		} else {
 			d.Name = mac
 		}
+		// Tipo estimado por hostname (el DHCP/FDB no dice qué es el cliente).
+		// Con nombre-MAC (sin hostname) queda "desconocido".
+		d.Type = GuessDeviceType(d.Name, d.Manufacturer)
 		if isSeen {
 			d.RouterID = s.routerID
 			d.Band = s.band