forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdelta.go
320 lines (269 loc) Β· 8.98 KB
/
delta.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
package charger
import (
"fmt"
"math"
"sync"
"time"
"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/core/loadpoint"
"github.com/evcc-io/evcc/util"
"github.com/evcc-io/evcc/util/modbus"
"github.com/evcc-io/evcc/util/sponsor"
"github.com/volkszaehler/mbmd/encoding"
)
// Delta charger implementation
type Delta struct {
log *util.Logger
conn *modbus.Connection
lp loadpoint.API
mu sync.Mutex
curr float64
base uint16
enabled bool
isBasic bool
}
const (
// EV Charger
// Read Input Registers (0x04)
deltaRegState = 100 // Charger State - UINT16 0: not ready, 1: operational, 10: faulted, 255: not responding
deltaRegCount = 102 // Charger EVSE Count - UINT16
deltaRegSerial = 110 // Charger Serial - STRING20
deltaRegModel = 130 // Charger Model - STRING20
// Write Multiple Registers (0x10)
deltaRegCommunicationTimeoutEnabled = 201 // Communication Timeout Enabled - UINT16 0: false, 1: true
deltaRegCommunicationTimeout = 202 // Communication Timeout - UINT16 [s]
deltaRegFallbackPower = 203 // Fallback Power - UINT32 [W]
// EVSE - The following Register tables are defined as repeating blocks for each single EVSE
// Read Input Registers (0x04)
deltaRegEvseState = 0 // EVSE State - UINT16 0: Unavailable, 1: Available, 2: Occupied, 3: Preparing, 4: Charging, 5: Finishing, 6: Suspended EV, 7: Suspended EVSE, 8: Not ready, 9: Faulted
deltaRegEvseChargerState = 1 // EVSE Charger State* - UINT16 0: Charging process not started (no vehicle connected), 1: Connected, waiting for release (by RFID or local), 2: Charging process starts, 3: Charging, 4: Suspended (paused), 5: Charging process successfully completed (vehicle still plugged in), 6: Charging process completed by user (vehicle still plugged in), 7: Charging ended with error (vehicle still connected)
deltaRegEvseActualOutputVoltage = 3 // EVSE Actual Output Voltage* - FLOAT32 [V]
deltaRegEvseActualChargingPower = 5 // EVSE Actual Charging Power - UINT32 [W]
deltaRegEvseActualChargingCurrent = 7 // EVSE Actual Charging Current* - FLOAT32 [A]
deltaRegEvseActualOutputPower = 9 // EVSE Actual Output Power* - FLOAT32 [W]
deltaRegEvseSoc = 11 // EVSE SOC* [%/10]
deltaRegEvseChargingTime = 17 // EVSE Charging Time* [s]
deltaRegEvseChargedEnergy = 19 // EVSE Charged Energy* [Wh]
deltaRegEvseRfidUID = 100 // EVSE Used Authentication ID - STRING
// Write Multiple Registers (0x10)
deltaRegEvseChargingPowerLimit = 600 // EVSE Charging Power Limit - UINT32 [W]
deltaRegEvseSuspendCharging = 602 // EVSE Suspend Charging - UINT16 0: no pause, 1 charging pause (lock on)
)
func init() {
registry.Add("delta", NewDeltaFromConfig)
}
// NewDeltaFromConfig creates a Delta charger from generic config
func NewDeltaFromConfig(other map[string]interface{}) (api.Charger, error) {
cc := struct {
Connector uint16
modbus.Settings `mapstructure:",squash"`
}{
Connector: 1,
Settings: modbus.Settings{
ID: 1,
},
}
if err := util.DecodeOther(other, &cc); err != nil {
return nil, err
}
return NewDelta(cc.URI, cc.Device, cc.Comset, cc.Baudrate, cc.Settings.Protocol(), cc.ID, cc.Connector)
}
// NewDelta creates Delta charger
func NewDelta(uri, device, comset string, baudrate int, proto modbus.Protocol, slaveID uint8, connector uint16) (api.Charger, error) {
conn, err := modbus.NewConnection(uri, device, comset, baudrate, proto, slaveID)
if err != nil {
return nil, err
}
if !sponsor.IsAuthorized() {
return nil, api.ErrSponsorRequired
}
log := util.NewLogger("delta")
conn.Logger(log.TRACE)
wb := &Delta{
log: log,
conn: conn,
curr: 6000, // assume min current
}
wb.base = connector * 1000
// check for basic or smart register set
if _, err := wb.conn.ReadInputRegisters(wb.base+deltaRegEvseChargerState, 1); err != nil {
wb.isBasic = true
}
b, err := wb.conn.ReadHoldingRegisters(deltaRegCommunicationTimeoutEnabled, 1)
if err != nil {
return nil, fmt.Errorf("failsafe timeout enabled: %w", err)
}
if encoding.Uint16(b) != 0 {
b, err := wb.conn.ReadHoldingRegisters(deltaRegCommunicationTimeout, 1)
if err != nil {
return nil, fmt.Errorf("failsafe timeout: %w", err)
}
if u := encoding.Uint16(b); u > 0 {
go wb.heartbeat(time.Duration(u) * time.Second / 2)
}
}
return wb, nil
}
func (wb *Delta) heartbeat(timeout time.Duration) {
for range time.Tick(timeout) {
wb.mu.Lock()
var curr float64
if wb.enabled {
curr = wb.curr
}
wb.mu.Unlock()
if err := wb.setCurrent(curr); err != nil {
wb.log.ERROR.Println("heartbeat:", err)
}
}
}
// Status implements the api.Charger interface
func (wb *Delta) Status() (api.ChargeStatus, error) {
b, err := wb.conn.ReadInputRegisters(wb.base+deltaRegEvseState, 1)
if err != nil {
return api.StatusNone, err
}
// 0: Unavailable
// 1: Available
// 2: Occupied
// 3: Preparing
// 4: Charging
// 5: Finishing
// 6: Suspended EV
// 7: Suspended EVSE
// 8: Not ready
// 9: Faulted
switch s := encoding.Uint16(b); s {
case 0, 1, 2:
return api.StatusA, nil
case 3:
if wb.isBasic {
return api.StatusA, nil
}
return api.StatusB, nil
case 5, 6, 7, 9:
return api.StatusB, nil
case 4:
return api.StatusC, nil
default:
return api.StatusNone, fmt.Errorf("invalid status: %0x", s)
}
}
var _ api.StatusReasoner = (*Delta)(nil)
// statusReason implements the api.StatusReasoner interface
func (wb *Delta) StatusReason() (api.Reason, error) {
if !wb.isBasic {
b, err := wb.conn.ReadInputRegisters(wb.base+deltaRegEvseState, 1)
if err != nil {
return api.ReasonUnknown, err
}
switch s := encoding.Uint16(b); s {
case 3:
return api.ReasonWaitingForAuthorization, nil
case 5:
return api.ReasonDisconnectRequired, nil
}
}
return api.ReasonUnknown, nil
}
// Enabled implements the api.Charger interface
func (wb *Delta) Enabled() (bool, error) {
b, err := wb.conn.ReadHoldingRegisters(wb.base+deltaRegEvseChargingPowerLimit, 2)
if err != nil {
return false, err
}
return encoding.Uint32(b) > 0, nil
}
// Enable implements the api.Charger interface
func (wb *Delta) Enable(enable bool) error {
var curr float64
if enable {
wb.mu.Lock()
curr = wb.curr
wb.mu.Unlock()
}
err := wb.setCurrent(curr)
if err == nil {
wb.mu.Lock()
wb.enabled = enable
wb.mu.Unlock()
}
return err
}
// setCurrent writes the current limit in A
func (wb *Delta) setCurrent(current float64) error {
activePhases := 3
if wb.lp != nil {
activePhases = wb.lp.ActivePhases()
}
b := make([]byte, 4)
encoding.PutUint32(b, uint32(math.Trunc(230.0*current*float64(activePhases))))
_, err := wb.conn.WriteMultipleRegisters(wb.base+deltaRegEvseChargingPowerLimit, 2, b)
return err
}
// MaxCurrent implements the api.Charger interface
func (wb *Delta) MaxCurrent(current int64) error {
return wb.MaxCurrentMillis(float64(current))
}
var _ api.ChargerEx = (*Delta)(nil)
// MaxCurrentMillis implements the api.ChargerEx interface
func (wb *Delta) MaxCurrentMillis(current float64) error {
if current < 6 {
return fmt.Errorf("invalid current %.1f", current)
}
err := wb.setCurrent(current)
if err == nil {
wb.mu.Lock()
wb.curr = current
wb.mu.Unlock()
}
return err
}
var _ api.Meter = (*Delta)(nil)
// CurrentPower implements the api.Meter interface
func (wb *Delta) CurrentPower() (float64, error) {
b, err := wb.conn.ReadInputRegisters(wb.base+deltaRegEvseActualChargingPower, 2)
if err != nil {
return 0, err
}
return float64(encoding.Uint32(b)), err
}
var _ api.Identifier = (*Delta)(nil)
// Identify implements the api.Identifier interface
func (wb *Delta) Identify() (string, error) {
b, err := wb.conn.ReadInputRegisters(wb.base+deltaRegEvseRfidUID, 20)
if err != nil {
return "", err
}
return bytesAsString(b), nil
}
var _ api.Diagnosis = (*Delta)(nil)
// Diagnose implements the api.Diagnosis interface
func (wb *Delta) Diagnose() {
if b, err := wb.conn.ReadInputRegisters(deltaRegState, 1); err == nil {
fmt.Printf("\tState:\t%d\n", encoding.Uint16(b))
}
if b, err := wb.conn.ReadInputRegisters(deltaRegCount, 1); err == nil {
fmt.Printf("\tEVSE Count:\t%d\n", encoding.Uint16(b))
}
if b, err := wb.conn.ReadInputRegisters(deltaRegSerial, 20); err == nil {
fmt.Printf("\tSerial:\t%s\n", bytesAsString(b))
}
if b, err := wb.conn.ReadInputRegisters(deltaRegModel, 20); err == nil {
fmt.Printf("\tModel:\t%s\n", bytesAsString(b))
}
if b, err := wb.conn.ReadHoldingRegisters(deltaRegCommunicationTimeoutEnabled, 1); err == nil {
fmt.Printf("\tCommunication Timeout Enabled:\t%d\n", encoding.Uint16(b))
}
if b, err := wb.conn.ReadHoldingRegisters(deltaRegCommunicationTimeout, 1); err == nil {
fmt.Printf("\tCommunication Timeout:\t%d\n", encoding.Uint16(b))
}
if b, err := wb.conn.ReadHoldingRegisters(deltaRegFallbackPower, 2); err == nil {
fmt.Printf("\tFallback Power:\t%d\n", encoding.Uint32(b))
}
}
var _ loadpoint.Controller = (*Delta)(nil)
// LoadpointControl implements loadpoint.Controller
func (wb *Delta) LoadpointControl(lp loadpoint.API) {
wb.lp = lp
}