Add ApiController
This commit is contained in:
parent
7529efa6d2
commit
73a13a249d
|
@ -76,6 +76,7 @@
|
|||
<Compile Include="AIParkingApplicationForm.Designer.cs">
|
||||
<DependentUpon>AIParkingApplicationForm.cs</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Include="ApiController.cs" />
|
||||
<Compile Include="AppConstant.cs" />
|
||||
<Compile Include="C3DeviceController.cs" />
|
||||
<Compile Include="Camera.cs" />
|
||||
|
|
180
AIParkingApplication/ApiController.cs
Normal file
180
AIParkingApplication/ApiController.cs
Normal file
|
@ -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<LoginResponseModel> Login(LoginModel loginData)
|
||||
{
|
||||
try
|
||||
{
|
||||
HttpResponseMessage response = await httpClient.PostAsJsonAsync("/api/login", loginData);
|
||||
response.EnsureSuccessStatusCode();
|
||||
var loginResult = await response.Content.ReadAsAsync<LoginResponseModel>();
|
||||
return loginResult;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"SendEngineRequest : {ex.Message}");
|
||||
return new LoginResponseModel
|
||||
{
|
||||
IsLoggedIn = false
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
//TODO For New API
|
||||
//public async Task<LoginResponseModel> Login(LoginModel loginData)
|
||||
//{
|
||||
// try
|
||||
// {
|
||||
// HttpResponseMessage response = await httpClient.PostAsJsonAsync("/api/login", loginData);
|
||||
// response.EnsureSuccessStatusCode();
|
||||
// var loginResult = await response.Content.ReadAsAsync<LoginResponseModel>();
|
||||
// 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<OcrResult>();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"SendEngineRequest : {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<OcrResult> 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<OcrResult>();
|
||||
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; }
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user