catie/internal/api/api.go

67 lines
1.4 KiB
Go

package api
import (
"github.com/go-gst/go-gst/gst"
"github.com/google/uuid"
"github.com/labstack/echo/v4"
)
type API struct {
deviceMonitors map[string]DeviceMonitor
previewPipelines map[string]*PreviewPipeline
}
type DeviceMonitor interface {
GetDevices() []Device
GetDevice(name string) Device
GetDeviceType() string
}
type Device interface {
GetDisplayName() string
GetDeviceType() string
GetProperties() map[string]interface{}
GetOutput() (string, *gst.Element, *gst.Element, error)
}
func New() *API {
return &API{
deviceMonitors: make(map[string]DeviceMonitor),
previewPipelines: make(map[string]*PreviewPipeline),
}
}
func (api *API) RegisterRoutes(g *echo.Group) {
g.Add("GET", "/devices", api.GetDevices)
g.Add("GET", "/preview/:devicemonitor-id/:source", api.previewHandler)
}
func (api *API) RegisterDeviceMonitor(monitors ...DeviceMonitor) {
for _, monitor := range monitors {
api.deviceMonitors[uuid.NewString()] = monitor
}
}
type device struct {
MonitorID string
DeviceType string
DeviceName string
Properties map[string]interface{}
}
func (api *API) GetDevices(c echo.Context) error {
devices := make([]device, 0)
for id, monitor := range api.deviceMonitors {
for _, d := range monitor.GetDevices() {
devices = append(devices, device{
MonitorID: id,
DeviceType: d.GetDeviceType(),
DeviceName: d.GetDisplayName(),
Properties: d.GetProperties(),
})
}
}
return c.JSON(200, devices)
}