|
|
|
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:5036";
|
|
|
|
|
|
|
|
private static int _times = 5;
|
|
|
|
private static int _successTimes = 3;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static async Task<bool> UnLockCarManyTimes(string carNo)
|
|
|
|
{
|
|
|
|
List<bool> bools = new List<bool>();
|
|
|
|
for (int i = 0; i < _times; i++)
|
|
|
|
{
|
|
|
|
Thread.Sleep(100);
|
|
|
|
var unLockCar = await UnLockCar(carNo);
|
|
|
|
if (unLockCar)
|
|
|
|
{
|
|
|
|
|
|
|
|
bools.Add(unLockCar);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return bools.Select(i => i).Count() > _successTimes;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public static async Task<bool> LockCarManyTimes(string carNo)
|
|
|
|
{
|
|
|
|
List<bool> bools = new List<bool>();
|
|
|
|
for (int i = 0; i < _times; i++)
|
|
|
|
{
|
|
|
|
Thread.Sleep(100);
|
|
|
|
var unLockCar = await LockCar(carNo);
|
|
|
|
if (unLockCar)
|
|
|
|
{
|
|
|
|
|
|
|
|
bools.Add(unLockCar);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return bools.Select(i => i).Count() > _successTimes;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|