Implement comprehensive configuration management with CLI flags for WiFi interface, device discovery method, and file paths. Add ARP table parsing as an alternative to DHCP leases for more reliable device detection. Improve WiFi scanning to handle concurrent scan requests gracefully. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
133 lines
3.8 KiB
Go
133 lines
3.8 KiB
Go
package handlers
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"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/wifi"
|
|
)
|
|
|
|
// ScanWiFi handles WiFi network scanning
|
|
func ScanWiFi(c *gin.Context) {
|
|
networks, err := wifi.ScanNetworks()
|
|
if err != nil {
|
|
logging.AddLog("WiFi", "Erreur lors du scan: "+err.Error())
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Erreur lors du scan WiFi"})
|
|
return
|
|
}
|
|
|
|
logging.AddLog("WiFi", "Scan terminé - "+string(rune(len(networks)))+" réseaux trouvés")
|
|
c.JSON(http.StatusOK, networks)
|
|
}
|
|
|
|
// ConnectWiFi handles WiFi connection requests
|
|
func ConnectWiFi(c *gin.Context) {
|
|
var req models.WiFiConnectRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Données invalides"})
|
|
return
|
|
}
|
|
|
|
logging.AddLog("WiFi", "Tentative de connexion à "+req.SSID)
|
|
|
|
err := wifi.Connect(req.SSID, req.Password)
|
|
if err != nil {
|
|
logging.AddLog("WiFi", "Échec de connexion: "+err.Error())
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Échec de connexion: " + err.Error()})
|
|
return
|
|
}
|
|
|
|
logging.AddLog("WiFi", "Connexion réussie à "+req.SSID)
|
|
c.JSON(http.StatusOK, gin.H{"status": "success"})
|
|
}
|
|
|
|
// DisconnectWiFi handles WiFi disconnection
|
|
func DisconnectWiFi(c *gin.Context) {
|
|
logging.AddLog("WiFi", "Tentative de déconnexion")
|
|
|
|
err := wifi.Disconnect()
|
|
if err != nil {
|
|
logging.AddLog("WiFi", "Erreur de déconnexion: "+err.Error())
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Erreur de déconnexion: " + err.Error()})
|
|
return
|
|
}
|
|
|
|
logging.AddLog("WiFi", "Déconnexion réussie")
|
|
c.JSON(http.StatusOK, gin.H{"status": "success"})
|
|
}
|
|
|
|
// ConfigureHotspot handles hotspot configuration
|
|
func ConfigureHotspot(c *gin.Context) {
|
|
var config models.HotspotConfig
|
|
if err := c.ShouldBindJSON(&config); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Données invalides"})
|
|
return
|
|
}
|
|
|
|
err := hotspot.Configure(config)
|
|
if err != nil {
|
|
logging.AddLog("Hotspot", "Erreur de configuration: "+err.Error())
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Erreur de configuration: " + err.Error()})
|
|
return
|
|
}
|
|
|
|
logging.AddLog("Hotspot", "Configuration mise à jour: "+config.SSID)
|
|
c.JSON(http.StatusOK, gin.H{"status": "success"})
|
|
}
|
|
|
|
// ToggleHotspot handles hotspot enable/disable
|
|
func ToggleHotspot(c *gin.Context, status *models.SystemStatus) {
|
|
status.HotspotEnabled = !status.HotspotEnabled
|
|
enabled := status.HotspotEnabled
|
|
|
|
var err error
|
|
if enabled {
|
|
err = hotspot.Start()
|
|
logging.AddLog("Hotspot", "Hotspot activé")
|
|
} else {
|
|
err = hotspot.Stop()
|
|
logging.AddLog("Hotspot", "Hotspot désactivé")
|
|
}
|
|
|
|
if err != nil {
|
|
logging.AddLog("Hotspot", "Erreur: "+err.Error())
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Erreur: " + err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{"enabled": enabled})
|
|
}
|
|
|
|
// GetDevices returns connected devices
|
|
func GetDevices(c *gin.Context, cfg *config.Config) {
|
|
devices, err := device.GetConnectedDevices(cfg)
|
|
if err != nil {
|
|
logging.AddLog("Système", "Erreur lors de la récupération des appareils: "+err.Error())
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Erreur lors de la récupération des appareils"})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, devices)
|
|
}
|
|
|
|
// GetStatus returns system status
|
|
func GetStatus(c *gin.Context, status *models.SystemStatus) {
|
|
c.JSON(http.StatusOK, status)
|
|
}
|
|
|
|
// GetLogs returns system logs
|
|
func GetLogs(c *gin.Context) {
|
|
logs := logging.GetLogs()
|
|
c.JSON(http.StatusOK, logs)
|
|
}
|
|
|
|
// ClearLogs clears system logs
|
|
func ClearLogs(c *gin.Context) {
|
|
logging.ClearLogs()
|
|
c.JSON(http.StatusOK, gin.H{"status": "success"})
|
|
}
|