-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.go
242 lines (201 loc) · 5.43 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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package main
import (
"flag"
"fmt"
"os"
"regexp"
"strconv"
"time"
"github.com/influxdata/influxdb/client/v2"
"github.com/maxatome/go-vitotrol"
)
func VitotrolInit(vconf *ConfigVitotrol) *vitotrol.Session {
var err error
for {
if err != nil {
fmt.Fprintf(os.Stderr, "%s\nSleeping before retrying...\n", err)
time.Sleep(time.Duration(vconf.RetryTimeout) * time.Second)
}
pVitotrol := &vitotrol.Session{}
fmt.Println("Vitotrol login...")
err = pVitotrol.Login(vconf.Login, vconf.Password)
if err != nil {
err = fmt.Errorf("Login failed: %s", err)
continue
}
fmt.Println("Vitotrol GetDevices...")
err = pVitotrol.GetDevices()
if err != nil {
err = fmt.Errorf("GetDevices failed: %s", err)
continue
}
if len(pVitotrol.Devices) == 0 {
err = fmt.Errorf("No device found")
continue
}
fmt.Printf("%d device(s) found\n", len(pVitotrol.Devices))
return pVitotrol
}
}
func getAttrValue(vdev *vitotrol.Device, attrID vitotrol.AttrID) (value interface{}) {
value, _ = vitotrol.AttributesRef[attrID].
Type.Vitodata2NativeValue(vdev.Attributes[attrID].Value)
// uint64 handled from influx 1.4
if vuint64, ok := value.(uint64); ok {
value = int(vuint64)
}
return
}
var customAttr = regexp.MustCompile(
`^([a-zA-Z0-9]+)[-_]0x([a-fA-F0-9]{1,4})\z`)
func handleDevices(conf *Config, pVitotrol *vitotrol.Session, influx client.Client) bool {
atLeastOneOK := false
for _, vdev := range pVitotrol.Devices {
start := time.Now()
if !vdev.IsConnected {
continue
}
// Check if this device has a configuration
cdev := conf.GetConfigDevice(vdev.DeviceName, vdev.LocationName)
if cdev == nil {
continue
}
ch, err := vdev.RefreshDataWait(pVitotrol, cdev.attrs)
if err != nil {
fmt.Fprintf(os.Stderr, "RefreshData error: %s\n", err)
continue
}
atLeastOneOK = true
// Create a new point batch
bp, err := client.NewBatchPoints(client.BatchPointsConfig{
Database: cdev.Database,
Precision: cdev.Precision,
RetentionPolicy: cdev.RetentionPolicy,
WriteConsistency: cdev.WriteConsistency,
})
if err != nil {
fmt.Printf("influx.NewBatchPoints failed: %s\n", err)
os.Exit(1)
}
if err = <-ch; err != nil {
fmt.Fprintf(os.Stderr, "RefreshData failed: %s\n", err)
continue
}
now := time.Now()
err = vdev.GetData(pVitotrol, cdev.attrs)
if err != nil {
fmt.Fprintf(os.Stderr, "GetData error: %s\n", err)
continue
}
fields := map[string]interface{}{}
for _, attrID := range cdev.attrs {
fields[vitotrol.AttributesRef[attrID].Name] =
getAttrValue(&vdev, attrID)
}
// Computed attrs
for _, fieldName := range cdev.computedFields {
fields[fieldName] = computedAttrs[fieldName].Compute(&vdev)
}
// If no tags are configured, use defaults
tags := cdev.Tags
if len(tags) == 0 {
tags = map[string]string{
"device": vdev.DeviceName,
"location": vdev.LocationName,
}
}
point, err := client.NewPoint(cdev.Measurement, tags, fields, now)
if err != nil {
fmt.Printf("influx.NewPoint failed: %s\n", err)
os.Exit(1)
}
bp.AddPoint(point)
fmt.Printf("%s %s → writing batch...\n",
now.Format(time.RFC3339),
now.Sub(start).Truncate(time.Millisecond),
)
// Write the batch
err = influx.Write(bp)
if err != nil {
fmt.Printf("influx.Write failed: %s\n", err)
os.Exit(1)
}
time.Sleep(time.Duration(conf.Vitotrol.Frequency) * time.Second)
}
return atLeastOneOK
}
func main() {
configFile := flag.String("config", "", "config file")
flag.Parse()
if *configFile == "" {
fmt.Fprintln(os.Stderr, "config file is missing")
os.Exit(1)
}
conf, err := ReadConfig(*configFile)
if err != nil {
fmt.Fprintf(os.Stderr, "Cannot read config: %s\n", err)
os.Exit(1)
}
// Resolve fields
for _, cdev := range conf.Devices {
attrs := make(map[vitotrol.AttrID]struct{}, len(cdev.Fields))
for _, fieldName := range cdev.Fields {
// Computed attribute
if cattr, ok := computedAttrs[fieldName]; ok {
for _, attrID := range cattr.Attrs {
attrs[attrID] = struct{}{}
}
cdev.computedFields = append(cdev.computedFields, fieldName)
} else {
// Already known attribute
attrID, ok := vitotrol.AttributesNames2IDs[fieldName]
if !ok {
// Custom attribute
m := customAttr.FindStringSubmatch(fieldName)
if m == nil {
fmt.Fprintf(os.Stderr, "Unknown attribute `%s'\n", fieldName)
os.Exit(1)
}
attrRef := vitotrol.AttrRef{
Type: vitotrol.TypeDouble,
Access: vitotrol.ReadOnly,
Name: m[1],
}
tmpID, _ := strconv.ParseUint(m[2], 16, 16)
attrID = vitotrol.AttrID(tmpID)
vitotrol.AddAttributeRef(attrID, attrRef)
}
attrs[attrID] = struct{}{}
}
}
if len(attrs) == 0 {
fmt.Fprintf(os.Stderr, "No attributes for device %s/location %s\n",
cdev.Name, cdev.Location)
os.Exit(1)
}
cdev.attrs = make([]vitotrol.AttrID, 0, len(attrs))
for attrID := range attrs {
cdev.attrs = append(cdev.attrs, attrID)
}
}
// Create a new Client
influx, err := client.NewHTTPClient(client.HTTPConfig{
Addr: conf.Influx.Address,
Username: conf.Influx.Login,
Password: conf.Influx.Password,
})
if err != nil {
fmt.Fprintf(os.Stderr, "Influx new client failed: %s\n", err)
os.Exit(1)
}
newVitotrol:
for {
pVitotrol := VitotrolInit(&conf.Vitotrol)
for {
if !handleDevices(conf, pVitotrol, influx) {
time.Sleep(time.Duration(30 * time.Second))
continue newVitotrol
}
}
}
}