From f9214a671eb9bcff3db9d308ef3bf6341bf13855 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20B=C3=B6hmke?= Date: Tue, 6 Jul 2021 18:13:17 +0200 Subject: [PATCH] SMA: fix error messages (#1196) --- go.mod | 2 +- go.sum | 4 ++-- meter/sma.go | 2 ++ provider/sma/device.go | 24 ++++++++++++++++++++++-- provider/sma/discover.go | 13 +------------ 5 files changed, 28 insertions(+), 17 deletions(-) diff --git a/go.mod b/go.mod index 312e9a962..b013ca495 100644 --- a/go.mod +++ b/go.mod @@ -66,7 +66,7 @@ require ( github.com/thoas/go-funk v0.8.0 github.com/tv42/httpunix v0.0.0-20191220191345-2ba4b9c3382c github.com/volkszaehler/mbmd v0.0.0-20210526131012-e1fec7232ed7 - gitlab.com/bboehmke/sunny v0.13.1 + gitlab.com/bboehmke/sunny v0.13.2 golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a // indirect golang.org/x/net v0.0.0-20210614182718-04defd469f4e golang.org/x/oauth2 v0.0.0-20210615190721-d04028783cf1 diff --git a/go.sum b/go.sum index 447e40357..ee143ff7e 100644 --- a/go.sum +++ b/go.sum @@ -660,8 +660,8 @@ github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9de github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= -gitlab.com/bboehmke/sunny v0.13.1 h1:X7fUD17KwG1DKgTccqA/q18F2tzB0qCu2n+qj3bedBk= -gitlab.com/bboehmke/sunny v0.13.1/go.mod h1:F5AIuL7kYteSJFR5E+YEocxIdpyCXmtDciFmMQVjP88= +gitlab.com/bboehmke/sunny v0.13.2 h1:blQPB2hcqX11/a9L1AiiEVSqxH+s9/I7JZ2M1TiS9bY= +gitlab.com/bboehmke/sunny v0.13.2/go.mod h1:F5AIuL7kYteSJFR5E+YEocxIdpyCXmtDciFmMQVjP88= go.coder.com/go-tools v0.0.0-20190317003359-0c6a35b74a16/go.mod h1:iKV5yK9t+J5nG9O3uF6KYdPEz3dyfMyB15MN1rbQ8Qw= go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU= go.etcd.io/etcd/api/v3 v3.5.0/go.mod h1:cbVKeC6lCfl7j/8jBhAK6aIYO9XOjdptoxU/nLQcPvs= diff --git a/meter/sma.go b/meter/sma.go index 859f266cd..7e180805e 100644 --- a/meter/sma.go +++ b/meter/sma.go @@ -81,6 +81,8 @@ func NewSMA(uri, password, iface string, serial uint32, scale float64) (api.Mete default: return nil, errors.New("missing uri or serial") } + // start update loop manually to get values as fast as possible + sm.device.StartUpdateLoop() // decorate api.Battery in case of inverter var soc func() (float64, error) diff --git a/provider/sma/device.go b/provider/sma/device.go index 61dfeddce..11a1738e4 100644 --- a/provider/sma/device.go +++ b/provider/sma/device.go @@ -2,6 +2,7 @@ package sma import ( "fmt" + "sync" "time" "github.com/andig/evcc/util" @@ -16,9 +17,22 @@ type Device struct { log *util.Logger mux *util.Waiter values map[sunny.ValueID]interface{} + once sync.Once } -func (d *Device) updateValues() error { +// StartUpdateLoop if not already started +func (d *Device) StartUpdateLoop() { + d.once.Do(func() { + go func() { + d.updateValues() + for range time.NewTicker(time.Second).C { + d.updateValues() + } + }() + }) +} + +func (d *Device) updateValues() { d.mux.Lock() defer d.mux.Unlock() @@ -27,10 +41,16 @@ func (d *Device) updateValues() error { err = mergo.Merge(&d.values, values, mergo.WithOverride) d.mux.Update() } - return err + + if err != nil { + d.log.ERROR.Println(err) + } } func (d *Device) Values() (map[sunny.ValueID]interface{}, error) { + // ensure update loop was started + d.StartUpdateLoop() + elapsed := d.mux.LockWithTimeout() defer d.mux.Unlock() diff --git a/provider/sma/discover.go b/provider/sma/discover.go index 82fee8418..4658729f5 100644 --- a/provider/sma/discover.go +++ b/provider/sma/discover.go @@ -62,23 +62,12 @@ type Discoverer struct { } func (d *Discoverer) createDevice(device *sunny.Device) *Device { - dev := &Device{ + return &Device{ Device: device, log: d.log, mux: util.NewWaiter(udpTimeout, func() { d.log.TRACE.Println("wait for initial value") }), values: make(map[sunny.ValueID]interface{}), } - - go func() { - for range time.NewTicker(time.Second).C { - err := dev.updateValues() - if err != nil { - d.log.ERROR.Println(err) - } - } - }() - - return dev } func (d *Discoverer) addDevice(device *sunny.Device) {