forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopenwb-pro.go
219 lines (173 loc) Β· 4.97 KB
/
openwb-pro.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package charger
import (
"fmt"
"strings"
"time"
"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/charger/openwb/pro"
"github.com/evcc-io/evcc/provider"
"github.com/evcc-io/evcc/util"
"github.com/evcc-io/evcc/util/request"
)
func init() {
registry.Add("openwbpro", NewOpenWBProFromConfig)
}
// https://openwb.de/main/?page_id=771
// OpenWBPro charger implementation
type OpenWBPro struct {
*request.Helper
uri string
current float64
statusG provider.Cacheable[pro.Status]
}
// NewOpenWBProFromConfig creates a OpenWBPro charger from generic config
func NewOpenWBProFromConfig(other map[string]interface{}) (api.Charger, error) {
cc := struct {
URI string
Cache time.Duration
}{
Cache: time.Second,
}
if err := util.DecodeOther(other, &cc); err != nil {
return nil, err
}
return NewOpenWBPro(util.DefaultScheme(cc.URI, "http"), cc.Cache)
}
// NewOpenWBPro creates OpenWBPro charger
func NewOpenWBPro(uri string, cache time.Duration) (*OpenWBPro, error) {
log := util.NewLogger("owbpro")
wb := &OpenWBPro{
Helper: request.NewHelper(log),
uri: strings.TrimRight(uri, "/"),
current: 6, // 6A defined value
}
wb.statusG = provider.ResettableCached(func() (pro.Status, error) {
var res pro.Status
uri := fmt.Sprintf("%s/%s", wb.uri, "connect.php")
err := wb.GetJSON(uri, &res)
return res, err
}, cache)
go wb.heartbeat(log)
return wb, nil
}
func (wb *OpenWBPro) heartbeat(log *util.Logger) {
for range time.Tick(30 * time.Second) {
if _, err := wb.statusG.Get(); err != nil {
log.ERROR.Printf("heartbeat: %v", err)
}
}
}
func (wb *OpenWBPro) set(payload string) error {
uri := fmt.Sprintf("%s/%s", wb.uri, "connect.php")
resp, err := wb.Post(uri, "application/x-www-form-urlencoded", strings.NewReader(payload))
if err == nil {
resp.Body.Close()
wb.statusG.Reset()
}
return err
}
// Status implements the api.Charger interface
func (wb *OpenWBPro) Status() (api.ChargeStatus, error) {
resp, err := wb.statusG.Get()
if err != nil {
return api.StatusNone, err
}
res := api.StatusA
switch {
case resp.ChargeState:
res = api.StatusC
case resp.PlugState:
res = api.StatusB
}
return res, nil
}
// Enabled implements the api.Charger interface
func (wb *OpenWBPro) Enabled() (bool, error) {
res, err := wb.statusG.Get()
return res.OfferedCurrent > 0, err
}
// Enable implements the api.Charger interface
func (wb *OpenWBPro) Enable(enable bool) error {
payload := "ampere=0"
if enable {
payload = fmt.Sprintf("ampere=%.1f", wb.current)
}
return wb.set(payload)
}
// MaxCurrent implements the api.Charger interface
func (wb *OpenWBPro) MaxCurrent(current int64) error {
return wb.MaxCurrentMillis(float64(current))
}
var _ api.ChargerEx = (*OpenWBPro)(nil)
// MaxCurrentMillis implements the api.ChargerEx interface
func (wb *OpenWBPro) MaxCurrentMillis(current float64) error {
err := wb.set(fmt.Sprintf("ampere=%.1f", current))
if err == nil {
wb.current = current
}
return err
}
var _ api.Meter = (*OpenWBPro)(nil)
// CurrentPower implements the api.Meter interface
func (wb *OpenWBPro) CurrentPower() (float64, error) {
res, err := wb.statusG.Get()
return res.PowerAll, err
}
var _ api.MeterEnergy = (*OpenWBPro)(nil)
// TotalEnergy implements the api.MeterEnergy interface
func (wb *OpenWBPro) TotalEnergy() (float64, error) {
res, err := wb.statusG.Get()
return res.Imported / 1e3, err
}
// getPhaseValues returns phase values
func (wb *OpenWBPro) getPhaseValues(f func(pro.Status) []float64) (float64, float64, float64, error) {
status, err := wb.statusG.Get()
if err != nil {
return 0, 0, 0, err
}
res := f(status)
if len(res) != 3 {
return 0, 0, 0, fmt.Errorf("invalid phases: %v", res)
}
return res[0], res[1], res[2], nil
}
var _ api.PhaseVoltages = (*OpenWBPro)(nil)
// Voltages implements the api.PhaseVoltages interface
func (wb *OpenWBPro) Voltages() (float64, float64, float64, error) {
return wb.getPhaseValues(func(s pro.Status) []float64 {
return s.Voltages
})
}
var _ api.PhaseCurrents = (*OpenWBPro)(nil)
// Currents implements the api.PhaseCurrents interface
func (wb *OpenWBPro) Currents() (float64, float64, float64, error) {
return wb.getPhaseValues(func(s pro.Status) []float64 {
return s.Currents
})
}
var _ api.Battery = (*OpenWBPro)(nil)
// Soc implements the api.Battery interface
func (wb *OpenWBPro) Soc() (float64, error) {
res, err := wb.statusG.Get()
if err != nil {
return 0, err
}
if time.Since(time.Unix(res.SocTimestamp, 0)) > 5*time.Minute {
return 0, api.ErrNotAvailable
}
return float64(res.Soc), nil
}
var _ api.PhaseSwitcher = (*OpenWBPro)(nil)
// Phases1p3p implements the api.PhaseSwitcher interface
func (wb *OpenWBPro) Phases1p3p(phases int) error {
return wb.set(fmt.Sprintf("phasetarget=%d", phases))
}
var _ api.Identifier = (*OpenWBPro)(nil)
// Identify implements the api.Identifier interface
func (wb *OpenWBPro) Identify() (string, error) {
res, err := wb.statusG.Get()
if err != nil || res.VehicleID == "--" {
return "", err
}
return res.VehicleID, err
}