forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodbus.go
192 lines (155 loc) Β· 4.54 KB
/
modbus.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
package meter
import (
"errors"
"fmt"
"time"
"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/util"
"github.com/evcc-io/evcc/util/modbus"
"github.com/volkszaehler/mbmd/meters"
"github.com/volkszaehler/mbmd/meters/rs485"
"github.com/volkszaehler/mbmd/meters/sunspec"
)
// Modbus is an api.Meter implementation with configurable getters and setters.
type Modbus struct {
conn *modbus.Connection
device meters.Device
opPower modbus.Operation
opEnergy modbus.Operation
opSoC modbus.Operation
}
func init() {
registry.Add("modbus", NewModbusFromConfig)
}
//go:generate go run ../cmd/tools/decorate.go -f decorateModbus -b api.Meter -t "api.MeterEnergy,TotalEnergy,func() (float64, error)" -t "api.MeterCurrent,Currents,func() (float64, float64, float64, error)" -t "api.Battery,SoC,func() (float64, error)"
// NewModbusFromConfig creates api.Meter from config
func NewModbusFromConfig(other map[string]interface{}) (api.Meter, error) {
cc := struct {
Model string
modbus.Settings `mapstructure:",squash"`
Power, Energy, SoC string
Currents []string
Delay time.Duration
Timeout time.Duration
}{
Power: "Power",
Settings: modbus.Settings{
ID: 1,
},
}
if err := util.DecodeOther(other, &cc); err != nil {
return nil, err
}
// assume RTU if not set and this is a known RS485 meter model
if cc.RTU == nil {
b := modbus.IsRS485(cc.Model)
cc.RTU = &b
}
conn, err := modbus.NewConnection(cc.URI, cc.Device, cc.Comset, cc.Baudrate, modbus.ProtocolFromRTU(cc.RTU), cc.ID)
if err != nil {
return nil, err
}
// set non-default delay
if cc.Delay > 0 {
conn.Delay(cc.Delay)
}
// set non-default timeout
if cc.Timeout > 0 {
conn.Timeout(cc.Timeout)
}
log := util.NewLogger("modbus")
conn.Logger(log.TRACE)
// prepare device
device, err := modbus.NewDevice(cc.Model, cc.SubDevice)
if err == nil {
err = device.Initialize(conn)
// silence Kostal implementation errors
if errors.Is(err, meters.ErrPartiallyOpened) {
err = nil
}
}
if err != nil {
return nil, err
}
m := &Modbus{
conn: conn,
device: device,
}
if err := modbus.ParseOperation(device, cc.Power, &m.opPower); err != nil {
return nil, fmt.Errorf("invalid measurement for power: %s", cc.Power)
}
// decorate energy reading
var totalEnergy func() (float64, error)
if cc.Energy != "" {
if err := modbus.ParseOperation(device, cc.Energy, &m.opEnergy); err != nil {
return nil, fmt.Errorf("invalid measurement for energy: %s", cc.Energy)
}
totalEnergy = m.totalEnergy
}
// decorate Meter with MeterCurrent
var currentsG func() (float64, float64, float64, error)
if len(cc.Currents) > 0 {
if len(cc.Currents) != 3 {
return nil, errors.New("need 3 currents")
}
var curr []func() (float64, error)
for _, cc := range cc.Currents {
var opCurrent modbus.Operation
if err := modbus.ParseOperation(device, cc, &opCurrent); err != nil {
return nil, fmt.Errorf("invalid measurement for current: %s", cc)
}
c := func() (float64, error) {
return m.floatGetter(opCurrent)
}
curr = append(curr, c)
}
currentsG = collectCurrentProviders(curr)
}
// decorate soc reading
var soc func() (float64, error)
if cc.SoC != "" {
if err := modbus.ParseOperation(device, cc.SoC, &m.opSoC); err != nil {
return nil, fmt.Errorf("invalid measurement for soc: %s", cc.SoC)
}
soc = m.soc
}
return decorateModbus(m, totalEnergy, currentsG, soc), nil
}
// floatGetter executes configured modbus read operation and implements func() (float64, error)
func (m *Modbus) floatGetter(op modbus.Operation) (float64, error) {
var res meters.MeasurementResult
var err error
if dev, ok := m.device.(*rs485.RS485); ok {
res, err = dev.QueryOp(m.conn, op.MBMD)
}
if dev, ok := m.device.(*sunspec.SunSpec); ok {
if op.MBMD.IEC61850 != 0 {
res, err = dev.QueryOp(m.conn, op.MBMD.IEC61850)
} else {
res.Value, err = dev.QueryPoint(
m.conn,
op.SunSpec.Model,
op.SunSpec.Block,
op.SunSpec.Point,
)
}
}
// silence NaN reading errors by assuming zero
if err != nil && errors.Is(err, meters.ErrNaN) {
res.Value = 0
err = nil
}
return res.Value, err
}
// CurrentPower implements the api.Meter interface
func (m *Modbus) CurrentPower() (float64, error) {
return m.floatGetter(m.opPower)
}
// totalEnergy implements the api.MeterEnergy interface
func (m *Modbus) totalEnergy() (float64, error) {
return m.floatGetter(m.opEnergy)
}
// soc implements the api.Battery interface
func (m *Modbus) soc() (float64, error) {
return m.floatGetter(m.opSoC)
}