127 lines
4.3 KiB
C#
127 lines
4.3 KiB
C#
using System;
|
|
using System.Windows.Forms;
|
|
using System.Threading;
|
|
using Newtonsoft.Json;
|
|
using System.Net.Http;
|
|
|
|
namespace AIParkingApplication
|
|
{
|
|
public partial class Statistic : UserControl
|
|
{
|
|
private readonly HttpClient client;
|
|
private readonly Thread thrStatistics;
|
|
private readonly TimeSpan updateInterval;
|
|
private ParkInfo parkInfo;
|
|
|
|
private Label lblLoaiXe;
|
|
private Label lblXeMay;
|
|
private Label lblOto;
|
|
private Label lblSoLuong;
|
|
private Label lblSoLuongXeMay;
|
|
private Label lblSoLuongOto;
|
|
|
|
public Statistic(string baseAddress, TimeSpan updateInterval)
|
|
{
|
|
InitializeComponent();
|
|
this.updateInterval = updateInterval;
|
|
client = new HttpClient { BaseAddress = new Uri(baseAddress), Timeout = TimeSpan.FromMilliseconds(5000) };
|
|
thrStatistics = new Thread(new ThreadStart(GetStatistic)) { IsBackground = true };
|
|
thrStatistics.Start();
|
|
}
|
|
|
|
private void GetStatistic()
|
|
{
|
|
while (true)
|
|
{
|
|
GetDataFromServer();
|
|
ShowInfo();
|
|
Thread.Sleep(updateInterval);
|
|
}
|
|
}
|
|
|
|
private async void GetDataFromServer()
|
|
{
|
|
try
|
|
{
|
|
HttpResponseMessage response = await client.GetAsync("/api/statistics");
|
|
response.EnsureSuccessStatusCode();
|
|
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)
|
|
{
|
|
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;
|
|
tlpStatisticTable.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;
|
|
|
|
System.Drawing.Font labelFont = new System.Drawing.Font("Microsoft Sans Serif", 11F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, 0);
|
|
lblLoaiXe = new Label() { Text = "Loại xe", Font = labelFont };
|
|
lblXeMay = new Label() { Text = "Xe máy", Font = labelFont };
|
|
lblOto = new Label() { Text = "Ô tô", Font = labelFont };
|
|
lblSoLuong = new Label() { Text = "Số Lượng", Font = labelFont };
|
|
lblSoLuongXeMay = new Label() { Text = "0", Font = labelFont };
|
|
lblSoLuongOto = new Label() { Text = "0", Font = labelFont };
|
|
|
|
tlpStatisticTable.Controls.Add(lblLoaiXe, 0, 0);
|
|
tlpStatisticTable.Controls.Add(lblXeMay, 0, 1);
|
|
tlpStatisticTable.Controls.Add(lblOto, 0, 2);
|
|
tlpStatisticTable.Controls.Add(lblSoLuong, 1, 0);
|
|
tlpStatisticTable.Controls.Add(lblSoLuongXeMay, 1, 1);
|
|
tlpStatisticTable.Controls.Add(lblSoLuongOto, 1, 2);
|
|
|
|
}
|
|
public class ParkInfo
|
|
{
|
|
[JsonProperty("oto")]
|
|
public int NumberOfCar { get; set; }
|
|
|
|
[JsonProperty("moto")]
|
|
public int NumberOfMoto { get; set; }
|
|
|
|
[JsonProperty("total_in")]
|
|
public string TotalIn { get; set; }
|
|
|
|
[JsonProperty("total_out")]
|
|
public string TotalOut { get; set; }
|
|
}
|
|
}
|
|
}
|