From 8f8f5343e9a6158dc1bcfa4b8e1abb827545cd6b Mon Sep 17 00:00:00 2001 From: Le Chau Date: Thu, 25 Jun 2020 10:21:40 +0700 Subject: [PATCH 01/11] =?UTF-8?q?truy=E1=BB=81n=20chu=20k=C3=AC=20l?= =?UTF-8?q?=E1=BA=A5y=20th=E1=BB=91ng=20k=C3=AA=20qua=20tham=20s=E1=BB=91?= =?UTF-8?q?=20c=E1=BB=A7a=20constructor=20thay=20v=C3=AC=20constant=20nh?= =?UTF-8?q?=C6=B0=20tr=C6=B0=E1=BB=9Bc?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AIParkingApplication/AppConstant.cs | 2 -- AIParkingApplication/Statistic.cs | 6 ++++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/AIParkingApplication/AppConstant.cs b/AIParkingApplication/AppConstant.cs index 35d9780..e447052 100644 --- a/AIParkingApplication/AppConstant.cs +++ b/AIParkingApplication/AppConstant.cs @@ -9,7 +9,5 @@ #endregion public const string CAMERA_FAILED_IMAGE_PATH = @"Images\CantConnectCamera.jpg"; - - public const int TIME_TO_GET_STATISTIC_INFO = 60; } } diff --git a/AIParkingApplication/Statistic.cs b/AIParkingApplication/Statistic.cs index d5b598d..feb5142 100644 --- a/AIParkingApplication/Statistic.cs +++ b/AIParkingApplication/Statistic.cs @@ -10,6 +10,7 @@ namespace AIParkingApplication { private readonly HttpClient client; private readonly Thread thrStatistics; + private int timeCycle; Label lblLoaiXe; Label lblXeMay; @@ -18,9 +19,10 @@ namespace AIParkingApplication Label lblSoLuongXeMay; Label lblSoLuongOto; - public Statistic(string apiUrl) + public Statistic(string apiUrl, int timeCycle_Sec = 1) { InitializeComponent(); + timeCycle = timeCycle_Sec; ; client = new HttpClient { BaseAddress = new Uri(apiUrl), Timeout = TimeSpan.FromMilliseconds(5000) }; thrStatistics = new Thread(new ThreadStart(GetStatistic)) { IsBackground = true }; thrStatistics.Start(); @@ -31,7 +33,7 @@ namespace AIParkingApplication while (true) { GETDataFromServer(); - Thread.Sleep(AppConstant.TIME_TO_GET_STATISTIC_INFO * 1000); + Thread.Sleep(timeCycle * 1000); } } From cdc5ffb96886278f88c3e50a472afb281f8450bb Mon Sep 17 00:00:00 2001 From: Le Chau Date: Thu, 25 Jun 2020 10:22:50 +0700 Subject: [PATCH 02/11] =?UTF-8?q?SmallRefactor:=20Th=C3=AAm=20AccessModifi?= =?UTF-8?q?er=20cho=20Label?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AIParkingApplication/Statistic.cs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/AIParkingApplication/Statistic.cs b/AIParkingApplication/Statistic.cs index feb5142..a9189f7 100644 --- a/AIParkingApplication/Statistic.cs +++ b/AIParkingApplication/Statistic.cs @@ -12,12 +12,12 @@ namespace AIParkingApplication private readonly Thread thrStatistics; private int timeCycle; - Label lblLoaiXe; - Label lblXeMay; - Label lblOto; - Label lblSoLuong; - Label lblSoLuongXeMay; - Label lblSoLuongOto; + private Label lblLoaiXe; + private Label lblXeMay; + private Label lblOto; + private Label lblSoLuong; + private Label lblSoLuongXeMay; + private Label lblSoLuongOto; public Statistic(string apiUrl, int timeCycle_Sec = 1) { From bc86b4c00fabd46f7b276cf0a0404b7fea1cd37f Mon Sep 17 00:00:00 2001 From: Le Chau Date: Thu, 25 Jun 2020 10:29:24 +0700 Subject: [PATCH 03/11] =?UTF-8?q?SmallRefactor:=20B=E1=BB=8F=20check=20obj?= =?UTF-8?q?ect=20{Vehicle}=20null=20khi=20g=C3=A1n=20gi=C3=A1=20tr?= =?UTF-8?q?=E1=BB=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AIParkingApplication/Statistic.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/AIParkingApplication/Statistic.cs b/AIParkingApplication/Statistic.cs index a9189f7..b2b1c06 100644 --- a/AIParkingApplication/Statistic.cs +++ b/AIParkingApplication/Statistic.cs @@ -44,19 +44,19 @@ namespace AIParkingApplication HttpResponseMessage response = await client.GetAsync("/api/statistics"); response.EnsureSuccessStatusCode(); var vehicle = await response.Content.ReadAsAsync(); - lblSoLuongXeMay?.Invoke(new Action(() => + lblSoLuongXeMay.Invoke(new Action(() => { lblSoLuongXeMay.Text = vehicle.NumberOfMoto.ToString(); })); - lblSoLuongOto?.Invoke(new Action(() => + lblSoLuongOto.Invoke(new Action(() => { lblSoLuongOto.Text = vehicle.NumberOfCar.ToString(); })); - lblVehicleTotalIn?.Invoke(new Action(() => + lblVehicleTotalIn.Invoke(new Action(() => { lblVehicleTotalIn.Text = vehicle.TotalIn; })); - lblVehicleTotalOut?.Invoke(new Action(() => + lblVehicleTotalOut.Invoke(new Action(() => { lblVehicleTotalOut.Text = vehicle.TotelOut; })); From effa22abc92c11d5bdd6131ad3aa95424f810cdb Mon Sep 17 00:00:00 2001 From: Le Chau Date: Thu, 25 Jun 2020 10:41:37 +0700 Subject: [PATCH 04/11] =?UTF-8?q?s=E1=BB=ADa=20l=E1=BB=97i=20ch=C3=ADnh=20?= =?UTF-8?q?t=E1=BA=A3=20c=E1=BB=A7a=20bi=E1=BA=BFn=20{TotalOut}?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AIParkingApplication/Statistic.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/AIParkingApplication/Statistic.cs b/AIParkingApplication/Statistic.cs index b2b1c06..c63b46d 100644 --- a/AIParkingApplication/Statistic.cs +++ b/AIParkingApplication/Statistic.cs @@ -58,7 +58,7 @@ namespace AIParkingApplication })); lblVehicleTotalOut.Invoke(new Action(() => { - lblVehicleTotalOut.Text = vehicle.TotelOut; + lblVehicleTotalOut.Text = vehicle.TotalOut; })); } @@ -95,14 +95,14 @@ namespace AIParkingApplication [JsonProperty("oto")] public int NumberOfCar { get; set; } - [JsonProperty("moto")] + [JsonProperty("moto1")] public int NumberOfMoto { get; set; } [JsonProperty("total_in")] public string TotalIn { get; set; } [JsonProperty("total_out")] - public string TotelOut { get; set; } + public string TotalOut { get; set; } } } } From b71846e3c945a3033b5728c8f05cabafe4c8d33d Mon Sep 17 00:00:00 2001 From: Le Chau Date: Thu, 25 Jun 2020 10:42:26 +0700 Subject: [PATCH 05/11] =?UTF-8?q?Check=20vehice=20=3D=3D=20null=20khi=20ap?= =?UTF-8?q?i=20tr=E1=BA=A3=20v=E1=BB=81=20null?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AIParkingApplication/Statistic.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/AIParkingApplication/Statistic.cs b/AIParkingApplication/Statistic.cs index c63b46d..67642f3 100644 --- a/AIParkingApplication/Statistic.cs +++ b/AIParkingApplication/Statistic.cs @@ -44,6 +44,10 @@ namespace AIParkingApplication HttpResponseMessage response = await client.GetAsync("/api/statistics"); response.EnsureSuccessStatusCode(); var vehicle = await response.Content.ReadAsAsync(); + if(vehicle == null) + { + return; + } lblSoLuongXeMay.Invoke(new Action(() => { lblSoLuongXeMay.Text = vehicle.NumberOfMoto.ToString(); From fefc90331d5c743ac6a639c51dab0fb93eb8f153 Mon Sep 17 00:00:00 2001 From: Le Chau Date: Thu, 25 Jun 2020 10:43:26 +0700 Subject: [PATCH 06/11] =?UTF-8?q?SmallRefactor:=20S=E1=BB=ADa=20naming=20C?= =?UTF-8?q?onvention=20c=E1=BB=A7a=20{GetDataFromServer()}?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AIParkingApplication/Statistic.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/AIParkingApplication/Statistic.cs b/AIParkingApplication/Statistic.cs index 67642f3..b704df9 100644 --- a/AIParkingApplication/Statistic.cs +++ b/AIParkingApplication/Statistic.cs @@ -32,12 +32,12 @@ namespace AIParkingApplication { while (true) { - GETDataFromServer(); + GetDataFromServer(); Thread.Sleep(timeCycle * 1000); } } - private async void GETDataFromServer() + private async void GetDataFromServer() { try { From e939613153e92d70f31f819320521d28d21a86ac Mon Sep 17 00:00:00 2001 From: Le Chau Date: Thu, 25 Jun 2020 11:09:02 +0700 Subject: [PATCH 07/11] SmallRefactor: {apiUrl} -> {baseAddress} --- AIParkingApplication/Statistic.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/AIParkingApplication/Statistic.cs b/AIParkingApplication/Statistic.cs index b704df9..818fd3b 100644 --- a/AIParkingApplication/Statistic.cs +++ b/AIParkingApplication/Statistic.cs @@ -10,7 +10,7 @@ namespace AIParkingApplication { private readonly HttpClient client; private readonly Thread thrStatistics; - private int timeCycle; + private readonly int timeCycle; private Label lblLoaiXe; private Label lblXeMay; @@ -19,11 +19,11 @@ namespace AIParkingApplication private Label lblSoLuongXeMay; private Label lblSoLuongOto; - public Statistic(string apiUrl, int timeCycle_Sec = 1) + public Statistic(string baseAddress, int timeCycle_Sec = 1) { InitializeComponent(); timeCycle = timeCycle_Sec; ; - client = new HttpClient { BaseAddress = new Uri(apiUrl), Timeout = TimeSpan.FromMilliseconds(5000) }; + client = new HttpClient { BaseAddress = new Uri(baseAddress), Timeout = TimeSpan.FromMilliseconds(5000) }; thrStatistics = new Thread(new ThreadStart(GetStatistic)) { IsBackground = true }; thrStatistics.Start(); } @@ -99,7 +99,7 @@ namespace AIParkingApplication [JsonProperty("oto")] public int NumberOfCar { get; set; } - [JsonProperty("moto1")] + [JsonProperty("moto")] public int NumberOfMoto { get; set; } [JsonProperty("total_in")] From 6344eaed34399a98a25ebddcd0569ad25380a58b Mon Sep 17 00:00:00 2001 From: Le Chau Date: Thu, 25 Jun 2020 11:11:20 +0700 Subject: [PATCH 08/11] =?UTF-8?q?Refactor:=20=C4=90=E1=BB=95i=20t=C3=AAn?= =?UTF-8?q?=20class=20{Vehicle}=20->=20{parkInfo}?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AIParkingApplication/Statistic.cs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/AIParkingApplication/Statistic.cs b/AIParkingApplication/Statistic.cs index 818fd3b..493b0ba 100644 --- a/AIParkingApplication/Statistic.cs +++ b/AIParkingApplication/Statistic.cs @@ -43,26 +43,26 @@ namespace AIParkingApplication { HttpResponseMessage response = await client.GetAsync("/api/statistics"); response.EnsureSuccessStatusCode(); - var vehicle = await response.Content.ReadAsAsync(); - if(vehicle == null) + var parkInfo = await response.Content.ReadAsAsync(); + if(parkInfo == null) { return; } lblSoLuongXeMay.Invoke(new Action(() => { - lblSoLuongXeMay.Text = vehicle.NumberOfMoto.ToString(); + lblSoLuongXeMay.Text = parkInfo.NumberOfMoto.ToString(); })); lblSoLuongOto.Invoke(new Action(() => { - lblSoLuongOto.Text = vehicle.NumberOfCar.ToString(); + lblSoLuongOto.Text = parkInfo.NumberOfCar.ToString(); })); lblVehicleTotalIn.Invoke(new Action(() => { - lblVehicleTotalIn.Text = vehicle.TotalIn; + lblVehicleTotalIn.Text = parkInfo.TotalIn; })); lblVehicleTotalOut.Invoke(new Action(() => { - lblVehicleTotalOut.Text = vehicle.TotalOut; + lblVehicleTotalOut.Text = parkInfo.TotalOut; })); } @@ -94,7 +94,7 @@ namespace AIParkingApplication tlpStatisticTable.Controls.Add(lblSoLuongOto, 1, 2); } - public class Vehicle + public class ParkInfo { [JsonProperty("oto")] public int NumberOfCar { get; set; } From 7c0383fa9f173c2a71953c8ab679e4e4722810f2 Mon Sep 17 00:00:00 2001 From: Le Chau Date: Thu, 25 Jun 2020 11:16:24 +0700 Subject: [PATCH 09/11] =?UTF-8?q?Check=20null=20trong=20tr=C6=B0=E1=BB=9Dn?= =?UTF-8?q?g=20h=E1=BB=A3p=20kh=C3=B4ng=20c=C3=B3=20jsonProperty=20tr?= =?UTF-8?q?=E1=BA=A3=20v=E1=BB=81=20th=C3=AC=20set=20gi=C3=A1=20tr?= =?UTF-8?q?=E1=BB=8B=20v=E1=BB=81=20"0"=20v=E1=BB=9Bi=20{TotalIn}=20v?= =?UTF-8?q?=C3=A0=20{TotalOut}?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AIParkingApplication/Statistic.cs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/AIParkingApplication/Statistic.cs b/AIParkingApplication/Statistic.cs index 493b0ba..1b58d7b 100644 --- a/AIParkingApplication/Statistic.cs +++ b/AIParkingApplication/Statistic.cs @@ -58,10 +58,18 @@ namespace AIParkingApplication })); lblVehicleTotalIn.Invoke(new Action(() => { + if (string.IsNullOrEmpty(parkInfo.TotalIn)) + { + parkInfo.TotalIn = "0"; + } lblVehicleTotalIn.Text = parkInfo.TotalIn; })); lblVehicleTotalOut.Invoke(new Action(() => { + if (string.IsNullOrEmpty(parkInfo.TotalOut)) + { + parkInfo.TotalOut = "0"; + } lblVehicleTotalOut.Text = parkInfo.TotalOut; })); From b6aef4b86479ba41458366b1338452bce536368f Mon Sep 17 00:00:00 2001 From: Le Chau Date: Thu, 25 Jun 2020 11:23:09 +0700 Subject: [PATCH 10/11] =?UTF-8?q?SmallRefactor:=20Xo=C3=A1=20comment?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AIParkingApplication/Statistic.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/AIParkingApplication/Statistic.cs b/AIParkingApplication/Statistic.cs index 1b58d7b..016abfd 100644 --- a/AIParkingApplication/Statistic.cs +++ b/AIParkingApplication/Statistic.cs @@ -93,7 +93,6 @@ namespace AIParkingApplication lblSoLuongXeMay = new Label() { Text = "0", Font = labelFont }; lblSoLuongOto = new Label() { Text = "0", Font = labelFont }; - //Fill to StaticTable tlpStatisticTable.Controls.Add(lblLoaiXe, 0, 0); tlpStatisticTable.Controls.Add(lblXeMay, 0, 1); tlpStatisticTable.Controls.Add(lblOto, 0, 2); From 04e74cea455ddaec31261db67ef084459db308f3 Mon Sep 17 00:00:00 2001 From: Le Chau Date: Thu, 25 Jun 2020 11:46:37 +0700 Subject: [PATCH 11/11] =?UTF-8?q?T=C3=A1ch=20show=20info=20th=E1=BB=91ng?= =?UTF-8?q?=20k=C3=AA=20ra=20kh=E1=BB=8Fi=20ph=E1=BA=A7n=20{GetDataFromSer?= =?UTF-8?q?ver}?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AIParkingApplication/Statistic.cs | 69 +++++++++++++++++-------------- 1 file changed, 38 insertions(+), 31 deletions(-) diff --git a/AIParkingApplication/Statistic.cs b/AIParkingApplication/Statistic.cs index 016abfd..efda3d1 100644 --- a/AIParkingApplication/Statistic.cs +++ b/AIParkingApplication/Statistic.cs @@ -10,7 +10,8 @@ namespace AIParkingApplication { private readonly HttpClient client; private readonly Thread thrStatistics; - private readonly int timeCycle; + private readonly TimeSpan updateInterval; + private ParkInfo parkInfo; private Label lblLoaiXe; private Label lblXeMay; @@ -19,10 +20,10 @@ namespace AIParkingApplication private Label lblSoLuongXeMay; private Label lblSoLuongOto; - public Statistic(string baseAddress, int timeCycle_Sec = 1) + public Statistic(string baseAddress, TimeSpan updateInterval) { InitializeComponent(); - timeCycle = timeCycle_Sec; ; + this.updateInterval = updateInterval; client = new HttpClient { BaseAddress = new Uri(baseAddress), Timeout = TimeSpan.FromMilliseconds(5000) }; thrStatistics = new Thread(new ThreadStart(GetStatistic)) { IsBackground = true }; thrStatistics.Start(); @@ -33,7 +34,8 @@ namespace AIParkingApplication while (true) { GetDataFromServer(); - Thread.Sleep(timeCycle * 1000); + ShowInfo(); + Thread.Sleep(updateInterval); } } @@ -43,36 +45,16 @@ namespace AIParkingApplication { HttpResponseMessage response = await client.GetAsync("/api/statistics"); response.EnsureSuccessStatusCode(); - var parkInfo = await response.Content.ReadAsAsync(); - if(parkInfo == null) + parkInfo = await response.Content.ReadAsAsync(); + + if (string.IsNullOrEmpty(parkInfo.TotalIn)) { - return; + parkInfo.TotalIn = "0"; } - lblSoLuongXeMay.Invoke(new Action(() => + if (string.IsNullOrEmpty(parkInfo.TotalOut)) { - lblSoLuongXeMay.Text = parkInfo.NumberOfMoto.ToString(); - })); - lblSoLuongOto.Invoke(new Action(() => - { - lblSoLuongOto.Text = parkInfo.NumberOfCar.ToString(); - })); - lblVehicleTotalIn.Invoke(new Action(() => - { - if (string.IsNullOrEmpty(parkInfo.TotalIn)) - { - parkInfo.TotalIn = "0"; - } - lblVehicleTotalIn.Text = parkInfo.TotalIn; - })); - lblVehicleTotalOut.Invoke(new Action(() => - { - if (string.IsNullOrEmpty(parkInfo.TotalOut)) - { - parkInfo.TotalOut = "0"; - } - lblVehicleTotalOut.Text = parkInfo.TotalOut; - })); - + parkInfo.TotalOut = "0"; + } } catch (Exception ex) { @@ -80,6 +62,31 @@ namespace AIParkingApplication } } + private void ShowInfo() + { + if (parkInfo == null) + { + return; + } + lblSoLuongXeMay.Invoke(new Action(() => + { + lblSoLuongXeMay.Text = parkInfo.NumberOfMoto.ToString(); + })); + lblSoLuongOto.Invoke(new Action(() => + { + lblSoLuongOto.Text = parkInfo.NumberOfCar.ToString(); + })); + lblVehicleTotalIn.Invoke(new Action(() => + { + lblVehicleTotalIn.Text = parkInfo.TotalIn; + })); + lblVehicleTotalOut.Invoke(new Action(() => + { + lblVehicleTotalOut.Text = parkInfo.TotalOut; + })); + } + + private void Statistic_Load(object sender, EventArgs e) { tlpStatisticTable.BorderStyle = BorderStyle.FixedSingle;