This commit is contained in:
DucDangAnh 2020-06-25 13:39:42 +07:00
commit 059e79924c
2 changed files with 50 additions and 32 deletions

View File

@ -9,7 +9,5 @@
#endregion
public const string CAMERA_FAILED_IMAGE_PATH = @"Images\CantConnectCamera.jpg";
public const int TIME_TO_GET_STATISTIC_INFO = 60;
}
}

View File

@ -10,18 +10,21 @@ namespace AIParkingApplication
{
private readonly HttpClient client;
private readonly Thread thrStatistics;
private readonly TimeSpan updateInterval;
private ParkInfo parkInfo;
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)
public Statistic(string baseAddress, TimeSpan updateInterval)
{
InitializeComponent();
client = new HttpClient { BaseAddress = new Uri(apiUrl), Timeout = TimeSpan.FromMilliseconds(5000) };
this.updateInterval = updateInterval;
client = new HttpClient { BaseAddress = new Uri(baseAddress), Timeout = TimeSpan.FromMilliseconds(5000) };
thrStatistics = new Thread(new ThreadStart(GetStatistic)) { IsBackground = true };
thrStatistics.Start();
}
@ -30,35 +33,28 @@ namespace AIParkingApplication
{
while (true)
{
GETDataFromServer();
Thread.Sleep(AppConstant.TIME_TO_GET_STATISTIC_INFO * 1000);
GetDataFromServer();
ShowInfo();
Thread.Sleep(updateInterval);
}
}
private async void GETDataFromServer()
private async void GetDataFromServer()
{
try
{
HttpResponseMessage response = await client.GetAsync("/api/statistics");
response.EnsureSuccessStatusCode();
var vehicle = await response.Content.ReadAsAsync<Vehicle>();
lblSoLuongXeMay?.Invoke(new Action(() =>
parkInfo = await response.Content.ReadAsAsync<ParkInfo>();
if (string.IsNullOrEmpty(parkInfo.TotalIn))
{
lblSoLuongXeMay.Text = vehicle.NumberOfMoto.ToString();
}));
lblSoLuongOto?.Invoke(new Action(() =>
parkInfo.TotalIn = "0";
}
if (string.IsNullOrEmpty(parkInfo.TotalOut))
{
lblSoLuongOto.Text = vehicle.NumberOfCar.ToString();
}));
lblVehicleTotalIn?.Invoke(new Action(() =>
{
lblVehicleTotalIn.Text = vehicle.TotalIn;
}));
lblVehicleTotalOut?.Invoke(new Action(() =>
{
lblVehicleTotalOut.Text = vehicle.TotelOut;
}));
parkInfo.TotalOut = "0";
}
}
catch (Exception ex)
{
@ -66,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;
@ -79,7 +100,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);
@ -88,7 +108,7 @@ namespace AIParkingApplication
tlpStatisticTable.Controls.Add(lblSoLuongOto, 1, 2);
}
public class Vehicle
public class ParkInfo
{
[JsonProperty("oto")]
public int NumberOfCar { get; set; }
@ -100,7 +120,7 @@ namespace AIParkingApplication
public string TotalIn { get; set; }
[JsonProperty("total_out")]
public string TotelOut { get; set; }
public string TotalOut { get; set; }
}
}
}