|
|
|
using log4net;
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
using Service.Execute.Model;
|
|
|
|
|
|
|
|
namespace Service.Execute.Api;
|
|
|
|
|
|
|
|
public class TBoxApi
|
|
|
|
{
|
|
|
|
private static readonly ILog Log = LogManager.GetLogger(typeof(TBoxApi));
|
|
|
|
|
|
|
|
//TODO::TBox 服务地址
|
|
|
|
private static readonly string BASE_URL = "http://localhost:5034";
|
|
|
|
|
|
|
|
private static readonly HttpClient _httpClient = new HttpClient()
|
|
|
|
{
|
|
|
|
Timeout = TimeSpan.FromSeconds(60)
|
|
|
|
};
|
|
|
|
|
|
|
|
public static async Task<TboxCarInfoModel> GetCarInfo()
|
|
|
|
{
|
|
|
|
Log.Info("GetCarInfo");
|
|
|
|
string url = BASE_URL + "/getCarInfo";
|
|
|
|
try
|
|
|
|
{
|
|
|
|
string s = await _httpClient.GetStringAsync(url);
|
|
|
|
TboxCarInfoModel? tboxCarInfoModel = null;
|
|
|
|
if (!String.IsNullOrWhiteSpace(s))
|
|
|
|
{
|
|
|
|
tboxCarInfoModel = JsonConvert.DeserializeObject<TboxCarInfoModel>(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
Log.Info($"GetCarInfo resp = {tboxCarInfoModel}");
|
|
|
|
return tboxCarInfoModel;
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
Console.WriteLine(e);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static async Task<bool> Reset()
|
|
|
|
{
|
|
|
|
Log.Info("Reset");
|
|
|
|
string url = BASE_URL + "/Clear";
|
|
|
|
try
|
|
|
|
{
|
|
|
|
string s = await _httpClient.GetStringAsync(url);
|
|
|
|
|
|
|
|
Log.Info($"Clear Tbox resp = {s}");
|
|
|
|
return bool.Parse(s);
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
Console.WriteLine(e);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static async Task<bool> IsConnected()
|
|
|
|
{
|
|
|
|
Log.Info("IsConnected");
|
|
|
|
string url = BASE_URL + "/getCarInfo";
|
|
|
|
try
|
|
|
|
{
|
|
|
|
string s = await _httpClient.GetStringAsync(url);
|
|
|
|
TboxCarInfoModel? tboxCarInfoModel = null;
|
|
|
|
if (!String.IsNullOrWhiteSpace(s))
|
|
|
|
{
|
|
|
|
tboxCarInfoModel = JsonConvert.DeserializeObject<TboxCarInfoModel>(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
Log.Info($"GetCarInfo resp = {tboxCarInfoModel}");
|
|
|
|
return tboxCarInfoModel.Connected;
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
Console.WriteLine(e);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 车辆解锁
|
|
|
|
/// </summary>
|
|
|
|
/// <returns></returns>
|
|
|
|
public static async Task<bool> UnLockCar(string carNo)
|
|
|
|
{
|
|
|
|
Log.Info("UnLockCar");
|
|
|
|
string url = BASE_URL + "/unLock/"+carNo;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
string s = await _httpClient.GetStringAsync(url);
|
|
|
|
|
|
|
|
|
|
|
|
Log.Info($"UnLockCar resp = {s}");
|
|
|
|
return bool.Parse(s);
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
Console.WriteLine(e);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 车辆上锁
|
|
|
|
/// </summary>
|
|
|
|
/// <returns></returns>
|
|
|
|
public static async Task<bool> LockCar(string carNo)
|
|
|
|
{
|
|
|
|
Log.Info("LockCar");
|
|
|
|
string url = BASE_URL + "/lock/"+carNo;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
string s = await _httpClient.GetStringAsync(url);
|
|
|
|
|
|
|
|
|
|
|
|
Log.Info($"LockCar resp = {s}");
|
|
|
|
return bool.Parse(s);
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
Console.WriteLine(e);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|