-
Notifications
You must be signed in to change notification settings - Fork 1
/
alarmBuilder.go
121 lines (102 loc) · 3.29 KB
/
alarmBuilder.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
package c8y
import (
"encoding/json"
)
// AlarmBuilder represents a alarm where the mandatory properties are set via its constructor NewAlarmBuilder
type AlarmBuilder struct {
data map[string]interface{}
}
// NewAlarmBuilder returns a new alarm builder with the required fields set.
// The alarm will have a timestamp set to Now(). The timestamp can be set to another timestamp by using SetTimestamp()
// The alarm will have a default severity of MAJOR, but it can be changed by using
// .SetSeverity functions
func NewAlarmBuilder(deviceID string, typeName string, text string) *AlarmBuilder {
b := &AlarmBuilder{
data: map[string]interface{}{},
}
b.SetTimestamp(nil)
b.SetType(typeName)
b.SetText(text)
b.SetDeviceID(deviceID)
b.SetSeverityMajor() // Set default severity to MAJOR
return b
}
// MarshalJSON returns the given event in json format
func (b AlarmBuilder) MarshalJSON() ([]byte, error) {
return json.Marshal(b.data)
}
// Severity returns the alarm severity
func (b AlarmBuilder) Severity() string {
if v, ok := b.data["severity"].(string); ok {
return v
}
return ""
}
// SetSeverityMajor sets the alarm severity to Major
func (b *AlarmBuilder) SetSeverityMajor() *AlarmBuilder {
return b.Set("severity", AlarmSeverityMajor)
}
// SetSeverityMinor sets the alarm severity to Minor
func (b *AlarmBuilder) SetSeverityMinor() *AlarmBuilder {
return b.Set("severity", AlarmSeverityMinor)
}
// SetSeverityCritical sets the alarm severity to Critical
func (b *AlarmBuilder) SetSeverityCritical() *AlarmBuilder {
return b.Set("severity", AlarmSeverityCritical)
}
// SetSeverityWarning sets the alarm severity to Warning
func (b *AlarmBuilder) SetSeverityWarning() *AlarmBuilder {
return b.Set("severity", AlarmSeverityWarning)
}
// DeviceID returns the device id of the alarm
func (b AlarmBuilder) DeviceID() string {
if v, ok := b.data["source"].(Source); ok {
return v.ID
}
return ""
}
// SetDeviceID sets the device id for the alarm
func (b *AlarmBuilder) SetDeviceID(ID string) *AlarmBuilder {
return b.Set("source", Source{
ID: ID,
})
}
// Text returns the device id of the alarm
func (b AlarmBuilder) Text() string {
return b.data["text"].(string)
}
// SetText sets the alarm text
func (b *AlarmBuilder) SetText(ID string) *AlarmBuilder {
return b.Set("text", ID)
}
// Type returns the alarm type
func (b AlarmBuilder) Type() string {
return b.data["type"].(string)
}
// SetType sets the alarm type
func (b *AlarmBuilder) SetType(ID string) *AlarmBuilder {
return b.Set("type", ID)
}
// Timestamp returns the timestamp of the alarm
func (b AlarmBuilder) Timestamp() Timestamp {
return *b.data["time"].(*Timestamp)
}
// SetTimestamp sets the timestamp when the event was created. If the value is nil, then the current timestamp will be used
func (b *AlarmBuilder) SetTimestamp(value *Timestamp) *AlarmBuilder {
if value == nil {
value = NewTimestamp()
}
return b.Set("time", value)
}
// Set sets the name property with the given value
func (b *AlarmBuilder) Set(name string, value interface{}) *AlarmBuilder {
b.data[name] = value
return b
}
// Get returns the given property value. If the property does not exist, then the second parameter will be set to false
func (b AlarmBuilder) Get(name string) (interface{}, bool) {
if v, ok := b.data[name]; ok {
return v, true
}
return nil, false
}