using log4net;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Service.Execute.Model;
using Service.Execute.Model.Tbox;
using Service.Init;
using TboxCarInfoModel = Service.Execute.Model.Tbox.TboxCarInfoModel;
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)
};
///
/// /Car/getCarInfo/
///
///
///
public static async Task GetCarInfo(string carNo)
{
Log.Info($" TBoxApi GetCarInfo carNo={carNo}");
string url = BASE_URL + StaticStationInfo.TboxUrlCarInfo + carNo;
try
{
string s = await _httpClient.GetStringAsync(url);
Log.Info($"TBoxApi GetCarInfo resp = {s}");
TboxCarInfoModel tboxCarInfoModel = null;
if (!String.IsNullOrWhiteSpace(s))
{
tboxCarInfoModel = JsonConvert.DeserializeObject(s);
}
return tboxCarInfoModel;
}
catch (Exception e)
{
Log.Error($" TBoxApi GetCarInfo e = {e}");
return null;
}
}
///
/// /Car/Clear
///
///
///
public static async Task Reset(string carNo)
{
Log.Info(" TBoxApi Reset");
string url = BASE_URL + StaticStationInfo.TboxUrlClear + 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 IsConnected(string carNo)
{
Log.Info($"IsConnected carNo={carNo}");
TboxCarInfoModel tboxCarInfoModel = await GetCarInfo(carNo);
Log.Info($"IsConnected resp={JsonConvert.SerializeObject(tboxCarInfoModel)}");
return tboxCarInfoModel == null ? false : tboxCarInfoModel.Connected;
}
///
/// 下发开始换电指令到tbox
///
///
public static async Task StartSwap(string carNo)
{
Log.Info($" TBoxApi start StartSwap carNo={carNo} ");
string url = BASE_URL + "/startSwap/" + carNo;
try
{
string s = await _httpClient.GetStringAsync(url);
Log.Info($"StartSwap resp = {s}");
if (string.IsNullOrWhiteSpace(s))
{
return 0;
}
return byte.Parse(s);
}
catch (Exception e)
{
Log.Error($"StartSwap e = {e}");
return 0;
}
}
public static async Task UnLockCarManyTimes(string carNo)
{
Log.Info($" TBoxApi start UnLockCarManyTimes carNo={carNo} ");
List bools = new List();
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 LockCarManyTimes(string carNo)
{
Log.Info($" TBoxApi start LockCarManyTimes carNo={carNo} ");
List bools = new List();
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;
}
///
/// 车辆解锁
/// /Car/unLock/
///
///
public static async Task UnLockCar(string carNo)
{
Log.Info("UnLockCar");
string url = BASE_URL + StaticStationInfo.TboxUrlUnLock + 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;
}
}
///
/// Tbox连接
/// /Car/unLock/
///
///
public static async Task Connect(string carNo)
{
Log.Info("TboxConnect");
string url = BASE_URL + StaticStationInfo.TboxUrlConnect + carNo;
try
{
string s = await _httpClient.GetStringAsync(url);
Log.Info($"TboxConnect resp = {s}");
return bool.Parse(s);
}
catch (Exception e)
{
Log.Error($"TboxConnect e = {e}");
return false;
}
}
///
/// Tbox断连
/// /Car/unLock/
///
///
public static async Task DisConnect(string carNo)
{
Log.Info("TboxDisConnect");
string url = BASE_URL + StaticStationInfo.TboxUrlDisConnect + carNo;
try
{
string s = await _httpClient.GetStringAsync(url);
Log.Info($"TboxDisConnect resp = {s}");
return bool.Parse(s);
}
catch (Exception e)
{
Log.Error($"TboxDisConnect e = {e}");
return false;
}
}
///
/// /Car/getCarInfoList
///
///
public static async Task> GetCarInfoList()
{
Log.Info(" TBoxApi GetCarInfoList start ");
string url = BASE_URL + StaticStationInfo.TboxUrlCarInfoList;
try
{
string s = await _httpClient.GetStringAsync(url);
List? tboxCarInfoModels = null;
if (!String.IsNullOrWhiteSpace(s))
{
tboxCarInfoModels = JsonConvert.DeserializeObject>(s);
}
Log.Info($"TBoxApi GetCarInfoList resp = {JsonConvert.SerializeObject(tboxCarInfoModels)}");
return tboxCarInfoModels;
}
catch (Exception e)
{
Log.Error($"TBoxApi GetCarInfoList e = {e}");
return null;
}
}
///
/// 车辆上锁
///
///
public static async Task LockCar(string carNo)
{
Log.Info("LockCar");
string url = BASE_URL + StaticStationInfo.TboxUrlLock + 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;
}
}
}