public void Connect() { PortNames = GetPortsNames(); if (PortNames == null) { return; } for (int i = 0; i < PortNames.Count(); i++) { PortHandle = new SerialPort(PortNames[i], _baudRate) { DataBits = 8, Parity = Parity.None, StopBits = StopBits.One }; ReadEnabled = true; try { PortHandle.Open(); } catch (Exception e) { ConnectionFailure?.Invoke(e); } ReadTimer.Change(0, PolyConstants.USBTimerInterval); CurrentPort = i; break; } }
public Task StartReconnection() { reconnectCancellationSource = new CancellationTokenSource(); return(Task.Factory.StartNew(async() => { reconnectCancellationSource.Token.ThrowIfCancellationRequested(); while (true) { if (reconnectCancellationSource.Token.IsCancellationRequested) { reconnectCancellationSource.Token.ThrowIfCancellationRequested(); } if (ConnectionChallengeFunction == null) { throw new ConfigurationException("Unsetted challenge function for reconnection."); } if (!await ConnectionChallengeFunction(this)) { StoptListener(); ConnectionFailure?.Invoke(this, DateTime.UtcNow); await ProtocolStream.Reconnect(); startListener(); } await Task.Delay(ConnectionDelay); } }, reconnectCancellationSource.Token)); }
protected void OnConnectionException(Exception e, ConnectionFailure connectionFailure) { _serverConnectedAndStreaming = false; if (ConnectionExceptionEvent != null) { ConnectionExceptionEvent.Invoke(this, new ConnectionExceptionArgs(e, connectionFailure)); } }
public ConnectFailedEventArgs(ConnectionFailure reason) { this.Reason = reason; }
public void Worker() { if (Log.IsDebugEnabled) Log.DebugFormat(Resources.Msg_Monitor_WorkerStarted); ConnectionFailure cf = new ConnectionFailure(); bool previousFailue = false; bool isModemLive = true; _eventAggregator.SendMessage<ConnectionUpEvent>(); while (_isWorking) { PingResponse pr = pingService.Ping(_configurationOptions.MonitorAddress, 3); PingResponse prModem = pingService.Ping(_configurationOptions.ModemIPAddress, 3); if (prModem.FailureCount == 3) { _eventAggregator.SendMessage<ModemDownEvent>(); isModemLive = false; } else { _eventAggregator.SendMessage<ModemUpEvent>(); isModemLive = true; } if (pr.FailureCount == 3) { if (Log.IsDebugEnabled) Log.DebugFormat(Resources.Msg_Monitor_WorkerPingTimeout); _eventAggregator.SendMessage<ConnectionDownEvent>(); if (!previousFailue) { previousFailue = true; cf.StartTimestamp = DateTime.Now; string path = null; if (isModemLive) { if (Log.IsDebugEnabled) Log.DebugFormat(Resources.Msg_Monitor_WorkerStartingModemPagesCapture); Image signalPage = null; Image logPage = null; try { signalPage = webImageService.GetWebpageImage(_configurationOptions.ModemSignalPageUrl, _configurationOptions.ModemUsername, _configurationOptions.ModemPassword); logPage = webImageService.GetWebpageImage(_configurationOptions.ModemLogPageUrl, _configurationOptions.ModemUsername, _configurationOptions.ModemPassword); } catch (Exception ex) { Log.ErrorFormat("============START CMWATCHER ERROR============="); Log.ErrorFormat(string.Format("Unable to capture images from the modem")); Log.ErrorFormat("Check to ensure CMWatcher can get to the modem paths and that they are correct.}"); if (Log.IsDebugEnabled) Log.DebugFormat(ex.ToString()); Log.ErrorFormat("============END CMWATCHER ERROR============="); } path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase).Replace("file:\\", ""); string dateDirectory = string.Format("{0}-{1}-{2}_{3}-{4}-{5}", DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Year, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second); path = path + string.Format("\\images\\{0}", dateDirectory); try { Directory.CreateDirectory(path); if (signalPage != null) signalPage.Save(string.Format("{0}\\SignalPage.bmp", path)); if (logPage != null) logPage.Save(string.Format("{0}\\LogPage.bmp", path)); } catch (Exception ex) { Log.ErrorFormat("============START ERROR============="); Log.ErrorFormat(string.Format("Unable to create or save the images to path: {0}", path)); Log.ErrorFormat("Check to ensure CMWatcher can write to that directory, or run CMWatcher as Administrator.}"); if (Log.IsDebugEnabled) Log.DebugFormat(ex.ToString()); Log.ErrorFormat("============END ERROR============="); } } else { Log.ErrorFormat("NOTICE: Modem is unreachable, not capturing images."); } if (Log.IsErrorEnabled) { Log.ErrorFormat(Resources.Msg_Monitor_WorkerConnectionErrorHeader); Log.ErrorFormat(pr.Data); if (!String.IsNullOrEmpty(path)) Log.ErrorFormat(string.Format(Resources.Msg_Montior_WorkerSavingPath, path)); } if (!String.IsNullOrEmpty(path)) cf.ScreenShotsPath = path; else cf.ScreenShotsPath = String.Empty; cf.Data = pr.Data; } else { if (Log.IsDebugEnabled) Log.DebugFormat(Resources.Msg_Monitor_WorkerPreviousFailureDetectedSkip); _eventAggregator.SendMessage<ConnectionDownEvent>(); } } else { if (Log.IsDebugEnabled) Log.DebugFormat(Resources.Msg_Monitor_WorkerPingSuccess); if (previousFailue) { if (Log.IsDebugEnabled) Log.DebugFormat(string.Format(Resources.Msg_Monitor_WorkerPreviousFailureDetected, cf.StartTimestamp, DateTime.Now)); _eventAggregator.SendMessage<ConnectionUpEvent>(); cf.EndTimestamp = DateTime.Now; connectionFailureService.SaveConnectionFailure(cf); previousFailue = false; cf = new ConnectionFailure(); _eventAggregator.SendMessage<StatusUpdateEvent>(); } } if (Log.IsDebugEnabled) Log.DebugFormat(string.Format(Resources.Msg_Monitor_WorkerThreadSleep, "1000")); Thread.Sleep(1000); } if (Log.IsDebugEnabled) Log.DebugFormat(Resources.Msg_Monitor_WorkerFinished); }
public ConnectionExceptionArgs(Exception e, ConnectionFailure connectionFailure) { _e = e; _connectionFailure = connectionFailure; }