-
-
Notifications
You must be signed in to change notification settings - Fork 641
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make vehicles config global instead of per loadpoint (BC) (#3827)
- Loading branch information
Showing
21 changed files
with
297 additions
and
178 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package coordinator | ||
|
||
import ( | ||
"github.com/evcc-io/evcc/api" | ||
"github.com/evcc-io/evcc/core/loadpoint" | ||
) | ||
|
||
type adapter struct { | ||
lp loadpoint.API | ||
c *Coordinator | ||
} | ||
|
||
// NewAdapter exposes the coordinator for a given loadpoint. | ||
// Using an adapter simplifies the method signatures seen from the loadpoint. | ||
func NewAdapter(lp loadpoint.API, c *Coordinator) API { | ||
return &adapter{ | ||
lp: lp, | ||
c: c, | ||
} | ||
} | ||
|
||
func (a *adapter) GetVehicles() []api.Vehicle { | ||
return a.c.GetVehicles() | ||
} | ||
|
||
func (a *adapter) Acquire(v api.Vehicle) { | ||
a.c.acquire(a.lp, v) | ||
} | ||
|
||
func (a *adapter) Release(v api.Vehicle) { | ||
a.c.release(v) | ||
} | ||
|
||
func (a *adapter) IdentifyVehicleByStatus() api.Vehicle { | ||
return a.c.identifyVehicleByStatus(a.lp) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package coordinator | ||
|
||
import "github.com/evcc-io/evcc/api" | ||
|
||
// API is the coordinator API | ||
type API interface { | ||
GetVehicles() []api.Vehicle | ||
Acquire(api.Vehicle) | ||
Release(api.Vehicle) | ||
IdentifyVehicleByStatus() api.Vehicle | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package coordinator | ||
|
||
import ( | ||
"github.com/evcc-io/evcc/api" | ||
"github.com/evcc-io/evcc/core/loadpoint" | ||
"github.com/evcc-io/evcc/util" | ||
) | ||
|
||
// Coordinator coordinates vehicle access between loadpoints | ||
type Coordinator struct { | ||
log *util.Logger | ||
vehicles []api.Vehicle | ||
tracked map[api.Vehicle]loadpoint.API | ||
} | ||
|
||
// New creates a coordinator for a set of vehicles | ||
func New(log *util.Logger, vehicles []api.Vehicle) *Coordinator { | ||
return &Coordinator{ | ||
log: log, | ||
vehicles: vehicles, | ||
tracked: make(map[api.Vehicle]loadpoint.API), | ||
} | ||
} | ||
|
||
func (c *Coordinator) GetVehicles() []api.Vehicle { | ||
return c.vehicles | ||
} | ||
|
||
func (c *Coordinator) acquire(owner loadpoint.API, vehicle api.Vehicle) { | ||
if o, ok := c.tracked[vehicle]; ok && o != owner { | ||
o.SetVehicle(nil) | ||
} | ||
c.tracked[vehicle] = owner | ||
} | ||
|
||
func (c *Coordinator) release(vehicle api.Vehicle) { | ||
delete(c.tracked, vehicle) | ||
} | ||
|
||
// availableDetectibleVehicles is the list of vehicles that are currently not | ||
// associated to another loadpoint and have a status api that allows for detection | ||
func (c *Coordinator) availableDetectibleVehicles(owner loadpoint.API) []api.Vehicle { | ||
var res []api.Vehicle | ||
|
||
for _, vv := range c.vehicles { | ||
if _, ok := vv.(api.ChargeState); ok { | ||
if o, ok := c.tracked[vv]; o == owner || !ok { | ||
res = append(res, vv) | ||
} | ||
} | ||
} | ||
|
||
return res | ||
} | ||
|
||
// identifyVehicleByStatus finds active vehicle by charge state | ||
func (c *Coordinator) identifyVehicleByStatus(owner loadpoint.API) api.Vehicle { | ||
available := c.availableDetectibleVehicles(owner) | ||
|
||
var res api.Vehicle | ||
for _, vehicle := range available { | ||
if vs, ok := vehicle.(api.ChargeState); ok { | ||
status, err := vs.Status() | ||
if err != nil { | ||
c.log.ERROR.Println("vehicle status:", err) | ||
continue | ||
} | ||
|
||
c.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 { | ||
c.log.WARN.Println("vehicle status: >1 matches, giving up") | ||
return nil | ||
} | ||
|
||
res = vehicle | ||
} | ||
} | ||
} | ||
|
||
return res | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package coordinator | ||
|
||
import ( | ||
"github.com/evcc-io/evcc/api" | ||
) | ||
|
||
type dummy struct{} | ||
|
||
// NewDummy creates a dummy coordinator without vehicles | ||
func NewDummy() API { | ||
return &dummy{} | ||
} | ||
|
||
func (a *dummy) GetVehicles() []api.Vehicle { | ||
return nil | ||
} | ||
|
||
func (a *dummy) Acquire(v api.Vehicle) {} | ||
|
||
func (a *dummy) Release(v api.Vehicle) {} | ||
|
||
func (a *dummy) IdentifyVehicleByStatus() api.Vehicle { | ||
return nil | ||
} |
Oops, something went wrong.