Refactor session settings and apis (BC) (#10335)

This commit is contained in:
andig 2023-12-02 20:56:30 +05:00 • committed by GitHub
parent 66143c2c61
commit d63dec9511
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
116 changed files with 4073 additions and 2539 deletions

View file

@ -1,8 +1,6 @@
package coordinator
import (
"slices"
"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/core/loadpoint"
)
@ -25,8 +23,8 @@ func (a *adapter) GetVehicles() []api.Vehicle {
return a.c.GetVehicles()
}
func (a *adapter) GetVehicleIndex(v api.Vehicle) int {
return slices.Index(a.c.vehicles, v)
func (a *adapter) Owner(v api.Vehicle) loadpoint.API {
return a.c.Owner(v)
}
func (a *adapter) Acquire(v api.Vehicle) {

View file

@ -1,12 +1,24 @@
package coordinator
import "github.com/evcc-io/evcc/api"
import (
"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/core/loadpoint"
)
// API is the coordinator API
type API interface {
// GetVehicles returns the list of all vehicles
GetVehicles() []api.Vehicle
// Owner returns the loadpoint that currently owns the vehicle
Owner(api.Vehicle) loadpoint.API
// Aquire acquires the vehicle for the loadpoint and releases it at any other loadpoint
Acquire(api.Vehicle)
// Release releases a vehicle from a loadpoint
Release(api.Vehicle)
// IdentifyVehicleByStatus returns an available vehicle that is currently connected or charging
IdentifyVehicleByStatus() api.Vehicle
GetVehicleIndex(api.Vehicle) int
}

View file

@ -1,6 +1,7 @@
package coordinator
import (
"slices"
"sync"
"github.com/evcc-io/evcc/api"
@ -10,7 +11,7 @@ import (
// Coordinator coordinates vehicle access between loadpoints
type Coordinator struct {
mu sync.Mutex
mu sync.RWMutex
log *util.Logger
vehicles []api.Vehicle
tracked map[api.Vehicle]loadpoint.API
@ -27,12 +28,25 @@ func New(log *util.Logger, vehicles []api.Vehicle) *Coordinator {
// GetVehicles returns the list of all vehicles
func (c *Coordinator) GetVehicles() []api.Vehicle {
c.mu.Lock()
defer c.mu.Unlock()
c.mu.RLock()
defer c.mu.RUnlock()
return c.vehicles
return slices.Clone(c.vehicles)
}
// Owner returns the loadpoint that currently owns the vehicle
func (c *Coordinator) Owner(vehicle api.Vehicle) loadpoint.API {
c.mu.RLock()
defer c.mu.RUnlock()
if owner, ok := c.tracked[vehicle]; ok {
return owner
}
return nil
}
// Add adds a vehicle to the coordinator
func (c *Coordinator) Add(vehicle api.Vehicle) {
c.mu.Lock()
defer c.mu.Unlock()
@ -40,6 +54,7 @@ func (c *Coordinator) Add(vehicle api.Vehicle) {
c.vehicles = append(c.vehicles, vehicle)
}
// Delete removes a vehicle from the coordinator
func (c *Coordinator) Delete(vehicle api.Vehicle) {
c.mu.Lock()
@ -90,8 +105,8 @@ func (c *Coordinator) release(vehicle api.Vehicle) {
func (c *Coordinator) availableDetectibleVehicles(owner loadpoint.API) []api.Vehicle {
var res []api.Vehicle
c.mu.Lock()
defer c.mu.Unlock()
c.mu.RLock()
defer c.mu.RUnlock()
for _, vv := range c.vehicles {
// status api available
@ -110,8 +125,8 @@ func (c *Coordinator) availableDetectibleVehicles(owner loadpoint.API) []api.Veh
func (c *Coordinator) identifyVehicleByStatus(available []api.Vehicle) api.Vehicle {
var res api.Vehicle
c.mu.Lock()
defer c.mu.Unlock()
c.mu.RLock()
defer c.mu.RUnlock()
for _, vehicle := range available {
if vs, ok := vehicle.(api.ChargeState); ok {

View file

@ -2,27 +2,28 @@ package coordinator
import (
"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/core/loadpoint"
)
type dummy struct{}
// NewDummy creates a dummy coordinator without vehicles
func NewDummy() API {
return &dummy{}
return new(dummy)
}
func (a *dummy) GetVehicles() []api.Vehicle {
return nil
}
func (a *dummy) Acquire(v api.Vehicle) {}
func (a *dummy) Owner(api.Vehicle) loadpoint.API {
return nil
}
func (a *dummy) Release(v api.Vehicle) {}
func (a *dummy) Acquire(api.Vehicle) {}
func (a *dummy) Release(api.Vehicle) {}
func (a *dummy) IdentifyVehicleByStatus() api.Vehicle {
return nil
}
func (a *dummy) GetVehicleIndex(v api.Vehicle) int {
return -1
}