diff --git a/AIParkingApplication/ApiController.cs b/AIParkingApplication/ApiController.cs index 2550e67..9042b1a 100644 --- a/AIParkingApplication/ApiController.cs +++ b/AIParkingApplication/ApiController.cs @@ -13,17 +13,15 @@ namespace AIParkingApplication private const int MAX_REQUEST = 3; private HttpClient httpClient; private bool isHttpClientDisposabled; - private int numberOfRetry; public ApiController(string baseAddress, int numberOfRetry = 5) { httpClient = new HttpClient { BaseAddress = new Uri(baseAddress), - Timeout = TimeSpan.FromSeconds(5) + Timeout = TimeSpan.FromSeconds(1) }; isHttpClientDisposabled = false; - this.numberOfRetry = numberOfRetry; } ~ApiController() @@ -91,7 +89,7 @@ namespace AIParkingApplication { response = await httpClient.PostAsJsonAsync("/api/check-card", request); requestCounter += 1; - Thread.Sleep(100); + await Task.Delay(100); } while (response.StatusCode != HttpStatusCode.OK && requestCounter < MAX_REQUEST); @@ -102,7 +100,7 @@ namespace AIParkingApplication catch (Exception ex) { Console.WriteLine($"CheckCard Exception:\t{DateTime.Now.GetTimeFormatted()} \t {ex.Message}"); - return new CardInformation(); + return null; } } @@ -135,9 +133,8 @@ namespace AIParkingApplication { response = await httpClient.PostAsJsonAsync("/api/save-logs", request); requestCounter += 1; - Thread.Sleep(100); - } - while (response.StatusCode != HttpStatusCode.OK && requestCounter < MAX_REQUEST); + await Task.Delay(100); + } while (response.StatusCode != HttpStatusCode.OK && requestCounter < MAX_REQUEST); response.EnsureSuccessStatusCode(); SaveLogRespone saveLogRespone = await response.Content.ReadAsAsync(); return saveLogRespone; diff --git a/AIParkingApplication/LaneIn.cs b/AIParkingApplication/LaneIn.cs index 4baf402..ae6f274 100644 --- a/AIParkingApplication/LaneIn.cs +++ b/AIParkingApplication/LaneIn.cs @@ -56,6 +56,11 @@ namespace AIParkingApplication } ClearPlateAndOverviewImage(); var cardInfoResult = await apiController.GetCardInformation(cardNumber); + if (cardInfoResult == null) + { + lblStatusInfo.UpdateLabel("KHÔNG THỂ KẾT NỐI SERVER", Color.Purple); + return; + } if (!cardInfoResult.IsValid) { lblStatusInfo.UpdateLabel("THẺ KHÔNG HỢP LỆ", Color.Purple); diff --git a/AIParkingApplication/LaneOut.cs b/AIParkingApplication/LaneOut.cs index 4bc3efb..489db20 100644 --- a/AIParkingApplication/LaneOut.cs +++ b/AIParkingApplication/LaneOut.cs @@ -59,6 +59,11 @@ namespace AIParkingApplication ClearPlateAndOverviewImage(); ClearPlateAndOverviewImageIn(); var cardInfoResult = await apiController.GetCardInformation(cardNumber); + if (cardInfoResult == null) + { + lblStatusInfo.UpdateLabel("KHÔNG THỂ KẾT NỐI SERVER", Color.Purple); + return; + } if (!cardInfoResult.IsValid) { lblStatusInfo.UpdateLabel("THẺ KHÔNG HỢP LỆ", Color.Purple);