AIParkingApplication/AIParkingApplication/Statistic.cs

96 lines
3.3 KiB
C#

using System;
using System.Windows.Forms;
using System.Threading;
using Newtonsoft.Json;
namespace AIParkingApplication
{
public partial class Statistic : UserControl
{
private readonly Thread thrStatistics;
private readonly TimeSpan updateInterval;
private ParkInfo parkInfo;
private string baseAddress;
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.baseAddress = baseAddress;
this.updateInterval = updateInterval;
thrStatistics = new Thread(new ThreadStart(GetStatistic)) { IsBackground = true };
thrStatistics.Start();
}
private void GetStatistic()
{
while (true)
{
GetDataFromServer();
ShowInfo();
Thread.Sleep(updateInterval);
}
}
async void GetDataFromServer()
{
ApiController apiController = new ApiController(this.baseAddress);
parkInfo = await apiController.GetStatisticInfo();
}
private void ShowInfo()
{
if (parkInfo == null)
{
return;
}
lblSoLuongXeMay.UpdateLabel(parkInfo.NumberOfMoto.ToString());
lblSoLuongOto.UpdateLabel(parkInfo.NumberOfCar.ToString());
lblVehicleTotalIn.UpdateLabel(parkInfo.TotalIn);
lblVehicleTotalOut.UpdateLabel(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; }
}
}
}