104 lines
3.8 KiB
C#
104 lines
3.8 KiB
C#
using System;
|
|
using System.Diagnostics;
|
|
using System.Drawing;
|
|
using System.IO;
|
|
using System.Threading;
|
|
using System.Windows.Forms;
|
|
|
|
namespace AIParkingApplication
|
|
{
|
|
public partial class StatusBar : UserControl
|
|
{
|
|
private string webServerIP;
|
|
private string doorAccessControlDeviceIP;
|
|
private TimeSpan updateInterval;
|
|
private const string engineFilePath = @"\engine.bat";
|
|
|
|
public StatusBar(string webServerIP, string doorAccessControlDeviceIP, TimeSpan updateInterval)
|
|
{
|
|
InitializeComponent();
|
|
this.webServerIP = webServerIP;
|
|
this.doorAccessControlDeviceIP = doorAccessControlDeviceIP;
|
|
this.updateInterval = updateInterval;
|
|
Thread thrStatus = new Thread(new ThreadStart(UpdateStatus));
|
|
thrStatus.IsBackground = true;
|
|
thrStatus.Start();
|
|
}
|
|
|
|
private void UpdateStatus()
|
|
{
|
|
while (true)
|
|
{
|
|
if (IsHandleCreated)
|
|
{
|
|
lblDateTime.UpdateLabel(DateTime.Now.GetTimeFormatted());
|
|
|
|
PingResult pingDoorAccessControlResult = GetPingStatus(doorAccessControlDeviceIP);
|
|
lblPingTimeToDoorAccessControl.UpdateLabel($"{pingDoorAccessControlResult.ReplyTime} ms", pingDoorAccessControlResult.BackColor, pingDoorAccessControlResult.ForeColor);
|
|
|
|
PingResult pingWebServerResult = GetPingStatus(webServerIP);
|
|
lblPingTimeServer.UpdateLabel($"{pingWebServerResult.ReplyTime} ms", pingWebServerResult.BackColor, pingWebServerResult.ForeColor);
|
|
|
|
if (Process.GetProcessesByName("tmux").Length == 0)
|
|
{
|
|
string engineBatFilePath = Application.StartupPath + engineFilePath;
|
|
if (File.Exists(engineBatFilePath))
|
|
{
|
|
Util.ExecuteCommand(engineBatFilePath);
|
|
lblEngineStatus.UpdateLabel("DỪNG HOẠT ĐỘNG", Color.Red);
|
|
}
|
|
else
|
|
{
|
|
lblEngineStatus.UpdateLabel("KHÔNG TÌM THẤY FILE", Color.Red);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
lblEngineStatus.UpdateLabel("ĐANG HOẠT ĐỘNG", Color.Green);
|
|
}
|
|
}
|
|
Thread.Sleep(updateInterval);
|
|
}
|
|
}
|
|
|
|
private PingResult GetPingStatus(string ipAddress)
|
|
{
|
|
PingResult pingResult = new PingResult();
|
|
bool isPingable;
|
|
isPingable = Util.IsPingable(ipAddress, 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 ForeColor { get; set; }
|
|
}
|
|
}
|
|
}
|
|
|