Check requestResult.Data null, Set lblStatusSign.BackColor Status

This commit is contained in:
DucDangAnh 2020-06-20 19:35:41 +07:00
parent 2aa346ec64
commit 241162c5ee

View File

@ -9,6 +9,7 @@ using System.Threading.Tasks;
using Newtonsoft.Json; using Newtonsoft.Json;
using System.Drawing; using System.Drawing;
using System.Net.Http; using System.Net.Http;
using System.Threading;
namespace BITable namespace BITable
{ {
@ -137,57 +138,60 @@ namespace BITable
private void ShowImageOnUI(RequestResult requestResult) 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.Invoke(new Action(() =>
pictureBoxIDCard.Image = ParseImageFromBase64(item.Base64); {
})); pictureBoxIDCard.Image?.Dispose();
} pictureBoxIDCard.Image = ParseImageFromBase64(item.Base64);
else }));
{ }
pictureBoxFaceImage.Invoke(new Action(() => else
{ {
pictureBoxFaceImage.Image?.Dispose(); pictureBoxFaceImage.Invoke(new Action(() =>
pictureBoxFaceImage.Image = ParseImageFromBase64(item.Base64); {
})); pictureBoxFaceImage.Image?.Dispose();
pictureBoxFaceImage.Image = ParseImageFromBase64(item.Base64);
}));
}
} }
} }
} switch (requestResult.Code)
switch (requestResult.Code) {
{ case 2:
case 2: MessageBox.Show("Không chụp được ảnh chân dung", "Cảnh báo");
MessageBox.Show("Không chụp được ảnh chân dung", "Cảnh báo"); break;
break; case 3:
case 3: MessageBox.Show("Không chụp được ảnh CMT (mặt trước hoặc măt sau)", "Cảnh báo");
MessageBox.Show("Không chụp được ảnh CMT (mặt trước hoặc măt sau)", "Cảnh báo"); break;
break; case 4:
case 4: MessageBox.Show("Không chụp được ảnh chân dung và ảnh CMT", "Cảnh báo");
MessageBox.Show("Không chụp được ảnh chân dung và ảnh CMT", "Cảnh báo"); break;
break; case 5:
case 5: MessageBox.Show("Không chụp được ảnh mặt trước CMT", "Cảnh báo");
MessageBox.Show("Không chụp được ảnh mặt trước CMT", "Cảnh báo"); break;
break; case 6:
case 6: MessageBox.Show("Không chụp được ảnh mặt sau CMT", "Cảnh báo");
MessageBox.Show("Không chụp được ảnh mặt sau CMT", "Cảnh báo"); break;
break; case 7:
case 7: MessageBox.Show("Không chụp được ảnh preview camera chân dung", "Cảnh báo");
MessageBox.Show("Không chụp được ảnh preview camera chân dung", "Cảnh báo"); break;
break; case 8:
case 8: MessageBox.Show("Không chụp được ảnh preview CMT", "Cảnh báo");
MessageBox.Show("Không chụp được ảnh preview CMT", "Cảnh báo"); break;
break; case 400:
case 400: MessageBox.Show("Request bị sai cú pháp", "Cảnh báo");
MessageBox.Show("Request bị sai cú pháp", "Cảnh báo"); break;
break; case 401:
case 401: MessageBox.Show("Thiết bị đang xử lý request khác", "Cảnh báo");
MessageBox.Show("Thiết bị đang xử lý request khác", "Cảnh báo"); break;
break; }
} }
} }
@ -247,7 +251,6 @@ namespace BITable
{ {
if (!string.IsNullOrEmpty(txtIPAddress.Text) && !string.IsNullOrEmpty(deviceUrl)) if (!string.IsNullOrEmpty(txtIPAddress.Text) && !string.IsNullOrEmpty(deviceUrl))
{ {
DoRequestAPI(1, true, true); DoRequestAPI(1, true, true);
} }
else else
@ -415,6 +418,7 @@ namespace BITable
{ {
lblStatus.Text = "ĐANG XỬ LÝ"; lblStatus.Text = "ĐANG XỬ LÝ";
})); }));
if (isClearIDCard) if (isClearIDCard)
{ {
pictureBoxIDCard.Invoke(new Action(() => pictureBoxIDCard.Invoke(new Action(() =>
@ -476,33 +480,56 @@ namespace BITable
{ {
lblStatusSign.Invoke(new Action(() => lblStatusSign.Invoke(new Action(() =>
{ {
lblStatusSign.BackColor = Color.Blue; lblStatusSign.BackColor = Color.Green;
})); }));
} }
} }
else 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; try
txtDeviceName.Invoke(new Action(() =>
{ {
deviceName = txtDeviceName.Text; string deviceName = string.Empty;
})); txtDeviceName.Invoke(new Action(() =>
{
deviceName = txtDeviceName.Text;
}));
HttpResponseMessage response = await client.GetAsync($"/get-ip-box?id={deviceName}"); HttpResponseMessage response = await client.GetAsync($"/get-ip-box?id={deviceName}");
response.EnsureSuccessStatusCode(); response.EnsureSuccessStatusCode();
var result = await response.Content.ReadAsAsync<DeviceUrlModel>(); var result = await response.Content.ReadAsAsync<DeviceUrlModel>();
if (!string.IsNullOrEmpty(result.Error)) if (!string.IsNullOrEmpty(result.Error))
{ {
MessageBox.Show("Thiết bị chưa được đăng ký!"); MessageBox.Show("Thiết bị chưa được đăng ký!");
return; 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; catch (Exception)
txtIPAddress.Invoke(new Action(() =>
{ {
txtIPAddress.Text = deviceUrl.Substring(7, deviceUrl.Length - 28); MessageBox.Show("Kết nối tới server api gặp sự cố!");
})); }
} }
} }
} }