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

chore: refactor plugin structure #18335

Merged
merged 11 commits into from
Jan 23, 2025
Merged
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
2 changes: 1 addition & 1 deletion api/globalconfig/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"time"

"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/provider/mqtt"
"github.com/evcc-io/evcc/plugin/mqtt"
"github.com/evcc-io/evcc/push"
"github.com/evcc-io/evcc/server/eebus"
"github.com/evcc-io/evcc/util/config"
Expand Down
59 changes: 25 additions & 34 deletions charger/charger.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/meter"
"github.com/evcc-io/evcc/provider"
"github.com/evcc-io/evcc/plugin"
"github.com/evcc-io/evcc/util"
)

Expand All @@ -30,40 +30,40 @@ func init() {
func NewConfigurableFromConfig(ctx context.Context, other map[string]interface{}) (api.Charger, error) {
var cc struct {
embed `mapstructure:",squash"`
Status, Enable, Enabled, MaxCurrent provider.Config
MaxCurrentMillis *provider.Config
Identify, Phases1p3p *provider.Config
Wakeup *provider.Config
Soc *provider.Config
Status, Enable, Enabled, MaxCurrent plugin.Config
MaxCurrentMillis *plugin.Config
Identify, Phases1p3p *plugin.Config
Wakeup *plugin.Config
Soc *plugin.Config
Tos bool

// optional measurements
Power *provider.Config
Energy *provider.Config
Power *plugin.Config
Energy *plugin.Config

Currents, Voltages []provider.Config
Currents, Voltages []plugin.Config
}

if err := util.DecodeOther(other, &cc); err != nil {
return nil, err
}

status, err := provider.NewStringGetterFromConfig(ctx, cc.Status)
status, err := cc.Status.StringGetter(ctx)
if err != nil {
return nil, fmt.Errorf("status: %w", err)
}

enabled, err := provider.NewBoolGetterFromConfig(ctx, cc.Enabled)
enabled, err := cc.Enabled.BoolGetter(ctx)
if err != nil {
return nil, fmt.Errorf("enabled: %w", err)
}

enable, err := provider.NewBoolSetterFromConfig(ctx, "enable", cc.Enable)
enable, err := cc.Enable.BoolSetter(ctx, "enable")
if err != nil {
return nil, fmt.Errorf("enable: %w", err)
}

maxcurrent, err := provider.NewIntSetterFromConfig(ctx, "maxcurrent", cc.MaxCurrent)
maxcurrent, err := cc.MaxCurrent.IntSetter(ctx, "maxcurrent")
if err != nil {
return nil, fmt.Errorf("maxcurrent: %w", err)
}
Expand All @@ -75,12 +75,9 @@ func NewConfigurableFromConfig(ctx context.Context, other map[string]interface{}

c.embed = &cc.embed

var maxcurrentmillis func(float64) error
if cc.MaxCurrentMillis != nil {
maxcurrentmillis, err = provider.NewFloatSetterFromConfig(ctx, "maxcurrentmillis", *cc.MaxCurrentMillis)
if err != nil {
return nil, fmt.Errorf("maxcurrentmillis: %w", err)
}
maxcurrentmillis, err := cc.MaxCurrentMillis.FloatSetter(ctx, "maxcurrentmillis")
if err != nil {
return nil, fmt.Errorf("maxcurrentmillis: %w", err)
}

// decorate phases
Expand All @@ -90,7 +87,7 @@ func NewConfigurableFromConfig(ctx context.Context, other map[string]interface{}
return nil, errors.New("1p3p does no longer handle disable/enable. Use tos: true to confirm you understand the consequences")
}

phases1p3pS, err := provider.NewIntSetterFromConfig(ctx, "phases", *cc.Phases1p3p)
phases1p3pS, err := cc.Phases1p3p.IntSetter(ctx, "phases")
if err != nil {
return nil, fmt.Errorf("phases: %w", err)
}
Expand All @@ -101,34 +98,28 @@ func NewConfigurableFromConfig(ctx context.Context, other map[string]interface{}
}

// decorate identifier
var identify func() (string, error)
if cc.Identify != nil {
identify, err = provider.NewStringGetterFromConfig(ctx, *cc.Identify)
if err != nil {
return nil, fmt.Errorf("identify: %w", err)
}
identify, err := cc.Identify.StringGetter(ctx)
if err != nil {
return nil, fmt.Errorf("identify: %w", err)
}

// decorate wakeup
var wakeup func() error
if cc.Wakeup != nil {
wakeupS, err := provider.NewBoolSetterFromConfig(ctx, "wakeup", *cc.Wakeup)
set, err := cc.Wakeup.BoolSetter(ctx, "wakeup")
if err != nil {
return nil, fmt.Errorf("wakeup: %w", err)
}

wakeup = func() error {
return wakeupS(true)
return set(true)
}
}

// decorate soc
var soc func() (float64, error)
if cc.Soc != nil {
soc, err = provider.NewFloatGetterFromConfig(ctx, *cc.Soc)
if err != nil {
return nil, fmt.Errorf("soc: %w", err)
}
soc, err := cc.Soc.FloatGetter(ctx)
if err != nil {
return nil, fmt.Errorf("soc: %w", err)
}

// decorate measurements
Expand Down
4 changes: 2 additions & 2 deletions charger/connectiq.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (

"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/charger/connectiq"
"github.com/evcc-io/evcc/provider"
"github.com/evcc-io/evcc/plugin"
"github.com/evcc-io/evcc/util"
"github.com/evcc-io/evcc/util/request"
)
Expand Down Expand Up @@ -71,7 +71,7 @@ func NewConnectIq(uri string, cache time.Duration) (api.Charger, error) {
}

// cache meter readings
wb.meterG = provider.Cached(func() (connectiq.MeterStatus, error) {
wb.meterG = plugin.Cached(func() (connectiq.MeterStatus, error) {
var res connectiq.MeterStatus
uri := fmt.Sprintf("%s/meter/status", wb.uri)
err := wb.GetJSON(uri, &res)
Expand Down
10 changes: 5 additions & 5 deletions charger/daheimladen.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/charger/daheimladen"
"github.com/evcc-io/evcc/provider"
"github.com/evcc-io/evcc/plugin"
"github.com/evcc-io/evcc/util"
"github.com/evcc-io/evcc/util/request"
"golang.org/x/oauth2"
Expand All @@ -22,8 +22,8 @@ type DaheimLaden struct {
idTag string
token string
transactionID int32
statusG provider.Cacheable[daheimladen.GetLatestStatus]
meterG provider.Cacheable[daheimladen.GetLatestMeterValueResponse]
statusG plugin.Cacheable[daheimladen.GetLatestStatus]
meterG plugin.Cacheable[daheimladen.GetLatestMeterValueResponse]
cache time.Duration
}

Expand Down Expand Up @@ -67,13 +67,13 @@ func NewDaheimLaden(token, stationID string, cache time.Duration) (*DaheimLaden,
Base: c.Client.Transport,
}

c.statusG = provider.ResettableCached(func() (daheimladen.GetLatestStatus, error) {
c.statusG = plugin.ResettableCached(func() (daheimladen.GetLatestStatus, error) {
var res daheimladen.GetLatestStatus
err := c.GetJSON(fmt.Sprintf("%s/cs/%s/status", daheimladen.BASE_URL, c.stationID), &res)
return res, err
}, c.cache)

c.meterG = provider.ResettableCached(func() (daheimladen.GetLatestMeterValueResponse, error) {
c.meterG = plugin.ResettableCached(func() (daheimladen.GetLatestMeterValueResponse, error) {
var res daheimladen.GetLatestMeterValueResponse
err := c.GetJSON(fmt.Sprintf("%s/cs/%s/metervalue", daheimladen.BASE_URL, c.stationID), &res)
return res, err
Expand Down
4 changes: 2 additions & 2 deletions charger/eebus.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"github.com/enbility/spine-go/model"
"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/core/loadpoint"
"github.com/evcc-io/evcc/provider"
"github.com/evcc-io/evcc/plugin"
"github.com/evcc-io/evcc/server/eebus"
"github.com/evcc-io/evcc/util"
"github.com/samber/lo"
Expand Down Expand Up @@ -88,7 +88,7 @@ func NewEEBus(ski, ip string, hasMeter, hasChargedEnergy, vasVW bool) (api.Charg
}

c.Connector = eebus.NewConnector()
c.minMaxG = provider.Cached(c.minMax, time.Second)
c.minMaxG = plugin.Cached(c.minMax, time.Second)

if err := eebus.Instance.RegisterDevice(ski, ip, c); err != nil {
return nil, err
Expand Down
6 changes: 3 additions & 3 deletions charger/evsewifi.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/charger/evse"
"github.com/evcc-io/evcc/provider"
"github.com/evcc-io/evcc/plugin"
"github.com/evcc-io/evcc/util"
"github.com/evcc-io/evcc/util/request"
)
Expand All @@ -20,7 +20,7 @@ type EVSEWifi struct {
alwaysActive bool
current int64 // current will always be the physical value sent to the API
hires bool
paramG provider.Cacheable[evse.ListEntry]
paramG plugin.Cacheable[evse.ListEntry]
}

func init() {
Expand Down Expand Up @@ -122,7 +122,7 @@ func NewEVSEWifi(uri string, cache time.Duration) (*EVSEWifi, error) {
current: 6, // 6A defined value
}

wb.paramG = provider.ResettableCached(func() (evse.ListEntry, error) {
wb.paramG = plugin.ResettableCached(func() (evse.ListEntry, error) {
var res evse.ParameterResponse
uri := fmt.Sprintf("%s/getParameters", wb.uri)
if err := wb.GetJSON(uri, &res); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions charger/hardybarth-ecb1.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
"github.com/evcc-io/evcc/charger/echarge"
"github.com/evcc-io/evcc/charger/echarge/ecb1"
"github.com/evcc-io/evcc/meter/obis"
"github.com/evcc-io/evcc/provider"
"github.com/evcc-io/evcc/plugin"
"github.com/evcc-io/evcc/util"
"github.com/evcc-io/evcc/util/request"
"github.com/evcc-io/evcc/util/sponsor"
Expand Down Expand Up @@ -85,7 +85,7 @@ func NewHardyBarth(uri string, chargecontrol, meter int, cache time.Duration) (a
}

// cache meter readings
wb.meterG = provider.Cached(func() (ecb1.Meter, error) {
wb.meterG = plugin.Cached(func() (ecb1.Meter, error) {
var res struct {
Meter struct {
ecb1.Meter
Expand Down
6 changes: 3 additions & 3 deletions charger/hardybarth-salia.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/charger/echarge"
"github.com/evcc-io/evcc/charger/echarge/salia"
"github.com/evcc-io/evcc/provider"
"github.com/evcc-io/evcc/plugin"
"github.com/evcc-io/evcc/util"
"github.com/evcc-io/evcc/util/request"
"github.com/evcc-io/evcc/util/sponsor"
Expand All @@ -46,7 +46,7 @@ type Salia struct {
uri string
current int64
fw int // 2 if fw 2.0
apiG provider.Cacheable[salia.Api]
apiG plugin.Cacheable[salia.Api]
}

func init() {
Expand Down Expand Up @@ -84,7 +84,7 @@ func NewSalia(ctx context.Context, uri string, cache time.Duration) (api.Charger
current: 6,
}

wb.apiG = provider.ResettableCached(func() (salia.Api, error) {
wb.apiG = plugin.ResettableCached(func() (salia.Api, error) {
var res salia.Api
err := wb.GetJSON(wb.uri, &res)
return res, err
Expand Down
6 changes: 3 additions & 3 deletions charger/mystrom.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/meter/mystrom"
"github.com/evcc-io/evcc/provider"
"github.com/evcc-io/evcc/plugin"
"github.com/evcc-io/evcc/util"
)

Expand All @@ -21,7 +21,7 @@ func init() {
type MyStrom struct {
*switchSocket
conn *mystrom.Connection
reportG provider.Cacheable[mystrom.Report]
reportG plugin.Cacheable[mystrom.Report]
}

// NewMyStromFromConfig creates a myStrom charger from generic config
Expand All @@ -44,7 +44,7 @@ func NewMyStromFromConfig(other map[string]interface{}) (api.Charger, error) {
}

c.switchSocket = NewSwitchSocket(&cc.embed, c.Enabled, c.conn.CurrentPower, cc.StandbyPower)
c.reportG = provider.ResettableCached(c.conn.Report, cc.Cache)
c.reportG = plugin.ResettableCached(c.conn.Report, cc.Cache)

return c, nil
}
Expand Down
10 changes: 5 additions & 5 deletions charger/nrgconnect.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/charger/nrg/connect"
"github.com/evcc-io/evcc/provider"
"github.com/evcc-io/evcc/plugin"
"github.com/evcc-io/evcc/util"
"github.com/evcc-io/evcc/util/request"
)
Expand All @@ -23,8 +23,8 @@ type NRGKickConnect struct {
mac string
password string
enabled bool
settingsG provider.Cacheable[connect.Settings]
measurementsG provider.Cacheable[connect.Measurements]
settingsG plugin.Cacheable[connect.Settings]
measurementsG plugin.Cacheable[connect.Measurements]
}

func init() {
Expand Down Expand Up @@ -56,7 +56,7 @@ func NewNRGKickConnect(uri, mac, password string, cache time.Duration) (*NRGKick
password: password,
}

nrg.settingsG = provider.ResettableCached(func() (connect.Settings, error) {
nrg.settingsG = plugin.ResettableCached(func() (connect.Settings, error) {
var res connect.Settings

err := nrg.GetJSON(nrg.apiURL(connect.SettingsPath), &res)
Expand All @@ -67,7 +67,7 @@ func NewNRGKickConnect(uri, mac, password string, cache time.Duration) (*NRGKick
return res, err
}, cache)

nrg.measurementsG = provider.ResettableCached(func() (connect.Measurements, error) {
nrg.measurementsG = plugin.ResettableCached(func() (connect.Measurements, error) {
var res connect.Measurements

err := nrg.GetJSON(nrg.apiURL(connect.MeasurementsPath), &res)
Expand Down
6 changes: 3 additions & 3 deletions charger/openevse.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/charger/openevse"
"github.com/evcc-io/evcc/provider"
"github.com/evcc-io/evcc/plugin"
"github.com/evcc-io/evcc/util"
"github.com/evcc-io/evcc/util/request"
"github.com/evcc-io/evcc/util/transport"
Expand All @@ -20,7 +20,7 @@ import (
type OpenEVSE struct {
*request.Helper
uri string
statusG provider.Cacheable[openevse.Status]
statusG plugin.Cacheable[openevse.Status]
current int
enabled bool
}
Expand Down Expand Up @@ -67,7 +67,7 @@ func NewOpenEVSE(uri, user, password string, cache time.Duration) (api.Charger,
c.Client.Transport = transport.BasicAuth(user, password, c.Client.Transport)
}

c.statusG = provider.ResettableCached(func() (openevse.Status, error) {
c.statusG = plugin.ResettableCached(func() (openevse.Status, error) {
var res openevse.Status

uri := fmt.Sprintf("%s/status", c.uri)
Expand Down
Loading
Loading