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.
113 lines
2.8 KiB
113 lines
2.8 KiB
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:7243";
|
|
|
|
private static readonly HttpClient _httpClient = new HttpClient()
|
|
{
|
|
Timeout = TimeSpan.FromSeconds(60)
|
|
};
|
|
|
|
public static async Task<TboxCarInfoModel> GetCarInfo()
|
|
{
|
|
Log.Info("GetCarInfo");
|
|
string url = BASE_URL + "/CarControl/getCarInfo";
|
|
try
|
|
{
|
|
string s = await _httpClient.GetStringAsync(url);
|
|
TboxCarInfoModel? tboxCarInfoModel = null;
|
|
if (!String.IsNullOrWhiteSpace(s))
|
|
{
|
|
tboxCarInfoModel = JsonConvert.DeserializeObject<TboxCarInfoModel>(s);
|
|
}
|
|
|
|
Log.Info($"GetCarInfo resp = {tboxCarInfoModel}");
|
|
return tboxCarInfoModel;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine(e);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public static async Task<bool> IsConnected()
|
|
{
|
|
Log.Info("IsConnected");
|
|
string url = BASE_URL + "/CarControl/getCarInfo";
|
|
try
|
|
{
|
|
string s = await _httpClient.GetStringAsync(url);
|
|
TboxCarInfoModel? tboxCarInfoModel = null;
|
|
if (!String.IsNullOrWhiteSpace(s))
|
|
{
|
|
tboxCarInfoModel = JsonConvert.DeserializeObject<TboxCarInfoModel>(s);
|
|
}
|
|
|
|
Log.Info($"GetCarInfo resp = {tboxCarInfoModel}");
|
|
return tboxCarInfoModel.Connected;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine(e);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 车辆解锁
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public static async Task<bool> UnLockCar()
|
|
{
|
|
Log.Info("UnLockCar");
|
|
string url = BASE_URL + "/CarControl/unLock";
|
|
try
|
|
{
|
|
string s = await _httpClient.GetStringAsync(url);
|
|
|
|
|
|
Log.Info($"UnLockCar resp = {s}");
|
|
return bool.Parse(s);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine(e);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 车辆上锁
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public static async Task<bool> LockCar()
|
|
{
|
|
Log.Info("LockCar");
|
|
string url = BASE_URL + "/CarControl/lock";
|
|
try
|
|
{
|
|
string s = await _httpClient.GetStringAsync(url);
|
|
|
|
|
|
Log.Info($"LockCar resp = {s}");
|
|
return bool.Parse(s);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine(e);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
|
|
} |