Skip to content

Commit

Permalink
Ocpp: split connection and runtime timeouts (evcc-io#6898)
Browse files Browse the repository at this point in the history
  • Loading branch information
andig authored Mar 17, 2023
1 parent 25ad1cf commit ec8c763
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 16 deletions.
23 changes: 16 additions & 7 deletions charger/ocpp.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,18 @@ func NewOCPPFromConfig(other map[string]interface{}) (api.Charger, error) {
Connector int
MeterInterval time.Duration
MeterValues string
ConnectTimeout time.Duration
Timeout time.Duration
BootNotification *bool
GetConfiguration *bool
Meter interface{} // TODO deprecated
Quirks interface{} // TODO deprecated
InitialReset interface{} // TODO deprecated
}{
Connector: 1,
IdTag: defaultIdTag,
Timeout: time.Minute,
Connector: 1,
IdTag: defaultIdTag,
ConnectTimeout: 2 * time.Minute,
Timeout: 2 * time.Minute,
}

if err := util.DecodeOther(other, &cc); err != nil {
Expand All @@ -75,7 +77,10 @@ func NewOCPPFromConfig(other map[string]interface{}) (api.Charger, error) {
boot := cc.BootNotification != nil && *cc.BootNotification
noConfig := cc.GetConfiguration != nil && !*cc.GetConfiguration

c, err := NewOCPP(cc.StationId, cc.Connector, cc.IdTag, cc.MeterValues, cc.MeterInterval, boot, noConfig, cc.Timeout)
c, err := NewOCPP(cc.StationId, cc.Connector, cc.IdTag,
cc.MeterValues, cc.MeterInterval,
boot, noConfig,
cc.ConnectTimeout, cc.Timeout)
if err != nil {
return c, err
}
Expand Down Expand Up @@ -106,7 +111,11 @@ func NewOCPPFromConfig(other map[string]interface{}) (api.Charger, error) {
// go:generate go run ../cmd/tools/decorate.go -f decorateOCPP -b *OCPP -r api.Charger -t "api.Meter,CurrentPower,func() (float64, error)" -t "api.MeterEnergy,TotalEnergy,func() (float64, error)" -t "api.PhaseCurrents,Currents,func() (float64, float64, float64, error)" -t "api.PhaseSwitcher,Phases1p3p,func(int) (error)"

// NewOCPP creates OCPP charger
func NewOCPP(id string, connector int, idtag string, meterValues string, meterInterval time.Duration, boot, noConfig bool, timeout time.Duration) (*OCPP, error) {
func NewOCPP(id string, connector int, idtag string,
meterValues string, meterInterval time.Duration,
boot, noConfig bool,
connectTimeout, timeout time.Duration,
) (*OCPP, error) {
unit := "ocpp"
if id != "" {
unit = id
Expand All @@ -126,10 +135,10 @@ func NewOCPP(id string, connector int, idtag string, meterValues string, meterIn
timeout: timeout,
}

c.log.DEBUG.Printf("waiting for chargepoint: %v", timeout)
c.log.DEBUG.Printf("waiting for chargepoint: %v", connectTimeout)

select {
case <-time.After(timeout):
case <-time.After(connectTimeout):
return nil, api.ErrTimeout
case <-cp.HasConnected():
}
Expand Down
11 changes: 6 additions & 5 deletions charger/ocpp/cp.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,12 @@ type CP struct {
connector int

connectC, statusC chan struct{}
updated time.Time
status *core.StatusNotificationRequest

timeout time.Duration
updated time.Time
meterUpdated time.Time
timeout time.Duration

measurements map[string]types.SampledValue

txnCount int // change initial value to the last known global transaction. Needs persistence
Expand Down Expand Up @@ -184,7 +185,7 @@ func (cp *CP) CurrentPower() (float64, error) {
cp.mu.Lock()
defer cp.mu.Unlock()

if cp.timeout > 0 && time.Since(cp.meterUpdated) > cp.timeout {
if cp.txnId != 0 && cp.timeout > 0 && time.Since(cp.meterUpdated) > cp.timeout {
return 0, api.ErrNotAvailable
}

Expand All @@ -202,7 +203,7 @@ func (cp *CP) TotalEnergy() (float64, error) {
cp.mu.Lock()
defer cp.mu.Unlock()

if cp.timeout > 0 && time.Since(cp.meterUpdated) > cp.timeout {
if cp.txnId != 0 && cp.timeout > 0 && time.Since(cp.meterUpdated) > cp.timeout {
return 0, api.ErrNotAvailable
}

Expand Down Expand Up @@ -235,7 +236,7 @@ func (cp *CP) Currents() (float64, float64, float64, error) {
cp.mu.Lock()
defer cp.mu.Unlock()

if cp.timeout > 0 && time.Since(cp.meterUpdated) > cp.timeout {
if cp.txnId != 0 && cp.timeout > 0 && time.Since(cp.meterUpdated) > cp.timeout {
return 0, 0, 0, api.ErrNotAvailable
}

Expand Down
3 changes: 2 additions & 1 deletion templates/docs/charger/elvi_0.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ render:
stationid: EVB-P12354 # Die Stations-ID der Wallbox (oder des Ladepunkts) # Optional
connector: 1 # Ladepunkt, normalerweise 1 für den ersten Anschluss. # Optional
idtag: '04E6B78921BBA0' # Token-ID welche für die Freischaltung der Ladevorgänge an den Ladepunkt zurückgesendet wird # Optional
timeout: 10m # Optional
connecttimeout: 5m # Optional
timeout: 2m # Optional
3 changes: 2 additions & 1 deletion templates/docs/charger/ocpp_0.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ render:
stationid: EVB-P12354 # Die Stations-ID der Wallbox (oder des Ladepunkts) # Optional
connector: 1 # Ladepunkt, normalerweise 1 für den ersten Anschluss. # Optional
idtag: '04E6B78921BBA0' # Token-ID welche für die Freischaltung der Ladevorgänge an den Ladepunkt zurückgesendet wird # Optional
timeout: 10m # Optional
connecttimeout: 5m # Optional
timeout: 2m # Optional
getconfiguration: true # Deaktivierung kann bei einigen Chargern hilfreich sein # Optional
bootnotification: false # Aktivierung kann bei einigen Chargern hilfreich sein # Optional
3 changes: 2 additions & 1 deletion templates/docs/charger/pulsarplus_0.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ render:
stationid: EVB-P12354 # Die Stations-ID der Wallbox (oder des Ladepunkts) # Optional
connector: 1 # Ladepunkt, normalerweise 1 für den ersten Anschluss. # Optional
idtag: '04E6B78921BBA0' # Token-ID welche für die Freischaltung der Ladevorgänge an den Ladepunkt zurückgesendet wird # Optional
timeout: 10m # Optional
connecttimeout: 5m # Optional
timeout: 2m # Optional
9 changes: 8 additions & 1 deletion util/templates/defaults.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -363,8 +363,15 @@ presets:
de: Token-ID welche für die Freischaltung der Ladevorgänge an den Ladepunkt zurückgesendet wird
advanced: true
example: 04E6B78921BBA0
- name: connecttimeout
description:
de: Zeitlimit für Registrierung des Ladepunktes
en: Timeout for initial connection
advanced: true
type: duration
default: 5m
- name: timeout
default: 10m
default: 2m

mqtt:
params:
Expand Down
1 change: 1 addition & 0 deletions util/templates/includes/ocpp.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ connector: {{ .connector }}
{{- if ne .idtag "" }}
idtag: {{ .idtag }}
{{- end }}
connecttimeout: {{ .connecttimeout }}
timeout: {{ .timeout }}
{{- end }}

0 comments on commit ec8c763

Please sign in to comment.