Skip to content

Commit

Permalink
Ocpp: set availability on connect (#15368)
Browse files Browse the repository at this point in the history
  • Loading branch information
andig authored Aug 25, 2024
1 parent b49f783 commit eff595c
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 18 deletions.
4 changes: 4 additions & 0 deletions charger/ocpp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 0 additions & 4 deletions charger/ocpp/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
14 changes: 14 additions & 0 deletions charger/ocpp/cs_core.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
8 changes: 2 additions & 6 deletions charger/ocpp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
10 changes: 2 additions & 8 deletions charger/ocpp_test_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit eff595c

Please sign in to comment.