-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
119 lines (102 loc) · 2.95 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
package main
import (
"flag"
"io"
"github.com/DataDog/datadog-go/statsd"
"github.com/j-vizcaino/goteleinfo"
log "github.com/sirupsen/logrus"
)
const (
BufferedFramesCount = 100
)
func ensureFrameReader(serialDevice string) teleinfo.Reader {
port, err := teleinfo.OpenPort(serialDevice)
if err != nil {
log.WithFields(log.Fields{"device": serialDevice, "error": err}).Fatal("Cannot open teleinfo port")
}
return teleinfo.NewReader(port)
}
func readFrames(reader teleinfo.Reader, framesChan chan teleinfo.Frame) {
for {
f, err := reader.ReadFrame()
if err != nil {
log.WithField("error", err).Warn("Teleinfo frame read failed")
if err == io.EOF {
log.Fatal("Device handle is closed, stopping application")
}
continue
}
framesChan <- f
}
}
func ensureStatsdClient(metricsNamespace string) *statsd.Client {
// TODO: provide a way to change this
url := "localhost:8125"
dsd, err := statsd.New(url)
if err != nil {
log.WithFields(log.Fields{"statsd_url": url, "error": err}).Fatal("statsd client setup failed")
}
dsd.Namespace = metricsNamespace
return dsd
}
func buildFrameTags(f teleinfo.Frame) []string {
if f.Type() != "HC.." {
log.WithField("frame_type", f.Type()).Warn("Skipped unsupported teleinfo frame")
return nil
}
rate, _ := f.GetStringField("PTEC")
var rateTag string
switch rate {
case "HP..":
rateTag = "heures pleines"
case "HC..":
rateTag = "heures creuses"
default:
rateTag = "unknown"
}
return []string{"price:" + rateTag}
}
func exportFrames(dsd *statsd.Client, framesChan chan teleinfo.Frame) {
gauges := map[string]string{
"HCHC": "counter_low_price_kwh",
"HCHP": "counter_full_price_kwh",
"PAPP": "power_va",
"IINST": "current_amps",
}
for f := range framesChan {
tags := buildFrameTags(f)
if tags == nil {
continue
}
for key, metric := range gauges {
value, ok := f.GetUIntField(key)
if !ok {
s, _ := f.GetStringField(key)
log.WithFields(log.Fields{"field_name": key, "field_value": s, "metric": metric}).Warn("Cannot extract metric value from frame")
continue
}
log.WithFields(log.Fields{"value": value, "metric": metric, "namespace": dsd.Namespace, "tags": tags}).Debug("Sending dogstatsd gauge")
dsd.Gauge(metric, float64(value), tags, 1)
}
}
}
func main() {
var serialDevice string
var logDebug bool
var metricsNamespace string
flag.StringVar(&serialDevice, "device", "/dev/ttyUSB0", "Serial port to read frames from")
flag.BoolVar(&logDebug, "debug", false, "Enable debug log messages")
flag.StringVar(&metricsNamespace, "metrics-namespace", "electrical_energy.", "Namespace for statsd metrics")
flag.Parse()
if logDebug {
log.SetLevel(log.DebugLevel)
} else {
log.SetLevel(log.InfoLevel)
}
log.Info("Starting dd-teleinfo...")
reader := ensureFrameReader(serialDevice)
dsd := ensureStatsdClient(metricsNamespace)
framesChan := make(chan teleinfo.Frame, BufferedFramesCount)
go readFrames(reader, framesChan)
exportFrames(dsd, framesChan)
}