Statistic - Refactor

This commit is contained in:
DucDangAnh 2020-07-02 09:43:45 +07:00
parent 2a2c1439b3
commit a8bb5bf67a
2 changed files with 93 additions and 54 deletions

View File

@ -34,12 +34,19 @@
this.lblVehicleTotalOut = new System.Windows.Forms.Label(); this.lblVehicleTotalOut = new System.Windows.Forms.Label();
this.lblVehicleTotalIn = new System.Windows.Forms.Label(); this.lblVehicleTotalIn = new System.Windows.Forms.Label();
this.tlpStatisticTable = new System.Windows.Forms.TableLayoutPanel(); this.tlpStatisticTable = new System.Windows.Forms.TableLayoutPanel();
this.lblLoaiXe = new System.Windows.Forms.Label();
this.lblSoLuong = new System.Windows.Forms.Label();
this.lblXeMay = new System.Windows.Forms.Label();
this.lblSoLuongXeMay = new System.Windows.Forms.Label();
this.lblOto = new System.Windows.Forms.Label();
this.lblSoLuongOto = new System.Windows.Forms.Label();
this.grbLoginInfo = new System.Windows.Forms.GroupBox(); this.grbLoginInfo = new System.Windows.Forms.GroupBox();
this.lblAuthorization = new System.Windows.Forms.Label(); this.lblAuthorization = new System.Windows.Forms.Label();
this.lblUsername = new System.Windows.Forms.Label(); this.lblUsername = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label(); this.label1 = new System.Windows.Forms.Label();
this.grbVehicleStatistics.SuspendLayout(); this.grbVehicleStatistics.SuspendLayout();
this.tlpStatisticTable.SuspendLayout();
this.grbLoginInfo.SuspendLayout(); this.grbLoginInfo.SuspendLayout();
this.SuspendLayout(); this.SuspendLayout();
// //
@ -106,9 +113,16 @@
// //
this.tlpStatisticTable.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) this.tlpStatisticTable.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right))); | System.Windows.Forms.AnchorStyles.Right)));
this.tlpStatisticTable.CellBorderStyle = System.Windows.Forms.TableLayoutPanelCellBorderStyle.Single;
this.tlpStatisticTable.ColumnCount = 2; this.tlpStatisticTable.ColumnCount = 2;
this.tlpStatisticTable.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 33.33333F)); this.tlpStatisticTable.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 33.33333F));
this.tlpStatisticTable.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 33.33333F)); this.tlpStatisticTable.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 33.33333F));
this.tlpStatisticTable.Controls.Add(this.lblLoaiXe, 0, 0);
this.tlpStatisticTable.Controls.Add(this.lblSoLuong, 1, 0);
this.tlpStatisticTable.Controls.Add(this.lblXeMay, 0, 1);
this.tlpStatisticTable.Controls.Add(this.lblSoLuongXeMay, 1, 1);
this.tlpStatisticTable.Controls.Add(this.lblOto, 0, 2);
this.tlpStatisticTable.Controls.Add(this.lblSoLuongOto, 1, 2);
this.tlpStatisticTable.Location = new System.Drawing.Point(6, 22); this.tlpStatisticTable.Location = new System.Drawing.Point(6, 22);
this.tlpStatisticTable.Name = "tlpStatisticTable"; this.tlpStatisticTable.Name = "tlpStatisticTable";
this.tlpStatisticTable.RowCount = 3; this.tlpStatisticTable.RowCount = 3;
@ -118,6 +132,66 @@
this.tlpStatisticTable.Size = new System.Drawing.Size(180, 100); this.tlpStatisticTable.Size = new System.Drawing.Size(180, 100);
this.tlpStatisticTable.TabIndex = 0; this.tlpStatisticTable.TabIndex = 0;
// //
// lblLoaiXe
//
this.lblLoaiXe.Anchor = System.Windows.Forms.AnchorStyles.None;
this.lblLoaiXe.AutoSize = true;
this.lblLoaiXe.Location = new System.Drawing.Point(17, 8);
this.lblLoaiXe.Name = "lblLoaiXe";
this.lblLoaiXe.Size = new System.Drawing.Size(55, 18);
this.lblLoaiXe.TabIndex = 0;
this.lblLoaiXe.Text = "Loại xe";
//
// lblSoLuong
//
this.lblSoLuong.Anchor = System.Windows.Forms.AnchorStyles.None;
this.lblSoLuong.AutoSize = true;
this.lblSoLuong.Location = new System.Drawing.Point(101, 8);
this.lblSoLuong.Name = "lblSoLuong";
this.lblSoLuong.Size = new System.Drawing.Size(67, 18);
this.lblSoLuong.TabIndex = 0;
this.lblSoLuong.Text = "Số lượng";
//
// lblXeMay
//
this.lblXeMay.Anchor = System.Windows.Forms.AnchorStyles.None;
this.lblXeMay.AutoSize = true;
this.lblXeMay.Location = new System.Drawing.Point(16, 41);
this.lblXeMay.Name = "lblXeMay";
this.lblXeMay.Size = new System.Drawing.Size(58, 18);
this.lblXeMay.TabIndex = 0;
this.lblXeMay.Text = "Xe máy";
//
// lblSoLuongXeMay
//
this.lblSoLuongXeMay.Anchor = System.Windows.Forms.AnchorStyles.None;
this.lblSoLuongXeMay.AutoSize = true;
this.lblSoLuongXeMay.Location = new System.Drawing.Point(126, 41);
this.lblSoLuongXeMay.Name = "lblSoLuongXeMay";
this.lblSoLuongXeMay.Size = new System.Drawing.Size(16, 18);
this.lblSoLuongXeMay.TabIndex = 0;
this.lblSoLuongXeMay.Text = "0";
//
// lblOto
//
this.lblOto.Anchor = System.Windows.Forms.AnchorStyles.None;
this.lblOto.AutoSize = true;
this.lblOto.Location = new System.Drawing.Point(28, 74);
this.lblOto.Name = "lblOto";
this.lblOto.Size = new System.Drawing.Size(33, 18);
this.lblOto.TabIndex = 0;
this.lblOto.Text = "Ôtô";
//
// lblSoLuongOto
//
this.lblSoLuongOto.Anchor = System.Windows.Forms.AnchorStyles.None;
this.lblSoLuongOto.AutoSize = true;
this.lblSoLuongOto.Location = new System.Drawing.Point(126, 74);
this.lblSoLuongOto.Name = "lblSoLuongOto";
this.lblSoLuongOto.Size = new System.Drawing.Size(16, 18);
this.lblSoLuongOto.TabIndex = 0;
this.lblSoLuongOto.Text = "0";
//
// grbLoginInfo // grbLoginInfo
// //
this.grbLoginInfo.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) this.grbLoginInfo.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
@ -182,8 +256,9 @@
this.Controls.Add(this.grbVehicleStatistics); this.Controls.Add(this.grbVehicleStatistics);
this.Name = "Statistic"; this.Name = "Statistic";
this.Size = new System.Drawing.Size(193, 292); this.Size = new System.Drawing.Size(193, 292);
this.Load += new System.EventHandler(this.Statistic_Load);
this.grbVehicleStatistics.ResumeLayout(false); this.grbVehicleStatistics.ResumeLayout(false);
this.tlpStatisticTable.ResumeLayout(false);
this.tlpStatisticTable.PerformLayout();
this.grbLoginInfo.ResumeLayout(false); this.grbLoginInfo.ResumeLayout(false);
this.grbLoginInfo.PerformLayout(); this.grbLoginInfo.PerformLayout();
this.ResumeLayout(false); this.ResumeLayout(false);
@ -203,5 +278,11 @@
private System.Windows.Forms.Label label3; private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label lblVehicleTotalOut; private System.Windows.Forms.Label lblVehicleTotalOut;
private System.Windows.Forms.Label lblVehicleTotalIn; private System.Windows.Forms.Label lblVehicleTotalIn;
private System.Windows.Forms.Label lblLoaiXe;
private System.Windows.Forms.Label lblSoLuong;
private System.Windows.Forms.Label lblXeMay;
private System.Windows.Forms.Label lblSoLuongXeMay;
private System.Windows.Forms.Label lblOto;
private System.Windows.Forms.Label lblSoLuongOto;
} }
} }

View File

@ -7,80 +7,38 @@ namespace AIParkingApplication
{ {
public partial class Statistic : UserControl public partial class Statistic : UserControl
{ {
private readonly Thread thrStatistics; private readonly Thread statisticsThread;
private readonly TimeSpan updateInterval; private readonly TimeSpan updateInterval;
private ParkInfo parkInfo;
private ApiController apiController; private ApiController apiController;
private Label lblLoaiXe;
private Label lblXeMay;
private Label lblOto;
private Label lblSoLuong;
private Label lblSoLuongXeMay;
private Label lblSoLuongOto;
public Statistic(ApiController apiController, TimeSpan updateInterval) public Statistic(ApiController apiController, TimeSpan updateInterval)
{ {
InitializeComponent(); InitializeComponent();
this.apiController = apiController; this.apiController = apiController;
this.updateInterval = updateInterval; this.updateInterval = updateInterval;
thrStatistics = new Thread(new ThreadStart(GetStatistic)) { IsBackground = true }; statisticsThread = new Thread(new ThreadStart(GetStatistic)) { IsBackground = true };
thrStatistics.Start(); statisticsThread.Start();
} }
private void GetStatistic() private void GetStatistic()
{ {
while (true) while (true)
{ {
GetDataFromServer(); GetDataUpdate();
ShowInfo();
Thread.Sleep(updateInterval); Thread.Sleep(updateInterval);
} }
} }
async void GetDataFromServer() private async void GetDataUpdate()
{ {
parkInfo = await apiController.GetStatisticInfo(); ParkInfo parkInfo = await apiController.GetStatisticInfo();
} if (parkInfo != null)
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);
} }
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 };
lblLoaiXe.Anchor = AnchorStyles.None;
lblXeMay.Anchor = AnchorStyles.None;
lblOto.Anchor = AnchorStyles.None;
lblSoLuong.Anchor = AnchorStyles.None;
lblSoLuongXeMay.Anchor = AnchorStyles.None;
lblSoLuongOto.Anchor = AnchorStyles.None;
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 public class ParkInfo