-
Notifications
You must be signed in to change notification settings - Fork 2
/
exportStore.go
96 lines (75 loc) · 2.37 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
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
package main
import (
"context"
"strconv"
"time"
"github.com/patrickalin/bloomsky-client-go/pkg/ring"
bloomsky "github.com/patrickalin/bloomsky-api-go"
"github.com/sirupsen/logrus"
)
type store struct {
in chan bloomsky.Bloomsky
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 bloomsky.Bloomsky) (store, error) {
stores := make(map[string]*ring.Ring)
stores["temperatureCelsius"] = &ring.Ring{}
stores["pressureHPa"] = &ring.Ring{}
stores["windGustkmh"] = &ring.Ring{}
stores["windSustainedSpeedkmh"] = &ring.Ring{}
stores["humidity"] = &ring.Ring{}
stores["rainDailyMm"] = &ring.Ring{}
stores["rainMm"] = &ring.Ring{}
stores["rainRate"] = &ring.Ring{}
stores["indexUV"] = &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 {
select {
case msg := <-c.in:
log.WithFields(logrus.Fields{
"fct": "exportStore.listen",
}).Debug("Receive message")
c.stores["temperatureCelsius"].Enqueue(measure{time.Now(), msg.GetTemperatureCelsius()})
c.stores["pressureHPa"].Enqueue(measure{time.Now(), msg.GetPressureHPa()})
c.stores["windGustkmh"].Enqueue(measure{time.Now(), msg.GetWindGustkmh()})
c.stores["windSustainedSpeedkmh"].Enqueue(measure{time.Now(), msg.GetSustainedWindSpeedkmh()})
c.stores["humidity"].Enqueue(measure{time.Now(), msg.GetHumidity()})
c.stores["rainDailyMm"].Enqueue(measure{time.Now(), msg.GetRainDailyMm()})
c.stores["rainMm"].Enqueue(measure{time.Now(), msg.GetRainMm()})
c.stores["rainRate"].Enqueue(measure{time.Now(), msg.GetRainRateMm()})
i, err := strconv.ParseFloat(msg.GetIndexUV(), 64)
checkErr(err, funcName(), "impossible to convert string to int")
c.stores["indexUV"].Enqueue(measure{time.Now(), i})
case <-context.Done():
return
}
}
}()
}
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
}