generated from CubeCraft-Creations/Tracehound
d538dd3b70
The /cameras/{id}/start and /stop handlers only wrote a recording_events
row — they never published the command, so the camera never recorded.
Add Subscriber.PublishCommand (publishes {"command":...} to
remoterig/cameras/<id>/command, which the XIAO forwards to the ESP-01S),
thread a CommandPublisher into the recording handlers, and wire mqttSub in
via apiRouter. Tests pass nil (publish skipped).
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
121 lines
3.7 KiB
Go
121 lines
3.7 KiB
Go
// Package api provides HTTP handlers for camera operations.
|
|
package api
|
|
|
|
import (
|
|
"log"
|
|
"net/http"
|
|
|
|
"github.com/cubecraft/remoterig/internal/db"
|
|
"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.
|
|
func StartRecording(database *db.DB, pub CommandPublisher) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
cameraID := chi.URLParam(r, "id")
|
|
if !validateCameraID(w, cameraID) {
|
|
return
|
|
}
|
|
|
|
// Check if camera is registered
|
|
var exists int
|
|
err := database.QueryRowContext(r.Context(),
|
|
"SELECT COUNT(*) FROM cameras WHERE camera_id = ?", cameraID).Scan(&exists)
|
|
if err != nil {
|
|
log.Printf("Error checking camera existence: %v", err)
|
|
respondError(w, http.StatusInternalServerError, "database error", err.Error())
|
|
return
|
|
}
|
|
if exists == 0 {
|
|
respondError(w, http.StatusNotFound, "camera not found")
|
|
return
|
|
}
|
|
|
|
// Open recording event
|
|
result, err := database.ExecContext(r.Context(), `
|
|
INSERT INTO recording_events (camera_id, started_at, reason)
|
|
VALUES (?, datetime('now'), 'manual')
|
|
`, cameraID)
|
|
if err != nil {
|
|
log.Printf("Error starting recording: %v", err)
|
|
respondError(w, http.StatusInternalServerError, "database error", err.Error())
|
|
return
|
|
}
|
|
|
|
rowsAffected, _ := result.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{
|
|
"status": "recording_started",
|
|
"camera_id": cameraID,
|
|
})
|
|
}
|
|
}
|
|
|
|
// StopRecording returns a handler for POST /cameras/{id}/stop.
|
|
func StopRecording(database *db.DB, pub CommandPublisher) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
cameraID := chi.URLParam(r, "id")
|
|
if !validateCameraID(w, cameraID) {
|
|
return
|
|
}
|
|
|
|
// Check if camera is registered
|
|
var exists int
|
|
err := database.QueryRowContext(r.Context(),
|
|
"SELECT COUNT(*) FROM cameras WHERE camera_id = ?", cameraID).Scan(&exists)
|
|
if err != nil {
|
|
log.Printf("Error checking camera existence: %v", err)
|
|
respondError(w, http.StatusInternalServerError, "database error", err.Error())
|
|
return
|
|
}
|
|
if exists == 0 {
|
|
respondError(w, http.StatusNotFound, "camera not found")
|
|
return
|
|
}
|
|
|
|
// Close the most recent open recording event
|
|
result, err := database.ExecContext(r.Context(), `
|
|
UPDATE recording_events SET stopped_at = datetime('now'), reason = 'manual'
|
|
WHERE camera_id = ? AND stopped_at IS NULL
|
|
`, cameraID)
|
|
if err != nil {
|
|
log.Printf("Error stopping recording: %v", err)
|
|
respondError(w, http.StatusInternalServerError, "database error", err.Error())
|
|
return
|
|
}
|
|
|
|
rowsAffected, _ := result.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{
|
|
"status": "recording_stopped",
|
|
"camera_id": cameraID,
|
|
})
|
|
}
|
|
}
|