forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
evsewifi.go
265 lines (221 loc) Β· 6.72 KB
/
evsewifi.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
package charger
import (
"errors"
"fmt"
"strings"
"time"
"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/charger/evse"
"github.com/evcc-io/evcc/provider"
"github.com/evcc-io/evcc/util"
"github.com/evcc-io/evcc/util/request"
)
// EVSEWifi charger implementation
type EVSEWifi struct {
*request.Helper
uri string
alwaysActive bool
current int64 // current will always be the physical value sent to the API
hires bool
paramG provider.Cacheable[evse.ListEntry]
}
func init() {
registry.Add("smartwb", NewEVSEWifiFromConfig)
registry.Add("evsewifi", NewEVSEWifiFromConfig)
}
//go:generate go run ../cmd/tools/decorate.go -f decorateEVSE -b *EVSEWifi -r api.Charger -t "api.Meter,CurrentPower,func() (float64, error)" -t "api.MeterEnergy,TotalEnergy,func() (float64, error)" -t "api.PhaseCurrents,Currents,func() (float64, float64, float64, error)" -t "api.PhaseVoltages,Voltages,func() (float64, float64, float64, error)" -t "api.ChargerEx,MaxCurrentMillis,func(float64) error" -t "api.Identifier,Identify,func() (string, error)"
// NewEVSEWifiFromConfig creates a EVSEWifi charger from generic config
func NewEVSEWifiFromConfig(other map[string]interface{}) (api.Charger, error) {
cc := struct {
URI string
Meter struct {
Power, Energy, Currents, Voltages bool
}
Cache time.Duration
}{
Cache: time.Second,
}
if err := util.DecodeOther(other, &cc); err != nil {
return nil, err
}
wb, err := NewEVSEWifi(util.DefaultScheme(cc.URI, "http"), cc.Cache)
if err != nil {
return wb, err
}
// auto-detect capabilities
params, err := wb.paramG.Get()
if err != nil {
return wb, err
}
if !params.AlwaysActive {
return nil, errors.New("evse must be configured to remote mode")
}
if params.UseMeter {
cc.Meter.Power = true
cc.Meter.Energy = true
cc.Meter.Currents = true
cc.Meter.Voltages = true
}
if params.ActualCurrentMA != nil {
wb.hires = true
}
// decorate Charger with Meter
var currentPower func() (float64, error)
if cc.Meter.Power {
currentPower = wb.currentPower
}
// decorate Charger with MeterEnergy
var totalEnergy func() (float64, error)
if cc.Meter.Energy {
totalEnergy = wb.totalEnergy
}
// decorate Charger with PhaseCurrents
var currents func() (float64, float64, float64, error)
if cc.Meter.Currents {
currents = wb.currents
}
// decorate Charger with PhaseVoltages
var voltages func() (float64, float64, float64, error)
if cc.Meter.Voltages {
voltages = wb.voltages
}
// decorate Charger with MaxCurrentEx
var maxCurrentEx func(float64) error
if wb.hires {
maxCurrentEx = wb.maxCurrentEx
wb.current = 100 * wb.current
}
// decorate Charger with Identifier
var identify func() (string, error)
if params.RFIDUID != nil {
identify = wb.identify
}
return decorateEVSE(wb, currentPower, totalEnergy, currents, voltages, maxCurrentEx, identify), nil
}
// NewEVSEWifi creates EVSEWifi charger
func NewEVSEWifi(uri string, cache time.Duration) (*EVSEWifi, error) {
log := util.NewLogger("evse")
wb := &EVSEWifi{
Helper: request.NewHelper(log),
uri: strings.TrimRight(uri, "/"),
current: 6, // 6A defined value
}
wb.paramG = provider.ResettableCached(func() (evse.ListEntry, error) {
var res evse.ParameterResponse
uri := fmt.Sprintf("%s/getParameters", wb.uri)
if err := wb.GetJSON(uri, &res); err != nil {
return evse.ListEntry{}, err
}
if len(res.List) != 1 {
return evse.ListEntry{}, fmt.Errorf("unexpected response: %s", res.Type)
}
wb.alwaysActive = res.List[0].AlwaysActive
return res.List[0], nil
}, cache)
return wb, nil
}
// Status implements the api.Charger interface
func (wb *EVSEWifi) Status() (api.ChargeStatus, error) {
params, err := wb.paramG.Get()
if err != nil {
return api.StatusNone, err
}
switch params.VehicleState {
case 1: // ready
return api.StatusA, nil
case 2: // EV is present
return api.StatusB, nil
case 3: // charging
return api.StatusC, nil
case 4: // charging with ventilation
return api.StatusD, nil
case 5: // failure (e.g. diode check, RCD failure)
return api.StatusE, nil
default:
return api.StatusNone, errors.New("invalid response")
}
}
// Enabled implements the api.Charger interface
func (wb *EVSEWifi) Enabled() (bool, error) {
params, err := wb.paramG.Get()
return params.EvseState, err
}
// get executes GET request and checks for EVSE error response
func (wb *EVSEWifi) get(uri string) error {
b, err := wb.GetBody(uri)
if err == nil && !strings.HasPrefix(string(b), evse.Success) {
err = errors.New(string(b))
}
return err
}
// Enable implements the api.Charger interface
func (wb *EVSEWifi) Enable(enable bool) error {
uri := fmt.Sprintf("%s/setStatus?active=%v", wb.uri, enable)
if wb.alwaysActive {
var current int64
if enable {
current = wb.current
}
uri = fmt.Sprintf("%s/setCurrent?current=%d", wb.uri, current)
}
err := wb.get(uri)
if err == nil {
wb.paramG.Reset()
}
return err
}
// MaxCurrent implements the api.Charger interface
func (wb *EVSEWifi) MaxCurrent(current int64) error {
if wb.hires {
current = 100 * current
}
wb.current = current
uri := fmt.Sprintf("%s/setCurrent?current=%d", wb.uri, current)
err := wb.get(uri)
if err == nil {
wb.paramG.Reset()
}
return err
}
// maxCurrentEx implements the api.ChargerEx interface
func (wb *EVSEWifi) maxCurrentEx(current float64) error {
wb.current = int64(100 * current)
uri := fmt.Sprintf("%s/setCurrent?current=%d", wb.uri, wb.current)
return wb.get(uri)
}
// CurrentPower implements the api.Meter interface
func (wb *EVSEWifi) currentPower() (float64, error) {
params, err := wb.paramG.Get()
return 1000 * params.ActualPower, err
}
// TotalEnergy implements the api.MeterEnergy interface
func (wb *EVSEWifi) totalEnergy() (float64, error) {
params, err := wb.paramG.Get()
return params.MeterReading, err
}
// Currents implements the api.PhaseCurrentss interface
func (wb *EVSEWifi) currents() (float64, float64, float64, error) {
params, err := wb.paramG.Get()
return params.CurrentP1, params.CurrentP2, params.CurrentP3, err
}
// Voltages implements the api.PhaseCurrentss interface
func (wb *EVSEWifi) voltages() (float64, float64, float64, error) {
params, err := wb.paramG.Get()
return params.VoltageP1, params.VoltageP2, params.VoltageP3, err
}
// Identify implements the api.Identifier interface
func (wb *EVSEWifi) identify() (string, error) {
params, err := wb.paramG.Get()
if err != nil {
return "", err
}
// we can rely on RFIDUID != nil here since identify() is only exposed if the EVSE API supports that property
return *params.RFIDUID, nil
}
var _ api.Resurrector = (*EVSEWifi)(nil)
// WakeUp implements the Resurrector interface
func (wb *EVSEWifi) WakeUp() error {
uri := fmt.Sprintf("%s/interruptCp", wb.uri)
_, err := wb.GetBody(uri)
return err
}