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.
268 lines
7.8 KiB
268 lines
7.8 KiB
using Entity.Constant;
|
|
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}");
|
|
|
|
StationSoftMgr.PutDeviceLog((int)StationConstant.DeviceCode.Tbox,SwapConstant.TboxProtocol.Heart,
|
|
|
|
s,(int)SwapConstant.CommunicationType.Receive);
|
|
|
|
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;
|
|
}
|
|
|
|
|
|
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>
|
|
/// 车辆解锁
|
|
/// /Car/unLock/
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public static async Task<bool> UnLockCar(string carNo)
|
|
{
|
|
Log.Info("UnLockCar");
|
|
string url = BASE_URL + StaticStationInfo.TboxUrlUnLock + carNo;
|
|
try
|
|
{
|
|
Thread.Sleep(1000);
|
|
StationSoftMgr.PutDeviceLog((int)StationConstant.DeviceCode.Tbox,SwapConstant.TboxProtocol.UnLock,
|
|
|
|
carNo ,(int)SwapConstant.CommunicationType.Send);
|
|
|
|
string s = await _httpClient.GetStringAsync(url);
|
|
StationSoftMgr.PutDeviceLog((int)StationConstant.DeviceCode.Tbox,SwapConstant.TboxProtocol.UnLock,
|
|
|
|
s ,(int)SwapConstant.CommunicationType.Receive);
|
|
|
|
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
|
|
{ StationSoftMgr.PutDeviceLog((int)StationConstant.DeviceCode.Tbox,SwapConstant.TboxProtocol.Lock,
|
|
|
|
carNo ,(int)SwapConstant.CommunicationType.Send);
|
|
|
|
string s = await _httpClient.GetStringAsync(url);
|
|
StationSoftMgr.PutDeviceLog((int)StationConstant.DeviceCode.Tbox,SwapConstant.TboxProtocol.Lock,
|
|
|
|
s ,(int)SwapConstant.CommunicationType.Receive);
|
|
|
|
Log.Info($"LockCar resp = {s}");
|
|
return bool.Parse(s);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Log.Error($"LockCar e = {e}");
|
|
return false;
|
|
}
|
|
}
|
|
} |