From d5089f189b17b619c578db25548684d44cdca38c Mon Sep 17 00:00:00 2001 From: Le Chau Date: Fri, 26 Jun 2020 15:21:22 +0700 Subject: [PATCH] =?UTF-8?q?Chuy=E1=BB=83n=20API=20l=E1=BA=A5y=20Th?= =?UTF-8?q?=E1=BB=91ng=20k=C3=AA=20t=E1=BB=AB=20{Statistic}=20sang=20{ApiC?= =?UTF-8?q?ontroller}?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AIParkingApplication/ApiController.cs | 25 +++++++++++++++++++++++++ AIParkingApplication/Statistic.cs | 27 ++++++--------------------- 2 files changed, 31 insertions(+), 21 deletions(-) diff --git a/AIParkingApplication/ApiController.cs b/AIParkingApplication/ApiController.cs index f66b7b4..4a325be 100644 --- a/AIParkingApplication/ApiController.cs +++ b/AIParkingApplication/ApiController.cs @@ -60,6 +60,31 @@ namespace AIParkingApplication } } + public async Task GetStatisticInfo() + { + try + { + HttpResponseMessage response = await httpClient.GetAsync("/api/statistics"); + response.EnsureSuccessStatusCode(); + Statistic.ParkInfo parkInfo = await response.Content.ReadAsAsync(); + + if (string.IsNullOrEmpty(parkInfo.TotalIn)) + { + parkInfo.TotalIn = "0"; + } + if (string.IsNullOrEmpty(parkInfo.TotalOut)) + { + parkInfo.TotalOut = "0"; + } + return parkInfo; + } + catch (Exception ex) + { + Console.WriteLine($"SendApiStatisticRequest : {ex.Message}"); + return new Statistic.ParkInfo(); + } + } + public async void GetApiPathFromServer() { try diff --git a/AIParkingApplication/Statistic.cs b/AIParkingApplication/Statistic.cs index efda3d1..e7f596b 100644 --- a/AIParkingApplication/Statistic.cs +++ b/AIParkingApplication/Statistic.cs @@ -3,6 +3,7 @@ using System.Windows.Forms; using System.Threading; using Newtonsoft.Json; using System.Net.Http; +using System.Xml.Serialization; namespace AIParkingApplication { @@ -12,6 +13,7 @@ namespace AIParkingApplication private readonly Thread thrStatistics; private readonly TimeSpan updateInterval; private ParkInfo parkInfo; + private string baseAddress; private Label lblLoaiXe; private Label lblXeMay; @@ -23,8 +25,8 @@ namespace AIParkingApplication public Statistic(string baseAddress, TimeSpan updateInterval) { InitializeComponent(); + this.baseAddress = baseAddress; this.updateInterval = updateInterval; - client = new HttpClient { BaseAddress = new Uri(baseAddress), Timeout = TimeSpan.FromMilliseconds(5000) }; thrStatistics = new Thread(new ThreadStart(GetStatistic)) { IsBackground = true }; thrStatistics.Start(); } @@ -39,27 +41,10 @@ namespace AIParkingApplication } } - private async void GetDataFromServer() + async void GetDataFromServer() { - try - { - HttpResponseMessage response = await client.GetAsync("/api/statistics"); - response.EnsureSuccessStatusCode(); - parkInfo = await response.Content.ReadAsAsync(); - - if (string.IsNullOrEmpty(parkInfo.TotalIn)) - { - parkInfo.TotalIn = "0"; - } - if (string.IsNullOrEmpty(parkInfo.TotalOut)) - { - parkInfo.TotalOut = "0"; - } - } - catch (Exception ex) - { - Console.WriteLine($"SendApiStatisticRequest : {ex.Message}"); - } + ApiController apiController = new ApiController(this.baseAddress); + parkInfo = await apiController.GetStatisticInfo(); } private void ShowInfo()