From a8bb5bf67a9fae6bc8e4dc95477e860f6e648f90 Mon Sep 17 00:00:00 2001 From: DucDangAnh Date: Thu, 2 Jul 2020 09:43:45 +0700 Subject: [PATCH] Statistic - Refactor --- AIParkingApplication/Statistic.Designer.cs | 83 +++++++++++++++++++++- AIParkingApplication/Statistic.cs | 64 +++-------------- 2 files changed, 93 insertions(+), 54 deletions(-) diff --git a/AIParkingApplication/Statistic.Designer.cs b/AIParkingApplication/Statistic.Designer.cs index 245ee88..5ebd733 100644 --- a/AIParkingApplication/Statistic.Designer.cs +++ b/AIParkingApplication/Statistic.Designer.cs @@ -34,12 +34,19 @@ this.lblVehicleTotalOut = new System.Windows.Forms.Label(); this.lblVehicleTotalIn = new System.Windows.Forms.Label(); 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.lblAuthorization = new System.Windows.Forms.Label(); this.lblUsername = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label(); this.label1 = new System.Windows.Forms.Label(); this.grbVehicleStatistics.SuspendLayout(); + this.tlpStatisticTable.SuspendLayout(); this.grbLoginInfo.SuspendLayout(); this.SuspendLayout(); // @@ -106,9 +113,16 @@ // this.tlpStatisticTable.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); + this.tlpStatisticTable.CellBorderStyle = System.Windows.Forms.TableLayoutPanelCellBorderStyle.Single; 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.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.Name = "tlpStatisticTable"; this.tlpStatisticTable.RowCount = 3; @@ -118,6 +132,66 @@ this.tlpStatisticTable.Size = new System.Drawing.Size(180, 100); 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 // 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.Name = "Statistic"; this.Size = new System.Drawing.Size(193, 292); - this.Load += new System.EventHandler(this.Statistic_Load); this.grbVehicleStatistics.ResumeLayout(false); + this.tlpStatisticTable.ResumeLayout(false); + this.tlpStatisticTable.PerformLayout(); this.grbLoginInfo.ResumeLayout(false); this.grbLoginInfo.PerformLayout(); this.ResumeLayout(false); @@ -203,5 +278,11 @@ private System.Windows.Forms.Label label3; private System.Windows.Forms.Label lblVehicleTotalOut; 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; } } diff --git a/AIParkingApplication/Statistic.cs b/AIParkingApplication/Statistic.cs index 3646c05..a3a0c86 100644 --- a/AIParkingApplication/Statistic.cs +++ b/AIParkingApplication/Statistic.cs @@ -7,80 +7,38 @@ namespace AIParkingApplication { public partial class Statistic : UserControl { - private readonly Thread thrStatistics; + private readonly Thread statisticsThread; private readonly TimeSpan updateInterval; - private ParkInfo parkInfo; 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) { InitializeComponent(); this.apiController = apiController; this.updateInterval = updateInterval; - thrStatistics = new Thread(new ThreadStart(GetStatistic)) { IsBackground = true }; - thrStatistics.Start(); + statisticsThread = new Thread(new ThreadStart(GetStatistic)) { IsBackground = true }; + statisticsThread.Start(); } private void GetStatistic() { while (true) { - GetDataFromServer(); - ShowInfo(); + GetDataUpdate(); Thread.Sleep(updateInterval); } } - async void GetDataFromServer() + private async void GetDataUpdate() { - parkInfo = await apiController.GetStatisticInfo(); - } - - private void ShowInfo() - { - if (parkInfo == null) + ParkInfo parkInfo = await apiController.GetStatisticInfo(); + 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