repeater/internal/station/station.go

111 lines
2.1 KiB
Go

package station
import (
"sync"
"github.com/nemunaire/repeater/internal/models"
"github.com/nemunaire/repeater/internal/station/backend"
)
var (
currentBackend backend.StationBackend
mu sync.RWMutex
)
// Initialize initializes the station discovery backend
func Initialize(backendName string, config backend.BackendConfig) error {
mu.Lock()
defer mu.Unlock()
// Close existing backend if any
if currentBackend != nil {
currentBackend.Close()
}
// Create new backend
b, err := createBackend(backendName)
if err != nil {
return err
}
// Initialize the backend
if err := b.Initialize(config); err != nil {
return err
}
currentBackend = b
return nil
}
// GetStations returns all connected stations as ConnectedDevice models
func GetStations() ([]models.ConnectedDevice, error) {
mu.RLock()
defer mu.RUnlock()
if currentBackend == nil {
return nil, nil
}
stations, err := currentBackend.GetStations()
if err != nil {
return nil, err
}
// Convert backend.Station to models.ConnectedDevice
devices := make([]models.ConnectedDevice, len(stations))
for i, s := range stations {
devices[i] = models.ConnectedDevice{
Name: s.Hostname,
Type: s.Type,
MAC: s.MAC,
IP: s.IP,
}
}
return devices, nil
}
// StartEventMonitoring starts monitoring for station events
func StartEventMonitoring(callbacks backend.EventCallbacks) error {
mu.RLock()
defer mu.RUnlock()
if currentBackend == nil {
return nil
}
return currentBackend.StartEventMonitoring(callbacks)
}
// StopEventMonitoring stops monitoring for station events
func StopEventMonitoring() {
mu.RLock()
defer mu.RUnlock()
if currentBackend != nil {
currentBackend.StopEventMonitoring()
}
}
// Close closes the current backend
func Close() {
mu.Lock()
defer mu.Unlock()
if currentBackend != nil {
currentBackend.Close()
currentBackend = nil
}
}
// SupportsRealTimeEvents returns true if the current backend supports real-time events
func SupportsRealTimeEvents() bool {
mu.RLock()
defer mu.RUnlock()
if currentBackend == nil {
return false
}
return currentBackend.SupportsRealTimeEvents()
}