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;