570 lines
20 KiB
C#
570 lines
20 KiB
C#
using System;
|
|
using System.Data;
|
|
using System.Linq;
|
|
using System.Windows.Forms;
|
|
using InTheHand.Net.Bluetooth;
|
|
using InTheHand.Net.Sockets;
|
|
using System.IO;
|
|
using System.Threading.Tasks;
|
|
using Newtonsoft.Json;
|
|
using System.Drawing;
|
|
using System.Net.Http;
|
|
using System.Threading;
|
|
|
|
namespace BITable
|
|
{
|
|
public partial class BiTable : Form
|
|
{
|
|
private BluetoothClient client;
|
|
private BluetoothDeviceInfo[] devices;
|
|
private string currentBluetoothDeviceName;
|
|
private string deviceUrl;
|
|
|
|
public BiTable()
|
|
{
|
|
InitializeComponent();
|
|
BluetoothRadio.PrimaryRadio.Mode = RadioMode.Connectable;
|
|
client = new BluetoothClient();
|
|
lblStatus.Text = string.Empty;
|
|
}
|
|
|
|
//public void TextGotFocus(object sender, EventArgs e)
|
|
//{
|
|
// TextBox tb = (TextBox)sender;
|
|
// if (tb.Text == "Your Text ..........")
|
|
// {
|
|
// tb.Text = "";
|
|
// tb.ForeColor = Color.Black;
|
|
// }
|
|
//}
|
|
|
|
//public void TextLostFocus(object sender, EventArgs e)
|
|
//{
|
|
// TextBox tb = (TextBox)sender;
|
|
// if (tb.Text == "")
|
|
// {
|
|
// tb.Text = "Your Text ..........";
|
|
// tb.ForeColor = Color.LightGray;
|
|
// }
|
|
//}
|
|
|
|
//private void SetupTextBox()
|
|
//{
|
|
// txtDeviceName.GotFocus += new EventHandler(this.TextGotFocus);
|
|
// txtDeviceName.LostFocus += new EventHandler(this.TextLostFocus);
|
|
//}
|
|
|
|
private void ScanBluetoothDevices()
|
|
{
|
|
dgvBluetoothDevices.Invoke(new Action(() =>
|
|
{
|
|
dgvBluetoothDevices.Rows.Clear();
|
|
}));
|
|
|
|
devices = client.DiscoverDevices();
|
|
foreach (var device in devices)
|
|
{
|
|
Console.WriteLine(device.DeviceName);
|
|
dgvBluetoothDevices.Invoke(new Action(() =>
|
|
{
|
|
dgvBluetoothDevices.Rows.Add(device.DeviceName);
|
|
}));
|
|
}
|
|
}
|
|
|
|
private void btnScanBluetoothDevices_Click(object sender, EventArgs e)
|
|
{
|
|
Task.Factory.StartNew(new Action(() =>
|
|
{
|
|
btnScanBluetoothDevices.Invoke(new Action(() =>
|
|
{
|
|
btnScanBluetoothDevices.Enabled = false;
|
|
}));
|
|
ScanBluetoothDevices();
|
|
})).ContinueWith(t =>
|
|
{
|
|
btnScanBluetoothDevices.Invoke(new Action(() =>
|
|
{
|
|
btnScanBluetoothDevices.Enabled = true;
|
|
}));
|
|
});
|
|
}
|
|
|
|
private void dgvBluetoothDevices_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
|
|
{
|
|
currentBluetoothDeviceName = dgvBluetoothDevices.Rows[e.RowIndex].Cells[0].Value.ToString();
|
|
var neededDevice = devices.Where(x => x.DeviceName == currentBluetoothDeviceName).FirstOrDefault();
|
|
if (neededDevice != null)
|
|
{
|
|
client.BeginConnect(neededDevice.DeviceAddress, BluetoothService.SerialPort, new AsyncCallback(Connect), client);
|
|
}
|
|
}
|
|
|
|
private void Connect(IAsyncResult result)
|
|
{
|
|
if (client.Connected)
|
|
{
|
|
SendRequestToDevice("{\"option\":\"0\", \"value\":\"0B8N62R9\"}\n");
|
|
lblConnectedDevice.Invoke(new Action(() =>
|
|
{
|
|
lblConnectedDevice.Text = $"Trạng thái: Đã kết nối tới {currentBluetoothDeviceName}";
|
|
}));
|
|
}
|
|
else
|
|
{
|
|
lblConnectedDevice.Invoke(new Action(() =>
|
|
{
|
|
lblConnectedDevice.Text = $"Trạng thái: Không thuể kết nối tới {currentBluetoothDeviceName}";
|
|
}));
|
|
}
|
|
}
|
|
|
|
private string SendRequestToDevice(string jsonObject)
|
|
{
|
|
Stream stream = client.GetStream();
|
|
stream.ReadTimeout = 20000;
|
|
StreamWriter streamWriter = new StreamWriter(stream);
|
|
streamWriter.Write(jsonObject); //"{\"option\":\"0\", \"value\":\"0B8N62R9\"}\n"
|
|
streamWriter.Flush();
|
|
StreamReader streamReader = new StreamReader(stream);
|
|
string result = streamReader.ReadLine();
|
|
return result;
|
|
}
|
|
|
|
private string CreateJsonString(int option, int timeout)
|
|
{
|
|
return "{\"option\":\"" + option + "\", \"timeout\":\"" + timeout + "\"}\n";
|
|
}
|
|
|
|
private void ShowImageOnUI(RequestResult requestResult)
|
|
{
|
|
if (requestResult != null && requestResult.Data != null)
|
|
{
|
|
foreach (var item in requestResult.Data)
|
|
{
|
|
if (!string.IsNullOrEmpty(item.Base64))
|
|
{
|
|
if (item.Type == "MT" || item.Type == "MS" || item.Type == "PRE-CMT")
|
|
{
|
|
pictureBoxIDCard.Invoke(new Action(() =>
|
|
{
|
|
pictureBoxIDCard.Image?.Dispose();
|
|
pictureBoxIDCard.Image = ParseImageFromBase64(item.Base64);
|
|
}));
|
|
}
|
|
else
|
|
{
|
|
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;
|
|
}
|
|
}
|
|
}
|
|
|
|
private void DoRequest(int option, bool isClearFaceImage, bool isClearIDCard)
|
|
{
|
|
Task.Factory.StartNew(new Action(() =>
|
|
{
|
|
lblStatus.Invoke(new Action(() =>
|
|
{
|
|
lblStatus.Text = "ĐANG XỬ LÝ";
|
|
}));
|
|
if (isClearIDCard)
|
|
{
|
|
pictureBoxIDCard.Invoke(new Action(() =>
|
|
{
|
|
pictureBoxIDCard.Image?.Dispose();
|
|
pictureBoxIDCard.Image = null;
|
|
}));
|
|
}
|
|
|
|
if (isClearFaceImage)
|
|
{
|
|
pictureBoxFaceImage.Invoke(new Action(() =>
|
|
{
|
|
pictureBoxFaceImage.Image?.Dispose();
|
|
pictureBoxFaceImage.Image = null;
|
|
}));
|
|
}
|
|
string jsonObject = CreateJsonString(option, 6);
|
|
string result = SendRequestToDevice(jsonObject);
|
|
var objectParsed = JsonConvert.DeserializeObject<RequestResult>(result);
|
|
ShowImageOnUI(objectParsed);
|
|
})).ContinueWith(t =>
|
|
{
|
|
lblStatus.Invoke(new Action(() =>
|
|
{
|
|
lblStatus.Text = string.Empty;
|
|
}));
|
|
});
|
|
}
|
|
|
|
private void btnFaceAndPaper_Click(object sender, EventArgs e)
|
|
{
|
|
if (radioButtonBluetooth.Checked)
|
|
{
|
|
if (!string.IsNullOrEmpty(currentBluetoothDeviceName))
|
|
{
|
|
DoRequest(1, true, true);
|
|
}
|
|
else
|
|
{
|
|
MessageBox.Show("Chưa kết nối tới thiết bị BI Table qua Bluetooth!");
|
|
}
|
|
}
|
|
|
|
if (radioButtonIpApi.Checked)
|
|
{
|
|
if (!string.IsNullOrEmpty(txtIPAddress.Text) && !string.IsNullOrEmpty(deviceUrl))
|
|
{
|
|
DoRequestAPI(1, true, true);
|
|
}
|
|
else
|
|
{
|
|
MessageBox.Show("Chưa kết nối tới thiết bị BI Table!");
|
|
}
|
|
}
|
|
}
|
|
|
|
private Bitmap ParseImageFromBase64(string imageBase64)
|
|
{
|
|
byte[] imageData = Convert.FromBase64String(imageBase64);
|
|
using (var ms = new MemoryStream(imageData))
|
|
{
|
|
Bitmap image = new Bitmap(ms);
|
|
return image;
|
|
}
|
|
}
|
|
|
|
private void btnFace_Click(object sender, EventArgs e)
|
|
{
|
|
if (radioButtonBluetooth.Checked)
|
|
{
|
|
if (!string.IsNullOrEmpty(currentBluetoothDeviceName))
|
|
{
|
|
DoRequest(2, true, false);
|
|
}
|
|
else
|
|
{
|
|
MessageBox.Show("Chưa kết nối tới thiết bị BI Table qua Bluetooth!");
|
|
}
|
|
}
|
|
|
|
if (radioButtonIpApi.Checked)
|
|
{
|
|
if (!string.IsNullOrEmpty(txtIPAddress.Text) && !string.IsNullOrEmpty(deviceUrl))
|
|
{
|
|
|
|
DoRequestAPI(2, true, false);
|
|
}
|
|
else
|
|
{
|
|
MessageBox.Show("Chưa kết nối tới thiết bị BI Table!");
|
|
}
|
|
}
|
|
}
|
|
|
|
private void btnFontPaper_Click(object sender, EventArgs e)
|
|
{
|
|
if (radioButtonBluetooth.Checked)
|
|
{
|
|
if (!string.IsNullOrEmpty(currentBluetoothDeviceName))
|
|
{
|
|
DoRequest(3, false, true);
|
|
}
|
|
else
|
|
{
|
|
MessageBox.Show("Chưa kết nối tới thiết bị BI Table qua Bluetooth!");
|
|
}
|
|
}
|
|
|
|
if (radioButtonIpApi.Checked)
|
|
{
|
|
if (!string.IsNullOrEmpty(txtIPAddress.Text) && !string.IsNullOrEmpty(deviceUrl))
|
|
{
|
|
|
|
DoRequestAPI(3, false, true);
|
|
}
|
|
else
|
|
{
|
|
MessageBox.Show("Chưa kết nối tới thiết bị BI Table!");
|
|
}
|
|
}
|
|
}
|
|
|
|
private void btnBehindPaper_Click(object sender, EventArgs e)
|
|
{
|
|
if (radioButtonBluetooth.Checked)
|
|
{
|
|
if (!string.IsNullOrEmpty(currentBluetoothDeviceName))
|
|
{
|
|
DoRequest(4, false, true);
|
|
}
|
|
else
|
|
{
|
|
MessageBox.Show("Chưa kết nối tới thiết bị BI Table qua Bluetooth!");
|
|
}
|
|
}
|
|
|
|
if (radioButtonIpApi.Checked)
|
|
{
|
|
if (!string.IsNullOrEmpty(txtIPAddress.Text) && !string.IsNullOrEmpty(deviceUrl))
|
|
{
|
|
|
|
DoRequestAPI(4, false, true);
|
|
}
|
|
else
|
|
{
|
|
MessageBox.Show("Chưa kết nối tới thiết bị BI Table!");
|
|
}
|
|
}
|
|
}
|
|
|
|
private void btnFaceCamera_Click(object sender, EventArgs e)
|
|
{
|
|
if (radioButtonBluetooth.Checked)
|
|
{
|
|
if (!string.IsNullOrEmpty(currentBluetoothDeviceName))
|
|
{
|
|
DoRequest(5, true, false);
|
|
}
|
|
else
|
|
{
|
|
MessageBox.Show("Chưa kết nối tới thiết bị BI Table qua Bluetooth!");
|
|
}
|
|
}
|
|
|
|
if (radioButtonIpApi.Checked)
|
|
{
|
|
if (!string.IsNullOrEmpty(txtIPAddress.Text) && !string.IsNullOrEmpty(deviceUrl))
|
|
{
|
|
|
|
DoRequestAPI(5, true, false);
|
|
}
|
|
else
|
|
{
|
|
MessageBox.Show("Chưa kết nối tới thiết bị BI Table!");
|
|
}
|
|
}
|
|
}
|
|
|
|
private void btnPaperCamera_Click(object sender, EventArgs e)
|
|
{
|
|
if (radioButtonBluetooth.Checked)
|
|
{
|
|
if (!string.IsNullOrEmpty(currentBluetoothDeviceName))
|
|
{
|
|
DoRequest(6, false, true);
|
|
}
|
|
else
|
|
{
|
|
MessageBox.Show("Chưa kết nối tới thiết bị BI Table qua Bluetooth!");
|
|
}
|
|
}
|
|
|
|
if (radioButtonIpApi.Checked)
|
|
{
|
|
if (!string.IsNullOrEmpty(txtIPAddress.Text) && !string.IsNullOrEmpty(deviceUrl))
|
|
{
|
|
|
|
DoRequestAPI(6, false, true);
|
|
}
|
|
else
|
|
{
|
|
MessageBox.Show("Chưa kết nối tới thiết bị BI Table!");
|
|
}
|
|
}
|
|
}
|
|
|
|
private void DoRequestAPI(int option, bool isClearFaceImage, bool isClearIDCard)
|
|
{
|
|
Task.Factory.StartNew(new Action(async () =>
|
|
{
|
|
lblStatus.Invoke(new Action(() =>
|
|
{
|
|
lblStatus.Text = "ĐANG XỬ LÝ";
|
|
}));
|
|
|
|
if (isClearIDCard)
|
|
{
|
|
pictureBoxIDCard.Invoke(new Action(() =>
|
|
{
|
|
pictureBoxIDCard.Image?.Dispose();
|
|
pictureBoxIDCard.Image = null;
|
|
}));
|
|
}
|
|
|
|
if (isClearFaceImage)
|
|
{
|
|
pictureBoxFaceImage.Invoke(new Action(() =>
|
|
{
|
|
pictureBoxFaceImage.Image?.Dispose();
|
|
pictureBoxFaceImage.Image = null;
|
|
}));
|
|
}
|
|
|
|
using (var client = new HttpClient { BaseAddress = new Uri($"http://{txtIPAddress.Text}:8096/"), Timeout = TimeSpan.FromMilliseconds(10000) })
|
|
{
|
|
string deviceName = string.Empty;
|
|
txtDeviceName.Invoke(new Action(() =>
|
|
{
|
|
deviceName = txtDeviceName.Text;
|
|
}));
|
|
|
|
HttpResponseMessage response = await client.GetAsync($"/bitable?option={option}&timeout={txtTimeout.Text}");
|
|
response.EnsureSuccessStatusCode();
|
|
var result = await response.Content.ReadAsStringAsync();
|
|
var objectParsed = JsonConvert.DeserializeObject<RequestResult>(result);
|
|
ShowImageOnUI(objectParsed);
|
|
}
|
|
})).ContinueWith(t =>
|
|
{
|
|
lblStatus.Invoke(new Action(() =>
|
|
{
|
|
lblStatus.Text = string.Empty;
|
|
}));
|
|
});
|
|
}
|
|
|
|
private void btnConnectIP_Click(object sender, EventArgs e)
|
|
{
|
|
Task.Factory.StartNew(new Action(async () =>
|
|
{
|
|
try
|
|
{
|
|
if (!string.IsNullOrEmpty(txtIPAddress.Text))
|
|
{
|
|
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;
|
|
}));
|
|
}
|
|
}
|
|
else
|
|
{
|
|
using (var client = new HttpClient { BaseAddress = new Uri("http://api.bitableconnect.beetai.com/"), Timeout = TimeSpan.FromMilliseconds(20000) })
|
|
{
|
|
try
|
|
{
|
|
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<DeviceUrlModel>();
|
|
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;
|
|
}));
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
MessageBox.Show("Kết nối tới server api gặp sự cố!");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
MessageBox.Show("Không có kết nối internet!");
|
|
}
|
|
}));
|
|
}
|
|
|
|
public class DeviceUrlModel
|
|
{
|
|
[JsonProperty("url")]
|
|
public string Url { get; set; }
|
|
[JsonProperty("error")]
|
|
public string Error { get; set; }
|
|
}
|
|
public class Base64Image
|
|
{
|
|
[JsonProperty("base64")]
|
|
public string Base64 { get; set; }
|
|
|
|
[JsonProperty("type")]
|
|
public string Type { get; set; }
|
|
}
|
|
|
|
public class RequestResult
|
|
{
|
|
[JsonProperty("data")]
|
|
public Base64Image[] Data { get; set; }
|
|
|
|
[JsonProperty("code")]
|
|
public int Code { get; set; }
|
|
}
|
|
}
|
|
|
|
}
|