forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipeline.go
209 lines (172 loc) Β· 4.26 KB
/
pipeline.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
package pipeline
import (
"bytes"
"encoding/hex"
"fmt"
"regexp"
"strings"
xj "github.com/basgys/goxml2json"
"github.com/evcc-io/evcc/util"
"github.com/evcc-io/evcc/util/jq"
"github.com/itchyny/gojq"
"github.com/volkszaehler/mbmd/meters/rs485"
)
type Pipeline struct {
log *util.Logger
re *regexp.Regexp
jq *gojq.Query
dflt string
unpack string
decode string
}
type Settings struct {
Regex string
Default string
Jq string
Unpack string
Decode string
}
func New(log *util.Logger, cc Settings) (*Pipeline, error) {
p := &Pipeline{
log: log,
}
var err error
if err == nil && cc.Regex != "" {
_, err = p.WithRegex(cc.Regex, cc.Default)
}
if err == nil && cc.Jq != "" {
_, err = p.WithJq(cc.Jq)
}
if err == nil && cc.Unpack != "" {
_, err = p.WithUnpack(cc.Unpack)
}
if err == nil && cc.Decode != "" {
_, err = p.WithDecode(cc.Decode)
}
return p, err
}
// WithRegex adds a regex query applied to the mqtt listener payload
func (p *Pipeline) WithRegex(regex, dflt string) (*Pipeline, error) {
re, err := regexp.Compile(regex)
if err != nil {
return nil, fmt.Errorf("invalid regex '%s': %w", re, err)
}
p.re = re
p.dflt = dflt
return p, nil
}
// WithJq adds a jq query applied to the mqtt listener payload
func (p *Pipeline) WithJq(jq string) (*Pipeline, error) {
op, err := gojq.Parse(jq)
if err != nil {
return nil, fmt.Errorf("invalid jq query '%s': %w", jq, err)
}
p.jq = op
return p, nil
}
// WithUnpack adds data unpacking
func (p *Pipeline) WithUnpack(unpack string) (*Pipeline, error) {
p.unpack = strings.ToLower(unpack)
return p, nil
}
// WithDecode adds data decoding
func (p *Pipeline) WithDecode(decode string) (*Pipeline, error) {
p.decode = strings.ToLower(decode)
return p, nil
}
// transform XML into JSON with attribute names getting 'attr' prefix
func (p *Pipeline) transformXML(value []byte) []byte {
value = bytes.TrimSpace(value)
// only do a simple check, as some devices e.g. Kostal Piko MP plus don't seem to send proper XML
if !bytes.HasPrefix(value, []byte("<?xml")) {
return value
}
in := bytes.NewReader(value)
// Decode XML document
root := new(xj.Node)
if err := xj.NewDecoder(in).DecodeWithCustomPrefixes(root, "", "attr"); err != nil {
return value
}
// Then encode it in JSON
out := new(bytes.Buffer)
if err := xj.NewEncoder(out).Encode(root); err != nil {
return value
}
if p.log != nil {
p.log.TRACE.Println(out.String())
}
return out.Bytes()
}
func (p *Pipeline) unpackValue(value []byte) (string, error) {
switch p.unpack {
case "hex":
b, err := hex.DecodeString(string(value))
if err != nil {
return "", err
}
return string(b), nil
}
return "", fmt.Errorf("invalid unpack: %s", p.unpack)
}
// decode a hex string to a proper value
// TODO reuse similar code from Modbus
func (p *Pipeline) decodeValue(value []byte) (interface{}, error) {
switch p.decode {
case "float32", "ieee754":
return rs485.RTUIeee754ToFloat64(value), nil
case "float32s", "ieee754s":
return rs485.RTUIeee754ToFloat64Swapped(value), nil
case "float64":
return rs485.RTUUint64ToFloat64(value), nil
case "uint16":
return rs485.RTUUint16ToFloat64(value), nil
case "uint32":
return rs485.RTUUint32ToFloat64(value), nil
case "uint32s":
return rs485.RTUUint32ToFloat64Swapped(value), nil
case "uint64":
return rs485.RTUUint64ToFloat64(value), nil
case "int16":
return rs485.RTUInt16ToFloat64(value), nil
case "int32":
return rs485.RTUInt32ToFloat64(value), nil
case "int32s":
return rs485.RTUInt32ToFloat64Swapped(value), nil
}
return nil, fmt.Errorf("invalid decoding: %s", p.decode)
}
func (p *Pipeline) Process(in []byte) ([]byte, error) {
b := p.transformXML(in)
if p.re != nil {
m := p.re.FindSubmatch(b)
if len(m) == 1 {
b = m[0] // full match
} else if len(m) > 1 {
b = m[1] // first submatch
} else if len(p.dflt) > 0 {
return []byte(p.dflt), nil
}
}
if p.jq != nil {
v, err := jq.Query(p.jq, b)
if err != nil {
return b, err
}
b = []byte(fmt.Sprintf("%v", v))
}
if p.unpack != "" {
v, err := p.unpackValue(b)
if err != nil {
return b, err
}
b = []byte(fmt.Sprintf("%v", v))
}
if p.decode != "" {
v, err := p.decodeValue(b)
if err != nil {
return b, err
}
b = []byte(fmt.Sprintf("%v", v))
}
return b, nil
}