forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
keba-modbus.go
362 lines (300 loc) Β· 10.2 KB
/
keba-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
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
package charger
// LICENSE
// Copyright (c) 2023 andig
// This module is NOT covered by the MIT license. All rights reserved.
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
import (
"encoding/binary"
"encoding/hex"
"fmt"
"strconv"
"strings"
"time"
"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/util"
"github.com/evcc-io/evcc/util/modbus"
"github.com/evcc-io/evcc/util/sponsor"
)
// https://www.keba.com/en/emobility/service-support/downloads/Downloads
// https://www.keba.com/download/x/dea7ae6b84/kecontactp30modbustcp_pgen.pdf
// Keba is an api.Charger implementation
type Keba struct {
*embed
log *util.Logger
conn *modbus.Connection
}
const (
kebaRegChargingState = 1000
kebaRegCableState = 1004
kebaRegCurrents = 1008 // 6 regs, mA
kebaRegSerial = 1014 // leading zeros trimmed
kebaRegProduct = 1016
kebaRegFirmware = 1018
kebaRegPower = 1020 // mW
kebaRegEnergy = 1036 // Wh
kebaRegVoltages = 1040 // 6 regs, V
kebaRegRfid = 1500 // hex
kebaRegSessionEnergy = 1502 // Wh
kebaRegPhaseSource = 1550
kebaRegPhaseState = 1552
kebaRegFailsafeTimeout = 1602
kebaRegMaxCurrent = 5004 // mA
kebaRegEnable = 5014
kebaRegTriggerPhase = 5052
)
func init() {
registry.Add("keba-modbus", NewKebaFromConfig)
}
//go:generate go run ../cmd/tools/decorate.go -f decorateKeba -b *Keba -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.Identifier,Identify,func() (string, error)" -t "api.StatusReasoner,StatusReason,func() (api.Reason, error)" -t "api.PhaseSwitcher,Phases1p3p,func(int) error" -t "api.PhaseGetter,GetPhases,func() (int, error)"
// NewKebaFromConfig creates a new Keba ModbusTCP charger
func NewKebaFromConfig(other map[string]interface{}) (api.Charger, error) {
cc := struct {
embed `mapstructure:",squash"`
modbus.TcpSettings `mapstructure:",squash"`
}{
TcpSettings: modbus.TcpSettings{
ID: 255,
},
}
if err := util.DecodeOther(other, &cc); err != nil {
return nil, err
}
wb, err := NewKeba(cc.embed, cc.URI, cc.ID)
if err != nil {
return nil, err
}
// features
var (
currentPower, totalEnergy func() (float64, error)
currents func() (float64, float64, float64, error)
identify func() (string, error)
reason func() (api.Reason, error)
)
b, err := wb.conn.ReadHoldingRegisters(kebaRegProduct, 2)
if err != nil {
return nil, err
}
if features := binary.BigEndian.Uint32(b); (features/10)%10 > 0 {
currentPower = wb.currentPower
totalEnergy = wb.totalEnergy
currents = wb.currents
}
if features := binary.BigEndian.Uint32(b); features%10 > 0 {
identify = wb.identify
reason = wb.statusReason
}
// phases
var (
phasesS func(int) error
phasesG func() (int, error)
)
if b, err := wb.conn.ReadHoldingRegisters(kebaRegPhaseSource, 2); err == nil {
if source := binary.BigEndian.Uint32(b); source == 3 {
phasesS = wb.phases1p3p
phasesG = wb.getPhases
}
}
// failsafe
b, err = wb.conn.ReadHoldingRegisters(kebaRegFailsafeTimeout, 2)
if err != nil {
return nil, fmt.Errorf("failsafe timeout: %w", err)
}
if u := binary.BigEndian.Uint32(b); u > 0 {
go wb.heartbeat(time.Duration(u) * time.Second / 2)
}
return decorateKeba(wb, currentPower, totalEnergy, currents, identify, reason, phasesS, phasesG), nil
}
// NewKeba creates a new charger
func NewKeba(embed embed, uri string, slaveID uint8) (*Keba, error) {
conn, err := modbus.NewConnection(uri, "", "", 0, modbus.Tcp, slaveID)
if err != nil {
return nil, err
}
if !sponsor.IsAuthorized() {
return nil, api.ErrSponsorRequired
}
log := util.NewLogger("keba")
conn.Logger(log.TRACE)
wb := &Keba{
embed: &embed,
log: log,
conn: conn,
}
return wb, err
}
func (wb *Keba) heartbeat(timeout time.Duration) {
for range time.Tick(timeout) {
if _, err := wb.Enabled(); err != nil {
wb.log.ERROR.Println("heartbeat:", err)
}
}
}
func (wb *Keba) isConnected() (bool, error) {
b, err := wb.conn.ReadHoldingRegisters(kebaRegCableState, 2)
if err != nil {
return false, err
}
// 0: No cable is plugged.
// 1: Cable is connected to the charging station (not to the electric vehicle).
// 3: Cable is connected to the charging station and locked (not to the electric vehicle).
// 5: Cable is connected to the charging station and the electric vehicle (not locked).
// 7: Cable is connected to the charging station and the electric vehicle and locked (charging).
return binary.BigEndian.Uint32(b)&(1<<2) != 0, err
}
func (wb *Keba) getChargingState() (uint32, error) {
b, err := wb.conn.ReadHoldingRegisters(kebaRegChargingState, 2)
if err != nil {
return 0, err
}
// 0: Start-up of the charging station
// 1: The charging station is not ready for charging. The charging station is not connected to an electric vehicle, it is locked by the authorization function or another mechanism.
// 2: The charging station is ready for charging and waits for a reaction from the electric vehicle.
// 3: A charging process is active.
// 4: An error has occurred.
// 5: The charging process is temporarily interrupted because the temperature is too high or the wallbox is in suspended mode.
return binary.BigEndian.Uint32(b), nil
}
// Status implements the api.Charger interface
func (wb *Keba) Status() (api.ChargeStatus, error) {
if connected, err := wb.isConnected(); err != nil || !connected {
return api.StatusA, err
}
s, err := wb.getChargingState()
if err != nil {
return api.StatusA, err
}
if s == 3 {
return api.StatusC, nil
}
return api.StatusB, nil
}
// statusReason implements the api.StatusReasoner interface
func (wb *Keba) statusReason() (api.Reason, error) {
if connected, err := wb.isConnected(); err != nil || !connected {
return api.ReasonUnknown, err
}
if s, err := wb.getChargingState(); err != nil || s != 1 {
return api.ReasonUnknown, err
}
return api.ReasonWaitingForAuthorization, nil
}
// Enabled implements the api.Charger interface
func (wb *Keba) Enabled() (bool, error) {
s, err := wb.getChargingState()
if err != nil {
return false, err
}
return !(s == 5 || s == 1), nil
}
// Enable implements the api.Charger interface
func (wb *Keba) Enable(enable bool) error {
var u uint16
if enable {
u = 1
}
_, err := wb.conn.WriteSingleRegister(kebaRegEnable, u)
return err
}
// MaxCurrent implements the api.Charger interface
func (wb *Keba) MaxCurrent(current int64) error {
return wb.MaxCurrentMillis(float64(current))
}
var _ api.ChargerEx = (*Keba)(nil)
// MaxCurrentMillis implements the api.ChargerEx interface
func (wb *Keba) MaxCurrentMillis(current float64) error {
u := uint16(current * 1000)
_, err := wb.conn.WriteSingleRegister(kebaRegMaxCurrent, u)
return err
}
// currentPower implements the api.Meter interface
func (wb *Keba) currentPower() (float64, error) {
b, err := wb.conn.ReadHoldingRegisters(kebaRegPower, 2)
if err != nil {
return 0, err
}
return float64(binary.BigEndian.Uint32(b)) / 1e3, nil
}
// totalEnergy implements the api.MeterEnergy interface
func (wb *Keba) totalEnergy() (float64, error) {
b, err := wb.conn.ReadHoldingRegisters(kebaRegEnergy, 2)
if err != nil {
return 0, err
}
return float64(binary.BigEndian.Uint32(b)) / 1e4, nil
}
// chargedEnergy is not supported since Keba does not reset it when plugging in a new car
// currents implements the api.PhaseCurrents interface
func (wb *Keba) currents() (float64, float64, float64, error) {
var res [3]float64
for i := range res {
// does not support reading across register boundaries
b, err := wb.conn.ReadHoldingRegisters(kebaRegCurrents+2*uint16(i), 2)
if err != nil {
return 0, 0, 0, err
}
res[i] = float64(binary.BigEndian.Uint32(b)) / 1e3
}
return res[0], res[1], res[2], nil
}
// identify implements the api.Identifier interface
func (wb *Keba) identify() (string, error) {
b, err := wb.conn.ReadHoldingRegisters(kebaRegRfid, 2)
if err != nil {
return "", err
}
id := hex.EncodeToString(b)
if id == "00000000" {
id = ""
}
return id, nil
}
// phases1p3p implements the api.PhaseSwitcher interface
func (wb *Keba) phases1p3p(phases int) error {
var u uint16
if phases == 3 {
u = 1
}
_, err := wb.conn.WriteSingleRegister(kebaRegTriggerPhase, u)
return err
}
// getPhases implements the api.PhaseGetter interface
func (wb *Keba) getPhases() (int, error) {
b, err := wb.conn.ReadHoldingRegisters(kebaRegPhaseState, 2)
if err != nil {
return 0, err
}
if binary.BigEndian.Uint32(b) == 0 {
return 1, nil
}
return 3, nil
}
var _ api.Diagnosis = (*Keba)(nil)
// Diagnose implements the api.Diagnosis interface
func (wb *Keba) Diagnose() {
if b, err := wb.conn.ReadHoldingRegisters(kebaRegSerial, 2); err == nil {
fmt.Printf("\tSerial:\t%s\n", strings.TrimLeft(strconv.Itoa(int(binary.BigEndian.Uint32(b))), "0"))
}
if b, err := wb.conn.ReadHoldingRegisters(kebaRegFirmware, 2); err == nil {
fmt.Printf("\tFirmware:\t%d.%d.%d\n", b[0], b[1], b[2])
}
if b, err := wb.conn.ReadHoldingRegisters(kebaRegProduct, 2); err == nil {
fmt.Printf("\tProduct:\t%6d\n", binary.BigEndian.Uint32(b))
}
if b, err := wb.conn.ReadHoldingRegisters(kebaRegPhaseSource, 2); err == nil {
fmt.Printf("\tPhases source:\t%d\n", binary.BigEndian.Uint32(b))
}
if b, err := wb.conn.ReadHoldingRegisters(kebaRegPhaseState, 2); err == nil {
fmt.Printf("\tPhases state:\t%d\n", binary.BigEndian.Uint32(b))
}
if b, err := wb.conn.ReadHoldingRegisters(kebaRegFailsafeTimeout, 2); err == nil {
fmt.Printf("\tFailsafe timeout:\t%ds\n", binary.BigEndian.Uint32(b))
}
}