public static void Main(string[] args) { Console.CancelKeyPress += (sender, eArgs) => { PlaySound(Sentinel.Sounds.Cancelled); Console.SetCursorPosition(0, LineConstants.InfoQuit); QuitEvent.Set(); eArgs.Cancel = true; }; PreLoadSounds(); WriterHelper.Writeheader(); requestType = GetRequestType(); var urlToCheck = GetUrlToCheck(); WriterHelper.WriteInfo(LineConstants.InfoGlobalStatus, text: $@"Global Status: { globalStatus }"); WriterHelper.WriteInfo(LineConstants.InfoGlobalAttempts, text: $@"Current Attempts left: { NumberOfAttempts }"); WriterHelper.WriteInfo(LineConstants.InfoCurrentException, text: @"Current Exception: -"); WriterHelper.WriteInfo(LineConstants.InfoResponseStatus, text: @"Response: -"); Task.Run(() => SendRequest(urlToCheck, requestType, NumberOfAttempts)); PlaySound(Sentinel.Sounds.Startup); QuitEvent.WaitOne(); }
private static async Task SendPing(string urlToCheck) { using (var ping = new Ping()) { var result = ping.Send(urlToCheck); WriterHelper.WriteInfo(LineConstants.InfoResponseStatus, text: $@"Response: Status: {result.Status}, RoundtripTime: {result.RoundtripTime}"); if (result.Status != IPStatus.Success || result.RoundtripTime > TimeOut) { throw new Exception("Ping fault"); } } }
private static async Task SendWebRequest(string urlToCheck) { var httpClient = new HttpClient { Timeout = TimeSpan.FromMilliseconds(TimeOut), BaseAddress = new Uri(urlToCheck) }; var responseMessage = await httpClient.GetAsync(string.Empty); if (responseMessage.StatusCode != HttpStatusCode.OK) { WriterHelper.WriteInfo(LineConstants.InfoResponseStatus, text: $@"Response: Status: {responseMessage.StatusCode}"); throw new Exception(responseMessage.Content.ToString()); } }
private static async Task SendRequest(string urlToCheck, RequestType requestType, int localAttempts) { try { if (requestType == RequestType.Ping) { await SendPing(urlToCheck); } else { await SendWebRequest(urlToCheck); } localAttempts = NumberOfAttempts; WriterHelper.WriteInfo(LineConstants.InfoGlobalAttempts, ColumnConstants.InfoGlobalAttempts, localAttempts.ToString()); WriterHelper.WriteInfo(LineConstants.InfoCurrentException, ColumnConstants.InfoCurrentException, "-"); if (globalStatus == ExecutionStatus.AlarmFired) { globalStatus = ExecutionStatus.SendRequest; WriterHelper.WriteInfo(LineConstants.InfoGlobalStatus, ColumnConstants.InfoGlobalStatus, globalStatus.ToString()); PlayVictory(); } if (globalStatus == ExecutionStatus.Warning) { globalStatus = ExecutionStatus.SendRequest; WriterHelper.WriteInfo(LineConstants.InfoGlobalStatus, ColumnConstants.InfoGlobalStatus, globalStatus.ToString()); PlaySound(Sentinel.Sounds.Startup); } } catch (Exception e) { WriterHelper.WriteInfo(LineConstants.InfoCurrentException, ColumnConstants.InfoCurrentException, e.Message); if (globalStatus != ExecutionStatus.AlarmFired) { globalStatus = ExecutionStatus.Warning; WriterHelper.WriteInfo(LineConstants.InfoGlobalStatus, ColumnConstants.InfoGlobalStatus, globalStatus.ToString()); localAttempts--; WriterHelper.WriteInfo(LineConstants.InfoGlobalAttempts, ColumnConstants.InfoGlobalAttempts, localAttempts.ToString()); } if (localAttempts <= 0) { globalStatus = globalStatus != ExecutionStatus.AlarmFired ? ExecutionStatus.AttempsExcedeed : globalStatus; WriterHelper.WriteInfo(LineConstants.InfoGlobalStatus, ColumnConstants.InfoGlobalStatus, globalStatus.ToString()); } } finally { switch (globalStatus) { case ExecutionStatus.SendRequest: case ExecutionStatus.AlarmFired: Thread.Sleep(5000); await SendRequest(urlToCheck, requestType, localAttempts); break; case ExecutionStatus.Warning: PlayAlarm(localAttempts); Thread.Sleep(5000); await SendRequest(urlToCheck, requestType, localAttempts); break; case ExecutionStatus.AttempsExcedeed: globalStatus = ExecutionStatus.AlarmFired; WriterHelper.WriteInfo(LineConstants.InfoGlobalStatus, ColumnConstants.InfoGlobalStatus, globalStatus.ToString()); PlayAlarm(localAttempts); Thread.Sleep(5000); await SendRequest(urlToCheck, requestType, localAttempts); break; } } }