forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheebus.go
512 lines (414 loc) · 13 KB
/
eebus.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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
package charger
import (
"errors"
"fmt"
"os"
"sync"
"time"
"github.com/enbility/cemd/emobility"
"github.com/enbility/eebus-go/features"
"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/charger/eebus"
"github.com/evcc-io/evcc/core/loadpoint"
"github.com/evcc-io/evcc/provider"
"github.com/evcc-io/evcc/util"
)
//go:generate mockgen -package charger -destination eebus_test_mock.go github.com/enbility/cemd/emobility EmobilityI
const (
maxIdRequestTimespan = time.Second * 120
idleFactor = 0.6
voltage float64 = 230
)
type minMax struct {
min, max float64
}
type EEBus struct {
ski string
emobility emobility.EmobilityI
log *util.Logger
lp loadpoint.API
minMaxG func() (minMax, error)
communicationStandard emobility.EVCommunicationStandardType
expectedEnableUnpluggedState bool
current float64
// connection tracking for api.CurrentGetter
evConnected bool
currentLimit float64
lastIsChargingCheck time.Time
lastIsChargingResult bool
connected bool
connectedC chan bool
connectedTime time.Time
mux sync.Mutex
}
func init() {
registry.Add("eebus", NewEEBusFromConfig)
}
// NewEEBusFromConfig creates an EEBus charger from generic config
func NewEEBusFromConfig(other map[string]interface{}) (api.Charger, error) {
cc := struct {
Ski string
Ip string
Meter bool
ChargedEnergy bool
}{
ChargedEnergy: true,
}
if err := util.DecodeOther(other, &cc); err != nil {
return nil, err
}
return NewEEBus(cc.Ski, cc.Ip, cc.Meter, cc.ChargedEnergy)
}
//go:generate go run ../cmd/tools/decorate.go -f decorateEEBus -b *EEBus -r api.Charger -t "api.Meter,CurrentPower,func() (float64, error)" -t "api.PhaseCurrents,Currents,func() (float64, float64, float64, error)" -t "api.ChargeRater,ChargedEnergy,func() (float64, error)"
// NewEEBus creates EEBus charger
func NewEEBus(ski, ip string, hasMeter, hasChargedEnergy bool) (api.Charger, error) {
log := util.NewLogger("eebus")
if eebus.Instance == nil {
return nil, errors.New("eebus not configured")
}
c := &EEBus{
ski: ski,
log: log,
connectedC: make(chan bool, 1),
communicationStandard: emobility.EVCommunicationStandardTypeUnknown,
current: 6,
}
c.emobility = eebus.Instance.RegisterEVSE(ski, ip, c.onConnect, c.onDisconnect, nil)
c.minMaxG = provider.Cached(c.minMax, time.Second)
if err := c.waitForConnection(); err != nil {
return c, err
}
if hasMeter {
var energyG func() (float64, error)
if hasChargedEnergy {
energyG = c.chargedEnergy
}
return decorateEEBus(c, c.currentPower, c.currents, energyG), nil
}
return c, nil
}
// waitForConnection wait for initial connection and returns an error on failure
func (c *EEBus) waitForConnection() error {
timeout := time.After(90 * time.Second)
for {
select {
case <-timeout:
return os.ErrDeadlineExceeded
case connected := <-c.connectedC:
if connected {
return nil
}
}
}
}
func (c *EEBus) onConnect(ski string) {
c.log.TRACE.Println("connect ski:", ski)
c.expectedEnableUnpluggedState = false
c.setDefaultValues()
c.setConnected(true)
}
func (c *EEBus) onDisconnect(ski string) {
c.log.TRACE.Println("disconnect ski:", ski)
c.expectedEnableUnpluggedState = false
c.setConnected(false)
c.setDefaultValues()
}
func (c *EEBus) setDefaultValues() {
c.communicationStandard = emobility.EVCommunicationStandardTypeUnknown
c.lastIsChargingCheck = time.Now().Add(-time.Hour * 1)
c.lastIsChargingResult = false
}
func (c *EEBus) setConnected(connected bool) {
c.mux.Lock()
defer c.mux.Unlock()
if connected && !c.connected {
c.connectedTime = time.Now()
}
select {
case c.connectedC <- connected:
default:
}
c.connected = connected
}
func (c *EEBus) isConnected() bool {
c.mux.Lock()
defer c.mux.Unlock()
return c.connected
}
var _ api.CurrentLimiter = (*EEBus)(nil)
func (c *EEBus) minMax() (minMax, error) {
minLimits, maxLimits, _, err := c.emobility.EVCurrentLimits()
if err != nil {
if err == features.ErrDataNotAvailable {
err = api.ErrNotAvailable
}
return minMax{}, err
}
if len(minLimits) == 0 || len(maxLimits) == 0 {
return minMax{}, api.ErrNotAvailable
}
return minMax{minLimits[0], maxLimits[0]}, nil
}
func (c *EEBus) GetMinMaxCurrent() (float64, float64, error) {
minMax, err := c.minMaxG()
return minMax.min, minMax.max, err
}
// we assume that if any phase current value is > idleFactor * min Current, then charging is active and enabled is true
func (c *EEBus) isCharging() bool { // d *communication.EVSEClientDataType
// check if an external physical meter is assigned
// we only want this for configured meters and not for internal meters!
// right now it works as expected
if c.lp != nil && c.lp.HasChargeMeter() {
// we only check ever 10 seconds, maybe we can use the config interval duration
if time.Since(c.lastIsChargingCheck) >= 10*time.Second {
c.lastIsChargingCheck = time.Now()
c.lastIsChargingResult = false
// compare charge power for all phases to 0.6 * min. charge power of a single phase
if c.lp.GetChargePower() > c.lp.EffectiveMinPower()*idleFactor {
c.lastIsChargingResult = true
return true
}
} else if c.lastIsChargingResult {
return true
}
}
// The above doesn't (yet) work for built in meters, so check the EEBUS measurements also
currents, err := c.emobility.EVCurrentsPerPhase()
if err != nil {
return false
}
limitsMin, _, _, err := c.emobility.EVCurrentLimits()
if err != nil || limitsMin == nil || len(limitsMin) == 0 {
return false
}
var phasesCurrent float64
for _, phaseCurrent := range currents {
phasesCurrent += phaseCurrent
}
// require sum of all phase currents to be > 0.6 * a single phase minimum
// in some scenarios, e.g. Cayenne Hybrid, sometimes the meter of a PMCC device
// reported 600W, even tough the car was not charging
limitMin := limitsMin[0]
return phasesCurrent > limitMin*idleFactor
}
// Status implements the api.Charger interface
func (c *EEBus) Status() (api.ChargeStatus, error) {
if !c.isConnected() {
return api.StatusNone, api.ErrTimeout
}
if !c.emobility.EVConnected() {
c.expectedEnableUnpluggedState = false
c.evConnected = false
return api.StatusA, nil
}
if !c.evConnected {
c.evConnected = true
c.currentLimit = -1
}
currentState, err := c.emobility.EVCurrentChargeState()
if err != nil {
return api.StatusNone, err
}
switch currentState {
case emobility.EVChargeStateTypeUnknown, emobility.EVChargeStateTypeUnplugged: // Unplugged
c.expectedEnableUnpluggedState = false
return api.StatusA, nil
case emobility.EVChargeStateTypeFinished, emobility.EVChargeStateTypePaused: // Finished, Paused
return api.StatusB, nil
case emobility.EVChargeStateTypeActive: // Active
if c.isCharging() {
return api.StatusC, nil
}
return api.StatusB, nil
case emobility.EVChargeStateTypeError: // Error
return api.StatusF, nil
default:
return api.StatusNone, fmt.Errorf("%s properties unknown result: %s", c.ski, currentState)
}
}
// Enabled implements the api.Charger interface
// should return true if the charger allows the EV to draw power
func (c *EEBus) Enabled() (bool, error) {
// when unplugged there is no overload limit data available
state, err := c.Status()
if err != nil || state == api.StatusA {
return c.expectedEnableUnpluggedState, nil
}
// if the EV is charging
if state == api.StatusC {
return true, nil
}
limits, err := c.emobility.EVLoadControlObligationLimits()
if err != nil {
// there are no overload protection limits available, e.g. because the data was not received yet
return true, nil
}
for _, limit := range limits {
// for IEC61851 the pause limit is 0A, for ISO15118-2 it is 0.1A
// instead of checking for the actual data, hardcode this, so we might run into less
// timing issues as the data might not be received yet
if limit >= 1 {
return true, nil
}
}
return false, nil
}
// Enable implements the api.Charger interface
func (c *EEBus) Enable(enable bool) error {
// if the ev is unplugged or the state is unknown, there is nothing to be done
if state, err := c.Status(); err != nil || state == api.StatusA {
c.expectedEnableUnpluggedState = enable
return nil
}
// if we disable charging with a potential but not yet known communication standard ISO15118
// this would set allowed A value to be 0. And this would trigger ISO connections to switch to IEC!
if !enable {
comStandard, err := c.emobility.EVCommunicationStandard()
if err != nil || comStandard == emobility.EVCommunicationStandardTypeUnknown {
return api.ErrMustRetry
}
}
var current float64
if enable {
current = c.current
}
return c.writeCurrentLimitData([]float64{current, current, current})
}
// send current charging power limits to the EV
func (c *EEBus) writeCurrentLimitData(currents []float64) error {
comStandard, err := c.emobility.EVCommunicationStandard()
if err != nil {
return err
}
// Only send currents smaller than 6A if the communication standard is known.
// Otherwise this could cause ISO15118 capable OBCs to stick with IEC61851 when plugging
// the charge cable in. Or even worse show an error and the cable then needs to be unplugged,
// wait for the car to go into sleep and plug it back in.
// So if there are currents smaller than 6A with unknown communication standard change them to 6A.
// Keep in mind that this will still confuse evcc as it thinks charging is stopped, but it hasn't yet.
if comStandard == emobility.EVCommunicationStandardTypeUnknown {
minLimits, _, _, err := c.emobility.EVCurrentLimits()
if err == nil {
for index, current := range currents {
if index < len(minLimits) && current < minLimits[index] {
currents[index] = minLimits[index]
}
}
}
}
// Set overload protection limits and self consumption limits to identical values,
// so if the EV supports self consumption it will be used automatically.
if err = c.emobility.EVWriteLoadControlLimits(currents, currents); err == nil {
c.currentLimit = currents[0]
}
return err
}
// MaxCurrent implements the api.Charger interface
func (c *EEBus) MaxCurrent(current int64) error {
return c.MaxCurrentMillis(float64(current))
}
var _ api.ChargerEx = (*EEBus)(nil)
// MaxCurrentMillis implements the api.ChargerEx interface
func (c *EEBus) MaxCurrentMillis(current float64) error {
if !c.connected || !c.emobility.EVConnected() {
return errors.New("can't set new current as ev is unplugged")
}
if err := c.writeCurrentLimitData([]float64{current, current, current}); err != nil {
return err
}
c.current = current
return nil
}
var _ api.CurrentGetter = (*Easee)(nil)
// GetMaxCurrent implements the api.CurrentGetter interface
func (c *EEBus) GetMaxCurrent() (float64, error) {
return c.currentLimit, nil
}
// CurrentPower implements the api.Meter interface
func (c *EEBus) currentPower() (float64, error) {
if !c.emobility.EVConnected() {
return 0, nil
}
connectedPhases, err := c.emobility.EVConnectedPhases()
if err != nil {
return 0, err
}
powers, err := c.emobility.EVPowerPerPhase()
if err != nil {
return 0, err
}
var power float64
for index, phasePower := range powers {
if index >= int(connectedPhases) {
break
}
power += phasePower
}
return power, nil
}
// ChargedEnergy implements the api.ChargeRater interface
func (c *EEBus) chargedEnergy() (float64, error) {
if !c.emobility.EVConnected() {
return 0, nil
}
energy, err := c.emobility.EVChargedEnergy()
if err != nil {
return 0, err
}
return energy / 1e3, nil
}
// Currents implements the api.PhaseCurrents interface
func (c *EEBus) currents() (float64, float64, float64, error) {
if !c.emobility.EVConnected() {
return 0, 0, 0, nil
}
res, err := c.emobility.EVCurrentsPerPhase()
if err != nil {
if err == features.ErrDataNotAvailable {
err = api.ErrNotAvailable
}
return 0, 0, 0, err
}
// fill phases
for len(res) < 3 {
res = append(res, 0)
}
return res[0], res[1], res[2], nil
}
var _ api.Identifier = (*EEBus)(nil)
// Identify implements the api.Identifier interface
func (c *EEBus) Identify() (string, error) {
if !c.isConnected() || !c.emobility.EVConnected() {
return "", nil
}
if !c.emobility.EVConnected() {
return "", nil
}
if identification, _ := c.emobility.EVIdentification(); identification != "" {
return identification, nil
}
if comStandard, _ := c.emobility.EVCommunicationStandard(); comStandard == emobility.EVCommunicationStandardTypeIEC61851 {
return "", nil
}
if time.Since(c.connectedTime) < maxIdRequestTimespan {
return "", api.ErrMustRetry
}
return "", nil
}
var _ api.Battery = (*EEBus)(nil)
// Soc implements the api.Vehicle interface
func (c *EEBus) Soc() (float64, error) {
if socSupported, err := c.emobility.EVSoCSupported(); err != nil || !socSupported {
return 0, api.ErrNotAvailable
}
soc, err := c.emobility.EVSoC()
if err != nil {
return 0, api.ErrNotAvailable
}
return soc, nil
}
var _ loadpoint.Controller = (*EEBus)(nil)
// LoadpointControl implements loadpoint.Controller
func (c *EEBus) LoadpointControl(lp loadpoint.API) {
c.lp = lp
}