Move power mode to the api from signal

This commit is contained in:
Owen
2026-01-18 11:46:03 -08:00
parent 61846f9ec4
commit 5d8ea92ef0
2 changed files with 57 additions and 33 deletions

View File

@@ -33,7 +33,12 @@ type ConnectionRequest struct {
// SwitchOrgRequest defines the structure for switching organizations // SwitchOrgRequest defines the structure for switching organizations
type SwitchOrgRequest struct { 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 // PeerStatus represents the status of a peer connection
@@ -86,6 +91,7 @@ type API struct {
onDisconnect func() error onDisconnect func() error
onExit func() error onExit func() error
onRebind func() error onRebind func() error
onPowerMode func(PowerModeRequest) error
statusMu sync.RWMutex statusMu sync.RWMutex
peerStatuses map[int]*PeerStatus peerStatuses map[int]*PeerStatus
@@ -136,12 +142,15 @@ func (s *API) SetHandlers(
onDisconnect func() error, onDisconnect func() error,
onExit func() error, onExit func() error,
onRebind func() error, onRebind func() error,
onPowerMode func(PowerModeRequest) error,
) { ) {
s.onConnect = onConnect s.onConnect = onConnect
s.onSwitchOrg = onSwitchOrg s.onSwitchOrg = onSwitchOrg
s.onMetadataChange = onMetadataChange
s.onDisconnect = onDisconnect s.onDisconnect = onDisconnect
s.onExit = onExit s.onExit = onExit
s.onRebind = onRebind s.onRebind = onRebind
s.onPowerMode = onPowerMode
} }
// Start starts the HTTP server // Start starts the HTTP server
@@ -159,6 +168,7 @@ func (s *API) Start() error {
mux.HandleFunc("/exit", s.handleExit) mux.HandleFunc("/exit", s.handleExit)
mux.HandleFunc("/health", s.handleHealth) mux.HandleFunc("/health", s.handleHealth)
mux.HandleFunc("/rebind", s.handleRebind) mux.HandleFunc("/rebind", s.handleRebind)
mux.HandleFunc("/power-mode", s.handlePowerMode)
s.server = &http.Server{ s.server = &http.Server{
Handler: mux, Handler: mux,
@@ -625,3 +635,44 @@ func (s *API) handleRebind(w http.ResponseWriter, r *http.Request) {
"status": "socket rebound successfully", "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),
})
}

View File

@@ -7,9 +7,7 @@ import (
"net/http" "net/http"
_ "net/http/pprof" _ "net/http/pprof"
"os" "os"
"os/signal"
"sync" "sync"
"syscall"
"time" "time"
"github.com/fosrl/newt/bind" "github.com/fosrl/newt/bind"
@@ -278,6 +276,11 @@ func (o *Olm) registerAPICallbacks() {
logger.Info("Processing rebind request via API") logger.Info("Processing rebind request via API")
return o.RebindSocket() 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() }() 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 // Wait for context cancellation
<-tunnelCtx.Done() <-tunnelCtx.Done()
logger.Info("Tunnel process context cancelled, cleaning up") logger.Info("Tunnel process context cancelled, cleaning up")