EEBus: use context (#19284)
Some checks failed
Some checks failed
This commit is contained in:
parent
59d7bb54ff
commit
acb521362a
5 changed files with 25 additions and 17 deletions
|
|
@ -1,6 +1,7 @@
|
|||
package charger
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"slices"
|
||||
|
|
@ -49,11 +50,11 @@ type EEBus struct {
|
|||
}
|
||||
|
||||
func init() {
|
||||
registry.Add("eebus", NewEEBusFromConfig)
|
||||
registry.AddCtx("eebus", NewEEBusFromConfig)
|
||||
}
|
||||
|
||||
// NewEEBusFromConfig creates an EEBus charger from generic config
|
||||
func NewEEBusFromConfig(other map[string]interface{}) (api.Charger, error) {
|
||||
func NewEEBusFromConfig(ctx context.Context, other map[string]interface{}) (api.Charger, error) {
|
||||
cc := struct {
|
||||
Ski string
|
||||
Ip string
|
||||
|
|
@ -68,13 +69,13 @@ func NewEEBusFromConfig(other map[string]interface{}) (api.Charger, error) {
|
|||
return nil, err
|
||||
}
|
||||
|
||||
return NewEEBus(cc.Ski, cc.Ip, cc.Meter, cc.ChargedEnergy, cc.VasVW)
|
||||
return NewEEBus(ctx, cc.Ski, cc.Ip, cc.Meter, cc.ChargedEnergy, cc.VasVW)
|
||||
}
|
||||
|
||||
//go:generate go tool decorate -f decorateEEBus -b *EEBus -r api.Charger -t "api.Meter,CurrentPower,func() (float64, error)" -t "api.PhaseCurrents,Currents,func() (float64, float64, float64, error)" -t "api.ChargeRater,ChargedEnergy,func() (float64, error)"
|
||||
|
||||
// NewEEBus creates EEBus charger
|
||||
func NewEEBus(ski, ip string, hasMeter, hasChargedEnergy, vasVW bool) (api.Charger, error) {
|
||||
func NewEEBus(ctx context.Context, ski, ip string, hasMeter, hasChargedEnergy, vasVW bool) (api.Charger, error) {
|
||||
if eebus.Instance == nil {
|
||||
return nil, errors.New("eebus not configured")
|
||||
}
|
||||
|
|
@ -93,7 +94,7 @@ func NewEEBus(ski, ip string, hasMeter, hasChargedEnergy, vasVW bool) (api.Charg
|
|||
return nil, err
|
||||
}
|
||||
|
||||
if err := c.Wait(90 * time.Second); err != nil {
|
||||
if err := c.Wait(ctx); err != nil {
|
||||
return c, err
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -23,7 +23,7 @@ func NewFromConfig(ctx context.Context, typ string, other map[string]interface{}
|
|||
case "sma", "shm", "semp":
|
||||
return semp.New(other, site, httpd)
|
||||
case "eebus":
|
||||
return eebus.New(other, site)
|
||||
return eebus.New(ctx, other, site)
|
||||
case "relay":
|
||||
return relay.New(ctx, other, site)
|
||||
default:
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package eebus
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"sync"
|
||||
"time"
|
||||
|
|
@ -40,7 +41,7 @@ type Limits struct {
|
|||
}
|
||||
|
||||
// New creates an EEBus HEMS from generic config
|
||||
func New(other map[string]interface{}, site site.API) (*EEBus, error) {
|
||||
func New(ctx context.Context, other map[string]interface{}, site site.API) (*EEBus, error) {
|
||||
cc := struct {
|
||||
Ski string
|
||||
Limits `mapstructure:",squash"`
|
||||
|
|
@ -75,11 +76,11 @@ func New(other map[string]interface{}, site site.API) (*EEBus, error) {
|
|||
}
|
||||
site.SetCircuit(lpc)
|
||||
|
||||
return NewEEBus(cc.Ski, cc.Limits, lpc)
|
||||
return NewEEBus(ctx, cc.Ski, cc.Limits, lpc)
|
||||
}
|
||||
|
||||
// NewEEBus creates EEBus charger
|
||||
func NewEEBus(ski string, limits Limits, root api.Circuit) (*EEBus, error) {
|
||||
func NewEEBus(ctx context.Context, ski string, limits Limits, root api.Circuit) (*EEBus, error) {
|
||||
if eebus.Instance == nil {
|
||||
return nil, errors.New("eebus not configured")
|
||||
}
|
||||
|
|
@ -104,7 +105,7 @@ func NewEEBus(ski string, limits Limits, root api.Circuit) (*EEBus, error) {
|
|||
return nil, err
|
||||
}
|
||||
|
||||
if err := c.Wait(90 * time.Second); err != nil {
|
||||
if err := c.Wait(ctx); err != nil {
|
||||
return c, err
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package meter
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
|
|
@ -23,11 +24,11 @@ type EEBus struct {
|
|||
}
|
||||
|
||||
func init() {
|
||||
registry.Add("eebus", NewEEBusFromConfig)
|
||||
registry.AddCtx("eebus", NewEEBusFromConfig)
|
||||
}
|
||||
|
||||
// New creates an EEBus HEMS from generic config
|
||||
func NewEEBusFromConfig(other map[string]interface{}) (api.Meter, error) {
|
||||
func NewEEBusFromConfig(ctx context.Context, other map[string]interface{}) (api.Meter, error) {
|
||||
cc := struct {
|
||||
Ski string
|
||||
Ip string
|
||||
|
|
@ -40,11 +41,11 @@ func NewEEBusFromConfig(other map[string]interface{}) (api.Meter, error) {
|
|||
return nil, err
|
||||
}
|
||||
|
||||
return NewEEBus(cc.Ski, cc.Ip, cc.Timeout)
|
||||
return NewEEBus(ctx, cc.Ski, cc.Ip, cc.Timeout)
|
||||
}
|
||||
|
||||
// NewEEBus creates EEBus charger
|
||||
func NewEEBus(ski, ip string, timeout time.Duration) (*EEBus, error) {
|
||||
func NewEEBus(ctx context.Context, ski, ip string, timeout time.Duration) (*EEBus, error) {
|
||||
if eebus.Instance == nil {
|
||||
return nil, errors.New("eebus not configured")
|
||||
}
|
||||
|
|
@ -63,7 +64,7 @@ func NewEEBus(ski, ip string, timeout time.Duration) (*EEBus, error) {
|
|||
return nil, err
|
||||
}
|
||||
|
||||
if err := c.Wait(90 * time.Second); err != nil {
|
||||
if err := c.Wait(ctx); err != nil {
|
||||
return c, err
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,12 +1,15 @@
|
|||
package eebus
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/evcc-io/evcc/api"
|
||||
)
|
||||
|
||||
const registerTimeout = 90 * time.Second
|
||||
|
||||
type Connector struct {
|
||||
once sync.Once
|
||||
connectC chan struct{}
|
||||
|
|
@ -16,9 +19,11 @@ func NewConnector() *Connector {
|
|||
return &Connector{connectC: make(chan struct{})}
|
||||
}
|
||||
|
||||
func (c *Connector) Wait(timeout time.Duration) error {
|
||||
func (c *Connector) Wait(ctx context.Context) error {
|
||||
select {
|
||||
case <-time.After(timeout):
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
case <-time.After(registerTimeout):
|
||||
return api.ErrTimeout
|
||||
case <-c.connectC:
|
||||
return nil
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue