EEBus: await control-write results across all use cases (#31350)
This commit is contained in:
parent
2ac2fae0a3
commit
258dcb41b5
5 changed files with 68 additions and 96 deletions
|
|
@ -2,9 +2,12 @@ package eebus
|
|||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
eebusapi "github.com/enbility/eebus-go/api"
|
||||
"github.com/enbility/spine-go/model"
|
||||
"github.com/evcc-io/evcc/api"
|
||||
)
|
||||
|
||||
|
|
@ -15,6 +18,33 @@ func WrapError(err error) error {
|
|||
return err
|
||||
}
|
||||
|
||||
// WriteTimeout bounds how long an awaited eebus write waits for its result.
|
||||
const WriteTimeout = 10 * time.Second
|
||||
|
||||
// Await runs a control write and waits for the remote device's result, returning
|
||||
// an error if the write is rejected or no result arrives within WriteTimeout.
|
||||
func Await(write func(func(model.ResultDataType)) (*model.MsgCounterType, error)) error {
|
||||
res := make(chan model.ResultDataType, 1)
|
||||
|
||||
if _, err := write(func(r model.ResultDataType) { res <- r }); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
select {
|
||||
case r := <-res:
|
||||
if r.ErrorNumber != nil && *r.ErrorNumber != 0 {
|
||||
err := fmt.Errorf("write rejected: %d", *r.ErrorNumber)
|
||||
if r.Description != nil {
|
||||
err = fmt.Errorf("%w (%s)", err, *r.Description)
|
||||
}
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
case <-time.After(WriteTimeout):
|
||||
return errors.New("write result timeout")
|
||||
}
|
||||
}
|
||||
|
||||
func LogEntities(log *log.Logger, actor string, uc eebusapi.UseCaseInterface) {
|
||||
ss := uc.RemoteEntitiesScenarios()
|
||||
if len(ss) > 0 {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue