using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using SlnMesnac.Business;
using SlnMesnac.Business.@base;
using SlnMesnac.Plc;
using SlnMesnac.WPF.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace SlnMesnac.WPF.Page
{
///
/// DevMonitorPage.xaml 的交互逻辑
///
public partial class DevMonitorPage : UserControl
{
private BaseBusiness baseBusiness = null;
PlcAbsractFactory plc = null;
int MachineSleep = 1000;
private readonly ILogger _logger;
public DevMonitorPage()
{
_logger = App.ServiceProvider.GetService>();
baseBusiness = App.ServiceProvider.GetService();
MachineSleep = Convert.ToInt32(baseBusiness.GetPlcAddressByConfigKey("设备启动间隔"));
plc = baseBusiness.GetPlcByKey("plc");
InitializeComponent();
this.DataContext = new DevMonitorViewModel();
}
///
/// 发送脉冲信号方法封装
///
/// plc地址对应的key
private void SendPulseSignal(string addressKey)
{
int PulseSleep = Convert.ToInt32(baseBusiness.GetPlcAddressByConfigKey("脉冲间隔"));
plc.writeBoolByAddress(baseBusiness.GetPlcAddressByConfigKey($"{addressKey}"), true);
Thread.Sleep(PulseSleep);
plc.writeBoolByAddress(baseBusiness.GetPlcAddressByConfigKey($"{addressKey}"), false);
}
///
/// 一键启动
///
///
///
private void StartButton_Click(object sender, RoutedEventArgs e)
{
try
{
if (plc.readBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("拆包机MES屏蔽")) == false)
{
MessageBox.Show("拆包机MES屏蔽,未切换远程");
return;
}
if (plc.readBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("磁选机MES屏蔽")) == false)
{
MessageBox.Show("磁选机MES屏蔽,未切换远程");
return;
}
if (plc.readBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("螺旋1MES屏蔽")) == false)
{
MessageBox.Show("螺旋1MES屏蔽,未切换远程");
return;
}
if (plc.readBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("螺旋2MES屏蔽")) == false)
{
MessageBox.Show("螺旋2MES屏蔽,未切换远程");
return;
}
//StartButton.IsEnabled = false;
//StopButton.IsEnabled = true;
//StopUrgentButton.IsEnabled = true;
Task.Run(() =>
{
plc.writeBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("拆包机远程急停"), false);
plc.writeBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("拆包机远程停止"), false);
#region 按顺序依次启动设备
#region 1.拆包机
plc.writeBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("拆包机远程急停"), false);
plc.writeBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("拆包机远程停止"), false);
SendPulseSignal("拆包机远程启动");
#endregion
Thread.Sleep(MachineSleep);
#region 2.磁选机
SendPulseSignal("磁选机一键启动");
#endregion
Thread.Sleep(MachineSleep);
#region 3.螺旋
plc.writeBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("螺旋1启动"), true);
Thread.Sleep(MachineSleep);
plc.writeBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("螺旋1启动"), true);
#endregion
Thread.Sleep(MachineSleep);
#region 4.烘干机
SendPulseSignal("烘干机螺旋启动");
Thread.Sleep(MachineSleep);
SendPulseSignal("烘干机风机启动");
Thread.Sleep(MachineSleep);
SendPulseSignal("烘干机燃烧启动");
#endregion
#endregion
});
}catch(Exception ex)
{
_logger.LogError($"一键启动:{ex.Message}");
}
}
///
/// 一键停止
///
///
///
private void StopButton_Click(object sender, RoutedEventArgs e)
{
try
{
if (plc.readBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("拆包机MES屏蔽")) == false)
{
MessageBox.Show("拆包机MES屏蔽,未切换远程");
return;
}
if (plc.readBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("磁选机MES屏蔽")) == false)
{
MessageBox.Show("磁选机MES屏蔽,未切换远程");
return;
}
#region 依次按序停止
Task.Run(() =>
{
});
#endregion
//StopButton.IsEnabled = false;
//StopUrgentButton.IsEnabled = false;
//StartButton.IsEnabled = true;
}catch (Exception ex)
{
_logger.LogError($"一键停止:{ex.Message}");
}
}
///
/// 急停
///
///
///
private void StopUrgentButton_Click(object sender, RoutedEventArgs e)
{
try
{
if (StopUrgentButton.Content.ToString() == "急停")
{
//StopButton.IsEnabled = false;
//StopUrgentButton.Content = "复位";
SendPulseSignal("拆包机远程急停");
}
else if (StopUrgentButton.Content.ToString() == "复位")
{
//StopButton.IsEnabled = true;
//StopUrgentButton.Content = "急停";
plc.writeBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("拆包机远程急停"), false);
}
}catch(Exception ex)
{
_logger.LogError($"拆包机远程急停:{ex.Message}");
}
}
#region 拆包机
///
/// 拆包机启动
///
///
///
private void StartUnPackButton_Click(object sender, RoutedEventArgs e)
{
try
{
if (plc.readBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("拆包机MES屏蔽")) == false)
{
MessageBox.Show("拆包机MES屏蔽,未切换远程");
return;
}
Task.Run(() =>
{
plc.writeBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("拆包机远程急停"), false);
plc.writeBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("拆包机远程停止"), false);
SendPulseSignal("拆包机远程启动");
});
//StartUnPackButton.IsEnabled = false;
//StopUnPackButton.IsEnabled = true;
}catch (Exception ex)
{
_logger.LogError($"拆包机远程启动:{ex.Message}");
}
}
///
/// 拆包机停止
///
///
///
private void StopUnPackButton_Click(object sender, RoutedEventArgs e)
{
try
{
if (plc.readBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("拆包机MES屏蔽")) == false)
{
MessageBox.Show("拆包机MES屏蔽,未切换远程");
return;
}
Task.Run(() =>
{
plc.writeBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("拆包机远程启动"), false);
SendPulseSignal("拆包机远程停止");
});
//StartUnPackButton.IsEnabled = true;
//StopUnPackButton.IsEnabled = false;
}
catch (Exception ex)
{
_logger.LogError($"拆包机停止:{ex.Message}");
}
}
#endregion
#region 磁选机
///
/// 磁选启动
///
///
///
private void StartMagNetButton_Click(object sender, RoutedEventArgs e)
{
try
{
if (plc.readBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("磁选机MES屏蔽")) == false)
{
MessageBox.Show("磁选机MES屏蔽,未切换远程");
return;
}
Task.Run(() =>
{
SendPulseSignal("磁选机一键启动");
});
}catch (Exception ex)
{
_logger.LogError($"磁选启动:{ex.Message}");
}
}
///
/// 磁选停止
///
///
///
private void StopMagNetButton_Click(object sender, RoutedEventArgs e)
{
try
{
if (plc.readBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("磁选机MES屏蔽")) == false)
{
MessageBox.Show("磁选机MES屏蔽,未切换远程");
return;
}
Task.Run(() =>
{
// plc.writeBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("磁选机一键启动"), false);
SendPulseSignal("磁选机一键停止");
});
}
catch (Exception ex)
{
_logger.LogError($"磁选停止:{ex.Message}");
}
}
#endregion
#region 螺旋机
///
/// 螺旋1启动
///
///
///
private void StartSpiral1Button_Click(object sender, RoutedEventArgs e)
{
try
{
if (plc.readBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("螺旋1MES屏蔽")) == false)
{
MessageBox.Show("螺旋1MES屏蔽,未切换远程");
return;
}
plc.writeBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("螺旋1启动"), true);
}
catch (Exception ex)
{
_logger.LogError($"螺旋1启动:{ex.Message}");
}
}
///
/// 螺旋2启动
///
///
///
private void StartSpiral2Button_Click(object sender, RoutedEventArgs e)
{
try
{
if (plc.readBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("螺旋2MES屏蔽")) == false)
{
MessageBox.Show("螺旋2MES屏蔽,未切换远程");
return;
}
plc.writeBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("螺旋2启动"), true);
}catch (Exception ex)
{
_logger.LogError($"螺旋2启动:{ex.Message}");
}
}
///
/// 螺旋1停止
///
///
///
private void StopSpiral1Button_Click(object sender, RoutedEventArgs e)
{
try
{
if (plc.readBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("螺旋1MES屏蔽")) == false)
{
MessageBox.Show("螺旋1MES屏蔽,未切换远程");
return;
}
plc.writeBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("螺旋1启动"), false);
}
catch (Exception ex)
{
_logger.LogError($"螺旋1停止:{ex.Message}");
}
}
///
/// 螺旋2停止
///
///
///
private void StopSpiral2Button_Click(object sender, RoutedEventArgs e)
{
try
{
if (plc.readBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("螺旋2MES屏蔽")) == false)
{
MessageBox.Show("螺旋2MES屏蔽,未切换远程");
return;
}
plc.writeBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("螺旋2启动"), false);
}
catch (Exception ex)
{
_logger.LogError($"螺旋2停止:{ex.Message}");
}
}
#endregion
///
/// 启动烘干机螺旋1
///
///
///
private void StartDryerSpiralButton_Click(object sender, RoutedEventArgs e)
{
try
{
Task.Run(() =>
{
SendPulseSignal("烘干机螺旋启动");
});
}
catch (Exception ex)
{
_logger.LogError($"烘干机螺旋启动异常:{ex.Message}");
}
}
///
/// 启动烘干机风机2
///
///
///
private void StartDryerFanButton_Click(object sender, RoutedEventArgs e)
{
try
{
Task.Run(() =>
{
SendPulseSignal("烘干机风机启动");
});
}
catch (Exception ex)
{
_logger.LogError($"烘干机风机启动:{ex.Message}");
}
}
///
/// 启动烘干机燃烧3
///
///
///
private void StartDryerHotButton_Click(object sender, RoutedEventArgs e)
{
try
{
Task.Run(() =>
{
SendPulseSignal("烘干机燃烧启动");
});
}
catch (Exception ex)
{
_logger.LogError($"烘干机燃烧启动:{ex.Message}");
}
}
///
/// 停止烘干机螺旋
///
///
///
private void StopDryerSpiralButton_Click(object sender, RoutedEventArgs e)
{
try
{
Task.Run(() =>
{
SendPulseSignal("烘干机螺旋停止");
});
}
catch (Exception ex)
{
_logger.LogError($"烘干机螺旋停止:{ex.Message}");
}
}
///
/// 停止烘干机燃烧
///
///
///
private void StopDryerHotButton_Click(object sender, RoutedEventArgs e)
{
try
{
Task.Run(() =>
{
SendPulseSignal("烘干机燃烧停止");
});
}
catch (Exception ex)
{
_logger.LogError($"烘干机燃烧停止:{ex.Message}");
}
}
///
/// 停止烘干机风机
///
///
///
private void StopDryerFanButton_Click(object sender, RoutedEventArgs e)
{
try
{
Task.Run(() =>
{
SendPulseSignal("烘干机风机停止");
});
}
catch (Exception ex)
{
_logger.LogError($"烘干机风机停止:{ex.Message}");
}
}
}
}