safesight/agent/internal/httpapi/capabilities.go
tian 477b82d60a feat: /v1/capabilities endpoint for device feature detection
Returns available detection capabilities based on installed media-server plugins.
Each capability includes key, label, description and availability flag.
2026-05-12 13:53:04 +08:00

90 lines
2.8 KiB
Go

package httpapi
import "net/http"
// Capability represents a detection capability the device supports.
type Capability struct {
Key string `json:"key"` // "face", "shoe", "intrusion", ...
Label string `json:"label"` // human-readable name
Available bool `json:"available"` // whether the device has the required plugins
Description string `json:"description"` // brief description
}
// handleCapabilities returns the list of detection capabilities supported by this device,
// derived from the available media-server node types (plugins).
func (s *Server) handleCapabilities(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
errorJSON(w, http.StatusMethodNotAllowed, "method not allowed")
return
}
if !s.authorize(r, false) {
errorJSON(w, http.StatusUnauthorized, "unauthorized")
return
}
// Query media-server for installed node types.
installed := s.installedNodeTypes()
caps := []Capability{
{
Key: "face",
Label: "人脸识别",
Available: hasAll(installed, "ai_face_recog"),
Description: "人脸检测与识别",
},
{
Key: "shoe",
Label: "劳保鞋检测",
Available: hasAll(installed, "ai_shoe_det"),
Description: "工鞋/劳保鞋检测",
},
{
Key: "helmet",
Label: "安全帽检测",
Available: hasAll(installed, "ai_yolo"),
Description: "安全帽佩戴检测(需安全帽模型)",
},
{
Key: "intrusion",
Label: "区域入侵",
Available: hasAll(installed, "region_event"),
Description: "区域入侵/越线检测",
},
{
Key: "action",
Label: "行为识别",
Available: hasAll(installed, "action_recog"),
Description: "摔倒、打架等行为识别",
},
{
Key: "tracking",
Label: "人员跟踪",
Available: hasAll(installed, "tracker"),
Description: "多目标跟踪",
},
}
writeJSON(w, http.StatusOK, map[string]any{
"capabilities": caps,
})
}
// installedNodeTypes queries the media-server's graph node types API.
func (s *Server) installedNodeTypes() map[string]bool {
set := map[string]bool{}
nodeTypes := graphNodeTypesCatalog()
for _, nt := range nodeTypes {
set[nt.Type] = true
}
return set
}
func hasAll(installed map[string]bool, keys ...string) bool {
for _, k := range keys {
if !installed[k] {
return false
}
}
return true
}