Fix crash on soc estimator nil due to async api call
This commit is contained in:
parent
7d3b649bfc
commit
a65d688bf9
1 changed files with 38 additions and 26 deletions
|
|
@ -1465,41 +1465,53 @@ func (lp *LoadPoint) publishSoCAndRange() {
|
|||
}
|
||||
|
||||
if lp.socPollAllowed() || lp.socProvidedByCharger() {
|
||||
lp.socUpdated = lp.clock.Now()
|
||||
var f float64
|
||||
var err error
|
||||
|
||||
f, err := lp.socEstimator.SoC(lp.chargedEnergy)
|
||||
if err == nil {
|
||||
lp.vehicleSoc = math.Trunc(f)
|
||||
lp.log.DEBUG.Printf("vehicle soc: %.0f%%", lp.vehicleSoc)
|
||||
lp.publish("vehicleSoC", lp.vehicleSoc)
|
||||
|
||||
if lp.charging() {
|
||||
lp.setRemainingDuration(lp.socEstimator.RemainingChargeDuration(lp.chargePower, lp.SoC.target))
|
||||
} else {
|
||||
lp.setRemainingDuration(-1)
|
||||
}
|
||||
|
||||
lp.setRemainingEnergy(1e3 * lp.socEstimator.RemainingChargeEnergy(lp.SoC.target))
|
||||
|
||||
// range
|
||||
if vs, ok := lp.vehicle.(api.VehicleRange); ok {
|
||||
if rng, err := vs.Range(); err == nil {
|
||||
lp.log.DEBUG.Printf("vehicle range: %dkm", rng)
|
||||
lp.publish("vehicleRange", rng)
|
||||
}
|
||||
}
|
||||
|
||||
// trigger message after variables are updated
|
||||
lp.bus.Publish(evVehicleSoC, f)
|
||||
// guard for socEstimator removed by api
|
||||
if se := lp.socEstimator; se != nil {
|
||||
lp.socUpdated = lp.clock.Now()
|
||||
f, err = se.SoC(lp.chargedEnergy)
|
||||
} else {
|
||||
return
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
if errors.Is(err, api.ErrMustRetry) {
|
||||
lp.socUpdated = time.Time{}
|
||||
} else {
|
||||
lp.log.ERROR.Printf("vehicle soc: %v", err)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
lp.vehicleSoc = math.Trunc(f)
|
||||
lp.log.DEBUG.Printf("vehicle soc: %.0f%%", lp.vehicleSoc)
|
||||
lp.publish("vehicleSoC", lp.vehicleSoc)
|
||||
|
||||
if se := lp.socEstimator; se != nil {
|
||||
if lp.charging() {
|
||||
lp.setRemainingDuration(se.RemainingChargeDuration(lp.chargePower, lp.SoC.target))
|
||||
} else {
|
||||
lp.setRemainingDuration(-1)
|
||||
}
|
||||
}
|
||||
|
||||
if se := lp.socEstimator; se != nil {
|
||||
lp.setRemainingEnergy(1e3 * se.RemainingChargeEnergy(lp.SoC.target))
|
||||
}
|
||||
|
||||
// range
|
||||
if vs, ok := lp.vehicle.(api.VehicleRange); ok {
|
||||
if rng, err := vs.Range(); err == nil {
|
||||
lp.log.DEBUG.Printf("vehicle range: %dkm", rng)
|
||||
lp.publish("vehicleRange", rng)
|
||||
}
|
||||
}
|
||||
|
||||
// trigger message after variables are updated
|
||||
lp.bus.Publish(evVehicleSoC, f)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue