main
Raw Download raw file
 1// Package protect provides a client for the UniFi Protect API.
 2package protect
 3
 4import "time"
 5
 6// Camera represents a UniFi Protect camera.
 7type Camera struct {
 8	ID           string `json:"id"`
 9	Name         string `json:"name"`
10	Type         string `json:"type"`
11	State        string `json:"state"`
12	IsConnected  bool   `json:"isConnected"`
13	Host         string `json:"host"`
14	Mac          string `json:"mac"`
15	ModelKey     string `json:"modelKey"`
16	FirmwareVer  string `json:"firmwareBuild"`
17	VideoWidth   int    `json:"videoWidth,omitempty"`
18	VideoHeight  int    `json:"videoHeight,omitempty"`
19	Channels     []struct {
20		ID       int  `json:"id"`
21		Width    int  `json:"width"`
22		Height   int  `json:"height"`
23		Enabled  bool `json:"enabled"`
24		FPS      int  `json:"fps"`
25		Bitrate  int  `json:"bitrate"`
26		IsRTSP   bool `json:"isRtspEnabled"`
27	} `json:"channels,omitempty"`
28}
29
30// Resolution returns the best available resolution for the camera.
31func (c *Camera) Resolution() (width, height int) {
32	if c.VideoWidth > 0 && c.VideoHeight > 0 {
33		return c.VideoWidth, c.VideoHeight
34	}
35	// Fall back to first enabled channel
36	for _, ch := range c.Channels {
37		if ch.Enabled && ch.Width > 0 {
38			return ch.Width, ch.Height
39		}
40	}
41	return 1920, 1080 // Default assumption
42}
43
44// NVR represents the UniFi Protect NVR.
45type NVR struct {
46	ID            string `json:"id"`
47	Name          string `json:"name"`
48	Host          string `json:"host"`
49	Mac           string `json:"mac"`
50	Version       string `json:"version"`
51	FirmwareVer   string `json:"firmwareBuild"`
52	IsConnected   bool   `json:"isConnectedToCloud"`
53	EnableAutoBkp bool   `json:"enableAutomaticBackups"`
54}
55
56// BootstrapResponse is the response from /api/bootstrap.
57type BootstrapResponse struct {
58	NVR     NVR      `json:"nvr"`
59	Cameras []Camera `json:"cameras"`
60}
61
62// Event represents a motion/smart detection event.
63type Event struct {
64	ID        string `json:"id"`
65	Type      string `json:"type"`
66	Start     int64  `json:"start"`     // Unix milliseconds
67	End       int64  `json:"end"`       // Unix milliseconds
68	Score     int    `json:"score"`     // 0-100 confidence
69	Camera    string `json:"camera"`    // Camera ID
70	Partition string `json:"partition"` // Storage partition
71	SmartTypes []string `json:"smartDetectTypes,omitempty"`
72}
73
74// StartTime returns the event start as a time.Time.
75func (e *Event) StartTime() time.Time {
76	return time.UnixMilli(e.Start)
77}
78
79// EndTime returns the event end as a time.Time.
80func (e *Event) EndTime() time.Time {
81	return time.UnixMilli(e.End)
82}
83
84// Duration returns the event duration.
85func (e *Event) Duration() time.Duration {
86	return time.Duration(e.End-e.Start) * time.Millisecond
87}
88
89// EventsResponse is the response from /api/events when pagination is used.
90type EventsResponse []Event