forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
openwb-2.0.go
231 lines (186 loc) Β· 5.63 KB
/
openwb-2.0.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
package charger
import (
"encoding/binary"
"fmt"
"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/util"
"github.com/evcc-io/evcc/util/modbus"
)
// OpenWB20 charger implementation
type OpenWB20 struct {
conn *modbus.Connection
enabled bool
curr uint16
base uint16
}
const (
openwbRegPower = 10100
openwbRegImport = 10102
openwbRegVoltages = 10104
openwbRegCurrents = 10107
openwbRegPlugged = 10114
openwbRegCharging = 10115
openwbRegActualAmps = 10116
openwbRegSerial = 10150
openwbRegRfid = 10160
openwbRegCurrent = 10171
openwbRegPhaseTarget = 10180
openwbRegPhaseTrigger = 10181
openwbRegHeartbeat = 10190
openwbRegCpTrigger = 10198
)
func init() {
registry.Add("openwb-2.0", NewOpenWB20FromConfig)
}
// https://openwb.de/main/wp-content/uploads/2023/10/ModbusTCP-openWB-series2-Pro-1.pdf
//go:generate go run ../cmd/tools/decorate.go -f decorateOpenWB20 -b *OpenWB20 -r api.Charger -t "api.Identifier,Identify,func() (string, error)"
// NewOpenWB20FromConfig creates a OpenWB20 charger from generic config
func NewOpenWB20FromConfig(other map[string]interface{}) (api.Charger, error) {
cc := struct {
Connector uint16
modbus.TcpSettings `mapstructure:",squash"`
}{
Connector: 1,
TcpSettings: modbus.TcpSettings{
ID: 1,
},
}
if err := util.DecodeOther(other, &cc); err != nil {
return nil, err
}
wb, err := NewOpenWB20(cc.URI, cc.ID, cc.Connector)
if err != nil {
return nil, err
}
var identify func() (string, error)
if _, err := wb.identify(); err == nil {
identify = wb.identify
}
return decorateOpenWB20(wb, identify), nil
}
// NewOpenWB20 creates OpenWB20 charger
func NewOpenWB20(uri string, slaveID uint8, connector uint16) (*OpenWB20, error) {
uri = util.DefaultPort(uri, 1502)
conn, err := modbus.NewConnection(uri, "", "", 0, modbus.Tcp, slaveID)
if err != nil {
return nil, err
}
log := util.NewLogger("openwb-2.0")
conn.Logger(log.TRACE)
wb := &OpenWB20{
conn: conn,
curr: 6 * 100,
base: (connector - 1) * 100,
}
return wb, nil
}
// Status implements the api.Charger interface
func (wb *OpenWB20) Status() (api.ChargeStatus, error) {
if b, err := wb.conn.ReadInputRegisters(wb.base+openwbRegCharging, 1); err != nil || binary.BigEndian.Uint16(b) == 1 {
return api.StatusC, err
}
if b, err := wb.conn.ReadInputRegisters(wb.base+openwbRegPlugged, 1); err != nil || binary.BigEndian.Uint16(b) == 1 {
return api.StatusB, err
}
return api.StatusA, nil
}
// Enabled implements the api.Charger interface
func (wb *OpenWB20) Enabled() (bool, error) {
return verifyEnabled(wb, wb.enabled)
}
func (wb *OpenWB20) setCurrent(u uint16) error {
_, err := wb.conn.WriteSingleRegister(wb.base+openwbRegCurrent, u)
return err
}
// Enable implements the api.Charger interface
func (wb *OpenWB20) Enable(enable bool) error {
var u uint16
if enable {
u = wb.curr
}
err := wb.setCurrent(u)
if err == nil {
wb.enabled = enable
}
return err
}
// MaxCurrent implements the api.Charger interface
func (wb *OpenWB20) MaxCurrent(current int64) error {
return wb.MaxCurrentMillis(float64(current))
}
var _ api.ChargerEx = (*OpenWB20)(nil)
// MaxCurrent implements the api.ChargerEx interface
func (wb *OpenWB20) MaxCurrentMillis(current float64) error {
if current < 6 {
return fmt.Errorf("invalid current %.1f", current)
}
curr := uint16(current * 100)
err := wb.setCurrent(curr)
if err == nil {
wb.curr = curr
}
return err
}
var _ api.Meter = (*OpenWB20)(nil)
// CurrentPower implements the api.Meter interface
func (wb *OpenWB20) CurrentPower() (float64, error) {
b, err := wb.conn.ReadInputRegisters(wb.base+openwbRegPower, 2)
if err != nil {
return 0, err
}
return float64(int32(binary.BigEndian.Uint32(b))), nil
}
var _ api.MeterEnergy = (*OpenWB20)(nil)
// TotalEnergy implements the api.MeterEnergy interface
func (wb *OpenWB20) TotalEnergy() (float64, error) {
b, err := wb.conn.ReadInputRegisters(wb.base+openwbRegImport, 2)
if err != nil {
return 0, err
}
return float64(binary.BigEndian.Uint32(b)) / 1e3, nil
}
// getPhaseValues returns phase values
func (wb *OpenWB20) getPhaseValues(reg uint16) (float64, float64, float64, error) {
b, err := wb.conn.ReadInputRegisters(reg, 3)
if err != nil {
return 0, 0, 0, err
}
var res [3]float64
for i := range res {
res[i] = float64(int16(binary.BigEndian.Uint16(b[2*i:]))) / 100
}
return res[0], res[1], res[2], nil
}
var _ api.PhaseCurrents = (*OpenWB20)(nil)
// Currents implements the api.PhaseCurrents interface
func (wb *OpenWB20) Currents() (float64, float64, float64, error) {
return wb.getPhaseValues(wb.base + openwbRegCurrents)
}
var _ api.PhaseVoltages = (*OpenWB20)(nil)
// Voltages implements the api.PhaseVoltages interface
func (wb *OpenWB20) Voltages() (float64, float64, float64, error) {
return wb.getPhaseValues(wb.base + openwbRegVoltages)
}
var _ api.PhaseSwitcher = (*OpenWB20)(nil)
// Phases1p3p implements the api.PhaseSwitcher interface
func (wb *OpenWB20) Phases1p3p(phases int) error {
if _, err := wb.conn.WriteSingleRegister(wb.base+openwbRegPhaseTarget, uint16(phases)); err != nil {
return err
}
_, err := wb.conn.WriteSingleRegister(wb.base+openwbRegPhaseTrigger, 1)
return err
}
var _ api.Resurrector = (*OpenWB20)(nil)
// WakeUp implements the api.Resurrector interface
func (wb *OpenWB20) WakeUp() error {
_, err := wb.conn.WriteSingleRegister(wb.base+openwbRegCpTrigger, 1)
return err
}
// Identify implements the api.Identifier interface
func (wb *OpenWB20) identify() (string, error) {
b, err := wb.conn.ReadInputRegisters(wb.base+openwbRegRfid, 10)
if err != nil {
return "", err
}
return bytesAsString(b), nil
}