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.

213 lines
6.6 KiB

using Entity.Constant;
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> BeginWriteAsync(string vehicleNo, string vin)
{
if (string.IsNullOrWhiteSpace(vehicleNo) || string.IsNullOrWhiteSpace(vin))
{
Log.Error("Vehicle number or VIN cannot be null or empty.");
return false;
}
string requestPath = $"{BASE_URL}/Api/BeginWrite/{vehicleNo};{vin}";
Log.Info($"BeginWrite request to: {requestPath}");
try
{
StationSoftMgr.PutDeviceLog((int)StationConstant.DeviceCode.Rfid,SwapConstant.RfidProtocol.BeginWrite,
$"{vehicleNo};{vin}",(int)SwapConstant.CommunicationType.Send);
string response = await _httpClient.GetStringAsync(requestPath);
StationSoftMgr.PutDeviceLog((int)StationConstant.DeviceCode.Rfid,SwapConstant.RfidProtocol.BeginWrite,
response,(int)SwapConstant.CommunicationType.Receive);
return response.Equals("true", StringComparison.OrdinalIgnoreCase);
}
catch (HttpRequestException httpEx)
{
Log.Error($"HTTP request error during BeginWrite: {httpEx.Message}");
}
catch (Exception ex)
{
Log.Error($"Unexpected error during BeginWrite: {ex.Message}");
}
return false;
}
public static async Task<bool> BeginRead()
{
// var disConnect =await DisConnect();
//断连
// var connect = await Connect();
/*
if (!connect)
{
Log.Info("RfidApi connect fail");
return false;
}
*/
Log.Info("RfidApi BeginRead");
string url = BASE_URL + "/Api/BeginRead";
try
{
StationSoftMgr.PutDeviceLog((int)StationConstant.DeviceCode.Rfid,SwapConstant.RfidProtocol.BeginRead,
null,(int)SwapConstant.CommunicationType.Send);
string s = await _httpClient.GetStringAsync(url);
Log.Info($"BeginRead resp = {s}");
StationSoftMgr.PutDeviceLog((int)StationConstant.DeviceCode.Rfid,SwapConstant.RfidProtocol.BeginRead,
s,(int)SwapConstant.CommunicationType.Receive);
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
{
StationSoftMgr.PutDeviceLog((int)StationConstant.DeviceCode.Rfid,SwapConstant.RfidProtocol.StopRead,
null,(int)SwapConstant.CommunicationType.Send);
string s = await _httpClient.GetStringAsync(url);
StationSoftMgr.PutDeviceLog((int)StationConstant.DeviceCode.Rfid,SwapConstant.RfidProtocol.StopRead,
s,(int)SwapConstant.CommunicationType.Receive);
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
{
StationSoftMgr.PutDeviceLog((int)StationConstant.DeviceCode.Rfid,SwapConstant.RfidProtocol.DisConnect,
null,(int)SwapConstant.CommunicationType.Send);
string s = await _httpClient.GetStringAsync(url);
Log.Info($"DisConnect resp={s}");
StationSoftMgr.PutDeviceLog((int)StationConstant.DeviceCode.Rfid,SwapConstant.RfidProtocol.DisConnect,
s,(int)SwapConstant.CommunicationType.Receive);
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
{
StationSoftMgr.PutDeviceLog((int)StationConstant.DeviceCode.Rfid,SwapConstant.RfidProtocol.Connect,
null,(int)SwapConstant.CommunicationType.Send);
string s = await _httpClient.GetStringAsync(url);
Log.Info($"Connect resp={s}");
StationSoftMgr.PutDeviceLog((int)StationConstant.DeviceCode.Rfid,SwapConstant.RfidProtocol.Connect,
s,(int)SwapConstant.CommunicationType.Receive);
return bool.Parse(s);
}
catch (Exception e)
{
Log.Error($"RfidApi Connect err e={e}");
return false;
}
}
public static async Task<RfidReadModel?> ReadRfid()
{
Log.Info("RfidApi ReadRfid");
// string readUrl = BASE_URL + "/Api/BeginRead";
string url = BASE_URL + "/Api/ReadRfidData";
Log.Info($"url={url}");
try
{
// var vBeginRead = await _httpClient.GetStringAsync(readUrl);
StationSoftMgr.PutDeviceLog((int)StationConstant.DeviceCode.Rfid,SwapConstant.RfidProtocol.ReadRfidData,
null,(int)SwapConstant.CommunicationType.Send);
string s = await _httpClient.GetStringAsync(url);
Log.Info($"ReadRfid resp={s}");
StationSoftMgr.PutDeviceLog((int)StationConstant.DeviceCode.Rfid,SwapConstant.RfidProtocol.ReadRfidData,
s,(int)SwapConstant.CommunicationType.Receive);
if (s != String.Empty)
{
RfidReadModel? model = JsonConvert.DeserializeObject<RfidReadModel>(s);
if (model != null)
{
return model;
}
}
}
catch (Exception e)
{
Log.Error($"RfidApi ReadRfid err e={e}");
return null;
}
return null;
}
}