From 853c49969d52aeafe81ee3c47d4d6e09ff872e06 Mon Sep 17 00:00:00 2001 From: Le Chau Date: Thu, 25 Jun 2020 09:33:57 +0700 Subject: [PATCH] =?UTF-8?q?SmallRefactor:=20=C4=90=C6=B0a=20HttpClient=20r?= =?UTF-8?q?a=20ngo=C3=A0i=20v=C3=B2ng=20l=E1=BA=B7p=20tr=C3=A1nh=20b?= =?UTF-8?q?=E1=BB=8B=20g=E1=BB=8Di=20qu=C3=A1=20nhi=E1=BB=81u=20l=E1=BA=A7?= =?UTF-8?q?n?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AIParkingApplication/Statistic.cs | 43 +++++++++++++++---------------- 1 file changed, 21 insertions(+), 22 deletions(-) diff --git a/AIParkingApplication/Statistic.cs b/AIParkingApplication/Statistic.cs index ffaac09..7c2a752 100644 --- a/AIParkingApplication/Statistic.cs +++ b/AIParkingApplication/Statistic.cs @@ -14,6 +14,7 @@ namespace AIParkingApplication public partial class Statistic : UserControl { private const string AIPARKING_CLIENT_HTTP_API_REQUEST = "http://{0}:{1}{2}"; + private readonly HttpClient client = new HttpClient { BaseAddress = new Uri("http://192.168.1.123:80/"), Timeout = TimeSpan.FromMilliseconds(5000) }; Label lblLoaiXe; Label lblXeMay; @@ -43,28 +44,26 @@ namespace AIParkingApplication { try { - using (var client = new HttpClient { BaseAddress = new Uri("http://192.168.1.123:80/"), Timeout = TimeSpan.FromMilliseconds(5000) }) + HttpResponseMessage response = await client.GetAsync("/api/statistics"); + response.EnsureSuccessStatusCode(); + var vehicle = await response.Content.ReadAsAsync(); + lblSoLuongXeMay?.Invoke(new Action(() => { - HttpResponseMessage response = await client.GetAsync("/api/statistics"); - response.EnsureSuccessStatusCode(); - var vehicle = await response.Content.ReadAsAsync(); - lblSoLuongXeMay?.Invoke(new Action(() => - { - lblSoLuongXeMay.Text = vehicle.NumberOfMoto.ToString(); - })); - lblSoLuongOto?.Invoke(new Action(() => - { - lblSoLuongOto.Text = vehicle.NumberOfCar.ToString(); - })); - lblVehicleTotalIn?.Invoke(new Action(() => - { - lblVehicleTotalIn.Text = vehicle.TotalIn; - })); - lblVehicleTotalOut?.Invoke(new Action(() => - { - lblVehicleTotalOut.Text = vehicle.TotelOut; - })); - } + lblSoLuongXeMay.Text = vehicle.NumberOfMoto.ToString(); + })); + lblSoLuongOto?.Invoke(new Action(() => + { + lblSoLuongOto.Text = vehicle.NumberOfCar.ToString(); + })); + lblVehicleTotalIn?.Invoke(new Action(() => + { + lblVehicleTotalIn.Text = vehicle.TotalIn; + })); + lblVehicleTotalOut?.Invoke(new Action(() => + { + lblVehicleTotalOut.Text = vehicle.TotelOut; + })); + } catch (Exception ex) { @@ -102,7 +101,7 @@ namespace AIParkingApplication [JsonProperty("moto")] public int NumberOfMoto { get; set; } - + [JsonProperty("total_in")] public string TotalIn { get; set; }