forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 2
/
combined.go
76 lines (61 loc) · 1.64 KB
/
combined.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
66
67
68
69
70
71
72
73
74
75
76
package provider
import (
"context"
"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/util"
)
func init() {
registry.AddCtx("combined", NewCombinedFromConfig)
registry.AddCtx("openwb", NewCombinedFromConfig)
}
// combinedProvider implements status conversion from openWB to api.Status
type combinedProvider struct {
plugged, charging func() (bool, error)
}
// NewCombinedFromConfig creates combined provider
func NewCombinedFromConfig(ctx context.Context, other map[string]interface{}) (Provider, error) {
var cc struct {
Plugged, Charging Config
}
if err := util.DecodeOther(other, &cc); err != nil {
return nil, err
}
plugged, err := NewBoolGetterFromConfig(ctx, cc.Plugged)
if err != nil {
return nil, err
}
charging, err := NewBoolGetterFromConfig(ctx, cc.Charging)
if err != nil {
return nil, err
}
o := NewCombinedProvider(plugged, charging)
return o, nil
}
// NewCombinedProvider creates provider for OpenWB status converted from MQTT topics
func NewCombinedProvider(plugged, charging func() (bool, error)) *combinedProvider {
return &combinedProvider{
plugged: plugged,
charging: charging,
}
}
var _ StringProvider = (*combinedProvider)(nil)
// StringGetter returns string from OpenWB charging/ plugged status
func (o *combinedProvider) StringGetter() (func() (string, error), error) {
return func() (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
}, nil
}