Tách show info thống kê ra khỏi phần {GetDataFromServer}

This commit is contained in:
Le Chau 2020-06-25 11:46:37 +07:00
parent b6aef4b864
commit 04e74cea45

View File

@ -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<ParkInfo>();
if(parkInfo == null)
parkInfo = await response.Content.ReadAsAsync<ParkInfo>();
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;