generated from CubeCraft-Creations/Tracehound
Dev #26
+4
-4
@@ -89,7 +89,7 @@ func main() {
|
|||||||
})
|
})
|
||||||
|
|
||||||
// API routes (auth required if API key is configured)
|
// API routes (auth required if API key is configured)
|
||||||
r.Mount("/api/v1", auth.Middleware(cfg.APIKey)(apiRouter(sseHub, sqlDB)))
|
r.Mount("/api/v1", auth.Middleware(cfg.APIKey)(apiRouter(sseHub, sqlDB, mqttSub)))
|
||||||
|
|
||||||
// Serve embedded React frontend with SPA fallback
|
// Serve embedded React frontend with SPA fallback
|
||||||
r.Mount("/", frontendHandler())
|
r.Mount("/", frontendHandler())
|
||||||
@@ -122,7 +122,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// apiRouter creates the API route tree.
|
// apiRouter creates the API route tree.
|
||||||
func apiRouter(sseHub *events.Hub, database *db.DB) http.Handler {
|
func apiRouter(sseHub *events.Hub, database *db.DB, pub api.CommandPublisher) http.Handler {
|
||||||
r := chi.NewRouter()
|
r := chi.NewRouter()
|
||||||
|
|
||||||
// Camera management routes
|
// Camera management routes
|
||||||
@@ -131,8 +131,8 @@ func apiRouter(sseHub *events.Hub, database *db.DB) http.Handler {
|
|||||||
r.Get("/cameras/{id}", api.GetCameraDetail(database))
|
r.Get("/cameras/{id}", api.GetCameraDetail(database))
|
||||||
|
|
||||||
// Recording control routes
|
// Recording control routes
|
||||||
r.Post("/cameras/{id}/start", api.StartRecording(database))
|
r.Post("/cameras/{id}/start", api.StartRecording(database, pub))
|
||||||
r.Post("/cameras/{id}/stop", api.StopRecording(database))
|
r.Post("/cameras/{id}/stop", api.StopRecording(database, pub))
|
||||||
|
|
||||||
// Status ingestion (from ESP32 nodes)
|
// Status ingestion (from ESP32 nodes)
|
||||||
r.Post("/cameras/{id}/status", api.PushStatus(database))
|
r.Post("/cameras/{id}/status", api.PushStatus(database))
|
||||||
|
|||||||
@@ -26,8 +26,8 @@ func setupTestRouter(t *testing.T) (*db.DB, chi.Router) {
|
|||||||
r.Get("/cameras", ListCameras(database))
|
r.Get("/cameras", ListCameras(database))
|
||||||
r.Post("/cameras", RegisterCamera(database))
|
r.Post("/cameras", RegisterCamera(database))
|
||||||
r.Get("/cameras/{id}", GetCameraDetail(database))
|
r.Get("/cameras/{id}", GetCameraDetail(database))
|
||||||
r.Post("/cameras/{id}/start", StartRecording(database))
|
r.Post("/cameras/{id}/start", StartRecording(database, nil))
|
||||||
r.Post("/cameras/{id}/stop", StopRecording(database))
|
r.Post("/cameras/{id}/stop", StopRecording(database, nil))
|
||||||
r.Post("/cameras/{id}/status", PushStatus(database))
|
r.Post("/cameras/{id}/status", PushStatus(database))
|
||||||
|
|
||||||
return database, r
|
return database, r
|
||||||
|
|||||||
@@ -9,8 +9,14 @@ import (
|
|||||||
"github.com/go-chi/chi/v5"
|
"github.com/go-chi/chi/v5"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// CommandPublisher sends a command to a camera (implemented by the MQTT
|
||||||
|
// subscriber). Nil is allowed (e.g. in tests) — the command is then skipped.
|
||||||
|
type CommandPublisher interface {
|
||||||
|
PublishCommand(cameraID, command string) error
|
||||||
|
}
|
||||||
|
|
||||||
// StartRecording returns a handler for POST /cameras/{id}/start.
|
// StartRecording returns a handler for POST /cameras/{id}/start.
|
||||||
func StartRecording(database *db.DB) http.HandlerFunc {
|
func StartRecording(database *db.DB, pub CommandPublisher) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
cameraID := chi.URLParam(r, "id")
|
cameraID := chi.URLParam(r, "id")
|
||||||
if !validateCameraID(w, cameraID) {
|
if !validateCameraID(w, cameraID) {
|
||||||
@@ -45,6 +51,15 @@ func StartRecording(database *db.DB) http.HandlerFunc {
|
|||||||
rowsAffected, _ := result.RowsAffected()
|
rowsAffected, _ := result.RowsAffected()
|
||||||
log.Printf("Recording started on %s (%d rows affected)", cameraID, rowsAffected)
|
log.Printf("Recording started on %s (%d rows affected)", cameraID, rowsAffected)
|
||||||
|
|
||||||
|
// Send the actual command to the camera over MQTT.
|
||||||
|
if pub != nil {
|
||||||
|
if err := pub.PublishCommand(cameraID, "start_recording"); err != nil {
|
||||||
|
log.Printf("Error sending start_recording to %s: %v", cameraID, err)
|
||||||
|
respondError(w, http.StatusBadGateway, "failed to send command to camera", err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
respondJSON(w, http.StatusOK, map[string]string{
|
respondJSON(w, http.StatusOK, map[string]string{
|
||||||
"status": "recording_started",
|
"status": "recording_started",
|
||||||
"camera_id": cameraID,
|
"camera_id": cameraID,
|
||||||
@@ -53,7 +68,7 @@ func StartRecording(database *db.DB) http.HandlerFunc {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// StopRecording returns a handler for POST /cameras/{id}/stop.
|
// StopRecording returns a handler for POST /cameras/{id}/stop.
|
||||||
func StopRecording(database *db.DB) http.HandlerFunc {
|
func StopRecording(database *db.DB, pub CommandPublisher) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
cameraID := chi.URLParam(r, "id")
|
cameraID := chi.URLParam(r, "id")
|
||||||
if !validateCameraID(w, cameraID) {
|
if !validateCameraID(w, cameraID) {
|
||||||
@@ -88,6 +103,15 @@ func StopRecording(database *db.DB) http.HandlerFunc {
|
|||||||
rowsAffected, _ := result.RowsAffected()
|
rowsAffected, _ := result.RowsAffected()
|
||||||
log.Printf("Recording stopped on %s (%d rows affected)", cameraID, rowsAffected)
|
log.Printf("Recording stopped on %s (%d rows affected)", cameraID, rowsAffected)
|
||||||
|
|
||||||
|
// Send the actual command to the camera over MQTT.
|
||||||
|
if pub != nil {
|
||||||
|
if err := pub.PublishCommand(cameraID, "stop_recording"); err != nil {
|
||||||
|
log.Printf("Error sending stop_recording to %s: %v", cameraID, err)
|
||||||
|
respondError(w, http.StatusBadGateway, "failed to send command to camera", err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
respondJSON(w, http.StatusOK, map[string]string{
|
respondJSON(w, http.StatusOK, map[string]string{
|
||||||
"status": "recording_stopped",
|
"status": "recording_stopped",
|
||||||
"camera_id": cameraID,
|
"camera_id": cameraID,
|
||||||
|
|||||||
@@ -143,6 +143,18 @@ type statusPayload struct {
|
|||||||
UptimeSec *int `json:"uptime_sec"`
|
UptimeSec *int `json:"uptime_sec"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PublishCommand sends a command (e.g. "start_recording") to a camera's
|
||||||
|
// command topic, which its ESP32 bridge subscribes to and forwards over UART.
|
||||||
|
func (s *Subscriber) PublishCommand(cameraID, command string) error {
|
||||||
|
topic := "remoterig/cameras/" + cameraID + "/command"
|
||||||
|
payload, _ := json.Marshal(map[string]string{"command": command})
|
||||||
|
tok := s.client.Publish(topic, 2, false, payload)
|
||||||
|
if !tok.WaitTimeout(3 * time.Second) {
|
||||||
|
return fmt.Errorf("publish to %s timed out", topic)
|
||||||
|
}
|
||||||
|
return tok.Error()
|
||||||
|
}
|
||||||
|
|
||||||
func (s *Subscriber) handleStatus(cameraID string, payload []byte) {
|
func (s *Subscriber) handleStatus(cameraID string, payload []byte) {
|
||||||
var sp statusPayload
|
var sp statusPayload
|
||||||
if err := json.Unmarshal(payload, &sp); err != nil {
|
if err := json.Unmarshal(payload, &sp); err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user