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/Car"; 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 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(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 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 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(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 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; } /// /// 车辆解锁 /// /// public static async Task 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> GetCarInfoList() { Log.Info(" TBoxApi GetCarInfoList start "); string url = BASE_URL + "/getCarInfoList" ; 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 + "/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; } } }