From b49f783334d01fc4c06e0823bf5a9406d3cbb39a Mon Sep 17 00:00:00 2001 From: andig Date: Sun, 25 Aug 2024 12:59:31 +0200 Subject: [PATCH] Ocpp: fix connection handling (#15669) --- charger/ocpp/cp.go | 26 ++++++++++++++------------ charger/ocpp/cp_core.go | 7 +++---- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/charger/ocpp/cp.go b/charger/ocpp/cp.go index 75d005412e..05621d58bf 100644 --- a/charger/ocpp/cp.go +++ b/charger/ocpp/cp.go @@ -12,14 +12,17 @@ import ( // Since ocpp-go interfaces at charge point level, we need to manage multiple connector separately type CP struct { - mu sync.RWMutex - log *util.Logger + mu sync.RWMutex + log *util.Logger + onceConnect sync.Once + onceBoot sync.Once + + bootNotificationRequestC chan *core.BootNotificationRequest id string - connected bool - connectC chan struct{} - bootNotificationRequestC chan *core.BootNotificationRequest + connected bool + connectC chan struct{} connectors map[int]*Connector } @@ -29,10 +32,10 @@ func NewChargePoint(log *util.Logger, id string) *CP { log: log, id: id, - connectC: make(chan struct{}, 1), - bootNotificationRequestC: make(chan *core.BootNotificationRequest, 1), - + connectC: make(chan struct{}), connectors: make(map[int]*Connector), + + bootNotificationRequestC: make(chan *core.BootNotificationRequest, 1), } } @@ -97,10 +100,9 @@ func (cp *CP) connect(connect bool) { cp.connected = connect if connect { - select { - case cp.connectC <- struct{}{}: - default: - } + cp.onceConnect.Do(func() { + close(cp.connectC) + }) } } diff --git a/charger/ocpp/cp_core.go b/charger/ocpp/cp_core.go index 2e8bf21e49..30be766550 100644 --- a/charger/ocpp/cp_core.go +++ b/charger/ocpp/cp_core.go @@ -38,10 +38,9 @@ func (cp *CP) BootNotification(request *core.BootNotificationRequest) (*core.Boo Status: core.RegistrationStatusAccepted, } - select { - case cp.bootNotificationRequestC <- request: - default: - } + cp.onceBoot.Do(func() { + cp.bootNotificationRequestC <- request + }) return res, nil }