forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpcelectric.go
284 lines (242 loc) · 7.44 KB
/
pcelectric.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package charger
import (
"fmt"
"net/http"
"strings"
"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/charger/pcelectric"
"github.com/evcc-io/evcc/util"
"github.com/evcc-io/evcc/util/request"
"github.com/evcc-io/evcc/util/sponsor"
)
// PCElectric charger implementation
type PCElectric struct {
*request.Helper
log *util.Logger
uri string // http://garo2216247:8080/servlet
slaveIndex int // 0 = Master, 1..n Slave
meter string // <CENTRAL100|CENTRAL101|INTERNAL|EXTERNAL|TWIN>
lbmode bool // true/false (wird automatisch bestimmt)
serialNumber int // 1234567
}
func init() {
registry.Add("garo", NewPCElectricFromConfig)
registry.Add("pcelectric", NewPCElectricFromConfig)
}
//go:generate go run ../cmd/tools/decorate.go -f decoratePCE -b *PCElectric -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)"
// NewPCElectricFromConfig creates a PCElectric charger from generic config
func NewPCElectricFromConfig(other map[string]interface{}) (api.Charger, error) {
cc := struct {
URI string
SlaveIndex int
Meter string
}{
Meter: "CENTRAL100",
}
if err := util.DecodeOther(other, &cc); err != nil {
return nil, err
}
wb, err := NewPCElectric(util.DefaultScheme(cc.URI, "http"), cc.SlaveIndex, cc.Meter)
if err == nil && wb.slaveIndex == 0 { // Nur Master hat den Zähler...leider
var res pcelectric.MeterInfo
if err := wb.GetJSON(wb.meter, &res); err == nil && res.MeterSerial != "" {
return decoratePCE(wb, wb.currentPower, wb.totalEnergy, wb.currents), nil
}
wb.meter = ""
}
return wb, err
}
// NewPCElectric creates PCElectric charger
func NewPCElectric(uri string, slaveIndex int, meter string) (*PCElectric, error) {
log := util.NewLogger("pce")
uri = strings.TrimSuffix(strings.TrimRight(uri, "/"), "/servlet") + "/servlet/rest/chargebox"
if !sponsor.IsAuthorized() {
return nil, api.ErrSponsorRequired
}
wb := &PCElectric{
Helper: request.NewHelper(log),
log: log,
uri: uri,
slaveIndex: slaveIndex,
meter: strings.ToUpper(meter),
}
// Nur Master: lb Config auslesen.
// Ohne Loadbalancer: Steuerung über currentlimit
// Mit Loadbalander: Steuerung über loadBalancingFuse
var lbconfig pcelectric.LbConfig
uri = fmt.Sprintf("%s/lbconfig/false", wb.uri)
if err := wb.GetJSON(uri, &lbconfig); err == nil {
wb.lbmode = lbconfig.MasterLoadBalanced
wb.serialNumber = lbconfig.Slaves[wb.slaveIndex].SerialNumber
log.DEBUG.Printf("lbmode: %t serial: %d ", wb.lbmode, wb.serialNumber)
}
return wb, nil
}
// Status implements the api.Charger interface
func (wb *PCElectric) Status() (api.ChargeStatus, error) {
var chargeStatus int
var sessionStartTime int64
if wb.slaveIndex == 0 {
var status pcelectric.Status
uri := fmt.Sprintf("%s/status", wb.uri)
if err := wb.GetJSON(uri, &status); err != nil {
return api.StatusNone, err
}
chargeStatus = status.ChargeStatus
sessionStartTime = status.SessionStartTime
} else {
var status pcelectric.SlaveStatus
uri := fmt.Sprintf("%s/slaves/false", wb.uri)
if err := wb.GetJSON(uri, &status); err != nil {
return api.StatusNone, err
}
if wb.slaveIndex >= len(status) {
return api.StatusNone, nil
}
chargeStatus = status[wb.slaveIndex].ChargeStatus
sessionStartTime = status[wb.slaveIndex].SessionStartTime
}
wb.log.DEBUG.Printf("chargeStatus: %d", chargeStatus)
var res api.ChargeStatus
switch chargeStatus {
case 0x00, 0x10: // notconnected
res = api.StatusA
case 0x30: // connected
res = api.StatusB
case 0x40: // charging
res = api.StatusC
case 0x42, // chargepaused
0x50, // chargefinished
0x60: // chargecancelled
res = api.StatusB
case 0x90: // unavailable
if sessionStartTime > 0 {
res = api.StatusB
} else {
res = api.StatusF
}
case 0x95, // dcfault
0x96, // dchardwarefault
0x9A, // cpfault
0x9B: // cpshorted
res = api.StatusE
case 0x70, // overheat
0x80, // criticaltemperature
0x91, // reserved
0x9C, // remotedisabled
0x9D, // dlmfault
0xA0, // cablefault
0xA1,
0xA2, // lockingfault
0xA3,
0xA4, // contactorfault
0xA8, // rcdfault
0xF0, // wait
0xF1: // ventfault
res = api.StatusF
default: // generalfault
res = api.StatusF
}
return res, nil
}
// Enabled implements the api.Charger interface
func (wb *PCElectric) Enabled() (bool, error) {
var res pcelectric.Status
uri := fmt.Sprintf("%s/status", wb.uri)
err := wb.GetJSON(uri, &res)
if err == nil && res.PowerMode == "ON" {
return true, err
}
return false, err
}
// Enable implements the api.Charger interface
func (wb *PCElectric) Enable(enable bool) error {
if wb.slaveIndex > 0 {
return nil // Slave wird immer mit dem Master geschaltet!
}
// Master Only !!
mode := "ALWAYS_OFF"
if enable {
mode = "ALWAYS_ON"
}
uri := fmt.Sprintf("%s/mode/%s", wb.uri, mode)
req, err := request.New(http.MethodPost, uri, nil, request.JSONEncoding)
if err == nil {
_, err = wb.DoBody(req)
}
return err
}
func (wb *PCElectric) MinCurrent(current int64) error {
data := pcelectric.MinCurrentLimitStruct{
{
MinCurrentLimit: int(current), // default=6
SerialNumber: wb.serialNumber,
TwinSerial: -1,
},
}
uri := fmt.Sprintf("%s/mincurrentlimit", wb.uri)
req, err := request.New(http.MethodPost, uri, request.MarshalJSON(data), request.JSONEncoding)
if err == nil {
_, err = wb.DoBody(req)
}
return err
}
// MaxCurrent implements the api.Charger interface
func (wb *PCElectric) MaxCurrent(current int64) error {
if wb.slaveIndex > 0 {
return nil // Slave wird immer mit dem Master geschaltet!
}
// Ohne Loadbalancer Regelung über currentlimit:
if !wb.lbmode {
data := pcelectric.ReducedIntervals{
ReducedIntervalsEnabled: true,
ReducedCurrentIntervals: []pcelectric.ReducedCurrentInterval{
{
SchemaId: 1,
Start: "00:00:00",
Stop: "24:00:00",
Weekday: 8,
ChargeLimit: int(current),
},
},
}
uri := fmt.Sprintf("%s/currentlimit", wb.uri)
req, err := request.New(http.MethodPost, uri, request.MarshalJSON(data), request.JSONEncoding)
if err == nil {
_, err = wb.DoBody(req)
}
return err
}
// Mit Loadbalancer Regelung über lbconfig/LoadBalancingFuse
var data pcelectric.LbConfigShort
uri := fmt.Sprintf("%s/lbconfig/false", wb.uri)
if err := wb.GetJSON(uri, &data); err != nil {
return err
}
data.LoadBalancingFuse = int(current)
uri = fmt.Sprintf("%s/lbconfig", wb.uri)
req, err := request.New(http.MethodPost, uri, request.MarshalJSON(data), request.JSONEncoding)
if err == nil {
_, err = wb.DoBody(req)
}
return err
}
// CurrentPower implements the api.Meter interface W
func (wb *PCElectric) currentPower() (float64, error) {
l1, l2, l3, err := wb.currents()
return 230 * (l1 + l2 + l3), err
}
// TotalEnergy implements the api.MeterEnergy interface kwh
func (wb *PCElectric) totalEnergy() (float64, error) {
var res pcelectric.MeterInfo
uri := fmt.Sprintf("%s/meterinfo/%s", wb.uri, wb.meter)
err := wb.GetJSON(uri, &res)
return float64(res.AccEnergy) / 1e3, err
}
// Currents implements the api.PhaseCurrentss interface A
func (wb *PCElectric) currents() (float64, float64, float64, error) {
var res pcelectric.MeterInfo
uri := fmt.Sprintf("%s/meterinfo/%s", wb.uri, wb.meter)
err := wb.GetJSON(uri, &res)
return float64(res.Phase1Current) / 10, float64(res.Phase2Current) / 10, float64(res.Phase3Current) / 10, err
}