using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Windows; using Aitex.Core.RT.IOCore; using Caliburn.Micro; using Caliburn.Micro.Core; using MECF.Framework.Common.DataCenter; using MECF.Framework.Common.IOCore; using MECF.Framework.Common.OperationCenter; using MECF.Framework.UI.Client.CenterViews.Dialogs; using MECF.Framework.UI.Client.CenterViews.Editors.Recipe; using MECF.Framework.UI.Client.CenterViews.Editors.Sequence; using MECF.Framework.UI.Client.ClientBase; using OpenSEMI.ClientBase; using OpenSEMI.ClientBase.Command; using OpenSEMI.ClientBase.IO; namespace MECF.Framework.UI.Client.CenterViews.Configs.SystemConfig { public class SystemConfigViewModel : UiViewModelBase, ISupportMultipleSystem { #region Properties public bool IsPermission { get => this.Permission == 3; } private SystemConfigView view1; private Visibility _isShowShutDown = Visibility.Hidden; public Visibility IsShowShutDown { get => _isShowShutDown; set { _isShowShutDown = value; NotifyOfPropertyChange(nameof(IsShowShutDown)); } } private Visibility _isShowTable = Visibility.Hidden; public Visibility IsShowTable { get => _isShowTable; set { _isShowTable = value; NotifyOfPropertyChange(nameof(IsShowTable)); } } private Visibility _isListData = Visibility.Hidden; public Visibility IsListData { get => _isListData; set { _isListData = value; NotifyOfPropertyChange(nameof(IsListData)); } } private Visibility _isShowData = Visibility.Hidden; public Visibility IsShowData { get => _isShowData; set { _isShowData = value; NotifyOfPropertyChange(nameof(IsShowData)); } } private Visibility _isShowAUX = Visibility.Hidden; public Visibility IsShowAUX { get => _isShowAUX; set { _isShowAUX = value; NotifyOfPropertyChange(nameof(IsShowAUX)); } } private List _ConfigNodes = new List(); public List ConfigNodes { get { return _ConfigNodes; } set { _ConfigNodes = value; NotifyOfPropertyChange("ConfigNodes"); } } private ObservableCollection _configItems = new ObservableCollection(); public ObservableCollection ConfigItems { get { return _configItems; } set { _configItems = value; NotifyOfPropertyChange("ConfigItems"); } } private ObservableCollection _configAUXItems = new ObservableCollection(); public ObservableCollection ConfigAUXItems { get { return _configAUXItems; } set { _configAUXItems = value; NotifyOfPropertyChange("ConfigAUXItems"); } } string _CurrentNodeName = string.Empty; public BaseCommand TreeViewSelectedItemChangedCmd { private set; get; } private ObservableCollection valueList { get; set; } = new ObservableCollection(); public ObservableCollection NameList { get; set; } = new ObservableCollection(); public ObservableCollection AOs { get; private set; } private string _currentCriteria = String.Empty; public string CurrentCriteria { get { return _currentCriteria; } set { if (value == _currentCriteria) return; _currentCriteria = value; NotifyOfPropertyChange("CurrentCriteria"); ApplyFilter(); } } public string SystemName { get; set; } private void ApplyFilter() { foreach (var node in ConfigNodes) node.ApplyCriteria(CurrentCriteria, new Stack()); } #endregion #region Functions public SystemConfigViewModel() { this.DisplayName = "System Config"; TreeViewSelectedItemChangedCmd = new BaseCommand(TreeViewSelectedItemChanged); } protected override void OnInitialize() { base.OnInitialize(); ConfigNodes = SystemConfigProvider.Instance.GetConfigTree(SystemName).SubNodes; } protected override void OnActivate() { base.OnActivate(); ConfigNodes = SystemConfigProvider.Instance.GetConfigTree(SystemName).SubNodes; } protected override void OnViewLoaded(object view) { base.OnViewLoaded(view); view1 = (SystemConfigView)view; } private void SetShowView(string name) { switch (name) { case "IsShowTable": IsShowTable = Visibility.Visible; IsShowData = Visibility.Hidden; IsListData = Visibility.Hidden; IsShowAUX = Visibility.Hidden; IsShowShutDown = Visibility.Hidden; break; case "IsShowData": IsShowTable = Visibility.Hidden; IsShowData = Visibility.Visible; IsListData = Visibility.Hidden; IsShowAUX = Visibility.Hidden; IsShowShutDown = Visibility.Hidden; if (view1 != null) { view1.ShowAUX.ItemsSource = null; view1.ShowData.ItemsSource = ConfigItems; } break; case "IsListData": IsShowTable = Visibility.Hidden; IsShowData = Visibility.Hidden; IsListData = Visibility.Visible; IsShowAUX = Visibility.Hidden; IsShowShutDown = Visibility.Hidden; break; case "IsShowAUX": IsShowTable = Visibility.Hidden; IsShowData = Visibility.Hidden; IsListData = Visibility.Hidden; IsShowAUX = Visibility.Visible; IsShowShutDown = Visibility.Hidden; if (view1 != null) { view1.ShowData.ItemsSource = null; view1.ShowAUX.ItemsSource = ConfigItems; } break; case "IsShowShutDown": IsShowShutDown = Visibility.Visible; IsShowTable = Visibility.Hidden; IsShowData = Visibility.Hidden; IsListData = Visibility.Hidden; IsShowAUX = Visibility.Hidden; break; default: break; } } private void TreeViewSelectedItemChanged(ConfigNode node) { valueList.Clear(); _CurrentNodeName = string.IsNullOrEmpty(node.Path) ? node.Name : $"{node.Path}.{node.Name}"; ConfigItems.Clear(); node.Items.ForEach(x => ConfigItems.Add(x)); if (_CurrentNodeName.EndsWith("ShutDown")) { SetShowView("IsShowShutDown"); } else if (_CurrentNodeName.Contains("WaferCycleTime")) { SetShowView("IsShowTable"); } else if (_CurrentNodeName.Contains("Step Name")) { int count = int.Parse(ConfigItems.FirstOrDefault(x => x.Name == "Count").DefaultValue); string names = ConfigItems.FirstOrDefault(x => x.Name == "Name").StringValue; NameList.Clear(); int index = 1; for (int i = 0; i < count; i++) { NameList.Add(new ListName() { No = index, Name = "" }); index++; } if (!string.IsNullOrEmpty(names)) { } SetShowView("IsListData"); } else if (_CurrentNodeName.Contains("AUX")) { //if (ConfigAUXItems.Count() == 0) //{ node.Items.ForEach(x => ConfigAUXItems.Add(x)); for (int i = 0; i < ConfigAUXItems.Count; i++) { ConfigAUXItems[i].CurrentValueParam.Value = ConfigAUXItems[i].CurrentValue; ConfigAUXItems[i].Unit = (i + 1).ToString(); } //} GetDataOfConfigAUXItems(); SetShowView("IsShowAUX"); return; } else { SetShowView("IsShowData"); } GetDataOfConfigItems(); } private void GetDataOfConfigItems() { if (ConfigItems == null || ConfigItems.Count == 0) return; for (int i = 0; i < ConfigItems.Count; i++) { string key = String.Format("{0}{1}{2}", _CurrentNodeName, ".", ConfigItems[i].Name); ConfigItems[i].CurrentValue = SystemConfigProvider.Instance.GetValueByName(SystemName, key); if (ConfigItems[i].Type == DataType.Bool) { bool value; if (bool.TryParse(ConfigItems[i].CurrentValue, out value)) { ConfigItems[i].BoolValue = value; ConfigItems[i].CurrentValue = value ? "Yes" : "No"; } } else ConfigItems[i].StringValue = ConfigItems[i].CurrentValue; } } private void GetDataOfConfigAUXItems() { if (ConfigAUXItems == null || ConfigAUXItems.Count == 0) return; for (int i = 0; i < ConfigAUXItems.Count; i++) { string key = String.Format("{0}{1}{2}", _CurrentNodeName, ".", ConfigAUXItems[i].Name); ConfigAUXItems[i].CurrentValueParam.Value = SystemConfigProvider.Instance.GetValueByName(SystemName, key); } } public void SetValue(ConfigItem item) { InputDialogViewModel dialog = new InputDialogViewModel(); RecipeSelectDialogViewModel selectDialog = new RecipeSelectDialogViewModel(); WindowManager wm = new WindowManager(); bool? bret = null; if (item.Name == "Idle Recipe" || item.Name == "Abort Recipe" || item.Name == "Reset Recipe") { selectDialog.DisplayName = "Select Recipe"; var recipeProvider = new RecipeProvider(); string selectRecipeType = ""; if (item.Name == "Idle Recipe") selectRecipeType = "Idle"; else if (item.Name == "Abort Recipe") selectRecipeType = "Abort"; else selectRecipeType = "Reset"; var processType = QueryDataClient.Instance.Service.GetConfig($"System.Recipe.{selectRecipeType}"); if (processType == null) { processType = selectRecipeType; } var ProcessTypeFileList = new ObservableCollection(); string[] recipeProcessType = ((string)processType).Split(','); for (int i = 0; i < recipeProcessType.Length; i++) { var type = new ProcessTypeFileItem(); type.ProcessType = recipeProcessType[i]; var prefix = $"Furnace\\{recipeProcessType[i]}"; var recipes = recipeProvider.GetXmlRecipeList(prefix); type.FileListByProcessType = RecipeSequenceTreeBuilder.BuildFileNode(prefix, "", false, recipes)[0].Files; ProcessTypeFileList.Add(type); } selectDialog.ProcessTypeFileList = ProcessTypeFileList; bret = wm.ShowDialog(selectDialog); } else { if (item.Type == DataType.String) dialog.KeyboardType = "FullKeyboard"; dialog.Item = item; dialog.DisplayName = "Set Value"; bret = wm.ShowDialog(dialog); } string strOldValue = item.CurrentValue; if ((bool)bret) { item.StringValue = dialog.DialogResult; string value = string.Empty; switch (item.Type) { case DataType.Int: int iValue; if (int.TryParse(item.StringValue, out iValue)) { if (!double.IsNaN(item.Max) && !double.IsNaN(item.Min)) { if (iValue > item.Max || iValue < item.Min) { DialogBox.ShowWarning(string.Format("The value should be between {0} and {1}.", ((int)item.Min).ToString(), ((int)item.Max).ToString())); return; } } } else { DialogBox.ShowWarning("Please input valid data."); return; } value = item.StringValue; break; case DataType.Double: double fValue; if (double.TryParse(item.StringValue, out fValue)) { if (!double.IsNaN(item.Max) && !double.IsNaN(item.Min)) { if (fValue > item.Max || fValue < item.Min) { DialogBox.ShowWarning(string.Format("The value should be between {0} and {1}.", item.Min.ToString(), item.Max.ToString())); return; } string[] box = fValue.ToString().Split('.'); if (box.Length > 1 && box[1].Length > 3) { DialogBox.ShowWarning(string.Format("The value should be more than three decimal places")); return; } } } else { DialogBox.ShowWarning("Please input valid data."); return; } value = item.StringValue; break; case DataType.Bool: if (item.StringValue.Equals("Yes", StringComparison.CurrentCultureIgnoreCase)) item.BoolValue = true; else if (item.StringValue.Equals("No", StringComparison.CurrentCultureIgnoreCase)) item.BoolValue = false; else { DialogBox.ShowWarning("The Value Should be Yes Or No."); return; } value = item.BoolValue.ToString().ToLower(); break; case DataType.String: if (item.StringValue != null) value = item.StringValue; else value = selectDialog.DialogResult; break; } if (_CurrentNodeName == "System.ShutDown" && item.StringValue == "ShutDown") InvokeClient.Instance.Service.DoOperation("System.ShutDown"); //压力单位改变时候,已有设置根据改变进行值转换 if (item.Name == "PressureUnit") ChangePressureUnit(value); string key = String.Format("{0}{1}{2}", _CurrentNodeName, ".", item.Name); valueList.Add(new PageValue() { Path = key, CurrentValue = value }); InvokeClient.Instance.Service.DoOperation($"{SystemName}.SetConfig", key, value); if (item.Name == "Idle Recipe" || item.Name == "Abort Recipe" || item.Name == "Reset Recipe") { GetDataOfConfigItems(); } } } private void ChangePressureUnit(string strNewValue) { //double dValue = 0; //if (strNewValue.Equals("Torr"))//从Pa转换成Torr // dValue = 0.0075006; //else //从Torr转换成Pa // dValue = 133.322; //for (int i = 1; i <= 5; i++) //{ // string key = $"PM1.RecipeEditParameter.PressureStabilizeTable.Table{i}.MaxScale"; // double newValue = dValue * double.Parse(SystemConfigProvider.Instance.GetValueByName(SystemName, key)); // InvokeClient.Instance.Service.DoOperation($"{SystemName}.SetConfig", key, newValue); //} InvokeClient.Instance.Service.DoOperation($"PM1.APC.SetUnit", strNewValue); } public void Reload() { GetDataOfConfigItems(); } public void SaveAll() { if (ConfigItems == null) return; ConfigItems.ToList().ForEach(item => SetValue(item)); } public void SaveParameter() { foreach (var item in valueList) { InvokeClient.Instance.Service.DoOperation($"{SystemName}.SetConfig", item.Path, item.CurrentValue); } Reload(); } public void Cancel() { valueList.Clear(); Reload(); } public void CollapseAll() { SetExpand(ConfigNodes, false); } public void ExpandAll() { SetExpand(ConfigNodes, true); } public void AllControl() { var windowManager = IoC.Get(); ShutDonwControlViewModel shutDonwControlViewModel = new ShutDonwControlViewModel(); shutDonwControlViewModel.AllControlVisibility = true; (windowManager as WindowManager)?.ShowDialogWithTitle(shutDonwControlViewModel, null, "All Control"); } public void UIControl() { var windowManager = IoC.Get(); ShutDonwControlViewModel shutDonwControlViewModel = new ShutDonwControlViewModel(); shutDonwControlViewModel.UIControlVisibility = true; (windowManager as WindowManager)?.ShowDialogWithTitle(shutDonwControlViewModel, null, "UI Control"); } public void UIAndRTControl() { var windowManager = IoC.Get(); ShutDonwControlViewModel shutDonwControlViewModel = new ShutDonwControlViewModel(); shutDonwControlViewModel.UIAndRTControlVisibility = true; (windowManager as WindowManager)?.ShowDialogWithTitle(shutDonwControlViewModel, null, "UI And RT Control"); } public void ClearFilter() { CurrentCriteria = ""; } public void SetExpand(List configs, bool expand) { if (configs == null) return; foreach (var configNode in configs) { configNode.IsExpanded = expand; if (configNode.SubNodes != null) SetExpand(configNode.SubNodes, expand); } } #endregion protected override void InvokeAfterUpdateProperty(Dictionary data) { base.InvokeAfterUpdateProperty(data); string _aoKey = $"{SystemName}.PM1.AOList"; if (data.ContainsKey(_aoKey)&&data[_aoKey] != null && _isReadPLC) { List lstData = (List)data[_aoKey]; for (int i = 0; i < lstData.Count; i++) { var findConfigAUX = ConfigAUXItems.FirstOrDefault(x => $"PM1.{x.Name}" == lstData[i].Name); if (findConfigAUX != null) { if (findConfigAUX.CurrentValueParam.Value != lstData[i].FloatValue.ToString()) { findConfigAUX.CurrentValueParam.SetValue(lstData[i].FloatValue.ToString()); } } } _isReadPLC = false; } } public void AUXSaveClick() { SaveValueToSC(); if (DialogBox.Confirm("Save SC Value To PLC?")) { SaveValueToPLC(true); } } public void AUXUploadClick() { if (DialogBox.Confirm("Upload save PLC data to SC?")) { GetPLCToSC(); } } public void AUXDownloadClick() { if (DialogBox.Confirm("Save SC data and download to PLC?")) { SaveValueToSC(); SaveValueToPLC(); } } private void SaveValueToSC() { foreach (var item in ConfigAUXItems) { InvokeClient.Instance.Service.DoOperation($"{SystemName}.SetConfig", $"{item.Path}.{item.Name}", item.CurrentValueParam.Value); } } private void SaveValueToPLC(bool isSaveAll = false) { if (isSaveAll) { foreach (var item in ConfigAUXItems) { float tempValue = 0; float.TryParse(item.CurrentValueParam.Value, out tempValue); InvokeClient.Instance.Service.DoOperation("System.SetAoValueFloat", $"PM1.{item.Name}", tempValue); item.CurrentValueParam.IsSaved = true; } } else { foreach (var item in ConfigAUXItems) { if (!item.CurrentValueParam.IsSaved) { float tempValue = 0; float.TryParse(item.CurrentValueParam.Value, out tempValue); InvokeClient.Instance.Service.DoOperation("System.SetAoValueFloat", $"PM1.{item.Name}", tempValue); item.CurrentValueParam.IsSaved = true; } } } } private static bool _isReadPLC = false; private void GetPLCToSC() { _isReadPLC = true; string _aoKey = $"{SystemName}.PM1.AOItemList"; this.AOs = InitIOData(IOType.AO, _aoKey); _aoKey = $"{SystemName}.PM1.AOList"; Subscribe(_aoKey); } public ObservableCollection InitIOData(IOType type, string dataName) { //get the whole informations ObservableCollection da = new ObservableCollection(); if (type == IOType.AO) { var diList = QueryDataClient.Instance.Service.GetData(dataName); if (diList != null) { List item = (List)diList; for (int i = 0; i < item.Count; i++) { { da.Add(new AOItemFloat() { Index = da.Count, Name = item[i].Name, //DisplayName = item[i].Name.Substring(item[i].Name.IndexOf('.') + 1), DisplayName = item[i].Description, Value = item[i].FloatValue, Address = item[i].Address }); } } } } return da; } } public class ListName : PropertyChangedBase { private int _no; public int No { get => _no; set { _no = value; NotifyOfPropertyChange(nameof(No)); } } private string _name; public string Name { get => _name; set { _name = value; NotifyOfPropertyChange(nameof(Name)); } } } public class PageValue { public string Path { get; set; } public double Min { get; set; } public double Max { get; set; } public DataType Type { get; set; } public string CurrentValue { get; set; } public List Parameters { get; set; } } }