-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathevents.go
More file actions
538 lines (433 loc) · 12.9 KB
/
Copy pathevents.go
File metadata and controls
538 lines (433 loc) · 12.9 KB
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
package securityspy
import (
"bufio"
"bytes"
"context"
"errors"
"fmt"
"io"
"net/url"
"strconv"
"strings"
"time"
)
// String provides a description of an event.
func (e *Event) String() string {
txt := EventName(e.Type)
if txt == "" {
return UnknownEventText
}
return txt
}
/* Events methods follow. */
// BindFunc binds a call-back function to an Event in SecuritySpy.
// Use this to receive incoming events via a callback method in a go routine.
func (e *Events) BindFunc(event EventType, callBack func(Event)) {
if callBack == nil {
return
}
e.binds.Lock()
defer e.binds.Unlock()
if val, ok := e.eventBinds[event]; ok {
e.eventBinds[event] = append(val, callBack)
return
}
e.eventBinds[event] = []func(Event){callBack}
}
// BindChan binds a receiving channel to an Event in SecuritySpy.
// Use this to receive incoming events over a channel.
// Avoid using unbuffered channels as they may block further event processing.
func (e *Events) BindChan(event EventType, channel chan Event) {
if channel == nil {
return
}
e.chans.Lock()
defer e.chans.Unlock()
if val, ok := e.eventChans[event]; ok {
e.eventChans[event] = append(val, channel)
return
}
e.eventChans[event] = []chan Event{channel}
}
// Stop stops Watch() loops and disconnects from the event stream.
// No further callback messages will fire after this is called.
// Closes all channels that were passed to BindChan if closeChans=true.
// Stop writing to the channels with Custom() before calling Stop().
func (e *Events) Stop(closeChans bool) {
e.mu.Lock()
running := e.Running
cancel := e.cancel
stream := e.stream
e.Running = false
e.cancel = nil
e.ctx = nil
e.stream = nil
e.mu.Unlock()
if running {
if cancel != nil {
cancel()
}
if stream != nil {
_ = stream.Close()
}
e.wg.Wait()
}
if !closeChans {
return
}
e.chans.Lock()
defer e.chans.Unlock()
closed := make(map[chan Event]struct{})
for _, chans := range e.eventChans {
for idx := range chans {
if _, ok := closed[chans[idx]]; ok {
continue
}
close(chans[idx])
closed[chans[idx]] = struct{}{}
}
}
}
// UnbindAll removes all event bindings and channels.
func (e *Events) UnbindAll() {
e.binds.Lock()
e.chans.Lock()
defer func() {
e.binds.Unlock()
e.chans.Unlock()
}()
e.eventBinds = make(map[EventType][]func(Event))
e.eventChans = make(map[EventType][]chan Event)
}
// UnbindChan removes all bound channels for a particular event.
func (e *Events) UnbindChan(event EventType) {
e.chans.Lock()
defer e.chans.Unlock()
delete(e.eventChans, event)
}
// UnbindFunc removes all bound callbacks for a particular event.
// EventType is a set of constants that begin with Event*.
func (e *Events) UnbindFunc(event EventType) {
e.binds.Lock()
defer e.binds.Unlock()
delete(e.eventBinds, event)
}
// Watch kicks off the routines to watch the eventStream and fire callback bindings.
// If your application relies on event stream messages, call this at least once
// to connect the stream. If you have no call back functions or channels then do not
// call this. Call Stop() to close the connection when you're done with it.
func (e *Events) Watch(retryInterval time.Duration, refreshOnConfigChange bool) {
e.mu.Lock()
defer e.mu.Unlock()
if e.Running {
return
}
e.ctx, e.cancel = context.WithCancel(context.Background())
watchCtx := e.ctx
e.eventChan = make(chan *Event, EventBuffer)
if e.callbackSem == nil {
e.callbackSem = make(chan struct{}, maxCallbackWorkers)
}
e.Running = true
e.wg.Go(func() { e.eventStreamSelector(watchCtx, refreshOnConfigChange) })
e.wg.Go(func() { e.eventStreamScanner(watchCtx, retryInterval) })
}
// Custom fires an event into the running event Watcher. Any functions or
// channels bound to the CUSTOM Event type will also be called.
func (e *Events) Custom(cameraNum int, msg string) {
e.custom(EventStreamCustom, -11000, cameraNum, msg)
}
// custom allows a quick way to make events.
func (e *Events) custom(eventType EventType, eventID, cam int, msg string) {
now := time.Now().Round(time.Second)
var camera *Camera
if cams := e.server.GetCameras(); cams != nil {
camera = cams.ByNum(cam)
}
e.enqueue(&Event{
Time: now,
When: now,
ID: eventID,
Msg: string(eventType) + " " + msg,
Type: eventType,
Camera: camera,
})
}
/* INTERFACE HELPER METHODS FOLLOW */
// eventStreamScanner connects to the securityspy event stream and fires events into a channel.
// EventStreamDisconnect is only emitted after a live stream ends, not on failed dials.
//
//nolint:cyclop // but it runs forever!
func (e *Events) eventStreamScanner(ctx context.Context, retryInterval time.Duration) {
for {
if ctx.Err() != nil {
return
}
stream, err := e.eventStreamConnect(ctx)
if err != nil {
select {
case <-ctx.Done():
return
case <-time.After(retryInterval):
continue
}
}
scanner := bufio.NewScanner(stream)
scanner.Split(scanLinesCR)
for scanner.Scan() {
// Constantly scan for new events, then report them to the event channel.
if text := scanner.Text(); strings.Count(text, " ") > 2 { //nolint:mnd // we need at least 2.
e.enqueue(e.UnmarshalEvent(text))
}
if ctx.Err() != nil {
break
}
}
err = scanner.Err()
_ = stream.Close()
e.clearStream(stream)
if ctx.Err() != nil {
return
}
msg := "Connection Closed"
if err != nil && !errors.Is(err, ErrDisconnect) {
msg = err.Error()
}
e.custom(EventStreamDisconnect, -10000, -1, msg)
select {
case <-ctx.Done():
return
case <-time.After(retryInterval):
}
}
}
// eventStreamConnect establishes a connection to the event stream and passes off the http Reader.
func (e *Events) eventStreamConnect(ctx context.Context) (io.ReadCloser, error) {
client := e.server.HTTPClient()
client.Timeout = 0
resp, err := e.server.GetContextClient(ctx, "++eventStream", url.Values{"version": []string{"3"}}, client)
if err != nil {
return nil, fmt.Errorf("connecting event stream: %w", err)
}
e.mu.Lock()
e.stream = resp.Body
e.mu.Unlock()
e.custom(EventStreamConnect, -9999, -1, EventName(EventStreamConnect))
return resp.Body, nil
}
// eventStreamSelector watches the event channel.
// Fires bound event call back functions.
// Also reconnects to the event stream if the connection fails.
// There is a "loop" that occurs among the eventStream* methods.
// Stop() properly handles the shutdown of the loop, so if can be safely restarted w/ Watch().
func (e *Events) eventStreamSelector(ctx context.Context, refreshOnConfigChange bool) { //nolint:cyclop // oh well?
for {
var (
event *Event
ok bool //nolint:varnamelen // ok is a valid variable name.
)
select {
case <-ctx.Done():
return
case event, ok = <-e.eventChan:
if !ok {
return
}
}
if event.Type == EventConfigChange && refreshOnConfigChange {
e.serverRefresh(ctx)
}
for _, callback := range e.callbacksFor(event.Type) {
if callback == nil {
continue
}
callbackFn := callback
select {
case e.callbackSem <- struct{}{}:
go func() {
defer func() { <-e.callbackSem }()
callbackFn(*event)
}()
default:
}
}
for _, ch := range e.channelsFor(event.Type) {
select {
case ch <- *event:
default:
}
}
}
}
func (e *Events) serverRefresh(ctx context.Context) {
if err := e.server.RefreshContext(ctx); err != nil {
e.custom(EventWatcherRefreshFail, -9997, -1, err.Error())
return
}
e.custom(EventWatcherRefreshed, -9998, -1, EventName(EventWatcherRefreshed))
}
/* Example Event Stream Flow:
(new, v5)
20190927092026 3 3 CLASSIFY HUMAN 99
20190927092026 4 3 TRIGGER_M 9
20190927092036 5 3 CLASSIFY HUMAN 5 VEHICLE 95
20190927092040 5 X NULL
20190927092050 6 3 FILE /Volumes/VolName/Cam/2019-07-26/26-07-2019 15-52-00 C Cam.m4v
20190927092055 7 3 DISARM_M
20190927092056 8 3 OFFLINE
*/
// UnmarshalEvent turns raw text into an Event that can fire callbacks.
// You generally shouldn't need to call this method, it's exposed for convenience.
/* [TIME] is specified in the order: "year, month, day, hour, minute, second" and is always 14 characters long.
* [EVENT NUMBER] increases by 1 for each subsequent event.
* [CAMERA NUMBER] specifies the camera that this event relates to, for example CAM15 for camera number 15.
* [EVENT] describes the event: ARM_C, DISARM_C, ARM_M, DISARM_M, ARM_A, DISARM_A, ERROR,
CONFIGCHANGE, MOTION, OFFLINE, ONLINE */
//
//nolint:cyclop,funlen,mnd // Events are hard.
func (e *Events) UnmarshalEvent(text string) *Event {
var (
err error
parts = strings.SplitN(text, " ", 4) //nolint:mnd // events have 4 parts...
newEvent = &Event{Msg: text, ID: -1, Time: time.Now()}
// Parse the time stamp; append the Offset from ++systemInfo to get the right time-location.
eventTime string
)
if len(parts) < 4 {
newEvent.Errors = append(newEvent.Errors, ErrUnknownEvent)
newEvent.Type = EventUnknownEvent
return newEvent
}
newEvent.Msg = parts[3]
gmtOffset := 0.0
if info := e.server.GetInfo(); info != nil {
gmtOffset = info.GmtOffset.Hours()
}
eventTime = fmt.Sprintf("%v%+03.0f", parts[0], gmtOffset)
//nolint:gosmopolitan // The event stream uses the system's local time.
if newEvent.When, err = time.ParseInLocation(EventTimeFormat+"-07", eventTime, time.Local); err != nil {
newEvent.When = time.Now()
newEvent.Errors = append(newEvent.Errors, ErrDateParseFail)
}
// Parse the ID
if newEvent.ID, err = strconv.Atoi(parts[1]); err != nil {
newEvent.ID = BadID
newEvent.Errors = append(newEvent.Errors, ErrIDParseFail)
}
// Parse the camera number.
parts[2] = strings.TrimPrefix(parts[2], "CAM")
if cams := e.server.GetCameras(); parts[2] != "X" && cams != nil {
if cameraNum, err := strconv.Atoi(parts[2]); err != nil {
newEvent.Errors = append(newEvent.Errors, ErrCAMParseFail)
} else if newEvent.Camera = cams.ByNum(cameraNum); newEvent.Camera == nil {
newEvent.Errors = append(newEvent.Errors, ErrCAMMissing)
}
}
// Parse and convert the type string to EventType.
parts = strings.Split(newEvent.Msg, " ")
newEvent.Type = EventType(parts[0])
// Check if the type we just converted is a known event.
if name := EventName(newEvent.Type); name == "" {
newEvent.Errors = append(newEvent.Errors, ErrUnknownEvent)
newEvent.Type = EventUnknownEvent
}
if newEvent.Type == EventClassify && len(parts) > 1 {
parseClassifyFields(parts[1:], newEvent)
}
// If this is a trigger-type event, add the trigger reason(s)
if (newEvent.Type == EventTriggerAction || newEvent.Type == EventTriggerMotion) && len(parts) == 2 {
b, _ := strconv.Atoi(parts[1])
msg := ""
// Check if this bitmask contains any of our known reasons.
for flag, txt := range Reasons() {
if b&int(flag) != 0 {
if msg != "" {
msg += ", "
}
newEvent.Reasons = append(newEvent.Reasons, flag)
msg += txt
}
}
newEvent.Msg += " - Reasons: " + msg
if msg == "" {
newEvent.Msg += UnknownReasonText
}
}
return newEvent
}
func parseClassifyFields(parts []string, event *Event) {
for idx := 0; idx+1 < len(parts); idx += 2 {
value, err := strconv.Atoi(parts[idx+1])
if err != nil {
continue
}
switch parts[idx] {
case "HUMAN":
event.ClassifyHuman = value
case "VEHICLE":
event.ClassifyVehicle = value
case "ANIMAL":
event.ClassifyAnimal = value
}
}
}
func (e *Events) enqueue(event *Event) {
if event == nil {
return
}
e.mu.RLock()
chn := e.eventChan
ctx := e.ctx
running := e.Running
e.mu.RUnlock()
if !running || chn == nil || ctx == nil {
return
}
select {
case chn <- event:
case <-ctx.Done():
}
}
func (e *Events) clearStream(stream io.ReadCloser) {
e.mu.Lock()
defer e.mu.Unlock()
if e.stream == stream {
e.stream = nil
}
}
func (e *Events) callbacksFor(eventType EventType) []func(Event) {
e.binds.RLock()
defer e.binds.RUnlock()
callbacks := make([]func(Event), 0, len(e.eventBinds[eventType])+len(e.eventBinds[EventAllEvents])+1)
if vals, ok := e.eventBinds[eventType]; ok {
callbacks = append(callbacks, vals...)
} else if eventType != EventUnknownEvent {
callbacks = append(callbacks, e.eventBinds[EventUnknownEvent]...)
}
callbacks = append(callbacks, e.eventBinds[EventAllEvents]...)
return callbacks
}
func (e *Events) channelsFor(eventType EventType) []chan Event {
e.chans.RLock()
defer e.chans.RUnlock()
channels := make([]chan Event, 0, len(e.eventChans[eventType])+len(e.eventChans[EventAllEvents]))
channels = append(channels, e.eventChans[eventType]...)
channels = append(channels, e.eventChans[EventAllEvents]...)
return channels
}
// scanLinesCR is a custom bufio.Scanner to read SecuritySpy eventStream.
func scanLinesCR(data []byte, atEOF bool) (int, []byte, error) {
if atEOF && len(data) == 0 {
return 0, nil, ErrDisconnect
}
if i := bytes.IndexByte(data, '\r'); i >= 0 {
// We have a full CR-terminated line.
return i + 1, data[0:i], nil
}
// If we're at EOF, we have a final, non-terminated line. Return it.
if atEOF {
return len(data), data, io.ErrShortBuffer
}
// Request more data.
return 0, nil, nil
}