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 HttpClient client;
private readonly Thread thrStatistics; private readonly Thread thrStatistics;
private readonly int timeCycle; private readonly TimeSpan updateInterval;
private ParkInfo parkInfo;
private Label lblLoaiXe; private Label lblLoaiXe;
private Label lblXeMay; private Label lblXeMay;
@ -19,10 +20,10 @@ namespace AIParkingApplication
private Label lblSoLuongXeMay; private Label lblSoLuongXeMay;
private Label lblSoLuongOto; private Label lblSoLuongOto;
public Statistic(string baseAddress, int timeCycle_Sec = 1) public Statistic(string baseAddress, TimeSpan updateInterval)
{ {
InitializeComponent(); InitializeComponent();
timeCycle = timeCycle_Sec; ; this.updateInterval = updateInterval;
client = new HttpClient { BaseAddress = new Uri(baseAddress), Timeout = TimeSpan.FromMilliseconds(5000) }; client = new HttpClient { BaseAddress = new Uri(baseAddress), Timeout = TimeSpan.FromMilliseconds(5000) };
thrStatistics = new Thread(new ThreadStart(GetStatistic)) { IsBackground = true }; thrStatistics = new Thread(new ThreadStart(GetStatistic)) { IsBackground = true };
thrStatistics.Start(); thrStatistics.Start();
@ -33,7 +34,8 @@ namespace AIParkingApplication
while (true) while (true)
{ {
GetDataFromServer(); GetDataFromServer();
Thread.Sleep(timeCycle * 1000); ShowInfo();
Thread.Sleep(updateInterval);
} }
} }
@ -43,7 +45,25 @@ namespace AIParkingApplication
{ {
HttpResponseMessage response = await client.GetAsync("/api/statistics"); HttpResponseMessage response = await client.GetAsync("/api/statistics");
response.EnsureSuccessStatusCode(); response.EnsureSuccessStatusCode();
var parkInfo = await response.Content.ReadAsAsync<ParkInfo>(); parkInfo = await response.Content.ReadAsAsync<ParkInfo>();
if (string.IsNullOrEmpty(parkInfo.TotalIn))
{
parkInfo.TotalIn = "0";
}
if (string.IsNullOrEmpty(parkInfo.TotalOut))
{
parkInfo.TotalOut = "0";
}
}
catch (Exception ex)
{
Console.WriteLine($"SendApiStatisticRequest : {ex.Message}");
}
}
private void ShowInfo()
{
if (parkInfo == null) if (parkInfo == null)
{ {
return; return;
@ -58,27 +78,14 @@ namespace AIParkingApplication
})); }));
lblVehicleTotalIn.Invoke(new Action(() => lblVehicleTotalIn.Invoke(new Action(() =>
{ {
if (string.IsNullOrEmpty(parkInfo.TotalIn))
{
parkInfo.TotalIn = "0";
}
lblVehicleTotalIn.Text = parkInfo.TotalIn; lblVehicleTotalIn.Text = parkInfo.TotalIn;
})); }));
lblVehicleTotalOut.Invoke(new Action(() => lblVehicleTotalOut.Invoke(new Action(() =>
{ {
if (string.IsNullOrEmpty(parkInfo.TotalOut))
{
parkInfo.TotalOut = "0";
}
lblVehicleTotalOut.Text = parkInfo.TotalOut; lblVehicleTotalOut.Text = parkInfo.TotalOut;
})); }));
}
}
catch (Exception ex)
{
Console.WriteLine($"SendApiStatisticRequest : {ex.Message}");
}
}
private void Statistic_Load(object sender, EventArgs e) private void Statistic_Load(object sender, EventArgs e)
{ {