forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoordinator.go
71 lines (55 loc) · 1.56 KB
/
coordinator.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
package core
import (
"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/util"
)
type vehicleCoordinator struct {
tracked map[api.Vehicle]interface{}
}
var coordinator *vehicleCoordinator
func init() {
coordinator = &vehicleCoordinator{
tracked: make(map[api.Vehicle]interface{}),
}
}
func (lp *vehicleCoordinator) acquire(owner interface{}, vehicle api.Vehicle) {
lp.tracked[vehicle] = owner
}
func (lp *vehicleCoordinator) release(vehicle api.Vehicle) {
delete(lp.tracked, vehicle)
}
func (lp *vehicleCoordinator) availableVehicles(owner interface{}, vehicles []api.Vehicle) []api.Vehicle {
var res []api.Vehicle
for _, vv := range vehicles {
if _, ok := vv.(api.ChargeState); ok {
if o, ok := lp.tracked[vv]; o == owner || !ok {
res = append(res, vv)
}
}
}
return res
}
// find active vehicle by charge state
func (lp *vehicleCoordinator) identifyVehicleByStatus(log *util.Logger, owner interface{}, vehicles []api.Vehicle) api.Vehicle {
available := lp.availableVehicles(owner, vehicles)
var res api.Vehicle
for _, vehicle := range available {
if vs, ok := vehicle.(api.ChargeState); ok {
status, err := vs.Status()
if err != nil {
log.ERROR.Println("vehicle status:", err)
continue
}
log.DEBUG.Printf("vehicle status: %s (%s)", status, vehicle.Title())
// vehicle is plugged or charging, so it should be the right one
if status == api.StatusB || status == api.StatusC {
if res != nil {
log.WARN.Println("vehicle status: >1 matches, giving up")
return nil
}
res = vehicle
}
}
}
return res
}