-
-
Notifications
You must be signed in to change notification settings - Fork 720
/
Copy pathgoodwe-wifi.go
86 lines (69 loc) · 1.77 KB
/
goodwe-wifi.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
77
78
79
80
81
82
83
84
85
86
package meter
import (
"time"
"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/meter/goodwe"
"github.com/evcc-io/evcc/util"
"github.com/evcc-io/evcc/util/request"
)
type goodWeWiFi struct {
usage string
inverter *util.Monitor[goodwe.Inverter]
}
func init() {
registry.Add("goodwe-wifi", NewGoodWeWifiFromConfig)
}
//go:generate go run ../cmd/tools/decorate.go -f decorateGoodWeWifi -b *goodWeWiFi -r api.Meter -t "api.Battery,Soc,func() (float64, error)"
// TODO deprecated remove
func NewGoodWeWifiFromConfig(other map[string]interface{}) (api.Meter, error) {
cc := struct {
capacity `mapstructure:",squash"`
URI, Usage string
Timeout time.Duration
}{
Timeout: request.Timeout,
}
if err := util.DecodeOther(other, &cc); err != nil {
return nil, err
}
return NewGoodWeWiFi(cc.URI, cc.Usage, cc.Timeout)
}
func NewGoodWeWiFi(uri, usage string, timeout time.Duration) (api.Meter, error) {
instance, err := goodwe.Instance(util.NewLogger("goodwe-wifi"))
if err != nil {
return nil, err
}
inverter := instance.GetInverter(uri)
if inverter == nil {
inverter = instance.AddInverter(uri, timeout)
}
res := &goodWeWiFi{
usage: usage,
inverter: inverter,
}
// decorate api.BatterySoc
var batterySoc func() (float64, error)
if usage == "battery" {
batterySoc = res.batterySoc
}
return decorateGoodWeWifi(res, batterySoc), nil
}
func (m *goodWeWiFi) CurrentPower() (float64, error) {
data, err := m.inverter.Get()
if err != nil {
return 0, err
}
switch m.usage {
case "grid":
return data.NetPower, nil
case "pv":
return data.PvPower, nil
case "battery":
return data.BatteryPower, nil
}
return 0, api.ErrNotAvailable
}
func (m *goodWeWiFi) batterySoc() (float64, error) {
data, err := m.inverter.Get()
return data.Soc, err
}