diff --git a/AgroBase/AgroBase/Services/APIService.cs b/AgroBase/AgroBase/Services/APIService.cs index afcc64f96..83adb26c3 100644 --- a/AgroBase/AgroBase/Services/APIService.cs +++ b/AgroBase/AgroBase/Services/APIService.cs @@ -5,6 +5,7 @@ using System.IO; using System.Net; using System.Net.Http; using System.Net.Http.Headers; +using System.Threading; using System.Threading.Tasks; namespace AgroBase.Services @@ -26,28 +27,7 @@ namespace AgroBase.Services return "https://zendioninc.com.br/agrobase/downloads"; } } - private static bool _hasInternet - { - get - { - try - { - using (var client = new WebClient()) - { - using (client.OpenRead("https://zendioninc.com.br/")) - { - return true; - } - } - } - catch - { - return false; - } - } - } public static bool HasInternet { get; set; } - public static bool HasInternetNow() => _hasInternet; public static async Task IniciarRotinasAsync() { @@ -64,9 +44,42 @@ namespace AgroBase.Services tmrMonitoramento.Start(); } + private static readonly HttpClient _internetClient = new HttpClient { Timeout = TimeSpan.FromSeconds(3) }; + + public static async Task HasInternetNowAsync(CancellationToken ct = default) + { + try + { + var request = + new HttpRequestMessage( + HttpMethod.Get, + "https://zendioninc.com.br/" + ); + + var response = + await _internetClient.SendAsync( + request, + HttpCompletionOption.ResponseHeadersRead, + ct + ).ConfigureAwait(false); + + /* + * Qualquer resposta HTTP significa que DNS, + * TCP e HTTPS chegaram ao servidor. + */ + return true; + } + catch + { + return false; + } + } + private static async Task tmrMonitoramento_Tick() { - HasInternet = HasInternetNow(); + HasInternet = + await HasInternetNowAsync() + .ConfigureAwait(false); } // Método para GET diff --git a/AgroBase/OperationControl/Services/GpsService.cs b/AgroBase/OperationControl/Services/GpsService.cs index 7cba7aa28..a3ae728a0 100644 --- a/AgroBase/OperationControl/Services/GpsService.cs +++ b/AgroBase/OperationControl/Services/GpsService.cs @@ -2062,21 +2062,13 @@ namespace OperationControl.Services private async Task AplicarCorrecaoRTK_NtripAsync(CancellationToken ct) { - if (!IsConnected || !APIService.HasInternet) + if (!IsConnected) return; double backoffSeconds = 1.0; while (!ct.IsCancellationRequested && CorrecaoRTK_Ntrip) { - if (!APIService.HasInternet) - { - Ntrip_Conectado = false; - LimparNtripAtivo(); - await Task.Delay(TimeSpan.FromSeconds(2), ct).ConfigureAwait(false); - continue; - } - List providers = ObterSequenciaProvedoresNtrip(); if (providers.Count == 0) @@ -3796,9 +3788,6 @@ namespace OperationControl.Services Func stopNtrip = null, CancellationToken ct = default) { - if (!APIService.HasInternetNow()) - return false; - await ConfigurarComoRoverParadoAsync(portaUsb, portaEntrada, ct) .ConfigureAwait(false);