forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
modbus_sunspec.go
242 lines (199 loc) Β· 5.46 KB
/
modbus_sunspec.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package provider
import (
"errors"
"fmt"
"math"
"time"
sunspec "github.com/andig/gosunspec"
"github.com/andig/gosunspec/typelabel"
"github.com/evcc-io/evcc/util"
"github.com/evcc-io/evcc/util/modbus"
"github.com/volkszaehler/mbmd/meters"
sunsdev "github.com/volkszaehler/mbmd/meters/sunspec"
)
// ModbusSunspec implements modbus RTU and TCP access
type ModbusSunspec struct {
log *util.Logger
conn *modbus.Connection
device *sunsdev.SunSpec
op modbus.SunSpecOperation
scale float64
}
func init() {
registry.Add("sunspec", NewModbusSunspecFromConfig)
}
// NewModbusSunspecFromConfig creates Modbus plugin
func NewModbusSunspecFromConfig(other map[string]interface{}) (Provider, error) {
cc := struct {
modbus.Settings `mapstructure:",squash"`
Value string
Scale float64
Delay time.Duration
ConnectDelay time.Duration
Timeout time.Duration
}{
Scale: 1,
}
if err := util.DecodeOther(other, &cc); err != nil {
return nil, err
}
conn, err := modbus.NewConnection(cc.URI, cc.Device, cc.Comset, cc.Baudrate, modbus.Tcp, cc.ID)
if err != nil {
return nil, err
}
// set non-default timeout
if cc.Timeout > 0 {
conn.Timeout(cc.Timeout)
}
// set non-default delay
if cc.Delay > 0 {
conn.Delay(cc.Delay)
}
// set non-default connect delay
if cc.ConnectDelay > 0 {
conn.ConnectDelay(cc.ConnectDelay)
}
log := util.NewLogger("sunspec")
conn.Logger(log.TRACE)
if cc.Value == "" {
return nil, errors.New("value is required")
}
// silence KOSTAL implementation errors
device := sunsdev.NewDevice("sunspec", cc.SubDevice)
if err := device.Initialize(conn); err != nil && !errors.Is(err, meters.ErrPartiallyOpened) {
return nil, err
}
ops, err := modbus.ParsePoint(cc.Value)
if err != nil {
return nil, fmt.Errorf("invalid sunspec value: %s", cc.Value)
}
mb := &ModbusSunspec{
log: log,
conn: conn,
device: device,
scale: cc.Scale,
}
for _, op := range ops {
if _, _, err := device.QueryPointAny(conn, op.Model, op.Block, op.Point); err == nil {
mb.op = op
return mb, nil
}
}
return nil, fmt.Errorf("sunspec model not found: %v", ops)
}
func (m *ModbusSunspec) floatGetter() (f float64, err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("panic: %v", r)
}
}()
res, err := m.device.QueryPoint(
m.conn,
m.op.Model,
m.op.Block,
m.op.Point,
)
if err != nil && !errors.Is(err, meters.ErrNaN) {
return 0, fmt.Errorf("model %d block %d point %s: %w", m.op.Model, m.op.Block, m.op.Point, err)
}
m.log.TRACE.Printf("model %d block %d point %s: %v", m.op.Model, m.op.Block, m.op.Point, res)
return m.scale * res, nil
}
var _ FloatProvider = (*Modbus)(nil)
// FloatGetter executes configured modbus read operation and implements func() (float64, error)
func (m *ModbusSunspec) FloatGetter() (func() (f float64, err error), error) {
return m.floatGetter, nil
}
var _ IntProvider = (*Modbus)(nil)
// IntGetter executes configured modbus read operation and implements IntProvider
func (m *ModbusSunspec) IntGetter() (func() (int64, error), error) {
g, err := m.FloatGetter()
return func() (int64, error) {
res, err := g()
return int64(math.Round(res)), err
}, err
}
func (m *ModbusSunspec) blockPoint() (block sunspec.Block, point sunspec.Point, err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("panic: %v", r)
}
}()
block, point, err = m.device.QueryPointAny(
m.conn,
m.op.Model,
m.op.Block,
m.op.Point,
)
if err != nil {
err = fmt.Errorf("model %d block %d point %s: %w", m.op.Model, m.op.Block, m.op.Point, err)
}
return block, point, err
}
// TODO scale factors
var _ SetFloatProvider = (*Modbus)(nil)
// FloatSetter executes configured modbus write operation and implements SetFloatProvider
func (m *ModbusSunspec) FloatSetter(_ string) (func(float64) error, error) {
block, point, err := m.blockPoint()
if err != nil {
return nil, err
}
typ := point.Type()
return func(val float64) (err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("panic: %v", r)
}
}()
val = val * m.scale
switch typ {
case typelabel.Float32:
point.SetFloat32(float32(val))
default:
return fmt.Errorf("invalid point type: %s", typ)
}
return block.Write(m.op.Point)
}, nil
}
var _ SetIntProvider = (*Modbus)(nil)
// IntSetter executes configured modbus write operation and implements SetIntProvider
func (m *ModbusSunspec) IntSetter(_ string) (func(int64) error, error) {
block, point, err := m.blockPoint()
if err != nil {
return nil, err
}
typ := point.Type()
return func(val int64) (err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("panic: %v", r)
}
}()
val = int64(float64(val) * m.scale)
switch typ {
case typelabel.Bitfield16:
point.SetBitfield16(sunspec.Bitfield16(val))
case typelabel.Bitfield32:
point.SetBitfield32(sunspec.Bitfield32(val))
case typelabel.Enum16:
point.SetEnum16(sunspec.Enum16(val))
case typelabel.Enum32:
point.SetEnum32(sunspec.Enum32(val))
case typelabel.Int16:
point.SetInt16(int16(val))
case typelabel.Int32:
point.SetInt32(int32(val))
case typelabel.Int64:
point.SetInt64(val)
case typelabel.Uint16:
point.SetUint16(uint16(val))
case typelabel.Uint32:
point.SetUint32(uint32(val))
case typelabel.Uint64:
point.SetUint64(uint64(val))
default:
return fmt.Errorf("invalid point type: %s", typ)
}
return block.Write(m.op.Point)
}, nil
}