|
|
|
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("TboxApi");
|
|
|
|
|
|
|
|
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(string carNo)
|
|
|
|
{
|
|
|
|
Log.Info($" TBoxApi GetCarInfo carNo={carNo}");
|
|
|
|
string url = BASE_URL + "/getCarInfo/" + carNo;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
string s = await _httpClient.GetStringAsync(url);
|
|
|
|
TboxCarInfoModel? tboxCarInfoModel = null;
|
|
|
|
if (!String.IsNullOrWhiteSpace(s))
|
|
|
|
{
|
|
|
|
tboxCarInfoModel = JsonConvert.DeserializeObject<TboxCarInfoModel>(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
Log.Info($"TBoxApi GetCarInfo resp = {JsonConvert.SerializeObject(tboxCarInfoModel)}");
|
|
|
|
return tboxCarInfoModel;
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
Log.Error($" TBoxApi GetCarInfo e = {e}");
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static async Task<bool> Reset(string carNo)
|
|
|
|
{
|
|
|
|
Log.Info(" TBoxApi Reset");
|
|
|
|
string url = BASE_URL + "/Clear/" + carNo;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
string s = await _httpClient.GetStringAsync(url);
|
|
|
|
|
|
|
|
Log.Info($"TBoxApi Reset resp = {s}");
|
|
|
|
return bool.Parse(s);
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
Log.Error($"TBoxApi Reset e = {e}");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static async Task<bool> IsConnected(string carNo)
|
|
|
|
{
|
|
|
|
Log.Info($"IsConnected carNo={carNo}");
|
|
|
|
string url = BASE_URL + "/getCarInfo/"+carNo;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
string s = await _httpClient.GetStringAsync(url);
|
|
|
|
TboxCarInfoModel? tboxCarInfoModel = null;
|
|
|
|
if (!String.IsNullOrWhiteSpace(s))
|
|
|
|
{
|
|
|
|
tboxCarInfoModel = JsonConvert.DeserializeObject<TboxCarInfoModel>(s);
|
|
|
|
Log.Info($"IsConnected resp={JsonConvert.SerializeObject(tboxCarInfoModel)}");
|
|
|
|
}
|
|
|
|
|
|
|
|
Log.Info($"GetCarInfo resp = {JsonConvert.SerializeObject(tboxCarInfoModel)}");
|
|
|
|
return tboxCarInfoModel.Connected;
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
Log.Error($"IsConnected e = {e}");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static async Task<bool> UnLockCarManyTimes(string carNo)
|
|
|
|
{
|
|
|
|
Log.Info($" TBoxApi start UnLockCarManyTimes carNo={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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Log.Info($" TBoxApi end UnLockCarManyTimes carNo={carNo} times={_times} resp={JsonConvert.SerializeObject(bools)}");
|
|
|
|
|
|
|
|
return bools.Select(i => i).Count() > _successTimes;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static async Task<bool> LockCarManyTimes(string carNo)
|
|
|
|
{
|
|
|
|
Log.Info($" TBoxApi start LockCarManyTimes carNo={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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Log.Info($" TBoxApi end LockCarManyTimes carNo={carNo} times={_times} resp={JsonConvert.SerializeObject(bools)}");
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
Log.Error($"UnLockCar e = {e}");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static async Task<List<TboxCarInfoModel>> GetCarInfoList()
|
|
|
|
{
|
|
|
|
Log.Info(" TBoxApi GetCarInfoList start ");
|
|
|
|
string url = BASE_URL + "/getCarInfoList" ;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
string s = await _httpClient.GetStringAsync(url);
|
|
|
|
List<TboxCarInfoModel>? tboxCarInfoModels = null;
|
|
|
|
if (!String.IsNullOrWhiteSpace(s))
|
|
|
|
{
|
|
|
|
tboxCarInfoModels = JsonConvert.DeserializeObject<List<TboxCarInfoModel>>(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
Log.Info($"TBoxApi GetCarInfoList resp = {JsonConvert.SerializeObject(tboxCarInfoModels)}");
|
|
|
|
return tboxCarInfoModels;
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
Log.Error($"TBoxApi GetCarInfoList e = {e}");
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <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)
|
|
|
|
{
|
|
|
|
Log.Error($"LockCar e = {e}");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|