package yabl import ( "cli-mon/can" "time" ) func NewChanProtocolConverter( tag string, fromBus <-chan *can.CanFrame, fromProducer <-chan *Event, toBus chan<- *can.CanFrame, toConsumer chan<- *Event, lossCheckMs time.Duration, isOffline bool) Converter { c := newConverter(tag) c.fromBus = fromBus c.fromProducer = fromProducer c.toBus = toBus c.toConsumer = toConsumer c.startFromBus() c.startToBus() c.startCheckTimeouts(isOffline, lossCheckMs, 2) return c } func (c *converter) startFromBus() { go func() { defer close(c.toConsumer) for frame := range c.fromBus { if events, ok := c.EventsFromFrame(frame); ok { for _, event := range events { c.toConsumer <- event } } } }() } func (c *converter) startToBus() { go func() { for event := range c.fromProducer { var events = []*Event{event} if frames, ok := c.FramesFromEvents(events); ok { for _, frame := range frames { c.toBus <- frame } } } }() } func (c *converter) startCheckTimeouts(isOffline bool, intervalMs time.Duration, waitStartSec time.Duration) { var f func() f = func() { if events, ok := c.CheckTimeouts(isOffline); ok { for _, event := range events { c.toConsumer <- event } } time.AfterFunc(intervalMs*time.Millisecond, f) } time.AfterFunc(waitStartSec*time.Second, f) }