-
Notifications
You must be signed in to change notification settings - Fork 1
/
monitor.go
275 lines (233 loc) · 6.89 KB
/
monitor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package zoneminder
import (
"fmt"
"log"
"reflect"
"time"
)
const monitorURL = "/api/monitors"
type Monitors []Monitor
type monitors struct {
Monitors []monitorPlaceHolder
}
type monitorPlaceHolder struct {
Monitor Monitor
}
// Monitor is the monitor object from zoneminder
type Monitor struct {
*Client
ID int `json:",string"`
Name string
ServerID int `json:",string"`
Type string
Function string
Enabled int `json:",string"`
LinkedMonitors string
Triggers string
Device string
Channel int `json:",string"`
Format int `json:",string"`
V4LMultiBuffer bool
V4LCapturesPerFrame int `json:",string"`
Protocol string
Method string
Host string
Port int `json:",string"`
SubPath string
Path string
Options string
User string
Pass string
Width int `json:",string"`
Height int `json:",string"`
Colours int `json:",string"`
Palette int `json:",string"`
Orientation int `json:",string"`
Deinterlacing int `json:",string"`
RTSPDescribe bool
Brightness int `json:",string"`
Contrast int `json:",string"`
Hue int `json:",string"`
Colour int `json:",string"`
EventPrefix string
LabelFormat string
LabelX int `json:",string"`
LabelY int `json:",string"`
LabelSize int `json:",string"`
ImageBufferCount int `json:",string"`
WarmupCount int `json:",string"`
PreEventCount int `json:",string"`
PostEventCount int `json:",string"`
StreamReplayBuffer int `json:",string"`
AlarmFrameCount int `json:",string"`
SectionLength int `json:",string"`
FrameSkip int `json:",string"`
MotionFrameSkip int `json:",string"`
AnalysisFPS float64 `json:",string"`
AnalysisUpdateDelay int `json:",string"`
MaxFPS float64 `json:",string"`
AlarmMaxFPS float64 `json:",string"`
FPSReportInterval int `json:",string"`
RefBlendPerc int `json:",string"`
AlarmRefBlendPerc int `json:",string"`
Controllable int `json:",string"`
ControlID int `json:",string"`
ControlDevice string
ControlAddress string
AutoStopTimeout string
TrackMotion int `json:",string"`
TrackDelay int `json:",string"`
ReturnLocation int `json:",string"`
ReturnDelay int `json:",string"`
DefaultView string
DefaultRate int `json:",string"`
DefaultScale int `json:",string"`
SignalCheckColour string
WebColour string
Exif bool
Sequence int `json:",string"`
eventMonitorT *time.Ticker
}
type MonitorOpts struct {
Name string
Function string
Protocol string
Method string
Host string
Port int
Path string
Width int
Height int
Colours int
}
func (m *Monitors) GetByID(id int) Monitor {
for _, mon := range *m {
if mon.ID == id {
return mon
}
}
return Monitor{}
}
func (m *Monitors) GetByName(name string) Monitor {
for _, mon := range *m {
if mon.Name == name {
return mon
}
}
return Monitor{}
}
func (c *Client) GetMonitors() (Monitors, error) {
monitorResponse, err := c.httpGet(fmt.Sprintf("%s.json", monitorURL), new(monitors))
if err != nil {
return Monitors{}, err
}
m := make(Monitors, len(monitorResponse.(*monitors).Monitors))
for _, monitor := range monitorResponse.(*monitors).Monitors {
monitor.Monitor.Client = c
m = append(m, monitor.Monitor)
}
return m, nil
}
func (c *Client) GetMonitorById(ID int) (Monitor, error) {
monitor, err := c.httpGet(fmt.Sprintf("%s/%d.json", monitorURL, ID), new(Monitor))
if err != nil {
return Monitor{}, err
}
return monitor.(Monitor), nil
}
func (m *Monitor) Refresh() (Monitor, error) {
return m.Client.GetMonitorById(m.ID)
}
func (m *Monitor) Enable() error {
postData := map[string]string{
"Monitor[Enabled]": "1",
}
_, err := m.Client.httpPost(fmt.Sprintf("%s/%d.json", monitorURL, m.ID), postData, nil)
return err
}
func (m *Monitor) Disable() error {
postData := map[string]string{
"Monitor[Enabled]": "0",
}
_, err := m.Client.httpPost(fmt.Sprintf("%s/%d.json", monitorURL, m.ID), postData, nil)
return err
}
func (m *Monitor) SetFunction(f string) error {
postData := map[string]string{
"Monitor[Function]": f,
}
_, err := m.Client.httpPost(fmt.Sprintf("%s/%d.json", monitorURL, m.ID), postData, nil)
return err
}
func (c *Client) AddMonitor(opts MonitorOpts) (Monitor, error) {
postData := make(map[string]string)
v := reflect.ValueOf(opts).Elem()
for i := 0; i < v.NumField(); i++ {
f := v.Field(i)
if f.Interface() == nil {
continue
}
postData["Monitor["+v.Type().Field(i).Name+"]"] = f.Interface().(string)
}
_, err := c.httpPost(fmt.Sprintf("%s.json", monitorURL), postData, nil)
return Monitor{}, err
}
func (m *Monitor) Edit(opts MonitorOpts) error {
postData := make(map[string]string)
v := reflect.ValueOf(opts).Elem()
for i := 0; i < v.NumField(); i++ {
f := v.Field(i)
if f.Interface() == nil {
continue
}
postData["Monitor["+v.Type().Field(i).Name+"]"] = f.Interface().(string)
}
_, err := m.Client.httpPut(fmt.Sprintf("%s/%d.json", monitorURL, m.ID), postData, nil)
return err
}
func (m *Monitor) ForceAlarm() error {
_, err := m.Client.httpGet(fmt.Sprintf("%s/alarm/id:%d/command:on.json", monitorURL, m.ID), nil)
return err
}
func (m *Monitor) StopAlarm() error {
_, err := m.Client.httpGet(fmt.Sprintf("%s/alarm/id:%d/command:off.json", monitorURL, m.ID), nil)
return err
}
func (m *Monitor) AlarmStatus() (int, error) {
type status struct {
Status int `json:"status,string"`
}
s, err := m.Client.httpGet(fmt.Sprintf("%s/alarm/id:%d/command:status.json", monitorURL, m.ID), new(status))
return s.(*status).Status, err
}
func (m *Monitor) GetEvents(opts *EventOpts) (Events, error) {
if opts == nil {
opts = &EventOpts{}
}
opts.MonitorID = m.ID
return m.Client.GetEvents(opts)
}
func (m *Monitor) MonitorForEvents(eventType string, interval int, eventChan chan Events) {
m.eventMonitorT = time.NewTicker(time.Duration(interval) * time.Second)
go func() {
for range m.eventMonitorT.C {
events, err := m.GetEvents(&EventOpts{
Cause: eventType,
StartTime: time.Now().Add(-time.Duration(interval) * time.Second),
StartTimeOperation: ">=",
})
if err != nil {
log.Println("Error getting events for monitor.", err)
continue
}
eventChan <- events
}
}()
}
func (m *Monitor) StopEventMonitoring() {
if m.eventMonitorT != nil {
m.eventMonitorT.Stop()
} else {
log.Println("No event monitor currently running")
}
}