-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
157 lines (142 loc) · 3.61 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
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"github.com/aldas/go-modbus-client"
"github.com/aldas/go-modbus-client/poller"
"log/slog"
"os"
"os/signal"
"time"
)
/*
Example `config.json` content to poll "Victron Energy Meter VM-3P75CT" over UDP
{
"defaults": {
"server_address": "udp://192.168.0.200:502?invalid_addr=1000,12000-12100&read_timeout=1s",
"function_code": 3,
"unit_id": 1,
"protocol": "tcp",
"interval": "1s"
},
"fields": [
{"name": "AcL1Voltage", "address": 12352, "type": "Int16", "scale": 0.01},
{"name": "AcL1Current", "address": 12353, "type": "Int16", "scale": 0.01},
{"name": "AcL1EnergyForward", "address": 12354, "type": "Uint32", "scale": 0.01},
{"name": "AcL1EnergyReverse", "address": 12356, "type": "Uint32", "scale": 0.01},
{"name": "AcL1ErrorCode", "address": 12358, "type": "Uint16"},
]
}
*/
type config struct {
Defaults modbus.BuilderDefaults `json:"defaults" mapstructure:"defaults"`
Fields []field `json:"fields" mapstructure:"fields"`
}
type field struct {
modbus.Field
Scale float64 `json:"scale,omitempty" mapstructure:"scale"`
}
// usage: ./modbus-poller -config=config.json
func main() {
var configLoc string
flag.StringVar(&configLoc, "config", "config.json", "path to json configuration")
flag.Parse()
logger := slog.New(slog.NewJSONHandler(os.Stderr, nil))
rawConfig, err := os.ReadFile(configLoc) // #nosec G304
if err != nil {
logger.Error("reading config.json failed", "err", err)
return
}
var conf config
if err := json.Unmarshal(rawConfig, &conf); err != nil {
logger.Error("config json unmarshalling failed", "err", err)
return
}
scales := map[string]float64{}
b := modbus.NewRequestBuilderWithConfig(conf.Defaults)
for _, f := range conf.Fields {
if f.Scale != 0 {
scales[f.Name] = f.Scale
}
b.AddField(f.Field)
}
batches, err := b.Split()
if err != nil {
logger.Error("splitting fields to requests failed", "err", err)
return
}
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
defer cancel()
p := poller.NewPollerWithConfig(batches, poller.Config{Logger: logger})
go func() {
for {
select {
case result := <-p.ResultChan:
values := map[string]any{}
for _, v := range result.Values {
if v.Error != nil {
continue
}
value := v.Value
if scale, ok := scales[v.Field.Name]; ok {
value = scaleValue(scale, value)
}
values[v.Field.Name] = value
}
if len(values) == 0 {
continue
}
raw, err := json.Marshal(struct {
Time time.Time `json:"time"`
Values map[string]any `json:"values"`
}{
Time: result.Time,
Values: values,
})
if err != nil {
logger.Error("failed to marshal result", "err", err)
continue
}
fmt.Printf("%s\n", raw)
case <-ctx.Done():
return
}
}
}()
if err = p.Poll(ctx); err != nil {
logger.Error("polling ended with failure", "err", err)
return
}
logger.Info("polling ended")
}
func scaleValue(scale float64, value any) any {
// when scale!=0 value will be converted to float64 type - this is a deliberate feature
if scale == 0 {
return value
}
switch v := value.(type) {
case uint8:
return float64(v) * scale
case int8:
return float64(v) * scale
case uint16:
return float64(v) * scale
case int16:
return float64(v) * scale
case uint32:
return float64(v) * scale
case int32:
return float64(v) * scale
case uint64:
return float64(v) * scale
case int64:
return float64(v) * scale
case float32:
return float64(v) * scale
case float64:
return v * scale
}
return value
}