forked from dim13/unifi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alarm.go
81 lines (62 loc) · 1.64 KB
/
alarm.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
// Copyright (c) 2014 The unifi Authors. All rights reserved.
// Use of this source code is governed by ISC-style license
// that can be found in the LICENSE file.
package unifi
import (
"encoding/json"
"fmt"
)
type AlarmFilter struct {
Limit int `json:"_limit"`
Start int `json:"_start"`
Withcount bool `json:"withcount"`
Archived bool `json:"archived"`
}
type RawAlarm struct {
Data json.RawMessage
Key string
}
// Returns a slice of json RawDevices as received by the controller
func (u *Unifi) RawAlarms(site *Site, filter interface{}) ([]RawAlarm, error) {
var rawAlarms []RawAlarm
// Response from controller
var response struct {
Data []json.RawMessage
Meta meta
}
err := u.parse(site, "stat/alarm", filter, &response)
if err != nil {
return nil, err
}
for _, d := range response.Data {
// unmarshal into a map to check the "type" field
var obj map[string]interface{}
err := json.Unmarshal(d, &obj)
if err != nil {
return nil, err
}
alarmKey, ok := obj["key"].(string)
if !ok {
return nil, fmt.Errorf("Error on retrieving object type from raw Json")
}
var ra RawAlarm
ra.Key = alarmKey
ra.Data = d
rawAlarms = append(rawAlarms, ra)
}
return rawAlarms, err
}
// Returns a slice of json RawDevices as received by the controller
func (u *Unifi) BasicAlarms(site *Site, filter interface{}) ([]BasicEvent, error) {
var basicAlarms []BasicEvent
rawAlarms, err := u.RawAlarms(site, filter)
for _, ra := range rawAlarms {
var ba BasicEvent
err := json.Unmarshal(ra.Data, &ba)
if err != nil {
return nil, err
}
basicAlarms = append(basicAlarms, ba)
}
return basicAlarms, err
}