using BPASmartClient.Academy._50L; using BPASmartClient.CustomResource.UserControls.MessageShow; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Controls.Primitives; 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 BPASmartClient.Academy.View { /// /// ReactionKettle500LView.xaml 的交互逻辑 /// public partial class ReactionKettle50LView : UserControl { private volatile static ReactionKettle50LView _Instance; public static ReactionKettle50LView GetInstance => _Instance ?? (_Instance = new ReactionKettle50LView()); private ReactionKettle50LView() { InitializeComponent(); this.DataContext = ReactionKettle50LViewModel.GetInstance; } private void open_Click(object sender, RoutedEventArgs e) { if (sender is ButtonBase button && !string.IsNullOrWhiteSpace(button.Tag.ToString().Trim())) { if (PlcControl.GetInstance.IsConnect) { var tag = button.Tag.ToString().Trim(); //BoolAddEnum addr = (BoolAddEnum)Enum.Parse(typeof(BoolAddEnum), tag); //var result = PlcControl.GetInstance.Write(addr, true); //if (result != null && result.IsSuccess) //{ // Notify(EnumPromptType.Success, $"写入成功", $"成功:写入{tag}:True"); //} //else //{ // Notify(EnumPromptType.Error, $"写入失败", $"失败:写入{tag}:True\n{result.Message}"); //} WriteBooleanAddr(tag,true); } else { Notify(EnumPromptType.Error, "失败", "设备未连接"); } } } private void close_Click(object sender, RoutedEventArgs e) { if (sender is ButtonBase button && !string.IsNullOrWhiteSpace(button.Tag.ToString().Trim())) { if (PlcControl.GetInstance.IsConnect) { var tag = button.Tag.ToString().Trim(); //BoolAddEnum addr = (BoolAddEnum)Enum.Parse(typeof(BoolAddEnum), tag); //var result = PlcControl.GetInstance.Write(addr, false); //if (result != null && result.IsSuccess) //{ // Notify(EnumPromptType.Success, $"写入成功", $"成功:写入{tag}:False"); //} //else //{ // Notify(EnumPromptType.Error, $"写入失败", $"失败:写入{tag}:False\n{result.Message}"); //} WriteBooleanAddr(tag, false); } else { Notify(EnumPromptType.Error, "失败", "设备未连接"); } } } private void Notify(EnumPromptType promptType,string title,string msg) { App.Current.Dispatcher.Invoke(new Action(() => { NoticeDemoViewModel.OpenMsg(promptType, App.Current.MainWindow, title, msg); })); MessageNotify.GetInstance.ShowRunLog($"人工介入:"+msg); } private void auto_click(object sender, RoutedEventArgs e) { } private void TempSet_CLick(object sender, RoutedEventArgs e) { if (sender is ButtonBase button && !string.IsNullOrWhiteSpace(button.Tag.ToString().Trim())) { if (PlcControl.GetInstance.IsConnect) { var tag = button.Tag.ToString().Trim(); switch (tag) { case "反应釜升温": if (float.TryParse(reactorHeatUpTemp.Text,out float reactorHeatTemp) && reactorHeatTemp <= 200f && reactorHeatTemp >= 0f) { PlcControl.GetInstance.Write(FloatAddEnum.反应釜设定升温温度, MathF.Round(reactorHeatTemp, 2)).OnSuccess((s) => { PlcControl.GetInstance.Write(BoolAddEnum.反应釜升温, true).OnSuccess((s2) => { Notify(EnumPromptType.Success, $"写入成功", $"成功:写入反应釜升温:{reactorHeatTemp}"); }).OnFailure((s) => { Notify(EnumPromptType.Error, "写入失败", $"{s.Message}。"); }); }).OnFailure((s) => { Notify(EnumPromptType.Error, "写入失败", $"{s.Message}。"); }); } else { Notify(EnumPromptType.Error, "写入失败", $"输入的温度不是一个有效的数字,范围0-200℃。"); } break; case "反应釜降温": if (float.TryParse(reactorCoolDownTemp.Text, out float reactorCoolTemp) && reactorCoolTemp<=200f && reactorCoolTemp>=0f) { PlcControl.GetInstance.Write(FloatAddEnum.反应釜设定降温温度, MathF.Round(reactorCoolTemp, 2)).OnSuccess((s) => { PlcControl.GetInstance.Write(BoolAddEnum.反应釜降温, true).OnSuccess((s2) => { Notify(EnumPromptType.Success, $"写入成功", $"成功:写入反应釜降温:{reactorCoolTemp}"); }).OnFailure((s) => { Notify(EnumPromptType.Error, "写入失败", $"{s.Message}。"); }); }).OnFailure((s) => { Notify(EnumPromptType.Error, "写入失败", $"{s.Message}。"); }); } else { Notify(EnumPromptType.Error, "写入失败", $"输入的温度不是一个有效的数字,范围0-200℃。"); } break; case "冷凝水罐降温": if (float.TryParse(ColdWaterTankCoolDownTemp.Text, out float CWTTemp) && CWTTemp <= 200f && CWTTemp >= 0f) { PlcControl.GetInstance.Write(FloatAddEnum.冷凝水罐设定降温温度, MathF.Round(CWTTemp, 2)).OnSuccess((s) => { PlcControl.GetInstance.Write(BoolAddEnum.冷凝水罐降温, true).OnSuccess((s2) => { Notify(EnumPromptType.Success, $"写入成功", $"成功:写入冷凝水罐降温:{CWTTemp}"); }).OnFailure((s) => { Notify(EnumPromptType.Error, "写入失败", $"{s.Message}。"); }); }).OnFailure((s) => { Notify(EnumPromptType.Error, "写入失败", $"{s.Message}。"); }); } else { Notify(EnumPromptType.Error, "写入失败", $"输入的温度不是一个有效的数字,范围0-200℃。"); } break; } } else { Notify(EnumPromptType.Error, "失败", "设备未连接"); } } } private void TempCLose_CLick(object sender, RoutedEventArgs e) { close_Click(sender, e); } private void TempAdjust_Click(object sender, RoutedEventArgs e) { if (PlcControl.GetInstance.IsConnect) { if (float.TryParse(reactorTempAdjustValue.Text, out float reactorAdjustTemp)) { PlcControl.GetInstance.Write(FloatAddEnum.反应釜温度校准, MathF.Round(reactorAdjustTemp, 2)).OnSuccess((s) => { Notify(EnumPromptType.Success, $"写入校准成功", $"成功:写入反应釜温度校准:{reactorAdjustTemp}"); }).OnFailure((s) => { Notify(EnumPromptType.Error, "写入校准失败", $"{s.Message}。"); }); } else { Notify(EnumPromptType.Error, "写入校准失败", $"【反应釜温度校准】输入的温度不是一个有效的数字"); } if (float.TryParse(coldWaterTankTempAdjustValue.Text, out float CWTAdjustValue)) { PlcControl.GetInstance.Write(FloatAddEnum.冷凝水罐温度校准, MathF.Round(CWTAdjustValue, 2)).OnSuccess((s) => { Notify(EnumPromptType.Success, $"写入校准成功", $"成功:写入冷凝水罐温度校准:{CWTAdjustValue}"); }).OnFailure((s) => { Notify(EnumPromptType.Error, "写入校准失败", $"{s.Message}。"); }); } else { Notify(EnumPromptType.Error, "写入校准失败", $"【冷凝水罐温度校准】输入的温度不是一个有效的数字"); } } else { Notify(EnumPromptType.Error, "校准失败", "设备未连接"); } } private void SetReactorSpeed_Click(object sender, RoutedEventArgs e) { if (PlcControl.GetInstance.IsConnect) { SetParamValue(reactorRotateStirSpeedValue.Text, FloatAddEnum.反应釜旋转炒制速度, 0, 100); SetParamValue(reactorRotateSlowSpeedValue.Text, FloatAddEnum.反应釜旋转慢速度, 0, 100); } else { Notify(EnumPromptType.Error, "写入失败", "设备未连接"); } } private void SetParamValue(string text,FloatAddEnum floatAdd,float minValue,float maxValue) { if (float.TryParse(text, out float tempValue) && tempValue>=minValue && tempValue<=maxValue) { PlcControl.GetInstance.Write(floatAdd, MathF.Round(tempValue, 2)).OnSuccess((s) => { Notify(EnumPromptType.Success, $"写入成功", $"成功:写入【{floatAdd.ToString()}】:{tempValue}"); }).OnFailure((s) => { Notify(EnumPromptType.Error, "写入失败", $"{s.Message}。"); }); } else { Notify(EnumPromptType.Error, "写入失败", $"【{floatAdd.ToString()}】输入的温度不是一个有效的数字,限值{minValue} -- {maxValue}。"); } } private void steamParamsSet_Click(object sender, RoutedEventArgs e) { if (PlcControl.GetInstance.IsConnect) { SetParamValue(steamValveApertureValve.Text, FloatAddEnum.蒸汽比例阀开度, 0, 100); SetParamValue(reactorSteamPressureLimitValue.Text, FloatAddEnum.反应釜蒸汽压力上限, -0.1F, 0.6F); SetParamValue(reactorPressureLimitValue.Text, FloatAddEnum.反应釜压力上限, 0F, 1F); } else { Notify(EnumPromptType.Error, "写入失败", "设备未连接"); } } private void Canvas_MouseDown(object sender, MouseButtonEventArgs e) { if (e.Source is HandValve valve) { if (PlcControl.GetInstance.IsConnect && valve.Tag != null && !string.IsNullOrWhiteSpace(valve.Tag.ToString().Trim())) { var tag = valve.Tag.ToString().Trim(); BoolAddEnum addr = (BoolAddEnum)Enum.Parse(typeof(BoolAddEnum), tag); DeviceControlView.Show(tag, "未知", valve.OpenEnable ? "打开" : "关闭", new Action(() => { var result = PlcControl.GetInstance.Write(addr, true); if (result != null && result.IsSuccess) { Notify(EnumPromptType.Success, $"写入成功", $"成功:写入{tag}:True"); } else { Notify(EnumPromptType.Error, $"写入失败", $"失败:写入{tag}:True\n{result.Message}"); } }), new Action(() => { var result = PlcControl.GetInstance.Write(addr, false); if (result != null && result.IsSuccess) { Notify(EnumPromptType.Success, $"写入成功", $"成功:写入{tag}:False"); } else { Notify(EnumPromptType.Error, $"写入失败", $"失败:写入{tag}:False\n{result.Message}"); } })); } else { Notify(EnumPromptType.Error, "失败", "设备未连接,不可进行控制。"); } } } private void WriteBooleanAddr(string tagName,bool value) { BoolAddEnum addr = BoolAddEnum.反应釜旋转电机; try { addr = (BoolAddEnum)Enum.Parse(typeof(BoolAddEnum), tagName); } catch (Exception) { Notify(EnumPromptType.Error, "失败", $"未找到该地址[{tagName}]。"); return; } var result = PlcControl.GetInstance.Write(addr, value); if (result != null && result.IsSuccess) { Notify(EnumPromptType.Success, $"写入成功", $"成功:写入{tagName}:{value}"); } else { Notify(EnumPromptType.Error, $"写入失败", $"失败:写入{tagName}:{value}\n{result.Message}"); } } private void Button_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) { open_Click(sender,e); } private void Button_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e) { close_Click(sender,e); } } }