Chuyển API lấy Thống kê từ {Statistic} sang {ApiController}
This commit is contained in:
parent
e972222b91
commit
d5089f189b
|
@ -60,6 +60,31 @@ namespace AIParkingApplication
|
|||
}
|
||||
}
|
||||
|
||||
public async Task<Statistic.ParkInfo> GetStatisticInfo()
|
||||
{
|
||||
try
|
||||
{
|
||||
HttpResponseMessage response = await httpClient.GetAsync("/api/statistics");
|
||||
response.EnsureSuccessStatusCode();
|
||||
Statistic.ParkInfo parkInfo = await response.Content.ReadAsAsync<Statistic.ParkInfo>();
|
||||
|
||||
if (string.IsNullOrEmpty(parkInfo.TotalIn))
|
||||
{
|
||||
parkInfo.TotalIn = "0";
|
||||
}
|
||||
if (string.IsNullOrEmpty(parkInfo.TotalOut))
|
||||
{
|
||||
parkInfo.TotalOut = "0";
|
||||
}
|
||||
return parkInfo;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"SendApiStatisticRequest : {ex.Message}");
|
||||
return new Statistic.ParkInfo();
|
||||
}
|
||||
}
|
||||
|
||||
public async void GetApiPathFromServer()
|
||||
{
|
||||
try
|
||||
|
|
|
@ -3,6 +3,7 @@ using System.Windows.Forms;
|
|||
using System.Threading;
|
||||
using Newtonsoft.Json;
|
||||
using System.Net.Http;
|
||||
using System.Xml.Serialization;
|
||||
|
||||
namespace AIParkingApplication
|
||||
{
|
||||
|
@ -12,6 +13,7 @@ namespace AIParkingApplication
|
|||
private readonly Thread thrStatistics;
|
||||
private readonly TimeSpan updateInterval;
|
||||
private ParkInfo parkInfo;
|
||||
private string baseAddress;
|
||||
|
||||
private Label lblLoaiXe;
|
||||
private Label lblXeMay;
|
||||
|
@ -23,8 +25,8 @@ namespace AIParkingApplication
|
|||
public Statistic(string baseAddress, TimeSpan updateInterval)
|
||||
{
|
||||
InitializeComponent();
|
||||
this.baseAddress = baseAddress;
|
||||
this.updateInterval = updateInterval;
|
||||
client = new HttpClient { BaseAddress = new Uri(baseAddress), Timeout = TimeSpan.FromMilliseconds(5000) };
|
||||
thrStatistics = new Thread(new ThreadStart(GetStatistic)) { IsBackground = true };
|
||||
thrStatistics.Start();
|
||||
}
|
||||
|
@ -39,27 +41,10 @@ namespace AIParkingApplication
|
|||
}
|
||||
}
|
||||
|
||||
private async void GetDataFromServer()
|
||||
async void GetDataFromServer()
|
||||
{
|
||||
try
|
||||
{
|
||||
HttpResponseMessage response = await client.GetAsync("/api/statistics");
|
||||
response.EnsureSuccessStatusCode();
|
||||
parkInfo = await response.Content.ReadAsAsync<ParkInfo>();
|
||||
|
||||
if (string.IsNullOrEmpty(parkInfo.TotalIn))
|
||||
{
|
||||
parkInfo.TotalIn = "0";
|
||||
}
|
||||
if (string.IsNullOrEmpty(parkInfo.TotalOut))
|
||||
{
|
||||
parkInfo.TotalOut = "0";
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"SendApiStatisticRequest : {ex.Message}");
|
||||
}
|
||||
ApiController apiController = new ApiController(this.baseAddress);
|
||||
parkInfo = await apiController.GetStatisticInfo();
|
||||
}
|
||||
|
||||
private void ShowInfo()
|
||||
|
|
Loading…
Reference in New Issue
Block a user