Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve SMA error handling #1975

Merged
merged 7 commits into from
Dec 2, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions meter/sma.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,14 @@ 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()

// call UpdateValues first to check if we get an error
DerAndereAndi marked this conversation as resolved.
Show resolved Hide resolved
if err = sm.device.UpdateValues(); err != nil {
return nil, err
} else {
DerAndereAndi marked this conversation as resolved.
Show resolved Hide resolved
// 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)
Expand Down
14 changes: 8 additions & 6 deletions provider/sma/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,19 @@ type Device struct {
func (d *Device) StartUpdateLoop() {
d.once.Do(func() {
go func() {
d.updateValues()
DerAndereAndi marked this conversation as resolved.
Show resolved Hide resolved
if err := d.UpdateValues(); err != nil {
d.log.ERROR.Println(err)
}
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()

Expand All @@ -42,9 +46,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) {
Expand Down