From 4273966f5f1feb40ac1bf26acbaa28b3766bca6b Mon Sep 17 00:00:00 2001 From: andig Date: Sun, 7 Jul 2024 09:33:59 +0200 Subject: [PATCH] Ocpp: add autostart mode (#14733) --- charger/ocpp.go | 74 ++++++++++++++++++-------- charger/ocpp/connector.go | 7 +++ charger/ocpp/connector_core.go | 1 + charger/ocpp_test.go | 4 +- templates/definition/charger/ocpp.yaml | 4 ++ 5 files changed, 67 insertions(+), 23 deletions(-) diff --git a/charger/ocpp.go b/charger/ocpp.go index b98c6758f..fd0b8d7ae 100644 --- a/charger/ocpp.go +++ b/charger/ocpp.go @@ -30,6 +30,7 @@ type OCPP struct { meterValuesSample string timeout time.Duration phaseSwitching bool + autoStart bool chargingRateUnit types.ChargingRateUnitType lp loadpoint.API } @@ -53,6 +54,7 @@ func NewOCPPFromConfig(other map[string]interface{}) (api.Charger, error) { BootNotification *bool GetConfiguration *bool ChargingRateUnit string + AutoStart bool }{ Connector: 1, IdTag: defaultIdTag, @@ -70,7 +72,7 @@ func NewOCPPFromConfig(other map[string]interface{}) (api.Charger, error) { c, err := NewOCPP(cc.StationId, cc.Connector, cc.IdTag, cc.MeterValues, cc.MeterInterval, - boot, noConfig, + boot, noConfig, cc.AutoStart, cc.ConnectTimeout, cc.Timeout, cc.ChargingRateUnit) if err != nil { return c, err @@ -104,7 +106,7 @@ func NewOCPPFromConfig(other map[string]interface{}) (api.Charger, error) { // NewOCPP creates OCPP charger func NewOCPP(id string, connector int, idtag string, meterValues string, meterInterval time.Duration, - boot, noConfig bool, + boot, noConfig, autoStart bool, connectTimeout, timeout time.Duration, chargingRateUnit string, ) (*OCPP, error) { @@ -132,10 +134,11 @@ func NewOCPP(id string, connector int, idtag string, } c := &OCPP{ - log: log, - conn: conn, - idtag: idtag, - timeout: timeout, + log: log, + conn: conn, + idtag: idtag, + autoStart: autoStart, + timeout: timeout, } c.log.DEBUG.Printf("waiting for chargepoint: %v", connectTimeout) @@ -287,6 +290,13 @@ func (c *OCPP) hasMeasurement(val types.Measurand) bool { return slices.Contains(strings.Split(c.meterValuesSample, ","), string(val)) } +func (c *OCPP) effectiveIdTag() string { + if idtag := c.conn.IdTag(); idtag != "" { + return idtag + } + return c.idtag +} + // configure updates CP configuration func (c *OCPP) configure(key, val string) error { rc := make(chan error, 1) @@ -325,17 +335,40 @@ func (c *OCPP) Enabled() (bool, error) { return c.enabled, nil } -// Enable implements the api.Charger interface -func (c *OCPP) Enable(enable bool) (err error) { - rc := make(chan error, 1) +func (c *OCPP) Enable(enable bool) error { + var err error + + if c.autoStart { + err = c.enableAutostart(enable) + } else { + err = c.enableRemote(enable) + } + + if err == nil { + c.enabled = enable + } + + return err +} + +// enableAutostart enables auto-started session +func (c *OCPP) enableAutostart(enable bool) error { + var current float64 + if enable { + current = c.current + } + + return c.updatePeriod(current) +} + +// enableRemote enables session by using RemoteStart/Stop +func (c *OCPP) enableRemote(enable bool) error { txn, err := c.conn.TransactionID() + if err != nil { + return err + } - defer func() { - if err == nil { - c.enabled = enable - } - }() - + rc := make(chan error, 1) if enable { if txn > 0 { // we have the transaction id, treat as enabled @@ -348,7 +381,7 @@ func (c *OCPP) Enable(enable bool) (err error) { } rc <- err - }, c.idtag, func(request *core.RemoteStartTransactionRequest) { + }, c.effectiveIdTag(), func(request *core.RemoteStartTransactionRequest) { connector := c.conn.ID() request.ConnectorId = &connector request.ChargingProfile = c.getTxChargingProfile(c.current, 0) @@ -490,11 +523,10 @@ func (c *OCPP) phases1p3p(phases int) error { return c.updatePeriod(c.current) } -// // Identify implements the api.Identifier interface -// Unless charger uses vehicle ID as idTag in authorize.req it is not possible to implement this in ocpp1.6 -// func (c *OCPP) Identify() (string, error) { -// return "", errors.New("not implemented") -// } +// Identify implements the api.Identifier interface +func (c *OCPP) Identify() (string, error) { + return c.conn.IdTag(), nil +} var _ loadpoint.Controller = (*OCPP)(nil) diff --git a/charger/ocpp/connector.go b/charger/ocpp/connector.go index 9ffbd1948..463ef63ab 100644 --- a/charger/ocpp/connector.go +++ b/charger/ocpp/connector.go @@ -31,6 +31,7 @@ type Connector struct { txnCount int // change initial value to the last known global transaction. Needs persistence txnId int + idTag string } func NewConnector(log *util.Logger, id int, cp *CP, timeout time.Duration) (*Connector, error) { @@ -61,6 +62,12 @@ func (conn *Connector) ID() int { return conn.id } +func (conn *Connector) IdTag() string { + conn.mu.Lock() + defer conn.mu.Unlock() + return conn.idTag +} + func (conn *Connector) TriggerMessageRequest(feature remotetrigger.MessageTrigger, f ...func(request *remotetrigger.TriggerMessageRequest)) { Instance().TriggerMessageRequest(conn.cp.ID(), feature, func(request *remotetrigger.TriggerMessageRequest) { request.ConnectorId = &conn.id diff --git a/charger/ocpp/connector_core.go b/charger/ocpp/connector_core.go index d288d87a3..3f3e47858 100644 --- a/charger/ocpp/connector_core.go +++ b/charger/ocpp/connector_core.go @@ -88,6 +88,7 @@ func (conn *Connector) StartTransaction(request *core.StartTransactionRequest) ( conn.txnCount++ conn.txnId = conn.txnCount + conn.idTag = request.IdTag res := &core.StartTransactionConfirmation{ IdTagInfo: &types.IdTagInfo{ diff --git a/charger/ocpp_test.go b/charger/ocpp_test.go index 8f70c18a2..7bb548a32 100644 --- a/charger/ocpp_test.go +++ b/charger/ocpp_test.go @@ -99,7 +99,7 @@ func (suite *ocppTestSuite) TestConnect() { suite.Require().True(cp1.IsConnected()) // 1st charge point- local - c1, err := NewOCPP("test-1", 1, "", "", 0, false, false, ocppTestConnectTimeout, ocppTestTimeout, "A") + c1, err := NewOCPP("test-1", 1, "", "", 0, false, false, false, ocppTestConnectTimeout, ocppTestTimeout, "A") suite.Require().NoError(err) // status and meter values @@ -158,7 +158,7 @@ func (suite *ocppTestSuite) TestConnect() { suite.Require().True(cp2.IsConnected()) // 2nd charge point - local - c2, err := NewOCPP("test-2", 1, "", "", 0, false, false, ocppTestConnectTimeout, ocppTestTimeout, "A") + c2, err := NewOCPP("test-2", 1, "", "", 0, false, false, false, ocppTestConnectTimeout, ocppTestTimeout, "A") suite.Require().NoError(err) { diff --git a/templates/definition/charger/ocpp.yaml b/templates/definition/charger/ocpp.yaml index cc677cb45..e615ef7d1 100644 --- a/templates/definition/charger/ocpp.yaml +++ b/templates/definition/charger/ocpp.yaml @@ -38,6 +38,9 @@ requirements: * Local network connection params: - preset: ocpp + - name: autostart + advanced: true + type: bool - name: getconfiguration advanced: true type: bool @@ -78,6 +81,7 @@ params: en: Unit for setting ChargingProfile values ("W" or "A") render: | {{ include "ocpp" . }} + autostart: {{ .autostart }} {{- if ne .getconfiguration "true" }} getconfiguration: {{ .getconfiguration }} {{- end }}