-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathconnector_core.go
More file actions
161 lines (132 loc) Β· 4.34 KB
/
connector_core.go
File metadata and controls
161 lines (132 loc) Β· 4.34 KB
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
package ocpp
import (
"strings"
"time"
"github.com/lorenzodonini/ocpp-go/ocpp1.6/core"
"github.com/lorenzodonini/ocpp-go/ocpp1.6/types"
)
// timestampValid returns false if status timestamps are outdated
func (conn *Connector) timestampValid(t time.Time) bool {
// reject if expired
if conn.clock.Since(t) > Timeout {
return false
}
// assume having a timestamp is better than not
if conn.status.Timestamp == nil {
return true
}
// reject older values than we already have
return !t.Before(conn.status.Timestamp.Time)
}
func (conn *Connector) OnStatusNotification(request *core.StatusNotificationRequest) (*core.StatusNotificationConfirmation, error) {
conn.mu.Lock()
defer conn.mu.Unlock()
if conn.status == nil {
conn.status = request
close(conn.statusC) // signal initial status received
} else if request.Timestamp == nil || conn.timestampValid(request.Timestamp.Time) {
conn.status = request
} else {
conn.log.TRACE.Printf("ignoring status: %s < %s", request.Timestamp.Time, conn.status.Timestamp)
}
if conn.isWaitingForAuth() {
if idTag := conn.remoteIdTag; idTag != "" {
go func() {
if err := conn.RemoteStartTransactionRequest(idTag); err != nil {
conn.log.ERROR.Printf("RemoteStartTransaction: %v", err)
}
}()
} else {
conn.log.DEBUG.Printf("waiting for local authentication")
}
}
return new(core.StatusNotificationConfirmation), nil
}
func getSampleKey(s types.SampledValue) types.Measurand {
if s.Phase != "" {
return s.Measurand + types.Measurand("."+string(s.Phase))
}
return s.Measurand
}
func (conn *Connector) OnMeterValues(request *core.MeterValuesRequest) (*core.MeterValuesConfirmation, error) {
conn.mu.Lock()
defer conn.mu.Unlock()
if request.TransactionId != nil && *request.TransactionId > 0 &&
conn.txnId == 0 && conn.status != nil &&
(conn.status.Status == core.ChargePointStatusCharging ||
conn.status.Status == core.ChargePointStatusSuspendedEV ||
conn.status.Status == core.ChargePointStatusSuspendedEVSE) {
conn.log.DEBUG.Printf("recovered transaction: %d", *request.TransactionId)
conn.txnId = *request.TransactionId
}
for _, meterValue := range sortByAge(request.MeterValue) {
if meterValue.Timestamp == nil {
// this should be done before the sorting, but lets assume either all or no sample has a timestamp
meterValue.Timestamp = types.NewDateTime(conn.clock.Now())
}
// ignore old meter value requests
if !meterValue.Timestamp.Time.Before(conn.meterUpdated) {
for _, sample := range meterValue.SampledValue {
sample.Value = strings.TrimSpace(sample.Value)
conn.measurements[getSampleKey(sample)] = sample
conn.meterUpdated = meterValue.Timestamp.Time
}
}
}
return new(core.MeterValuesConfirmation), nil
}
func (conn *Connector) OnStartTransaction(request *core.StartTransactionRequest) (*core.StartTransactionConfirmation, error) {
conn.mu.Lock()
defer conn.mu.Unlock()
conn.txnId = int(instance.txnId.Add(1))
conn.idTag = request.IdTag
res := &core.StartTransactionConfirmation{
IdTagInfo: &types.IdTagInfo{
Status: types.AuthorizationStatusAccepted,
},
TransactionId: conn.txnId,
}
return res, nil
}
func (conn *Connector) assumeMeterStopped() {
conn.meterUpdated = conn.clock.Now()
if _, ok := conn.measurements[types.MeasurandPowerActiveImport]; ok {
conn.measurements[types.MeasurandPowerActiveImport] = types.SampledValue{
Value: "0",
Unit: types.UnitOfMeasureW,
}
}
for phase := 1; phase <= 3; phase++ {
// phase powers
for _, suffix := range []types.Measurand{"", "-N"} {
key := getPhaseKey(types.MeasurandPowerActiveImport, phase) + suffix
if _, ok := conn.measurements[key]; ok {
conn.measurements[key] = types.SampledValue{
Value: "0",
Unit: types.UnitOfMeasureW,
}
}
}
// phase currents
key := getPhaseKey(types.MeasurandCurrentImport, phase)
if _, ok := conn.measurements[key]; ok {
conn.measurements[key] = types.SampledValue{
Value: "0",
Unit: types.UnitOfMeasureA,
}
}
}
}
func (conn *Connector) OnStopTransaction(request *core.StopTransactionRequest) (*core.StopTransactionConfirmation, error) {
conn.mu.Lock()
defer conn.mu.Unlock()
conn.txnId = 0
conn.idTag = ""
res := &core.StopTransactionConfirmation{
IdTagInfo: &types.IdTagInfo{
Status: types.AuthorizationStatusAccepted, // accept
},
}
conn.assumeMeterStopped()
return res, nil
}