Skip to content

Commit

Permalink
Replace current precendence with min/max logic (#11311)
Browse files Browse the repository at this point in the history
  • Loading branch information
andig authored Dec 30, 2023
1 parent 32dc838 commit cf59742
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 13 deletions.
2 changes: 1 addition & 1 deletion api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"time"
)

//go:generate mockgen -package api -destination mock.go github.com/evcc-io/evcc/api Charger,ChargeState,PhaseSwitcher,Identifier,Meter,MeterEnergy,Vehicle,ChargeRater,Battery,Tariff,BatteryController
//go:generate mockgen -package api -destination mock.go github.com/evcc-io/evcc/api Charger,ChargeState,CurrentLimiter,PhaseSwitcher,Identifier,Meter,MeterEnergy,Vehicle,ChargeRater,Battery,Tariff,BatteryController

// ChargeMode is the charge operation mode. Valid values are off, now, minpv and pv
type ChargeMode string
Expand Down
41 changes: 40 additions & 1 deletion api/mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 16 additions & 8 deletions core/loadpoint_effective.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,38 +60,46 @@ func (lp *Loadpoint) SocBasedPlanning() bool {

// effectiveMinCurrent returns the effective min current
func (lp *Loadpoint) effectiveMinCurrent() float64 {
minCurrent := lp.GetMinCurrent()

if v := lp.GetVehicle(); v != nil {
if res, ok := v.OnIdentified().GetMinCurrent(); ok {
return res
minCurrent = max(minCurrent, res)
}
}

if c, ok := lp.charger.(api.CurrentLimiter); ok {
if res, _, err := c.GetMinMaxCurrent(); err == nil {
lp.publish(keys.EffectiveMinCurrent, res)
return res
if res > 0 && res < minCurrent {
minCurrent = res
} else {
minCurrent = max(minCurrent, res)
}
lp.publish(keys.EffectiveMinCurrent, minCurrent)
}
}

return lp.GetMinCurrent()
return minCurrent
}

// effectiveMaxCurrent returns the effective max current
func (lp *Loadpoint) effectiveMaxCurrent() float64 {
maxCurrent := lp.GetMaxCurrent()

if v := lp.GetVehicle(); v != nil {
if res, ok := v.OnIdentified().GetMaxCurrent(); ok {
return res
maxCurrent = min(maxCurrent, res)
}
}

if c, ok := lp.charger.(api.CurrentLimiter); ok {
if _, res, err := c.GetMinMaxCurrent(); err == nil {
lp.publish(keys.EffectiveMaxCurrent, res)
return res
maxCurrent = min(maxCurrent, res)
lp.publish(keys.EffectiveMaxCurrent, maxCurrent)
}
}

return lp.GetMaxCurrent()
return maxCurrent
}

// effectiveLimitSoc returns the effective session limit soc
Expand Down
53 changes: 53 additions & 0 deletions core/loadpoint_effective_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,64 @@ package core
import (
"testing"

"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/util"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
)

func TestEffectiveLimitSoc(t *testing.T) {
lp := NewLoadpoint(util.NewLogger("foo"), nil)
assert.Equal(t, 100, lp.effectiveLimitSoc())
}

func TestEffectiveMinMaxCurrent(t *testing.T) {
tc := []struct {
chargerMin, chargerMax float64
vehicleMin, vehicleMax float64
effectiveMin, effectiveMax float64
}{
{0, 0, 0, 0, 6, 16},
{1, 10, 0, 0, 1, 10}, // charger lower
{10, 20, 0, 0, 10, 16}, // charger higher - max ignored
{0, 0, 1, 10, 6, 10}, // vehicle lower - min ignored
{0, 0, 10, 20, 10, 16}, // vehicle higher - max ignored
{1, 10, 2, 12, 1, 10}, // charger + vehicle lower
{10, 20, 12, 22, 10, 16}, // charger + vehicle higher
}

for _, tc := range tc {
t.Logf("%+v", tc)
ctrl := gomock.NewController(t)

lp := NewLoadpoint(util.NewLogger("foo"), nil)
lp.charger = api.NewMockCharger(ctrl)

if tc.chargerMin+tc.chargerMax > 0 {
currentLimiter := api.NewMockCurrentLimiter(ctrl)
currentLimiter.EXPECT().GetMinMaxCurrent().Return(tc.chargerMin, tc.chargerMax, nil).AnyTimes()

lp.charger = struct {
api.Charger
api.CurrentLimiter
}{
Charger: lp.charger,
CurrentLimiter: currentLimiter,
}
}

if tc.vehicleMin+tc.vehicleMax > 0 {
vehicle := api.NewMockVehicle(ctrl)
ac := api.ActionConfig{
MinCurrent: tc.vehicleMin,
MaxCurrent: tc.vehicleMax,
}
vehicle.EXPECT().OnIdentified().Return(ac).AnyTimes()

lp.vehicle = vehicle
}

assert.Equal(t, tc.effectiveMin, lp.effectiveMinCurrent())
assert.Equal(t, tc.effectiveMax, lp.effectiveMaxCurrent())
}
}
3 changes: 0 additions & 3 deletions core/loadpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,6 @@ func TestNew(t *testing.T) {
if lp.phases != 0 {
t.Errorf("Phases %v", lp.phases)
}
if lp.MinCurrent != minA {
t.Errorf("MinCurrent %v", lp.MinCurrent)
}
if lp.MaxCurrent != maxA {
t.Errorf("MaxCurrent %v", lp.MaxCurrent)
}
Expand Down

0 comments on commit cf59742

Please sign in to comment.