From e2ba2c065d016e357f76fbeca2d2d890354b6aba Mon Sep 17 00:00:00 2001 From: Le Chau Date: Thu, 18 Jun 2020 17:02:29 +0700 Subject: [PATCH] Add UserControl: StatusBar --- .../AIParkingApplication.csproj | 9 ++ AIParkingApplication/C3DeviceController.cs | 4 +- AIParkingApplication/StatusBar.Designer.cs | 125 ++++++++++++++++++ AIParkingApplication/StatusBar.cs | 87 ++++++++++++ AIParkingApplication/StatusBar.resx | 120 +++++++++++++++++ AIParkingApplication/Util.cs | 25 ++-- 6 files changed, 358 insertions(+), 12 deletions(-) create mode 100644 AIParkingApplication/StatusBar.Designer.cs create mode 100644 AIParkingApplication/StatusBar.cs create mode 100644 AIParkingApplication/StatusBar.resx diff --git a/AIParkingApplication/AIParkingApplication.csproj b/AIParkingApplication/AIParkingApplication.csproj index 345aee6..4a62757 100644 --- a/AIParkingApplication/AIParkingApplication.csproj +++ b/AIParkingApplication/AIParkingApplication.csproj @@ -94,6 +94,12 @@ + + UserControl + + + StatusBar.cs + AIParkingApplicationForm.cs @@ -114,6 +120,9 @@ Resources.resx True + + StatusBar.cs + SettingsSingleFileGenerator diff --git a/AIParkingApplication/C3DeviceController.cs b/AIParkingApplication/C3DeviceController.cs index 9f8c2e2..b251667 100644 --- a/AIParkingApplication/C3DeviceController.cs +++ b/AIParkingApplication/C3DeviceController.cs @@ -57,7 +57,7 @@ namespace AIParkingApplication public ActionResult Connect() { - if (Util.IsPingable(IPAddress)) + if (Util.IsPingable(IPAddress, out long replyTime)) { oneTimeConnectedParamHandler = Connect(connectionString); if (oneTimeConnectedParamHandler != IntPtr.Zero) @@ -137,7 +137,7 @@ namespace AIParkingApplication doorId = C3Constant.DEFAULT_DOOR_ID; cardNumber = C3Constant.DEFAULT_CARD_NUMBER; - if (!Util.IsPingable(IPAddress)) + if (!Util.IsPingable(IPAddress, out long replyTime)) { isReconnected = false; var result = new ActionResult diff --git a/AIParkingApplication/StatusBar.Designer.cs b/AIParkingApplication/StatusBar.Designer.cs new file mode 100644 index 0000000..3bf2165 --- /dev/null +++ b/AIParkingApplication/StatusBar.Designer.cs @@ -0,0 +1,125 @@ +namespace AIParkingApplication +{ + partial class StatusBar + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Component Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.lblPingTimeServer = new System.Windows.Forms.Label(); + this.lblPingServer = new System.Windows.Forms.Label(); + this.lblDateTime = new System.Windows.Forms.Label(); + this.lblPingC3200 = new System.Windows.Forms.Label(); + this.lblPingTimeC3 = new System.Windows.Forms.Label(); + this.label1 = new System.Windows.Forms.Label(); + this.SuspendLayout(); + // + // lblPingTimeServer + // + this.lblPingTimeServer.Font = new System.Drawing.Font("Microsoft Sans Serif", 11F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.lblPingTimeServer.Location = new System.Drawing.Point(181, 2); + this.lblPingTimeServer.Name = "lblPingTimeServer"; + this.lblPingTimeServer.Size = new System.Drawing.Size(75, 23); + this.lblPingTimeServer.TabIndex = 0; + this.lblPingTimeServer.Text = "1ms"; + this.lblPingTimeServer.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; + // + // lblPingServer + // + this.lblPingServer.AutoSize = true; + this.lblPingServer.Font = new System.Drawing.Font("Microsoft Sans Serif", 11F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.lblPingServer.Location = new System.Drawing.Point(128, 4); + this.lblPingServer.Name = "lblPingServer"; + this.lblPingServer.Size = new System.Drawing.Size(51, 18); + this.lblPingServer.TabIndex = 1; + this.lblPingServer.Text = "Server"; + // + // lblDateTime + // + this.lblDateTime.AutoSize = true; + this.lblDateTime.Font = new System.Drawing.Font("Microsoft Sans Serif", 11F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.lblDateTime.Location = new System.Drawing.Point(597, 4); + this.lblDateTime.Name = "lblDateTime"; + this.lblDateTime.Size = new System.Drawing.Size(140, 18); + this.lblDateTime.TabIndex = 2; + this.lblDateTime.Text = "17/06/2020 16:54:43"; + // + // lblPingC3200 + // + this.lblPingC3200.AutoSize = true; + this.lblPingC3200.Font = new System.Drawing.Font("Microsoft Sans Serif", 11F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.lblPingC3200.Location = new System.Drawing.Point(308, 4); + this.lblPingC3200.Name = "lblPingC3200"; + this.lblPingC3200.Size = new System.Drawing.Size(109, 18); + this.lblPingC3200.TabIndex = 3; + this.lblPingC3200.Text = "Thiết bị mở cửa"; + // + // lblPingTimeC3 + // + this.lblPingTimeC3.Font = new System.Drawing.Font("Microsoft Sans Serif", 11F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.lblPingTimeC3.Location = new System.Drawing.Point(436, 2); + this.lblPingTimeC3.Name = "lblPingTimeC3"; + this.lblPingTimeC3.Size = new System.Drawing.Size(75, 23); + this.lblPingTimeC3.TabIndex = 3; + this.lblPingTimeC3.Text = "500ms"; + this.lblPingTimeC3.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; + // + // label1 + // + this.label1.AutoSize = true; + this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 11F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.label1.Location = new System.Drawing.Point(3, 4); + this.label1.Name = "label1"; + this.label1.Size = new System.Drawing.Size(125, 18); + this.label1.TabIndex = 1; + this.label1.Text = "Trạng thái kết nối:"; + // + // StatusBar + // + this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.Controls.Add(this.lblPingTimeC3); + this.Controls.Add(this.lblPingC3200); + this.Controls.Add(this.lblDateTime); + this.Controls.Add(this.label1); + this.Controls.Add(this.lblPingServer); + this.Controls.Add(this.lblPingTimeServer); + this.Name = "StatusBar"; + this.Size = new System.Drawing.Size(740, 25); + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private System.Windows.Forms.Label lblPingTimeServer; + private System.Windows.Forms.Label lblPingServer; + private System.Windows.Forms.Label lblDateTime; + private System.Windows.Forms.Label lblPingC3200; + private System.Windows.Forms.Label lblPingTimeC3; + private System.Windows.Forms.Label label1; + } +} diff --git a/AIParkingApplication/StatusBar.cs b/AIParkingApplication/StatusBar.cs new file mode 100644 index 0000000..24f0397 --- /dev/null +++ b/AIParkingApplication/StatusBar.cs @@ -0,0 +1,87 @@ +using System; +using System.Drawing; +using System.Windows.Forms; +using System.Threading; + +namespace AIParkingApplication +{ + public partial class StatusBar : UserControl + { + private string webServerIP; + private string c3IP; + public StatusBar(string webServerIP, string c3IP) + { + InitializeComponent(); + this.webServerIP = webServerIP; + this.c3IP = c3IP; + Thread thrStatus = new Thread(new ThreadStart(UpdateStatus)); + thrStatus.IsBackground = true; + thrStatus.Start(); + } + + private void UpdateStatus() + { + while (true) + { + lblDateTime.Invoke(new Action(() => + { + lblDateTime.Text = DateTime.Now.ToString(AppConstant.DATETIME_FORMAT); + })); + + lblPingTimeC3.Invoke(new Action(() => + { + PingResult pingResult = GetPingStatus(c3IP); + lblPingTimeC3.Text = $"{pingResult.ReplyTime} ms"; + lblPingTimeC3.BackColor = pingResult.BackColor; + lblPingTimeC3.ForeColor = pingResult.ForceColor; + })); + + lblPingTimeServer.Invoke(new Action(() => + { + PingResult pingResult = GetPingStatus(webServerIP); + lblPingTimeServer.Text = $"{pingResult.ReplyTime} ms"; + lblPingTimeServer.BackColor = pingResult.BackColor; + lblPingTimeServer.ForeColor = pingResult.ForceColor; + })); + Thread.Sleep(1000); + } + } + + private PingResult GetPingStatus(string ip) + { + PingResult pingResult = new PingResult(); + bool isPingable; + isPingable = Util.IsPingable(ip, out long replyTime); + if (isPingable) + switch (replyTime) + { + case long n when (n >= 0 && n < 1): + pingResult.ReplyTime = 1; + pingResult.BackColor = Color.Green; + break; + case long n when (n >= 1 && n < 50): + pingResult.ReplyTime = replyTime; + pingResult.BackColor = Color.Green; + break; + case long n when (n >= 50): + pingResult.ReplyTime = replyTime; + pingResult.BackColor = Color.Red; + break; + } + else + { + pingResult.ReplyTime = 0; + pingResult.BackColor = Color.Gray; + } + return pingResult; + } + + private class PingResult + { + public long ReplyTime { get; set; } + public Color BackColor { get; set; } + public Color ForceColor { get; set; } + } + } +} + diff --git a/AIParkingApplication/StatusBar.resx b/AIParkingApplication/StatusBar.resx new file mode 100644 index 0000000..1af7de1 --- /dev/null +++ b/AIParkingApplication/StatusBar.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/AIParkingApplication/Util.cs b/AIParkingApplication/Util.cs index 37eb7b4..0fd440f 100644 --- a/AIParkingApplication/Util.cs +++ b/AIParkingApplication/Util.cs @@ -2,6 +2,7 @@ using OpenCvSharp; using System; using System.Net; +using System.CodeDom.Compiler; using System.Net.Http; using System.Net.NetworkInformation; using System.Runtime.InteropServices; @@ -12,33 +13,37 @@ namespace AIParkingApplication { public static class Util { - public static bool IsPingable(string destinationIPAddress, int timeOut = UtilConstant.PING_TIMEOUT_MS) + public static bool IsPingable(string destinationIPAddress, out long pingTime, int timeOut = UtilConstant.PING_TIMEOUT_MS) { var pingTask = Task.Factory.StartNew(async () => { - bool isPingable = false; Ping pinger = new Ping(); try { PingReply reply = await pinger.SendPingAsync(destinationIPAddress, timeOut); - isPingable = reply.Status == IPStatus.Success; - if (!isPingable) - { - // Logging - } + return reply; } catch (Exception) { - isPingable = false; + return null; } finally { pinger.Dispose(); } - return isPingable; }); - return pingTask.Result.Result; + var pingResult = pingTask.Result.Result; + if (pingResult != null && pingResult.Status == IPStatus.Success) + { + pingTime = pingResult.RoundtripTime; + return true; + } + else + { + pingTime = -1; + return false; + } } public static async Task SendEngineRequestAsync(Mat plateImage, PlateType plateType)