AppConstant - add App.config key

This commit is contained in:
DucDangAnh 2020-07-21 15:52:47 +07:00
parent 59208674e0
commit 9400402141
9 changed files with 77 additions and 52 deletions

View File

@ -9,8 +9,6 @@ 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;
@ -154,7 +152,7 @@ namespace AIParkingApplication
private void toolStripMenuItemSwitchLaneInIn_Click(object sender, EventArgs e)
{
Util.UpsertAppSettings(CURRENT_LANE_SETTING_KEY, "IN-IN");
Util.UpsertAppSettings(AppConstant.CURRENT_LANE_SETTING, "IN-IN");
UpdateMenuStripItemStatus(sender as ToolStripMenuItem);
StopAllLanes();
UpdateLaneInIn();
@ -163,7 +161,7 @@ namespace AIParkingApplication
private void toolStripMenuItemSwitchLaneInOut_Click(object sender, EventArgs e)
{
Util.UpsertAppSettings(CURRENT_LANE_SETTING_KEY, "IN-OUT");
Util.UpsertAppSettings(AppConstant.CURRENT_LANE_SETTING, "IN-OUT");
UpdateMenuStripItemStatus(sender as ToolStripMenuItem);
StopAllLanes();
UpdateLaneInOut();
@ -172,7 +170,7 @@ namespace AIParkingApplication
private void toolStripMenuItemSwitchLaneOutIn_Click(object sender, EventArgs e)
{
Util.UpsertAppSettings(CURRENT_LANE_SETTING_KEY, "OUT-IN");
Util.UpsertAppSettings(AppConstant.CURRENT_LANE_SETTING, "OUT-IN");
UpdateMenuStripItemStatus(sender as ToolStripMenuItem);
StopAllLanes();
UpdateLaneOutIn();
@ -181,7 +179,7 @@ namespace AIParkingApplication
private void toolStripMenuItemSwitchLaneOutOut_Click(object sender, EventArgs e)
{
Util.UpsertAppSettings(CURRENT_LANE_SETTING_KEY, "OUT-OUT");
Util.UpsertAppSettings(AppConstant.CURRENT_LANE_SETTING, "OUT-OUT");
UpdateMenuStripItemStatus(sender as ToolStripMenuItem);
StopAllLanes();
UpdateLaneOutOut();
@ -227,7 +225,7 @@ namespace AIParkingApplication
{
try
{
string lanesConfig = ConfigurationManager.AppSettings[CURRENT_LANE_SETTING_KEY];
string lanesConfig = ConfigurationManager.AppSettings[AppConstant.CURRENT_LANE_SETTING];
if (!string.IsNullOrEmpty(lanesConfig))
{
string[] lanes = lanesConfig.Split('-');
@ -262,7 +260,7 @@ namespace AIParkingApplication
}
else
{
Util.UpsertAppSettings(CURRENT_LANE_SETTING_KEY, "IN-OUT");
Util.UpsertAppSettings(AppConstant.CURRENT_LANE_SETTING, "IN-OUT");
UpdateLaneInOut();
toolStripMenuItemSwitchLaneInOut.DisableSelected();
appLogger.Log(LogLevel.Info, $"Khởi động App đọc config không đủ 2 param: IN-OUT");
@ -271,7 +269,7 @@ namespace AIParkingApplication
}
catch (Exception ex)
{
Util.UpsertAppSettings(CURRENT_LANE_SETTING_KEY, "IN-OUT");
Util.UpsertAppSettings(AppConstant.CURRENT_LANE_SETTING, "IN-OUT");
UpdateLaneInOut();
toolStripMenuItemSwitchLaneInOut.DisableSelected();
Console.WriteLine($"{DateTime.Now.GetTimeFormatted()}\tReadLaneSettingFromConfigurationFile\t{ex.Message}");
@ -283,13 +281,13 @@ namespace AIParkingApplication
{
try
{
doorAccessControlDeviceIP = ConfigurationManager.AppSettings["DOOR_ACCESS_DEVICE_CONTROL_IP"].Trim();
doorAccessControlDeviceIP = ConfigurationManager.AppSettings[AppConstant.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}");
appLogger.Log(LogLevel.Error, $"Cấu hình IP thiết bị mở cửa lỗi({AppConstant.DOOR_ACCESS_DEVICE_CONTROL_IP})!\texMessage: {ex.Message}");
}
}

View File

@ -6,11 +6,25 @@
public const string ERROR_TITLE = "Lỗi";
public const string DATETIME_FORMAT = "HH:mm:ss dd/MM/yyyy";
public const string CAMERA_FAILED_IMAGE_PATH = @"Images\CantConnectCamera.jpg";
public const string CAMERA_FAILED_IMAGE_PATH = @".\Images\CantConnectCamera.jpg";
public const string DEFAULT_LOGO_IMAGE = @".\Images\ApplicationLogo.ico";
public const string APPLICATION_LOGGER_NAME = "ApplicationLogger";
public const string PING_SERVER_LOGGER_NAME = "PingServerLogger";
public const string PING_DOOR_DEVICE_CONTROL_ACCESS_LOGGER_NAME = "PingDoorDeviceControlAccessLogger";
public const string PING_DOOR_DEVICE_CONTROL_ACCESS_LOGGER_NAME = "PingDoorDeviceControlAccessLogger";
#region App Config Key
public const string DOOR_ACCESS_DEVICE_CONTROL_IP = "DOOR_ACCESS_DEVICE_CONTROL_IP";
public const string DEFAULT_USERNAME = "DEFAULT_USERNAME";
public const string DEFAULT_PASSWORD = "DEFAULT_PASSWORD";
public const string DEFAULT_WEB_SERVER = "DEFAULT_WEB_SERVER";
public const string CURRENT_LANE_SETTING = "CURRENT_LANE_SETTING";
public const string AUTO_LOGIN = "AUTO_LOGIN";
public const string USE_PRINTER = "USE_PRINTER";
public const string AUTO_OPEN_DOOR_1 = "AUTO_OPEN_DOOR_1";
public const string AUTO_OPEN_DOOR_2 = "AUTO_OPEN_DOOR_2";
public const string ALLOW_CHANGE_ROI_RECT = "ALLOW_CHANGE_ROI_RECT";
public const string AUTO_RUN_AT_STARTUP = "AUTO_RUN_AT_STARTUP";
#endregion
}
}

View File

@ -1,5 +1,6 @@
using OpenCvSharp;
using System.Threading;
using System.Windows.Forms;
namespace AIParkingApplication
{
@ -50,7 +51,7 @@ namespace AIParkingApplication
{
if (!videoCapture.Open(streamUrl))
{
OnOpenVideoStreamFailed?.Invoke(Cv2.ImRead(AppConstant.CAMERA_FAILED_IMAGE_PATH));
OnOpenVideoStreamFailed?.Invoke(Cv2.ImRead(Application.StartupPath + AppConstant.CAMERA_FAILED_IMAGE_PATH));
return;
}
@ -72,7 +73,7 @@ namespace AIParkingApplication
}
else
{
OnOpenVideoStreamFailed?.Invoke(Cv2.ImRead(AppConstant.CAMERA_FAILED_IMAGE_PATH));
OnOpenVideoStreamFailed?.Invoke(Cv2.ImRead(Application.StartupPath + AppConstant.CAMERA_FAILED_IMAGE_PATH));
}
}
}

View File

@ -29,13 +29,13 @@ namespace AIParkingApplication
private void btnSaveSettings_Click(object sender, EventArgs e)
{
Util.UpsertAppSettings("DOOR_ACCESS_DEVICE_CONTROL_IP", txtDoorDeviceControlAccessIP.Text);
Util.UpsertAppSettings("AUTO_OPEN_DOOR_1", chkAllowAutoDoor1.Checked.ToString().ToLower());
Util.UpsertAppSettings("AUTO_OPEN_DOOR_2", chkAllowAutoDoor2.Checked.ToString().ToLower());
Util.UpsertAppSettings("USE_PRINTER", chkAllowUsePrinter.Checked.ToString().ToLower());
Util.UpsertAppSettings("ALLOW_CHANGE_ROI_RECT", chkAllowChangeROIRect.Checked.ToString().ToLower());
Util.UpsertAppSettings("AUTO_LOGIN", chkAllowAutoLogin.Checked.ToString().ToLower());
Util.UpsertAppSettings("AUTO_RUN_AT_STARTUP", chkAllowAutoRunAtStartUp.Checked.ToString().ToLower());
Util.UpsertAppSettings(AppConstant.DOOR_ACCESS_DEVICE_CONTROL_IP, txtDoorDeviceControlAccessIP.Text);
Util.UpsertAppSettings(AppConstant.AUTO_OPEN_DOOR_1, chkAllowAutoDoor1.Checked.ToString().ToLower());
Util.UpsertAppSettings(AppConstant.AUTO_OPEN_DOOR_2, chkAllowAutoDoor2.Checked.ToString().ToLower());
Util.UpsertAppSettings(AppConstant.USE_PRINTER, chkAllowUsePrinter.Checked.ToString().ToLower());
Util.UpsertAppSettings(AppConstant.ALLOW_CHANGE_ROI_RECT, chkAllowChangeROIRect.Checked.ToString().ToLower());
Util.UpsertAppSettings(AppConstant.AUTO_LOGIN, chkAllowAutoLogin.Checked.ToString().ToLower());
Util.UpsertAppSettings(AppConstant.AUTO_RUN_AT_STARTUP, chkAllowAutoRunAtStartUp.Checked.ToString().ToLower());
if (chkAllowAutoRunAtStartUp.Checked)
{
Util.AddApplicationToStartup();
@ -61,13 +61,13 @@ namespace AIParkingApplication
private void LoadConfiguration()
{
doorAccessControlDeviceIP = ReadConfigurationFromAppSettings("DOOR_ACCESS_DEVICE_CONTROL_IP", string.Empty);
bool.TryParse(ReadConfigurationFromAppSettings("AUTO_OPEN_DOOR_1", "false"), out allowAutoOpenDoor1);
bool.TryParse(ReadConfigurationFromAppSettings("AUTO_OPEN_DOOR_2", "false"), out allowAutoOpenDoor2);
bool.TryParse(ReadConfigurationFromAppSettings("USE_PRINTER", "false"), out allowUsePrinter);
bool.TryParse(ReadConfigurationFromAppSettings("ALLOW_CHANGE_ROI_RECT", "false"), out allowChangeROIRect);
bool.TryParse(ReadConfigurationFromAppSettings("AUTO_LOGIN", "false"), out allowAutoLogin);
bool.TryParse(ReadConfigurationFromAppSettings("AUTO_RUN_AT_STARTUP", "false"), out allowAutoRunAtStartUp);
doorAccessControlDeviceIP = ReadConfigurationFromAppSettings(AppConstant.DOOR_ACCESS_DEVICE_CONTROL_IP, string.Empty);
bool.TryParse(ReadConfigurationFromAppSettings(AppConstant.AUTO_OPEN_DOOR_1, "false"), out allowAutoOpenDoor1);
bool.TryParse(ReadConfigurationFromAppSettings(AppConstant.AUTO_OPEN_DOOR_2, "false"), out allowAutoOpenDoor2);
bool.TryParse(ReadConfigurationFromAppSettings(AppConstant.USE_PRINTER, "false"), out allowUsePrinter);
bool.TryParse(ReadConfigurationFromAppSettings(AppConstant.ALLOW_CHANGE_ROI_RECT, "false"), out allowChangeROIRect);
bool.TryParse(ReadConfigurationFromAppSettings(AppConstant.AUTO_LOGIN, "false"), out allowAutoLogin);
bool.TryParse(ReadConfigurationFromAppSettings(AppConstant.AUTO_RUN_AT_STARTUP, "false"), out allowAutoRunAtStartUp);
}
private string ReadConfigurationFromAppSettings(string configurationKey, string defaultValueIfReadFailed)

View File

@ -156,12 +156,12 @@ namespace AIParkingApplication
{
try
{
bool.TryParse(ConfigurationManager.AppSettings["USE_PRINTER"], out isUsePrinter);
bool.TryParse(ConfigurationManager.AppSettings[AppConstant.USE_PRINTER], out isUsePrinter);
}
catch (Exception ex)
{
Util.UpsertAppSettings("USE_PRINTER", "false");
appLogger.Log(LogLevel.Error, $"Không thể đọc cấu hình: USE_PRINTER. ex: {ex.Message }");
Util.UpsertAppSettings(AppConstant.USE_PRINTER, "false");
appLogger.Log(LogLevel.Error, $"Không thể đọc cấu hình: {AppConstant.USE_PRINTER}. ex: {ex.Message }");
}
}

View File

@ -3,7 +3,6 @@ using System;
using System.Configuration;
using System.Drawing;
using System.IO;
using System.Threading;
using System.Windows.Forms;
namespace AIParkingApplication
@ -20,9 +19,10 @@ namespace AIParkingApplication
applicationLogger = LogManager.GetLogger(AppConstant.APPLICATION_LOGGER_NAME);
applicationLogger.Log(LogLevel.Info, new string('-', 20));
isAutoLogin = false;
if (File.Exists(AppConstant.DEFAULT_LOGO_IMAGE))
string defaultLogoImagePath = Application.StartupPath + AppConstant.DEFAULT_LOGO_IMAGE;
if (File.Exists(defaultLogoImagePath))
{
pictureBoxImageLogo.Image = new Bitmap(AppConstant.DEFAULT_LOGO_IMAGE);
pictureBoxImageLogo.Image = new Bitmap(defaultLogoImagePath);
}
txtUsername.Focus();
lblLoginStatus.Text = string.Empty;
@ -98,9 +98,9 @@ namespace AIParkingApplication
if (loginResult.IsLoginSuccess)
{
Util.UpsertAppSettings("DEFAULT_USERNAME", txtUsername.Text);
Util.UpsertAppSettings("DEFAULT_PASSWORD", txtPassword.Text);
Util.UpsertAppSettings("DEFAULT_WEB_SERVER", txtServerAddress.Text);
Util.UpsertAppSettings(AppConstant.DEFAULT_USERNAME, txtUsername.Text);
Util.UpsertAppSettings(AppConstant.DEFAULT_PASSWORD, txtPassword.Text);
Util.UpsertAppSettings(AppConstant.DEFAULT_WEB_SERVER, txtServerAddress.Text);
new AIParkingApplicationForm(apiController, ipAddress, loginResult.LoginData, applicationLogger).Show();
Hide();
applicationLogger.Log(LogLevel.Info, $"Đăng nhập thành công với tài khoản: {txtUsername.Text}");
@ -116,10 +116,10 @@ namespace AIParkingApplication
{
try
{
txtUsername.Text = ConfigurationManager.AppSettings["DEFAULT_USERNAME"];
txtPassword.Text = ConfigurationManager.AppSettings["DEFAULT_PASSWORD"];
txtServerAddress.Text = ConfigurationManager.AppSettings["DEFAULT_WEB_SERVER"];
bool.TryParse(ConfigurationManager.AppSettings["AUTO_LOGIN"], out isAutoLogin);
txtUsername.Text = ConfigurationManager.AppSettings[AppConstant.DEFAULT_USERNAME];
txtPassword.Text = ConfigurationManager.AppSettings[AppConstant.DEFAULT_PASSWORD];
txtServerAddress.Text = ConfigurationManager.AppSettings[AppConstant.DEFAULT_WEB_SERVER];
bool.TryParse(ConfigurationManager.AppSettings[AppConstant.AUTO_LOGIN], out isAutoLogin);
if (isAutoLogin)
{
chkAutoLogin.Checked = true;
@ -130,13 +130,13 @@ namespace AIParkingApplication
{
txtUsername.Text = string.Empty;
txtPassword.Text = string.Empty;
Util.UpsertAppSettings("DEFAULT_USERNAME", string.Empty);
Util.UpsertAppSettings("DEFAULT_PASSWORD", string.Empty);
Util.UpsertAppSettings("AUTO_LOGIN", "false");
Util.UpsertAppSettings("DEFAULT_WEB_SERVER", "127.0.0.1:80");
Util.UpsertAppSettings(AppConstant.DEFAULT_USERNAME, string.Empty);
Util.UpsertAppSettings(AppConstant.DEFAULT_PASSWORD, string.Empty);
Util.UpsertAppSettings(AppConstant.AUTO_LOGIN, "false");
Util.UpsertAppSettings(AppConstant.DEFAULT_WEB_SERVER, "127.0.0.1:80");
chkAutoLogin.Checked = false;
Console.WriteLine($"{DateTime.Now.GetTimeFormatted()}\tReadAccessControlDeviceIPConfiguration\t{ex.Message}");
applicationLogger.Log(LogLevel.Error, "Không thể đọc cấu hình tài khoản mặc định: DEFAULT_USERNAME, DEFAULT_PASSWORD");
applicationLogger.Log(LogLevel.Error, $"Không thể đọc cấu hình tài khoản mặc định: {AppConstant.DEFAULT_USERNAME}, {AppConstant.DEFAULT_PASSWORD}");
}
}
@ -147,7 +147,7 @@ namespace AIParkingApplication
private void chkAutoLogin_CheckedChanged(object sender, EventArgs e)
{
Util.UpsertAppSettings("AUTO_LOGIN", (sender as CheckBox).Checked.ToString().ToLower());
Util.UpsertAppSettings(AppConstant.AUTO_LOGIN, (sender as CheckBox).Checked.ToString().ToLower());
}
}
}

View File

@ -1,6 +1,7 @@
using OpenCvSharp;
using System;
using System.Linq;
using System.Windows.Forms;
namespace AIParkingApplication
{
@ -20,7 +21,8 @@ namespace AIParkingApplication
this.maxSizePlate = maxSizePlate;
this.scaleFactor = scaleFactor;
this.minNeighbors = minNeighbors;
plateCascadeClassifier = new CascadeClassifier(this.plateType == PlateType.Square ? PlateDetectorConstant.SQUARE_PLATE_WEIGHT_FILENAME : PlateDetectorConstant.LONG_PLATE_WEIGHT_FILENAME);
string plateWeightPath = Application.StartupPath + (this.plateType == PlateType.Square ? PlateDetectorConstant.SQUARE_PLATE_WEIGHT_FILENAME : PlateDetectorConstant.LONG_PLATE_WEIGHT_FILENAME);
plateCascadeClassifier = new CascadeClassifier(plateWeightPath);
}
~PlateDetector()
@ -87,8 +89,8 @@ namespace AIParkingApplication
public static class PlateDetectorConstant
{
public const string SQUARE_PLATE_WEIGHT_FILENAME = "plate.xml";
public const string LONG_PLATE_WEIGHT_FILENAME = "plateLong.xml";
public const string SQUARE_PLATE_WEIGHT_FILENAME = @".\plate.xml";
public const string LONG_PLATE_WEIGHT_FILENAME = @".\plateLong.xml";
public const double SCALE_FACTOR_DEFAULT_SQUARE_PLATE = 1.03;
public static int MIN_NEIGHBORS_DEFAULT_SQUARE_PLATE = 2;

View File

@ -36,6 +36,7 @@
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Name = "Sidebar";
this.Size = new System.Drawing.Size(192, 692);
this.Load += new System.EventHandler(this.Sidebar_Load);
this.ResumeLayout(false);
}

View File

@ -8,11 +8,20 @@ namespace AIParkingApplication
public partial class Sidebar : UserControl
{
private Statistic statistic;
private string logoImagePath;
private ApiController apiController;
public Sidebar(ApiController apiController, string logoImagePath = AppConstant.DEFAULT_LOGO_IMAGE)
{
InitializeComponent();
if (File.Exists(logoImagePath))
this.apiController = apiController;
this.logoImagePath = logoImagePath;
}
private void Sidebar_Load(object sender, EventArgs e)
{
string defaultLogoImagePath = Application.StartupPath + logoImagePath;
if (File.Exists(defaultLogoImagePath))
{
PictureBox pictureBoxImageLogo;
pictureBoxImageLogo = new PictureBox();