Skip to content

Commit acb5213

Browse files
authored
EEBus: use context (#19284)
1 parent 59d7bb5 commit acb5213

File tree

5 files changed

+25
-17
lines changed

5 files changed

+25
-17
lines changed

charger/eebus.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package charger
22

33
import (
4+
"context"
45
"errors"
56
"fmt"
67
"slices"
@@ -49,11 +50,11 @@ type EEBus struct {
4950
}
5051

5152
func init() {
52-
registry.Add("eebus", NewEEBusFromConfig)
53+
registry.AddCtx("eebus", NewEEBusFromConfig)
5354
}
5455

5556
// NewEEBusFromConfig creates an EEBus charger from generic config
56-
func NewEEBusFromConfig(other map[string]interface{}) (api.Charger, error) {
57+
func NewEEBusFromConfig(ctx context.Context, other map[string]interface{}) (api.Charger, error) {
5758
cc := struct {
5859
Ski string
5960
Ip string
@@ -68,13 +69,13 @@ func NewEEBusFromConfig(other map[string]interface{}) (api.Charger, error) {
6869
return nil, err
6970
}
7071

71-
return NewEEBus(cc.Ski, cc.Ip, cc.Meter, cc.ChargedEnergy, cc.VasVW)
72+
return NewEEBus(ctx, cc.Ski, cc.Ip, cc.Meter, cc.ChargedEnergy, cc.VasVW)
7273
}
7374

7475
//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)"
7576

7677
// NewEEBus creates EEBus charger
77-
func NewEEBus(ski, ip string, hasMeter, hasChargedEnergy, vasVW bool) (api.Charger, error) {
78+
func NewEEBus(ctx context.Context, ski, ip string, hasMeter, hasChargedEnergy, vasVW bool) (api.Charger, error) {
7879
if eebus.Instance == nil {
7980
return nil, errors.New("eebus not configured")
8081
}
@@ -93,7 +94,7 @@ func NewEEBus(ski, ip string, hasMeter, hasChargedEnergy, vasVW bool) (api.Charg
9394
return nil, err
9495
}
9596

96-
if err := c.Wait(90 * time.Second); err != nil {
97+
if err := c.Wait(ctx); err != nil {
9798
return c, err
9899
}
99100

hems/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func NewFromConfig(ctx context.Context, typ string, other map[string]interface{}
2323
case "sma", "shm", "semp":
2424
return semp.New(other, site, httpd)
2525
case "eebus":
26-
return eebus.New(other, site)
26+
return eebus.New(ctx, other, site)
2727
case "relay":
2828
return relay.New(ctx, other, site)
2929
default:

hems/eebus/eebus.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package eebus
22

33
import (
4+
"context"
45
"errors"
56
"sync"
67
"time"
@@ -40,7 +41,7 @@ type Limits struct {
4041
}
4142

4243
// New creates an EEBus HEMS from generic config
43-
func New(other map[string]interface{}, site site.API) (*EEBus, error) {
44+
func New(ctx context.Context, other map[string]interface{}, site site.API) (*EEBus, error) {
4445
cc := struct {
4546
Ski string
4647
Limits `mapstructure:",squash"`
@@ -75,11 +76,11 @@ func New(other map[string]interface{}, site site.API) (*EEBus, error) {
7576
}
7677
site.SetCircuit(lpc)
7778

78-
return NewEEBus(cc.Ski, cc.Limits, lpc)
79+
return NewEEBus(ctx, cc.Ski, cc.Limits, lpc)
7980
}
8081

8182
// NewEEBus creates EEBus charger
82-
func NewEEBus(ski string, limits Limits, root api.Circuit) (*EEBus, error) {
83+
func NewEEBus(ctx context.Context, ski string, limits Limits, root api.Circuit) (*EEBus, error) {
8384
if eebus.Instance == nil {
8485
return nil, errors.New("eebus not configured")
8586
}
@@ -104,7 +105,7 @@ func NewEEBus(ski string, limits Limits, root api.Circuit) (*EEBus, error) {
104105
return nil, err
105106
}
106107

107-
if err := c.Wait(90 * time.Second); err != nil {
108+
if err := c.Wait(ctx); err != nil {
108109
return c, err
109110
}
110111

meter/eebus.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package meter
22

33
import (
4+
"context"
45
"errors"
56
"time"
67

@@ -23,11 +24,11 @@ type EEBus struct {
2324
}
2425

2526
func init() {
26-
registry.Add("eebus", NewEEBusFromConfig)
27+
registry.AddCtx("eebus", NewEEBusFromConfig)
2728
}
2829

2930
// New creates an EEBus HEMS from generic config
30-
func NewEEBusFromConfig(other map[string]interface{}) (api.Meter, error) {
31+
func NewEEBusFromConfig(ctx context.Context, other map[string]interface{}) (api.Meter, error) {
3132
cc := struct {
3233
Ski string
3334
Ip string
@@ -40,11 +41,11 @@ func NewEEBusFromConfig(other map[string]interface{}) (api.Meter, error) {
4041
return nil, err
4142
}
4243

43-
return NewEEBus(cc.Ski, cc.Ip, cc.Timeout)
44+
return NewEEBus(ctx, cc.Ski, cc.Ip, cc.Timeout)
4445
}
4546

4647
// NewEEBus creates EEBus charger
47-
func NewEEBus(ski, ip string, timeout time.Duration) (*EEBus, error) {
48+
func NewEEBus(ctx context.Context, ski, ip string, timeout time.Duration) (*EEBus, error) {
4849
if eebus.Instance == nil {
4950
return nil, errors.New("eebus not configured")
5051
}
@@ -63,7 +64,7 @@ func NewEEBus(ski, ip string, timeout time.Duration) (*EEBus, error) {
6364
return nil, err
6465
}
6566

66-
if err := c.Wait(90 * time.Second); err != nil {
67+
if err := c.Wait(ctx); err != nil {
6768
return c, err
6869
}
6970

server/eebus/connector.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package eebus
22

33
import (
4+
"context"
45
"sync"
56
"time"
67

78
"github.com/evcc-io/evcc/api"
89
)
910

11+
const registerTimeout = 90 * time.Second
12+
1013
type Connector struct {
1114
once sync.Once
1215
connectC chan struct{}
@@ -16,9 +19,11 @@ func NewConnector() *Connector {
1619
return &Connector{connectC: make(chan struct{})}
1720
}
1821

19-
func (c *Connector) Wait(timeout time.Duration) error {
22+
func (c *Connector) Wait(ctx context.Context) error {
2023
select {
21-
case <-time.After(timeout):
24+
case <-ctx.Done():
25+
return ctx.Err()
26+
case <-time.After(registerTimeout):
2227
return api.ErrTimeout
2328
case <-c.connectC:
2429
return nil

0 commit comments

Comments
 (0)