From eff595c6e7b93125fa0fa77920ee7a8317859f23 Mon Sep 17 00:00:00 2001 From: andig Date: Sun, 25 Aug 2024 15:04:11 +0200 Subject: [PATCH] Ocpp: set availability on connect (#15368) --- charger/ocpp.go | 4 ++++ charger/ocpp/connector.go | 4 ---- charger/ocpp/cs_core.go | 14 ++++++++++++++ charger/ocpp_test.go | 8 ++------ charger/ocpp_test_handler.go | 10 ++-------- 5 files changed, 22 insertions(+), 18 deletions(-) diff --git a/charger/ocpp.go b/charger/ocpp.go index 460b66a418..2526824202 100644 --- a/charger/ocpp.go +++ b/charger/ocpp.go @@ -179,6 +179,10 @@ func NewOCPP(id string, connector int, idtag string, // fix timing issue in EVBox when switching OCPP protocol version time.Sleep(time.Second) + if err := ocpp.Instance().ChangeAvailabilityRequest(cp.ID(), 0, core.AvailabilityTypeOperative); err != nil { + return nil, err + } + meterValuesSampledData, meterValuesSampledDataMaxLength, err := c.getConfiguration(cp.ID(), connector) if err != nil { return nil, err diff --git a/charger/ocpp/connector.go b/charger/ocpp/connector.go index dbbc6a66f6..9e0439e42f 100644 --- a/charger/ocpp/connector.go +++ b/charger/ocpp/connector.go @@ -100,16 +100,12 @@ func (conn *Connector) WatchDog(timeout time.Duration) { // 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 } diff --git a/charger/ocpp/cs_core.go b/charger/ocpp/cs_core.go index 593640bc7b..b60eed03bb 100644 --- a/charger/ocpp/cs_core.go +++ b/charger/ocpp/cs_core.go @@ -38,6 +38,20 @@ func (cs *CS) TriggerMessageRequest(id string, requestedMessage remotetrigger.Me return Wait(err, rc, cs.timeout) } +func (cs *CS) ChangeAvailabilityRequest(id string, connector int, availabilityType core.AvailabilityType) error { + rc := make(chan error, 1) + + err := cs.ChangeAvailability(id, func(request *core.ChangeAvailabilityConfirmation, err error) { + if err == nil && request != nil && request.Status != core.AvailabilityStatusAccepted { + err = errors.New(string(request.Status)) + } + + rc <- err + }, connector, availabilityType) + + return Wait(err, rc, cs.timeout) +} + // cp actions func (cs *CS) OnAuthorize(id string, request *core.AuthorizeRequest) (*core.AuthorizeConfirmation, error) { diff --git a/charger/ocpp_test.go b/charger/ocpp_test.go index 2f557e42fe..2903b839f8 100644 --- a/charger/ocpp_test.go +++ b/charger/ocpp_test.go @@ -59,12 +59,8 @@ func (suite *ocppTestSuite) startChargePoint(id string, connectorId int) ocpp16. func (suite *ocppTestSuite) handleTrigger(cp ocpp16.ChargePoint, connectorId int, msg remotetrigger.MessageTrigger) { switch msg { - case core.BootNotificationFeatureName: - if res, err := cp.BootNotification("demo", "evcc"); err != nil { - suite.T().Log("BootNotification:", err) - } else { - suite.T().Log("BootNotification:", res) - } + case core.ChangeAvailabilityFeatureName: + fallthrough case core.StatusNotificationFeatureName: if res, err := cp.StatusNotification(connectorId, core.NoError, core.ChargePointStatusCharging); err != nil { diff --git a/charger/ocpp_test_handler.go b/charger/ocpp_test_handler.go index 02a3e91f3c..082dd3168f 100644 --- a/charger/ocpp_test_handler.go +++ b/charger/ocpp_test_handler.go @@ -16,6 +16,7 @@ type ChargePointHandler struct { // core func (handler *ChargePointHandler) OnChangeAvailability(request *core.ChangeAvailabilityRequest) (confirmation *core.ChangeAvailabilityConfirmation, err error) { + defer func() { handler.triggerC <- core.ChangeAvailabilityFeatureName }() fmt.Printf("%T %+v\n", request, request) return core.NewChangeAvailabilityConfirmation(core.AvailabilityStatusAccepted), nil } @@ -71,15 +72,8 @@ func (handler *ChargePointHandler) OnUnlockConnector(request *core.UnlockConnect } func (handler *ChargePointHandler) OnTriggerMessage(request *remotetrigger.TriggerMessageRequest) (confirmation *remotetrigger.TriggerMessageConfirmation, err error) { + defer func() { handler.triggerC <- request.RequestedMessage }() fmt.Printf("%T %+v\n", request, request) - - if c := handler.triggerC; request != nil && c != nil { - select { - case c <- request.RequestedMessage: - default: - } - } - return remotetrigger.NewTriggerMessageConfirmation(remotetrigger.TriggerMessageStatusAccepted), nil }