/
exportStore.go
71 lines (54 loc) · 1.36 KB
/
exportStore.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
package main
import (
"context"
"time"
"github.com/patrickalin/nest-client-go/pkg/ring"
nest "github.com/patrickalin/nest-api-go"
"github.com/sirupsen/logrus"
)
type store struct {
in chan nest.Nest
stores map[string]*ring.Ring
}
type measure struct {
Timestamp time.Time
value float64
}
func (m measure) TimeStamp() time.Time {
return m.Timestamp
}
/**
* Measure represents a measure that has a GetValue
*/
func (m measure) Value() float64 {
return m.value
}
//InitConsole listen on the chanel
func createStore(messages chan nest.Nest) (store, error) {
stores := make(map[string]*ring.Ring)
stores["temperatureCelsius"] = &ring.Ring{}
stores["humidity"] = &ring.Ring{}
return store{in: messages, stores: stores}, nil
}
func (c *store) listen(context context.Context) {
go func() {
log.WithFields(logrus.Fields{
"fct": "exportStore.listen",
}).Info("init")
for {
msg := <-c.in
log.WithFields(logrus.Fields{
"fct": "exportStore.listen",
}).Debug("Receive message")
c.stores["temperatureCelsius"].Enqueue(measure{time.Now(), msg.GetAmbientTemperatureC()})
c.stores["humidity"].Enqueue(measure{time.Now(), msg.GetHumidity()})
}
}()
}
func (c *store) GetValues(name string) []ring.TimeMeasure {
return c.stores[name].Values()
}
func (c *store) String(name string) string {
s, _ := c.stores[name].DumpLine()
return s
}