forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopenwb.go
57 lines (46 loc) · 1.01 KB
/
openwb.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package provider
import (
"github.com/andig/evcc/api"
"github.com/andig/evcc/util"
)
type openWBStatusProvider struct {
plugged, charging func() (bool, error)
}
func openWBStatusFromConfig(other map[string]interface{}) (func() (string, error), error) {
cc := struct {
Plugged, Charging Config
}{}
if err := util.DecodeOther(other, &cc); err != nil {
return nil, err
}
plugged, err := NewBoolGetterFromConfig(cc.Plugged)
var charging func() (bool, error)
if err == nil {
charging, err = NewBoolGetterFromConfig(cc.Charging)
}
if err != nil {
return nil, err
}
o := &openWBStatusProvider{
plugged: plugged,
charging: charging,
}
return o.stringGetter, nil
}
func (o *openWBStatusProvider) stringGetter() (string, error) {
charging, err := o.charging()
if err != nil {
return "", err
}
if charging {
return string(api.StatusC), nil
}
plugged, err := o.plugged()
if err != nil {
return "", err
}
if plugged {
return string(api.StatusB), nil
}
return string(api.StatusA), nil
}