AIParkingApplication/AIParkingApplication/AIParkingApplicationForm.cs

309 lines
13 KiB
C#

using NLog;
using System;
using System.Configuration;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
namespace AIParkingApplication
{
public partial class AIParkingApplicationForm : Form
{
private const string CURRENT_LANE_SETTING_KEY = "CURRENT_LANE_SETTING";
private ApiController apiController;
private IDoorControlAccess c3Device;
private LaneIn laneIn12;
private LaneIn laneIn56;
private LaneOut laneOut34;
private LaneOut laneOut78;
private StatusBar statusBar;
private Sidebar sidebar;
private Config configOnWeb;
private EngineApiController engineApiController;
private string doorAccessControlDeviceIP;
private Logger appLogger;
public AIParkingApplicationForm(ApiController apiController, string serverIPAddress, Config configOnWeb, Logger appLogger)
{
InitializeComponent();
this.apiController = apiController;
this.configOnWeb = configOnWeb;
this.appLogger = appLogger;
ReadAccessControlDeviceIPConfigurationFile();
if (string.IsNullOrEmpty(doorAccessControlDeviceIP) || !Util.IsValidIPAddress(doorAccessControlDeviceIP))
{
appLogger.Log(LogLevel.Error, "Kiểm tra lại cấu hình IP thiết bị mở cửa!");
MessageBox.Show("Kiểm tra lại cấu hình IP thiết bị mở cửa! (C3200)", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
Application.Exit();
}
c3Device = new C3DeviceController(appLogger, doorAccessControlDeviceIP);
sidebar = new Sidebar(apiController)
{
Location = new Point(0, 0)
};
Controls.Add(sidebar);
statusBar = new StatusBar(serverIPAddress, doorAccessControlDeviceIP)
{
Location = new Point(0, sidebar.Location.Y + sidebar.Height + 26),
Anchor = AnchorStyles.Bottom | AnchorStyles.Left
};
Controls.Add(statusBar);
try
{
engineApiController = new EngineApiController($"http://{this.configOnWeb.APIPath.ApiPlateRecognize.IP}:{this.configOnWeb.APIPath.ApiPlateRecognize.Port}");
}
catch (Exception ex)
{
appLogger.Log(LogLevel.Error, $"Cấu hình URL của Engine nhận diện biển số lỗi. exMessage: {ex.Message}");
MessageBox.Show("Cấu hình API Plate Recognize lỗi!", "Cấu hình API Engine lỗi!", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
Application.ThreadException += Application_ThreadException;
}
private void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
{
appLogger.Log(LogLevel.Error, $"Application.ThreadException. ex:{e.Exception.Message}");
}
private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
appLogger.Log(LogLevel.Error, $"AppDomain.CurrentDomain.UnhandledException. ex:{(e.ExceptionObject as Exception).Message}");
}
private void InitAllLanes()
{
laneIn12 = new LaneIn(10, configOnWeb.CameraData1.Id.ToString(), configOnWeb.CameraData1.StreamUrl, configOnWeb.CameraData2.StreamUrl, c3Device, apiController, engineApiController, appLogger, true, false, true);
laneIn12.BorderStyle = BorderStyle.FixedSingle;
laneIn12.Hide();
Controls.Add(laneIn12);
laneOut34 = new LaneOut(20, configOnWeb.CameraData3.Id.ToString(), configOnWeb.CameraData3.StreamUrl, configOnWeb.CameraData4.StreamUrl, c3Device, apiController, engineApiController, appLogger, true, false, true);
laneOut34.BorderStyle = BorderStyle.FixedSingle;
laneOut34.Hide();
Controls.Add(laneOut34);
laneIn56 = new LaneIn(10, configOnWeb.CameraData5.Id.ToString(), configOnWeb.CameraData5.StreamUrl, configOnWeb.CameraData6.StreamUrl, c3Device, apiController, engineApiController, appLogger, true, false, true);
laneIn56.BorderStyle = BorderStyle.FixedSingle;
laneIn56.Hide();
Controls.Add(laneIn56);
laneOut78 = new LaneOut(20, configOnWeb.CameraData7.Id.ToString(), configOnWeb.CameraData7.StreamUrl, configOnWeb.CameraData8.StreamUrl, c3Device, apiController, engineApiController, appLogger, true, false, true);
laneOut78.BorderStyle = BorderStyle.FixedSingle;
laneOut78.Hide();
Controls.Add(laneOut78);
}
private void UpdateLaneInOut()
{
laneIn12.DoorId = 1;
laneIn12.Location = new Point(sidebar.Location.X + sidebar.Width + 20, menuStrip.Height);
laneIn12.Show();
laneIn12.Start();
laneOut34.DoorId = 2;
laneOut34.Location = new Point(laneIn12.Location.X + laneIn12.Width + 20, menuStrip.Height);
laneOut34.Show();
laneOut34.Start();
}
private void UpdateLaneInIn()
{
laneIn12.DoorId = 1;
laneIn12.Location = new Point(sidebar.Location.X + sidebar.Width + 20, menuStrip.Height);
laneIn12.Show();
laneIn12.Start();
laneIn56.DoorId = 2;
laneIn56.Location = new Point(laneIn12.Location.X + laneIn12.Width + 20, menuStrip.Height);
laneIn56.Show();
laneIn56.Start();
}
private void UpdateLaneOutIn()
{
laneOut78.DoorId = 1;
laneOut78.Location = new Point(sidebar.Location.X + sidebar.Width + 20, menuStrip.Height);
laneOut78.Show();
laneOut78.Start();
laneIn56.DoorId = 2;
laneIn56.Location = new Point(laneOut78.Location.X + laneOut78.Width + 20, menuStrip.Height);
laneIn56.Show();
laneIn56.Start();
}
private void UpdateLaneOutOut()
{
laneOut78.DoorId = 1;
laneOut78.Location = new Point(sidebar.Location.X + sidebar.Width + 20, menuStrip.Height);
laneOut78.Show();
laneOut78.Start();
laneOut34.DoorId = 2;
laneOut34.Location = new Point(laneOut78.Location.X + laneOut78.Width + 20, menuStrip.Height);
laneOut34.Show();
laneOut34.Start();
}
private void toolStripMenuItemSwitchLaneInIn_Click(object sender, EventArgs e)
{
Util.UpsertAppSettings(CURRENT_LANE_SETTING_KEY, "IN-IN");
UpdateMenuStripItemStatus(sender as ToolStripMenuItem);
StopAllLanes();
UpdateLaneInIn();
appLogger.Log(LogLevel.Info, "Chuyển làn: IN-IN");
}
private void toolStripMenuItemSwitchLaneInOut_Click(object sender, EventArgs e)
{
Util.UpsertAppSettings(CURRENT_LANE_SETTING_KEY, "IN-OUT");
UpdateMenuStripItemStatus(sender as ToolStripMenuItem);
StopAllLanes();
UpdateLaneInOut();
appLogger.Log(LogLevel.Info, "Chuyển làn: IN-OUT");
}
private void toolStripMenuItemSwitchLaneOutIn_Click(object sender, EventArgs e)
{
Util.UpsertAppSettings(CURRENT_LANE_SETTING_KEY, "OUT-IN");
UpdateMenuStripItemStatus(sender as ToolStripMenuItem);
StopAllLanes();
UpdateLaneOutIn();
appLogger.Log(LogLevel.Info, "Chuyển làn: OUT-IN");
}
private void toolStripMenuItemSwitchLaneOutOut_Click(object sender, EventArgs e)
{
Util.UpsertAppSettings(CURRENT_LANE_SETTING_KEY, "OUT-OUT");
UpdateMenuStripItemStatus(sender as ToolStripMenuItem);
StopAllLanes();
UpdateLaneOutOut();
appLogger.Log(LogLevel.Info, "Chuyển làn: OUT-OUT");
}
private void UpdateMenuStripItemStatus(ToolStripMenuItem selectedToolStripMenuItem)
{
if (selectedToolStripMenuItem != null)
{
((ToolStripMenuItem)selectedToolStripMenuItem.OwnerItem).DropDownItems
.OfType<ToolStripMenuItem>().ToList()
.ForEach(item =>
{
item.Checked = false;
item.Enabled = true;
});
selectedToolStripMenuItem.Checked = true;
selectedToolStripMenuItem.Enabled = false;
}
}
private void StopAllLanes()
{
laneIn12.DoorId = 10;
laneIn56.DoorId = 10;
laneOut34.DoorId = 10;
laneOut78.DoorId = 10;
laneIn12?.Stop();
laneIn56?.Stop();
laneOut34?.Stop();
laneOut78?.Stop();
laneIn12?.Hide();
laneIn56?.Hide();
laneOut34?.Hide();
laneOut78?.Hide();
}
private void InitLaneFirstTime()
{
try
{
string lanesConfig = ConfigurationManager.AppSettings[CURRENT_LANE_SETTING_KEY];
if (!string.IsNullOrEmpty(lanesConfig))
{
string[] lanes = lanesConfig.Split('-');
if (lanes.Length == 2)
{
string lane0 = lanes[0].ToLower();
string lane1 = lanes[1].ToLower();
if (lane0 == "in" && lane1 == "in")
{
UpdateLaneInIn();
toolStripMenuItemSwitchLaneInIn.DisableSelected();
appLogger.Log(LogLevel.Info, $"Khởi động App sử dụng lane: IN-IN");
}
if (lane0 == "in" && lane1 == "out")
{
UpdateLaneInOut();
toolStripMenuItemSwitchLaneInOut.DisableSelected();
appLogger.Log(LogLevel.Info, $"Khởi động App sử dụng lane: IN-OUT");
}
if (lane0 == "out" && lane1 == "in")
{
UpdateLaneOutIn();
toolStripMenuItemSwitchLaneOutIn.DisableSelected();
appLogger.Log(LogLevel.Info, $"Khởi động App sử dụng lane: OUT-IN");
}
if (lane0 == "out" && lane1 == "out")
{
UpdateLaneOutOut();
toolStripMenuItemSwitchLaneOutOut.DisableSelected();
appLogger.Log(LogLevel.Info, $"Khởi động App sử dụng lane: OUT-OUT");
}
}
else
{
Util.UpsertAppSettings(CURRENT_LANE_SETTING_KEY, "IN-OUT");
UpdateLaneInOut();
toolStripMenuItemSwitchLaneInOut.DisableSelected();
appLogger.Log(LogLevel.Info, $"Khởi động App đọc config không đủ 2 param: IN-OUT");
}
}
}
catch (Exception ex)
{
Util.UpsertAppSettings(CURRENT_LANE_SETTING_KEY, "IN-OUT");
UpdateLaneInOut();
toolStripMenuItemSwitchLaneInOut.DisableSelected();
Console.WriteLine($"{DateTime.Now.GetTimeFormatted()}\tReadLaneSettingFromConfigurationFile\t{ex.Message}");
appLogger.Log(LogLevel.Error, $"Đọc config làn lỗi khi khởi động app!\texMessage: {ex.Message}");
}
}
private void ReadAccessControlDeviceIPConfigurationFile()
{
try
{
doorAccessControlDeviceIP = ConfigurationManager.AppSettings["DOOR_ACCESS_DEVICE_CONTROL_IP"].Trim();
}
catch (Exception ex)
{
doorAccessControlDeviceIP = string.Empty;
Console.WriteLine($"{DateTime.Now.GetTimeFormatted()}\tReadAccessControlDeviceIPConfiguration\t{ex.Message}");
appLogger.Log(LogLevel.Error, $"Cấu hình IP thiết bị mở cửa lỗi(DOOR_ACCESS_DEVICE_CONTROL_IP)!\texMessage: {ex.Message}");
}
}
private void AIParkingApplicationForm_Load(object sender, EventArgs e)
{
InitAllLanes();
InitLaneFirstTime();
}
private void AIParkingApplicationForm_FormClosing(object sender, FormClosingEventArgs e)
{
appLogger.Log(LogLevel.Info, $"Đóng ứng dụng");
Application.Exit();
}
}
}