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.

131 lines
3.3 KiB

using log4net;
using Newtonsoft.Json;
using Service.Execute.Model;
namespace Service.Execute.Api;
public class RfidApi
{
private static readonly ILog Log = LogManager.GetLogger("RfidApi");
private static readonly string BASE_URL = "http://localhost:5037";
private static readonly HttpClient _httpClient = new HttpClient()
{
Timeout = TimeSpan.FromSeconds(60)
};
public static async Task<bool> BeginRead()
{
5 months ago
// var disConnect =await DisConnect();
//断连
5 months ago
// var connect = await Connect();
/*
if (!connect)
{
Log.Info("RfidApi connect fail");
return false;
}
5 months ago
*/
Log.Info("RfidApi BeginRead");
string url = BASE_URL + "/Api/BeginRead";
try
{
string s = await _httpClient.GetStringAsync(url);
Log.Info($"BeginRead resp = {s}");
return bool.Parse(s);
}
catch (Exception e)
{
Log.Error($"RfidApi BeginRead err e={e}");
return false;
}
}
public static async Task<bool> StopRead()
{
Log.Info("RfidApi StopRead");
string url = BASE_URL + "/Api/StopRead";
try
{
string s = await _httpClient.GetStringAsync(url);
Log.Info($"StopRead resp = {s}");
return bool.Parse(s);
}
catch (Exception e)
{
Log.Error($"RfidApi StopRead err e={e}");
return false;
}
}
public static async Task<bool> DisConnect()
{
Log.Info("RfidApi DisConnect");
string url = BASE_URL + "/Api/Close";
try
{
string s = await _httpClient.GetStringAsync(url);
Log.Info($"DisConnect resp={s}");
return bool.Parse(s);
}
catch (Exception e)
{
Log.Error($"RfidApi DisConnect err e={e}");
return false;
}
}
public static async Task<bool> Connect()
{
var disConnect = await DisConnect();
Log.Info("RfidApi Connect");
string url = BASE_URL + "/Api/Open";
try
{
string s = await _httpClient.GetStringAsync(url);
Log.Info($"Connect resp={s}");
return bool.Parse(s);
}
catch (Exception e)
{
Log.Error($"RfidApi Connect err e={e}");
return false;
}
}
6 months ago
public static async Task<RfidReadModel?> ReadRfid()
{
6 months ago
Log.Info("RfidApi ReadRfid");
2 months ago
// string readUrl = BASE_URL + "/Api/BeginRead";
string url = BASE_URL + "/Api/ReadRfidData";
2 months ago
Log.Info($"url={url}");
try
{
2 months ago
//var vBeginRead = await _httpClient.GetStringAsync(readUrl);
string s = await _httpClient.GetStringAsync(url);
6 months ago
Log.Info($"ReadRfid resp={s}");
if (s != String.Empty)
{
RfidReadModel? model = JsonConvert.DeserializeObject<RfidReadModel>(s);
if (model != null)
{
return model;
}
}
}
catch (Exception e)
{
6 months ago
Log.Error($"RfidApi ReadRfid err e={e}");
return null;
}
return null;
}
}