Refactor stations discovery and add hostapd discovery

This commit is contained in:
nemunaire 2026-01-01 23:29:34 +07:00
commit 2922a03724
15 changed files with 1339 additions and 249 deletions

View file

@ -11,10 +11,11 @@ import (
"github.com/nemunaire/repeater/internal/api"
"github.com/nemunaire/repeater/internal/config"
"github.com/nemunaire/repeater/internal/device"
"github.com/nemunaire/repeater/internal/hotspot"
"github.com/nemunaire/repeater/internal/logging"
"github.com/nemunaire/repeater/internal/models"
"github.com/nemunaire/repeater/internal/station"
"github.com/nemunaire/repeater/internal/station/backend"
"github.com/nemunaire/repeater/internal/syslog"
"github.com/nemunaire/repeater/internal/wifi"
)
@ -62,6 +63,28 @@ func (a *App) Initialize(cfg *config.Config) error {
// Don't fail - polling fallback still works
}
// Initialize station backend
stationConfig := backend.BackendConfig{
InterfaceName: cfg.HotspotInterface,
ARPTablePath: cfg.ARPTablePath,
DHCPLeasesPath: cfg.DHCPLeasesPath,
HostapdInterface: cfg.HotspotInterface,
}
if err := station.Initialize(cfg.StationBackend, stationConfig); err != nil {
log.Printf("Warning: Station backend initialization failed: %v", err)
// Don't fail - will continue without station discovery
} else {
// Start event monitoring for station events
if err := station.StartEventMonitoring(backend.EventCallbacks{
OnStationConnected: a.handleStationConnected,
OnStationDisconnected: a.handleStationDisconnected,
OnStationUpdated: a.handleStationUpdated,
}); err != nil {
log.Printf("Warning: Station event monitoring failed: %v", err)
// Don't fail - polling fallback still works
}
}
// Start syslog tailing if enabled
if cfg.SyslogEnabled {
a.SyslogTailer = syslog.NewSyslogTailer(
@ -98,6 +121,10 @@ func (a *App) Shutdown() {
a.SyslogTailer.Stop()
}
// Stop station monitoring and close backend
station.StopEventMonitoring()
station.Close()
wifi.StopEventMonitoring()
wifi.Close()
logging.AddLog("Système", "Application arrêtée")
@ -181,10 +208,9 @@ func (a *App) periodicDeviceUpdate() {
defer ticker.Stop()
for range ticker.C {
devices, err := device.GetConnectedDevices(a.Config)
devices, err := station.GetStations()
if err != nil {
log.Printf("Error getting connected devices: %v", err)
continue
}
a.StatusMutex.Lock()
@ -193,3 +219,69 @@ func (a *App) periodicDeviceUpdate() {
a.StatusMutex.Unlock()
}
}
// handleStationConnected handles station connection events
func (a *App) handleStationConnected(st backend.Station) {
a.StatusMutex.Lock()
defer a.StatusMutex.Unlock()
// Convert backend.Station to models.ConnectedDevice
device := models.ConnectedDevice{
Name: st.Hostname,
Type: st.Type,
MAC: st.MAC,
IP: st.IP,
}
// Check if device already exists
found := false
for i, d := range a.Status.ConnectedDevices {
if d.MAC == device.MAC {
a.Status.ConnectedDevices[i] = device
found = true
break
}
}
// Add new device if not found
if !found {
a.Status.ConnectedDevices = append(a.Status.ConnectedDevices, device)
a.Status.ConnectedCount = len(a.Status.ConnectedDevices)
logging.AddLog("Stations", "Device connected: "+device.MAC+" ("+device.IP+")")
}
}
// handleStationDisconnected handles station disconnection events
func (a *App) handleStationDisconnected(mac string) {
a.StatusMutex.Lock()
defer a.StatusMutex.Unlock()
// Remove device from list
for i, d := range a.Status.ConnectedDevices {
if d.MAC == mac {
a.Status.ConnectedDevices = append(a.Status.ConnectedDevices[:i], a.Status.ConnectedDevices[i+1:]...)
a.Status.ConnectedCount = len(a.Status.ConnectedDevices)
logging.AddLog("Stations", "Device disconnected: "+mac)
break
}
}
}
// handleStationUpdated handles station update events
func (a *App) handleStationUpdated(st backend.Station) {
a.StatusMutex.Lock()
defer a.StatusMutex.Unlock()
// Update existing device
for i, d := range a.Status.ConnectedDevices {
if d.MAC == st.MAC {
a.Status.ConnectedDevices[i] = models.ConnectedDevice{
Name: st.Hostname,
Type: st.Type,
MAC: st.MAC,
IP: st.IP,
}
break
}
}
}