add-添加RFID设备断线重连机制

dev
liuwf 1 week ago
parent ad63f9a886
commit 54a0ad12d7

@ -50,6 +50,8 @@ namespace SlnMesnac.Extensions
{
RfidAbsractFactory _rfid = x.GetService<RflyFactory>();
bool connectResult = _rfid.Connect(item.equipIp, item.equipPort);
_rfid.ip = item.equipIp;
_rfid.port = item.equipPort;
if (connectResult)
{
Log.Information($"RFID{item.equipIp}:{item.equipPort};连接成功,时间:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}");
@ -59,12 +61,20 @@ namespace SlnMesnac.Extensions
{
absractFactories.Remove(_rfid);
}
_rfid.IsConnected = true;
absractFactories.Add(_rfid);
}
else
{
Log.Information($"RFID{item.equipIp}:{item.equipPort};连接失败,时间:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}");
_rfid.ConfigKey = item.equipKey;
if (absractFactories.Contains(_rfid))
{
absractFactories.Remove(_rfid);
}
_rfid.IsConnected = false;
absractFactories.Add(_rfid);
}
}
}

@ -40,7 +40,7 @@ namespace SlnMesnac.Rfid.Factory
public class RflyFactory:RfidAbsractFactory
{
private ILogger<RflyFactory> _logger;
private readonly TcpClient _tcpClient = new TcpClient();
public readonly TcpClient _tcpClient = new TcpClient();
private readonly StringChange _stringChange;
public RflyFactory(ILogger<RflyFactory> logger,StringChange stringChange)
@ -49,6 +49,10 @@ namespace SlnMesnac.Rfid.Factory
_stringChange = stringChange;
}
/// <summary>
/// 建立连接
/// </summary>
@ -66,7 +70,8 @@ namespace SlnMesnac.Rfid.Factory
}
catch (Exception e)
{
throw new InvalidOperationException($"设备连接异常:{e.Message}");
_logger.LogError($"RFID设备{ip}连接异常:{e.Message}");
return false;
}
}
@ -534,6 +539,18 @@ namespace SlnMesnac.Rfid.Factory
return epcLength;
}
public override void RefreshStatus()
{
if (_tcpClient != null)
{
this.IsConnected = _tcpClient.Online;
}
else
{
this.IsConnected = false;
}
}
#endregion
}
}

@ -32,8 +32,24 @@ namespace SlnMesnac.Rfid
/// </summary>
public abstract class RfidAbsractFactory
{
public string ip;
public int port;
public bool IsConnected { get; set; }
public string ConfigKey { get; set; }
/// <summary>
/// 获取连接状态
/// </summary>
/// <param name="ip"></param>
/// <param name="port"></param>
/// <returns></returns>
public abstract void RefreshStatus();
/// <summary>
/// 建立连接
/// </summary>

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<UseWPF>true</UseWPF>

@ -39,7 +39,7 @@ namespace SlnMesnac.WPF.ViewModel
private readonly BaseConfigInfoPage configInfoPage = new BaseConfigInfoPage();
private readonly RecipeManagePage recipeManagePage = new RecipeManagePage();
private readonly AgvAndTaskMonitorPage agvAndTaskMonitorPage = new AgvAndTaskMonitorPage();
private System.Timers.Timer timer = new System.Timers.Timer(1000 * 5);
private System.Timers.Timer timer = new System.Timers.Timer(1000 * 60);
PlcAbsractFactory? plc = null;
private BaseBusiness? baseBusiness = null;
#region 参数定义
@ -273,15 +273,16 @@ namespace SlnMesnac.WPF.ViewModel
#region RFID状态
var rfidEquip = baseBusiness.GetRfidByKey("secondFloorOut");
if (rfidEquip != null)
if (rfidEquip != null && rfidEquip.IsConnected)
{
rfidEquip.RefreshStatus();
Out2FRfidStatus = 1;
}
else
else
{
Out2FRfidStatus = 2;
Out2FRfidStatus = 2;
bool result = rfidEquip.Connect(rfidEquip.ip, rfidEquip.port);
rfidEquip.IsConnected = result;
}
#endregion

@ -66,6 +66,7 @@
// RFID
"configId": 2,
"equipIp": "192.168.2.28",
// "equipIp": "127.0.0.1",
"equipPort": 20108,
"equipKey": "secondFloorOut",
"isFlage": true

Loading…
Cancel
Save