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 #endregion
public const string CAMERA_FAILED_IMAGE_PATH = @"Images\CantConnectCamera.jpg"; 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 HttpClient client;
private readonly Thread thrStatistics; private readonly Thread thrStatistics;
private readonly TimeSpan updateInterval;
private ParkInfo parkInfo;
Label lblLoaiXe; private Label lblLoaiXe;
Label lblXeMay; private Label lblXeMay;
Label lblOto; private Label lblOto;
Label lblSoLuong; private Label lblSoLuong;
Label lblSoLuongXeMay; private Label lblSoLuongXeMay;
Label lblSoLuongOto; private Label lblSoLuongOto;
public Statistic(string apiUrl) public Statistic(string baseAddress, TimeSpan updateInterval)
{ {
InitializeComponent(); 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 = new Thread(new ThreadStart(GetStatistic)) { IsBackground = true };
thrStatistics.Start(); thrStatistics.Start();
} }
@ -30,35 +33,28 @@ namespace AIParkingApplication
{ {
while (true) while (true)
{ {
GETDataFromServer(); GetDataFromServer();
Thread.Sleep(AppConstant.TIME_TO_GET_STATISTIC_INFO * 1000); ShowInfo();
Thread.Sleep(updateInterval);
} }
} }
private async void GETDataFromServer() private async void GetDataFromServer()
{ {
try try
{ {
HttpResponseMessage response = await client.GetAsync("/api/statistics"); HttpResponseMessage response = await client.GetAsync("/api/statistics");
response.EnsureSuccessStatusCode(); response.EnsureSuccessStatusCode();
var vehicle = await response.Content.ReadAsAsync<Vehicle>(); parkInfo = await response.Content.ReadAsAsync<ParkInfo>();
lblSoLuongXeMay?.Invoke(new Action(() =>
if (string.IsNullOrEmpty(parkInfo.TotalIn))
{ {
lblSoLuongXeMay.Text = vehicle.NumberOfMoto.ToString(); parkInfo.TotalIn = "0";
})); }
lblSoLuongOto?.Invoke(new Action(() => if (string.IsNullOrEmpty(parkInfo.TotalOut))
{ {
lblSoLuongOto.Text = vehicle.NumberOfCar.ToString(); parkInfo.TotalOut = "0";
})); }
lblVehicleTotalIn?.Invoke(new Action(() =>
{
lblVehicleTotalIn.Text = vehicle.TotalIn;
}));
lblVehicleTotalOut?.Invoke(new Action(() =>
{
lblVehicleTotalOut.Text = vehicle.TotelOut;
}));
} }
catch (Exception ex) 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) private void Statistic_Load(object sender, EventArgs e)
{ {
tlpStatisticTable.BorderStyle = BorderStyle.FixedSingle; tlpStatisticTable.BorderStyle = BorderStyle.FixedSingle;
@ -79,7 +100,6 @@ namespace AIParkingApplication
lblSoLuongXeMay = new Label() { Text = "0", Font = labelFont }; lblSoLuongXeMay = new Label() { Text = "0", Font = labelFont };
lblSoLuongOto = 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(lblLoaiXe, 0, 0);
tlpStatisticTable.Controls.Add(lblXeMay, 0, 1); tlpStatisticTable.Controls.Add(lblXeMay, 0, 1);
tlpStatisticTable.Controls.Add(lblOto, 0, 2); tlpStatisticTable.Controls.Add(lblOto, 0, 2);
@ -88,7 +108,7 @@ namespace AIParkingApplication
tlpStatisticTable.Controls.Add(lblSoLuongOto, 1, 2); tlpStatisticTable.Controls.Add(lblSoLuongOto, 1, 2);
} }
public class Vehicle public class ParkInfo
{ {
[JsonProperty("oto")] [JsonProperty("oto")]
public int NumberOfCar { get; set; } public int NumberOfCar { get; set; }
@ -100,7 +120,7 @@ namespace AIParkingApplication
public string TotalIn { get; set; } public string TotalIn { get; set; }
[JsonProperty("total_out")] [JsonProperty("total_out")]
public string TotelOut { get; set; } public string TotalOut { get; set; }
} }
} }
} }