148 lines
4.3 KiB
Go
148 lines
4.3 KiB
Go
package handlers
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/nemunaire/repeater/internal/config"
|
|
"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/wifi"
|
|
)
|
|
|
|
// GetWiFiNetworks returns cached WiFi networks without scanning
|
|
func GetWiFiNetworks(c *gin.Context) {
|
|
networks, err := wifi.GetCachedNetworks()
|
|
if err != nil {
|
|
logging.AddLog("WiFi", "Erreur lors de la récupération des réseaux: "+err.Error())
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Erreur lors de la récupération des réseaux"})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, networks)
|
|
}
|
|
|
|
// 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) {
|
|
// Determine current state
|
|
isEnabled := status.HotspotStatus != nil && status.HotspotStatus.State == "ENABLED"
|
|
|
|
var err error
|
|
if !isEnabled {
|
|
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
|
|
}
|
|
|
|
// Update status immediately
|
|
status.HotspotStatus = hotspot.GetDetailedStatus()
|
|
|
|
c.JSON(http.StatusOK, gin.H{"enabled": !isEnabled})
|
|
}
|
|
|
|
// GetDevices returns connected devices
|
|
func GetDevices(c *gin.Context, cfg *config.Config) {
|
|
devices, err := station.GetStations()
|
|
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"})
|
|
}
|