Fix offline timeouts

fix/offline-timeouts
Terekhin Alexandr 2 years ago
parent d5d0d903f0
commit 46b1f6dbb9
Signed by: didinst
GPG Key ID: D2EF94423C23BF12
  1. 4
      cli-mon.go
  2. 26
      yabl/protocol.go

@ -54,9 +54,11 @@ func main() {
updates := make(chan *yabl.Event, 1000)
isOffline := len(*filename) > 0 || *stdin
var f func()
f = func() {
if events, ok := converter.CheckTimeouts(); ok {
if events, ok := converter.CheckTimeouts(isOffline); ok {
for _, event := range events {
updates <- event
}

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

Loading…
Cancel
Save