Fix offline timeouts

This commit is contained in:
2023-10-10 14:00:38 +03:00
parent d5d0d903f0
commit 46b1f6dbb9
2 changed files with 26 additions and 4 deletions
+23 -3
View File
@@ -57,7 +57,7 @@ type converter struct {
type Converter interface {
EventsFromFrame(*can.CanFrame) ([]*Event, bool)
CheckTimeouts() ([]*Event, bool)
CheckTimeouts(isOffline bool) ([]*Event, bool)
}
func NewProtocolConverter() Converter {
@@ -122,12 +122,19 @@ func (u unit) GetUnitId() uint {
return uint(u)
}
func (c *converter) CheckTimeouts() ([]*Event, bool) {
func (c *converter) CheckTimeouts(isOffline bool) ([]*Event, bool) {
var multipler uint = 2
var events []*Event
for k, param := range c.protocolMap {
now := time.Now()
var now time.Time
if isOffline {
now = getMaxTime(&c.protocolMap)
} else {
now = time.Now()
}
deadline := now.Add(-time.Duration(param.interval*multipler) * time.Millisecond)
var from uint8 = 0
@@ -161,3 +168,16 @@ func (c *converter) CheckTimeouts() ([]*Event, bool) {
}
return events, events != nil
}
func getMaxTime(protocolMap *map[key]action) time.Time {
now := time.Unix(0, 0)
for _, param := range *protocolMap {
for _, fld := range param.fields {
if fld.last != nil && fld.last.After(now) {
now = *fld.last
}
}
}
return now
}