From 8b3deb21c179ec2c20ce3f9b64663c3cfa435f11 Mon Sep 17 00:00:00 2001 From: andig Date: Mon, 3 Mar 2025 11:31:56 +0100 Subject: [PATCH] RCT: handle initial connection failing to read (#19362) --- go.mod | 2 +- go.sum | 4 ++-- meter/rct.go | 9 +++++++++ meter/template_test.go | 1 + 4 files changed, 13 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 87772285d..c0061add5 100644 --- a/go.mod +++ b/go.mod @@ -222,4 +222,4 @@ replace github.com/grid-x/modbus => github.com/evcc-io/modbus v0.0.0-20241027151 replace github.com/lorenzodonini/ocpp-go => github.com/evcc-io/ocpp-go v0.0.0-20241230132027-815870498cc3 -replace github.com/mlnoga/rct => github.com/andig/rct v0.1.2-0.20250302110530-e1b03e640f26 +replace github.com/mlnoga/rct => github.com/andig/rct v0.1.2-0.20250303102452-38b496bdd2c9 diff --git a/go.sum b/go.sum index 3a48bba7c..f1ded472b 100644 --- a/go.sum +++ b/go.sum @@ -39,8 +39,8 @@ github.com/andig/gosunspec v0.0.0-20240918203654-860ce51d602b h1:81UMfM949I7StrR github.com/andig/gosunspec v0.0.0-20240918203654-860ce51d602b/go.mod h1:c6P6szcR+ROkqZruOR4f6qbDKFjZX6OitPpj+yJ/r8k= github.com/andig/mbserver v0.0.0-20230310211055-1d29cbb5820e h1:m/NTP3JWpR7M0ljLxiQU4fzR25jjhe1LDtxLMNcoNJQ= github.com/andig/mbserver v0.0.0-20230310211055-1d29cbb5820e/go.mod h1:4VtYzTm//oUipwvO3yh0g/udTE7pYJM+U/kyAuFDsgM= -github.com/andig/rct v0.1.2-0.20250302110530-e1b03e640f26 h1:Kqq17bxhK6TsBs54gf2Bk4pghBAAeJw1WN/RU0pgV1M= -github.com/andig/rct v0.1.2-0.20250302110530-e1b03e640f26/go.mod h1:KRjBDV8kuvGd2kJfN8/F0kjH8JjkFVpBpn7MhW+ACL8= +github.com/andig/rct v0.1.2-0.20250303102452-38b496bdd2c9 h1:hHUPfKTJlyHX0i5iZy7UNtIYoNDtcPH/Q1bbOe2OZ+g= +github.com/andig/rct v0.1.2-0.20250303102452-38b496bdd2c9/go.mod h1:KRjBDV8kuvGd2kJfN8/F0kjH8JjkFVpBpn7MhW+ACL8= github.com/andig/yaml v0.0.0-20240531135838-1ff5761ab467 h1:JqIoHxsQSV39xaemvVRu3HOkSf5AbhQ1YEENJa2cL3Q= github.com/andig/yaml v0.0.0-20240531135838-1ff5761ab467/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= github.com/andybalholm/cascadia v1.3.3 h1:AG2YHrzJIm4BZ19iwJ/DAua6Btl3IwJX+VI4kktS1LM= diff --git a/meter/rct.go b/meter/rct.go index 1ba17a762..bd2b577f0 100644 --- a/meter/rct.go +++ b/meter/rct.go @@ -6,7 +6,9 @@ import ( "errors" "fmt" "math" + "math/rand/v2" "strings" + "sync" "time" "github.com/evcc-io/evcc/api" @@ -20,6 +22,8 @@ type RCT struct { usage string // grid, pv, battery } +var rctMu sync.Mutex + func init() { registry.AddCtx("rct", NewRCTFromConfig) } @@ -52,6 +56,11 @@ func NewRCTFromConfig(ctx context.Context, other map[string]interface{}) (api.Me func NewRCT(ctx context.Context, uri, usage string, minSoc, maxSoc int, cache time.Duration, capacity func() float64) (api.Meter, error) { log := util.NewLogger("rct") + // prevent concurrent connections + rctMu.Lock() + time.Sleep(time.Duration(rand.Uint64N(uint64(time.Second)))) + rctMu.Unlock() + conn, err := rct.NewConnection(ctx, uri, rct.WithErrorCallback(func(err error) { if err != nil { log.ERROR.Println(err) diff --git a/meter/template_test.go b/meter/template_test.go index 0cdae033f..619db2f5e 100644 --- a/meter/template_test.go +++ b/meter/template_test.go @@ -23,6 +23,7 @@ var acceptable = []string{ "connect: connection refused", "connect: network is unreachable", "i/o timeout", + "timeout", // RCT "'sma': missing uri or serial", // SMA "[1ESY1161052714 1ESY1161229249 1EMH0008842285 1ESY1161978584 1EMH0004864048 1ESY1161979033 7ELS8135823805]", // Discovergy "can only have either uri or device", // modbus