-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrecorder.go
209 lines (188 loc) · 3.85 KB
/
recorder.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
package gogadgets
import (
"bytes"
"encoding/json"
"fmt"
"log"
"net/http"
"time"
)
type summary struct {
start time.Time
n int
v float64
}
//Recorder takes all the update messages it receives and saves them
//by posting to quimby
type Recorder struct {
url string
token string
status bool
filter []string
summaries map[string]time.Duration
history map[string]summary
}
func NewRecorder(pin *Pin) (OutputDevice, error) {
s := getSummaries(pin.Args["summarize"])
r := &Recorder{
url: pin.Args["host"].(string),
token: pin.Args["token"].(string),
filter: getFilter(pin.Args["filter"]),
history: map[string]summary{},
summaries: s,
}
return r, nil
}
func (r *Recorder) Commands(location, name string) *Commands {
return nil
}
func getSummaries(s interface{}) map[string]time.Duration {
if s == nil {
return map[string]time.Duration{}
}
d, _ := json.Marshal(s)
vals := map[string]int{}
err := json.Unmarshal(d, &vals)
out := map[string]time.Duration{}
if err != nil {
log.Println("WARNING, could not parse recorder summaires", s)
return out
}
for key, val := range vals {
var d time.Duration
d = time.Duration(val) * time.Minute
out[key] = d
}
return out
}
func (r *Recorder) Update(msg *Message) bool {
if r.status && msg.Type == "update" {
r.save(msg)
}
return false
}
func (r *Recorder) On(val *Value) error {
r.status = true
return nil
}
func (r *Recorder) Off() error {
r.status = false
return nil
}
func (r *Recorder) Status() map[string]bool {
return map[string]bool{"recorder": r.status}
}
func (r *Recorder) save(msg *Message) {
if len(r.filter) > 0 {
if !r.inFilter(msg) {
return
}
}
d, ok := r.summaries[msg.Sender]
if ok {
r.summarize(msg, d)
} else {
r.doSave(msg)
}
}
func (r *Recorder) inFilter(msg *Message) bool {
for _, item := range r.filter {
if msg.Sender == item {
return true
}
}
return false
}
func (r *Recorder) summarize(msg *Message, duration time.Duration) {
now := time.Now().UTC()
s, ok := r.history[msg.Sender]
if !ok {
s = summary{start: now}
}
s.n += 1
f, _ := msg.Value.ToFloat()
s.v += f
lapsed := now.Sub(s.start)
if lapsed >= duration {
msg.Value.Value = s.v / float64(s.n)
r.doSave(msg)
delete(r.history, msg.Sender)
} else {
r.history[msg.Sender] = s
}
}
func (r *Recorder) doSave(msg *Message) {
vals, ok := r.getValue(msg)
if !ok {
return
}
for _, v := range vals {
u := fmt.Sprintf(r.url, msg.Location, msg.Name+v.name)
r.doSaveVal(v.val, u)
}
}
func (r *Recorder) doSaveVal(m map[string]float64, u string) {
buf := bytes.Buffer{}
enc := json.NewEncoder(&buf)
enc.Encode(m)
req, err := http.NewRequest("POST", u, &buf)
if err != nil {
log.Println("couldn't post data", err)
return
}
req.Header.Add("Authorization", r.token)
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Println("couldn't post data", err)
return
}
resp.Body.Close()
}
type dataPoint struct {
name string
val map[string]float64
}
func (r *Recorder) getValue(msg *Message) ([]dataPoint, bool) {
if msg.Info.Direction == "output" {
return r.getOutputValue(msg)
}
return r.getInputValue(msg)
}
func (r *Recorder) getOutputValue(msg *Message) ([]dataPoint, bool) {
var o []dataPoint
var ok bool
for k, v := range msg.Value.Output {
ok = true
val := dataPoint{
val: map[string]float64{"value": bTof(v)},
name: fmt.Sprintf(" %s", k),
}
o = append(o, val)
}
return o, ok
}
func (r *Recorder) getInputValue(msg *Message) ([]dataPoint, bool) {
v, ok := msg.Value.Value.(float64)
if !ok {
return nil, ok
}
return []dataPoint{
{val: map[string]float64{"value": v}},
}, true
}
func getFilter(f interface{}) []string {
if f == nil {
return []string{}
}
filters, ok := f.([]string)
if !ok {
return []string{}
}
return filters
}
func bTof(b bool) float64 {
if b {
return 1.0
}
return 0.0
}