Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: split charger into current- and power controllers #7338

Draft
wants to merge 9 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import (
"time"
)

//go:generate mockgen -package mock -destination ../mock/mock_api.go github.com/evcc-io/evcc/api Charger,ChargeState,PhaseSwitcher,Identifier,Meter,MeterEnergy,Vehicle,ChargeRater,Battery,Tariff
//go:generate mockgen -package mock -destination ../mock/mock_api.go github.com/evcc-io/evcc/api ChargeState,Identifier,Vehicle,Battery,Tariff
//go:generate mockgen -package mock -destination ../mock/mock_meter.go github.com/evcc-io/evcc/api Meter,MeterEnergy,ChargeRater
//go:generate mockgen -package mock -destination ../mock/mock_charger.go github.com/evcc-io/evcc/api Charger,CurrentController,PowerController,PhaseSwitcher

// ChargeMode is the charge operation mode. Valid values are off, now, minpv and pv
type ChargeMode string
Expand Down Expand Up @@ -124,11 +126,22 @@ type ChargeState interface {
Status() (ChargeStatus, error)
}

// CurrentLimiter provides settings charging maximum charging current
// CurrentLimiter limits maximum charging current
type CurrentLimiter interface {
MaxCurrent(current int64) error
}

// TODO rename to CurrentLimiterEx/MaxCurrentFloat
// ChargerEx provides milli-amp precision charger current control
type ChargerEx interface {
MaxCurrentMillis(current float64) error
}

// PowerLimiter limits maximum charging power
type PowerLimiter interface {
MaxPower(power float64) (float64, error)
}

// CurrentGetter provides getting charging maximum charging current for validation
type CurrentGetter interface {
GetMaxCurrent() (float64, error)
Expand All @@ -139,12 +152,18 @@ type Charger interface {
ChargeState
Enabled() (bool, error)
Enable(enable bool) error
}

// CurrentController combines Charger with CurrentLimiter
type CurrentController interface {
Charger
CurrentLimiter
}

// ChargerEx provides milli-amp precision charger current control
type ChargerEx interface {
MaxCurrentMillis(current float64) error
// PowerController combines Charger with PowerLimiter
type PowerController interface {
Charger
PowerLimiter
}

// PhaseSwitcher provides 1p3p switching
Expand Down
8 changes: 6 additions & 2 deletions cmd/charger.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,12 @@ func runCharger(cmd *cobra.Command, args []string) {
if current != noCurrent {
flagUsed = true

if err := v.MaxCurrent(current); err != nil {
log.ERROR.Println("set current:", err)
if vv, ok := v.(api.CurrentLimiter); ok {
if err := vv.MaxCurrent(current); err != nil {
log.ERROR.Println("set current:", err)
}
} else {
log.ERROR.Printf("current limiter: not implemented")
}
}

Expand Down
16 changes: 11 additions & 5 deletions cmd/charger_ramp.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func init() {
chargerRampCmd.Flags().StringP(flagDelay, "", "1s", "ramp delay")
}

func ramp(c api.Charger, digits int, delay time.Duration) {
func ramp(c api.CurrentController, digits int, delay time.Duration) {
steps := math.Pow10(digits)
delta := 1 / steps

Expand Down Expand Up @@ -96,11 +96,17 @@ func runChargerRamp(cmd *cobra.Command, args []string) {

chargers := config.Chargers().Devices()

for _, v := range config.Instances(chargers) {
if _, ok := v.(api.ChargerEx); digits > 0 && !ok {
log.ERROR.Fatalln("charger does not support mA control")
for _, c := range config.Instances(chargers) {
cc, ok := c.(api.CurrentController)
if !ok {
log.ERROR.Fatalf("current limiter: not implemented")
}
ramp(v, digits, delay)

if _, ok := cc.(api.ChargerEx); digits > 0 && !ok {
log.ERROR.Fatalln("charger ex: not implemented")
}

ramp(cc, digits, delay)
}

// wait for shutdown
Expand Down
19 changes: 19 additions & 0 deletions core/current_controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package core

import (
"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/util"
)

type currentAdapter struct {
api.CurrentController
log *util.Logger
minCurrent, maxCurrent float64
}

var _ api.PowerLimiter = (*currentAdapter)(nil)

// currentAdapter implements the api.PowerLimiter interface
func (c *currentAdapter) MaxPower(power float64) (float64, error) {
return 0, api.ErrNotAvailable
}
Loading
Loading