From 5d8ea92ef0518bf5c4b59642e6d05e0fdcdf3fd0 Mon Sep 17 00:00:00 2001 From: Owen Date: Sun, 18 Jan 2026 11:46:03 -0800 Subject: [PATCH] Move power mode to the api from signal --- api/api.go | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++++- olm/olm.go | 37 +++++-------------------------------- 2 files changed, 57 insertions(+), 33 deletions(-) diff --git a/api/api.go b/api/api.go index b11cc70..efd3346 100644 --- a/api/api.go +++ b/api/api.go @@ -33,7 +33,12 @@ type ConnectionRequest struct { // SwitchOrgRequest defines the structure for switching organizations type SwitchOrgRequest struct { - OrgID string `json:"orgId"` + OrgID string `json:"org_id"` +} + +// PowerModeRequest represents a request to change power mode +type PowerModeRequest struct { + Mode string `json:"mode"` // "normal" or "low" } // PeerStatus represents the status of a peer connection @@ -86,6 +91,7 @@ type API struct { onDisconnect func() error onExit func() error onRebind func() error + onPowerMode func(PowerModeRequest) error statusMu sync.RWMutex peerStatuses map[int]*PeerStatus @@ -136,12 +142,15 @@ func (s *API) SetHandlers( onDisconnect func() error, onExit func() error, onRebind func() error, + onPowerMode func(PowerModeRequest) error, ) { s.onConnect = onConnect s.onSwitchOrg = onSwitchOrg + s.onMetadataChange = onMetadataChange s.onDisconnect = onDisconnect s.onExit = onExit s.onRebind = onRebind + s.onPowerMode = onPowerMode } // Start starts the HTTP server @@ -159,6 +168,7 @@ func (s *API) Start() error { mux.HandleFunc("/exit", s.handleExit) mux.HandleFunc("/health", s.handleHealth) mux.HandleFunc("/rebind", s.handleRebind) + mux.HandleFunc("/power-mode", s.handlePowerMode) s.server = &http.Server{ Handler: mux, @@ -625,3 +635,44 @@ func (s *API) handleRebind(w http.ResponseWriter, r *http.Request) { "status": "socket rebound successfully", }) } + +// handlePowerMode handles the /power-mode endpoint +// This allows changing the power mode between "normal" and "low" +func (s *API) handlePowerMode(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodPost { + http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) + return + } + + var req PowerModeRequest + if err := json.NewDecoder(r.Body).Decode(&req); err != nil { + http.Error(w, fmt.Sprintf("Invalid request body: %v", err), http.StatusBadRequest) + return + } + + // Validate power mode + if req.Mode != "normal" && req.Mode != "low" { + http.Error(w, "Invalid power mode: must be 'normal' or 'low'", http.StatusBadRequest) + return + } + + logger.Info("Received power mode change request via API: mode=%s", req.Mode) + + // Call the power mode handler if set + if s.onPowerMode != nil { + if err := s.onPowerMode(req); err != nil { + http.Error(w, fmt.Sprintf("Power mode change failed: %v", err), http.StatusInternalServerError) + return + } + } else { + http.Error(w, "Power mode handler not configured", http.StatusNotImplemented) + return + } + + // Return a success response + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) + _ = json.NewEncoder(w).Encode(map[string]string{ + "status": fmt.Sprintf("power mode changed to %s successfully", req.Mode), + }) +} diff --git a/olm/olm.go b/olm/olm.go index 6c975d3..691d716 100644 --- a/olm/olm.go +++ b/olm/olm.go @@ -7,9 +7,7 @@ import ( "net/http" _ "net/http/pprof" "os" - "os/signal" "sync" - "syscall" "time" "github.com/fosrl/newt/bind" @@ -278,6 +276,11 @@ func (o *Olm) registerAPICallbacks() { logger.Info("Processing rebind request via API") return o.RebindSocket() }, + // onPowerMode + func(req api.PowerModeRequest) error { + logger.Info("Processing power mode change request via API: mode=%s", req.Mode) + return o.SetPowerMode(req.Mode) + }, ) } @@ -470,36 +473,6 @@ func (o *Olm) StartTunnel(config TunnelConfig) { } defer func() { _ = o.websocket.Close() }() - // Setup SIGHUP signal handler for testing (toggles power state) - // THIS SHOULD ONLY BE USED AND ON IN A DEV MODE - sigChan := make(chan os.Signal, 1) - signal.Notify(sigChan, syscall.SIGHUP) - - go func() { - powerMode := "normal" - for { - select { - case <-sigChan: - - logger.Info("SIGHUP received, toggling power mode") - if powerMode == "normal" { - powerMode = "low" - if err := o.SetPowerMode("low"); err != nil { - logger.Error("Failed to set low power mode: %v", err) - } - } else { - powerMode = "normal" - if err := o.SetPowerMode("normal"); err != nil { - logger.Error("Failed to set normal power mode: %v", err) - } - } - - case <-tunnelCtx.Done(): - return - } - } - }() - // Wait for context cancellation <-tunnelCtx.Done() logger.Info("Tunnel process context cancelled, cleaning up")