From 14d2ba9bb1e93575b8c1d855dfa6e97dc9695bb2 Mon Sep 17 00:00:00 2001 From: andig Date: Mon, 13 Mar 2023 17:42:30 +0100 Subject: [PATCH] Ocpp: match connector id (#6826) --- charger/ocpp.go | 2 +- charger/ocpp/cp.go | 9 +++++++-- charger/ocpp/cp_core.go | 44 ++++++++++++++++++++++++----------------- 3 files changed, 34 insertions(+), 21 deletions(-) diff --git a/charger/ocpp.go b/charger/ocpp.go index 9b35a3432..a2877ed81 100644 --- a/charger/ocpp.go +++ b/charger/ocpp.go @@ -113,7 +113,7 @@ func NewOCPP(id string, connector int, idtag string, meterValues string, meterIn } log := util.NewLogger(unit) - cp := ocpp.NewChargePoint(log, id, timeout) + cp := ocpp.NewChargePoint(log, id, connector, timeout) if err := ocpp.Instance().Register(id, cp); err != nil { return nil, err } diff --git a/charger/ocpp/cp.go b/charger/ocpp/cp.go index b9e3b3ac9..119217bc4 100644 --- a/charger/ocpp/cp.go +++ b/charger/ocpp/cp.go @@ -32,12 +32,16 @@ const ( KeyAlfenPlugAndChargeIdentifier = "PlugAndChargeIdentifier" ) +// TODO support multiple connectors +// Since ocpp-go interfaces at charge point level, we need to manage multiple connector separately + type CP struct { mu sync.Mutex log *util.Logger once sync.Once - id string + id string + connector int connectC, statusC chan struct{} updated time.Time @@ -51,10 +55,11 @@ type CP struct { txnId int } -func NewChargePoint(log *util.Logger, id string, timeout time.Duration) *CP { +func NewChargePoint(log *util.Logger, id string, connector int, timeout time.Duration) *CP { return &CP{ log: log, id: id, + connector: connector, connectC: make(chan struct{}), statusC: make(chan struct{}), measurements: make(map[string]types.SampledValue), diff --git a/charger/ocpp/cp_core.go b/charger/ocpp/cp_core.go index 16285402f..4cace1a94 100644 --- a/charger/ocpp/cp_core.go +++ b/charger/ocpp/cp_core.go @@ -51,7 +51,7 @@ func (cp *CP) timestampValid(t time.Time) bool { } func (cp *CP) StatusNotification(request *core.StatusNotificationRequest) (*core.StatusNotificationConfirmation, error) { - if request != nil { + if request != nil && request.ConnectorId == cp.connector { cp.mu.Lock() defer cp.mu.Unlock() @@ -92,15 +92,17 @@ func (cp *CP) Heartbeat(request *core.HeartbeatRequest) (*core.HeartbeatConfirma } func (cp *CP) MeterValues(request *core.MeterValuesRequest) (*core.MeterValuesConfirmation, error) { - cp.mu.Lock() - defer cp.mu.Unlock() + if request != nil && request.ConnectorId == cp.connector { + cp.mu.Lock() + defer cp.mu.Unlock() - for _, meterValue := range request.MeterValue { - // ignore old meter value requests - if meterValue.Timestamp.Time.After(cp.meterUpdated) { - for _, sample := range meterValue.SampledValue { - cp.measurements[getSampleKey(sample)] = sample - cp.meterUpdated = time.Now() + for _, meterValue := range request.MeterValue { + // ignore old meter value requests + if meterValue.Timestamp.Time.After(cp.meterUpdated) { + for _, sample := range meterValue.SampledValue { + cp.measurements[getSampleKey(sample)] = sample + cp.meterUpdated = time.Now() + } } } } @@ -117,6 +119,10 @@ func getSampleKey(s types.SampledValue) string { } func (cp *CP) StartTransaction(request *core.StartTransactionRequest) (*core.StartTransactionConfirmation, error) { + if request == nil || request.ConnectorId != cp.connector { + return new(core.StartTransactionConfirmation), nil + } + cp.mu.Lock() defer cp.mu.Unlock() @@ -139,17 +145,19 @@ func (cp *CP) StartTransaction(request *core.StartTransactionRequest) (*core.Sta } func (cp *CP) StopTransaction(request *core.StopTransactionRequest) (*core.StopTransactionConfirmation, error) { - cp.mu.Lock() - defer cp.mu.Unlock() + if request != nil { + cp.mu.Lock() + defer cp.mu.Unlock() - // reset transaction - if request != nil && time.Since(request.Timestamp.Time) < transactionExpiry { // only respect transactions in the last hour - // log mismatching id but close transaction anyway - if request.TransactionId != cp.txnId { - cp.log.ERROR.Printf("stop transaction: invalid id %d", request.TransactionId) + // reset transaction + if time.Since(request.Timestamp.Time) < transactionExpiry { // only respect transactions in the last hour + // log mismatching id but close transaction anyway + if request.TransactionId != cp.txnId { + cp.log.ERROR.Printf("stop transaction: invalid id %d", request.TransactionId) + } + + cp.txnId = 0 } - - cp.txnId = 0 } res := &core.StopTransactionConfirmation{