using Aitex.Core.RT.SCCore; using Caliburn.Micro; using Caliburn.Micro.Core; using MECF.Framework.Common.DataCenter; using MECF.Framework.Common.OperationCenter; using MECF.Framework.UI.Client.CenterViews.Configs.SystemConfig; using MECF.Framework.UI.Client.CenterViews.Dialogs; using MECF.Framework.UI.Client.CenterViews.Editors; using MECF.Framework.UI.Client.CenterViews.Editors.Recipe; using MECF.Framework.UI.Client.ClientBase; using OpenSEMI.ClientBase; using SciChart.Core.Extensions; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Globalization; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Input; using System.Windows.Threading; namespace MECF.Framework.UI.Client.CenterViews.Parameter { public class MainParameterEditViewModel : ModuleUiViewModelBase, ISupportMultipleSystem { public bool IsPermission { get => this.Permission == 3; } public List ParameterTree { get; set; } = new List(); public ObservableCollection HeadLst { get; set; } = new ObservableCollection(); public ObservableCollection PIDHeadLst { get; set; } = new ObservableCollection(); public ObservableCollection StepNames { get; set; } = new ObservableCollection(); private ConfigNode _currentNode; public ConfigNode CurrentNode { get { return _currentNode; } set { _currentNode = value; this.NotifyOfPropertyChange(nameof(CurrentNode)); } } private string _allZoneValue = "0"; public string AllZoneValue { get { return _allZoneValue; } set { _allZoneValue = value; this.NotifyOfPropertyChange(nameof(AllZoneValue)); } } private ConfigNode _currentSubNode; public ConfigNode CurrentSubNode { get { return _currentSubNode; } set { _currentSubNode = value; this.NotifyOfPropertyChange(nameof(CurrentSubNode)); } } private ConfigNode _currentTwoSubNode; public ConfigNode CurrentTwoSubNode { get { return _currentTwoSubNode; } set { _currentTwoSubNode = value; this.NotifyOfPropertyChange(nameof(CurrentTwoSubNode)); } } private List _ConfigNodes = new List(); public List ConfigNodes { get { return _ConfigNodes; } set { _ConfigNodes = value; NotifyOfPropertyChange("ConfigNodes"); } } private string _defaultUnit = "Pa"; public string DefaultUnit { get { return _defaultUnit; } set { _defaultUnit = value; NotifyOfPropertyChange("DefaultUnit"); } } private ConfigNode _rootNode; private string _CurrentNodeName = string.Empty; private string path; private string currentValue; private ObservableCollection valueList { get; set; } = new ObservableCollection(); private ObservableCollection newValueList { get; set; } = new ObservableCollection(); protected override void OnInitialize() { base.OnInitialize(); this.SystemName = "System"; ConfigNodes = SystemConfigProvider.Instance.GetConfigTree(SystemName).SubNodes; _rootNode = SystemConfigProvider.Instance.GetConfig(true); InitTree(); } protected override void OnActivate() { base.OnActivate(); } private void InitTree() { var node = SystemConfigProvider.Instance.GetConfigTree(SystemName).SubNodes; ParameterTree.Add(new ParamBase() { ID = 1, ParentID = 0, Name = "StepName", ConfigName = "StepName" }); ParameterTree.Add(new ParamBase() { ID = 2, ParentID = 0, Name = "StepGroup", ConfigName = "StepGroup" }); //ParameterTree.Add(new ParamBase() { ID = 2, ParentID = 0, Name = "TempSetting", ConfigName = "TempSetting" }); //ParameterTree.Add(new ParamBase() { ID = 3, ParentID = 0, Name = "TempSetting", ConfigName = "TempSetting" }); ParameterTree = this.LoadTreeView(0); foreach (var item in ParameterTree) { InitAllParameterTree(item); } var stepNameNode = FindNodeByName(_rootNode, $"{strHeader}.StepName"); if (stepNameNode != null && stepNameNode.Items.Count > 0) { stepNameNode.Items.ForEach(x => StepNames.Add(x)); CurrentNode = stepNameNode; } this.GetHeaderConfig(); } string strHeader = "PM1.RecipeEditParameter"; private void InitAllParameterTree(ParamBase para) { CurrentNode = new ConfigNode(); CurrentNode = FindNodeByName(_rootNode, $"{strHeader}.{para.ConfigName}"); InitItemsCurrentValue(CurrentNode, true); if (CurrentNode.SubNodes != null) { foreach (var item in CurrentNode.SubNodes) { InitItemsCurrentValue(item, true); } } } private Visibility _stepNameVisibility = Visibility.Visible; public Visibility StepNameVisibility { get => _stepNameVisibility; set { _stepNameVisibility = value; NotifyOfPropertyChange(nameof(StepNameVisibility)); } } private Visibility _stepGroupSettingVisibility = Visibility.Hidden; public Visibility StepGroupSettingVisibility { get => _stepGroupSettingVisibility; set { _stepGroupSettingVisibility = value; NotifyOfPropertyChange(nameof(StepGroupSettingVisibility)); } } private Visibility _tempSettingeVisibility = Visibility.Hidden; public Visibility TempSettingVisibility { get => _tempSettingeVisibility; set { _tempSettingeVisibility = value; NotifyOfPropertyChange(nameof(TempSettingVisibility)); } } private List LoadTreeView(int id) { List node = ParameterTree.FindAll(s => s.ParentID.Equals(id)); foreach (var item in node) { item.Children = LoadTreeView(item.ID); } return node; } public void HideAllCanvas() { StepNameVisibility = Visibility.Hidden; TempSettingVisibility = Visibility.Hidden; StepGroupSettingVisibility = Visibility.Hidden; } public void TreeViewSelectedItemChanged(object obj) { valueList.Clear(); var view = GetView() as MainParameterEditView; HideAllCanvas(); ParamBase para = obj as ParamBase; string strHeader = "PM1.RecipeEditParameter"; switch (para.ConfigName) { case "StepName": StepNameVisibility = Visibility.Visible; break; case "StepGroup": CurrentNode = FindNodeByName(_rootNode, $"{strHeader}.{para.ConfigName}"); StepGroupSettingVisibility = Visibility.Visible; break; case "TempSetting": CurrentNode = FindNodeByName(_rootNode, $"{strHeader}.{para.ConfigName}"); TempSettingVisibility = Visibility.Visible; if (CurrentNode.SubNodes.Where(x => x.StabilizeBoolValue == true).Count() <= 0) { CurrentNode.SubNodes[0].StabilizeBoolValue = true; } CurrentSubNode = CurrentNode.SubNodes.FirstOrDefault(x => x.StabilizeBoolValue); break; } } public void StepGroupEdit(string groupName) { var windowManager = IoC.Get(); var groupList = CurrentNode.Items.Where(a => a.Display != groupName).Select(a => a.Path).ToList(); List hasSelectStepIdList = new List(); foreach (var item in QueryDataClient.Instance.Service.PollConfig(groupList).Values) { hasSelectStepIdList.AddRange( item.ToString().Split(',').ToList()); } StepGroupEditViewModel recipeStepNameViewModel = new StepGroupEditViewModel(); recipeStepNameViewModel.StepGroupName = groupName; recipeStepNameViewModel.HasSelectStepIds = hasSelectStepIdList; if ((bool)(windowManager as WindowManager)?.ShowDialogWithTitle(recipeStepNameViewModel, null, "Step Group Edit")) { var selectStepId = recipeStepNameViewModel.StepIdList.Where(a => a.IsExpanded).Select(a => a.Name).ToList(); InvokeClient.Instance.Service.DoOperation($"{SystemName}.SetConfig", $"PM1.RecipeEditParameter.StepGroup.{groupName}", string.Join(",", selectStepId.ToArray())); } } private void InitItemsCurrentValue(ConfigNode node, bool initSubItems = true) { if (node == null) return; _CurrentNodeName = string.IsNullOrEmpty(node.Path) ? node.Name : $"{node.Path}.{node.Name}"; if (node.Items == null || node.Items.Count <= 0) { if (!initSubItems) return; foreach (var item in node.SubNodes) { InitItemsCurrentValue(item); } } else { GetDataOfConfigItems(node); } } private void GetDataOfConfigItems(ConfigNode node) { if (node.Items == null) return; List allItems = new List(); for (int i = 0; i < node.Items.Count; i++) { string key = String.Format("{0}{1}{2}", _CurrentNodeName, ".", node.Items[i].Name); allItems.Add(key); } List newallItems = allItems.Distinct().ToList(); var DictItems = QueryDataClient.Instance.Service.PollConfig(newallItems); for (int i = 0; i < node.Items.Count; i++) { string key = String.Format("{0}{1}{2}", _CurrentNodeName, ".", node.Items[i].Name); node.Items[i].CurrentValue = DictItems.ContainsKey(key) ? DictItems[key].ToString() : ""; node.Items[i].Path = key; //if (CurrenItems[i].Path.Contains("PM1.RecipeEditParameter.PressureStabilizeTable")) // DefaultUnit = CurrenItems[i].Unit; if (node.Items[i].Type == DataType.Bool) { bool value; if (bool.TryParse(node.Items[i].CurrentValue, out value)) { node.Items[i].BoolValue = value; node.Items[i].CurrentValue = value ? "Yes" : "No"; } } else node.Items[i].StringValue = node.Items[i].CurrentValue; } } public void MenuCommand(object obj, object menuLevel) { RadioButton radioButton = obj as RadioButton; switch ((string)menuLevel) { case "LevelOne": CurrentSubNode = CurrentNode.SubNodes.Find((x) => x.Name == radioButton.ToolTip.ToString()); break; case "LevelTwo": CurrentTwoSubNode = CurrentSubNode.SubNodes.Find((x) => x.Name == radioButton.ToolTip.ToString()); break; default: break; } } public void SetValue(object obj) { if (CurrentNode.Items == null || CurrentNode.Items.Count == 0) return; ConfigItem item = null; if (obj is Control) item = CurrentNode.Items.ToList().Find((x) => x.Name == (obj as Control).ToolTip.ToString()); else item = obj as ConfigItem; if (item == null) return; if (item.Type == DataType.String) { currentValue = item.CurrentValue; path = item.Path; FullKeyboard fullKeyboard = new FullKeyboard("", currentValue); if ((bool)fullKeyboard.ShowDialog()) { InvokeClient.Instance.Service.DoOperation($"{SystemName}.SetConfig", path, fullKeyboard.ValueString); currentValue = fullKeyboard.ValueString; item.CurrentValue = fullKeyboard.ValueString; } valueList.Add(new PageValue() { Path = path, CurrentValue = currentValue }); } else { InputDialogViewModel dialog = new InputDialogViewModel(); dialog.Item = item; dialog.DisplayName = "Set Value"; WindowManager wm = new WindowManager(); bool? bret = wm.ShowDialog(dialog); if ((bool)bret) { item.StringValue = dialog.DialogResult; //key :System.IsSimulatorMode //value: true or false 都是字符串 //input check string value; if (item.Type == 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(); } else { if (item.Type == 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; } else if (item.Type == 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; } else value = item.StringValue; } string key = String.Format("{0}{1}{2}", _CurrentNodeName, ".", item.Name); path = item.Path; currentValue = value; valueList.Add(new PageValue() { Path = path, CurrentValue = currentValue }); } } } public void AllZoneTextChanged() { if (CurrentSubNode.Items == null || CurrentSubNode.Items.Count == 0) return; newValueList = new ObservableCollection(); valueList = new ObservableCollection(); foreach (var item in CurrentSubNode.Items) { item.CurrentValue = AllZoneValue; newValueList.Add(new PageValue() { Path = item.Path, CurrentValue = AllZoneValue }); } } public void SetCurrentSubNodeValue(object obj) { if (CurrentSubNode.Items == null || CurrentSubNode.Items.Count == 0) return; ConfigItem item = null; if (obj is Control) item = CurrentSubNode.Items.ToList().Find((x) => x.Name == (obj as Control).ToolTip.ToString()); else item = obj as ConfigItem; if (item == null) return; if (item.Type == DataType.String) { currentValue = item.CurrentValue; path = item.Path; FullKeyboard fullKeyboard = new FullKeyboard("", currentValue); if ((bool)fullKeyboard.ShowDialog()) { InvokeClient.Instance.Service.DoOperation($"{SystemName}.SetConfig", path, fullKeyboard.ValueString); currentValue = fullKeyboard.ValueString; item.CurrentValue = fullKeyboard.ValueString; } valueList.Add(new PageValue() { Path = path, CurrentValue = currentValue }); } else { InputDialogViewModel dialog = new InputDialogViewModel(); dialog.Item = item; dialog.DisplayName = "Set Value"; WindowManager wm = new WindowManager(); bool? bret = wm.ShowDialog(dialog); if ((bool)bret) { item.StringValue = dialog.DialogResult; //key :System.IsSimulatorMode //value: true or false 都是字符串 //input check string value; if (item.Type == 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(); } else { if (item.Type == 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; } else if (item.Type == 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; } else value = item.StringValue; } path = item.Path; currentValue = value; valueList.Add(new PageValue() { Path = path, CurrentValue = currentValue }); } } } public void SetStepNameValue(object obj, MouseButtonEventArgs e) { e.Handled = true; if (StepNames == null || StepNames.Count == 0) return; ConfigItem item = null; if (obj is Control) item = StepNames.ToList().Find((x) => x.Name == (obj as Control).ToolTip.ToString()); else item = obj as ConfigItem; if (item == null) return; if (item.Type == DataType.String) { currentValue = item.CurrentValue; path = item.Path; FullKeyboard fullKeyboard = new FullKeyboard("", currentValue); if ((bool)fullKeyboard.ShowDialog()) { InvokeClient.Instance.Service.DoOperation($"{SystemName}.SetConfig", path, fullKeyboard.ValueString); currentValue = fullKeyboard.ValueString; item.CurrentValue = fullKeyboard.ValueString; } valueList.Add(new PageValue() { Path = path, CurrentValue = currentValue }); } else { InputDialogViewModel dialog = new InputDialogViewModel(); dialog.Item = item; dialog.DisplayName = "Set Value"; WindowManager wm = new WindowManager(); bool? bret = wm.ShowDialog(dialog); if ((bool)bret) { item.StringValue = dialog.DialogResult; //key :System.IsSimulatorMode //value: true or false 都是字符串 //input check string value; value = item.StringValue; path = item.Path; currentValue = value; valueList.Add(new PageValue() { Path = path, CurrentValue = currentValue }); } } } public void SaveParameter() { if (newValueList != null && newValueList.Count > 0) { var one = newValueList[0]; if (one.Path.Contains("TempSetting")) { foreach (var item in valueList) { newValueList.Add(item); } valueList.Clear(); valueList = newValueList; } } foreach (var item in valueList) { InvokeClient.Instance.Service.DoOperation($"{SystemName}.SetConfig", item.Path, item.CurrentValue); } valueList = new ObservableCollection(); newValueList = new ObservableCollection(); } private string[] HeaterNameList = new string[5] { "HeaterU", "HeaterCU", "HeaterC", "HeaterCL", "HeaterL" }; public void GetHeaderConfig() { HeadLst.Clear(); for (int i = 0; i < 5; i++) { var configHeater = (string)QueryDataClient.Instance.Service.GetConfig($"PM1.Heater.{HeaterNameList[i]}.DisplayName"); TempSetData item = new TempSetData(configHeater); item.SetValue.Value = configHeater; HeadLst.Add(item); } } private ConfigNode FindNodeByName(ConfigNode parentNode, string strName) { string strCates = strName.Split('.')[0]; ConfigNode node = parentNode.SubNodes.Find((x) => x.Name == strCates); if (node == null) return parentNode; else return FindNodeByName(node, strName.Replace(strCates + ".", "")); } } public class ParamBase { public int ID { get; set; } public string Name { get; set; } public int ParentID { get; set; } public string ConfigName { get; set; } public List Children { get; set; } public ParamBase() { Children = new List(); } } public class ParameterConvert : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { return value.ToString().Replace("Table", "").Replace("LineNo", ""); } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } } }