-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
knx_listener.go
188 lines (164 loc) · 4.79 KB
/
knx_listener.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
//go:generate ../../../tools/readme_config_includer/generator
package knx_listener
import (
_ "embed"
"fmt"
"reflect"
"sync"
"github.com/vapourismo/knx-go/knx"
"github.com/vapourismo/knx-go/knx/dpt"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/plugins/inputs"
)
//go:embed sample.conf
var sampleConfig string
type KNXInterface interface {
Inbound() <-chan knx.GroupEvent
Close()
}
type addressTarget struct {
measurement string
datapoint dpt.DatapointValue
}
type Measurement struct {
Name string
Dpt string
Addresses []string
}
type KNXListener struct {
ServiceType string `toml:"service_type"`
ServiceAddress string `toml:"service_address"`
Measurements []Measurement `toml:"measurement"`
Log telegraf.Logger `toml:"-"`
client KNXInterface
gaTargetMap map[string]addressTarget
gaLogbook map[string]bool
acc telegraf.Accumulator
wg sync.WaitGroup
}
func (*KNXListener) SampleConfig() string {
return sampleConfig
}
func (kl *KNXListener) Gather(_ telegraf.Accumulator) error {
return nil
}
func (kl *KNXListener) Start(acc telegraf.Accumulator) error {
// Store the accumulator for later use
kl.acc = acc
// Setup a logbook to track unknown GAs to avoid log-spamming
kl.gaLogbook = make(map[string]bool)
// Construct the mapping of Group-addresses (GAs) to DPTs and the name
// of the measurement
kl.gaTargetMap = make(map[string]addressTarget)
for _, m := range kl.Measurements {
kl.Log.Debugf("Group-address mapping for measurement %q:", m.Name)
for _, ga := range m.Addresses {
kl.Log.Debugf(" %s --> %s", ga, m.Dpt)
if _, ok := kl.gaTargetMap[ga]; ok {
return fmt.Errorf("duplicate specification of address %q", ga)
}
d, ok := dpt.Produce(m.Dpt)
if !ok {
return fmt.Errorf("cannot create datapoint-type %q for address %q", m.Dpt, ga)
}
kl.gaTargetMap[ga] = addressTarget{m.Name, d}
}
}
// Connect to the KNX-IP interface
kl.Log.Infof("Trying to connect to %q at %q", kl.ServiceType, kl.ServiceAddress)
switch kl.ServiceType {
case "tunnel", "tunnel_udp":
tunnelconfig := knx.DefaultTunnelConfig
tunnelconfig.UseTCP = false
c, err := knx.NewGroupTunnel(kl.ServiceAddress, tunnelconfig)
if err != nil {
return err
}
kl.client = &c
case "tunnel_tcp":
tunnelconfig := knx.DefaultTunnelConfig
tunnelconfig.UseTCP = true
c, err := knx.NewGroupTunnel(kl.ServiceAddress, tunnelconfig)
if err != nil {
return err
}
kl.client = &c
case "router":
c, err := knx.NewGroupRouter(kl.ServiceAddress, knx.DefaultRouterConfig)
if err != nil {
return err
}
kl.client = &c
case "dummy":
c := NewDummyInterface()
kl.client = &c
default:
return fmt.Errorf("invalid interface type: %s", kl.ServiceAddress)
}
kl.Log.Infof("Connected!")
// Listen to the KNX bus
kl.wg.Add(1)
go func() {
kl.wg.Done()
kl.listen()
}()
return nil
}
func (kl *KNXListener) Stop() {
if kl.client != nil {
kl.client.Close()
kl.wg.Wait()
}
}
func (kl *KNXListener) listen() {
for msg := range kl.client.Inbound() {
// Match GA to DataPointType and measurement name
ga := msg.Destination.String()
target, ok := kl.gaTargetMap[ga]
if !ok {
if !kl.gaLogbook[ga] {
kl.Log.Infof("Ignoring message %+v for unknown GA %q", msg, ga)
kl.gaLogbook[ga] = true
}
continue
}
// Extract the value from the data-frame
err := target.datapoint.Unpack(msg.Data)
if err != nil {
kl.Log.Errorf("Unpacking data failed: %v", err)
continue
}
kl.Log.Debugf("Matched GA %q to measurement %q with value %v", ga, target.measurement, target.datapoint)
// Convert the DatapointValue interface back to its basic type again
// as otherwise telegraf will not push out the metrics and eat it
// silently.
var value interface{}
vi := reflect.Indirect(reflect.ValueOf(target.datapoint))
switch vi.Kind() {
case reflect.Bool:
value = vi.Bool()
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
value = vi.Int()
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
value = vi.Uint()
case reflect.Float32, reflect.Float64:
value = vi.Float()
default:
kl.Log.Errorf("Type conversion %v failed for address %q", vi.Kind(), ga)
continue
}
// Compose the actual data to be pushed out
fields := map[string]interface{}{"value": value}
tags := map[string]string{
"groupaddress": ga,
"unit": target.datapoint.(dpt.DatapointMeta).Unit(),
"source": msg.Source.String(),
}
kl.acc.AddFields(target.measurement, fields, tags)
}
}
func init() {
inputs.Add("knx_listener", func() telegraf.Input { return &KNXListener{ServiceType: "tunnel"} })
// Register for backward compatibility
inputs.Add("KNXListener", func() telegraf.Input { return &KNXListener{ServiceType: "tunnel"} })
}