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(typeof(TBoxApi)); //TODO::TBox 服务地址 private static readonly string BASE_URL = "http://localhost:5036"; private static readonly HttpClient _httpClient = new HttpClient() { Timeout = TimeSpan.FromSeconds(60) }; public static async Task GetCarInfo() { Log.Info("GetCarInfo"); string url = BASE_URL + "/getCarInfo"; try { string s = await _httpClient.GetStringAsync(url); TboxCarInfoModel? tboxCarInfoModel = null; if (!String.IsNullOrWhiteSpace(s)) { tboxCarInfoModel = JsonConvert.DeserializeObject(s); } Log.Info($"GetCarInfo resp = {tboxCarInfoModel}"); return tboxCarInfoModel; } catch (Exception e) { Console.WriteLine(e); return null; } } public static async Task Reset() { Log.Info("Reset"); string url = BASE_URL + "/Clear"; try { string s = await _httpClient.GetStringAsync(url); Log.Info($"Clear Tbox resp = {s}"); return bool.Parse(s); } catch (Exception e) { Console.WriteLine(e); return false; } } public static async Task IsConnected() { Log.Info("IsConnected"); string url = BASE_URL + "/getCarInfo"; try { string s = await _httpClient.GetStringAsync(url); TboxCarInfoModel? tboxCarInfoModel = null; if (!String.IsNullOrWhiteSpace(s)) { tboxCarInfoModel = JsonConvert.DeserializeObject(s); } Log.Info($"GetCarInfo resp = {tboxCarInfoModel}"); return tboxCarInfoModel.Connected; } catch (Exception e) { Console.WriteLine(e); return false; } } /// /// 车辆解锁 /// /// 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) { Console.WriteLine(e); return false; } } /// /// 车辆上锁 /// /// 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) { Console.WriteLine(e); return false; } } }