Compare commits

...

4 Commits

Author SHA1 Message Date
74855b6467 LoginForm - Check IP Address Input. 2020-07-13 14:51:44 +07:00
728f65cd90 StatusBar - Remove updateInterval 2020-07-13 11:04:48 +07:00
50301189d8 Update App Title 2020-07-13 10:50:00 +07:00
6d7125511c Util - Add IsValidIPAddress 2020-07-13 10:48:20 +07:00
7 changed files with 58 additions and 26 deletions

View File

@ -20,14 +20,14 @@ namespace AIParkingApplication
private EngineApiController engineApiController;
private string doorAccessControlDeviceIP;
public AIParkingApplicationForm(ApiController apiController, Config configOnWeb)
public AIParkingApplicationForm(ApiController apiController, string serverIPAddress, Config configOnWeb)
{
InitializeComponent();
this.apiController = apiController;
this.configOnWeb = configOnWeb;
ReadAccessControlDeviceIPConfiguration();
if (string.IsNullOrEmpty(doorAccessControlDeviceIP))
if (string.IsNullOrEmpty(doorAccessControlDeviceIP) || !Util.IsValidIPAddress(doorAccessControlDeviceIP))
{
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();
@ -40,7 +40,7 @@ namespace AIParkingApplication
};
Controls.Add(sidebar);
statusBar = new StatusBar("192.168.1.122", doorAccessControlDeviceIP, TimeSpan.FromSeconds(1))
statusBar = new StatusBar("192.168.1.122", doorAccessControlDeviceIP)
{
Location = new Point(0, sidebar.Location.Y + sidebar.Height + 26),
Anchor = AnchorStyles.Bottom | AnchorStyles.Left
@ -56,8 +56,6 @@ namespace AIParkingApplication
MessageBox.Show("Cấu hình API Plate Recognize lỗi!", "Cấu hình API Engine lỗi!", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
private void InitAllLanes()

View File

@ -13,11 +13,12 @@ namespace AIParkingApplication
private HttpClient httpClient;
private bool isHttpClientDisposabled;
public ApiController(string baseAddress, int numberOfRetry = 5)
public ApiController(string serverIPAddress, int numberOfRetry = 5)
{
httpClient = new HttpClient
{
BaseAddress = new Uri(baseAddress),
BaseAddress = new Uri($"http://{serverIPAddress}"),
Timeout = TimeSpan.FromSeconds(5)
};
isHttpClientDisposabled = false;

View File

@ -92,7 +92,7 @@
this.txtServerAddress.Name = "txtServerAddress";
this.txtServerAddress.Size = new System.Drawing.Size(227, 20);
this.txtServerAddress.TabIndex = 1;
this.txtServerAddress.Text = "http://localhost:80/";
this.txtServerAddress.Text = "192.168.1.122:80";
//
// lblServerAddress
//
@ -152,7 +152,7 @@
this.MaximumSize = new System.Drawing.Size(550, 250);
this.Name = "LoginForm";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "AIParking - Hệ thống quản lý bãi đỗ xe - Đăng nhập hệ thống";
this.Text = "AIParking - Hệ thống quản lý bãi đỗ xe - Đăng nhập";
this.TopMost = true;
((System.ComponentModel.ISupportInitialize)(this.pictureBoxImageLogo)).EndInit();
this.ResumeLayout(false);

View File

@ -23,25 +23,32 @@ namespace AIParkingApplication
private async void Login()
{
if (string.IsNullOrEmpty(txtServerAddress.Text))
btnLogin.Enabled = false;
string serverUrl = txtServerAddress.Text;
if (string.IsNullOrEmpty(serverUrl))
{
lblLoginStatus.Text = "Địa chỉ server không được để trống!";
lblLoginStatus.Text = "Địa chỉ server không được để trống! \r\n ví dụ: 192.168.1.2:80";
btnLogin.Enabled = true;
return;
}
if (!Util.IsUrlValid(txtServerAddress.Text))
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 http://localhost:80 hoặc http://192.168.1.2:80";
lblLoginStatus.Text = "Địa chỉ server không đúng định dạng: \r\n ví dụ: 192.168.1.122:80";
btnLogin.Enabled = true;
return;
}
try
{
apiController = new ApiController(txtServerAddress.Text);
apiController = new ApiController(serverUrl);
}
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;
return;
}
@ -51,12 +58,14 @@ namespace AIParkingApplication
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;
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"))
{
@ -74,11 +83,13 @@ namespace AIParkingApplication
if (loginResult.IsLoginSuccess)
{
new AIParkingApplicationForm(apiController, loginResult.LoginData).Show();
new AIParkingApplicationForm(apiController, ipAddress, loginResult.LoginData).Show();
Hide();
}
}
private void btnLogin_Click(object sender, EventArgs e)
{
Login();

View File

@ -4,12 +4,12 @@ using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("AIParkingApplication")]
[assembly: AssemblyTitle("AIParking - Phần mềm quản lý bãi đỗ xe")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("AIParkingApplication")]
[assembly: AssemblyCopyright("Copyright © 2020")]
[assembly: AssemblyCopyright("Copyright © DucDA 2020")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

View File

@ -11,17 +11,15 @@ namespace AIParkingApplication
{
private string webServerIP;
private string doorAccessControlDeviceIP;
private TimeSpan updateInterval;
private const string engineFilePath = @"\engine.bat";
private Thread updateInfoThread;
public StatusBar(string webServerIP, string doorAccessControlDeviceIP, TimeSpan updateInterval)
public StatusBar(string webServerIP, string doorAccessControlDeviceIP)
{
InitializeComponent();
this.webServerIP = webServerIP;
this.doorAccessControlDeviceIP = doorAccessControlDeviceIP;
this.updateInterval = updateInterval;
updateInfoThread = new Thread(new ThreadStart(UpdateStatus)) { IsBackground = true };
updateInfoThread = new Thread(UpdateStatus) { IsBackground = true };
}
private void UpdateStatus()
@ -56,7 +54,7 @@ namespace AIParkingApplication
lblEngineStatus.UpdateLabel("ĐANG HOẠT ĐỘNG", Color.Green);
}
}
Thread.Sleep(updateInterval);
Thread.Sleep(TimeSpan.FromSeconds(1));
}
}

View File

@ -3,6 +3,7 @@ using System;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Net;
using System.Net.NetworkInformation;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
@ -143,12 +144,35 @@ namespace AIParkingApplication
}
}
public static bool IsUrlValid(string url)
public static bool IsValidIPAddress(string ipAddress)
{
if (string.IsNullOrEmpty(ipAddress))
{
return false;
}
bool isValidIPAddress = IPAddress.TryParse(ipAddress, out IPAddress ip);
return isValidIPAddress;
}
string pattern = @"^(http|https|ftp|)\://|[a-zA-Z0-9\-\.]+\.[a-zA-Z](:[a-zA-Z0-9]*)?/?([a-zA-Z0-9\-\._\?\,\'/\\\+&%\$#\=~])*[^\.\,\)\(\s]$";
Regex reg = new Regex(pattern, RegexOptions.Compiled | RegexOptions.IgnoreCase);
return reg.IsMatch(url);
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);
}
}