add -添加压力PLC断线重连,PLC断线重连抽离成独立方法

dev
启龙 曹 4 weeks ago
parent 3e1db78037
commit aa1b64d33b

@ -41,6 +41,7 @@ namespace SlnMesnac.WPF.ViewModel
private readonly AgvAndTaskMonitorPage agvAndTaskMonitorPage = new AgvAndTaskMonitorPage();
private System.Timers.Timer timer = new System.Timers.Timer(1000 * 60);
PlcAbsractFactory? plc = null;
private PlcAbsractFactory? pressurePlc = null;
private BaseBusiness? baseBusiness = null;
#region 参数定义
@ -79,8 +80,6 @@ namespace SlnMesnac.WPF.ViewModel
}
}
/// <summary>
/// PLC设备状态
/// </summary>
@ -90,6 +89,17 @@ namespace SlnMesnac.WPF.ViewModel
get { return _PlcStatus; }
set { _PlcStatus = value; RaisePropertyChanged(nameof(PlcStatus)); }
}
/// <summary>
/// 压力PLC设备状态
/// </summary>
private int _PressurePlcStatus = 0;
public int PressurePlcStatus
{
get { return _PressurePlcStatus; }
set { _PressurePlcStatus = value; RaisePropertyChanged(nameof(PressurePlcStatus)); }
}
/// <summary>
/// 喷码机状态
/// </summary>
@ -154,6 +164,7 @@ namespace SlnMesnac.WPF.ViewModel
FormControlCommand = new RelayCommand<object>(x => FormControl(x));
baseBusiness = App.ServiceProvider.GetService<BaseBusiness>();
plc = baseBusiness.GetPlcByKey("plc");
pressurePlc = baseBusiness.GetPlcByKey("pressurePlc");
StartLiseningStatus();
checkStatus();
RefreshTime();
@ -232,32 +243,16 @@ namespace SlnMesnac.WPF.ViewModel
timer.Start();
}
/// <summary>
/// 检查设备状态
/// </summary>
private void checkStatus()
{
try
{
#region PLC状态
if (plc != null && plc.IsConnected)
{
plc.IsConnected = plc.readHeartByAddress("M100");
PlcStatus = 1;
}
else
{
PlcStatus = 2;
PlcConfig? plcConfig = App.ServiceProvider.GetService<AppConfig>().plcConfig.FirstOrDefault(x => x.plcKey == "plc");
if (plcConfig != null)
{
bool result = plc.Connect(plcConfig.plcIp, plcConfig.plcPort);
plc.IsConnected = result;
}
CheckPlcStatus(plc, "plc", status => PlcStatus = status);
#endregion
}
#region 压力PLC状态断线重连
CheckPlcStatus(pressurePlc, "pressurePlc", status => PressurePlcStatus = status);
#endregion
#region 喷码机状态
@ -295,6 +290,28 @@ namespace SlnMesnac.WPF.ViewModel
}
/// <summary>
/// 检查设备状态
/// </summary>
private void CheckPlcStatus(PlcAbsractFactory plc, string plcKey, Action<int> setStatus)
{
if (plc != null && plc.IsConnected)
{
plc.IsConnected = plc.readHeartByAddress("M100");
setStatus(1);
}
else
{
setStatus(2);
PlcConfig? plcConfig = App.ServiceProvider.GetService<AppConfig>().plcConfig.FirstOrDefault(x => x.plcKey == plcKey);
if (plcConfig != null)
{
bool result = plc.Connect(plcConfig.plcIp, plcConfig.plcPort);
plc.IsConnected = result;
}
}
}
#endregion
/// <summary>

Loading…
Cancel
Save