public RobotConnectionService(IStreamResource streamResource) { _streamResource = streamResource; _watchdog = new Watchdog(1100); //this should be minimum 2x keepAlive sending period _watchdog.TimeoutOccured += (sender, args) => TimeoutOccured?.Invoke(sender, args); //TODO unifiy the error event args, so we can use one common event handler var messageExtractor = new MessageExtractor(); messageExtractor.KeepAliveReceived += (sender, args) => _watchdog.ResetWatchdog(); messageExtractor.MessageLostOccured += (sender, args) => Logger.Fatal($"Lost message, total count: {args.TotalLostCount}"); messageExtractor.SpeedCurrentFeedbackReceived += (sender, args) => SpeedCurrentFeedbackReceived?.Invoke(sender, args); messageExtractor.VoltageTemperatureFeedbackReceived += (sender, args) => VoltageTemperatureFeedbackReceived?.Invoke(sender, args); messageExtractor.ParametersReceived += (sender, args) => ParametersReceived?.Invoke(sender, args); _receiverTask = new ReceiverTask(_streamResource); _receiverTask.ErrorOccurred += (sender, args) => Logger.Error($"Receiver task error: {args.GetException().Message}"); _receiverTask.DataReceived += (sender, args) => messageExtractor.TryGetMessage(args.Data); _receiverTask.Start(); _senderQueue = new SendQueueWrapper(); _senderTask = new SenderTask(_streamResource, _senderQueue); _senderTask.ErrorOccurred += (sender, args) => Logger.Error($"Sender task error: {args.GetException().Message}"); _senderTask.Start(); _watchdog.Start(); }
private void OnElapsed(object sender, ElapsedEventArgs e) { Debug.WriteLine("Connection timeout: robot is not sending keep alive"); TimeoutOccured?.Invoke(this, EventArgs.Empty); }