Allow vehicles to request soc update (#981)

This commit is contained in:
andig 2021-05-10 22:33:49 +02:00 • committed by GitHub
parent 62052e0159
commit cb93b9b2f1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 11 deletions

View file

@ -5,6 +5,9 @@ import "errors"
// ErrNotAvailable indicates that a feature is not available
var ErrNotAvailable = errors.New("not available")
// ErrMustRetry indicates that a rate-limited operation should be retried
var ErrMustRetry = errors.New("must retry")
// ErrTimeout is the error returned when a timeout happened.
// Modeled after context.DeadlineError
var ErrTimeout error = errTimeoutError{}

View file

@ -868,12 +868,7 @@ func (lp *LoadPoint) socPollAllowed() bool {
lp.log.DEBUG.Printf("next soc poll remaining time: %v", remaining.Truncate(time.Second))
}
res := lp.charging() || honourUpdateInterval && (remaining <= 0) || lp.connected() && lp.socUpdated.IsZero()
if res {
lp.socUpdated = lp.clock.Now()
}
return res
return lp.charging() || honourUpdateInterval && (remaining <= 0) || lp.connected() && lp.socUpdated.IsZero()
}
// publish state of charge, remaining charge duration and range
@ -883,6 +878,8 @@ func (lp *LoadPoint) publishSoCAndRange() {
}
if lp.socPollAllowed() {
lp.socUpdated = lp.clock.Now()
f, err := lp.socEstimator.SoC(lp.chargedEnergy)
if err == nil {
lp.socCharge = math.Trunc(f)
@ -898,10 +895,12 @@ func (lp *LoadPoint) publishSoCAndRange() {
chargeRemainingEnergy := 1e3 * lp.socEstimator.RemainingChargeEnergy(lp.SoC.Target)
lp.publish("chargeRemainingEnergy", chargeRemainingEnergy)
} else {
// we need a value- so retry on error
lp.socUpdated = lp.clock.Now()
lp.log.ERROR.Printf("vehicle: %v", err)
if errors.Is(err, api.ErrMustRetry) {
lp.socUpdated = time.Time{}
lp.log.DEBUG.Printf("vehicle: waiting for update")
} else {
lp.log.ERROR.Printf("vehicle: %v", err)
}
}
// range

View file

@ -699,7 +699,13 @@ func TestSoCPoll(t *testing.T) {
lp.SoC.Poll.Mode = tc.mode
lp.status = tc.status
if res := lp.socPollAllowed(); tc.res != res {
res := lp.socPollAllowed()
if res {
// mimic update outside of socPollAllowed
lp.socUpdated = clock.Now()
}
if tc.res != res {
t.Errorf("expected %v, got %v", tc.res, res)
}
}