chore: use rw mutex for vehicle

This commit is contained in:
andig 2023-12-18 14:03:36 +01:00
parent 733579d3c7
commit 7dd0666f70
3 changed files with 8 additions and 8 deletions

View file

@ -105,8 +105,8 @@ type Loadpoint struct {
// exposed public configuration
sync.RWMutex // guard status
vehicleMux sync.Mutex // guard vehicle
Mode_ api.ChargeMode `mapstructure:"mode"` // Default charge mode, used for disconnect
vmu sync.RWMutex // guard vehicle
Mode_ api.ChargeMode `mapstructure:"mode"` // Default charge mode, used for disconnect
Title_ string `mapstructure:"title"` // UI title
Priority_ int `mapstructure:"priority"` // Priority

View file

@ -418,8 +418,8 @@ func (lp *Loadpoint) setRemainingEnergy(chargeRemainingEnergy float64) {
// GetVehicle gets the active vehicle
func (lp *Loadpoint) GetVehicle() api.Vehicle {
lp.vehicleMux.Lock()
defer lp.vehicleMux.Unlock()
lp.vmu.RLock()
defer lp.vmu.RUnlock()
return lp.vehicle
}
@ -428,8 +428,8 @@ func (lp *Loadpoint) SetVehicle(vehicle api.Vehicle) {
// set desired vehicle (protected by lock, no locking here)
lp.setActiveVehicle(vehicle)
lp.vehicleMux.Lock()
defer lp.vehicleMux.Unlock()
lp.vmu.Lock()
defer lp.vmu.Unlock()
// disable auto-detect
lp.stopVehicleDetection()

View file

@ -101,7 +101,7 @@ func (lp *Loadpoint) selectVehicleByID(id string) api.Vehicle {
// setActiveVehicle assigns currently active vehicle, configures soc estimator
// and adds an odometer task
func (lp *Loadpoint) setActiveVehicle(v api.Vehicle) {
lp.vehicleMux.Lock()
lp.vmu.Lock()
from := "unknown"
if lp.vehicle != nil {
@ -115,7 +115,7 @@ func (lp *Loadpoint) setActiveVehicle(v api.Vehicle) {
}
lp.vehicle = v
lp.vehicleMux.Unlock()
lp.vmu.Unlock()
if from != to {
lp.log.INFO.Printf("vehicle updated: %s -> %s", from, to)