91 lines
3.0 KiB
C#
91 lines
3.0 KiB
C#
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)
|
|
{
|
|
if (IsHandleCreated)
|
|
{
|
|
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; }
|
|
}
|
|
}
|
|
}
|
|
|