package api import ( "encoding/json" "net/http" ) // GET /api/devices — list all known device states. func (s *Server) handleListDevices(w http.ResponseWriter, r *http.Request) { writeJSON(w, http.StatusOK, s.hub.Snapshot()) } // GET /api/devices/{id}/track — GPS track for the map trail. func (s *Server) handleTrack(w http.ResponseWriter, r *http.Request) { writeJSON(w, http.StatusOK, s.hub.Track(r.PathValue("id"))) } // POST /api/devices/{id}/command — push a command down to a device. // Body: {"command":"...","payload":{...}} func (s *Server) handleCommand(w http.ResponseWriter, r *http.Request) { id := r.PathValue("id") var body struct { Command string `json:"command"` Payload map[string]any `json:"payload"` } if err := json.NewDecoder(r.Body).Decode(&body); err != nil || body.Command == "" { writeError(w, http.StatusBadRequest, "command required") return } if !s.hub.SendCommand(id, body.Command, body.Payload) { writeJSON(w, http.StatusNotFound, map[string]any{"error": "device not connected", "deviceId": id}) return } writeJSON(w, http.StatusOK, map[string]any{"sent": true, "deviceId": id, "command": body.Command}) } // DELETE /api/devices/{id} — forget a device's stored state (clears stale entries). func (s *Server) handleForget(w http.ResponseWriter, r *http.Request) { id := r.PathValue("id") existed := s.hub.Forget(id) writeJSON(w, http.StatusOK, map[string]any{"removed": existed, "deviceId": id}) }