144 lines
6.2 KiB
C#
144 lines
6.2 KiB
C#
using NLog;
|
|
using System;
|
|
using System.Configuration;
|
|
using System.Drawing;
|
|
using System.IO;
|
|
using System.Windows.Forms;
|
|
|
|
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);
|
|
}
|
|
txtUsername.Focus();
|
|
lblLoginStatus.Text = string.Empty;
|
|
AcceptButton = btnLogin;
|
|
}
|
|
|
|
private async void Login()
|
|
{
|
|
btnLogin.Enabled = false;
|
|
string serverUrl = txtServerAddress.Text;
|
|
if (string.IsNullOrEmpty(serverUrl))
|
|
{
|
|
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.Info, "Địa chỉ server không được để trống");
|
|
return;
|
|
}
|
|
|
|
Util.ParseHostString(serverUrl, out string ipAddress, out int port);
|
|
|
|
if (!Util.IsValidIPAddress(ipAddress))
|
|
{
|
|
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.Info, "Địa chỉ server không đúng định dạng");
|
|
return;
|
|
}
|
|
|
|
try
|
|
{
|
|
apiController = new ApiController(serverUrl, applicationLogger);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
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. ex: {ex.Message}");
|
|
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;
|
|
return;
|
|
}
|
|
|
|
lblLoginStatus.Text = string.Empty;
|
|
string username = txtUsername.Text;
|
|
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";
|
|
btnLogin.Enabled = true;
|
|
applicationLogger.Log(LogLevel.Info, "Tên tài khoản hoặc mật khẩu không được để trống");
|
|
return;
|
|
}
|
|
|
|
var loginResult = await apiController.Login(new LoginModel { Username = username, Password = password });
|
|
if (loginResult.Exception != null)
|
|
{
|
|
btnLogin.Enabled = true;
|
|
var execeptioMessage = loginResult.Exception.Message;
|
|
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.Info, "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, applicationLogger).Show();
|
|
Hide();
|
|
applicationLogger.Log(LogLevel.Info, $"Đăng nhập thành công với tài khoản: {txtUsername.Text}");
|
|
}
|
|
}
|
|
|
|
private void btnLogin_Click(object sender, EventArgs e)
|
|
{
|
|
Login();
|
|
}
|
|
|
|
private void btnExit_Click(object sender, EventArgs e)
|
|
{
|
|
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();
|
|
}
|
|
}
|
|
|
|
private void ReadAppConfigurationFromFile()
|
|
{
|
|
try
|
|
{
|
|
txtUsername.Text = ConfigurationManager.AppSettings["DEFAULT_USERNAME"];
|
|
txtPassword.Text = ConfigurationManager.AppSettings["DEFAULT_PASSWORD"];
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
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");
|
|
}
|
|
}
|
|
|
|
private void LoginForm_Load(object sender, EventArgs e)
|
|
{
|
|
ReadAppConfigurationFromFile();
|
|
}
|
|
}
|
|
}
|