/
nodify.go
132 lines (113 loc) · 2.73 KB
/
nodify.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
package parser
import (
"fmt"
"regexp"
"strconv"
parsec "github.com/prataprc/goparsec"
)
var idSplitter *regexp.Regexp
func init() {
idSplitter = regexp.MustCompile(`[-:.*]`)
}
func nodifyTelegram(ns []parsec.ParsecNode) parsec.ParsecNode {
objs := make([]Object, 0)
for _, obj := range ns[1].([]parsec.ParsecNode) {
objs = append(objs, obj.(Object))
}
return Telegram{
Identification: ns[0].(*parsec.Terminal).GetValue(),
Objects: objs,
}
}
func nodifyID(idS string) ID {
parts := idSplitter.Split(idS, 7)
ints := []byte{}
for idx, str := range parts {
if idx > 5 { // should not be more than 6. If so, it's an unknown format
break
}
i, err := strconv.ParseUint(str, 10, 8)
if err != nil {
panic(fmt.Errorf("error converting ID to byte: %w", err))
}
ints = append(ints, byte(i))
}
switch len(parts) {
case 2:
return ID{
A: nil,
B: nil,
C: &ints[0],
D: &ints[1],
E: nil,
F: nil,
}
case 5:
return ID{
A: &ints[0],
B: &ints[1],
C: &ints[2],
D: &ints[3],
E: &ints[4],
}
case 6:
return ID{
A: &ints[0],
B: &ints[1],
C: &ints[2],
D: &ints[3],
E: &ints[4],
F: &ints[5],
}
default:
panic(fmt.Sprintf("don't know how to process ID field with %d values", len(parts)))
}
}
func nodifyIDOnly(ns []parsec.ParsecNode) parsec.ParsecNode {
return Object{
ID: nodifyID(ns[0].(*parsec.Terminal).GetValue()),
Value: NullValue{},
Timestamp: nil,
}
}
func nodifyCosem(ns []parsec.ParsecNode) parsec.ParsecNode {
return Object{
ID: nodifyID(ns[0].(*parsec.Terminal).GetValue()),
Value: ns[1].(Value),
Timestamp: nil,
}
}
func nodifyCosemEmpty(ns []parsec.ParsecNode) parsec.ParsecNode {
return NullValue{}
}
func nodifyCosemValue(ns []parsec.ParsecNode) parsec.ParsecNode {
return NewValue(ns[1].(*parsec.Terminal).GetValue(), nil)
}
func nodifyCosemValueUnit(ns []parsec.ParsecNode) parsec.ParsecNode {
unit := ns[3].(*parsec.Terminal).GetValue()
return NewValue(ns[1].(*parsec.Terminal).GetValue(), &unit)
}
func nodifyDSMR3Gas(ns []parsec.ParsecNode) parsec.ParsecNode {
ts := ns[2].(*parsec.Terminal).GetValue()
unit := ns[17].(*parsec.Terminal).GetValue()
val := NewValue(ns[17].(*parsec.Terminal).GetValue(), &unit)
return Object{
ID: nodifyID(ns[0].(*parsec.Terminal).GetValue()),
Value: val,
Timestamp: &ts,
}
}
func nodifyMBus(ns []parsec.ParsecNode) parsec.ParsecNode {
ts := ns[2].(*parsec.Terminal).GetValue()
return Object{
ID: nodifyID(ns[0].(*parsec.Terminal).GetValue()),
Value: ns[4].(Value),
Timestamp: &ts,
}
}
func nodifyIdentification(ns []parsec.ParsecNode) parsec.ParsecNode {
return ns[1]
}
func nodifyFirstItem(ns []parsec.ParsecNode) parsec.ParsecNode {
return ns[0]
}