up
This commit is contained in:
75
cmd/server/main.go
Normal file
75
cmd/server/main.go
Normal file
@@ -0,0 +1,75 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"os/signal"
|
||||||
|
"syscall"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"git.rouggy.com/rouggy/ShackMaster/internal/api"
|
||||||
|
"git.rouggy.com/rouggy/ShackMaster/internal/config"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
log.Println("Starting ShackMaster server...")
|
||||||
|
|
||||||
|
// Load configuration
|
||||||
|
cfg, err := config.Load("configs/config.yaml")
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Failed to load configuration: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("Configuration loaded: %s:%d", cfg.Server.Host, cfg.Server.Port)
|
||||||
|
|
||||||
|
// Create WebSocket hub
|
||||||
|
hub := api.NewHub()
|
||||||
|
go hub.Run()
|
||||||
|
|
||||||
|
// Initialize device manager with hub
|
||||||
|
deviceManager := api.NewDeviceManager(cfg, hub)
|
||||||
|
if err := deviceManager.Initialize(); err != nil {
|
||||||
|
log.Fatalf("Failed to initialize device manager: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Start device monitoring (will broadcast automatically)
|
||||||
|
if err := deviceManager.Start(); err != nil {
|
||||||
|
log.Fatalf("Failed to start device manager: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create HTTP server
|
||||||
|
server := api.NewServer(deviceManager, hub, cfg)
|
||||||
|
mux := server.SetupRoutes()
|
||||||
|
|
||||||
|
// Setup HTTP server
|
||||||
|
addr := fmt.Sprintf("%s:%d", cfg.Server.Host, cfg.Server.Port)
|
||||||
|
httpServer := &http.Server{
|
||||||
|
Addr: addr,
|
||||||
|
Handler: mux,
|
||||||
|
ReadTimeout: 15 * time.Second,
|
||||||
|
WriteTimeout: 15 * time.Second,
|
||||||
|
IdleTimeout: 60 * time.Second,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Start HTTP server in goroutine
|
||||||
|
go func() {
|
||||||
|
log.Printf("Server listening on %s", addr)
|
||||||
|
log.Printf("WebSocket endpoint: ws://%s/ws", addr)
|
||||||
|
log.Printf("Web interface: http://%s/", addr)
|
||||||
|
|
||||||
|
if err := httpServer.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
||||||
|
log.Fatalf("HTTP server error: %v", err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
// Wait for interrupt signal
|
||||||
|
quit := make(chan os.Signal, 1)
|
||||||
|
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
|
||||||
|
<-quit
|
||||||
|
|
||||||
|
log.Println("Shutting down server...")
|
||||||
|
deviceManager.Stop()
|
||||||
|
log.Println("Server stopped")
|
||||||
|
}
|
||||||
@@ -13,7 +13,6 @@ devices:
|
|||||||
tuner_genius:
|
tuner_genius:
|
||||||
host: "10.10.10.129"
|
host: "10.10.10.129"
|
||||||
port: 9010
|
port: 9010
|
||||||
id_number: 1 # Default ID for commands
|
|
||||||
|
|
||||||
antenna_genius:
|
antenna_genius:
|
||||||
host: "10.10.10.130"
|
host: "10.10.10.130"
|
||||||
|
|||||||
5
go.mod
5
go.mod
@@ -2,4 +2,7 @@ module git.rouggy.com/rouggy/ShackMaster
|
|||||||
|
|
||||||
go 1.24.3
|
go 1.24.3
|
||||||
|
|
||||||
require gopkg.in/yaml.v3 v3.0.1 // indirect
|
require (
|
||||||
|
github.com/gorilla/websocket v1.5.3
|
||||||
|
gopkg.in/yaml.v3 v3.0.1
|
||||||
|
)
|
||||||
|
|||||||
3
go.sum
3
go.sum
@@ -1,3 +1,6 @@
|
|||||||
|
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
|
||||||
|
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
|||||||
243
internal/api/device_manager.go
Normal file
243
internal/api/device_manager.go
Normal file
@@ -0,0 +1,243 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"git.rouggy.com/rouggy/ShackMaster/internal/config"
|
||||||
|
"git.rouggy.com/rouggy/ShackMaster/internal/devices/antennagenius"
|
||||||
|
"git.rouggy.com/rouggy/ShackMaster/internal/devices/powergenius"
|
||||||
|
"git.rouggy.com/rouggy/ShackMaster/internal/devices/rotatorgenius"
|
||||||
|
"git.rouggy.com/rouggy/ShackMaster/internal/devices/tunergenius"
|
||||||
|
"git.rouggy.com/rouggy/ShackMaster/internal/devices/webswitch"
|
||||||
|
"git.rouggy.com/rouggy/ShackMaster/internal/services/solar"
|
||||||
|
"git.rouggy.com/rouggy/ShackMaster/internal/services/weather"
|
||||||
|
)
|
||||||
|
|
||||||
|
type DeviceManager struct {
|
||||||
|
config *config.Config
|
||||||
|
|
||||||
|
webSwitch *webswitch.Client
|
||||||
|
powerGenius *powergenius.Client
|
||||||
|
tunerGenius *tunergenius.Client
|
||||||
|
antennaGenius *antennagenius.Client
|
||||||
|
rotatorGenius *rotatorgenius.Client
|
||||||
|
solarClient *solar.Client
|
||||||
|
weatherClient *weather.Client
|
||||||
|
|
||||||
|
hub *Hub
|
||||||
|
statusMu sync.RWMutex
|
||||||
|
lastStatus *SystemStatus
|
||||||
|
|
||||||
|
updateInterval time.Duration
|
||||||
|
stopChan chan struct{}
|
||||||
|
}
|
||||||
|
|
||||||
|
type SystemStatus struct {
|
||||||
|
WebSwitch *webswitch.Status `json:"webswitch"`
|
||||||
|
PowerGenius *powergenius.Status `json:"power_genius"`
|
||||||
|
TunerGenius *tunergenius.Status `json:"tuner_genius"`
|
||||||
|
AntennaGenius *antennagenius.Status `json:"antenna_genius"`
|
||||||
|
RotatorGenius *rotatorgenius.Status `json:"rotator_genius"`
|
||||||
|
Solar *solar.SolarData `json:"solar"`
|
||||||
|
Weather *weather.WeatherData `json:"weather"`
|
||||||
|
Timestamp time.Time `json:"timestamp"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewDeviceManager(cfg *config.Config, hub *Hub) *DeviceManager {
|
||||||
|
return &DeviceManager{
|
||||||
|
config: cfg,
|
||||||
|
hub: hub,
|
||||||
|
updateInterval: 1 * time.Second, // Update status every second
|
||||||
|
stopChan: make(chan struct{}),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (dm *DeviceManager) Initialize() error {
|
||||||
|
log.Println("Initializing device manager...")
|
||||||
|
|
||||||
|
// Initialize WebSwitch
|
||||||
|
dm.webSwitch = webswitch.New(dm.config.Devices.WebSwitch.Host)
|
||||||
|
|
||||||
|
// Initialize Power Genius
|
||||||
|
dm.powerGenius = powergenius.New(
|
||||||
|
dm.config.Devices.PowerGenius.Host,
|
||||||
|
dm.config.Devices.PowerGenius.Port,
|
||||||
|
)
|
||||||
|
|
||||||
|
// Initialize Tuner Genius
|
||||||
|
dm.tunerGenius = tunergenius.New(
|
||||||
|
dm.config.Devices.TunerGenius.Host,
|
||||||
|
dm.config.Devices.TunerGenius.Port,
|
||||||
|
)
|
||||||
|
|
||||||
|
// Initialize Antenna Genius
|
||||||
|
dm.antennaGenius = antennagenius.New(
|
||||||
|
dm.config.Devices.AntennaGenius.Host,
|
||||||
|
dm.config.Devices.AntennaGenius.Port,
|
||||||
|
)
|
||||||
|
|
||||||
|
// Initialize Rotator Genius
|
||||||
|
dm.rotatorGenius = rotatorgenius.New(
|
||||||
|
dm.config.Devices.RotatorGenius.Host,
|
||||||
|
dm.config.Devices.RotatorGenius.Port,
|
||||||
|
)
|
||||||
|
|
||||||
|
// Initialize Solar data client
|
||||||
|
dm.solarClient = solar.New()
|
||||||
|
|
||||||
|
// Initialize Weather client
|
||||||
|
dm.weatherClient = weather.New(
|
||||||
|
dm.config.Weather.OpenWeatherMapAPIKey,
|
||||||
|
dm.config.Location.Latitude,
|
||||||
|
dm.config.Location.Longitude,
|
||||||
|
)
|
||||||
|
|
||||||
|
// Start PowerGenius continuous polling
|
||||||
|
if err := dm.powerGenius.Start(); err != nil {
|
||||||
|
log.Printf("Warning: Failed to start PowerGenius polling: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Println("Device manager initialized")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (dm *DeviceManager) Start() error {
|
||||||
|
log.Println("Starting device monitoring...")
|
||||||
|
go dm.monitorDevices()
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (dm *DeviceManager) Stop() {
|
||||||
|
log.Println("Stopping device manager...")
|
||||||
|
close(dm.stopChan)
|
||||||
|
|
||||||
|
// Close all connections
|
||||||
|
if dm.powerGenius != nil {
|
||||||
|
dm.powerGenius.Close()
|
||||||
|
}
|
||||||
|
if dm.tunerGenius != nil {
|
||||||
|
dm.tunerGenius.Close()
|
||||||
|
}
|
||||||
|
if dm.antennaGenius != nil {
|
||||||
|
dm.antennaGenius.Close()
|
||||||
|
}
|
||||||
|
if dm.rotatorGenius != nil {
|
||||||
|
dm.rotatorGenius.Close()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (dm *DeviceManager) monitorDevices() {
|
||||||
|
ticker := time.NewTicker(dm.updateInterval)
|
||||||
|
defer ticker.Stop()
|
||||||
|
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-ticker.C:
|
||||||
|
dm.updateStatus()
|
||||||
|
case <-dm.stopChan:
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (dm *DeviceManager) updateStatus() {
|
||||||
|
status := &SystemStatus{
|
||||||
|
Timestamp: time.Now(),
|
||||||
|
}
|
||||||
|
|
||||||
|
// Query all devices
|
||||||
|
// WebSwitch - get actual relay states
|
||||||
|
if wsStatus, err := dm.webSwitch.GetStatus(); err == nil {
|
||||||
|
status.WebSwitch = wsStatus
|
||||||
|
} else {
|
||||||
|
log.Printf("WebSwitch error: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Power Genius
|
||||||
|
if pgStatus, err := dm.powerGenius.GetStatus(); err == nil {
|
||||||
|
status.PowerGenius = pgStatus
|
||||||
|
} else {
|
||||||
|
log.Printf("Power Genius error: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// // Tuner Genius
|
||||||
|
// if tgStatus, err := dm.tunerGenius.GetStatus(); err == nil {
|
||||||
|
// status.TunerGenius = tgStatus
|
||||||
|
// } else {
|
||||||
|
// log.Printf("Tuner Genius error: %v", err)
|
||||||
|
// }
|
||||||
|
|
||||||
|
// // Antenna Genius
|
||||||
|
// if agStatus, err := dm.antennaGenius.GetStatus(); err == nil {
|
||||||
|
// status.AntennaGenius = agStatus
|
||||||
|
// } else {
|
||||||
|
// log.Printf("Antenna Genius error: %v", err)
|
||||||
|
// }
|
||||||
|
|
||||||
|
// // Rotator Genius
|
||||||
|
// if rgStatus, err := dm.rotatorGenius.GetStatus(); err == nil {
|
||||||
|
// status.RotatorGenius = rgStatus
|
||||||
|
// } else {
|
||||||
|
// log.Printf("Rotator Genius error: %v", err)
|
||||||
|
// }
|
||||||
|
|
||||||
|
// Solar Data (fetched every 15 minutes, cached)
|
||||||
|
if solarData, err := dm.solarClient.GetSolarData(); err == nil {
|
||||||
|
status.Solar = solarData
|
||||||
|
} else {
|
||||||
|
log.Printf("Solar data error: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Weather Data (fetched every 10 minutes, cached)
|
||||||
|
if weatherData, err := dm.weatherClient.GetWeatherData(); err == nil {
|
||||||
|
status.Weather = weatherData
|
||||||
|
} else {
|
||||||
|
log.Printf("Weather data error: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update cached status
|
||||||
|
dm.statusMu.Lock()
|
||||||
|
dm.lastStatus = status
|
||||||
|
dm.statusMu.Unlock()
|
||||||
|
|
||||||
|
// Broadcast to all connected clients
|
||||||
|
if dm.hub != nil {
|
||||||
|
dm.hub.BroadcastStatusUpdate(status)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (dm *DeviceManager) GetStatus() *SystemStatus {
|
||||||
|
dm.statusMu.RLock()
|
||||||
|
defer dm.statusMu.RUnlock()
|
||||||
|
|
||||||
|
if dm.lastStatus == nil {
|
||||||
|
return &SystemStatus{
|
||||||
|
Timestamp: time.Now(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return dm.lastStatus
|
||||||
|
}
|
||||||
|
|
||||||
|
// Device control methods
|
||||||
|
func (dm *DeviceManager) WebSwitch() *webswitch.Client {
|
||||||
|
return dm.webSwitch
|
||||||
|
}
|
||||||
|
|
||||||
|
func (dm *DeviceManager) PowerGenius() *powergenius.Client {
|
||||||
|
return dm.powerGenius
|
||||||
|
}
|
||||||
|
|
||||||
|
func (dm *DeviceManager) TunerGenius() *tunergenius.Client {
|
||||||
|
return dm.tunerGenius
|
||||||
|
}
|
||||||
|
|
||||||
|
func (dm *DeviceManager) AntennaGenius() *antennagenius.Client {
|
||||||
|
return dm.antennaGenius
|
||||||
|
}
|
||||||
|
|
||||||
|
func (dm *DeviceManager) RotatorGenius() *rotatorgenius.Client {
|
||||||
|
return dm.rotatorGenius
|
||||||
|
}
|
||||||
373
internal/api/handlers.go
Normal file
373
internal/api/handlers.go
Normal file
@@ -0,0 +1,373 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"git.rouggy.com/rouggy/ShackMaster/internal/config"
|
||||||
|
"github.com/gorilla/websocket"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Server struct {
|
||||||
|
deviceManager *DeviceManager
|
||||||
|
hub *Hub
|
||||||
|
config *config.Config
|
||||||
|
upgrader websocket.Upgrader
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewServer(dm *DeviceManager, hub *Hub, cfg *config.Config) *Server {
|
||||||
|
return &Server{
|
||||||
|
deviceManager: dm,
|
||||||
|
hub: hub,
|
||||||
|
config: cfg,
|
||||||
|
upgrader: websocket.Upgrader{
|
||||||
|
ReadBufferSize: 1024,
|
||||||
|
WriteBufferSize: 1024,
|
||||||
|
CheckOrigin: func(r *http.Request) bool {
|
||||||
|
return true // Allow all origins for now
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) SetupRoutes() *http.ServeMux {
|
||||||
|
mux := http.NewServeMux()
|
||||||
|
|
||||||
|
// WebSocket endpoint
|
||||||
|
mux.HandleFunc("/ws", s.handleWebSocket)
|
||||||
|
|
||||||
|
// REST API endpoints
|
||||||
|
mux.HandleFunc("/api/status", s.handleGetStatus)
|
||||||
|
mux.HandleFunc("/api/config", s.handleGetConfig)
|
||||||
|
|
||||||
|
// WebSwitch endpoints
|
||||||
|
mux.HandleFunc("/api/webswitch/relay/on", s.handleWebSwitchRelayOn)
|
||||||
|
mux.HandleFunc("/api/webswitch/relay/off", s.handleWebSwitchRelayOff)
|
||||||
|
mux.HandleFunc("/api/webswitch/all/on", s.handleWebSwitchAllOn)
|
||||||
|
mux.HandleFunc("/api/webswitch/all/off", s.handleWebSwitchAllOff)
|
||||||
|
|
||||||
|
// Rotator endpoints
|
||||||
|
mux.HandleFunc("/api/rotator/move", s.handleRotatorMove)
|
||||||
|
mux.HandleFunc("/api/rotator/cw", s.handleRotatorCW)
|
||||||
|
mux.HandleFunc("/api/rotator/ccw", s.handleRotatorCCW)
|
||||||
|
mux.HandleFunc("/api/rotator/stop", s.handleRotatorStop)
|
||||||
|
|
||||||
|
// Tuner endpoints
|
||||||
|
mux.HandleFunc("/api/tuner/operate", s.handleTunerOperate)
|
||||||
|
mux.HandleFunc("/api/tuner/tune", s.handleTunerAutoTune)
|
||||||
|
mux.HandleFunc("/api/tuner/antenna", s.handleTunerAntenna)
|
||||||
|
|
||||||
|
// Antenna Genius endpoints
|
||||||
|
mux.HandleFunc("/api/antenna/set", s.handleAntennaSet)
|
||||||
|
|
||||||
|
// Power Genius endpoints
|
||||||
|
mux.HandleFunc("/api/power/fanmode", s.handlePowerFanMode)
|
||||||
|
|
||||||
|
// Static files (will be frontend)
|
||||||
|
mux.Handle("/", http.FileServer(http.Dir("./web/dist")))
|
||||||
|
|
||||||
|
return mux
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) handleWebSocket(w http.ResponseWriter, r *http.Request) {
|
||||||
|
conn, err := s.upgrader.Upgrade(w, r, nil)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("WebSocket upgrade error: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
ServeWs(s.hub, conn)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) handleGetStatus(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.Method != http.MethodGet {
|
||||||
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
status := s.deviceManager.GetStatus()
|
||||||
|
s.sendJSON(w, status)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) handleGetConfig(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.Method != http.MethodGet {
|
||||||
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Only send public config info (not API keys)
|
||||||
|
configInfo := map[string]interface{}{
|
||||||
|
"callsign": s.config.Location.Callsign,
|
||||||
|
"location": map[string]float64{
|
||||||
|
"latitude": s.config.Location.Latitude,
|
||||||
|
"longitude": s.config.Location.Longitude,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
s.sendJSON(w, configInfo)
|
||||||
|
}
|
||||||
|
|
||||||
|
// WebSwitch handlers
|
||||||
|
func (s *Server) handleWebSwitchRelayOn(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.Method != http.MethodPost {
|
||||||
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
relay, err := strconv.Atoi(r.URL.Query().Get("relay"))
|
||||||
|
if err != nil || relay < 1 || relay > 5 {
|
||||||
|
http.Error(w, "Invalid relay number", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := s.deviceManager.WebSwitch().TurnOn(relay); err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
s.sendJSON(w, map[string]string{"status": "ok"})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) handleWebSwitchRelayOff(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.Method != http.MethodPost {
|
||||||
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
relay, err := strconv.Atoi(r.URL.Query().Get("relay"))
|
||||||
|
if err != nil || relay < 1 || relay > 5 {
|
||||||
|
http.Error(w, "Invalid relay number", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := s.deviceManager.WebSwitch().TurnOff(relay); err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
s.sendJSON(w, map[string]string{"status": "ok"})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) handleWebSwitchAllOn(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.Method != http.MethodPost {
|
||||||
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := s.deviceManager.WebSwitch().AllOn(); err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
s.sendJSON(w, map[string]string{"status": "ok"})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) handleWebSwitchAllOff(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.Method != http.MethodPost {
|
||||||
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := s.deviceManager.WebSwitch().AllOff(); err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
s.sendJSON(w, map[string]string{"status": "ok"})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Rotator handlers
|
||||||
|
func (s *Server) handleRotatorMove(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.Method != http.MethodPost {
|
||||||
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var req struct {
|
||||||
|
Rotator int `json:"rotator"`
|
||||||
|
Azimuth int `json:"azimuth"`
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||||
|
http.Error(w, "Invalid request body", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := s.deviceManager.RotatorGenius().MoveToAzimuth(req.Rotator, req.Azimuth); err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
s.sendJSON(w, map[string]string{"status": "ok"})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) handleRotatorCW(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.Method != http.MethodPost {
|
||||||
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
rotator, err := strconv.Atoi(r.URL.Query().Get("rotator"))
|
||||||
|
if err != nil || rotator < 1 || rotator > 2 {
|
||||||
|
http.Error(w, "Invalid rotator number", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := s.deviceManager.RotatorGenius().RotateCW(rotator); err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
s.sendJSON(w, map[string]string{"status": "ok"})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) handleRotatorCCW(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.Method != http.MethodPost {
|
||||||
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
rotator, err := strconv.Atoi(r.URL.Query().Get("rotator"))
|
||||||
|
if err != nil || rotator < 1 || rotator > 2 {
|
||||||
|
http.Error(w, "Invalid rotator number", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := s.deviceManager.RotatorGenius().RotateCCW(rotator); err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
s.sendJSON(w, map[string]string{"status": "ok"})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) handleRotatorStop(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.Method != http.MethodPost {
|
||||||
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := s.deviceManager.RotatorGenius().Stop(); err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
s.sendJSON(w, map[string]string{"status": "ok"})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Tuner handlers
|
||||||
|
func (s *Server) handleTunerOperate(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.Method != http.MethodPost {
|
||||||
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var req struct {
|
||||||
|
Operate bool `json:"operate"`
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||||
|
http.Error(w, "Invalid request body", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := s.deviceManager.TunerGenius().SetOperate(req.Operate); err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
s.sendJSON(w, map[string]string{"status": "ok"})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) handleTunerAutoTune(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.Method != http.MethodPost {
|
||||||
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := s.deviceManager.TunerGenius().AutoTune(); err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
s.sendJSON(w, map[string]string{"status": "ok"})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) handleTunerAntenna(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.Method != http.MethodPost {
|
||||||
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var req struct {
|
||||||
|
Antenna int `json:"antenna"`
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||||
|
http.Error(w, "Invalid request body", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := s.deviceManager.TunerGenius().ActivateAntenna(req.Antenna); err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
s.sendJSON(w, map[string]string{"status": "ok"})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Antenna Genius handlers
|
||||||
|
func (s *Server) handleAntennaSet(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.Method != http.MethodPost {
|
||||||
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var req struct {
|
||||||
|
Radio int `json:"radio"`
|
||||||
|
Antenna int `json:"antenna"`
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||||
|
http.Error(w, "Invalid request body", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := s.deviceManager.AntennaGenius().SetRadioAntenna(req.Radio, req.Antenna); err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
s.sendJSON(w, map[string]string{"status": "ok"})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Power Genius handlers
|
||||||
|
func (s *Server) handlePowerFanMode(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.Method != http.MethodPost {
|
||||||
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var req struct {
|
||||||
|
Mode string `json:"mode"`
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||||
|
http.Error(w, "Invalid request body", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := s.deviceManager.PowerGenius().SetFanMode(req.Mode); err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
s.sendJSON(w, map[string]string{"status": "ok"})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) sendJSON(w http.ResponseWriter, data interface{}) {
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
json.NewEncoder(w).Encode(data)
|
||||||
|
}
|
||||||
193
internal/api/websocket.go
Normal file
193
internal/api/websocket.go
Normal file
@@ -0,0 +1,193 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"log"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"git.rouggy.com/rouggy/ShackMaster/pkg/protocol"
|
||||||
|
"github.com/gorilla/websocket"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Hub struct {
|
||||||
|
clients map[*Client]bool
|
||||||
|
broadcast chan *protocol.WebSocketMessage
|
||||||
|
register chan *Client
|
||||||
|
unregister chan *Client
|
||||||
|
mu sync.RWMutex
|
||||||
|
}
|
||||||
|
|
||||||
|
type Client struct {
|
||||||
|
hub *Hub
|
||||||
|
conn *websocket.Conn
|
||||||
|
send chan *protocol.WebSocketMessage
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewHub() *Hub {
|
||||||
|
return &Hub{
|
||||||
|
clients: make(map[*Client]bool),
|
||||||
|
broadcast: make(chan *protocol.WebSocketMessage, 256),
|
||||||
|
register: make(chan *Client),
|
||||||
|
unregister: make(chan *Client),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Hub) Run() {
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case client := <-h.register:
|
||||||
|
h.mu.Lock()
|
||||||
|
h.clients[client] = true
|
||||||
|
h.mu.Unlock()
|
||||||
|
log.Printf("Client connected, total: %d", len(h.clients))
|
||||||
|
|
||||||
|
case client := <-h.unregister:
|
||||||
|
h.mu.Lock()
|
||||||
|
if _, ok := h.clients[client]; ok {
|
||||||
|
delete(h.clients, client)
|
||||||
|
close(client.send)
|
||||||
|
}
|
||||||
|
h.mu.Unlock()
|
||||||
|
log.Printf("Client disconnected, total: %d", len(h.clients))
|
||||||
|
|
||||||
|
case message := <-h.broadcast:
|
||||||
|
h.mu.RLock()
|
||||||
|
for client := range h.clients {
|
||||||
|
select {
|
||||||
|
case client.send <- message:
|
||||||
|
default:
|
||||||
|
// Client's send buffer is full, close it
|
||||||
|
h.mu.RUnlock()
|
||||||
|
h.unregister <- client
|
||||||
|
h.mu.RLock()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
h.mu.RUnlock()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Hub) Broadcast(msg *protocol.WebSocketMessage) {
|
||||||
|
h.broadcast <- msg
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Hub) ClientCount() int {
|
||||||
|
h.mu.RLock()
|
||||||
|
defer h.mu.RUnlock()
|
||||||
|
return len(h.clients)
|
||||||
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
writeWait = 10 * time.Second
|
||||||
|
pongWait = 60 * time.Second
|
||||||
|
pingPeriod = (pongWait * 9) / 10
|
||||||
|
maxMessageSize = 512 * 1024 // 512KB
|
||||||
|
)
|
||||||
|
|
||||||
|
func (c *Client) readPump() {
|
||||||
|
defer func() {
|
||||||
|
c.hub.unregister <- c
|
||||||
|
c.conn.Close()
|
||||||
|
}()
|
||||||
|
|
||||||
|
c.conn.SetReadDeadline(time.Now().Add(pongWait))
|
||||||
|
c.conn.SetReadLimit(maxMessageSize)
|
||||||
|
c.conn.SetPongHandler(func(string) error {
|
||||||
|
c.conn.SetReadDeadline(time.Now().Add(pongWait))
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
|
||||||
|
for {
|
||||||
|
var msg protocol.WebSocketMessage
|
||||||
|
err := c.conn.ReadJSON(&msg)
|
||||||
|
if err != nil {
|
||||||
|
if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway, websocket.CloseAbnormalClosure) {
|
||||||
|
log.Printf("WebSocket error: %v", err)
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle incoming commands from client
|
||||||
|
log.Printf("Received message: type=%s, device=%s", msg.Type, msg.Device)
|
||||||
|
|
||||||
|
// Commands are handled via REST API, not WebSocket
|
||||||
|
// WebSocket is primarily for server -> client updates
|
||||||
|
// Client should use REST endpoints for commands
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) writePump() {
|
||||||
|
ticker := time.NewTicker(pingPeriod)
|
||||||
|
defer func() {
|
||||||
|
ticker.Stop()
|
||||||
|
c.conn.Close()
|
||||||
|
}()
|
||||||
|
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case message, ok := <-c.send:
|
||||||
|
c.conn.SetWriteDeadline(time.Now().Add(writeWait))
|
||||||
|
if !ok {
|
||||||
|
// Hub closed the channel
|
||||||
|
c.conn.WriteMessage(websocket.CloseMessage, []byte{})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := c.conn.WriteJSON(message); err != nil {
|
||||||
|
log.Printf("Error writing message: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
case <-ticker.C:
|
||||||
|
c.conn.SetWriteDeadline(time.Now().Add(writeWait))
|
||||||
|
if err := c.conn.WriteMessage(websocket.PingMessage, nil); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func ServeWs(hub *Hub, conn *websocket.Conn) {
|
||||||
|
client := &Client{
|
||||||
|
hub: hub,
|
||||||
|
conn: conn,
|
||||||
|
send: make(chan *protocol.WebSocketMessage, 256),
|
||||||
|
}
|
||||||
|
|
||||||
|
client.hub.register <- client
|
||||||
|
|
||||||
|
// Send initial status
|
||||||
|
go func() {
|
||||||
|
time.Sleep(100 * time.Millisecond)
|
||||||
|
client.send <- &protocol.WebSocketMessage{
|
||||||
|
Type: protocol.MsgTypeStatus,
|
||||||
|
Data: map[string]string{"status": "connected"},
|
||||||
|
Timestamp: time.Now(),
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
go client.writePump()
|
||||||
|
go client.readPump()
|
||||||
|
}
|
||||||
|
|
||||||
|
// BroadcastStatusUpdate sends a status update to all connected clients
|
||||||
|
func (h *Hub) BroadcastStatusUpdate(status interface{}) {
|
||||||
|
data, err := json.Marshal(status)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Error marshaling status: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var statusMap map[string]interface{}
|
||||||
|
if err := json.Unmarshal(data, &statusMap); err != nil {
|
||||||
|
log.Printf("Error unmarshaling status: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
h.Broadcast(&protocol.WebSocketMessage{
|
||||||
|
Type: protocol.MsgTypeUpdate,
|
||||||
|
Data: statusMap,
|
||||||
|
Timestamp: time.Now(),
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -37,9 +37,8 @@ type PowerGeniusConfig struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type TunerGeniusConfig struct {
|
type TunerGeniusConfig struct {
|
||||||
Host string `yaml:"host"`
|
Host string `yaml:"host"`
|
||||||
Port int `yaml:"port"`
|
Port int `yaml:"port"`
|
||||||
IDNumber int `yaml:"id_number"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type AntennaGeniusConfig struct {
|
type AntennaGeniusConfig struct {
|
||||||
|
|||||||
159
internal/devices/antennagenius/antennagenius.go
Normal file
159
internal/devices/antennagenius/antennagenius.go
Normal file
@@ -0,0 +1,159 @@
|
|||||||
|
package antennagenius
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"fmt"
|
||||||
|
"net"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
. "git.rouggy.com/rouggy/ShackMaster/internal/devices"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Client struct {
|
||||||
|
host string
|
||||||
|
port int
|
||||||
|
conn net.Conn
|
||||||
|
}
|
||||||
|
|
||||||
|
type Status struct {
|
||||||
|
Radio1Antenna int `json:"radio1_antenna"` // 0-7 (antenna index)
|
||||||
|
Radio2Antenna int `json:"radio2_antenna"` // 0-7 (antenna index)
|
||||||
|
Connected bool `json:"connected"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func New(host string, port int) *Client {
|
||||||
|
return &Client{
|
||||||
|
host: host,
|
||||||
|
port: port,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) Connect() error {
|
||||||
|
conn, err := net.DialTimeout("tcp", fmt.Sprintf("%s:%d", c.host, c.port), 5*time.Second)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to connect: %w", err)
|
||||||
|
}
|
||||||
|
c.conn = conn
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) Close() error {
|
||||||
|
if c.conn != nil {
|
||||||
|
return c.conn.Close()
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) sendCommand(cmd string) (string, error) {
|
||||||
|
if c.conn == nil {
|
||||||
|
if err := c.Connect(); err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get next command ID from global counter
|
||||||
|
cmdID := GetGlobalCommandID().GetNextID()
|
||||||
|
|
||||||
|
// Format command with ID: C<id>|<command>
|
||||||
|
fullCmd := fmt.Sprintf("C%d|%s\n", cmdID, cmd)
|
||||||
|
|
||||||
|
// Send command
|
||||||
|
_, err := c.conn.Write([]byte(fullCmd))
|
||||||
|
if err != nil {
|
||||||
|
c.conn = nil
|
||||||
|
return "", fmt.Errorf("failed to send command: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read response
|
||||||
|
reader := bufio.NewReader(c.conn)
|
||||||
|
response, err := reader.ReadString('\n')
|
||||||
|
if err != nil {
|
||||||
|
c.conn = nil
|
||||||
|
return "", fmt.Errorf("failed to read response: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return strings.TrimSpace(response), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) GetStatus() (*Status, error) {
|
||||||
|
resp, err := c.sendCommand("status")
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.parseStatus(resp)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) parseStatus(resp string) (*Status, error) {
|
||||||
|
status := &Status{
|
||||||
|
Connected: true,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse response format from 4O3A API
|
||||||
|
// Expected format will vary - this is a basic parser
|
||||||
|
pairs := strings.Fields(resp)
|
||||||
|
|
||||||
|
for _, pair := range pairs {
|
||||||
|
parts := strings.SplitN(pair, "=", 2)
|
||||||
|
if len(parts) != 2 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
key := parts[0]
|
||||||
|
value := parts[1]
|
||||||
|
|
||||||
|
switch key {
|
||||||
|
case "radio1", "r1":
|
||||||
|
status.Radio1Antenna, _ = strconv.Atoi(value)
|
||||||
|
case "radio2", "r2":
|
||||||
|
status.Radio2Antenna, _ = strconv.Atoi(value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return status, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetRadioAntenna sets which antenna a radio should use
|
||||||
|
// radio: 1 or 2
|
||||||
|
// antenna: 0-7 (antenna index)
|
||||||
|
func (c *Client) SetRadioAntenna(radio int, antenna int) error {
|
||||||
|
if radio < 1 || radio > 2 {
|
||||||
|
return fmt.Errorf("radio must be 1 or 2")
|
||||||
|
}
|
||||||
|
if antenna < 0 || antenna > 7 {
|
||||||
|
return fmt.Errorf("antenna must be between 0 and 7")
|
||||||
|
}
|
||||||
|
|
||||||
|
cmd := fmt.Sprintf("set radio%d=%d", radio, antenna)
|
||||||
|
resp, err := c.sendCommand(cmd)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check response for success
|
||||||
|
if !strings.Contains(strings.ToLower(resp), "ok") && resp != "" {
|
||||||
|
// If response doesn't contain "ok" but isn't empty, assume success
|
||||||
|
// (some devices may return the new state instead of "ok")
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetRadioAntenna gets which antenna a radio is currently using
|
||||||
|
func (c *Client) GetRadioAntenna(radio int) (int, error) {
|
||||||
|
if radio < 1 || radio > 2 {
|
||||||
|
return -1, fmt.Errorf("radio must be 1 or 2")
|
||||||
|
}
|
||||||
|
|
||||||
|
status, err := c.GetStatus()
|
||||||
|
if err != nil {
|
||||||
|
return -1, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if radio == 1 {
|
||||||
|
return status.Radio1Antenna, nil
|
||||||
|
}
|
||||||
|
return status.Radio2Antenna, nil
|
||||||
|
}
|
||||||
35
internal/devices/command_id.go
Normal file
35
internal/devices/command_id.go
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
package devices
|
||||||
|
|
||||||
|
import "sync"
|
||||||
|
|
||||||
|
// CommandIDManager manages the global command ID counter for 4O3A devices
|
||||||
|
// All 4O3A devices share the same command ID sequence
|
||||||
|
type CommandIDManager struct {
|
||||||
|
mu sync.Mutex
|
||||||
|
counter int
|
||||||
|
}
|
||||||
|
|
||||||
|
var globalCommandID = &CommandIDManager{
|
||||||
|
counter: 0,
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetNextID returns the next command ID and increments the counter
|
||||||
|
func (m *CommandIDManager) GetNextID() int {
|
||||||
|
m.mu.Lock()
|
||||||
|
defer m.mu.Unlock()
|
||||||
|
|
||||||
|
m.counter++
|
||||||
|
return m.counter
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reset resets the counter to 0 (useful for testing or restart)
|
||||||
|
func (m *CommandIDManager) Reset() {
|
||||||
|
m.mu.Lock()
|
||||||
|
defer m.mu.Unlock()
|
||||||
|
m.counter = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetGlobalCommandID returns the global command ID manager
|
||||||
|
func GetGlobalCommandID() *CommandIDManager {
|
||||||
|
return globalCommandID
|
||||||
|
}
|
||||||
380
internal/devices/powergenius/powergenius.go
Normal file
380
internal/devices/powergenius/powergenius.go
Normal file
@@ -0,0 +1,380 @@
|
|||||||
|
package powergenius
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"math"
|
||||||
|
"net"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
. "git.rouggy.com/rouggy/ShackMaster/internal/devices"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Client struct {
|
||||||
|
host string
|
||||||
|
port int
|
||||||
|
conn net.Conn
|
||||||
|
connMu sync.Mutex
|
||||||
|
lastStatus *Status
|
||||||
|
statusMu sync.RWMutex
|
||||||
|
stopChan chan struct{}
|
||||||
|
running bool
|
||||||
|
}
|
||||||
|
|
||||||
|
type Status struct {
|
||||||
|
PowerForward float64 `json:"power_forward"`
|
||||||
|
PowerReflected float64 `json:"power_reflected"`
|
||||||
|
SWR float64 `json:"swr"`
|
||||||
|
Voltage float64 `json:"voltage"`
|
||||||
|
VDD float64 `json:"vdd"`
|
||||||
|
Current float64 `json:"current"`
|
||||||
|
PeakCurrent float64 `json:"peak_current"`
|
||||||
|
Temperature float64 `json:"temperature"`
|
||||||
|
HarmonicLoadTemp float64 `json:"harmonic_load_temp"`
|
||||||
|
FanSpeed int `json:"fan_speed"`
|
||||||
|
FanMode string `json:"fan_mode"`
|
||||||
|
State string `json:"state"`
|
||||||
|
BandA string `json:"band_a"`
|
||||||
|
BandB string `json:"band_b"`
|
||||||
|
FaultPresent bool `json:"fault_present"`
|
||||||
|
Connected bool `json:"connected"`
|
||||||
|
Meffa string `json:"meffa"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func New(host string, port int) *Client {
|
||||||
|
return &Client{
|
||||||
|
host: host,
|
||||||
|
port: port,
|
||||||
|
stopChan: make(chan struct{}),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) Connect() error {
|
||||||
|
c.connMu.Lock()
|
||||||
|
defer c.connMu.Unlock()
|
||||||
|
|
||||||
|
if c.conn != nil {
|
||||||
|
return nil // Already connected
|
||||||
|
}
|
||||||
|
|
||||||
|
conn, err := net.DialTimeout("tcp", fmt.Sprintf("%s:%d", c.host, c.port), 5*time.Second)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to connect: %w", err)
|
||||||
|
}
|
||||||
|
c.conn = conn
|
||||||
|
|
||||||
|
// Read and discard version banner
|
||||||
|
reader := bufio.NewReader(c.conn)
|
||||||
|
_, _ = reader.ReadString('\n')
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) Close() error {
|
||||||
|
c.connMu.Lock()
|
||||||
|
defer c.connMu.Unlock()
|
||||||
|
|
||||||
|
if c.stopChan != nil {
|
||||||
|
close(c.stopChan)
|
||||||
|
}
|
||||||
|
|
||||||
|
if c.conn != nil {
|
||||||
|
return c.conn.Close()
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Start begins continuous polling of the device
|
||||||
|
func (c *Client) Start() error {
|
||||||
|
if err := c.Connect(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if c.running {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
c.running = true
|
||||||
|
go c.pollLoop()
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// pollLoop continuously polls the device for status
|
||||||
|
func (c *Client) pollLoop() {
|
||||||
|
ticker := time.NewTicker(150 * time.Millisecond)
|
||||||
|
defer ticker.Stop()
|
||||||
|
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-ticker.C:
|
||||||
|
status, err := c.queryStatus()
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("PowerGenius query error: %v", err)
|
||||||
|
// Try to reconnect
|
||||||
|
c.connMu.Lock()
|
||||||
|
if c.conn != nil {
|
||||||
|
c.conn.Close()
|
||||||
|
c.conn = nil
|
||||||
|
}
|
||||||
|
c.connMu.Unlock()
|
||||||
|
|
||||||
|
if err := c.Connect(); err != nil {
|
||||||
|
log.Printf("PowerGenius reconnect failed: %v", err)
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Merge with existing status (spontaneous messages may only update some fields)
|
||||||
|
c.statusMu.Lock()
|
||||||
|
if c.lastStatus != nil {
|
||||||
|
// Keep existing values for fields not in the new status
|
||||||
|
if status.PowerForward == 0 && c.lastStatus.PowerForward != 0 {
|
||||||
|
status.PowerForward = c.lastStatus.PowerForward
|
||||||
|
}
|
||||||
|
if status.Temperature == 0 && c.lastStatus.Temperature != 0 {
|
||||||
|
status.Temperature = c.lastStatus.Temperature
|
||||||
|
}
|
||||||
|
if status.HarmonicLoadTemp == 0 && c.lastStatus.HarmonicLoadTemp != 0 {
|
||||||
|
status.HarmonicLoadTemp = c.lastStatus.HarmonicLoadTemp
|
||||||
|
}
|
||||||
|
if status.Voltage == 0 && c.lastStatus.Voltage != 0 {
|
||||||
|
status.Voltage = c.lastStatus.Voltage
|
||||||
|
}
|
||||||
|
if status.FanMode == "" && c.lastStatus.FanMode != "" {
|
||||||
|
status.FanMode = c.lastStatus.FanMode
|
||||||
|
}
|
||||||
|
if status.BandA == "" && c.lastStatus.BandA != "" {
|
||||||
|
status.BandA = c.lastStatus.BandA
|
||||||
|
}
|
||||||
|
if status.BandB == "" && c.lastStatus.BandB != "" {
|
||||||
|
status.BandB = c.lastStatus.BandB
|
||||||
|
}
|
||||||
|
}
|
||||||
|
c.lastStatus = status
|
||||||
|
c.statusMu.Unlock()
|
||||||
|
|
||||||
|
case <-c.stopChan:
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) queryStatus() (*Status, error) {
|
||||||
|
c.connMu.Lock()
|
||||||
|
defer c.connMu.Unlock()
|
||||||
|
|
||||||
|
if c.conn == nil {
|
||||||
|
return nil, fmt.Errorf("not connected")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get next command ID from global counter
|
||||||
|
cmdID := GetGlobalCommandID().GetNextID()
|
||||||
|
|
||||||
|
// Format command with ID: C<id>|status
|
||||||
|
fullCmd := fmt.Sprintf("C%d|status\n", cmdID)
|
||||||
|
|
||||||
|
// Send command
|
||||||
|
_, err := c.conn.Write([]byte(fullCmd))
|
||||||
|
if err != nil {
|
||||||
|
c.conn = nil
|
||||||
|
return nil, fmt.Errorf("failed to send command: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read response
|
||||||
|
reader := bufio.NewReader(c.conn)
|
||||||
|
response, err := reader.ReadString('\n')
|
||||||
|
if err != nil {
|
||||||
|
c.conn = nil
|
||||||
|
return nil, fmt.Errorf("failed to read response: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.parseStatus(strings.TrimSpace(response))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) sendCommand(cmd string) (string, error) {
|
||||||
|
c.connMu.Lock()
|
||||||
|
defer c.connMu.Unlock()
|
||||||
|
|
||||||
|
if c.conn == nil {
|
||||||
|
return "", fmt.Errorf("not connected")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get next command ID from global counter
|
||||||
|
cmdID := GetGlobalCommandID().GetNextID()
|
||||||
|
|
||||||
|
// Format command with ID: C<id>|<command>
|
||||||
|
fullCmd := fmt.Sprintf("C%d|%s\n", cmdID, cmd)
|
||||||
|
|
||||||
|
// Send command
|
||||||
|
_, err := c.conn.Write([]byte(fullCmd))
|
||||||
|
if err != nil {
|
||||||
|
c.conn = nil
|
||||||
|
return "", fmt.Errorf("failed to send command: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read response
|
||||||
|
reader := bufio.NewReader(c.conn)
|
||||||
|
response, err := reader.ReadString('\n')
|
||||||
|
if err != nil {
|
||||||
|
c.conn = nil
|
||||||
|
return "", fmt.Errorf("failed to read response: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return strings.TrimSpace(response), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) GetStatus() (*Status, error) {
|
||||||
|
c.statusMu.RLock()
|
||||||
|
defer c.statusMu.RUnlock()
|
||||||
|
|
||||||
|
if c.lastStatus == nil {
|
||||||
|
return &Status{Connected: false}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.lastStatus, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) parseStatus(resp string) (*Status, error) {
|
||||||
|
status := &Status{
|
||||||
|
Connected: true,
|
||||||
|
}
|
||||||
|
|
||||||
|
var data string
|
||||||
|
|
||||||
|
// Handle two message formats:
|
||||||
|
// 1. Response to command: R<id>|0|state=IDLE bandA=40 ...
|
||||||
|
// 2. Spontaneous status: S0|state=TRANSMIT_A
|
||||||
|
|
||||||
|
if strings.HasPrefix(resp, "R") {
|
||||||
|
// Response format: R<id>|0|state=IDLE bandA=40 ...
|
||||||
|
parts := strings.SplitN(resp, "|", 3)
|
||||||
|
if len(parts) < 3 {
|
||||||
|
return nil, fmt.Errorf("invalid response format: %s", resp)
|
||||||
|
}
|
||||||
|
data = parts[2] // Get everything after "R<id>|0|"
|
||||||
|
|
||||||
|
} else if strings.HasPrefix(resp, "S") {
|
||||||
|
// Spontaneous message format: S0|state=TRANSMIT_A
|
||||||
|
parts := strings.SplitN(resp, "|", 2)
|
||||||
|
if len(parts) < 2 {
|
||||||
|
return nil, fmt.Errorf("invalid spontaneous message format: %s", resp)
|
||||||
|
}
|
||||||
|
data = parts[1] // Get everything after "S0|"
|
||||||
|
|
||||||
|
} else {
|
||||||
|
return nil, fmt.Errorf("unknown message format: %s", resp)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse key=value pairs separated by spaces
|
||||||
|
pairs := strings.Fields(data)
|
||||||
|
|
||||||
|
for _, pair := range pairs {
|
||||||
|
kv := strings.SplitN(pair, "=", 2)
|
||||||
|
if len(kv) != 2 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
key := kv[0]
|
||||||
|
value := kv[1]
|
||||||
|
|
||||||
|
switch key {
|
||||||
|
case "fwd":
|
||||||
|
// fwd is in dBm (e.g., 56.5 dBm = 446W)
|
||||||
|
// Formula: watts = 10^(dBm/10) / 1000
|
||||||
|
if dBm, err := strconv.ParseFloat(value, 64); err == nil {
|
||||||
|
milliwatts := math.Pow(10, dBm/10.0)
|
||||||
|
status.PowerForward = milliwatts / 1000.0
|
||||||
|
}
|
||||||
|
case "peakfwd":
|
||||||
|
// Peak forward power
|
||||||
|
case "swr":
|
||||||
|
// SWR from return loss
|
||||||
|
// Formula: returnLoss = abs(swr) / 20
|
||||||
|
// swr = (10^returnLoss + 1) / (10^returnLoss - 1)
|
||||||
|
if swrRaw, err := strconv.ParseFloat(value, 64); err == nil {
|
||||||
|
returnLoss := math.Abs(swrRaw) / 20.0
|
||||||
|
tenPowRL := math.Pow(10, returnLoss)
|
||||||
|
calculatedSWR := (tenPowRL + 1) / (tenPowRL - 1)
|
||||||
|
status.SWR = calculatedSWR
|
||||||
|
}
|
||||||
|
case "vac":
|
||||||
|
status.Voltage, _ = strconv.ParseFloat(value, 64)
|
||||||
|
case "meffa":
|
||||||
|
status.Meffa = value
|
||||||
|
case "vdd":
|
||||||
|
status.VDD, _ = strconv.ParseFloat(value, 64)
|
||||||
|
case "id":
|
||||||
|
status.Current, _ = strconv.ParseFloat(value, 64)
|
||||||
|
case "peakid":
|
||||||
|
status.PeakCurrent, _ = strconv.ParseFloat(value, 64)
|
||||||
|
case "temp":
|
||||||
|
status.Temperature, _ = strconv.ParseFloat(value, 64)
|
||||||
|
case "hltemp":
|
||||||
|
status.HarmonicLoadTemp, _ = strconv.ParseFloat(value, 64)
|
||||||
|
case "bandA":
|
||||||
|
if band, err := strconv.Atoi(value); err == nil {
|
||||||
|
status.BandA = fmt.Sprintf("%dM", band)
|
||||||
|
}
|
||||||
|
case "bandB":
|
||||||
|
if band, err := strconv.Atoi(value); err == nil && band > 0 {
|
||||||
|
status.BandB = fmt.Sprintf("%dM", band)
|
||||||
|
}
|
||||||
|
case "fanmode":
|
||||||
|
status.FanMode = value
|
||||||
|
case "state":
|
||||||
|
status.State = value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return status, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Subscribe starts receiving periodic status updates
|
||||||
|
func (c *Client) Subscribe() error {
|
||||||
|
_, err := c.sendCommand("sub status")
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unsubscribe stops receiving periodic status updates
|
||||||
|
func (c *Client) Unsubscribe() error {
|
||||||
|
_, err := c.sendCommand("unsub status")
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// ReadUpdate reads a status update (when subscribed)
|
||||||
|
func (c *Client) ReadUpdate() (*Status, error) {
|
||||||
|
if c.conn == nil {
|
||||||
|
return nil, fmt.Errorf("not connected")
|
||||||
|
}
|
||||||
|
|
||||||
|
reader := bufio.NewReader(c.conn)
|
||||||
|
response, err := reader.ReadString('\n')
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to read update: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.parseStatus(strings.TrimSpace(response))
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetFanMode sets the fan mode
|
||||||
|
// mode can be: STANDARD, CONTEST, or BROADCAST
|
||||||
|
func (c *Client) SetFanMode(mode string) error {
|
||||||
|
validModes := map[string]bool{
|
||||||
|
"STANDARD": true,
|
||||||
|
"CONTEST": true,
|
||||||
|
"BROADCAST": true,
|
||||||
|
}
|
||||||
|
|
||||||
|
if !validModes[mode] {
|
||||||
|
return fmt.Errorf("invalid fan mode: %s (must be STANDARD, CONTEST, or BROADCAST)", mode)
|
||||||
|
}
|
||||||
|
|
||||||
|
cmd := fmt.Sprintf("setup fanmode=%s", mode)
|
||||||
|
_, err := c.sendCommand(cmd)
|
||||||
|
return err
|
||||||
|
}
|
||||||
@@ -7,6 +7,8 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
. "git.rouggy.com/rouggy/ShackMaster/internal/devices"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Client struct {
|
type Client struct {
|
||||||
@@ -65,8 +67,14 @@ func (c *Client) sendCommand(cmd string) (string, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Get next command ID from global counter
|
||||||
|
cmdID := GetGlobalCommandID().GetNextID()
|
||||||
|
|
||||||
|
// Format command with ID: C<id>|<command>
|
||||||
|
fullCmd := fmt.Sprintf("C%d%s", cmdID, cmd)
|
||||||
|
|
||||||
// Send command
|
// Send command
|
||||||
_, err := c.conn.Write([]byte(cmd))
|
_, err := c.conn.Write([]byte(fullCmd))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.conn = nil
|
c.conn = nil
|
||||||
return "", fmt.Errorf("failed to send command: %w", err)
|
return "", fmt.Errorf("failed to send command: %w", err)
|
||||||
|
|||||||
@@ -6,13 +6,14 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
. "git.rouggy.com/rouggy/ShackMaster/internal/devices"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Client struct {
|
type Client struct {
|
||||||
host string
|
host string
|
||||||
port int
|
port int
|
||||||
idNumber int
|
conn net.Conn
|
||||||
conn net.Conn
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type Status struct {
|
type Status struct {
|
||||||
@@ -31,11 +32,10 @@ type Status struct {
|
|||||||
Connected bool `json:"connected"`
|
Connected bool `json:"connected"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(host string, port int, idNumber int) *Client {
|
func New(host string, port int) *Client {
|
||||||
return &Client{
|
return &Client{
|
||||||
host: host,
|
host: host,
|
||||||
port: port,
|
port: port,
|
||||||
idNumber: idNumber,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -62,8 +62,11 @@ func (c *Client) sendCommand(cmd string) (string, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Format command with ID
|
// Get next command ID from global counter
|
||||||
fullCmd := fmt.Sprintf("C%d|%s\n", c.idNumber, cmd)
|
cmdID := GetGlobalCommandID().GetNextID()
|
||||||
|
|
||||||
|
// Format command with ID: C<id>|<command>
|
||||||
|
fullCmd := fmt.Sprintf("C%d|%s\n", cmdID, cmd)
|
||||||
|
|
||||||
// Send command
|
// Send command
|
||||||
_, err := c.conn.Write([]byte(fullCmd))
|
_, err := c.conn.Write([]byte(fullCmd))
|
||||||
|
|||||||
@@ -4,6 +4,8 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -82,6 +84,56 @@ func (c *Client) AllOff() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetStatus queries the actual state of all relays
|
||||||
|
func (c *Client) GetStatus() (*Status, error) {
|
||||||
|
url := fmt.Sprintf("http://%s/relaystate/get2/1$2$3$4$5$", c.host)
|
||||||
|
|
||||||
|
resp, err := c.httpClient.Get(url)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to get relay status: %w", err)
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
return nil, fmt.Errorf("unexpected status code %d", resp.StatusCode)
|
||||||
|
}
|
||||||
|
|
||||||
|
body, err := io.ReadAll(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to read response: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse response format: "1,1\n2,1\n3,1\n4,1\n5,0\n"
|
||||||
|
status := &Status{
|
||||||
|
Relays: make([]RelayState, 0, 5),
|
||||||
|
}
|
||||||
|
|
||||||
|
lines := strings.Split(strings.TrimSpace(string(body)), "\n")
|
||||||
|
for _, line := range lines {
|
||||||
|
parts := strings.Split(strings.TrimSpace(line), ",")
|
||||||
|
if len(parts) != 2 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
relayNum, err := strconv.Atoi(parts[0])
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
relayState, err := strconv.Atoi(parts[1])
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
status.Relays = append(status.Relays, RelayState{
|
||||||
|
Number: relayNum,
|
||||||
|
State: relayState == 1,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return status, nil
|
||||||
|
}
|
||||||
|
|
||||||
// Ping checks if the device is reachable
|
// Ping checks if the device is reachable
|
||||||
func (c *Client) Ping() error {
|
func (c *Client) Ping() error {
|
||||||
url := fmt.Sprintf("http://%s/", c.host)
|
url := fmt.Sprintf("http://%s/", c.host)
|
||||||
|
|||||||
96
internal/services/solar/solar.go
Normal file
96
internal/services/solar/solar.go
Normal file
@@ -0,0 +1,96 @@
|
|||||||
|
package solar
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/xml"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// SolarData contains current solar and geomagnetic conditions
|
||||||
|
type SolarData struct {
|
||||||
|
SolarFluxIndex int `json:"sfi"` // Solar Flux Index
|
||||||
|
Sunspots int `json:"sunspots"` // Number of sunspots
|
||||||
|
AIndex int `json:"a_index"` // A-index (geomagnetic activity)
|
||||||
|
KIndex int `json:"k_index"` // K-index (geomagnetic activity)
|
||||||
|
GeomagField string `json:"geomag"` // Geomagnetic field status
|
||||||
|
UpdatedAt string `json:"updated"` // Last update time
|
||||||
|
}
|
||||||
|
|
||||||
|
// HamQSLResponse matches the XML structure from hamqsl.com
|
||||||
|
type HamQSLResponse struct {
|
||||||
|
XMLName xml.Name `xml:"solar"`
|
||||||
|
SolarData SolarDataXML `xml:"solardata"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type SolarDataXML struct {
|
||||||
|
Updated string `xml:"updated"`
|
||||||
|
SolarFlux string `xml:"solarflux"`
|
||||||
|
AIndex string `xml:"aindex"`
|
||||||
|
KIndex string `xml:"kindex"`
|
||||||
|
Sunspots string `xml:"sunspots"`
|
||||||
|
GeomagField string `xml:"geomagfield"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Client struct {
|
||||||
|
httpClient *http.Client
|
||||||
|
lastUpdate time.Time
|
||||||
|
cachedData *SolarData
|
||||||
|
}
|
||||||
|
|
||||||
|
func New() *Client {
|
||||||
|
return &Client{
|
||||||
|
httpClient: &http.Client{
|
||||||
|
Timeout: 10 * time.Second,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetSolarData fetches current solar data from HamQSL
|
||||||
|
// Data is cached for 15 minutes to avoid excessive requests
|
||||||
|
func (c *Client) GetSolarData() (*SolarData, error) {
|
||||||
|
// Return cached data if less than 15 minutes old
|
||||||
|
if c.cachedData != nil && time.Since(c.lastUpdate) < 15*time.Minute {
|
||||||
|
return c.cachedData, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, err := c.httpClient.Get("http://www.hamqsl.com/solarxml.php")
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to fetch solar data: %w", err)
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
|
||||||
|
}
|
||||||
|
|
||||||
|
body, err := io.ReadAll(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to read response: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var hamqslData HamQSLResponse
|
||||||
|
if err := xml.Unmarshal(body, &hamqslData); err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to parse XML: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse the data
|
||||||
|
solarData := &SolarData{
|
||||||
|
GeomagField: hamqslData.SolarData.GeomagField,
|
||||||
|
UpdatedAt: hamqslData.SolarData.Updated,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse numeric values (trim spaces)
|
||||||
|
fmt.Sscanf(strings.TrimSpace(hamqslData.SolarData.SolarFlux), "%d", &solarData.SolarFluxIndex)
|
||||||
|
fmt.Sscanf(strings.TrimSpace(hamqslData.SolarData.AIndex), "%d", &solarData.AIndex)
|
||||||
|
fmt.Sscanf(strings.TrimSpace(hamqslData.SolarData.KIndex), "%d", &solarData.KIndex)
|
||||||
|
fmt.Sscanf(strings.TrimSpace(hamqslData.SolarData.Sunspots), "%d", &solarData.Sunspots)
|
||||||
|
|
||||||
|
// Cache the data
|
||||||
|
c.cachedData = solarData
|
||||||
|
c.lastUpdate = time.Now()
|
||||||
|
|
||||||
|
return solarData, nil
|
||||||
|
}
|
||||||
126
internal/services/weather/weather.go
Normal file
126
internal/services/weather/weather.go
Normal file
@@ -0,0 +1,126 @@
|
|||||||
|
package weather
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// WeatherData contains current weather conditions
|
||||||
|
type WeatherData struct {
|
||||||
|
Temperature float64 `json:"temp"`
|
||||||
|
FeelsLike float64 `json:"feels_like"`
|
||||||
|
Humidity int `json:"humidity"`
|
||||||
|
Pressure int `json:"pressure"`
|
||||||
|
WindSpeed float64 `json:"wind_speed"`
|
||||||
|
WindGust float64 `json:"wind_gust"`
|
||||||
|
WindDeg int `json:"wind_deg"`
|
||||||
|
Clouds int `json:"clouds"`
|
||||||
|
Description string `json:"description"`
|
||||||
|
Icon string `json:"icon"`
|
||||||
|
UpdatedAt string `json:"updated"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// OpenWeatherMapResponse matches the API response
|
||||||
|
type OpenWeatherMapResponse struct {
|
||||||
|
Weather []struct {
|
||||||
|
Description string `json:"description"`
|
||||||
|
Icon string `json:"icon"`
|
||||||
|
} `json:"weather"`
|
||||||
|
Main struct {
|
||||||
|
Temp float64 `json:"temp"`
|
||||||
|
FeelsLike float64 `json:"feels_like"`
|
||||||
|
Humidity int `json:"humidity"`
|
||||||
|
Pressure int `json:"pressure"`
|
||||||
|
} `json:"main"`
|
||||||
|
Wind struct {
|
||||||
|
Speed float64 `json:"speed"`
|
||||||
|
Deg int `json:"deg"`
|
||||||
|
Gust float64 `json:"gust"`
|
||||||
|
} `json:"wind"`
|
||||||
|
Clouds struct {
|
||||||
|
All int `json:"all"`
|
||||||
|
} `json:"clouds"`
|
||||||
|
Dt int64 `json:"dt"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Client struct {
|
||||||
|
apiKey string
|
||||||
|
latitude float64
|
||||||
|
longitude float64
|
||||||
|
httpClient *http.Client
|
||||||
|
lastUpdate time.Time
|
||||||
|
cachedData *WeatherData
|
||||||
|
}
|
||||||
|
|
||||||
|
func New(apiKey string, latitude, longitude float64) *Client {
|
||||||
|
return &Client{
|
||||||
|
apiKey: apiKey,
|
||||||
|
latitude: latitude,
|
||||||
|
longitude: longitude,
|
||||||
|
httpClient: &http.Client{
|
||||||
|
Timeout: 10 * time.Second,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetWeatherData fetches current weather from OpenWeatherMap
|
||||||
|
// Data is cached for 10 minutes
|
||||||
|
func (c *Client) GetWeatherData() (*WeatherData, error) {
|
||||||
|
// Return cached data if less than 10 minutes old
|
||||||
|
if c.cachedData != nil && time.Since(c.lastUpdate) < 10*time.Minute {
|
||||||
|
return c.cachedData, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
url := fmt.Sprintf(
|
||||||
|
"https://api.openweathermap.org/data/2.5/weather?lat=%f&lon=%f&appid=%s&units=metric",
|
||||||
|
c.latitude, c.longitude, c.apiKey,
|
||||||
|
)
|
||||||
|
|
||||||
|
resp, err := c.httpClient.Get(url)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to fetch weather data: %w", err)
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
body, _ := io.ReadAll(resp.Body)
|
||||||
|
return nil, fmt.Errorf("unexpected status code %d: %s", resp.StatusCode, string(body))
|
||||||
|
}
|
||||||
|
|
||||||
|
body, err := io.ReadAll(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to read response: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var owmData OpenWeatherMapResponse
|
||||||
|
if err := json.Unmarshal(body, &owmData); err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to parse JSON: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Convert to our structure
|
||||||
|
weatherData := &WeatherData{
|
||||||
|
Temperature: owmData.Main.Temp,
|
||||||
|
FeelsLike: owmData.Main.FeelsLike,
|
||||||
|
Humidity: owmData.Main.Humidity,
|
||||||
|
Pressure: owmData.Main.Pressure,
|
||||||
|
WindSpeed: owmData.Wind.Speed,
|
||||||
|
WindGust: owmData.Wind.Gust,
|
||||||
|
WindDeg: owmData.Wind.Deg,
|
||||||
|
Clouds: owmData.Clouds.All,
|
||||||
|
UpdatedAt: time.Unix(owmData.Dt, 0).Format(time.RFC3339),
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(owmData.Weather) > 0 {
|
||||||
|
weatherData.Description = owmData.Weather[0].Description
|
||||||
|
weatherData.Icon = owmData.Weather[0].Icon
|
||||||
|
}
|
||||||
|
|
||||||
|
// Cache the data
|
||||||
|
c.cachedData = weatherData
|
||||||
|
c.lastUpdate = time.Now()
|
||||||
|
|
||||||
|
return weatherData, nil
|
||||||
|
}
|
||||||
15
web/index.html
Normal file
15
web/index.html
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>ShackMaster - XV9Q Shack</title>
|
||||||
|
<link rel="preconnect" href="https://fonts.googleapis.com">
|
||||||
|
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
|
||||||
|
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;500;700&display=swap" rel="stylesheet">
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div id="app"></div>
|
||||||
|
<script type="module" src="/src/main.js"></script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
18
web/package.json
Normal file
18
web/package.json
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
{
|
||||||
|
"name": "shackmaster-web",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"type": "module",
|
||||||
|
"scripts": {
|
||||||
|
"dev": "vite",
|
||||||
|
"build": "vite build",
|
||||||
|
"preview": "vite preview"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"@sveltejs/vite-plugin-svelte": "^3.0.1",
|
||||||
|
"svelte": "^4.2.8",
|
||||||
|
"vite": "^5.0.11"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"@turf/turf": "^6.5.0"
|
||||||
|
}
|
||||||
|
}
|
||||||
265
web/src/App.svelte
Normal file
265
web/src/App.svelte
Normal file
@@ -0,0 +1,265 @@
|
|||||||
|
<script>
|
||||||
|
import { onMount, onDestroy } from 'svelte';
|
||||||
|
import { wsService, connected, systemStatus } from './lib/websocket.js';
|
||||||
|
import { api } from './lib/api.js';
|
||||||
|
import WebSwitch from './components/WebSwitch.svelte';
|
||||||
|
import PowerGenius from './components/PowerGenius.svelte';
|
||||||
|
import TunerGenius from './components/TunerGenius.svelte';
|
||||||
|
import AntennaGenius from './components/AntennaGenius.svelte';
|
||||||
|
import RotatorGenius from './components/RotatorGenius.svelte';
|
||||||
|
|
||||||
|
let status = null;
|
||||||
|
let isConnected = false;
|
||||||
|
let currentTime = new Date();
|
||||||
|
let callsign = 'F4BPO'; // Default
|
||||||
|
|
||||||
|
const unsubscribeStatus = systemStatus.subscribe(value => {
|
||||||
|
status = value;
|
||||||
|
});
|
||||||
|
|
||||||
|
const unsubscribeConnected = connected.subscribe(value => {
|
||||||
|
isConnected = value;
|
||||||
|
});
|
||||||
|
|
||||||
|
// Solar data from status
|
||||||
|
$: solarData = status?.solar || {
|
||||||
|
sfi: 0,
|
||||||
|
sunspots: 0,
|
||||||
|
a_index: 0,
|
||||||
|
k_index: 0,
|
||||||
|
geomag: 'Unknown'
|
||||||
|
};
|
||||||
|
|
||||||
|
onMount(async () => {
|
||||||
|
wsService.connect();
|
||||||
|
|
||||||
|
// Fetch config to get callsign
|
||||||
|
try {
|
||||||
|
const config = await api.getConfig();
|
||||||
|
if (config.callsign) {
|
||||||
|
callsign = config.callsign;
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Failed to fetch config:', err);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update clock every second
|
||||||
|
const clockInterval = setInterval(() => {
|
||||||
|
currentTime = new Date();
|
||||||
|
}, 1000);
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
clearInterval(clockInterval);
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
onDestroy(() => {
|
||||||
|
wsService.disconnect();
|
||||||
|
unsubscribeStatus();
|
||||||
|
unsubscribeConnected();
|
||||||
|
});
|
||||||
|
|
||||||
|
function formatTime(date) {
|
||||||
|
return date.toTimeString().slice(0, 8);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Weather data from status
|
||||||
|
$: weatherData = status?.weather || {
|
||||||
|
wind_speed: 0,
|
||||||
|
wind_gust: 0,
|
||||||
|
temp: 0,
|
||||||
|
feels_like: 0
|
||||||
|
};
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<div class="app">
|
||||||
|
<header>
|
||||||
|
<div class="header-left">
|
||||||
|
<h1>{callsign} Shack</h1>
|
||||||
|
<div class="connection-status">
|
||||||
|
<span class="status-indicator" class:status-online={isConnected} class:status-offline={!isConnected}></span>
|
||||||
|
{isConnected ? 'Connected' : 'Disconnected'}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="header-center">
|
||||||
|
<div class="solar-info">
|
||||||
|
<span class="solar-item">SFI <span class="value">{solarData.sfi}</span></span>
|
||||||
|
<span class="solar-item">Spots <span class="value">{solarData.sunspots}</span></span>
|
||||||
|
<span class="solar-item">A <span class="value">{solarData.a_index}</span></span>
|
||||||
|
<span class="solar-item">K <span class="value">{solarData.k_index}</span></span>
|
||||||
|
<span class="solar-item">G <span class="value">{solarData.geomag}</span></span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="header-right">
|
||||||
|
<div class="weather-info">
|
||||||
|
<span title="Wind">🌬️ {weatherData.wind_speed.toFixed(1)}m/s</span>
|
||||||
|
<span title="Gust">💨 {weatherData.wind_gust.toFixed(1)}m/s</span>
|
||||||
|
<span title="Temperature">🌡️ {weatherData.temp.toFixed(1)}°C</span>
|
||||||
|
<span title="Feels like">→ {weatherData.feels_like.toFixed(1)}°C</span>
|
||||||
|
</div>
|
||||||
|
<div class="clock">
|
||||||
|
<span class="time">{formatTime(currentTime)}</span>
|
||||||
|
<span class="date">{currentTime.toLocaleDateString()}</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</header>
|
||||||
|
|
||||||
|
<main>
|
||||||
|
<div class="dashboard-grid">
|
||||||
|
<div class="row">
|
||||||
|
<WebSwitch status={status?.webswitch} />
|
||||||
|
<PowerGenius status={status?.power_genius} />
|
||||||
|
<TunerGenius status={status?.tuner_genius} />
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="row">
|
||||||
|
<AntennaGenius status={status?.antenna_genius} />
|
||||||
|
<RotatorGenius status={status?.rotator_genius} />
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</main>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<style>
|
||||||
|
.app {
|
||||||
|
min-height: 100vh;
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
}
|
||||||
|
|
||||||
|
header {
|
||||||
|
background: linear-gradient(135deg, #1e3c72 0%, #2a5298 100%);
|
||||||
|
padding: 16px 24px;
|
||||||
|
display: flex;
|
||||||
|
justify-content: space-between;
|
||||||
|
align-items: center;
|
||||||
|
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
|
||||||
|
flex-wrap: wrap;
|
||||||
|
gap: 16px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.header-left {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 16px;
|
||||||
|
}
|
||||||
|
|
||||||
|
h1 {
|
||||||
|
font-size: 24px;
|
||||||
|
font-weight: 500;
|
||||||
|
margin: 0;
|
||||||
|
color: white;
|
||||||
|
}
|
||||||
|
|
||||||
|
.connection-status {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 8px;
|
||||||
|
font-size: 14px;
|
||||||
|
padding: 6px 12px;
|
||||||
|
background: rgba(0, 0, 0, 0.3);
|
||||||
|
border-radius: 16px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.header-center {
|
||||||
|
flex: 1;
|
||||||
|
display: flex;
|
||||||
|
justify-content: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
.solar-info {
|
||||||
|
display: flex;
|
||||||
|
gap: 20px;
|
||||||
|
font-size: 14px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.solar-item {
|
||||||
|
color: rgba(255, 255, 255, 0.8);
|
||||||
|
}
|
||||||
|
|
||||||
|
.solar-item .value {
|
||||||
|
color: var(--accent-teal);
|
||||||
|
font-weight: 500;
|
||||||
|
margin-left: 4px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.header-right {
|
||||||
|
display: flex;
|
||||||
|
gap: 20px;
|
||||||
|
align-items: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
.weather-info {
|
||||||
|
display: flex;
|
||||||
|
gap: 12px;
|
||||||
|
font-size: 14px;
|
||||||
|
color: rgba(255, 255, 255, 0.9);
|
||||||
|
}
|
||||||
|
|
||||||
|
.clock {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
align-items: flex-end;
|
||||||
|
}
|
||||||
|
|
||||||
|
.time {
|
||||||
|
font-size: 18px;
|
||||||
|
font-weight: 500;
|
||||||
|
color: white;
|
||||||
|
}
|
||||||
|
|
||||||
|
.date {
|
||||||
|
font-size: 12px;
|
||||||
|
color: rgba(255, 255, 255, 0.7);
|
||||||
|
}
|
||||||
|
|
||||||
|
main {
|
||||||
|
flex: 1;
|
||||||
|
padding: 24px;
|
||||||
|
overflow-y: auto;
|
||||||
|
}
|
||||||
|
|
||||||
|
.dashboard-grid {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
gap: 24px;
|
||||||
|
max-width: 1800px;
|
||||||
|
margin: 0 auto;
|
||||||
|
}
|
||||||
|
|
||||||
|
.row {
|
||||||
|
display: flex;
|
||||||
|
gap: 24px;
|
||||||
|
flex-wrap: wrap;
|
||||||
|
}
|
||||||
|
|
||||||
|
.row > :global(*) {
|
||||||
|
flex: 1;
|
||||||
|
min-width: 300px;
|
||||||
|
}
|
||||||
|
|
||||||
|
@media (max-width: 1200px) {
|
||||||
|
.row {
|
||||||
|
flex-direction: column;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@media (max-width: 768px) {
|
||||||
|
header {
|
||||||
|
flex-direction: column;
|
||||||
|
align-items: flex-start;
|
||||||
|
}
|
||||||
|
|
||||||
|
.header-center,
|
||||||
|
.header-right {
|
||||||
|
width: 100%;
|
||||||
|
justify-content: flex-start;
|
||||||
|
}
|
||||||
|
|
||||||
|
.solar-info {
|
||||||
|
flex-wrap: wrap;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</style>
|
||||||
128
web/src/app.css
Normal file
128
web/src/app.css
Normal file
@@ -0,0 +1,128 @@
|
|||||||
|
:root {
|
||||||
|
--bg-primary: #1a1a1a;
|
||||||
|
--bg-secondary: #2a2a2a;
|
||||||
|
--bg-card: #333333;
|
||||||
|
--text-primary: #ffffff;
|
||||||
|
--text-secondary: #b0b0b0;
|
||||||
|
--accent-teal: #00bcd4;
|
||||||
|
--accent-green: #4caf50;
|
||||||
|
--accent-red: #f44336;
|
||||||
|
--accent-blue: #2196f3;
|
||||||
|
--border-color: #444444;
|
||||||
|
}
|
||||||
|
|
||||||
|
* {
|
||||||
|
margin: 0;
|
||||||
|
padding: 0;
|
||||||
|
box-sizing: border-box;
|
||||||
|
}
|
||||||
|
|
||||||
|
body {
|
||||||
|
margin: 0;
|
||||||
|
padding: 0;
|
||||||
|
font-family: 'Roboto', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
|
||||||
|
background-color: var(--bg-primary);
|
||||||
|
color: var(--text-primary);
|
||||||
|
overflow-x: hidden;
|
||||||
|
}
|
||||||
|
|
||||||
|
#app {
|
||||||
|
min-height: 100vh;
|
||||||
|
}
|
||||||
|
|
||||||
|
button {
|
||||||
|
font-family: inherit;
|
||||||
|
cursor: pointer;
|
||||||
|
border: none;
|
||||||
|
outline: none;
|
||||||
|
transition: all 0.2s ease;
|
||||||
|
}
|
||||||
|
|
||||||
|
button:hover {
|
||||||
|
transform: translateY(-1px);
|
||||||
|
}
|
||||||
|
|
||||||
|
button:active {
|
||||||
|
transform: translateY(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
input, select {
|
||||||
|
font-family: inherit;
|
||||||
|
outline: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
.card {
|
||||||
|
background: var(--bg-card);
|
||||||
|
border: 1px solid var(--border-color);
|
||||||
|
border-radius: 8px;
|
||||||
|
padding: 16px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.status-indicator {
|
||||||
|
display: inline-block;
|
||||||
|
width: 12px;
|
||||||
|
height: 12px;
|
||||||
|
border-radius: 50%;
|
||||||
|
margin-right: 8px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.status-online {
|
||||||
|
background-color: var(--accent-green);
|
||||||
|
box-shadow: 0 0 8px var(--accent-green);
|
||||||
|
}
|
||||||
|
|
||||||
|
.status-offline {
|
||||||
|
background-color: var(--text-secondary);
|
||||||
|
}
|
||||||
|
|
||||||
|
.btn {
|
||||||
|
padding: 12px 24px;
|
||||||
|
border-radius: 6px;
|
||||||
|
font-weight: 500;
|
||||||
|
font-size: 14px;
|
||||||
|
text-transform: uppercase;
|
||||||
|
letter-spacing: 0.5px;
|
||||||
|
transition: all 0.2s ease;
|
||||||
|
}
|
||||||
|
|
||||||
|
.btn-primary {
|
||||||
|
background: var(--accent-green);
|
||||||
|
color: white;
|
||||||
|
}
|
||||||
|
|
||||||
|
.btn-primary:hover {
|
||||||
|
background: #45a049;
|
||||||
|
}
|
||||||
|
|
||||||
|
.btn-danger {
|
||||||
|
background: var(--accent-red);
|
||||||
|
color: white;
|
||||||
|
}
|
||||||
|
|
||||||
|
.btn-danger:hover {
|
||||||
|
background: #da190b;
|
||||||
|
}
|
||||||
|
|
||||||
|
.btn-secondary {
|
||||||
|
background: var(--bg-secondary);
|
||||||
|
color: var(--text-primary);
|
||||||
|
border: 1px solid var(--border-color);
|
||||||
|
}
|
||||||
|
|
||||||
|
.btn-secondary:hover {
|
||||||
|
background: var(--bg-card);
|
||||||
|
}
|
||||||
|
|
||||||
|
.value-display {
|
||||||
|
font-size: 24px;
|
||||||
|
font-weight: 300;
|
||||||
|
color: var(--accent-teal);
|
||||||
|
}
|
||||||
|
|
||||||
|
.label {
|
||||||
|
font-size: 12px;
|
||||||
|
color: var(--text-secondary);
|
||||||
|
text-transform: uppercase;
|
||||||
|
letter-spacing: 1px;
|
||||||
|
margin-bottom: 4px;
|
||||||
|
}
|
||||||
129
web/src/components/AntennaGenius.svelte
Normal file
129
web/src/components/AntennaGenius.svelte
Normal file
@@ -0,0 +1,129 @@
|
|||||||
|
<script>
|
||||||
|
import { api } from '../lib/api.js';
|
||||||
|
|
||||||
|
export let status;
|
||||||
|
|
||||||
|
$: radio1Antenna = status?.radio1_antenna || 0;
|
||||||
|
$: radio2Antenna = status?.radio2_antenna || 0;
|
||||||
|
$: connected = status?.connected || false;
|
||||||
|
|
||||||
|
async function setRadioAntenna(radio, antenna) {
|
||||||
|
try {
|
||||||
|
await api.antenna.set(radio, antenna);
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Failed to set antenna:', err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<div class="antenna-card card">
|
||||||
|
<h2>
|
||||||
|
AG 8X2
|
||||||
|
<span class="status-indicator" class:status-online={connected} class:status-offline={!connected}></span>
|
||||||
|
</h2>
|
||||||
|
|
||||||
|
<div class="radio-section">
|
||||||
|
<div class="radio-label">Radio 1 / Radio 2</div>
|
||||||
|
|
||||||
|
<div class="radio-grid">
|
||||||
|
<div class="radio-column">
|
||||||
|
<div class="radio-title">Radio 1</div>
|
||||||
|
<div class="antenna-slots">
|
||||||
|
{#each Array(4) as _, i}
|
||||||
|
<button
|
||||||
|
class="slot"
|
||||||
|
class:active={radio1Antenna === i}
|
||||||
|
on:click={() => setRadioAntenna(1, i)}
|
||||||
|
>
|
||||||
|
{i + 1}
|
||||||
|
</button>
|
||||||
|
{/each}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="radio-column">
|
||||||
|
<div class="radio-title">Radio 2</div>
|
||||||
|
<div class="antenna-slots">
|
||||||
|
{#each Array(4) as _, i}
|
||||||
|
<button
|
||||||
|
class="slot"
|
||||||
|
class:active={radio2Antenna === i}
|
||||||
|
on:click={() => setRadioAntenna(2, i)}
|
||||||
|
>
|
||||||
|
{i + 1}
|
||||||
|
</button>
|
||||||
|
{/each}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<style>
|
||||||
|
.antenna-card {
|
||||||
|
min-width: 300px;
|
||||||
|
}
|
||||||
|
|
||||||
|
h2 {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 8px;
|
||||||
|
margin-bottom: 16px;
|
||||||
|
font-size: 18px;
|
||||||
|
font-weight: 500;
|
||||||
|
color: var(--accent-teal);
|
||||||
|
}
|
||||||
|
|
||||||
|
.radio-section {
|
||||||
|
margin-bottom: 16px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.radio-label {
|
||||||
|
font-size: 12px;
|
||||||
|
color: var(--text-secondary);
|
||||||
|
margin-bottom: 12px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.radio-grid {
|
||||||
|
display: flex;
|
||||||
|
gap: 16px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.radio-column {
|
||||||
|
flex: 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
.radio-title {
|
||||||
|
font-size: 14px;
|
||||||
|
font-weight: 500;
|
||||||
|
margin-bottom: 8px;
|
||||||
|
text-align: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
.antenna-slots {
|
||||||
|
display: grid;
|
||||||
|
grid-template-columns: 1fr 1fr;
|
||||||
|
gap: 8px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.slot {
|
||||||
|
padding: 16px;
|
||||||
|
background: var(--bg-secondary);
|
||||||
|
color: var(--text-primary);
|
||||||
|
border: 2px solid var(--border-color);
|
||||||
|
border-radius: 6px;
|
||||||
|
font-size: 16px;
|
||||||
|
font-weight: 500;
|
||||||
|
transition: all 0.2s ease;
|
||||||
|
}
|
||||||
|
|
||||||
|
.slot:hover {
|
||||||
|
border-color: var(--accent-blue);
|
||||||
|
}
|
||||||
|
|
||||||
|
.slot.active {
|
||||||
|
background: var(--accent-blue);
|
||||||
|
border-color: var(--accent-blue);
|
||||||
|
color: white;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
209
web/src/components/PowerGenius.svelte
Normal file
209
web/src/components/PowerGenius.svelte
Normal file
@@ -0,0 +1,209 @@
|
|||||||
|
<script>
|
||||||
|
export let status;
|
||||||
|
|
||||||
|
$: powerForward = status?.power_forward || 0;
|
||||||
|
$: powerReflected = status?.power_reflected || 0;
|
||||||
|
$: swr = status?.swr || 1.0;
|
||||||
|
$: voltage = status?.voltage || 0;
|
||||||
|
$: vdd = status?.vdd || 0;
|
||||||
|
$: current = status?.current || 0;
|
||||||
|
$: peakCurrent = status?.peak_current || 0;
|
||||||
|
$: temperature = status?.temperature || 0;
|
||||||
|
$: harmonicLoadTemp = status?.harmonic_load_temp || 0;
|
||||||
|
$: fanMode = status?.fan_mode || 'CONTEST';
|
||||||
|
$: state = status?.state || 'IDLE';
|
||||||
|
$: bandA = status?.band_a || '0';
|
||||||
|
$: bandB = status?.band_b || '0';
|
||||||
|
$: connected = status?.connected || false;
|
||||||
|
$: displayState = state.replace('TRANSMIT_A', 'TRANSMIT').replace('TRANSMIT_B', 'TRANSMIT');
|
||||||
|
$: meffa = status?.meffa || 'STANDBY';
|
||||||
|
|
||||||
|
async function setFanMode(mode) {
|
||||||
|
try {
|
||||||
|
await api.power.setFanMode(mode);
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Failed to set fan mode:', err);
|
||||||
|
alert('Failed to set fan mode');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<div class="powergenius-card card">
|
||||||
|
<h2>
|
||||||
|
PGXL
|
||||||
|
<span class="status-indicator" class:status-online={connected} class:status-offline={!connected}></span>
|
||||||
|
</h2>
|
||||||
|
|
||||||
|
<div class="status-row">
|
||||||
|
<div class="status-label" class:normal={state === 'IDLE'} class:warning={state.includes('TRANSMIT')}>
|
||||||
|
{displayState}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="metrics">
|
||||||
|
<div class="metric">
|
||||||
|
<div class="label">FWD PWR (W)</div>
|
||||||
|
<div class="value">{powerForward.toFixed(1)}</div>
|
||||||
|
<div class="bar">
|
||||||
|
<div class="bar-fill" style="width: {Math.min(100, (powerForward / 1500) * 100)}%"></div>
|
||||||
|
</div>
|
||||||
|
<div class="scale">
|
||||||
|
<span>0</span>
|
||||||
|
<span>1000</span>
|
||||||
|
<span>2000</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="metric">
|
||||||
|
<div class="label">PG XL SWR 1:1.00 use</div>
|
||||||
|
<div class="value">{swr.toFixed(2)}</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="metric">
|
||||||
|
<div class="label">Temp / HL Temp</div>
|
||||||
|
<div class="value">{temperature.toFixed(0)}°C / {harmonicLoadTemp.toFixed(1)}°C</div>
|
||||||
|
<div class="bar">
|
||||||
|
<div class="bar-fill" style="width: {(temperature / 80) * 100}%"></div>
|
||||||
|
</div>
|
||||||
|
<div class="scale">
|
||||||
|
<span>25</span>
|
||||||
|
<span>55</span>
|
||||||
|
<span>80</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="metric-row">
|
||||||
|
<div class="metric small">
|
||||||
|
<div class="label">VAC</div>
|
||||||
|
<div class="value">{voltage.toFixed(0)}</div>
|
||||||
|
</div>
|
||||||
|
<div class="metric small">
|
||||||
|
<div class="label">VDD</div>
|
||||||
|
<div class="value">{vdd.toFixed(1)}</div>
|
||||||
|
</div>
|
||||||
|
<div class="metric small">
|
||||||
|
<div class="label">ID peak</div>
|
||||||
|
<div class="value">{peakCurrent.toFixed(1)}</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="fan-speed">
|
||||||
|
<div class="label">Fan Speed</div>
|
||||||
|
<select value={fanMode} on:change={(e) => setFanMode(e.target.value)}>
|
||||||
|
<option value="STANDARD">STANDARD</option>
|
||||||
|
<option value="CONTEST">CONTEST</option>
|
||||||
|
<option value="BROADCAST">BROADCAST</option>
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="band-info">
|
||||||
|
<div class="label">Band A</div>
|
||||||
|
<div class="value">{bandA}</div>
|
||||||
|
</div>
|
||||||
|
<div class="band-info">
|
||||||
|
<div class="label">Band B</div>
|
||||||
|
<div class="value">{bandB}</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
<style>
|
||||||
|
.powergenius-card {
|
||||||
|
min-width: 350px;
|
||||||
|
}
|
||||||
|
|
||||||
|
h2 {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 8px;
|
||||||
|
margin-bottom: 12px;
|
||||||
|
font-size: 18px;
|
||||||
|
font-weight: 500;
|
||||||
|
color: var(--accent-teal);
|
||||||
|
}
|
||||||
|
|
||||||
|
.status-label {
|
||||||
|
display: inline-block;
|
||||||
|
padding: 8px 16px;
|
||||||
|
border-radius: 4px;
|
||||||
|
font-size: 14px;
|
||||||
|
font-weight: 500;
|
||||||
|
margin-bottom: 16px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.status-label.normal {
|
||||||
|
background: var(--accent-green);
|
||||||
|
color: white;
|
||||||
|
}
|
||||||
|
|
||||||
|
.status-label.warning {
|
||||||
|
background: var(--accent-red);
|
||||||
|
color: white;
|
||||||
|
}
|
||||||
|
|
||||||
|
.metrics {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
gap: 16px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.metric {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
gap: 4px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.metric-row {
|
||||||
|
display: flex;
|
||||||
|
gap: 16px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.metric.small {
|
||||||
|
flex: 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
.value {
|
||||||
|
font-size: 20px;
|
||||||
|
font-weight: 300;
|
||||||
|
color: var(--accent-teal);
|
||||||
|
}
|
||||||
|
|
||||||
|
.bar {
|
||||||
|
width: 100%;
|
||||||
|
height: 8px;
|
||||||
|
background: #555;
|
||||||
|
border-radius: 4px;
|
||||||
|
overflow: hidden;
|
||||||
|
}
|
||||||
|
|
||||||
|
.bar-fill {
|
||||||
|
height: 100%;
|
||||||
|
background: linear-gradient(90deg, var(--accent-green), var(--accent-red));
|
||||||
|
transition: width 0.3s ease;
|
||||||
|
}
|
||||||
|
|
||||||
|
.scale {
|
||||||
|
display: flex;
|
||||||
|
justify-content: space-between;
|
||||||
|
font-size: 10px;
|
||||||
|
color: var(--text-secondary);
|
||||||
|
}
|
||||||
|
|
||||||
|
.fan-speed select {
|
||||||
|
width: 100%;
|
||||||
|
padding: 8px;
|
||||||
|
background: var(--bg-secondary);
|
||||||
|
color: var(--text-primary);
|
||||||
|
border: 1px solid var(--border-color);
|
||||||
|
border-radius: 4px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.band-info {
|
||||||
|
display: flex;
|
||||||
|
justify-content: space-between;
|
||||||
|
align-items: center;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
334
web/src/components/RotatorGenius.svelte
Normal file
334
web/src/components/RotatorGenius.svelte
Normal file
@@ -0,0 +1,334 @@
|
|||||||
|
<script>
|
||||||
|
import { onMount } from 'svelte';
|
||||||
|
import { api } from '../lib/api.js';
|
||||||
|
|
||||||
|
export let status;
|
||||||
|
|
||||||
|
$: rotator1 = status?.rotator1 || {};
|
||||||
|
$: rotator2 = status?.rotator2 || {};
|
||||||
|
$: currentHeading = rotator1.current_azimuth || 0;
|
||||||
|
$: targetHeading = rotator1.target_azimuth || 0;
|
||||||
|
$: moving = rotator1.moving || 0;
|
||||||
|
$: connected = rotator1.connected || false;
|
||||||
|
|
||||||
|
let targetInput = currentHeading;
|
||||||
|
let canvas;
|
||||||
|
let ctx;
|
||||||
|
|
||||||
|
onMount(() => {
|
||||||
|
if (canvas) {
|
||||||
|
ctx = canvas.getContext('2d');
|
||||||
|
drawGlobe();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
$: if (ctx && currentHeading !== undefined) {
|
||||||
|
drawGlobe();
|
||||||
|
}
|
||||||
|
|
||||||
|
function drawGlobe() {
|
||||||
|
if (!ctx) return;
|
||||||
|
|
||||||
|
const width = canvas.width;
|
||||||
|
const height = canvas.height;
|
||||||
|
const centerX = width / 2;
|
||||||
|
const centerY = height / 2;
|
||||||
|
const radius = Math.min(width, height) / 2 - 20;
|
||||||
|
|
||||||
|
// Clear canvas
|
||||||
|
ctx.clearRect(0, 0, width, height);
|
||||||
|
|
||||||
|
// Draw globe circle
|
||||||
|
ctx.beginPath();
|
||||||
|
ctx.arc(centerX, centerY, radius, 0, 2 * Math.PI);
|
||||||
|
ctx.strokeStyle = '#444';
|
||||||
|
ctx.lineWidth = 2;
|
||||||
|
ctx.stroke();
|
||||||
|
|
||||||
|
// Draw grid lines (latitude/longitude)
|
||||||
|
ctx.strokeStyle = '#333';
|
||||||
|
ctx.lineWidth = 1;
|
||||||
|
|
||||||
|
// Latitude lines
|
||||||
|
for (let i = 1; i < 4; i++) {
|
||||||
|
const y = centerY - radius + (radius * 2 * i / 4);
|
||||||
|
ctx.beginPath();
|
||||||
|
ctx.moveTo(centerX - radius, y);
|
||||||
|
ctx.lineTo(centerX + radius, y);
|
||||||
|
ctx.stroke();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Longitude lines
|
||||||
|
for (let i = 1; i < 4; i++) {
|
||||||
|
const x = centerX - radius + (radius * 2 * i / 4);
|
||||||
|
ctx.beginPath();
|
||||||
|
ctx.moveTo(x, centerY - radius);
|
||||||
|
ctx.lineTo(x, centerY + radius);
|
||||||
|
ctx.stroke();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Draw cardinal directions
|
||||||
|
ctx.fillStyle = '#888';
|
||||||
|
ctx.font = '14px Roboto';
|
||||||
|
ctx.textAlign = 'center';
|
||||||
|
ctx.textBaseline = 'middle';
|
||||||
|
|
||||||
|
ctx.fillText('N', centerX, centerY - radius - 10);
|
||||||
|
ctx.fillText('S', centerX, centerY + radius + 10);
|
||||||
|
ctx.fillText('E', centerX + radius + 10, centerY);
|
||||||
|
ctx.fillText('W', centerX - radius - 10, centerY);
|
||||||
|
|
||||||
|
// Draw heading indicator
|
||||||
|
const angle = (currentHeading - 90) * Math.PI / 180;
|
||||||
|
const lineLength = radius - 10;
|
||||||
|
const endX = centerX + Math.cos(angle) * lineLength;
|
||||||
|
const endY = centerY + Math.sin(angle) * lineLength;
|
||||||
|
|
||||||
|
ctx.beginPath();
|
||||||
|
ctx.moveTo(centerX, centerY);
|
||||||
|
ctx.lineTo(endX, endY);
|
||||||
|
ctx.strokeStyle = '#00bcd4';
|
||||||
|
ctx.lineWidth = 3;
|
||||||
|
ctx.stroke();
|
||||||
|
|
||||||
|
// Draw arrow head
|
||||||
|
const arrowSize = 15;
|
||||||
|
const arrowAngle1 = angle + Math.PI * 0.85;
|
||||||
|
const arrowAngle2 = angle - Math.PI * 0.85;
|
||||||
|
|
||||||
|
ctx.beginPath();
|
||||||
|
ctx.moveTo(endX, endY);
|
||||||
|
ctx.lineTo(endX + Math.cos(arrowAngle1) * arrowSize, endY + Math.sin(arrowAngle1) * arrowSize);
|
||||||
|
ctx.moveTo(endX, endY);
|
||||||
|
ctx.lineTo(endX + Math.cos(arrowAngle2) * arrowSize, endY + Math.sin(arrowAngle2) * arrowSize);
|
||||||
|
ctx.strokeStyle = '#00bcd4';
|
||||||
|
ctx.lineWidth = 3;
|
||||||
|
ctx.stroke();
|
||||||
|
|
||||||
|
// Draw center dot
|
||||||
|
ctx.beginPath();
|
||||||
|
ctx.arc(centerX, centerY, 5, 0, 2 * Math.PI);
|
||||||
|
ctx.fillStyle = '#f44336';
|
||||||
|
ctx.fill();
|
||||||
|
}
|
||||||
|
|
||||||
|
async function moveToHeading() {
|
||||||
|
const heading = parseInt(targetInput);
|
||||||
|
if (isNaN(heading) || heading < 0 || heading > 360) {
|
||||||
|
alert('Please enter a valid heading (0-360)');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
await api.rotator.move(1, heading);
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Failed to move rotator:', err);
|
||||||
|
alert('Failed to move rotator');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function rotateCW() {
|
||||||
|
try {
|
||||||
|
await api.rotator.cw(1);
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Failed to rotate CW:', err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function rotateCCW() {
|
||||||
|
try {
|
||||||
|
await api.rotator.ccw(1);
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Failed to rotate CCW:', err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function stop() {
|
||||||
|
try {
|
||||||
|
await api.rotator.stop();
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Failed to stop:', err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Preset directions
|
||||||
|
const presets = [
|
||||||
|
{ name: 'EU-0', heading: 0 },
|
||||||
|
{ name: 'JA-35', heading: 35 },
|
||||||
|
{ name: 'AS-75', heading: 75 },
|
||||||
|
{ name: 'VK-120', heading: 120 },
|
||||||
|
{ name: 'AF-180', heading: 180 },
|
||||||
|
{ name: 'SA-230', heading: 230 },
|
||||||
|
{ name: 'WI-270', heading: 270 },
|
||||||
|
{ name: 'NA-300', heading: 300 }
|
||||||
|
];
|
||||||
|
|
||||||
|
async function gotoPreset(heading) {
|
||||||
|
try {
|
||||||
|
await api.rotator.move(1, heading);
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Failed to move to preset:', err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<div class="rotator-card card">
|
||||||
|
<h2>
|
||||||
|
ROTATOR GENIUS
|
||||||
|
<span class="status-indicator" class:status-online={connected} class:status-offline={!connected}></span>
|
||||||
|
</h2>
|
||||||
|
|
||||||
|
<div class="heading-display">
|
||||||
|
CURRENT HEADING: <span class="heading-value">{currentHeading}°</span>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{#if moving > 0}
|
||||||
|
<div class="moving-indicator">
|
||||||
|
{moving === 1 ? '↻ ROTATING CW' : '↺ ROTATING CCW'}
|
||||||
|
</div>
|
||||||
|
{/if}
|
||||||
|
|
||||||
|
<canvas bind:this={canvas} width="300" height="300"></canvas>
|
||||||
|
|
||||||
|
<div class="controls">
|
||||||
|
<div class="heading-input">
|
||||||
|
<input
|
||||||
|
type="number"
|
||||||
|
min="0"
|
||||||
|
max="360"
|
||||||
|
bind:value={targetInput}
|
||||||
|
placeholder="Enter heading"
|
||||||
|
/>
|
||||||
|
<button class="btn btn-primary" on:click={moveToHeading}>GO</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="rotation-controls">
|
||||||
|
<button class="btn btn-secondary" on:click={rotateCCW}>↺ CCW</button>
|
||||||
|
<button class="btn btn-danger" on:click={stop}>STOP</button>
|
||||||
|
<button class="btn btn-secondary" on:click={rotateCW}>CW ↻</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="presets">
|
||||||
|
{#each presets as preset}
|
||||||
|
<button
|
||||||
|
class="preset-btn"
|
||||||
|
class:active={Math.abs(currentHeading - preset.heading) < 5}
|
||||||
|
on:click={() => gotoPreset(preset.heading)}
|
||||||
|
>
|
||||||
|
{preset.name}
|
||||||
|
</button>
|
||||||
|
{/each}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<style>
|
||||||
|
.rotator-card {
|
||||||
|
min-width: 350px;
|
||||||
|
}
|
||||||
|
|
||||||
|
h2 {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 8px;
|
||||||
|
margin-bottom: 16px;
|
||||||
|
font-size: 18px;
|
||||||
|
font-weight: 500;
|
||||||
|
color: var(--accent-teal);
|
||||||
|
}
|
||||||
|
|
||||||
|
.heading-display {
|
||||||
|
font-size: 16px;
|
||||||
|
font-weight: 500;
|
||||||
|
text-align: center;
|
||||||
|
margin-bottom: 8px;
|
||||||
|
padding: 12px;
|
||||||
|
background: var(--bg-secondary);
|
||||||
|
border-radius: 6px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.heading-value {
|
||||||
|
color: var(--accent-blue);
|
||||||
|
font-size: 24px;
|
||||||
|
margin-left: 8px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.moving-indicator {
|
||||||
|
text-align: center;
|
||||||
|
padding: 8px;
|
||||||
|
background: var(--accent-green);
|
||||||
|
color: white;
|
||||||
|
border-radius: 4px;
|
||||||
|
margin-bottom: 12px;
|
||||||
|
font-weight: 500;
|
||||||
|
animation: pulse 1.5s ease-in-out infinite;
|
||||||
|
}
|
||||||
|
|
||||||
|
@keyframes pulse {
|
||||||
|
0%, 100% { opacity: 1; }
|
||||||
|
50% { opacity: 0.7; }
|
||||||
|
}
|
||||||
|
|
||||||
|
canvas {
|
||||||
|
display: block;
|
||||||
|
margin: 16px auto;
|
||||||
|
background: var(--bg-secondary);
|
||||||
|
border-radius: 8px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.controls {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
gap: 12px;
|
||||||
|
margin-bottom: 16px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.heading-input {
|
||||||
|
display: flex;
|
||||||
|
gap: 8px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.heading-input input {
|
||||||
|
flex: 1;
|
||||||
|
padding: 12px;
|
||||||
|
background: var(--bg-secondary);
|
||||||
|
color: var(--text-primary);
|
||||||
|
border: 1px solid var(--border-color);
|
||||||
|
border-radius: 4px;
|
||||||
|
font-size: 16px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.rotation-controls {
|
||||||
|
display: flex;
|
||||||
|
gap: 8px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.rotation-controls button {
|
||||||
|
flex: 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
.presets {
|
||||||
|
display: grid;
|
||||||
|
grid-template-columns: repeat(4, 1fr);
|
||||||
|
gap: 8px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.preset-btn {
|
||||||
|
padding: 12px 8px;
|
||||||
|
background: var(--accent-blue);
|
||||||
|
color: white;
|
||||||
|
border-radius: 4px;
|
||||||
|
font-size: 12px;
|
||||||
|
font-weight: 500;
|
||||||
|
transition: all 0.2s ease;
|
||||||
|
}
|
||||||
|
|
||||||
|
.preset-btn:hover {
|
||||||
|
background: #1976d2;
|
||||||
|
transform: translateY(-2px);
|
||||||
|
}
|
||||||
|
|
||||||
|
.preset-btn.active {
|
||||||
|
background: var(--accent-green);
|
||||||
|
}
|
||||||
|
</style>
|
||||||
257
web/src/components/TunerGenius.svelte
Normal file
257
web/src/components/TunerGenius.svelte
Normal file
@@ -0,0 +1,257 @@
|
|||||||
|
<script>
|
||||||
|
import { api } from '../lib/api.js';
|
||||||
|
|
||||||
|
export let status;
|
||||||
|
|
||||||
|
$: operate = status?.operate || false;
|
||||||
|
$: activeAntenna = status?.active_antenna || 0;
|
||||||
|
$: tuningStatus = status?.tuning_status || 'READY';
|
||||||
|
$: frequencyA = status?.frequency_a || 0;
|
||||||
|
$: frequencyB = status?.frequency_b || 0;
|
||||||
|
$: c1 = status?.c1 || 0;
|
||||||
|
$: l = status?.l || 0;
|
||||||
|
$: c2 = status?.c2 || 0;
|
||||||
|
$: connected = status?.connected || false;
|
||||||
|
|
||||||
|
let tuning = false;
|
||||||
|
|
||||||
|
async function toggleOperate() {
|
||||||
|
try {
|
||||||
|
await api.tuner.operate(!operate);
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Failed to toggle operate:', err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function startTune() {
|
||||||
|
tuning = true;
|
||||||
|
try {
|
||||||
|
await api.tuner.tune();
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Failed to tune:', err);
|
||||||
|
alert('Tuning failed');
|
||||||
|
} finally {
|
||||||
|
tuning = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function setAntenna(ant) {
|
||||||
|
try {
|
||||||
|
await api.tuner.antenna(ant);
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Failed to set antenna:', err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<div class="tuner-card card">
|
||||||
|
<h2>
|
||||||
|
TGXL
|
||||||
|
<span class="status-indicator" class:status-online={connected} class:status-offline={!connected}></span>
|
||||||
|
</h2>
|
||||||
|
|
||||||
|
<div class="power-status">
|
||||||
|
<div class="label">Power 0.0w</div>
|
||||||
|
<div class="status-badge">1500</div>
|
||||||
|
<div class="status-badge">1650</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="tuning-controls">
|
||||||
|
<div class="tuning-row">
|
||||||
|
<div class="tuning-label">TG XL SWR 1.00 use</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="antenna-buttons">
|
||||||
|
<button
|
||||||
|
class="antenna-btn"
|
||||||
|
class:active={activeAntenna === 0}
|
||||||
|
on:click={() => setAntenna(0)}
|
||||||
|
>
|
||||||
|
C1
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
class="antenna-btn"
|
||||||
|
class:active={activeAntenna === 1}
|
||||||
|
on:click={() => setAntenna(1)}
|
||||||
|
>
|
||||||
|
L
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
class="antenna-btn"
|
||||||
|
class:active={activeAntenna === 2}
|
||||||
|
on:click={() => setAntenna(2)}
|
||||||
|
>
|
||||||
|
C2
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="tuning-values">
|
||||||
|
<div class="value-box">
|
||||||
|
<div class="value">{c1}</div>
|
||||||
|
<div class="label">C1</div>
|
||||||
|
</div>
|
||||||
|
<div class="value-box">
|
||||||
|
<div class="value">{l}</div>
|
||||||
|
<div class="label">L</div>
|
||||||
|
</div>
|
||||||
|
<div class="value-box">
|
||||||
|
<div class="value">{c2}</div>
|
||||||
|
<div class="label">C2</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="status-row">
|
||||||
|
<div class="metric">
|
||||||
|
<div class="label">Tuning Status</div>
|
||||||
|
<div class="status-badge" class:tuning={tuningStatus === 'TUNING'}>
|
||||||
|
{tuningStatus}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="frequency-row">
|
||||||
|
<div class="metric">
|
||||||
|
<div class="label">Frequency A</div>
|
||||||
|
<div class="value-display">{(frequencyA / 1000).toFixed(3)}</div>
|
||||||
|
</div>
|
||||||
|
<div class="metric">
|
||||||
|
<div class="label">Frequency B</div>
|
||||||
|
<div class="value-display">{(frequencyB / 1000).toFixed(3)}</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="action-buttons">
|
||||||
|
<button
|
||||||
|
class="btn"
|
||||||
|
class:btn-primary={!operate}
|
||||||
|
class:btn-danger={operate}
|
||||||
|
on:click={toggleOperate}
|
||||||
|
>
|
||||||
|
{operate ? 'STANDBY' : 'OPERATE'}
|
||||||
|
</button>
|
||||||
|
<button class="btn btn-secondary" disabled>BYPASS</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<button
|
||||||
|
class="btn btn-danger tune-btn"
|
||||||
|
disabled={tuning || !operate}
|
||||||
|
on:click={startTune}
|
||||||
|
>
|
||||||
|
{tuning ? 'TUNING...' : 'TUNE'}
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<style>
|
||||||
|
.tuner-card {
|
||||||
|
min-width: 350px;
|
||||||
|
}
|
||||||
|
|
||||||
|
h2 {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 8px;
|
||||||
|
margin-bottom: 16px;
|
||||||
|
font-size: 18px;
|
||||||
|
font-weight: 500;
|
||||||
|
color: var(--accent-teal);
|
||||||
|
}
|
||||||
|
|
||||||
|
.power-status {
|
||||||
|
display: flex;
|
||||||
|
gap: 12px;
|
||||||
|
align-items: center;
|
||||||
|
margin-bottom: 16px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.status-badge {
|
||||||
|
padding: 4px 12px;
|
||||||
|
background: var(--bg-secondary);
|
||||||
|
border: 1px solid var(--border-color);
|
||||||
|
border-radius: 4px;
|
||||||
|
font-size: 12px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.status-badge.tuning {
|
||||||
|
background: var(--accent-green);
|
||||||
|
color: white;
|
||||||
|
}
|
||||||
|
|
||||||
|
.tuning-controls {
|
||||||
|
margin-bottom: 16px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.tuning-label {
|
||||||
|
font-size: 12px;
|
||||||
|
margin-bottom: 8px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.antenna-buttons {
|
||||||
|
display: flex;
|
||||||
|
gap: 8px;
|
||||||
|
margin: 12px 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.antenna-btn {
|
||||||
|
flex: 1;
|
||||||
|
padding: 12px;
|
||||||
|
background: var(--bg-secondary);
|
||||||
|
color: var(--text-primary);
|
||||||
|
border: 1px solid var(--border-color);
|
||||||
|
border-radius: 4px;
|
||||||
|
font-weight: 500;
|
||||||
|
}
|
||||||
|
|
||||||
|
.antenna-btn.active {
|
||||||
|
background: var(--accent-blue);
|
||||||
|
border-color: var(--accent-blue);
|
||||||
|
}
|
||||||
|
|
||||||
|
.tuning-values {
|
||||||
|
display: flex;
|
||||||
|
gap: 12px;
|
||||||
|
margin: 16px 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.value-box {
|
||||||
|
flex: 1;
|
||||||
|
text-align: center;
|
||||||
|
padding: 12px;
|
||||||
|
background: var(--bg-secondary);
|
||||||
|
border-radius: 4px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.value-box .value {
|
||||||
|
font-size: 20px;
|
||||||
|
font-weight: 300;
|
||||||
|
color: var(--accent-teal);
|
||||||
|
}
|
||||||
|
|
||||||
|
.status-row {
|
||||||
|
margin-bottom: 16px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.frequency-row {
|
||||||
|
display: flex;
|
||||||
|
gap: 16px;
|
||||||
|
margin-bottom: 16px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.metric {
|
||||||
|
flex: 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
.action-buttons {
|
||||||
|
display: flex;
|
||||||
|
gap: 12px;
|
||||||
|
margin-bottom: 12px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.action-buttons button {
|
||||||
|
flex: 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
.tune-btn {
|
||||||
|
width: 100%;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
152
web/src/components/WebSwitch.svelte
Normal file
152
web/src/components/WebSwitch.svelte
Normal file
@@ -0,0 +1,152 @@
|
|||||||
|
<script>
|
||||||
|
import { api } from '../lib/api.js';
|
||||||
|
|
||||||
|
export let status;
|
||||||
|
|
||||||
|
$: relays = status?.relays || [];
|
||||||
|
|
||||||
|
const relayNames = {
|
||||||
|
1: 'Power Supply',
|
||||||
|
2: 'PGXL',
|
||||||
|
3: 'TGXL',
|
||||||
|
4: 'Flex Radio Start',
|
||||||
|
5: 'Reserve'
|
||||||
|
};
|
||||||
|
|
||||||
|
let loading = {};
|
||||||
|
|
||||||
|
async function toggleRelay(relayNum) {
|
||||||
|
const relay = relays.find(r => r.number === relayNum);
|
||||||
|
const currentState = relay?.state || false;
|
||||||
|
|
||||||
|
loading[relayNum] = true;
|
||||||
|
try {
|
||||||
|
if (currentState) {
|
||||||
|
await api.webswitch.relayOff(relayNum);
|
||||||
|
} else {
|
||||||
|
await api.webswitch.relayOn(relayNum);
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Failed to toggle relay:', err);
|
||||||
|
alert('Failed to control relay');
|
||||||
|
} finally {
|
||||||
|
loading[relayNum] = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function allOn() {
|
||||||
|
try {
|
||||||
|
await api.webswitch.allOn();
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Failed to turn all on:', err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function allOff() {
|
||||||
|
try {
|
||||||
|
await api.webswitch.allOff();
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Failed to turn all off:', err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<div class="webswitch-card card">
|
||||||
|
<h2>
|
||||||
|
1216RH
|
||||||
|
<span class="status-indicator" class:status-online={relays.length > 0} class:status-offline={relays.length === 0}></span>
|
||||||
|
</h2>
|
||||||
|
|
||||||
|
<div class="relays">
|
||||||
|
{#each [1, 2, 3, 4, 5] as relayNum}
|
||||||
|
{@const relay = relays.find(r => r.number === relayNum)}
|
||||||
|
{@const isOn = relay?.state || false}
|
||||||
|
<div class="relay-row">
|
||||||
|
<span class="relay-name">{relayNames[relayNum]}</span>
|
||||||
|
<button
|
||||||
|
class="relay-toggle"
|
||||||
|
class:active={isOn}
|
||||||
|
disabled={loading[relayNum]}
|
||||||
|
on:click={() => toggleRelay(relayNum)}
|
||||||
|
>
|
||||||
|
<div class="toggle-icon"></div>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
{/each}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="controls">
|
||||||
|
<button class="btn btn-primary" on:click={allOn}>ALL ON</button>
|
||||||
|
<button class="btn btn-danger" on:click={allOff}>ALL OFF</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<style>
|
||||||
|
.webswitch-card {
|
||||||
|
min-width: 280px;
|
||||||
|
}
|
||||||
|
|
||||||
|
h2 {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 8px;
|
||||||
|
margin-bottom: 20px;
|
||||||
|
font-size: 18px;
|
||||||
|
font-weight: 500;
|
||||||
|
color: var(--accent-teal);
|
||||||
|
}
|
||||||
|
|
||||||
|
.relays {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
gap: 12px;
|
||||||
|
margin-bottom: 20px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.relay-row {
|
||||||
|
display: flex;
|
||||||
|
justify-content: space-between;
|
||||||
|
align-items: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
.relay-name {
|
||||||
|
font-size: 14px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.relay-toggle {
|
||||||
|
width: 60px;
|
||||||
|
height: 32px;
|
||||||
|
background: #555;
|
||||||
|
border-radius: 16px;
|
||||||
|
position: relative;
|
||||||
|
transition: background 0.3s ease;
|
||||||
|
}
|
||||||
|
|
||||||
|
.relay-toggle.active {
|
||||||
|
background: var(--accent-green);
|
||||||
|
}
|
||||||
|
|
||||||
|
.toggle-icon {
|
||||||
|
width: 24px;
|
||||||
|
height: 24px;
|
||||||
|
background: white;
|
||||||
|
border-radius: 50%;
|
||||||
|
position: absolute;
|
||||||
|
top: 4px;
|
||||||
|
left: 4px;
|
||||||
|
transition: transform 0.3s ease;
|
||||||
|
}
|
||||||
|
|
||||||
|
.relay-toggle.active .toggle-icon {
|
||||||
|
transform: translateX(28px);
|
||||||
|
}
|
||||||
|
|
||||||
|
.controls {
|
||||||
|
display: flex;
|
||||||
|
gap: 12px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.controls button {
|
||||||
|
flex: 1;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
76
web/src/lib/api.js
Normal file
76
web/src/lib/api.js
Normal file
@@ -0,0 +1,76 @@
|
|||||||
|
const API_BASE = '/api';
|
||||||
|
|
||||||
|
async function request(endpoint, options = {}) {
|
||||||
|
try {
|
||||||
|
const response = await fetch(`${API_BASE}${endpoint}`, {
|
||||||
|
...options,
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
...options.headers,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error(`HTTP error! status: ${response.status}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return await response.json();
|
||||||
|
} catch (error) {
|
||||||
|
console.error('API request failed:', error);
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const api = {
|
||||||
|
// Status
|
||||||
|
getStatus: () => request('/status'),
|
||||||
|
getConfig: () => request('/config'),
|
||||||
|
|
||||||
|
// WebSwitch
|
||||||
|
webswitch: {
|
||||||
|
relayOn: (relay) => request(`/webswitch/relay/on?relay=${relay}`, { method: 'POST' }),
|
||||||
|
relayOff: (relay) => request(`/webswitch/relay/off?relay=${relay}`, { method: 'POST' }),
|
||||||
|
allOn: () => request('/webswitch/all/on', { method: 'POST' }),
|
||||||
|
allOff: () => request('/webswitch/all/off', { method: 'POST' }),
|
||||||
|
},
|
||||||
|
|
||||||
|
// Rotator
|
||||||
|
rotator: {
|
||||||
|
move: (rotator, azimuth) => request('/rotator/move', {
|
||||||
|
method: 'POST',
|
||||||
|
body: JSON.stringify({ rotator, azimuth }),
|
||||||
|
}),
|
||||||
|
cw: (rotator) => request(`/rotator/cw?rotator=${rotator}`, { method: 'POST' }),
|
||||||
|
ccw: (rotator) => request(`/rotator/ccw?rotator=${rotator}`, { method: 'POST' }),
|
||||||
|
stop: () => request('/rotator/stop', { method: 'POST' }),
|
||||||
|
},
|
||||||
|
|
||||||
|
// Tuner
|
||||||
|
tuner: {
|
||||||
|
operate: (operate) => request('/tuner/operate', {
|
||||||
|
method: 'POST',
|
||||||
|
body: JSON.stringify({ operate }),
|
||||||
|
}),
|
||||||
|
tune: () => request('/tuner/tune', { method: 'POST' }),
|
||||||
|
antenna: (antenna) => request('/tuner/antenna', {
|
||||||
|
method: 'POST',
|
||||||
|
body: JSON.stringify({ antenna }),
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
|
||||||
|
// Antenna Genius
|
||||||
|
antenna: {
|
||||||
|
set: (radio, antenna) => request('/antenna/set', {
|
||||||
|
method: 'POST',
|
||||||
|
body: JSON.stringify({ radio, antenna }),
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
|
||||||
|
// Power Genius
|
||||||
|
power: {
|
||||||
|
setFanMode: (mode) => request('/power/fanmode', {
|
||||||
|
method: 'POST',
|
||||||
|
body: JSON.stringify({ mode }),
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
};
|
||||||
81
web/src/lib/websocket.js
Normal file
81
web/src/lib/websocket.js
Normal file
@@ -0,0 +1,81 @@
|
|||||||
|
import { writable } from 'svelte/store';
|
||||||
|
|
||||||
|
export const connected = writable(false);
|
||||||
|
export const systemStatus = writable(null);
|
||||||
|
export const lastUpdate = writable(null);
|
||||||
|
|
||||||
|
class WebSocketService {
|
||||||
|
constructor() {
|
||||||
|
this.ws = null;
|
||||||
|
this.reconnectTimeout = null;
|
||||||
|
this.reconnectDelay = 3000;
|
||||||
|
}
|
||||||
|
|
||||||
|
connect() {
|
||||||
|
const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
|
||||||
|
const wsUrl = `${protocol}//${window.location.host}/ws`;
|
||||||
|
|
||||||
|
try {
|
||||||
|
this.ws = new WebSocket(wsUrl);
|
||||||
|
|
||||||
|
this.ws.onopen = () => {
|
||||||
|
console.log('WebSocket connected');
|
||||||
|
connected.set(true);
|
||||||
|
};
|
||||||
|
|
||||||
|
this.ws.onmessage = (event) => {
|
||||||
|
try {
|
||||||
|
const message = JSON.parse(event.data);
|
||||||
|
|
||||||
|
if (message.type === 'update') {
|
||||||
|
systemStatus.set(message.data);
|
||||||
|
lastUpdate.set(new Date(message.timestamp));
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Error parsing message:', err);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
this.ws.onerror = (error) => {
|
||||||
|
console.error('WebSocket error:', error);
|
||||||
|
};
|
||||||
|
|
||||||
|
this.ws.onclose = () => {
|
||||||
|
console.log('WebSocket disconnected');
|
||||||
|
connected.set(false);
|
||||||
|
this.scheduleReconnect();
|
||||||
|
};
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Error creating WebSocket:', err);
|
||||||
|
this.scheduleReconnect();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
scheduleReconnect() {
|
||||||
|
if (this.reconnectTimeout) {
|
||||||
|
clearTimeout(this.reconnectTimeout);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.reconnectTimeout = setTimeout(() => {
|
||||||
|
console.log('Attempting to reconnect...');
|
||||||
|
this.connect();
|
||||||
|
}, this.reconnectDelay);
|
||||||
|
}
|
||||||
|
|
||||||
|
send(message) {
|
||||||
|
if (this.ws && this.ws.readyState === WebSocket.OPEN) {
|
||||||
|
this.ws.send(JSON.stringify(message));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
disconnect() {
|
||||||
|
if (this.reconnectTimeout) {
|
||||||
|
clearTimeout(this.reconnectTimeout);
|
||||||
|
}
|
||||||
|
if (this.ws) {
|
||||||
|
this.ws.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const wsService = new WebSocketService();
|
||||||
8
web/src/main.js
Normal file
8
web/src/main.js
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
import App from './App.svelte'
|
||||||
|
import './app.css'
|
||||||
|
|
||||||
|
const app = new App({
|
||||||
|
target: document.getElementById('app')
|
||||||
|
})
|
||||||
|
|
||||||
|
export default app
|
||||||
1
web/svelte.config.js
Normal file
1
web/svelte.config.js
Normal file
@@ -0,0 +1 @@
|
|||||||
|
export default {}
|
||||||
19
web/vite.config.js
Normal file
19
web/vite.config.js
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
import { defineConfig } from 'vite'
|
||||||
|
import { svelte } from '@sveltejs/vite-plugin-svelte'
|
||||||
|
|
||||||
|
export default defineConfig({
|
||||||
|
plugins: [svelte()],
|
||||||
|
build: {
|
||||||
|
outDir: 'dist'
|
||||||
|
},
|
||||||
|
server: {
|
||||||
|
port: 5173,
|
||||||
|
proxy: {
|
||||||
|
'/api': 'http://localhost:8081',
|
||||||
|
'/ws': {
|
||||||
|
target: 'ws://localhost:8081',
|
||||||
|
ws: true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
Reference in New Issue
Block a user