Add RESTART command for master and slaves via ESP-NOW.
UART RESTART (client_id 0 = local reboot, >0 = unicast); dashboard and CLI hooks; delayed esp_restart after response. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -50,6 +50,16 @@ type findMeAPIResponse struct {
|
||||
Error string `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
type restartAPIRequest struct {
|
||||
ClientID uint32 `json:"client_id"`
|
||||
}
|
||||
|
||||
type restartAPIResponse struct {
|
||||
Success bool `json:"success"`
|
||||
ClientID uint32 `json:"client_id,omitempty"`
|
||||
Error string `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
type otaAPIResponse struct {
|
||||
Success bool `json:"success"`
|
||||
BytesWritten uint32 `json:"bytes_written,omitempty"`
|
||||
@@ -82,6 +92,13 @@ func mountServeAPI(mux *http.ServeMux, link *managedSerial, hub *wsHub) {
|
||||
}
|
||||
serveFindMe(w, r, link)
|
||||
})
|
||||
mux.HandleFunc("/api/restart", func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
serveRestart(w, r, link)
|
||||
})
|
||||
mux.HandleFunc("/api/ota", func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||
@@ -233,6 +250,22 @@ func applyDeadzoneToSlaves(link *managedSerial, deadzone uint32) (uint32, error)
|
||||
return updated, nil
|
||||
}
|
||||
|
||||
func serveRestart(w http.ResponseWriter, r *http.Request, link *managedSerial) {
|
||||
var body restartAPIRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
||||
writeJSON(w, http.StatusBadRequest, restartAPIResponse{Error: "invalid JSON"})
|
||||
return
|
||||
}
|
||||
if err := link.Restart(body.ClientID); err != nil {
|
||||
writeJSON(w, http.StatusServiceUnavailable, restartAPIResponse{
|
||||
ClientID: body.ClientID,
|
||||
Error: err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, restartAPIResponse{Success: true, ClientID: body.ClientID})
|
||||
}
|
||||
|
||||
func serveFindMe(w http.ResponseWriter, r *http.Request, link *managedSerial) {
|
||||
var body findMeAPIRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
||||
|
||||
Reference in New Issue
Block a user