safesight-edge/agent/internal/httpapi/capabilities.go

93 lines
2.9 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 returns all node types known to this agent build.
// The media-server binary carries the same plugin set, so all catalogued
// types are considered available.
func (s *Server) installedNodeTypes() map[string]bool {
set := map[string]bool{}
for _, nt := range graphNodeTypesCatalog() {
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
}