From c342880b6350e66ab099476a5cb19931af63dded Mon Sep 17 00:00:00 2001 From: andig Date: Sun, 6 Nov 2022 12:07:35 +0100 Subject: [PATCH] Fix a race condition when stopping the charging session --- core/loadpoint.go | 16 ++++++++-------- core/loadpoint_api.go | 16 +++++++++++++++- core/loadpoint_database.go | 2 +- core/site.go | 1 + 4 files changed, 25 insertions(+), 10 deletions(-) diff --git a/core/loadpoint.go b/core/loadpoint.go index f21647823..6c7892d52 100644 --- a/core/loadpoint.go +++ b/core/loadpoint.go @@ -412,8 +412,8 @@ func (lp *LoadPoint) evVehicleConnectHandler() { lp.log.INFO.Printf("car connected") // energy - lp.chargedEnergy = 0 - lp.publish("chargedEnergy", lp.chargedEnergy) + lp.setChargedEnergy(0) + lp.publish("chargedEnergy", lp.getChargedEnergy()) // duration lp.connectedTime = lp.clock.Now() @@ -446,7 +446,7 @@ func (lp *LoadPoint) evVehicleDisconnectHandler() { lp.resetMeasuredPhases() // energy and duration - lp.publish("chargedEnergy", lp.chargedEnergy) + lp.publish("chargedEnergy", lp.getChargedEnergy()) lp.publish("connectedDuration", lp.clock.Since(lp.connectedTime)) // remove charger vehicle id and stop potential detection @@ -698,7 +698,7 @@ func (lp *LoadPoint) setStatus(status api.ChargeStatus) { func (lp *LoadPoint) targetEnergyReached() bool { return (lp.vehicle == nil || lp.vehicleHasFeature(api.Offline)) && lp.targetEnergy > 0 && - lp.chargedEnergy/1e3 >= float64(lp.targetEnergy) + lp.getChargedEnergy()/1e3 >= float64(lp.targetEnergy) } // targetSocReached checks if target is configured and reached. @@ -1510,7 +1510,7 @@ func (lp *LoadPoint) updateChargeCurrents() { // publish charged energy and duration func (lp *LoadPoint) publishChargeProgress() { if f, err := lp.chargeRater.ChargedEnergy(); err == nil { - lp.chargedEnergy = 1e3 * f // convert to Wh + lp.setChargedEnergy(1e3 * f) // convert to Wh } else { lp.log.ERROR.Printf("charge rater: %v", err) } @@ -1521,7 +1521,7 @@ func (lp *LoadPoint) publishChargeProgress() { lp.log.ERROR.Printf("charge timer: %v", err) } - lp.publish("chargedEnergy", lp.chargedEnergy) + lp.publish("chargedEnergy", lp.getChargedEnergy()) lp.publish("chargeDuration", lp.chargeDuration) if _, ok := lp.chargeMeter.(api.MeterEnergy); ok { lp.publish("chargeTotalImport", lp.chargeMeterTotal()) @@ -1566,7 +1566,7 @@ func (lp *LoadPoint) publishSoCAndRange() { // guard for socEstimator removed by api if se := lp.socEstimator; se != nil { lp.socUpdated = lp.clock.Now() - f, err = se.SoC(lp.chargedEnergy) + f, err = se.SoC(lp.getChargedEnergy()) } else { return } @@ -1708,7 +1708,7 @@ func (lp *LoadPoint) Update(sitePower float64, cheap, batteryBuffered bool) { } case lp.targetEnergyReached(): - lp.log.DEBUG.Printf("targetEnergy reached: %.0fkWh > %dkWh", lp.chargedEnergy/1e3, lp.targetEnergy) + lp.log.DEBUG.Printf("targetEnergy reached: %.0fkWh > %dkWh", lp.getChargedEnergy()/1e3, lp.targetEnergy) err = lp.disableUnlessClimater() case lp.targetSocReached(): diff --git a/core/loadpoint_api.go b/core/loadpoint_api.go index 766992569..a7f41380c 100644 --- a/core/loadpoint_api.go +++ b/core/loadpoint_api.go @@ -49,7 +49,21 @@ func (lp *LoadPoint) SetMode(mode api.ChargeMode) { } } -// GetTargetSoC returns loadpoint charge target energy +// getChargedEnergy returns loadpoint charge target energy +func (lp *LoadPoint) getChargedEnergy() float64 { + lp.Lock() + defer lp.Unlock() + return lp.chargedEnergy +} + +// setChargedEnergy returns loadpoint charge target energy +func (lp *LoadPoint) setChargedEnergy(energy float64) { + lp.Lock() + defer lp.Unlock() + lp.chargedEnergy = energy +} + +// GetTargetEnergy returns loadpoint charge target energy func (lp *LoadPoint) GetTargetEnergy() int { lp.Lock() defer lp.Unlock() diff --git a/core/loadpoint_database.go b/core/loadpoint_database.go index 8d09a5974..0553a1df8 100644 --- a/core/loadpoint_database.go +++ b/core/loadpoint_database.go @@ -48,7 +48,7 @@ func (lp *LoadPoint) stopSession() { return } - lp.session.Stop(lp.chargedEnergy, lp.chargeMeterTotal()) + lp.session.Stop(lp.getChargedEnergy(), lp.chargeMeterTotal()) lp.db.Persist(lp.session) } diff --git a/core/site.go b/core/site.go index 59613e659..48cb66081 100644 --- a/core/site.go +++ b/core/site.go @@ -117,6 +117,7 @@ func NewSiteFromConfig( return nil, err } + // NOTE: this requires stopSession to respect async access shutdown.Register(lp.stopSession) } }