From 53dabe7d5beac0a195637021675491d710c466f5 Mon Sep 17 00:00:00 2001 From: andig Date: Sun, 20 Apr 2025 12:46:46 +0200 Subject: [PATCH] Revert "Circuit: fix validateCurrent and validatePower (#20183)" This reverts commit 455bc3348cbef15e80fcb803e05c1759e95beb72. --- api/api.go | 4 ++-- api/mock.go | 16 +++++++-------- core/circuit/circuit.go | 39 +++++++++++++----------------------- core/circuit/circuit_test.go | 4 ++-- core/loadpoint.go | 4 ++-- 5 files changed, 28 insertions(+), 39 deletions(-) diff --git a/api/api.go b/api/api.go index 2082caa82..605de8959 100644 --- a/api/api.go +++ b/api/api.go @@ -239,8 +239,8 @@ type Circuit interface { SetMaxPower(float64) SetMaxCurrent(float64) Update([]CircuitLoad) error - ValidateCurrent(old, new float64, charging bool) float64 - ValidatePower(old, new float64, charging bool) float64 + ValidateCurrent(old, new float64) float64 + ValidatePower(old, new float64) float64 } // Redactor is an interface to redact sensitive data diff --git a/api/mock.go b/api/mock.go index c7a2315ea..7a00eeb17 100644 --- a/api/mock.go +++ b/api/mock.go @@ -953,31 +953,31 @@ func (mr *MockCircuitMockRecorder) Update(arg0 any) *gomock.Call { } // ValidateCurrent mocks base method. -func (m *MockCircuit) ValidateCurrent(old, new float64, charging bool) float64 { +func (m *MockCircuit) ValidateCurrent(old, new float64) float64 { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "ValidateCurrent", old, new, charging) + ret := m.ctrl.Call(m, "ValidateCurrent", old, new) ret0, _ := ret[0].(float64) return ret0 } // ValidateCurrent indicates an expected call of ValidateCurrent. -func (mr *MockCircuitMockRecorder) ValidateCurrent(old, new, charging any) *gomock.Call { +func (mr *MockCircuitMockRecorder) ValidateCurrent(old, new any) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ValidateCurrent", reflect.TypeOf((*MockCircuit)(nil).ValidateCurrent), old, new, charging) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ValidateCurrent", reflect.TypeOf((*MockCircuit)(nil).ValidateCurrent), old, new) } // ValidatePower mocks base method. -func (m *MockCircuit) ValidatePower(old, new float64, charging bool) float64 { +func (m *MockCircuit) ValidatePower(old, new float64) float64 { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "ValidatePower", old, new, charging) + ret := m.ctrl.Call(m, "ValidatePower", old, new) ret0, _ := ret[0].(float64) return ret0 } // ValidatePower indicates an expected call of ValidatePower. -func (mr *MockCircuitMockRecorder) ValidatePower(old, new, charging any) *gomock.Call { +func (mr *MockCircuitMockRecorder) ValidatePower(old, new any) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ValidatePower", reflect.TypeOf((*MockCircuit)(nil).ValidatePower), old, new, charging) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ValidatePower", reflect.TypeOf((*MockCircuit)(nil).ValidatePower), old, new) } // Wrap mocks base method. diff --git a/core/circuit/circuit.go b/core/circuit/circuit.go index d52d161ea..9fd0a77bc 100644 --- a/core/circuit/circuit.go +++ b/core/circuit/circuit.go @@ -321,22 +321,17 @@ func (c *Circuit) GetMaxPhaseCurrent() float64 { } // ValidatePower validates power request -func (c *Circuit) ValidatePower(old, new float64, charging bool) float64 { +func (c *Circuit) ValidatePower(old, new float64) float64 { if maxPower := c.GetMaxPower(); maxPower != 0 { delta := max(0, new-old) potential := maxPower - c.power - if charging { - if delta > potential { - capped := max(0, min(new, min(old+potential, maxPower*1.1))) //allow 10% more than maxPower since some devices may not utilize offered power completely - c.log.DEBUG.Printf("validate power: %.5gW + (%.5gW -> %.5gW) > %.5gW capped at %.5gW", c.power, old, new, maxPower, capped) - new = capped - } else { - c.log.TRACE.Printf("validate power: %.5gW + (%.5gW -> %.5gW) <= %.5gW ok", c.power, old, new, maxPower) - new = min(new, maxPower*1.1) - } + if delta > potential { + capped := max(0, old+potential) + c.log.DEBUG.Printf("validate power: %.5gW + (%.5gW -> %.5gW) > %.5gW capped at %.5gW", c.power, old, new, maxPower, capped) + new = capped } else { - new = max(0, min(new, potential)) + c.log.TRACE.Printf("validate power: %.5gW + (%.5gW -> %.5gW) <= %.5gW ok", c.power, old, new, maxPower) } } @@ -344,27 +339,21 @@ func (c *Circuit) ValidatePower(old, new float64, charging bool) float64 { return new } - return c.parent.ValidatePower(old, new, charging) + return c.parent.ValidatePower(old, new) } // ValidateCurrent validates current request -// ValidateCurrent validates current request -func (c *Circuit) ValidateCurrent(old, new float64, charging bool) float64 { +func (c *Circuit) ValidateCurrent(old, new float64) float64 { if maxCurrent := c.GetMaxCurrent(); maxCurrent != 0 { delta := max(0, new-old) potential := maxCurrent - c.current - if charging { - if delta > potential { - capped := max(0, min(new, min(old+potential, maxCurrent*1.1))) //allow up to 10% more than maxCurrent since some devices may not utilize offered current completely - c.log.DEBUG.Printf("validate current: %.3gA + (%.3gA -> %.3gA) > %.3gA capped at %.3gA", c.current, old, new, maxCurrent, capped) - new = capped - } else { - c.log.TRACE.Printf("validate current: %.3gA + (%.3gA -> %.3gA) <= %.3gA ok", c.current, old, new, maxCurrent) - new = min(new, maxCurrent*1.1) - } + if delta > potential { + capped := max(0, old+potential) + c.log.DEBUG.Printf("validate current: %.3gA + (%.3gA -> %.3gA) > %.3gA capped at %.3gA", c.current, old, new, maxCurrent, capped) + new = capped } else { - new = max(0, min(new, potential)) + c.log.TRACE.Printf("validate current: %.3gA + (%.3gA -> %.3gA) <= %.3gA ok", c.current, old, new, maxCurrent) } } @@ -372,5 +361,5 @@ func (c *Circuit) ValidateCurrent(old, new float64, charging bool) float64 { return new } - return c.parent.ValidateCurrent(old, new, charging) + return c.parent.ValidateCurrent(old, new) } diff --git a/core/circuit/circuit_test.go b/core/circuit/circuit_test.go index a3aba44d9..d7c5175c4 100644 --- a/core/circuit/circuit_test.go +++ b/core/circuit/circuit_test.go @@ -82,7 +82,7 @@ func TestCircuitPower(t *testing.T) { cm2.EXPECT().CurrentPower().Return(tc.c2, nil) require.NoError(t, pc.Update(nil)) - assert.Equal(t, tc.res, c1.ValidatePower(tc.old, tc.new, true), tc) + assert.Equal(t, tc.res, c1.ValidatePower(tc.old, tc.new), tc) ctrl.Finish() } @@ -124,7 +124,7 @@ func TestCircuitCurrents(t *testing.T) { cm2.MockPhaseCurrents.EXPECT().Currents().Return(tc.c2, tc.c2, tc.c2, nil) require.NoError(t, pc.Update(nil)) - assert.Equal(t, tc.res, c1.ValidateCurrent(tc.old, tc.new, true), tc) + assert.Equal(t, tc.res, c1.ValidateCurrent(tc.old, tc.new), tc) ctrl.Finish() } diff --git a/core/loadpoint.go b/core/loadpoint.go index f0ebac21c..44fea0ea1 100644 --- a/core/loadpoint.go +++ b/core/loadpoint.go @@ -837,10 +837,10 @@ func (lp *Loadpoint) setLimit(current float64) error { // apply circuit limits if lp.circuit != nil { - currentLimit := lp.circuit.ValidateCurrent(lp.offeredCurrent, current, lp.charging()) + currentLimit := lp.circuit.ValidateCurrent(lp.offeredCurrent, current) activePhases := lp.ActivePhases() - powerLimit := lp.circuit.ValidatePower(lp.chargePower, currentToPower(current, activePhases), lp.charging()) + powerLimit := lp.circuit.ValidatePower(lp.chargePower, currentToPower(current, activePhases)) currentLimitViaPower := powerToCurrent(powerLimit, activePhases) current = lp.roundedCurrent(min(currentLimit, currentLimitViaPower))