forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloadpoint_vehicle.go
362 lines (302 loc) · 8.82 KB
/
loadpoint_vehicle.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 core
import (
"errors"
"regexp"
"slices"
"strings"
"time"
"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/core/keys"
"github.com/evcc-io/evcc/core/session"
"github.com/evcc-io/evcc/core/soc"
"github.com/evcc-io/evcc/core/vehicle"
"github.com/evcc-io/evcc/provider"
)
const (
vehicleDetectInterval = 1 * time.Minute
vehicleDetectDuration = 10 * time.Minute
)
// coordinatedVehicles is the slice of vehicles from the coordinator
func (lp *Loadpoint) coordinatedVehicles() []api.Vehicle {
if lp.coordinator == nil {
return nil
}
return lp.coordinator.GetVehicles()
}
// setVehicleIdentifier updated the vehicle id as read from the charger
func (lp *Loadpoint) setVehicleIdentifier(id string) {
if lp.vehicleIdentifier != id {
lp.vehicleIdentifier = id
lp.publish(keys.VehicleIdentity, id)
}
}
// identifyVehicle reads vehicle identification from charger
func (lp *Loadpoint) identifyVehicle() {
identifier, ok := lp.charger.(api.Identifier)
if !ok {
return
}
id, err := identifier.Identify()
if err != nil {
lp.log.ERROR.Println("charger vehicle id:", err)
return
}
if lp.vehicleIdentifier == id {
return
}
// vehicle found or removed
lp.setVehicleIdentifier(id)
if id != "" {
lp.log.DEBUG.Println("charger vehicle id:", id)
if vehicle := lp.selectVehicleByID(id); vehicle != nil {
lp.stopVehicleDetection()
lp.setActiveVehicle(vehicle)
}
}
}
// selectVehicleByID selects the vehicle with the given ID
func (lp *Loadpoint) selectVehicleByID(id string) api.Vehicle {
vehicles := lp.coordinatedVehicles()
// find exact match
for _, vehicle := range vehicles {
for _, vid := range vehicle.Identifiers() {
if strings.EqualFold(id, vid) {
return vehicle
}
}
}
// find placeholder match
for _, vehicle := range vehicles {
for _, vid := range vehicle.Identifiers() {
// case insensitive match
re, err := regexp.Compile("(?i)" + strings.ReplaceAll(vid, "*", ".*?"))
if err != nil {
lp.log.ERROR.Printf("vehicle id: %v", err)
continue
}
if re.MatchString(id) {
return vehicle
}
}
}
return nil
}
// setActiveVehicle assigns currently active vehicle, configures soc estimator
// and adds an odometer task
func (lp *Loadpoint) setActiveVehicle(v api.Vehicle) {
lp.vmu.Lock()
from := "unknown"
if lp.vehicle != nil {
lp.coordinator.Release(lp.vehicle)
from = lp.vehicle.Title()
}
to := "unknown"
if v != nil {
lp.coordinator.Acquire(v)
to = v.Title()
}
lp.vehicle = v
lp.vmu.Unlock()
if from != to {
lp.log.INFO.Printf("vehicle updated: %s -> %s", from, to)
}
if v != nil {
lp.socUpdated = time.Time{}
// resolve optional config
var estimate bool
if lp.Soc.Estimate == nil || *lp.Soc.Estimate {
estimate = true
}
lp.socEstimator = soc.NewEstimator(lp.log, lp.charger, v, estimate)
lp.publish(keys.VehicleName, vehicle.Settings(lp.log, v).Name())
if mode, ok := v.OnIdentified().GetMode(); ok {
lp.SetMode(mode)
}
lp.addTask(lp.vehicleOdometer)
lp.progress.Reset()
} else {
lp.socEstimator = nil
lp.publish(keys.VehicleSoc, 0)
lp.publish(keys.VehicleName, "")
lp.publish(keys.VehicleOdometer, 0.0)
}
// re-publish vehicle settings
lp.publish(keys.PhasesActive, lp.ActivePhases())
lp.unpublishVehicle()
// publish effective values
lp.PublishEffectiveValues()
lp.updateSession(func(session *session.Session) {
var title string
if v != nil {
title = v.Title()
}
lp.session.Vehicle = title
})
}
func (lp *Loadpoint) wakeUpVehicle() {
// charger
if c, ok := lp.charger.(api.Resurrector); ok {
lp.log.DEBUG.Println("wake-up charger")
if err := c.WakeUp(); err != nil {
lp.log.ERROR.Printf("wake-up charger: %v", err)
}
}
// vehicle
if vs, ok := lp.GetVehicle().(api.Resurrector); ok {
lp.log.DEBUG.Println("wake-up vehicle")
if err := vs.WakeUp(); err != nil {
lp.log.ERROR.Printf("wake-up vehicle: %v", err)
}
}
}
// unpublishVehicle resets published vehicle data
func (lp *Loadpoint) unpublishVehicle() {
lp.vehicleSoc = 0
lp.publish(keys.VehicleClimaterActive, nil)
lp.publish(keys.VehicleSoc, 0.0)
lp.publish(keys.VehicleRange, int64(0))
lp.publish(keys.VehicleTargetSoc, 0.0)
lp.setRemainingEnergy(0)
lp.setRemainingDuration(0)
}
// vehicleHasFeature checks availability of vehicle feature
func (lp *Loadpoint) vehicleHasFeature(f api.Feature) bool {
v, ok := lp.GetVehicle().(api.FeatureDescriber)
if ok {
ok = slices.Contains(v.Features(), f)
}
return ok
}
// vehicleUnidentified returns true if there are associated vehicles and detection is running.
// It will also reset the api cache at regular intervals.
// Detection is stopped after maximum duration and the "guest vehicle" message dispatched.
func (lp *Loadpoint) vehicleUnidentified() bool {
if lp.vehicle != nil || lp.vehicleDetect.IsZero() || len(lp.coordinatedVehicles()) == 0 {
return false
}
// stop detection
if lp.clock.Since(lp.vehicleDetect) > vehicleDetectDuration {
lp.stopVehicleDetection()
lp.pushEvent(evVehicleUnidentified)
return false
}
// request vehicle api refresh while waiting to identify
select {
case <-lp.vehicleDetectTicker.C:
lp.log.DEBUG.Println("vehicle api refresh")
provider.ResetCached()
default:
}
return true
}
// vehicleDefaultOrDetect will assign and update default vehicle or start detection
func (lp *Loadpoint) vehicleDefaultOrDetect() {
if lp.defaultVehicle != nil {
if lp.vehicle != lp.defaultVehicle {
lp.setActiveVehicle(lp.defaultVehicle)
} else {
// default vehicle is already active, update odometer anyway
// need to do this here since setActiveVehicle would short-circuit
lp.addTask(lp.vehicleOdometer)
}
} else if len(lp.coordinatedVehicles()) > 0 && lp.connected() {
lp.startVehicleDetection()
}
}
// startVehicleDetection reset connection timer and starts api refresh timer
func (lp *Loadpoint) startVehicleDetection() {
// flush all vehicles before detection starts
lp.log.DEBUG.Println("vehicle api refresh")
provider.ResetCached()
lp.vehicleDetect = lp.clock.Now()
lp.vehicleDetectTicker = lp.clock.Ticker(vehicleDetectInterval)
lp.publish(keys.VehicleDetectionActive, true)
}
// stopVehicleDetection expires the connection timer and ticker
func (lp *Loadpoint) stopVehicleDetection() {
lp.vehicleDetect = time.Time{}
if lp.vehicleDetectTicker != nil {
lp.vehicleDetectTicker.Stop()
}
lp.publish(keys.VehicleDetectionActive, false)
}
// identifyVehicleByStatus validates if the active vehicle is still connected to the loadpoint
func (lp *Loadpoint) identifyVehicleByStatus() {
if len(lp.coordinatedVehicles()) == 0 {
return
}
if vehicle := lp.coordinator.IdentifyVehicleByStatus(); vehicle != nil {
lp.stopVehicleDetection()
lp.setActiveVehicle(vehicle)
return
}
// remove previous vehicle if status was not confirmed
if _, ok := lp.GetVehicle().(api.ChargeState); ok {
lp.setActiveVehicle(nil)
}
}
// vehicleOdometer updates odometer
func (lp *Loadpoint) vehicleOdometer() {
if vs, ok := lp.GetVehicle().(api.VehicleOdometer); ok {
if odo, err := vs.Odometer(); err == nil {
lp.log.DEBUG.Printf("vehicle odometer: %.0fkm", odo)
lp.publish(keys.VehicleOdometer, odo)
// update session once odometer is read
lp.updateSession(func(session *session.Session) {
session.Odometer = &odo
})
} else if !errors.Is(err, api.ErrNotAvailable) {
lp.log.ERROR.Printf("vehicle odometer: %v", err)
}
}
}
// vehicleClimatePollAllowed determines if polling depending on mode and connection status
func (lp *Loadpoint) vehicleClimatePollAllowed() bool {
switch {
case lp.Soc.Poll.Mode == pollCharging && lp.charging():
return true
case (lp.Soc.Poll.Mode == pollConnected || lp.Soc.Poll.Mode == pollAlways) && lp.connected():
return true
default:
return false
}
}
// vehicleSocPollAllowed validates charging state against polling mode
func (lp *Loadpoint) vehicleSocPollAllowed() bool {
// always update soc when charging
if lp.charging() {
return true
}
// update if connected and soc unknown
if lp.connected() && lp.socUpdated.IsZero() {
return true
}
remaining := lp.Soc.Poll.Interval - lp.clock.Since(lp.socUpdated)
honourUpdateInterval := lp.Soc.Poll.Mode == pollAlways ||
lp.connected() && lp.Soc.Poll.Mode == pollConnected
if honourUpdateInterval {
if remaining > 0 {
lp.log.DEBUG.Printf("next soc poll remaining time: %v", remaining.Truncate(time.Second))
} else {
return true
}
}
return false
}
// vehicleClimateActive checks if vehicle has active climate request
func (lp *Loadpoint) vehicleClimateActive() bool {
if cl, ok := lp.GetVehicle().(api.VehicleClimater); ok && lp.vehicleClimatePollAllowed() {
active, err := cl.Climater()
if err == nil {
if active {
lp.log.DEBUG.Println("climater active")
}
lp.publish(keys.VehicleClimaterActive, active)
return active
}
if !errors.Is(err, api.ErrNotAvailable) {
lp.log.ERROR.Printf("climater: %v", err)
}
}
return false
}