forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
connector.go
260 lines (207 loc) · 6.05 KB
/
connector.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
package ocpp
import (
"fmt"
"strconv"
"strings"
"sync"
"time"
"github.com/benbjohnson/clock"
"github.com/connectorjs/evcm/api"
"github.com/connectorjs/evcm/util"
"github.com/lorenzodonini/ocpp-go/ocpp1.6/core"
"github.com/lorenzodonini/ocpp-go/ocpp1.6/remotetrigger"
"github.com/lorenzodonini/ocpp-go/ocpp1.6/types"
)
type Connector struct {
log *util.Logger
mu sync.Mutex
clock clock.Clock // mockable time
cp *CP
id int
status *core.StatusNotificationRequest
statusC chan struct{}
meterUpdated time.Time
measurements map[string]types.SampledValue
timeout time.Duration
txnCount int // change initial value to the last known global transaction. Needs persistence
txnId int
}
func NewConnector(log *util.Logger, id int, cp *CP, timeout time.Duration) (*Connector, error) {
conn := &Connector{
log: log,
cp: cp,
id: id,
clock: clock.New(),
statusC: make(chan struct{}),
measurements: make(map[string]types.SampledValue),
timeout: timeout,
}
err := cp.registerConnector(id, conn)
return conn, err
}
func (conn *Connector) TestClock(clock clock.Clock) {
conn.clock = clock
}
func (conn *Connector) ChargePoint() *CP {
return conn.cp
}
func (conn *Connector) ID() int {
return conn.id
}
func (conn *Connector) TriggerMessageRequest(feature remotetrigger.MessageTrigger, f ...func(request *remotetrigger.TriggerMessageRequest)) {
Instance().TriggerMessageRequest(conn.cp.ID(), feature, func(request *remotetrigger.TriggerMessageRequest) {
request.ConnectorId = &conn.id
for _, f := range f {
f(request)
}
})
}
// WatchDog triggers meter values messages if older than timeout.
// Must be wrapped in a goroutine.
func (conn *Connector) WatchDog(timeout time.Duration) {
for ; true; <-time.Tick(timeout) {
conn.mu.Lock()
update := conn.txnId != 0 && conn.clock.Since(conn.meterUpdated) > timeout
conn.mu.Unlock()
if update {
conn.TriggerMessageRequest(core.MeterValuesFeatureName)
}
}
}
// Initialized waits for initial charge point status notification
func (conn *Connector) Initialized() error {
trigger := time.After(conn.timeout / 2)
timeout := time.After(conn.timeout)
for {
select {
case <-conn.statusC:
return nil
case <-trigger:
conn.TriggerMessageRequest(core.StatusNotificationFeatureName)
case <-timeout:
return api.ErrTimeout
}
}
}
// TransactionID returns the current transaction id
func (conn *Connector) TransactionID() (int, error) {
if !conn.cp.Connected() {
return 0, api.ErrTimeout
}
conn.mu.Lock()
defer conn.mu.Unlock()
return conn.txnId, nil
}
func (conn *Connector) Status() (api.ChargeStatus, error) {
conn.mu.Lock()
defer conn.mu.Unlock()
res := api.StatusNone
if !conn.cp.Connected() {
return res, api.ErrTimeout
}
if conn.status.ErrorCode != core.NoError {
return res, fmt.Errorf("%s: %s", conn.status.ErrorCode, conn.status.Info)
}
switch conn.status.Status {
case core.ChargePointStatusAvailable, // "Available"
core.ChargePointStatusUnavailable: // "Unavailable"
res = api.StatusA
case
core.ChargePointStatusPreparing, // "Preparing"
core.ChargePointStatusSuspendedEVSE, // "SuspendedEVSE"
core.ChargePointStatusSuspendedEV, // "SuspendedEV"
core.ChargePointStatusFinishing: // "Finishing"
res = api.StatusB
case core.ChargePointStatusCharging: // "Charging"
res = api.StatusC
case core.ChargePointStatusReserved, // "Reserved"
core.ChargePointStatusFaulted: // "Faulted"
return api.StatusF, fmt.Errorf("chargepoint status: %s", conn.status.ErrorCode)
default:
return api.StatusNone, fmt.Errorf("invalid chargepoint status: %s", conn.status.Status)
}
return res, nil
}
// isMeterTimeout checks if meter values are outdated.
// Must only be called while holding lock.
func (conn *Connector) isMeterTimeout() bool {
return conn.timeout > 0 && conn.clock.Since(conn.meterUpdated) > conn.timeout
}
var _ api.Meter = (*Connector)(nil)
func (conn *Connector) CurrentPower() (float64, error) {
if !conn.cp.Connected() {
return 0, api.ErrTimeout
}
conn.mu.Lock()
defer conn.mu.Unlock()
// zero value on timeout when not charging
if conn.isMeterTimeout() {
if conn.txnId != 0 {
return 0, api.ErrTimeout
}
return 0, nil
}
if m, ok := conn.measurements[string(types.MeasurandPowerActiveImport)]; ok {
f, err := strconv.ParseFloat(m.Value, 64)
return scale(f, m.Unit), err
}
return 0, api.ErrNotAvailable
}
var _ api.MeterEnergy = (*Connector)(nil)
func (conn *Connector) TotalEnergy() (float64, error) {
if !conn.cp.Connected() {
return 0, api.ErrTimeout
}
conn.mu.Lock()
defer conn.mu.Unlock()
// fallthrough for last value on timeout when not charging
if conn.txnId != 0 && conn.isMeterTimeout() {
return 0, api.ErrTimeout
}
if m, ok := conn.measurements[string(types.MeasurandEnergyActiveImportRegister)]; ok {
f, err := strconv.ParseFloat(m.Value, 64)
return scale(f, m.Unit) / 1e3, err
}
return 0, api.ErrNotAvailable
}
func scale(f float64, scale types.UnitOfMeasure) float64 {
switch {
case strings.HasPrefix(string(scale), "k"):
return f * 1e3
case strings.HasPrefix(string(scale), "m"):
return f / 1e3
default:
return f
}
}
func getKeyCurrentPhase(phase int) string {
return string(types.MeasurandCurrentImport) + "@L" + strconv.Itoa(phase)
}
var _ api.PhaseCurrents = (*Connector)(nil)
func (conn *Connector) Currents() (float64, float64, float64, error) {
if !conn.cp.Connected() {
return 0, 0, 0, api.ErrTimeout
}
conn.mu.Lock()
defer conn.mu.Unlock()
// zero value on timeout when not charging
if conn.isMeterTimeout() {
if conn.txnId != 0 {
return 0, 0, 0, api.ErrTimeout
}
return 0, 0, 0, nil
}
currents := make([]float64, 0, 3)
for phase := 1; phase <= 3; phase++ {
m, ok := conn.measurements[getKeyCurrentPhase(phase)]
if !ok {
return 0, 0, 0, api.ErrNotAvailable
}
f, err := strconv.ParseFloat(m.Value, 64)
if err != nil {
return 0, 0, 0, fmt.Errorf("invalid current for phase %d: %w", phase, err)
}
currents = append(currents, scale(f, m.Unit))
}
return currents[0], currents[1], currents[2], nil
}