From e79505e602cbeae10285c0ceea9fa46d9076eb77 Mon Sep 17 00:00:00 2001 From: andig Date: Tue, 18 Oct 2022 08:48:54 +0200 Subject: [PATCH] Ocpp: refactor watchdog --- charger/ocpp.go | 2 +- charger/ocpp/cp.go | 20 +++++++++----------- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/charger/ocpp.go b/charger/ocpp.go index 3304f3252..08753f444 100644 --- a/charger/ocpp.go +++ b/charger/ocpp.go @@ -217,7 +217,7 @@ func NewOCPP(id string, connector int, idtag string, meterValues string, meterIn c.log.DEBUG.Println("enabling meter watchdog") } - cp.WatchDog(meterInterval) + go cp.WatchDog(meterInterval) } // TODO deprecate diff --git a/charger/ocpp/cp.go b/charger/ocpp/cp.go index d01662bec..dee0aa7c8 100644 --- a/charger/ocpp/cp.go +++ b/charger/ocpp/cp.go @@ -113,20 +113,18 @@ func (cp *CP) Initialized(timeout time.Duration) bool { } } +// WatchDog triggers meter values messages if older than timeout. +// Must be wrapped in a goroutine. func (cp *CP) WatchDog(timeout time.Duration) { - cp.timeout = timeout + for ; true; <-time.NewTicker(timeout).C { + cp.mu.Lock() + update := cp.txnId != 0 && time.Since(cp.meterUpdated) > timeout + cp.mu.Unlock() - go func() { - for ; true; <-time.NewTicker(timeout).C { - cp.mu.Lock() - update := cp.txnId != 0 && time.Since(cp.meterUpdated) > timeout - cp.mu.Unlock() - - if update { - Instance().TriggerMessageRequest(cp.ID(), core.MeterValuesFeatureName) - } + if update { + Instance().TriggerMessageRequest(cp.ID(), core.MeterValuesFeatureName) } - }() + } } // TransactionID returns the current transaction id