SMA: improve error handling (#1975)

This commit is contained in:
Andreas Linde 2021-12-02 11:12:26 +01:00 • committed by GitHub
parent 23bf8b51b3
commit cb3c8efb1e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 8 deletions

View file

@ -15,7 +15,6 @@ import (
// SMA supporting SMA Home Manager 2.0, SMA Energy Meter 30 and SMA inverter
type SMA struct {
log *util.Logger
uri string
scale float64
device *sma.Device
@ -48,7 +47,6 @@ func NewSMAFromConfig(other map[string]interface{}) (api.Meter, error) {
// NewSMA creates a SMA Meter
func NewSMA(uri, password, iface string, serial uint32, scale float64) (api.Meter, error) {
sm := &SMA{
log: util.NewLogger("sma").Redact(password),
uri: uri,
scale: scale,
}
@ -74,6 +72,12 @@ func NewSMA(uri, password, iface string, serial uint32, scale float64) (api.Mete
default:
return nil, errors.New("missing uri or serial")
}
// call UpdateValues first to check if we get an error
if err := sm.device.UpdateValues(); err != nil {
return nil, err
}
// start update loop manually to get values as fast as possible
sm.device.StartUpdateLoop()

View file

@ -24,15 +24,16 @@ type Device struct {
func (d *Device) StartUpdateLoop() {
d.once.Do(func() {
go func() {
d.updateValues()
for range time.NewTicker(time.Second * 5).C {
d.updateValues()
if err := d.UpdateValues(); err != nil {
d.log.ERROR.Println(err)
}
}
}()
})
}
func (d *Device) updateValues() {
func (d *Device) UpdateValues() error {
d.mux.Lock()
defer d.mux.Unlock()
@ -42,9 +43,7 @@ func (d *Device) updateValues() {
d.mux.Update()
}
if err != nil {
d.log.ERROR.Println(err)
}
return err
}
func (d *Device) Values() (map[sunny.ValueID]interface{}, error) {