diff --git a/AIParkingApplication/AIParkingApplicationForm.cs b/AIParkingApplication/AIParkingApplicationForm.cs index b807aae..956191c 100644 --- a/AIParkingApplication/AIParkingApplicationForm.cs +++ b/AIParkingApplication/AIParkingApplicationForm.cs @@ -20,7 +20,7 @@ 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; diff --git a/AIParkingApplication/ApiController.cs b/AIParkingApplication/ApiController.cs index 469caca..0a02a20 100644 --- a/AIParkingApplication/ApiController.cs +++ b/AIParkingApplication/ApiController.cs @@ -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; diff --git a/AIParkingApplication/LoginForm.Designer.cs b/AIParkingApplication/LoginForm.Designer.cs index 6e77344..ac4bf11 100644 --- a/AIParkingApplication/LoginForm.Designer.cs +++ b/AIParkingApplication/LoginForm.Designer.cs @@ -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 // diff --git a/AIParkingApplication/LoginForm.cs b/AIParkingApplication/LoginForm.cs index 85a376f..2ae95d3 100644 --- a/AIParkingApplication/LoginForm.cs +++ b/AIParkingApplication/LoginForm.cs @@ -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(); diff --git a/AIParkingApplication/Util.cs b/AIParkingApplication/Util.cs index 7b1e78d..6886a31 100644 --- a/AIParkingApplication/Util.cs +++ b/AIParkingApplication/Util.cs @@ -154,12 +154,25 @@ namespace AIParkingApplication return isValidIPAddress; } - public static bool IsUrlValid(string url) + public static void ParseHostString(string hostString, out string hostName, out int port) { + if (!hostString.Contains(":")) + { + hostName = hostString; + port = 80; + return; + } - 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); + string[] hostParts = hostString.Split(':'); + if (hostParts.Length != 2) + { + hostName = string.Empty; + port = 0; + return; + } + + hostName = hostParts[0]; + int.TryParse(hostParts[1], out port); } }