232 lines
6.8 KiB
C#
232 lines
6.8 KiB
C#
using Newtonsoft.Json;
|
|
using System;
|
|
using System.Configuration;
|
|
using System.Diagnostics;
|
|
using System.Drawing;
|
|
using System.IO;
|
|
using System.Net;
|
|
using System.Net.NetworkInformation;
|
|
using System.Text.RegularExpressions;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
|
|
namespace AIParkingApplication
|
|
{
|
|
public static class Util
|
|
{
|
|
public static bool IsPingable(string destinationIPAddress, out long pingTime, int timeOut = UtilConstant.PING_TIMEOUT_MS)
|
|
{
|
|
var pingTask = Task.Factory.StartNew(async () =>
|
|
{
|
|
Ping pinger = new Ping();
|
|
try
|
|
{
|
|
PingReply reply = await pinger.SendPingAsync(destinationIPAddress, timeOut);
|
|
return reply;
|
|
}
|
|
catch (Exception)
|
|
{
|
|
return null;
|
|
}
|
|
finally
|
|
{
|
|
pinger.Dispose();
|
|
}
|
|
});
|
|
|
|
var pingResult = pingTask.Result.Result;
|
|
if (pingResult != null && pingResult.Status == IPStatus.Success)
|
|
{
|
|
pingTime = pingResult.RoundtripTime;
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
pingTime = -1;
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public static string GetCurrentMethodName()
|
|
{
|
|
var strackTrace = new StackTrace();
|
|
var sf = strackTrace.GetFrame(1);
|
|
return sf.GetMethod().Name;
|
|
}
|
|
|
|
public static void UpdateImage(this PictureBox pictureBox, Bitmap image)
|
|
{
|
|
if (pictureBox.IsDisposed)
|
|
{
|
|
return;
|
|
}
|
|
|
|
pictureBox.Invoke(new Action(() =>
|
|
{
|
|
pictureBox.Image?.Dispose();
|
|
pictureBox.Image = image;
|
|
}));
|
|
}
|
|
|
|
public static void UpdateLabel(this Label label, string text, Color backColor, Color foreColor)
|
|
{
|
|
if (label.IsDisposed)
|
|
{
|
|
return;
|
|
}
|
|
|
|
label.Invoke(new Action(() =>
|
|
{
|
|
label.Text = text;
|
|
label.BackColor = backColor;
|
|
label.ForeColor = foreColor;
|
|
}));
|
|
}
|
|
|
|
public static void UpdateLabel(this Label label, string text, Color backColor)
|
|
{
|
|
if (label.IsDisposed)
|
|
{
|
|
return;
|
|
}
|
|
|
|
label.Invoke(new Action(() =>
|
|
{
|
|
label.Text = text;
|
|
label.BackColor = backColor;
|
|
}));
|
|
}
|
|
|
|
public static void UpdateLabel(this Label label, string text)
|
|
{
|
|
if (label.IsDisposed)
|
|
{
|
|
return;
|
|
}
|
|
|
|
label.Invoke(new Action(() =>
|
|
{
|
|
label.Text = text;
|
|
}));
|
|
}
|
|
|
|
public static Bitmap ConvertFromBase64Image(string base64ImageString)
|
|
{
|
|
byte[] imageData = Convert.FromBase64String(base64ImageString);
|
|
using (var memoryStream = new MemoryStream(imageData))
|
|
{
|
|
Bitmap image = new Bitmap(memoryStream);
|
|
return image;
|
|
}
|
|
}
|
|
|
|
public static string GetTimeFormatted(this DateTime dateTime)
|
|
{
|
|
return dateTime.ToString(AppConstant.DATETIME_FORMAT);
|
|
}
|
|
|
|
public static void ExecuteCommand(string command)
|
|
{
|
|
var processInfo = new ProcessStartInfo("cmd.exe", "/c " + command);
|
|
processInfo.CreateNoWindow = true;
|
|
processInfo.UseShellExecute = false;
|
|
processInfo.RedirectStandardError = true;
|
|
processInfo.RedirectStandardOutput = true;
|
|
using (var process = Process.Start(processInfo))
|
|
{
|
|
process.OutputDataReceived += (object sender, DataReceivedEventArgs e) => Console.WriteLine("Engine Output >>" + e.Data);
|
|
process.BeginOutputReadLine();
|
|
|
|
process.ErrorDataReceived += (object sender, DataReceivedEventArgs e) => Console.WriteLine("Engine Error >>" + e.Data);
|
|
process.BeginErrorReadLine();
|
|
//process.WaitForExit();
|
|
//Console.WriteLine("ExitCode: {0}", process.ExitCode);
|
|
//process.Close();
|
|
}
|
|
}
|
|
|
|
public static bool IsValidIPAddress(string ipAddress)
|
|
{
|
|
if (string.IsNullOrEmpty(ipAddress))
|
|
{
|
|
return false;
|
|
}
|
|
bool isValidIPAddress = IPAddress.TryParse(ipAddress, out IPAddress ip);
|
|
return isValidIPAddress;
|
|
}
|
|
|
|
public static void ParseHostString(string hostString, out string hostName, out int port)
|
|
{
|
|
if (!hostString.Contains(":"))
|
|
{
|
|
hostName = hostString;
|
|
port = 80;
|
|
return;
|
|
}
|
|
|
|
string[] hostParts = hostString.Split(':');
|
|
if (hostParts.Length != 2)
|
|
{
|
|
hostName = string.Empty;
|
|
port = 0;
|
|
return;
|
|
}
|
|
|
|
hostName = hostParts[0];
|
|
int.TryParse(hostParts[1], out port);
|
|
}
|
|
|
|
public static void AddOrUpdateAppSettings(string key, string value)
|
|
{
|
|
try
|
|
{
|
|
var configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
|
|
var settings = configFile.AppSettings.Settings;
|
|
if (settings[key] == null)
|
|
{
|
|
settings.Add(key, value);
|
|
}
|
|
else
|
|
{
|
|
settings[key].Value = value;
|
|
}
|
|
configFile.Save(ConfigurationSaveMode.Modified);
|
|
ConfigurationManager.RefreshSection(configFile.AppSettings.SectionInformation.Name);
|
|
}
|
|
catch (ConfigurationErrorsException)
|
|
{
|
|
Console.WriteLine("Error writing app settings");
|
|
}
|
|
}
|
|
}
|
|
|
|
public class PlateRequestEngineModel
|
|
{
|
|
[JsonProperty("img64")]
|
|
public string Img64 { get; set; }
|
|
|
|
[JsonProperty("mode")]
|
|
public string Mode { get; set; }
|
|
|
|
[JsonProperty("display")]
|
|
public string Display { get; set; }
|
|
}
|
|
|
|
public class OcrResult
|
|
{
|
|
[JsonProperty("ocr")]
|
|
public string PlateString { get; set; }
|
|
|
|
[JsonProperty("plate")]
|
|
public string PlateImageBase64 { get; set; }
|
|
}
|
|
|
|
public static class UtilConstant
|
|
{
|
|
public const int PING_TIMEOUT_MS = 500;
|
|
|
|
public const string HTTP_POST_METHOD = "POST";
|
|
public const string HTTP_POST_CONTENTTYPE = "application/json";
|
|
}
|
|
}
|