From 578b9ac2f726edca5cb4931aa510552453902a74 Mon Sep 17 00:00:00 2001 From: andig Date: Sun, 28 Jan 2024 15:00:10 +0100 Subject: [PATCH] Revert "Loadpoint: publish guardTimer independently from pvTimer (#11204)" This reverts commit 6aa9d995a4e777884260bb68261c11a1264d6763. --- core/loadpoint.go | 12 ++-- core/loadpoint_test.go | 132 +---------------------------------------- 2 files changed, 9 insertions(+), 135 deletions(-) diff --git a/core/loadpoint.go b/core/loadpoint.go index 276fc2123..ee70d4b1c 100644 --- a/core/loadpoint.go +++ b/core/loadpoint.go @@ -740,14 +740,14 @@ func (lp *Loadpoint) setLimit(chargeCurrent float64, force bool) error { lp.bus.Publish(evChargeCurrent, chargeCurrent) } - if lp.clock.Since(lp.guardUpdated).Truncate(time.Second) < lp.GuardDuration && !force { - lp.publishTimer(guardTimer, lp.GuardDuration, guardEnable) - return nil - } - lp.elapseGuard() - // set enabled/disabled if enabled := chargeCurrent >= lp.effectiveMinCurrent(); enabled != lp.enabled { + if remaining := (lp.GuardDuration - lp.clock.Since(lp.guardUpdated)).Truncate(time.Second); remaining > 0 && !force { + lp.publishTimer(guardTimer, lp.GuardDuration, guardEnable) + return nil + } + lp.elapseGuard() + if err := lp.charger.Enable(enabled); err != nil { v := lp.GetVehicle() if vv, ok := v.(api.Resurrector); enabled && ok && errors.Is(err, api.ErrAsleep) { diff --git a/core/loadpoint_test.go b/core/loadpoint_test.go index a254e2bef..aee70d13f 100644 --- a/core/loadpoint_test.go +++ b/core/loadpoint_test.go @@ -10,7 +10,6 @@ import ( "github.com/evcc-io/evcc/core/soc" "github.com/evcc-io/evcc/push" "github.com/evcc-io/evcc/util" - "github.com/stretchr/testify/assert" "go.uber.org/mock/gomock" ) @@ -525,13 +524,13 @@ func cacheExpecter(t *testing.T, lp *Loadpoint) (*util.Cache, func(key string, v expect := func(key string, val interface{}) { time.Sleep(100 * time.Millisecond) // wait for cache to catch up - p := cache.Get(key) - t.Logf("%s: %v", key, p.Val) // REMOVE + t.Logf("%s: %.f", key, p.Val) // REMOVE if p.Val != val { - t.Errorf("%s wanted: %v, got %v", key, val, p.Val) + t.Errorf("%s wanted: %.0f, got %v", key, val, p.Val) } } + return cache, expect } @@ -792,128 +791,3 @@ func TestSocPoll(t *testing.T) { } } } - -// test guard timer is properly published during disable cycle -func TestGuardPublish(t *testing.T) { - ctrl := gomock.NewController(t) - defer ctrl.Finish() - clock := clock.NewMock() - charger := api.NewMockCharger(ctrl) - rater := api.NewMockChargeRater(ctrl) - startPointInTime := clock.Now() - - lp := &Loadpoint{ - log: util.NewLogger("foo"), - bus: evbus.New(), - clock: clock, - charger: charger, - chargeMeter: &Null{}, // silence nil panics - chargeRater: rater, - chargeTimer: &Null{}, // silence nil panics - guardUpdated: startPointInTime, - pvTimer: time.Time{}, - wakeUpTimer: NewTimer(), - sessionEnergy: NewEnergyMetrics(), - MinCurrent: minA, - MaxCurrent: maxA, - chargeCurrent: maxA, - status: api.StatusC, - mode: api.ModePV, - enabled: true, - Disable: ThresholdConfig{Delay: 2 * time.Minute, Threshold: 0}, // t, W - GuardDuration: 3 * time.Minute, - } - - util.LogLevel("DEBUG", nil) - - attachListeners(t, lp) - // attach cache for verifying values - _, expectCache := cacheExpecter(t, lp) - - rater.EXPECT().ChargedEnergy().AnyTimes() - charger.EXPECT().Enabled().DoAndReturn(func() (bool, error) { return lp.enabled, nil }).AnyTimes() // just follow the loadpoint - charger.EXPECT().Status().Return(api.StatusC, nil).Times(4) - charger.EXPECT().Enable(false).Return(nil) - - tc := []struct { - step string - guardUpdates time.Time - guardTimerRemaining time.Duration - }{ - {"kick off pv disable timer", startPointInTime, 3 * time.Minute}, - {"continue pv disable timer", startPointInTime, 2 * time.Minute}, - {"disable charger prevented by guard", startPointInTime, 1 * time.Minute}, - {"guard elapse, publish new guard time", startPointInTime.Add(3 * time.Minute), time.Duration(0)}, - } - - for _, tc := range tc { - t.Logf("%+v", tc) - lp.Update(15000, false, false, false, 0, nil, nil) - assert.Equal(t, tc.guardUpdates, lp.guardUpdated) - expectCache(guardTimer+"Remaining", tc.guardTimerRemaining) - clock.Add(time.Minute) - } -} - -// test guard timer is properly published if charger disable sequence is aborted -func TestGuardPublishOnDisableAbort(t *testing.T) { - ctrl := gomock.NewController(t) - defer ctrl.Finish() - clock := clock.NewMock() - charger := api.NewMockCharger(ctrl) - rater := api.NewMockChargeRater(ctrl) - startPointInTime := clock.Now() - - lp := &Loadpoint{ - log: util.NewLogger("foo"), - bus: evbus.New(), - clock: clock, - charger: charger, - chargeMeter: &Null{}, // silence nil panics - chargeRater: rater, - chargeTimer: &Null{}, // silence nil panics - guardUpdated: startPointInTime, - pvTimer: time.Time{}, - wakeUpTimer: NewTimer(), - sessionEnergy: NewEnergyMetrics(), - MinCurrent: minA, - MaxCurrent: maxA, - chargeCurrent: maxA, - status: api.StatusC, - mode: api.ModePV, - enabled: true, - Disable: ThresholdConfig{Delay: 2 * time.Minute, Threshold: 0}, // t, W - GuardDuration: 4 * time.Minute, - } - - util.LogLevel("DEBUG", nil) - - attachListeners(t, lp) - // attach cache for verifying values - _, expectCache := cacheExpecter(t, lp) - - rater.EXPECT().ChargedEnergy().AnyTimes() - charger.EXPECT().Enabled().DoAndReturn(func() (bool, error) { return lp.enabled, nil }).AnyTimes() // just follow the loadpoint - charger.EXPECT().Status().Return(api.StatusC, nil).Times(5) - - tc := []struct { - step string - sitePower float64 - guardUpdated time.Time - guardTimerRemaining time.Duration - }{ - {"kick off pv disable timer", 15000, startPointInTime, 4 * time.Minute}, - {"continue pv disable timer", 15000, startPointInTime, 3 * time.Minute}, - {"disable charger prevented by guard", 15000, startPointInTime, 2 * time.Minute}, - {"disable charger reset, pv power is back", -1, startPointInTime, 1 * time.Minute}, - {"guard elapsed, publish new guard time", -1, elapsed, time.Duration(0)}, - } - - for _, tc := range tc { - t.Logf("%+v", tc) - lp.Update(tc.sitePower, false, false, false, 0, nil, nil) - assert.Equal(t, tc.guardUpdated, lp.guardUpdated) - expectCache(guardTimer+"Remaining", tc.guardTimerRemaining) - clock.Add(time.Minute) - } -}