From 73a13a249d9a3783bd7585910424a80a7ef3b4e5 Mon Sep 17 00:00:00 2001 From: DucDangAnh Date: Thu, 25 Jun 2020 13:39:28 +0700 Subject: [PATCH] Add ApiController --- .../AIParkingApplication.csproj | 1 + AIParkingApplication/ApiController.cs | 180 ++++++++++++++++++ 2 files changed, 181 insertions(+) create mode 100644 AIParkingApplication/ApiController.cs diff --git a/AIParkingApplication/AIParkingApplication.csproj b/AIParkingApplication/AIParkingApplication.csproj index 94dbfc7..b892ded 100644 --- a/AIParkingApplication/AIParkingApplication.csproj +++ b/AIParkingApplication/AIParkingApplication.csproj @@ -76,6 +76,7 @@ AIParkingApplicationForm.cs + diff --git a/AIParkingApplication/ApiController.cs b/AIParkingApplication/ApiController.cs new file mode 100644 index 0000000..b3eb64a --- /dev/null +++ b/AIParkingApplication/ApiController.cs @@ -0,0 +1,180 @@ +using System; +using System.Net.Http; +using System.Threading.Tasks; +using OpenCvSharp; +using Newtonsoft.Json; + +namespace AIParkingApplication +{ + public class ApiController : IDisposable + { + private HttpClient httpClient; + private bool isHttpClientDisposabled; + private int numberOfRetry; + private ApiPath apiPath; + + public ApiController(string baseAddress, int numberOfRetry = 5) + { + httpClient = new HttpClient + { + BaseAddress = new Uri(baseAddress), + Timeout = TimeSpan.FromSeconds(5) + }; + isHttpClientDisposabled = false; + this.numberOfRetry = numberOfRetry; + } + + ~ApiController() + { + Dispose(); + } + + //TODO Old API + public async Task Login(LoginModel loginData) + { + try + { + HttpResponseMessage response = await httpClient.PostAsJsonAsync("/api/login", loginData); + response.EnsureSuccessStatusCode(); + var loginResult = await response.Content.ReadAsAsync(); + return loginResult; + } + catch (Exception ex) + { + Console.WriteLine($"SendEngineRequest : {ex.Message}"); + return new LoginResponseModel + { + IsLoggedIn = false + }; + } + } + + //TODO For New API + //public async Task Login(LoginModel loginData) + //{ + // try + // { + // HttpResponseMessage response = await httpClient.PostAsJsonAsync("/api/login", loginData); + // response.EnsureSuccessStatusCode(); + // var loginResult = await response.Content.ReadAsAsync(); + // return loginResult; + // } + // catch (Exception ex) + // { + // Console.WriteLine($"SendEngineRequest : {ex.Message}"); + // return new LoginResponseModel + // { + // IsLoggedIn = false + // }; + // } + //} + + public async void GetApiPathFromServer() + { + try + { + var request = new PlateRequestEngineModel + { + }; + HttpResponseMessage response = await httpClient.PostAsJsonAsync("/get-from-frame", request); + response.EnsureSuccessStatusCode(); + var ocrResult = await response.Content.ReadAsAsync(); + } + catch (Exception ex) + { + Console.WriteLine($"SendEngineRequest : {ex.Message}"); + } + } + + public async Task SaveLogIn(Mat plateImage, PlateType plateType) + { + string plateImageBase64 = Convert.ToBase64String(plateImage.ToBytes()); + try + { + var request = new PlateRequestEngineModel + { + Img64 = plateImageBase64, + Mode = plateType == PlateType.Square ? "square" : "long", + Display = "full" + }; + HttpResponseMessage response = await httpClient.PostAsJsonAsync("/get-from-frame", request); + response.EnsureSuccessStatusCode(); + var ocrResult = await response.Content.ReadAsAsync(); + return ocrResult; + } + catch (Exception ex) + { + Console.WriteLine($"SendEngineRequest : {ex.Message}"); + return new OcrResult(); + } + } + + public void Dispose() + { + if (httpClient != null && !isHttpClientDisposabled) + { + isHttpClientDisposabled = true; + httpClient.Dispose(); + } + } + } + + public class ApiPath + { + [JsonProperty("savelogsin")] //wtf is this from server. + public string SaveVehicleIn { get; set; } + } + + public class LoginModel + { + [JsonProperty("username")] + public string Username { get; set; } + + [JsonProperty("password")] + public string Password { get; set; } + } + + public class LoginResponseOldModel + { + + } + + public class LoginResponseModel + { + [JsonProperty("")] + public bool IsLoggedIn { get; set; } + } + + public class PlateLogModel + { + [JsonProperty("card")] + public string CardID { get; set; } + + [JsonProperty("plate")] + public string PlateString { get; set; } + + [JsonProperty("type")] + public string LaneType { get; set; } + + [JsonProperty("mod")] + public string PlateMode { get; set; } //0 or 1 + + [JsonProperty("camera")] + public string CameraID { get; set; } + + [JsonProperty("time")] + public string Time { get; set; } + + [JsonProperty("plateImage")] + public string PlateImageBase64 { get; set; } + + [JsonProperty("plateResultImage")] + public string PlateResultImageBase64 { get; set; } + + [JsonProperty("plateFrameImage")] + public string PlateFrameImageBase64 { get; set; } + + [JsonProperty("frameImage")] + public string FrameImageBase64 { get; set; } + } +}