AIParkingApplication/AIParkingApplication/ApiController.cs

517 lines
15 KiB
C#

using System;
using System.Net.Http;
using System.Threading.Tasks;
using OpenCvSharp;
using Newtonsoft.Json;
using System.Net;
namespace AIParkingApplication
{
public class ApiController : IDisposable
{
private const int MAX_REQUEST = 3;
private HttpClient httpClient;
private bool isHttpClientDisposabled;
public ApiController(string baseAddress, int numberOfRetry = 5)
{
httpClient = new HttpClient
{
BaseAddress = new Uri(baseAddress),
Timeout = TimeSpan.FromSeconds(1)
};
isHttpClientDisposabled = false;
}
~ApiController()
{
Dispose();
}
public async Task<LoginDataModel> Login(LoginModel loginData)
{
try
{
HttpResponseMessage response = await httpClient.PostAsJsonAsync("/api/login", loginData);
response.EnsureSuccessStatusCode();
var loginResult = await response.Content.ReadAsAsync<LoginDataModel>();
return loginResult;
}
catch (Exception ex)
{
Console.WriteLine($"Login Exception:\t{DateTime.Now.GetTimeFormatted()} \t {ex.Message}");
return new LoginDataModel
{
IsLoginSuccess = false,
Exception = ex
};
}
}
public async Task<Statistic.ParkInfo> GetStatisticInfo()
{
try
{
HttpResponseMessage response = await httpClient.GetAsync("/api/statistics");
response.EnsureSuccessStatusCode();
Statistic.ParkInfo parkingInfo = await response.Content.ReadAsAsync<Statistic.ParkInfo>();
if (string.IsNullOrEmpty(parkingInfo.TotalIn))
{
parkingInfo.TotalIn = "0";
}
if (string.IsNullOrEmpty(parkingInfo.TotalOut))
{
parkingInfo.TotalOut = "0";
}
return parkingInfo;
}
catch (Exception ex)
{
Console.WriteLine($"GetStatisticInfo Exception:\t{DateTime.Now.GetTimeFormatted()} \t {ex.Message}");
return null;
}
}
public async Task<CardInformation> GetCardInformation(string cardNumber)
{
try
{
var request = new CardModel()
{
CardNumber = cardNumber
};
HttpResponseMessage response;
int requestCounter = 1;
do
{
response = await httpClient.PostAsJsonAsync("/api/check-card", request);
response.EnsureSuccessStatusCode();
requestCounter += 1;
await Task.Delay(100);
}
while (response.StatusCode != HttpStatusCode.OK && requestCounter < MAX_REQUEST);
var cardValication = await response.Content.ReadAsAsync<CardInformation>();
return cardValication;
}
catch (Exception ex)
{
Console.WriteLine($"CheckCard Exception:\t{DateTime.Now.GetTimeFormatted()} \t {ex.Message}");
return null;
}
}
//Neu Dicrection la Out thi logID la logID lay ve khi check the
public async Task<SaveLogRespone> SaveLog(LaneDirection direction, string cardID, string cameraID, PlateType plateType, string timestamp, string plateString, Mat plateImage, Mat plateImageResult, Mat plateFrameImage, Mat frameImage, string logID = "")
{
string plateImageBase64 = Convert.ToBase64String(plateImage.ToBytes());
string plateImageResultBase64 = Convert.ToBase64String(plateImageResult.ToBytes());
string plateFrameImageBase64 = Convert.ToBase64String(plateFrameImage.ToBytes());
string frameImageBase64 = Convert.ToBase64String(frameImage.ToBytes());
try
{
var request = new SaveLogModel
{
Direction = direction == LaneDirection.In ? "in" : "out",
LogID = logID,
CardID = cardID,
TextPlate = plateString,
CameraID = cameraID,
ModePlate = plateType == PlateType.Square ? "1" : "0",
Timestamp = timestamp,
PlateImage = plateImageBase64,
PlateResultImage = plateImageResultBase64,
PlateFrameImage = plateFrameImageBase64,
FrameImage = frameImageBase64
};
HttpResponseMessage response;
int requestCounter = 1;
do
{
response = await httpClient.PostAsJsonAsync("/api/save-logs", request);
response.EnsureSuccessStatusCode();
requestCounter += 1;
await Task.Delay(100);
} while (response.StatusCode != HttpStatusCode.OK && requestCounter < MAX_REQUEST);
SaveLogRespone saveLogRespone = await response.Content.ReadAsAsync<SaveLogRespone>();
return saveLogRespone;
}
catch (Exception ex)
{
Console.WriteLine($"SaveLog Exception:\t{DateTime.Now.GetTimeFormatted()} \t {ex.Message}");
return new SaveLogRespone();
}
}
public void Dispose()
{
if (httpClient != null && !isHttpClientDisposabled)
{
isHttpClientDisposabled = true;
httpClient.Dispose();
}
}
}
#region Check card validation API Data Model
public class CardModel
{
[JsonProperty("card")]
public string CardNumber { get; set; }
}
public class CardInformation
{
[JsonProperty("status")]
public bool IsValid { get; set; }
[JsonProperty("type")]
public string Direction { get; set; }
[JsonProperty("cardID")]
public string CardId { get; set; }
[JsonProperty("cardRealID")]
public int CardRealID { get; set; }
[JsonProperty("log_id")]
public int LogID { get; set; }
[JsonProperty("cardType")]
public string CardType { get; set; }
[JsonProperty("vehicleType")]
public string VehicleType { get; set; }
[JsonProperty("autoOpen")]
public bool AutoOpenDoor { get; set; }
[JsonProperty("plate")]
public string PlateString { get; set; }
[JsonProperty("time")]
public string TimeIn { get; set; }
[JsonProperty("plateImage")]
public string PlateImageBase64 { get; set; }
[JsonProperty("frameImage")]
public string FrameImageBase64 { get; set; }
[JsonProperty("faceImage")]
public string FaceImageBase64 { get; set; }
[JsonProperty("name")]
public string CustomerName { get; set; }
}
#endregion
#region Save Log API Data Model
public class SaveLogRespone
{
[JsonProperty("status")]
public bool Status { get; set; }
[JsonProperty("logID")]
public int LogID { get; set; }
[JsonProperty("cost")]
public string Cost { get; set; }
}
public class SaveLogModel
{
[JsonProperty("card")]
public string CardID { get; set; }
[JsonProperty("plate")]
public string TextPlate { get; set; }
[JsonProperty("type")]
public string Direction { get; set; }
[JsonProperty("mod")]
public string ModePlate { get; set; }
[JsonProperty("log_id")]
public string LogID { get; set; }
[JsonProperty("camera")]
public string CameraID { get; set; }
[JsonProperty("time")]
public string Timestamp { get; set; }
[JsonProperty("plateImage")]
public string PlateImage { get; set; }
[JsonProperty("plateResultImage")]
public string PlateResultImage { get; set; }
[JsonProperty("plateFrameImage")]
public string PlateFrameImage { get; set; }
[JsonProperty("frameImage")]
public string FrameImage { get; set; }
}
#endregion
#region Get Config API Data Model
public class LoginModel
{
[JsonProperty("username")]
public string Username { get; set; }
[JsonProperty("password")]
public string Password { get; set; }
}
public class LoginDataModel
{
[JsonProperty("status")]
public bool IsLoginSuccess { get; set; }
[JsonProperty("data")]
public Config LoginData { get; set; }
public Exception Exception { get; set; }
}
public class Config
{
[JsonProperty("userID")]
public int UserID { get; set; }
[JsonProperty("name")]
public string UserName { get; set; }
[JsonProperty("configROI")]
public bool IsConfigROI { get; set; }
[JsonProperty("configSystemLogs")]
public bool IsSaveSystemLogs { get; set; }
[JsonProperty("api")]
public APIPathModel APIPath { get; set; }
[JsonProperty("camera1")]
public CameraData CameraData1 { get; set; }
[JsonProperty("camera2")]
public CameraData CameraData2 { get; set; }
[JsonProperty("camera3")]
public CameraData CameraData3 { get; set; }
[JsonProperty("camera4")]
public CameraData CameraData4 { get; set; }
[JsonProperty("camera5")]
public CameraData CameraData5 { get; set; }
[JsonProperty("camera6")]
public CameraData CameraData6 { get; set; }
[JsonProperty("camera7")]
public CameraData CameraData7 { get; set; }
[JsonProperty("camera8")]
public CameraData CameraData8 { get; set; }
[JsonProperty("statistic")]
public Statistics StatisticsData { get; set; }
[JsonProperty("cascadeConfig")]
public CascadeConfig CascadeConfigData { get; set; }
[JsonProperty("laneConfig")]
public LaneConfig LaneConfigData { get; set; }
}
#region API Path
public class APIPathModel
{
[JsonProperty("apiStatistics")]
public APIProperties ApiStatistics { get; set; }
[JsonProperty("apiPlateRecognize")]
public APIProperties ApiPlateRecognize { get; set; }
[JsonProperty("apiCheckCard")]
public APIProperties ApiCheckCard { get; set; }
[JsonProperty("apiSaveLogs")]
public APIProperties ApiSaveLogs { get; set; }
[JsonProperty("apiUpdateLogs")]
public APIProperties ApiUpdateLogs { get; set; }
}
public class APIProperties
{
[JsonProperty("ip")]
public string IP { get; set; }
[JsonProperty("port")]
public string Port { get; set; }
[JsonProperty("path")]
public string Path { get; set; }
}
#endregion
public class CameraData
{
[JsonProperty("id")]
public int Id { get; set; }
[JsonProperty("gate")]
public int Gate { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("type")]
public string Type { get; set; }
[JsonProperty("stream_url")]
public string StreamUrl { get; set; }
[JsonProperty("minWidth")]
public double MinWidth { get; set; }
[JsonProperty("maxWidth")]
public double MaxWidth { get; set; }
[JsonProperty("minHeight")]
public double MinHeight { get; set; }
[JsonProperty("maxHeight")]
public double MaxHeight { get; set; }
[JsonProperty("scaleFactor")]
public double ScaleFactor { get; set; }
[JsonProperty("minNeighbors")]
public double MinNeighbors { get; set; }
[JsonProperty("status")]
public double Status { get; set; }
[JsonProperty("isRecognizeCamera")]
public bool IsRecognizeCamera { get; set; }
[JsonProperty("isLongPlate")]
public bool IsLongPlate { get; set; }
[JsonProperty("stt")]
public int Stt { get; set; }
[JsonProperty("roi_config")]
public string Roi_config { get; set; }
[JsonProperty("created_at")]
public int Created_at { get; set; }
[JsonProperty("modified_at")]
public int Modified_at { get; set; }
}
public class Statistics
{
[JsonProperty("moto")]
public string Moto { get; set; }
[JsonProperty("oto")]
public string Oto { get; set; }
[JsonProperty("vip")]
public string Vip { get; set; }
[JsonProperty("total_in")]
public string Total_in { get; set; }
[JsonProperty("total_out")]
public string Total_out { get; set; }
}
public class CascadeConfig
{
[JsonProperty("minWidthPlate")]
public int MinWidthPlate { get; set; }
[JsonProperty("maxWidthPlate")]
public int MaxWidthPlate { get; set; }
[JsonProperty("minHeightPlate")]
public int MinHeightPlate { get; set; }
[JsonProperty("maxHeightPlate")]
public int MaxHeightPlate { get; set; }
[JsonProperty("minWidthLongPlate")]
public int MinWidthLongPlate { get; set; }
[JsonProperty("maxWidthLongPlate")]
public int MaxWidthLongPlate { get; set; }
[JsonProperty("minHeightLongPlate")]
public int MinHeightLongPlate { get; set; }
[JsonProperty("maxHeightLongPlate")]
public int MaxHeightLongPlate { get; set; }
[JsonProperty("scaleFactor")]
public double ScaleFactor { get; set; }
[JsonProperty("scaleFactorLong")]
public double ScaleFactorLong { get; set; }
[JsonProperty("minNeighbors")]
public double MinNeighbors { get; set; }
[JsonProperty("minNeighborsLong")]
public double MinNeighborsLong { get; set; }
[JsonProperty("timeOut")]
public int TimeOut { get; set; }
[JsonProperty("logs")]
public int Logs { get; set; }
}
#region LaneConfig
public class LaneConfig
{
[JsonProperty("laneConfig")]
public LaneConfigPosision Posision { get; set; }
}
public class LaneConfigPosision
{
[JsonProperty("left")]
public LaneConfigParam LeftLane { get; set; }
[JsonProperty("right")]
public LaneConfigParam RightLane { get; set; }
}
public class LaneConfigParam
{
[JsonProperty("name")]
public string LaneName { get; set; }
[JsonProperty("default")]
public string DefaultLane { get; set; }
[JsonProperty("longPlate")]
public bool IsLongPlate { get; set; }
}
#endregion
#endregion
}