forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwallbe.go
233 lines (183 loc) Β· 5.56 KB
/
wallbe.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
package charger
import (
"encoding/binary"
"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/encoding"
"github.com/volkszaehler/mbmd/meters/rs485"
)
const (
wbSlaveID = 255
wbRegStatus = 100 // Input
wbRegChargeTime = 102 // Input
wbRegEnable = 400 // Coil
wbRegMaxCurrent = 528 // Holding
wbRegFirmware = 149 // Firmware
wbRegPower = 120 // power reading
wbRegEnergy = 128 // energy reading
wbRegEnergyDecimals = 904 // energy reading decimals
)
var wbRegCurrents = []uint16{114, 116, 118} // current readings
// Wallbe is an api.Charger implementation for Wallbe wallboxes.
// It supports both wallbe controllers (post 2019 models) and older ones using the
// Phoenix EV-CC-AC1-M3-CBC-RCM-ETH controller.
// It uses Modbus TCP to communicate with the wallbox at modbus client id 255.
type Wallbe struct {
conn *modbus.Connection
factor int64
}
func init() {
registry.Add("wallbe", NewWallbeFromConfig)
}
// go:generate go run ../cmd/tools/decorate.go -f decorateWallbe -b *Wallbe -r api.Charger -t "api.Meter,CurrentPower,func() (float64, error)" -t "api.MeterEnergy,TotalEnergy,func() (float64, error)" -t "api.PhaseCurrents,Currents,func() (float64, float64, float64, error)" -t "api.ChargerEx,MaxCurrentMillis,func(current float64) error"
// NewWallbeFromConfig creates a Wallbe charger from generic config
func NewWallbeFromConfig(other map[string]interface{}) (api.Charger, error) {
cc := struct {
URI string
Legacy bool
Meter struct {
Power, Energy, Currents bool
}
}{
URI: "192.168.0.8:502",
}
if err := util.DecodeOther(other, &cc); err != nil {
return nil, err
}
wb, err := NewWallbe(cc.URI)
if err != nil {
return nil, err
}
if cc.Legacy {
wb.factor = 1
}
var currentPower func() (float64, error)
if cc.Meter.Power {
currentPower = wb.currentPower
}
var totalEnergy func() (float64, error)
if cc.Meter.Energy {
totalEnergy = wb.totalEnergy
}
var currents func() (float64, float64, float64, error)
if cc.Meter.Currents {
currents = wb.currents
}
var maxCurrentMillis func(float64) error
if !cc.Legacy {
maxCurrentMillis = wb.maxCurrentMillis
}
return decorateWallbe(wb, currentPower, totalEnergy, currents, maxCurrentMillis), nil
}
// NewWallbe creates a Wallbe charger
func NewWallbe(uri string) (*Wallbe, error) {
conn, err := modbus.NewConnection(uri, "", "", 0, modbus.Tcp, wbSlaveID)
if err != nil {
return nil, err
}
log := util.NewLogger("wallbe")
conn.Logger(log.TRACE)
wb := &Wallbe{
conn: conn,
factor: 10,
}
return wb, nil
}
// Status implements the api.Charger interface
func (wb *Wallbe) Status() (api.ChargeStatus, error) {
b, err := wb.conn.ReadInputRegisters(wbRegStatus, 1)
if err != nil {
return api.StatusNone, err
}
return api.ChargeStatus(string(b[1])), nil
}
// Enabled implements the api.Charger interface
func (wb *Wallbe) Enabled() (bool, error) {
b, err := wb.conn.ReadCoils(wbRegEnable, 1)
if err != nil {
return false, err
}
return b[0] == 1, nil
}
// Enable implements the api.Charger interface
func (wb *Wallbe) Enable(enable bool) error {
var u uint16
if enable {
u = modbus.CoilOn
}
_, err := wb.conn.WriteSingleCoil(wbRegEnable, u)
return err
}
// MaxCurrent implements the api.Charger interface
func (wb *Wallbe) MaxCurrent(current int64) error {
if current < 6 {
return fmt.Errorf("invalid current %d", current)
}
u := uint16(current * wb.factor)
_, err := wb.conn.WriteSingleRegister(wbRegMaxCurrent, u)
return err
}
// maxCurrentMillis implements the api.ChargerEx interface
func (wb *Wallbe) maxCurrentMillis(current float64) error {
if current < 6 {
return fmt.Errorf("invalid current %.5g", current)
}
u := uint16(current * float64(wb.factor))
_, err := wb.conn.WriteSingleRegister(wbRegMaxCurrent, u)
return err
}
var _ api.ChargeTimer = (*Wallbe)(nil)
// ChargingTime implements the api.ChargeTimer interface
func (wb *Wallbe) ChargingTime() (time.Duration, error) {
b, err := wb.conn.ReadInputRegisters(wbRegChargeTime, 2)
if err != nil {
return 0, err
}
// 2 words, least significant word first
secs := uint64(b[1]) | uint64(b[0])<<8 | uint64(b[3])<<16 | uint64(b[2])<<24
return time.Duration(secs) * time.Second, nil
}
// currentPower implements the api.Meter interface
func (wb *Wallbe) currentPower() (float64, error) {
b, err := wb.conn.ReadInputRegisters(wbRegPower, 2)
if err != nil {
return 0, err
}
return rs485.RTUInt32ToFloat64Swapped(b), nil
}
// totalEnergy implements the api.MeterEnergy interface
func (wb *Wallbe) totalEnergy() (float64, error) {
b, err := wb.conn.ReadInputRegisters(wbRegEnergy, 2)
if err != nil {
return 0, err
}
res := rs485.RTUUint32ToFloat64Swapped(b)
d, err := wb.conn.ReadHoldingRegisters(wbRegEnergyDecimals, 1)
if err != nil {
return 0, err
}
res += float64(binary.BigEndian.Uint16(d)) / 1e3
return res, nil
}
// currents implements the api.PhaseCurrents interface
func (wb *Wallbe) currents() (float64, float64, float64, error) {
var currents []float64
for _, regCurrent := range wbRegCurrents {
b, err := wb.conn.ReadInputRegisters(regCurrent, 2)
if err != nil {
return 0, 0, 0, err
}
currents = append(currents, rs485.RTUInt32ToFloat64Swapped(b))
}
return currents[0], currents[1], currents[2], nil
}
var _ api.Diagnosis = (*Wallbe)(nil)
// Diagnose implements the api.Diagnosis interface
func (wb *Wallbe) Diagnose() {
if b, err := wb.conn.ReadInputRegisters(wbRegFirmware, 6); err == nil {
fmt.Printf("Firmware:\t%s\n", encoding.StringLsbFirst(b))
}
}