forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopenwb.go
65 lines (53 loc) · 1.44 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
58
59
60
61
62
63
64
65
package provider
import (
"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/util"
)
// OpenWBStatus implements status conversion from openWB to api.Status
type OpenWBStatus struct {
plugged, charging func() (bool, error)
}
// NewOpenWBStatusProviderFromConfig creates OpenWBStatus from given configuration
func NewOpenWBStatusProviderFromConfig(other map[string]interface{}) (func() (string, error), error) {
var 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 := NewOpenWBStatusProvider(plugged, charging)
return o.StringGetter, nil
}
// NewOpenWBStatusProvider creates provider for OpenWB status converted from MQTT topics
func NewOpenWBStatusProvider(plugged, charging func() (bool, error)) *OpenWBStatus {
return &OpenWBStatus{
plugged: plugged,
charging: charging,
}
}
// StringGetter returns string from OpenWB charging/ plugged status
func (o *OpenWBStatus) 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
}