Ocpp: refactor watchdog

This commit is contained in:
andig 2022-10-18 08:48:54 +02:00
parent 6cca320b9d
commit e79505e602
2 changed files with 10 additions and 12 deletions

View file

@ -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

View file

@ -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