Add UserControl: StatusBar

This commit is contained in:
Le Chau 2020-06-18 17:02:29 +07:00
parent b731d24880
commit e2ba2c065d
6 changed files with 358 additions and 12 deletions

View File

@ -94,6 +94,12 @@
<Compile Include="PlateDetector.cs" /> <Compile Include="PlateDetector.cs" />
<Compile Include="Program.cs" /> <Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="StatusBar.cs">
<SubType>UserControl</SubType>
</Compile>
<Compile Include="StatusBar.Designer.cs">
<DependentUpon>StatusBar.cs</DependentUpon>
</Compile>
<Compile Include="Util.cs" /> <Compile Include="Util.cs" />
<EmbeddedResource Include="AIParkingApplicationForm.resx"> <EmbeddedResource Include="AIParkingApplicationForm.resx">
<DependentUpon>AIParkingApplicationForm.cs</DependentUpon> <DependentUpon>AIParkingApplicationForm.cs</DependentUpon>
@ -114,6 +120,9 @@
<DependentUpon>Resources.resx</DependentUpon> <DependentUpon>Resources.resx</DependentUpon>
<DesignTime>True</DesignTime> <DesignTime>True</DesignTime>
</Compile> </Compile>
<EmbeddedResource Include="StatusBar.resx">
<DependentUpon>StatusBar.cs</DependentUpon>
</EmbeddedResource>
<None Include="packages.config" /> <None Include="packages.config" />
<None Include="Properties\Settings.settings"> <None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator> <Generator>SettingsSingleFileGenerator</Generator>

View File

@ -57,7 +57,7 @@ namespace AIParkingApplication
public ActionResult Connect() public ActionResult Connect()
{ {
if (Util.IsPingable(IPAddress)) if (Util.IsPingable(IPAddress, out long replyTime))
{ {
oneTimeConnectedParamHandler = Connect(connectionString); oneTimeConnectedParamHandler = Connect(connectionString);
if (oneTimeConnectedParamHandler != IntPtr.Zero) if (oneTimeConnectedParamHandler != IntPtr.Zero)
@ -137,7 +137,7 @@ namespace AIParkingApplication
doorId = C3Constant.DEFAULT_DOOR_ID; doorId = C3Constant.DEFAULT_DOOR_ID;
cardNumber = C3Constant.DEFAULT_CARD_NUMBER; cardNumber = C3Constant.DEFAULT_CARD_NUMBER;
if (!Util.IsPingable(IPAddress)) if (!Util.IsPingable(IPAddress, out long replyTime))
{ {
isReconnected = false; isReconnected = false;
var result = new ActionResult var result = new ActionResult

125
AIParkingApplication/StatusBar.Designer.cs generated Normal file
View File

@ -0,0 +1,125 @@
namespace AIParkingApplication
{
partial class StatusBar
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
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;
}
}

View File

@ -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; }
}
}
}

View File

@ -0,0 +1,120 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
</root>

View File

@ -2,6 +2,7 @@
using OpenCvSharp; using OpenCvSharp;
using System; using System;
using System.Net; using System.Net;
using System.CodeDom.Compiler;
using System.Net.Http; using System.Net.Http;
using System.Net.NetworkInformation; using System.Net.NetworkInformation;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
@ -12,33 +13,37 @@ namespace AIParkingApplication
{ {
public static class Util 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 () => var pingTask = Task.Factory.StartNew(async () =>
{ {
bool isPingable = false;
Ping pinger = new Ping(); Ping pinger = new Ping();
try try
{ {
PingReply reply = await pinger.SendPingAsync(destinationIPAddress, timeOut); PingReply reply = await pinger.SendPingAsync(destinationIPAddress, timeOut);
isPingable = reply.Status == IPStatus.Success; return reply;
if (!isPingable)
{
// Logging
}
} }
catch (Exception) catch (Exception)
{ {
isPingable = false; return null;
} }
finally finally
{ {
pinger.Dispose(); 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<OcrResult> SendEngineRequestAsync(Mat plateImage, PlateType plateType) public static async Task<OcrResult> SendEngineRequestAsync(Mat plateImage, PlateType plateType)