Skip to content

Commit

Permalink
Ocpp: refactor watchdog
Browse files Browse the repository at this point in the history
  • Loading branch information
andig committed Oct 18, 2022
1 parent 6cca320 commit e79505e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 12 deletions.
2 changes: 1 addition & 1 deletion charger/ocpp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 9 additions & 11 deletions charger/ocpp/cp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e79505e

Please sign in to comment.