SMA: fix error messages (#1196)

This commit is contained in:
Benjamin Böhmke 2021-07-06 18:13:17 +02:00 • committed by GitHub
parent ae764c19ec
commit f9214a671e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 28 additions and 17 deletions

2
go.mod
View file

@ -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

4
go.sum
View file

@ -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=

View file

@ -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)

View file

@ -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()

View file

@ -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) {