using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Newtonsoft.Json.Linq; 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; using TouchSocket.Core; 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 按顺序依次启动设备 //1.螺旋2 bool Spiral2Status = StartSpiral2(); if (!Spiral2Status) return; //2.磁选机 bool MagNetStatus = StartMagNet(); if (!MagNetStatus) return; //3.螺旋1 bool Spiral1Status = StartSpiral1(); if (!MagNetStatus) return; //4.烘干机:烘干螺旋、烘干风机、烘干燃烧 bool DryerStatus1 = StartDryerMagNet(); if (!DryerStatus1) return; bool DryerStatus2 = StartDryerFan(); if (!DryerStatus2) return; bool DryerStatus3 = StartDryerHot(); if (!DryerStatus3) return; //5.拆包机 bool unpackStatus = StartUnPack(); #endregion if (unpackStatus) { MessageBoxAndLog("一键启动所有机器成功!"); } }); }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; } if (plc.readBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("螺旋1MES允许远程")) == false) { MessageBox.Show("螺旋1MES允许远程,未切换远程"); return; } if (plc.readBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("螺旋2MES允许远程")) == false) { MessageBox.Show("螺旋2MES允许远程,未切换远程"); return; } #region 依次按序停止 Task.Run(() => { plc.writeBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("拆包机远程启动"), false); SendPulseSignal("拆包机远程停止"); Thread.Sleep(MachineSleep); #region 烘干机 SendPulseSignal("烘干机螺旋停止"); Thread.Sleep(MachineSleep); SendPulseSignal("烘干机燃烧停止"); Thread.Sleep(MachineSleep); SendPulseSignal("烘干机风机停止"); plc.writeBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("螺旋1启动"), false); Thread.Sleep(MachineSleep); SendPulseSignal("磁选机一键停止"); Thread.Sleep(MachineSleep); plc.writeBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("螺旋2启动"), false); #endregion MessageBoxAndLog("一键停止所有机器成功!"); }); #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) { StartMagNet(); } #endregion #region 螺旋机 /// /// 螺旋1启动 /// /// /// private void StartSpiral1Button_Click(object sender, RoutedEventArgs e) { StartSpiral1(); } /// /// 螺旋2启动 /// /// /// private void StartSpiral2Button_Click(object sender, RoutedEventArgs e) { StartSpiral2(); } /// /// 螺旋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 { StartDryerMagNet(); } 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) { StartDryerHot(); } /// /// 停止烘干机螺旋 /// /// /// 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}"); } } /// /// 启动螺旋2 /// private bool StartSpiral2() { bool result = false; try { if (plc.readBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("螺旋2MES允许远程")) == false) { MessageBox.Show("螺旋2MES允许远程,未切换远程"); result = false; } else { int value2 = int.Parse(baseBusiness.GetPlcAddressByConfigKey("螺旋2速度值")) * 100; plc.writeBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("螺旋2启动"), true); plc.writeInt16ByAddress(baseBusiness.GetPlcAddressByConfigKey("螺旋2速度设置"), value2); Thread.Sleep(MachineSleep); result = plc.readBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("螺旋2启动")); } } catch (Exception ex) { MessageBoxAndLog($"螺旋2启动异常:{ex.Message}", true); result = false; } return result; } /// /// 启动磁选 /// private bool StartMagNet() { try { if (plc.readBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("磁选机MES允许远程")) == false) { MessageBox.Show("磁选机MES允许远程,未切换远程"); return false; } #region 启动磁选机 / 前提:check螺旋2启动及速度是否达标 int value2 = int.Parse(baseBusiness.GetPlcAddressByConfigKey("螺旋2速度值")) * 100; bool startFlag = plc.readBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("螺旋2启动")); int speed2 = plc.readInt16ByAddress(baseBusiness.GetPlcAddressByConfigKey("螺旋2速度反馈")); if (!startFlag) { MessageBox.Show($"前提条件螺旋2未成功启动,请检查后重新启动"); return false; } if (speed2 < value2 * 0.8) { plc.writeBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("螺旋2启动"), false); MessageBox.Show($"前提条件螺旋2速度{speed2}未达到设定值{value2}的下限阈值80%,请检查后重新启动"); return false; } SendPulseSignal("磁选机一键启动"); Thread.Sleep(MachineSleep); #endregion bool flag1 = plc.readBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("磁选机磁选启动")); bool flag2 = plc.readBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("磁选机震动启动")); if(flag1&& flag2) { return true; } else { MessageBoxAndLog("磁选机启动失败,请检查后设备状态后重新尝试",true); return false; } } catch (Exception ex) { MessageBoxAndLog($"磁选机启动异常:{ex.Message}", true); return false; } } /// /// 启动螺旋1 /// private bool StartSpiral1() { bool result = false; try { if (plc.readBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("螺旋1MES允许远程")) == false) { MessageBox.Show("螺旋1MES允许远程,未切换远程"); return false; } bool flag1 = plc.readBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("磁选机磁选启动")); bool flag2 = plc.readBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("磁选机震动启动")); if (!flag1 || !flag2) { MessageBoxAndLog("前提条件磁选机未启动,请先检查设备状态再启动", true); return false; } int value1 = int.Parse(baseBusiness.GetPlcAddressByConfigKey("螺旋1速度值")); plc.writeBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("螺旋1启动"), true); plc.writeInt16ByAddress(baseBusiness.GetPlcAddressByConfigKey("螺旋1速度设置"), value1 * 100); Thread.Sleep(MachineSleep); result = plc.readBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("螺旋1启动")); } catch (Exception ex) { MessageBoxAndLog($"螺旋1启动异常:{ex.Message}", true); result = false; } return result; } #region 烘干机 /// /// 烘干机螺旋启动/前提:check:螺旋1启动且速度达到值 /// private bool StartDryerMagNet() { bool result = false; try { int value1 = int.Parse(baseBusiness.GetPlcAddressByConfigKey("螺旋1速度值")) * 100; bool startFlag = plc.readBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("螺旋1启动")); int speed1 = plc.readInt16ByAddress(baseBusiness.GetPlcAddressByConfigKey("螺旋1速度反馈")); if (!startFlag) { MessageBox.Show($"烘干机螺旋启动失败:前提条件螺旋1未成功启动,请检查后重新启动"); return false; } if (speed1 < value1 * 0.8) { // plc.writeBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("螺旋2启动"), false); MessageBox.Show($"烘干机螺旋启动失败:前提条件螺旋1速度{speed1}未达到设定值{value1}的下限阈值80%,请检查后重新启动"); return false; } int value = int.Parse(baseBusiness.GetPlcAddressByConfigKey("烘干机螺旋频率设定值")); SendPulseSignal("烘干机螺旋启动"); plc.writeInt16ByAddress(baseBusiness.GetPlcAddressByConfigKey("烘干机螺旋频率设定"), value1 * 100); Thread.Sleep(MachineSleep/2); return true; } catch (Exception ex) { MessageBoxAndLog($"烘干机螺旋启动异常:{ex.Message}", true); result = false; } return result; } /// /// 烘干机风机启动/前提:check:烘干机螺旋启动 /// private bool StartDryerFan() { bool result = false; try { bool startFlag = plc.readBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("烘干机反馈传动启动")); if (!startFlag) { MessageBox.Show($"烘干机风机启动失败:前提条件烘干机螺旋未成功启动,请检查后重新启动"); return false; } SendPulseSignal("烘干机风机启动"); Thread.Sleep(MachineSleep); return true; } catch (Exception ex) { MessageBoxAndLog($"烘干机风机启动异常:{ex.Message}", true); result = false; } return result; } /// /// 烘干机燃烧启动/前提:check:烘干机风机启动 /// private bool StartDryerHot() { bool result = false; try { bool startFlag = plc.readBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("烘干机反馈风机启动")); if (!startFlag) { MessageBox.Show($"烘干机燃烧启动失败:前提条件烘干机风机未成功启动,请检查后重新启动"); return false; } int value = int.Parse(baseBusiness.GetPlcAddressByConfigKey("烘干机温度设定值")); SendPulseSignal("烘干机燃烧启动"); plc.writeInt16ByAddress(baseBusiness.GetPlcAddressByConfigKey("烘干机温度设定"), value *100); Thread.Sleep(MachineSleep); return true; } catch (Exception ex) { MessageBoxAndLog($"烘干机燃烧启动异常:{ex.Message}", true); result = false; } return result; } #endregion /// /// 拆包机启动/前提:check:燃烧机启动 /// private bool StartUnPack() { bool result = false; try { bool startFlag = plc.readBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("烘干机反馈燃烧启动")); if (!startFlag) { MessageBox.Show($"拆包机启动失败:前提条件烘干机燃烧未成功启动,请检查后重新启动"); return false; } plc.writeBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("拆包机远程急停"), false); plc.writeBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("拆包机远程停止"), false); SendPulseSignal("拆包机远程启动"); Thread.Sleep(MachineSleep); return plc.readBoolByAddress(baseBusiness.GetPlcAddressByConfigKey("拆包机运行状态系统")); } catch (Exception ex) { MessageBoxAndLog($"拆包机启动异常:{ex.Message}", true); result = false; } return result; } private void MessageBoxAndLog(string message,bool isError = false) { if (isError) { _logger.LogError(message); } else { _logger.LogInformation(message); } MessageBox.Show(message); } } }