Combine cached accel and tap in one low-overhead master command for ~16 ms host polling. The dashboard uses a single live-stream toggle plus per-slave accel-stream controls; fix live_stream state so polling is not cleared every slow client refresh. Co-authored-by: Cursor <cursoragent@cursor.com>
53 lines
1.2 KiB
Go
53 lines
1.2 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
)
|
|
|
|
type liveStreamAPIResponse struct {
|
|
Enabled bool `json:"enabled"`
|
|
Success bool `json:"success"`
|
|
Error string `json:"error,omitempty"`
|
|
}
|
|
|
|
func mountLiveStreamAPI(mux *http.ServeMux, hub *wsHub) {
|
|
mux.HandleFunc("/api/live-stream", func(w http.ResponseWriter, r *http.Request) {
|
|
switch r.Method {
|
|
case http.MethodGet:
|
|
serveLiveStreamGet(w, hub)
|
|
case http.MethodPut:
|
|
serveLiveStreamPut(w, r, hub)
|
|
default:
|
|
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
|
}
|
|
})
|
|
}
|
|
|
|
func serveLiveStreamGet(w http.ResponseWriter, hub *wsHub) {
|
|
enabled := false
|
|
if hub != nil {
|
|
enabled = hub.liveStreamEnabled()
|
|
}
|
|
writeJSON(w, http.StatusOK, liveStreamAPIResponse{Enabled: enabled, Success: true})
|
|
}
|
|
|
|
func serveLiveStreamPut(w http.ResponseWriter, r *http.Request, hub *wsHub) {
|
|
var body struct {
|
|
Enable bool `json:"enable"`
|
|
}
|
|
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
|
writeJSON(w, http.StatusBadRequest, liveStreamAPIResponse{Error: "invalid JSON"})
|
|
return
|
|
}
|
|
|
|
if hub != nil {
|
|
hub.patchLiveStream(body.Enable)
|
|
}
|
|
|
|
writeJSON(w, http.StatusOK, liveStreamAPIResponse{
|
|
Enabled: body.Enable,
|
|
Success: true,
|
|
})
|
|
}
|