-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmain.go
172 lines (148 loc) · 3.72 KB
/
main.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
package main
import (
"context"
"net/http"
"os"
"time"
"github.com/XciD/loxone-prometheus-exporter/config"
loxone "github.com/XciD/loxone-ws"
"github.com/bep/debounce"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
log "github.com/sirupsen/logrus"
)
var (
changes = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "loxone_changes",
Help: "Number of changes",
},
[]string{"control", "room", "type", "cat", "state"},
)
values = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "loxone_values",
Help: "Current Value of changes",
},
[]string{"control", "room", "type", "cat", "state"},
)
)
func main() {
ctx := context.Background()
log.SetOutput(os.Stdout)
log.SetLevel(log.InfoLevel)
log.SetFormatter(&log.TextFormatter{
FullTimestamp: true,
})
// Read config
cfg, err := config.NewConfig()
if err != nil {
log.Error(err)
return
}
// Start prometheus server
http.Handle("/metrics", promhttp.Handler())
go http.ListenAndServe(":8080", nil)
prometheus.MustRegister(changes)
prometheus.MustRegister(values)
// Open socket
lox, err := loxone.New(cfg.Host, cfg.Port, cfg.User, cfg.Password)
if err != nil {
log.Error(err)
return
}
// Get config
loxoneConfig, err := lox.GetConfig()
if err != nil {
log.Error(err)
return
}
log.Info("Get Config OK")
// Register events
err = lox.RegisterEvents()
if err != nil {
log.Error(err)
return
}
log.Info("RegisterEvents OK")
// Build Control Map by states
globalStates := make(map[string]*eventMetric)
for _, control := range loxoneConfig.Controls {
labels := map[string]string{
"control": control.Name,
"room": loxoneConfig.RoomName(control.Room),
"type": control.Type,
"cat": loxoneConfig.CatName(control.Cat),
"state": "",
}
for stateName, stateValue := range control.States {
// Can be a string or a float...
switch stateValue := stateValue.(type) {
case string:
// Create the target map
currentLabel := prometheus.Labels{}
for key, value := range labels {
currentLabel[key] = value
}
currentLabel["state"] = stateName
globalStates[stateValue] = newEventMetric(¤tLabel)
case []string:
for index, childStateValue := range stateValue {
// Create the target map
currentLabel := prometheus.Labels{}
for key, value := range labels {
currentLabel[key] = value
}
currentLabel["state"] = stateName + "-" + string(index)
globalStates[childStateValue] = newEventMetric(¤tLabel)
}
}
}
}
for stateName, stateValue := range loxoneConfig.GlobalStates {
currentLabel := prometheus.Labels{
"control": "global",
"room": "global",
"type": "global",
"cat": "global",
"state": stateName,
}
globalStates[stateValue] = newEventMetric(¤tLabel)
}
log.Info("Start reading events")
for {
select {
case <-ctx.Done():
log.Infof("Shutting Down")
case event := <-lox.GetEvents():
if eventMetric, ok := globalStates[event.UUID]; ok {
eventMetric.update(event.Value)
} else {
log.Debugf("event unknown: %+v\n", event)
}
}
}
}
type eventMetric struct {
labels *prometheus.Labels
initialized bool
debounceFunction func(f func())
}
func newEventMetric(labels *prometheus.Labels) *eventMetric {
return &eventMetric{
initialized: false,
labels: labels,
debounceFunction: debounce.New(500 * time.Millisecond),
}
}
func (e *eventMetric) update(value float64) {
values.With(*e.labels).Set(value)
if !e.initialized {
e.initialized = true
return
}
log.Infof("New event %+v with value %f", e.labels, value)
e.debounceFunction(func() {
changes.With(*e.labels).Inc()
})
}