Support decoding openWB status
This commit is contained in:
parent
516934ca6e
commit
67d2144fc2
2 changed files with 55 additions and 0 deletions
|
|
@ -114,6 +114,8 @@ func NewStringGetterFromConfig(log *api.Logger, config Config) (res StringGetter
|
|||
if pc.Cache > 0 {
|
||||
res = NewCached(res, pc.Cache).StringGetter()
|
||||
}
|
||||
case "openwb":
|
||||
res = openWBStatusFromConfig(log, config.Other)
|
||||
default:
|
||||
log.FATAL.Fatalf("invalid provider type %s", config.Type)
|
||||
}
|
||||
|
|
|
|||
53
provider/openwb.go
Normal file
53
provider/openwb.go
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
package provider
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/andig/evcc/api"
|
||||
)
|
||||
|
||||
// openWBConfig is the specific mqtt getter/setter configuration
|
||||
type openWBConfig struct {
|
||||
PlugStat, ChargeStat string
|
||||
Timeout time.Duration
|
||||
}
|
||||
|
||||
type openWBStatusProvider struct {
|
||||
plugStat, chargeStat BoolGetter
|
||||
}
|
||||
|
||||
func openWBStatusFromConfig(log *api.Logger, other map[string]interface{}) (res StringGetter) {
|
||||
if MQTT == nil {
|
||||
log.FATAL.Fatal("mqtt not configured")
|
||||
}
|
||||
|
||||
var pc openWBConfig
|
||||
api.DecodeOther(log, other, &pc)
|
||||
|
||||
o := &openWBStatusProvider{
|
||||
plugStat: MQTT.BoolGetter(pc.PlugStat, pc.Timeout),
|
||||
chargeStat: MQTT.BoolGetter(pc.ChargeStat, pc.Timeout),
|
||||
}
|
||||
|
||||
return o.stringGetter
|
||||
}
|
||||
|
||||
func (o *openWBStatusProvider) stringGetter() (string, error) {
|
||||
charging, err := o.chargeStat()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if charging {
|
||||
return string(api.StatusC), nil
|
||||
}
|
||||
|
||||
plugged, err := o.plugStat()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if plugged {
|
||||
return string(api.StatusB), nil
|
||||
}
|
||||
|
||||
return string(api.StatusA), nil
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue