Compare commits

...

9 Commits

8 changed files with 130 additions and 64 deletions

View File

@ -22,25 +22,24 @@ namespace AIParkingApplication
private Config configOnWeb;
private EngineApiController engineApiController;
private string doorAccessControlDeviceIP;
private Logger appLogger;
private Logger applicationLogger;
public AIParkingApplicationForm(ApiController apiController, string serverIPAddress, Config configOnWeb)
public AIParkingApplicationForm(ApiController apiController, string serverIPAddress, Config configOnWeb, Logger appLogger)
{
InitializeComponent();
this.apiController = apiController;
this.configOnWeb = configOnWeb;
applicationLogger = LogManager.GetLogger(AppConstant.APPLICATION_LOGGER_NAME);
this.appLogger = appLogger;
ReadAccessControlDeviceIPConfigurationFile();
if (string.IsNullOrEmpty(doorAccessControlDeviceIP) || !Util.IsValidIPAddress(doorAccessControlDeviceIP))
{
applicationLogger.Log(LogLevel.Error, "Kiểm tra lại cấu hình IP thiết bị mở cửa!");
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(doorAccessControlDeviceIP);
c3Device = new C3DeviceController(appLogger, doorAccessControlDeviceIP);
sidebar = new Sidebar(apiController)
{
Location = new Point(0, 0)
@ -58,9 +57,9 @@ namespace AIParkingApplication
{
engineApiController = new EngineApiController($"http://{this.configOnWeb.APIPath.ApiPlateRecognize.IP}:{this.configOnWeb.APIPath.ApiPlateRecognize.Port}");
}
catch (Exception)
catch (Exception ex)
{
applicationLogger.Log(LogLevel.Error, "Cấu hình URL của Engine nhận diện biển số lỗi!");
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;
}
@ -68,22 +67,22 @@ namespace AIParkingApplication
private void InitAllLanes()
{
laneIn12 = new LaneIn(10, configOnWeb.CameraData1.Id.ToString(), configOnWeb.CameraData1.StreamUrl, configOnWeb.CameraData2.StreamUrl, c3Device, apiController, engineApiController, true, false, true);
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, true, false, true);
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, true, false, true);
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, true, false, true);
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);
@ -100,8 +99,6 @@ namespace AIParkingApplication
laneOut34.Location = new Point(laneIn12.Location.X + laneIn12.Width + 20, menuStrip.Height);
laneOut34.Show();
laneOut34.Start();
toolStripMenuItemSwitchLaneInOut.DisableSelected();
}
private void UpdateLaneInIn()
@ -115,8 +112,6 @@ namespace AIParkingApplication
laneIn56.Location = new Point(laneIn12.Location.X + laneIn12.Width + 20, menuStrip.Height);
laneIn56.Show();
laneIn56.Start();
toolStripMenuItemSwitchLaneInIn.DisableSelected();
}
private void UpdateLaneOutIn()
@ -130,8 +125,6 @@ namespace AIParkingApplication
laneIn56.Location = new Point(laneOut78.Location.X + laneOut78.Width + 20, menuStrip.Height);
laneIn56.Show();
laneIn56.Start();
toolStripMenuItemSwitchLaneOutIn.DisableSelected();
}
private void UpdateLaneOutOut()
@ -145,8 +138,6 @@ namespace AIParkingApplication
laneOut34.Location = new Point(laneOut78.Location.X + laneOut78.Width + 20, menuStrip.Height);
laneOut34.Show();
laneOut34.Start();
toolStripMenuItemSwitchLaneOutOut.DisableSelected();
}
private void toolStripMenuItemSwitchLaneInIn_Click(object sender, EventArgs e)
@ -155,7 +146,7 @@ namespace AIParkingApplication
UpdateMenuStripItemStatus(sender as ToolStripMenuItem);
StopAllLanes();
UpdateLaneInIn();
applicationLogger.Log(LogLevel.Info, "Chuyển làn: IN-IN");
appLogger.Log(LogLevel.Info, "Chuyển làn: IN-IN");
}
private void toolStripMenuItemSwitchLaneInOut_Click(object sender, EventArgs e)
@ -164,7 +155,7 @@ namespace AIParkingApplication
UpdateMenuStripItemStatus(sender as ToolStripMenuItem);
StopAllLanes();
UpdateLaneInOut();
applicationLogger.Log(LogLevel.Info, "Chuyển làn: IN-OUT");
appLogger.Log(LogLevel.Info, "Chuyển làn: IN-OUT");
}
private void toolStripMenuItemSwitchLaneOutIn_Click(object sender, EventArgs e)
@ -173,7 +164,7 @@ namespace AIParkingApplication
UpdateMenuStripItemStatus(sender as ToolStripMenuItem);
StopAllLanes();
UpdateLaneOutIn();
applicationLogger.Log(LogLevel.Info, "Chuyển làn: OUT-IN");
appLogger.Log(LogLevel.Info, "Chuyển làn: OUT-IN");
}
private void toolStripMenuItemSwitchLaneOutOut_Click(object sender, EventArgs e)
@ -182,7 +173,7 @@ namespace AIParkingApplication
UpdateMenuStripItemStatus(sender as ToolStripMenuItem);
StopAllLanes();
UpdateLaneOutOut();
applicationLogger.Log(LogLevel.Info, "Chuyển làn: OUT-OUT");
appLogger.Log(LogLevel.Info, "Chuyển làn: OUT-OUT");
}
private void UpdateMenuStripItemStatus(ToolStripMenuItem selectedToolStripMenuItem)
@ -235,29 +226,34 @@ namespace AIParkingApplication
if (lane0 == "in" && lane1 == "in")
{
UpdateLaneInIn();
applicationLogger.Log(LogLevel.Info, $"Khởi động App sử dụng lane: IN-IN");
toolStripMenuItemSwitchLaneInIn.DisableSelected();
appLogger.Log(LogLevel.Info, $"Khởi động App sử dụng lane: IN-IN");
}
if (lane0 == "in" && lane1 == "out")
{
UpdateLaneInOut();
applicationLogger.Log(LogLevel.Info, $"Khởi động App sử dụng lane: IN-OUT");
toolStripMenuItemSwitchLaneInOut.DisableSelected();
appLogger.Log(LogLevel.Info, $"Khởi động App sử dụng lane: IN-OUT");
}
if (lane0 == "out" && lane1 == "in")
{
UpdateLaneOutIn();
applicationLogger.Log(LogLevel.Info, $"Khởi động App sử dụng lane: OUT-IN");
toolStripMenuItemSwitchLaneOutIn.DisableSelected();
appLogger.Log(LogLevel.Info, $"Khởi động App sử dụng lane: OUT-IN");
}
if (lane0 == "out" && lane1 == "out")
{
UpdateLaneOutOut();
applicationLogger.Log(LogLevel.Info, $"Khởi động App sử dụng lane: OUT-OUT");
toolStripMenuItemSwitchLaneOutOut.DisableSelected();
appLogger.Log(LogLevel.Info, $"Khởi động App sử dụng lane: OUT-OUT");
}
}
else
{
Util.AddOrUpdateAppSettings(CURRENT_LANE_SETTING_KEY, "IN-OUT");
UpdateLaneInOut();
applicationLogger.Log(LogLevel.Info, $"Khởi động App đọc config không đủ 2 param: IN-OUT");
toolStripMenuItemSwitchLaneInOut.DisableSelected();
appLogger.Log(LogLevel.Info, $"Khởi động App đọc config không đủ 2 param: IN-OUT");
}
}
}
@ -265,8 +261,9 @@ namespace AIParkingApplication
{
Util.AddOrUpdateAppSettings(CURRENT_LANE_SETTING_KEY, "IN-OUT");
UpdateLaneInOut();
toolStripMenuItemSwitchLaneInOut.DisableSelected();
Console.WriteLine($"{DateTime.Now.GetTimeFormatted()}\tReadLaneSettingFromConfigurationFile\t{ex.Message}");
applicationLogger.Log(LogLevel.Error, $"Đọc config làn lỗi khi khởi động app!\texMessage: {ex.Message}");
appLogger.Log(LogLevel.Error, $"Đọc config làn lỗi khi khởi động app!\texMessage: {ex.Message}");
}
}
@ -280,7 +277,7 @@ namespace AIParkingApplication
{
doorAccessControlDeviceIP = string.Empty;
Console.WriteLine($"{DateTime.Now.GetTimeFormatted()}\tReadAccessControlDeviceIPConfiguration\t{ex.Message}");
applicationLogger.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(DOOR_ACCESS_DEVICE_CONTROL_IP)!\texMessage: {ex.Message}");
}
}
@ -292,7 +289,7 @@ namespace AIParkingApplication
private void AIParkingApplicationForm_FormClosing(object sender, FormClosingEventArgs e)
{
applicationLogger.Log(LogLevel.Error, $"Đóng ứng dụng");
appLogger.Log(LogLevel.Info, $"Đóng ứng dụng");
Application.Exit();
}
}

View File

@ -4,24 +4,27 @@ using System.Threading.Tasks;
using OpenCvSharp;
using Newtonsoft.Json;
using System.Net;
using NLog;
namespace AIParkingApplication
{
public class ApiController : IDisposable
{
private const int MAX_REQUEST = 3;
private HttpClient httpClient;
private bool isHttpClientDisposabled;
private int numberOfRetry;
private Logger appLogger;
public ApiController(string serverIPAddress, int numberOfRetry = 5)
public ApiController(string serverIPAddress, Logger appLogger, int numberOfRetry = 3)
{
this.appLogger = appLogger;
httpClient = new HttpClient
{
BaseAddress = new Uri($"http://{serverIPAddress}"),
Timeout = TimeSpan.FromSeconds(5)
};
isHttpClientDisposabled = false;
this.numberOfRetry = numberOfRetry;
}
~ApiController()
@ -71,12 +74,14 @@ namespace AIParkingApplication
catch (Exception ex)
{
Console.WriteLine($"GetStatisticInfo Exception:\t{DateTime.Now.GetTimeFormatted()} \t {ex.Message}");
appLogger.Log(LogLevel.Info, $"Query dữ liệu thống kê bãi xe lỗi. exMessage: {ex.Message}");
return null;
}
}
public async Task<CardInformation> GetCardInformation(string cardNumber)
{
int requestCounter = 1;
try
{
var request = new CardModel()
@ -84,7 +89,6 @@ namespace AIParkingApplication
CardNumber = cardNumber
};
HttpResponseMessage response;
int requestCounter = 1;
do
{
response = await httpClient.PostAsJsonAsync("/api/check-card", request);
@ -92,7 +96,7 @@ namespace AIParkingApplication
requestCounter += 1;
await Task.Delay(100);
}
while (response.StatusCode != HttpStatusCode.OK && requestCounter < MAX_REQUEST);
while (response.StatusCode != HttpStatusCode.OK && requestCounter < numberOfRetry);
var cardValication = await response.Content.ReadAsAsync<CardInformation>();
return cardValication;
@ -100,6 +104,7 @@ namespace AIParkingApplication
catch (Exception ex)
{
Console.WriteLine($"CheckCard Exception:\t{DateTime.Now.GetTimeFormatted()} \t {ex.Message}");
appLogger.Log(LogLevel.Info, $"Query thông tin thẻ lỗi. Thẻ số: {cardNumber} | số lần thử lại: {requestCounter} | exMessage: {ex.Message}");
return null;
}
}
@ -111,6 +116,8 @@ namespace AIParkingApplication
string plateImageResultBase64 = Convert.ToBase64String(plateImageResult.ToBytes());
string plateFrameImageBase64 = Convert.ToBase64String(plateFrameImage.ToBytes());
string frameImageBase64 = Convert.ToBase64String(frameImage.ToBytes());
int requestCounter = 1;
try
{
var request = new SaveLogModel
@ -128,21 +135,22 @@ namespace AIParkingApplication
FrameImage = frameImageBase64
};
HttpResponseMessage response;
int requestCounter = 1;
do
{
response = await httpClient.PostAsJsonAsync("/api/save-logs", request);
response.EnsureSuccessStatusCode();
requestCounter += 1;
await Task.Delay(100);
} while (response.StatusCode != HttpStatusCode.OK && requestCounter < MAX_REQUEST);
} while (response.StatusCode != HttpStatusCode.OK && requestCounter < numberOfRetry);
SaveLogRespone saveLogRespone = await response.Content.ReadAsAsync<SaveLogRespone>();
return saveLogRespone;
}
catch (Exception ex)
{
Console.WriteLine($"SaveLog Exception:\t{DateTime.Now.GetTimeFormatted()} \t {ex.Message}");
return new SaveLogRespone();
appLogger.Log(LogLevel.Info, string.Format("Không thể lưu log gửi xe. Làn: {0} | Mã thẻ: {1} | cameraID: {2} | Thời gian: {3} | Biển số: {4} | LogID: {5} | exMessage: ", direction == LaneDirection.In ? "IN" : "OUT", cardID, cameraID, timestamp, plateString, logID, ex.Message));
return null;
}
}

View File

@ -1,4 +1,5 @@
using System;
using NLog;
using System;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
@ -15,10 +16,11 @@ namespace AIParkingApplication
private string connectionString;
private bool isReconnected;
private int reconnectCounter;
private Logger appLogger;
public event C3DeviceEvent OnNewCardReceived;
public C3DeviceController(string ipAddress, int port = C3Constant.DEFAULT_C3_PORT)
public C3DeviceController(Logger appLogger, string ipAddress, int port = C3Constant.DEFAULT_C3_PORT)
{
this.ipAddress = ipAddress;
this.port = port;
@ -26,6 +28,7 @@ namespace AIParkingApplication
oneTimeConnectedParamHandler = IntPtr.Zero;
isReconnected = true;
reconnectCounter = 0;
this.appLogger = appLogger;
}
~C3DeviceController()

View File

@ -3,6 +3,7 @@ using System.Drawing;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using NLog;
using OpenCvSharp;
using OpenCvSharp.Extensions;
@ -21,6 +22,7 @@ namespace AIParkingApplication
private bool isRetryMode;
private IDoorControlAccess doorControlAccess;
private ApiController apiController;
private Logger appLogger;
public LaneIn(int doorId,
string cameraId,
@ -29,12 +31,14 @@ namespace AIParkingApplication
IDoorControlAccess doorControlAccess,
ApiController apiController,
EngineApiController engineApiController,
Logger appLogger,
bool isSupportSquarePlate = true,
bool isSupportLongPlate = false,
bool isAutoOpenDoor = true,
bool isRetryMode = false)
{
InitializeComponent();
this.appLogger = appLogger;
DoorId = doorId;
this.cameraId = cameraId;
this.isSupportSquarePlate = isSupportSquarePlate;
@ -59,23 +63,27 @@ namespace AIParkingApplication
if (cardInfoResult == null)
{
lblStatusInfo.UpdateLabel("KHÔNG THỂ KẾT NỐI SERVER", Color.Purple);
appLogger.Log(LogLevel.Error, $"Request thẻ cổng {doorId} vào | số thẻ : {cardNumber} | lỗi: KHÔNG THỂ KẾT NỐI TỚI SERVER");
return;
}
if (!cardInfoResult.IsValid)
{
lblStatusInfo.UpdateLabel("THẺ KHÔNG HỢP LỆ", Color.Purple);
appLogger.Log(LogLevel.Error, $"Request thẻ cổng {doorId} vào | số thẻ : {cardNumber} | lỗi: THẺ KHÔNG HỢP LỆ");
return;
}
if (cardInfoResult.Direction != "in")
{
lblStatusInfo.UpdateLabel("XE ĐÃ Ở TRONG BÃI", Color.Red);
appLogger.Log(LogLevel.Error, $"Request thẻ cổng {doorId} vào | số thẻ : {cardNumber} | lỗi: XE ĐÃ Ở TRONG BÃI");
return;
}
plateCamera.RequestCaptureOneFrame();
overviewCamera.RequestCaptureOneFrame();
await Task.Delay(200);
appLogger.Log(LogLevel.Info, $"Request thẻ cổng {doorId} vào | số thẻ : {cardNumber} | Chụp ảnh camera");
var plateVideoFrame = plateCamera.CurrentFrame;
FinalPlateResult result = await ProcessFrameImage(plateProcessor, plateVideoFrame, isRetryMode);
@ -88,6 +96,13 @@ namespace AIParkingApplication
var saveLogResult = await apiController.SaveLog(LaneDirection.In, cardInformation.CardRealID.ToString(), cameraId, result.PlateType, DateTime.Now.GetTimeFormatted(), result.PlateString, result.PlateImage, result.PlateImage, result.PlateImage, overviewVideoFrame);
if (saveLogResult == null)
{
lblStatusInfo.UpdateLabel("KHÔNG CÓ KẾT NỐI ĐẾN MÁY CHỦ", Color.Red);
appLogger.Log(LogLevel.Error, $"Request thẻ cổng {doorId} vào | số thẻ : {cardNumber} | lỗi: KHÔNG CÓ KẾT NỐI ĐẾN MÁY CHỦ - KHÔNG LƯU ĐƯỢC LOG");
return;
}
ShowCardInfoOnUI(cardNumber, result.PlateString, cardInformation.CardType, DateTime.Now.GetTimeFormatted());
if (saveLogResult.Status)
@ -95,11 +110,13 @@ namespace AIParkingApplication
if (isAutoOpenDoor && DoorId == doorId)
{
OpenDoor(doorId);
appLogger.Log(LogLevel.Info, $"Request thẻ cổng {doorId} vào | số thẻ : {cardNumber} | biển số: {result.PlateString} | Đã mở cửa");
}
}
else
{
lblStatusInfo.UpdateLabel("KHÔNG CÓ KẾT NỐI ĐẾN MÁY CHỦ", Color.Red);
appLogger.Log(LogLevel.Error, $"Request thẻ cổng {doorId} vào | số thẻ : {cardNumber} | lỗi: LƯU BẢN GHI LỖI");
lblStatusInfo.UpdateLabel("LƯU BẢN GHI LỖI", Color.Red);
}
}
@ -159,6 +176,12 @@ namespace AIParkingApplication
{
plateCamera.Stop();
overviewCamera.Stop();
pictureBoxPlateImage.UpdateImage(null);
pictureBoxPlateVideo.UpdateImage(null);
pictureBoxOverviewImage.UpdateImage(null);
pictureBoxOverviewVideo.UpdateImage(null);
}
public void Start()

View File

@ -3,6 +3,7 @@ using System.Drawing;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using NLog;
using OpenCvSharp;
using OpenCvSharp.Extensions;
@ -22,6 +23,7 @@ namespace AIParkingApplication
private IDoorControlAccess doorControlAccess;
private ApiController apiController;
private Printer printer;
private Logger appLogger;
public LaneOut(int doorId,
string cameraId,
@ -30,12 +32,14 @@ namespace AIParkingApplication
IDoorControlAccess doorControlAccess,
ApiController apiController,
EngineApiController engineApiController,
Logger appLogger,
bool isSupportSquarePlate = true,
bool isSupportLongPlate = false,
bool isAutoOpenDoor = true,
bool isRetryMode = false)
{
InitializeComponent();
this.appLogger = appLogger;
DoorId = doorId;
this.cameraId = cameraId;
this.isSupportSquarePlate = isSupportSquarePlate;
@ -46,7 +50,7 @@ namespace AIParkingApplication
plateCamera = new Camera(plateStream);
this.apiController = apiController;
this.doorControlAccess = doorControlAccess;
printer = new Printer();
printer = new Printer(appLogger);
plateProcessor = new PlateProcessor(engineApiController, this.isSupportSquarePlate, this.isSupportLongPlate);
}
@ -62,17 +66,20 @@ namespace AIParkingApplication
if (cardInfoResult == null)
{
lblStatusInfo.UpdateLabel("KHÔNG THỂ KẾT NỐI SERVER", Color.Purple);
appLogger.Log(LogLevel.Error, $"Request thẻ cổng {doorId} vào | số thẻ : {cardNumber} | lỗi: KHÔNG THỂ KẾT NỐI TỚI SERVER");
return;
}
if (!cardInfoResult.IsValid)
{
lblStatusInfo.UpdateLabel("THẺ KHÔNG HỢP LỆ", Color.Purple);
appLogger.Log(LogLevel.Error, $"Request thẻ cổng {doorId} vào | số thẻ : {cardNumber} | lỗi: THẺ KHÔNG HỢP LỆ");
return;
}
if (cardInfoResult.Direction != "out")
{
lblStatusInfo.UpdateLabel("CHƯA NHẬN THẺ VÀO", Color.Red);
appLogger.Log(LogLevel.Error, $"Request thẻ cổng {doorId} vào | số thẻ : {cardNumber} | lỗi: XE ĐÃ Ở TRONG BÃI");
return;
}
@ -85,6 +92,7 @@ namespace AIParkingApplication
plateCamera.RequestCaptureOneFrame();
overviewCamera.RequestCaptureOneFrame();
await Task.Delay(200);
appLogger.Log(LogLevel.Info, $"Request thẻ cổng {doorId} vào | số thẻ : {cardNumber} | Chụp ảnh camera");
var plateVideoFrame = plateCamera.CurrentFrame;
FinalPlateResult result = await ProcessFrameImage(plateProcessor, plateVideoFrame, isRetryMode);
@ -97,6 +105,13 @@ namespace AIParkingApplication
var saveLogResult = await apiController.SaveLog(LaneDirection.Out, cardInformation.CardRealID.ToString(), cameraId, result.PlateType, DateTime.Now.GetTimeFormatted(), result.PlateString, result.PlateImage, result.PlateImage, result.PlateImage, overviewVideoFrame, cardInformation.LogID.ToString());
if (saveLogResult == null)
{
lblStatusInfo.UpdateLabel("KHÔNG CÓ KẾT NỐI ĐẾN MÁY CHỦ", Color.Red);
appLogger.Log(LogLevel.Error, $"Request thẻ cổng {doorId} vào | số thẻ : {cardNumber} | lỗi: KHÔNG CÓ KẾT NỐI ĐẾN MÁY CHỦ - KHÔNG LƯU ĐƯỢC LOG");
return;
}
ShowCardInfoOnUI(cardNumber, result.PlateString, cardInformation.PlateString, cardInformation.CardType, cardInformation.TimeIn, DateTime.Now.GetTimeFormatted(), saveLogResult.Cost);
if (saveLogResult.Status)
@ -104,6 +119,7 @@ namespace AIParkingApplication
if (isAutoOpenDoor)
{
OpenDoor(doorId);
appLogger.Log(LogLevel.Info, $"Request thẻ cổng {doorId} vào | số thẻ : {cardNumber} | biển số: {result.PlateString} | Đã mở cửa");
}
PrinterData printerData = new PrinterData
{
@ -116,7 +132,8 @@ namespace AIParkingApplication
}
else
{
lblStatusInfo.UpdateLabel("KHÔNG CÓ KẾT NỐI ĐẾN MÁY CHỦ", Color.Red);
appLogger.Log(LogLevel.Error, $"Request thẻ cổng {doorId} vào | số thẻ : {cardNumber} | lỗi: LƯU BẢN GHI LỖI");
lblStatusInfo.UpdateLabel("LƯU BẢN GHI LỖI", Color.Red);
}
}
@ -182,6 +199,12 @@ namespace AIParkingApplication
{
plateCamera.Stop();
overviewCamera.Stop();
pictureBoxPlateImage.UpdateImage(null);
pictureBoxPlateVideo.UpdateImage(null);
pictureBoxOverviewImage.UpdateImage(null);
pictureBoxOverviewVideo.UpdateImage(null);
}
public void Start()

View File

@ -1,4 +1,4 @@
using OpenCvSharp;
using NLog;
using System;
using System.Configuration;
using System.Drawing;
@ -10,10 +10,13 @@ namespace AIParkingApplication
public partial class LoginForm : Form
{
private ApiController apiController;
private Logger applicationLogger;
public LoginForm()
{
InitializeComponent();
applicationLogger = LogManager.GetLogger(AppConstant.APPLICATION_LOGGER_NAME);
applicationLogger.Log(LogLevel.Info, new string('-', 20));
if (File.Exists(AppConstant.DEFAULT_LOGO_IMAGE))
{
pictureBoxImageLogo.Image = new Bitmap(AppConstant.DEFAULT_LOGO_IMAGE);
@ -31,6 +34,7 @@ namespace AIParkingApplication
{
lblLoginStatus.Text = "Địa chỉ server không được để trống! \r\n ví dụ: 192.168.1.2:80";
btnLogin.Enabled = true;
applicationLogger.Log(LogLevel.Error, "Địa chỉ server không được để trống");
return;
}
@ -40,17 +44,19 @@ namespace AIParkingApplication
{
lblLoginStatus.Text = "Địa chỉ server không đúng định dạng: \r\n ví dụ: 192.168.1.122:80";
btnLogin.Enabled = true;
applicationLogger.Log(LogLevel.Error, "Địa chỉ server không đúng định dạng");
return;
}
try
{
apiController = new ApiController(serverUrl);
apiController = new ApiController(serverUrl, applicationLogger);
}
catch (Exception ex)
{
MessageBox.Show($"Cấu máy chủ lỗi - Kiểm tra lại tên máy chủ, kết nối máy chủ! \r\n {ex.Message}", "Lỗi khởi tạo API!", MessageBoxButtons.OK, MessageBoxIcon.Error);
btnLogin.Enabled = true;
applicationLogger.Log(LogLevel.Error, "Cấu máy chủ lỗi - Kiểm tra lại tên máy chủ, kết nối máy chủ! Không thể khởi tạo API Webserver");
return;
}
@ -59,8 +65,9 @@ namespace AIParkingApplication
string password = txtPassword.Text;
if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password))
{
lblLoginStatus.Text = "Tên tài khoản hoặc mật khẩu không được để trống!";
lblLoginStatus.Text = "Tên tài khoản hoặc mật khẩu không được để trống";
btnLogin.Enabled = true;
applicationLogger.Log(LogLevel.Error, "Tên tài khoản hoặc mật khẩu không được để trống");
return;
}
@ -72,23 +79,27 @@ namespace AIParkingApplication
if (execeptioMessage.Contains("Error converting value"))
{
lblLoginStatus.UpdateLabel("Sai tên tài khoản hoặc mật khẩu!", Color.Red);
applicationLogger.Log(LogLevel.Error, "Sai tên tài khoản hoặc mật khẩu");
return;
}
if (execeptioMessage.Contains("An error occurred while sending the request"))
{
lblLoginStatus.UpdateLabel("Không có kết nối tới server!\r\nKiểm tra lại kết nối tới server!", Color.Red, Color.White);
applicationLogger.Log(LogLevel.Error, "Không có kết nối tới server!");
return;
}
lblLoginStatus.UpdateLabel("Không có kết nối tới server!\r\nKiểm tra lại kết nối tới server!", Color.Red, Color.White);
applicationLogger.Log(LogLevel.Error, "Không có kết nối tới server!");
}
if (loginResult.IsLoginSuccess)
{
Util.AddOrUpdateAppSettings("DEFAULT_USERNAME", txtUsername.Text);
Util.AddOrUpdateAppSettings("DEFAULT_PASSWORD", txtPassword.Text);
new AIParkingApplicationForm(apiController, ipAddress, loginResult.LoginData).Show();
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}");
}
}
@ -101,6 +112,7 @@ namespace AIParkingApplication
{
if (DialogResult.OK == MessageBox.Show("AIParking - Bạn muốn thoát ứng dụng?", "Cảnh báo!", MessageBoxButtons.OKCancel, MessageBoxIcon.Question))
{
applicationLogger.Log(LogLevel.Info, "Login - Thoát ứng dụng");
Application.Exit();
}
}
@ -109,14 +121,17 @@ namespace AIParkingApplication
{
try
{
txtUsername.Text = ConfigurationManager.AppSettings["DEFAULT_USERNAME"];
txtUsername.Text = ConfigurationManager.AppSettings["DEFAULT_USERNAME"];
txtPassword.Text = ConfigurationManager.AppSettings["DEFAULT_PASSWORD"];
}
catch (Exception ex)
{
Console.WriteLine($"{DateTime.Now.GetTimeFormatted()}\tReadAccessControlDeviceIPConfiguration\t{ex.Message}");
txtUsername.Text = string.Empty;
txtPassword.Text = string.Empty;
Util.AddOrUpdateAppSettings("DEFAULT_USERNAME", string.Empty);
Util.AddOrUpdateAppSettings("DEFAULT_PASSWORD", string.Empty);
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");
}
}

View File

@ -6,24 +6,16 @@
throwExceptions="false"
internalLogLevel="Off" internalLogFile="D:\nlog-internal.log">
<variable name="logDirectory" value="${basedir}/logs"/>
<variable name="logLayout" value="${date:format=HH\:mm\:ss dd-MM-yyyy} ${uppercase:${level}} ${message} ${exception}"/>
<targets>
<target xsi:type="File" name="applicationLogger" fileName="${logDirectory}/${shortdate}_App.log"
layout="${uppercase:${level}} ${message} ${exception}"
maxArchiveFiles="30"
encoding="Unicode"
writeBom="true"/>
layout="${logLayout}" maxArchiveFiles="30" encoding="Unicode" writeBom="true"/>
<target xsi:type="File" name="pingServerLoggingFile" fileName="${logDirectory}/${shortdate}_PingServer.log"
layout="${uppercase:${level}} ${message} ${exception}"
maxArchiveFiles="30"
encoding="Unicode"
writeBom="true"/>
layout="${logLayout}" maxArchiveFiles="30" encoding="Unicode" writeBom="true"/>
<target xsi:type="File" name="pingDoorDeviceControlAccessLoggingFile" fileName="${logDirectory}/${shortdate}_PingDoorDeviceControlAccess.log"
layout="${uppercase:${level}} ${message} ${exception}"
maxArchiveFiles="30"
encoding="Unicode"
writeBom="true"/>
layout="${logLayout}" maxArchiveFiles="30" encoding="Unicode" writeBom="true"/>
</targets>
<rules>
<logger name="ApplicationLogger" minlevel="Trace" writeTo="applicationLogger" />

View File

@ -1,4 +1,5 @@
using Microsoft.Win32;
using NLog;
using System;
using System.IO;
using System.Windows.Forms;
@ -7,8 +8,11 @@ namespace AIParkingApplication
{
public class Printer
{
public Printer()
private Logger appLogger;
public Printer(Logger appLogger)
{
this.appLogger = appLogger;
SetupPrinterPageSetting();
}
@ -43,6 +47,7 @@ namespace AIParkingApplication
catch (Exception ex)
{
Console.WriteLine($"ProcessingString\texMessage:{ex.Message}");
appLogger.Log(LogLevel.Info, $"Lỗi khi In hóa đơn: {ex.Message}");
return string.Empty;
}
}