using Caliburn.Micro; using Caliburn.Micro.Core; using MECF.Framework.Common.OperationCenter; using MECF.Framework.UI.Client.CenterViews.Configs.SystemConfig; using MECF.Framework.UI.Client.CenterViews.Dialogs; using OpenSEMI.ClientBase; using System; using System.Collections.Generic; 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 FurnaceUI.Models; using FurnaceUI.Views.Parameter; using MECF.Framework.Common.DataCenter; namespace FurnaceUI.Views.Editors { public class RecipePressureAlarmWatchTableSettingViewModel : FurnaceUIViewModelBase { public string SelectAlarmWatchTable { get; set; } = "None"; public bool IsSave { get; set; } = false; private ConfigNode levelOneNode; public ConfigNode LevelOneNode { get { return levelOneNode; } set { levelOneNode = value; this.NotifyOfPropertyChange(nameof(LevelOneNode)); } } private ConfigNode levelTwoNode; public ConfigNode LevelTwoNode { get { return levelTwoNode; } set { levelTwoNode = value; this.NotifyOfPropertyChange(nameof(LevelTwoNode)); } } private List currenItems; public List CurrenItems { get { return currenItems; } set { currenItems = value; this.NotifyOfPropertyChange(nameof(CurrenItems)); } } private string _CurrentNodeName = string.Empty; private ConfigNode _rootNode; public bool IsEnable => CGlobal.RecipeProcessEditViewEnable;//是否是View模式 private bool _isAlarmNoneChecked; public bool IsAlarmNoneChecked { get => _isAlarmNoneChecked; set { _isAlarmNoneChecked = value; NotifyOfPropertyChange(nameof(IsAlarmNoneChecked)); } } public Visibility isForbid; public Visibility IsForbid { get { return isForbid; } set { isForbid = value; this.NotifyOfPropertyChange(nameof(IsForbid)); } } public bool isAvailabled; public bool IsAvailabled { get { return isAvailabled; } set { isAvailabled = value; this.NotifyOfPropertyChange(nameof(IsAvailabled)); } } private string _pressureUnit; public string PressureUnit { get => _pressureUnit; set { _pressureUnit = value; NotifyOfPropertyChange(nameof(PressureUnit)); } } protected override void OnViewLoaded(object view) { base.OnViewLoaded(view); IsAlarmNoneChecked = SelectAlarmWatchTable == "None"; _rootNode = SystemConfigProvider.Instance.GetConfig(true); LevelOneNode = new ConfigNode(); LevelOneNode = FindNodeByName(_rootNode, "PM1.RecipeEditParameter.AlarmWatchTable.PressureAlarmWatch"); LevelTwoNode = LevelOneNode.SubNodes.FirstOrDefault(); IsForbid = IsAlarmNoneChecked ? Visibility.Hidden : Visibility.Visible; if (SelectAlarmWatchTable != "None") { LevelOneNode.SubNodes[int.Parse(SelectAlarmWatchTable) - 1].AlarmWatchBoolValue = true; } InitItemsCurrentValue(LevelTwoNode, true); PressureUnit = (string)QueryDataClient.Instance.Service.GetConfig($"PM1.APC.PressureUnit"); } //public void SetAlarmWatchTable(string nameCmd) //{ // int iTableIndex = 0; // if (nameCmd != "None") // { // iTableIndex = int.Parse(nameCmd); // } // var windowManager = IoC.Get(); // RecipeAlarmActionViewModel recipeAlarmActionViewModel = new RecipeAlarmActionViewModel() { TableIndex = iTableIndex }; // (windowManager as WindowManager)?.ShowDialogWithTitle(recipeAlarmActionViewModel, null, "Alarm Action"); // SelectAlarmWatchTable = nameCmd; //} private void InitItemsCurrentValue(ConfigNode node, bool initSubItems = true) { if (node == null) return; CurrenItems = node.Items; _CurrentNodeName = string.IsNullOrEmpty(node.Path) ? node.Name : $"{node.Path}.{"Table" + SelectAlarmWatchTable}"; if (CurrenItems == null || CurrenItems.Count <= 0) { if (!initSubItems) return; foreach (var item in node.SubNodes) { InitItemsCurrentValue(item); } } else { GetDataOfConfigItems(); } } private void GetDataOfConfigItems() { if (CurrenItems == null) return; for (int i = 0; i < CurrenItems.Count; i++) { string key = String.Format("{0}{1}{2}", _CurrentNodeName, ".", CurrenItems[i].Name); CurrenItems[i].CurrentValue = SystemConfigProvider.Instance.GetValueByName(key); CurrenItems[i].Path = key; CurrenItems[i].StringValue = CurrenItems[i].CurrentValue; } } public void MenuCommand(object obj, object menuLevel) { RadioButton radioButton = obj as RadioButton; ConfigNode currentNode = null; SelectAlarmWatchTable = radioButton.Content.ToString(); IsForbid = Visibility.Visible; IsAvailabled = true; //IsAlarmNoneChecked = false; if (radioButton.Content.ToString() == "None") { IsForbid = Visibility.Hidden; IsAvailabled = false; return; } switch (menuLevel.ToString()) { case "LevelOne": currentNode = LevelTwoNode = LevelOneNode.SubNodes.Find((x) => x.Name == radioButton.ToolTip.ToString()); break; } InitItemsCurrentValue(currentNode); } public void SetValue(object obj) { if (CurrenItems == null || currenItems.Count == 0) return; ConfigItem item = null; if (obj is Control) item = CurrenItems.Find((x) => x.Name == (obj as Control).ToolTip.ToString()); else item = obj as ConfigItem; 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; string value; 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; } value = item.StringValue; string key = String.Format("{0}{1}{2}", _CurrentNodeName, ".", item.Name); InvokeClient.Instance.Service.DoOperation("System.SetConfig", item.Path, value); } } 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 void SaveCmd() { IsSave = true; ((Window)GetView()).DialogResult = true; } public void CloseCmd() { IsSave = false; ((Window)GetView()).DialogResult = false; } } 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(); } } }