You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
315 lines
8.4 KiB
315 lines
8.4 KiB
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)
|
|
};
|
|
|
|
/// <summary>
|
|
/// /Car/getCarInfo/
|
|
/// </summary>
|
|
/// <param name="carNo"></param>
|
|
/// <returns></returns>
|
|
public static async Task<TboxCarInfoModel> 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<TboxCarInfoModel>(s);
|
|
}
|
|
|
|
return tboxCarInfoModel;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Log.Error($" TBoxApi GetCarInfo e = {e}");
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// /Car/Clear
|
|
/// </summary>
|
|
/// <param name="carNo"></param>
|
|
/// <returns></returns>
|
|
public static async Task<bool> 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<bool> 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;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 设置心跳返回信息
|
|
/// </summary>
|
|
/// <param name="status">
|
|
/// 1 换电站待机
|
|
/// 2 开始换电
|
|
/// 3 换电进行中
|
|
/// 4 换电完成
|
|
/// 5 换电站故障</param>
|
|
/// <returns></returns>
|
|
public static async Task<bool> SetHeart(string VIN,byte status)
|
|
{
|
|
Log.Info($" TBoxApi SetHeart VIN={VIN} status={status} ");
|
|
string url = BASE_URL + $"/Car/Vin/{VIN}/SetStatus/{status}";
|
|
try
|
|
{
|
|
string s = await _httpClient.GetStringAsync(url);
|
|
Log.Info($"SetHeart resp = {s}");
|
|
if (!string.IsNullOrWhiteSpace(s))
|
|
{
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Log.Error($"StartSwap e = {e}");
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 解锁状态
|
|
/// </summary>
|
|
/// <param name="carNo"></param>
|
|
/// <returns></returns>
|
|
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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 加锁状态
|
|
/// </summary>
|
|
/// <param name="carNo"></param>
|
|
/// <returns></returns>
|
|
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>
|
|
/// 车辆解锁
|
|
/// /Car/unLock/
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public static async Task<bool> 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;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Tbox连接
|
|
/// /Car/unLock/
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public static async Task<bool> 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;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Tbox断连
|
|
/// /Car/unLock/
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public static async Task<bool> 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;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// /Car/getCarInfoList
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public static async Task<List<TboxCarInfoModel>> GetCarInfoList()
|
|
{
|
|
Log.Info(" TBoxApi GetCarInfoList start ");
|
|
string url = BASE_URL + StaticStationInfo.TboxUrlCarInfoList;
|
|
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 + 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;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 货箱落下
|
|
/// </summary>
|
|
/// <param name="carNo"></param>
|
|
/// <returns></returns>
|
|
public static async Task<bool> BoxDown(string carNo)
|
|
{
|
|
Log.Info("BoxDown");
|
|
string url = BASE_URL + $"/Car/BoxDown/{carNo}";
|
|
try
|
|
{
|
|
string s = await _httpClient.GetStringAsync(url);
|
|
|
|
Log.Info($"BoxDown resp = {s}");
|
|
return bool.Parse(s);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Log.Error($"LockCar e = {e}");
|
|
return false;
|
|
}
|
|
}
|
|
|
|
} |