using Aitex.Core.Common.DeviceData; using Aitex.Core.RT.IOCore; using Aitex.Core.UI.Control; using Aitex.Core.UI.ControlDataContext; using Aitex.Core.UI.DeviceControl; using Aitex.Core.Util; using Caliburn.Micro.Core; using DocumentFormat.OpenXml.Drawing.Charts; using DocumentFormat.OpenXml.Office2019.Excel.RichData; using DocumentFormat.OpenXml.Wordprocessing; using FurnaceUI.Models; using MECF.Framework.Common; using MECF.Framework.Common.DataCenter; using MECF.Framework.Common.Equipment; using MECF.Framework.Common.OperationCenter; using MECF.Framework.UI.Client.CenterViews.Editors.Recipe; using MECF.Framework.UI.Core.Control; using MECF.Framework.UI.Core.DxfScript; using MECF.Framework.UI.Core.DxfScript.Converter; using MECF.Framework.UI.Core.ExtendedControls; using OpenSEMI.ClientBase; using SciChart.Core.AttachedProperties; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Xml.Linq; using static MECF.Framework.UI.Core.DxfScript.Express; using static System.Windows.Forms.VisualStyles.VisualStyleElement.Window; namespace FurnaceUI.Views.Editors { public class RecipeGasXmlViewModel : FurnaceUIViewModelBase, IUserFunctions, IDisposable { private string _PM = $"{ModuleName.PM1.ToString()}."; private Dictionary ConvertNameDict = new Dictionary() { }; public List ChangeMfcValue = new List(); public Window WinOwner { get; set; } private string _selectedStepName; public string SelectedStepName { get { return _selectedStepName; } set { _selectedStepName = value; NotifyOfPropertyChange("SelectedStepName"); } } private Step _step; public Step SelectedStep { get => _step; set { _step = value; NotifyOfPropertyChange("SelectedStep"); } } public delegate void ChangedStepHandler(Step step); public event ChangedStepHandler ChangedStepEvent; public RecipeDataBase CurrentRecipe { get; set; } private GasPanelStateType _selectedGasStateType = GasPanelStateType.Recipe; public GasPanelStateType SelectedGasStateType { get { return _selectedGasStateType; } set { _selectedGasStateType = value; NotifyOfPropertyChange(nameof(SelectedGasStateType)); } } private bool _isUsePREVBtn = false; public bool IsUsePREVBtn { get => _isUsePREVBtn; set { _isUsePREVBtn = value; NotifyOfPropertyChange("IsUsePREVBtn"); } } private bool _isUseNEXTBtn = false; public bool IsUseNEXTBtn { get => _isUseNEXTBtn; set { _isUseNEXTBtn = value; NotifyOfPropertyChange("IsUseNEXTBtn"); } } public RecipeGasXmlViewModel() { UserFunctionsEvents.ClearAll(); UserFunctionsEvents.RegisterEvent>("SwichValue", SwichValue); UserFunctionsEvents.RegisterEvent>("MfcFlowTouchUp", MfcFlowTouchUp); UserFunctionsEvents.RegisterEvent>("ValveTouchUp", ValveTouchUp); } protected override void OnViewLoaded(object view) { base.OnViewLoaded(view); GetIoValveBtn(); SetValveVirtualFeedbackValue(); SwitchKey(false); SetPREVAndNEXTBtn(); } /// /// DevicePM中有些IoValve命名存在问题,没有表明是btn类型,故需要转化下。 /// private void GetIoValveBtn() { ConvertNameDict.Clear(); var valueSetsName = SelectedStep.ValveSets.Where(a => a.Name.StartsWith("ValveAV")).Select(a => $"{_PM}{a.Name}Btn").ToList(); var valuveDatas = QueryDataClient.Instance.Service.PollData(valueSetsName); foreach (var item in valuveDatas) { var valveItem = SelectedStep.ValveSets.Where(a => $"{_PM}{a.Name}Btn" == item.Key).FirstOrDefault().Name; ConvertNameDict.Add(valveItem, item.Value.ToString()); } } protected override void InvokeAfterUpdateProperty(Dictionary data) { base.InvokeAfterUpdateProperty(data); GasMapProvider.GasMap.ModifyDrag(); } /// /// 切换Key /// /// public void SwitchKey(bool isMonitor = true) { ScriptVariables.IsFetchRT = isMonitor; foreach (var item in GasMapProvider.GasMap.Valves) { var realKey = $"{ModuleName.PM1.ToString()}.{item.Name}.Feedback"; item.Key = isMonitor ? realKey : $"{realKey.Replace(_PM, "")}"; } foreach (var item in GasMapProvider.GasMap.Buttons) { bool isExextue = item.BoolCondition != null; var realKey = $"{ModuleName.PM1}.{item.InnerText.Text}Enable"; if (isExextue) { item.BoolCondition.Execute(); realKey = item.BoolCondition.ReadBoolValue().Item1; } item.DataKey = isMonitor ? realKey : $"{realKey.Replace(_PM, "")}"; } foreach (var item in GasMapProvider.GasMap.Analogs) { item.Brush = GasMapProvider.InitMFCColor; if (ChangeMfcValue.Contains(item.Name) && !isMonitor) { item.Brush = GasMapProvider.ChangeMFCColor; } var realKey = $"{ModuleName.PM1}.{item.Name}.Feedback"; item.ValueKey = isMonitor ? realKey : $"{realKey.Replace(_PM, "")}"; } foreach (var item in GasMapProvider.GasMap.Circles) { bool isExextue = item.ClickCondition == null && item.BoolCondition != null && item.StringCondition == null; if (isExextue) { item.BoolCondition.Execute(); item.Key = item.BoolCondition.ReadBoolValue().Item1; continue; } } } /// /// 初始化虚拟值 /// public void SetValveVirtualFeedbackValue() { var str = $"{ModuleName.PM1.ToString()}."; ChangeMfcValue.Clear(); Dictionary btnNameKeyValues = new Dictionary(); foreach (var item in SelectedStep.ValveSets) { var itemName = item.Name; if (ConvertNameDict.ContainsKey(item.Name)) { itemName = ConvertNameDict[item.Name]; } if (itemName.StartsWith("ValveAV")) { var temporaryKey = $"{itemName}.Feedback"; var realKey = $"{ModuleName.PM1}.{temporaryKey}"; if (!btnNameKeyValues.Keys.Contains(temporaryKey)) { btnNameKeyValues.Add(temporaryKey, item.Value); } } else { var temporaryKey = $"{itemName}Enable"; var realKey = $"{ModuleName.PM1}.{temporaryKey}"; if (!btnNameKeyValues.Keys.Contains(temporaryKey)) { btnNameKeyValues.Add(temporaryKey, item.Value); } } } foreach (var item in SelectedStep.MFCSets) { var temporaryKey = $"{item.ControlName}.Feedback"; var realKey = $"{ModuleName.PM1}.{temporaryKey}"; var recipeValue = double.Parse(item.SetValue.Value); if (recipeValue > 0) { ChangeMfcValue.Add(item.ControlName); } btnNameKeyValues.Add(temporaryKey, recipeValue); temporaryKey = $"{item.ControlName}.Scal"; realKey = $"{ModuleName.PM1}.{temporaryKey}"; if (ScriptVariables.ContainsVariable(realKey)) { var newvalue = ScriptVariables.GetDoubleByName(realKey); if (btnNameKeyValues.ContainsKey(realKey)) { btnNameKeyValues[realKey] = newvalue; } else { btnNameKeyValues.Add(realKey, newvalue); } } temporaryKey = $"{item.ControlName}.Unit"; realKey = $"{ModuleName.PM1}.{temporaryKey}"; btnNameKeyValues.Add(temporaryKey, item.Unit); if (ScriptVariables.ContainsVariable(realKey)) { var newvalue = ScriptVariables.GetStringByName(realKey); if (btnNameKeyValues.ContainsKey(realKey)) { btnNameKeyValues[realKey] = newvalue; } else { btnNameKeyValues.Add(realKey, newvalue); } } } ScriptVariables.InitializeDefaultSubscriptions(btnNameKeyValues); } /// /// btn点击事件 /// /// public void SwichValue(Dictionary parameter) { if (SelectedGasStateType == GasPanelStateType.Monitor) return; var setCmd = (string)parameter["name"]; var showType = (ShowType)parameter["showType"]; var shape = (GasButton)parameter["shape"]; ScriptVariables.IsFetchRT = false; bool setValue = false; var status = ScriptVariables.GetBoolByName(shape.DataKey); setValue = !status; ScriptVariables.SetBoolByName(shape.DataKey, setValue); setCmd = ConvertNameDict.ContainsValue(setCmd) ? ConvertNameDict.FirstOrDefault(x => x.Value == setCmd).Key : setCmd; SelectedStep.ValveSets.FirstOrDefault(x => x.Name == setCmd).SetValue(setValue); if (!string.IsNullOrEmpty(setCmd)) { SetValveFollow(setCmd, setValue); } } /// /// 阀点击事件 /// /// public void ValveTouchUp(Dictionary parameter) { var shape = (GasAITValve)parameter["shape"]; var name = shape.Name; if (SelectedGasStateType == GasPanelStateType.Monitor) return; var status = ScriptVariables.GetBoolByName(shape.Key); var newStatus = !status; ScriptVariables.SetBoolByName(shape.Key, newStatus); InvokeClient.Instance.Service.DoOperation($"{_PM}{name}.{AITValveOperation.GVVirtualTurnValve}", newStatus); var obj = new object[] { name, AITValveOperation.GVVirtualTurnValve, newStatus }; SetValveCommand(obj); } [Subscription("PM1.MFC1.DeviceData")] public AITMfcData MFC1Data { get; set; } /// /// MFC点击事件 /// /// public void MfcFlowTouchUp(Dictionary parameter) { var shape = (GasAnalogControl4Jet)parameter["shape"]; var name = shape.Name; if (SelectedGasStateType == GasPanelStateType.Monitor || shape.IsMfm) return; var setPoint = ScriptVariables.GetVariableByName(shape.ValueKey).DoubleValue; var maxValue = ScriptVariables.GetVariableByName(shape.MaxValueKey).DoubleValue; var unit = ScriptVariables.GetVariableByName(shape.UnitKey).StringValue; InputDialogBox dialogBox = new InputDialogBox { TagName = "Number", CommandDelegate = Execute, DeviceName = shape.Name, SetPoint = setPoint, MaxValue = maxValue, Unit = unit, }; dialogBox.SetPoint = Math.Round(setPoint * 100.0, 1); dialogBox.Owner = WinOwner; dialogBox.Topmost = false; dialogBox.WindowStartupLocation = WindowStartupLocation.CenterScreen; if ((bool)dialogBox.ShowDialog()) { if (!string.IsNullOrEmpty(dialogBox.InputText)) { var newValue = double.Parse(dialogBox.InputText); ScriptVariables.SetDoubleByName(shape.ValueKey, newValue); shape.Brush = GasMapProvider.ChangeMFCColor; var obj = new object[] { $"{_PM}{shape.Name}", AITMfcOperation.GVMfcVirtualTurnValve, newValue }; PerformCmdSetMfcFlow(obj); //if (SelectedGasStateType == GasPanelStateType.Manual) //{ // obj = new object[] { $"{_PM}{shape.Name}", AITMfcOperation.GVMfcValve, newValue }; // PerformCmdSetMfcFlow(obj); //} } } dialogBox.Close(); dialogBox.Dispose(); } private void Execute(double value, double? ramp) { } /// /// 下一步/上一步 /// /// public void StepClick(string cmd) { bool isChangedStep = false; if (SelectedStep == null) { return; } switch (cmd) { case "PREV": SelectedStep = CurrentRecipe.Steps.Where(x => x.StepNo == SelectedStep.StepNo - 1).FirstOrDefault(); isChangedStep = true; break; case "NEXT": SelectedStep = CurrentRecipe.Steps.Where(x => x.StepNo == SelectedStep.StepNo + 1).FirstOrDefault(); isChangedStep = true; break; default: break; } if (isChangedStep && SelectedStep != null) { SelectedStepName = $"{SelectedStep.StepNo}:{SelectedStep.Name}"; SetChangedStep(SelectedStep); SetValveVirtualFeedbackValue(); } SetPREVAndNEXTBtn(); } private void SetPREVAndNEXTBtn() { if (SelectedStep != null && CurrentRecipe != null) { if (CurrentRecipe.Steps.Count == 0) { IsUsePREVBtn = false; IsUseNEXTBtn = false; } else { if (SelectedStep.StepNo > CurrentRecipe.Steps[0].StepNo) { IsUsePREVBtn = true; } else { IsUsePREVBtn = false; } if (SelectedStep.StepNo < CurrentRecipe.Steps[CurrentRecipe.Steps.Count - 1].StepNo) { IsUseNEXTBtn = true; } else { IsUseNEXTBtn = false; } } } } public void SetChangedStep(Step step) { if (ChangedStepEvent != null) { ChangedStepEvent(step); } } public void SelectedGasStateTypeCmd(string cmd) { switch (cmd) { case "Manual": SelectedGasStateType = GasPanelStateType.Manual; break; case "Monitor": SelectedGasStateType = GasPanelStateType.Monitor; break; case "Recipe": SelectedGasStateType = GasPanelStateType.Recipe; break; default: break; } SwitchKey(SelectedGasStateType == GasPanelStateType.Monitor); } public void SetValveCommand(object param) { object[] args = (object[])param; if (args.Length == 3) if (SelectedStep != null && SelectedStep.ValveSets.Count > 0 && SelectedStep.ValveSets.FirstOrDefault(x => x.Name == (string)args[0]) != null) { SelectedStep.ValveSets.FirstOrDefault(x => x.Name == (string)args[0]).SetValue((bool)args[2]); //InvokeClient.Instance.Service.DoOperation($"{args[0]}.{args[1]}", args[2]); // SetValveFollow((string)args[0], (bool)args[2]); } //SetCursorPos(0, 0); } private void PerformCmdSetMfcFlow(object param) { object[] args = (object[])param; //0:devicename, 1:operation, 2:args if (args.Length == 3) { InvokeClient.Instance.Service.DoOperation($"{args[0]}.{args[1]}", args[2]); if (SelectedStep != null) { var mfc = SelectedStep.MFCSets.FirstOrDefault(x => x.ControlName == ((string)args[0]).Replace("PM1.", "")); if (mfc != null) { mfc.SetValue.SetValue(args[2].ToString()); } } } // SetCursorPos(0, 0); } private void PerformCmdSetMfmFlow(object param) { object[] args = (object[])param; //0:devicename, 1:operation, 2:args if (args.Length == 3) { InvokeClient.Instance.Service.DoOperation($"PM1.{args[0]}.{args[1]}", args[2]); } // SetCursorPos(0, 0); } public void TempSetSave() { foreach (var mfcitem in SelectedStep.MFCSets) { var replaceStr = mfcitem.ControlName; var property = this.GetType().GetProperties().Where(x => x.Name == replaceStr).FirstOrDefault(); if (property == null) continue; object value = property.GetValue(this, null); if (value != null) { mfcitem.SetValue.Value = ((AnalogDeviceDataItem)value).VirtualFeedBack.ToString(); } } ((Window)GetView()).DialogResult = true; } public void TempSetCancel() { ((Window)GetView()).DialogResult = false; } private bool disposed = false; public void Dispose() { Dispose(true); GC.SuppressFinalize(this); // 防止垃圾回收器再次调用析构函数 } protected virtual void Dispose(bool disposing) { if (!disposed) { if (disposing) { this.OnDeactivate(true); // 释放托管资源 } // 释放非托管资源 disposed = true; } } private void SwichValueBtn(string name, GasButton btn) { var dialog = new SwitchValueDialog { }; var status = ScriptVariables.GetBoolByName(btn.DataKey); dialog.IsOpen = status; dialog.Owner = WinOwner; dialog.Topmost = true; dialog.WindowStartupLocation = WindowStartupLocation.CenterScreen; dialog.DeviceName = $"Swich {name}"; dialog.ShowDialog(); if ((bool)dialog.IsSave) { string setCmd = name.Replace("Enable", ""); ScriptVariables.SetBoolByName(btn.DataKey, dialog.IsOpen); SelectedStep.ValveSets.FirstOrDefault(x => x.Name == setCmd).Value = dialog.IsOpen; if (!string.IsNullOrEmpty(setCmd)) { SetValveFollow(setCmd, dialog.IsOpen); } } } private void SetHeaterBtn(string name, GasButton btn) { ScriptVariables.IsFetchRT = false; string setCmd = ""; bool setValue = false; var status = ScriptVariables.GetBoolByName(btn.DataKey); setCmd = name.Replace("Enable", ""); setValue = !status; ScriptVariables.SetBoolByName(btn.DataKey, setValue); SelectedStep.ValveSets.FirstOrDefault(x => x.Name == setCmd).SetValue(setValue); if (!string.IsNullOrEmpty(setCmd)) { SetValveFollow(setCmd, setValue); } } private void SelectPumpBtn(string name, GasButton btn) { ScriptVariables.IsFetchRT = false; var isConfirm = DialogBox.Confirm($"Are you sure change {name} status?"); if (isConfirm) { string setCmd = ""; bool setValue = false; //if (name == "DP1Enable") //{ // name = "DPEnable"; // btn.DataKey = name; //} var status = ScriptVariables.GetBoolByName(btn.DataKey); setCmd = name.Replace("Enable", ""); setValue = !status; ScriptVariables.SetBoolByName(btn.DataKey, setValue); SelectedStep.ValveSets.FirstOrDefault(x => x.Name.ToUpper() == setCmd.ToUpper()).SetValue(setValue); if (!string.IsNullOrEmpty(setCmd)) { SetValveFollow(setCmd, setValue); } } } private void SetValveFollow(string controlName, bool value) { int firstIndex = CurrentRecipe.Steps.IndexOf(SelectedStep); if (firstIndex < CurrentRecipe.Steps.Count) { for (int i = firstIndex + 1; i < CurrentRecipe.Steps.Count; i++) { if (!CurrentRecipe.Steps[i].ValveIsSaved) { break; } else { CurrentRecipe.Steps[i].ValveSets.FirstOrDefault(x => x.Name == controlName).Value = value; } } } } } }