diff --git a/BITable/BiTable.cs b/BITable/BiTable.cs index 31b08bb..4dc3582 100644 --- a/BITable/BiTable.cs +++ b/BITable/BiTable.cs @@ -9,6 +9,7 @@ using System.Threading.Tasks; using Newtonsoft.Json; using System.Drawing; using System.Net.Http; +using System.Threading; namespace BITable { @@ -137,57 +138,60 @@ namespace BITable private void ShowImageOnUI(RequestResult requestResult) { - foreach (var item in requestResult.Data) + if (requestResult != null && requestResult.Data != null) { - if (!string.IsNullOrEmpty(item.Base64)) + foreach (var item in requestResult.Data) { - if (item.Type == "MT" || item.Type == "MS" || item.Type == "PRE-CMT") + if (!string.IsNullOrEmpty(item.Base64)) { - pictureBoxIDCard.Invoke(new Action(() => + if (item.Type == "MT" || item.Type == "MS" || item.Type == "PRE-CMT") { - pictureBoxIDCard.Image?.Dispose(); - pictureBoxIDCard.Image = ParseImageFromBase64(item.Base64); - })); - } - else - { - pictureBoxFaceImage.Invoke(new Action(() => + pictureBoxIDCard.Invoke(new Action(() => + { + pictureBoxIDCard.Image?.Dispose(); + pictureBoxIDCard.Image = ParseImageFromBase64(item.Base64); + })); + } + else { - pictureBoxFaceImage.Image?.Dispose(); - pictureBoxFaceImage.Image = ParseImageFromBase64(item.Base64); - })); + pictureBoxFaceImage.Invoke(new Action(() => + { + pictureBoxFaceImage.Image?.Dispose(); + pictureBoxFaceImage.Image = ParseImageFromBase64(item.Base64); + })); + } } } - } - switch (requestResult.Code) - { - case 2: - MessageBox.Show("Không chụp được ảnh chân dung", "Cảnh báo"); - break; - case 3: - MessageBox.Show("Không chụp được ảnh CMT (mặt trước hoặc măt sau)", "Cảnh báo"); - break; - case 4: - MessageBox.Show("Không chụp được ảnh chân dung và ảnh CMT", "Cảnh báo"); - break; - case 5: - MessageBox.Show("Không chụp được ảnh mặt trước CMT", "Cảnh báo"); - break; - case 6: - MessageBox.Show("Không chụp được ảnh mặt sau CMT", "Cảnh báo"); - break; - case 7: - MessageBox.Show("Không chụp được ảnh preview camera chân dung", "Cảnh báo"); - break; - case 8: - MessageBox.Show("Không chụp được ảnh preview CMT", "Cảnh báo"); - break; - case 400: - MessageBox.Show("Request bị sai cú pháp", "Cảnh báo"); - break; - case 401: - MessageBox.Show("Thiết bị đang xử lý request khác", "Cảnh báo"); - break; + switch (requestResult.Code) + { + case 2: + MessageBox.Show("Không chụp được ảnh chân dung", "Cảnh báo"); + break; + case 3: + MessageBox.Show("Không chụp được ảnh CMT (mặt trước hoặc măt sau)", "Cảnh báo"); + break; + case 4: + MessageBox.Show("Không chụp được ảnh chân dung và ảnh CMT", "Cảnh báo"); + break; + case 5: + MessageBox.Show("Không chụp được ảnh mặt trước CMT", "Cảnh báo"); + break; + case 6: + MessageBox.Show("Không chụp được ảnh mặt sau CMT", "Cảnh báo"); + break; + case 7: + MessageBox.Show("Không chụp được ảnh preview camera chân dung", "Cảnh báo"); + break; + case 8: + MessageBox.Show("Không chụp được ảnh preview CMT", "Cảnh báo"); + break; + case 400: + MessageBox.Show("Request bị sai cú pháp", "Cảnh báo"); + break; + case 401: + MessageBox.Show("Thiết bị đang xử lý request khác", "Cảnh báo"); + break; + } } } @@ -247,7 +251,6 @@ namespace BITable { if (!string.IsNullOrEmpty(txtIPAddress.Text) && !string.IsNullOrEmpty(deviceUrl)) { - DoRequestAPI(1, true, true); } else @@ -415,6 +418,7 @@ namespace BITable { lblStatus.Text = "ĐANG XỬ LÝ"; })); + if (isClearIDCard) { pictureBoxIDCard.Invoke(new Action(() => @@ -476,33 +480,56 @@ namespace BITable { lblStatusSign.Invoke(new Action(() => { - lblStatusSign.BackColor = Color.Blue; + lblStatusSign.BackColor = Color.Green; })); } } else { - using (var client = new HttpClient { BaseAddress = new Uri("http://api.bitableconnect.beetai.com/"), Timeout = TimeSpan.FromMilliseconds(10000) }) + using (var client = new HttpClient { BaseAddress = new Uri("http://api.bitableconnect.beetai.com/"), Timeout = TimeSpan.FromMilliseconds(20000) }) { - string deviceName = string.Empty; - txtDeviceName.Invoke(new Action(() => + try { - deviceName = txtDeviceName.Text; - })); + string deviceName = string.Empty; + txtDeviceName.Invoke(new Action(() => + { + deviceName = txtDeviceName.Text; + })); - HttpResponseMessage response = await client.GetAsync($"/get-ip-box?id={deviceName}"); - response.EnsureSuccessStatusCode(); - var result = await response.Content.ReadAsAsync(); - if (!string.IsNullOrEmpty(result.Error)) - { - MessageBox.Show("Thiết bị chưa được đăng ký!"); - return; + HttpResponseMessage response = await client.GetAsync($"/get-ip-box?id={deviceName}"); + response.EnsureSuccessStatusCode(); + var result = await response.Content.ReadAsAsync(); + if (!string.IsNullOrEmpty(result.Error)) + { + MessageBox.Show("Thiết bị chưa được đăng ký!"); + return; + } + deviceUrl = result.Url; + txtIPAddress.Invoke(new Action(() => + { + txtIPAddress.Text = deviceUrl.Substring(7, deviceUrl.Length - 28); + })); + + if (!Util.IsPingable(txtIPAddress.Text, out long pingTime)) + { + lblStatusSign.Invoke(new Action(() => + { + lblStatusSign.BackColor = Color.Red; + })); + MessageBox.Show("Không thể ping tới thiết bị!"); + } + else + { + lblStatusSign.Invoke(new Action(() => + { + lblStatusSign.BackColor = Color.Green; + })); + } } - deviceUrl = result.Url; - txtIPAddress.Invoke(new Action(() => + catch (Exception) { - txtIPAddress.Text = deviceUrl.Substring(7, deviceUrl.Length - 28); - })); + MessageBox.Show("Kết nối tới server api gặp sự cố!"); + } } } }