using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Text; using System.Threading.Tasks; using Microsoft.Toolkit.Mvvm.ComponentModel; using Microsoft.Toolkit.Mvvm.Input; using BPASmart.Model; using System.Diagnostics; using Microsoft.EntityFrameworkCore; using BPASmartClient.Helper; using System.Text.Json.Serialization; using Microsoft.EntityFrameworkCore.Metadata.Internal; using Ubiety.Dns.Core.Records; using System.Windows; namespace BPASmart.VariableManager.ViewModels { public class VariableConfigViewModel : NoticeBase { private int varialeInfosIndex = -1; string DeviceName = string.Empty; public VariableConfigViewModel(string o) { DeviceName = o; varialeInfosIndex = Array.FindIndex(Json.Data.CommunicationDevices.ToArray(), p => p.DeviceName == o); if (varialeInfosIndex >= 0 && varialeInfosIndex < Json.Data.CommunicationDevices.Count) { varialeInfos = Json.Data.CommunicationDevices.ElementAt(varialeInfosIndex).VarTableModels; if (varialeInfos.Count <= 0) AddRow(); } ComboBoxItemInit(); ActionManage.GetInstance.Register(new Action((p) => { if (p != null && p.Length == 2) { NameWidth = Convert.ToDouble(p[0]); AddressWidth = Convert.ToDouble(p[1]); } }), "TabGridSizeChanged"); DelegationNotifi.GetInstance.VarNameChanged = new Action((p) => { var res = varialeInfos.FirstOrDefault(w => w.ID == p); var temp = varialeInfos.Where(s => s.VarName == res?.VarName)?.ToList(); if (temp != null && temp.Count > 1) { temp.ForEach(item => { int index = Array.FindIndex(varialeInfos.ToArray(), t => t.VarName == item.VarName && t.ID == item.ID); if (index >= 0 && index < varialeInfos.Count) { varialeInfos.ElementAt(index).IsRedundant = true; } }); } else { for (int i = 0; i < varialeInfos.Count; i++) { varialeInfos.ElementAt(i).IsRedundant = false; } } if ((p - 1) >= 0) { if (varialeInfos?.Count >= p) { if (varialeInfos.ElementAt(p - 1).VarName != null) { if (varialeInfos.ElementAt(p - 1).VarName.Length > 0) { if (varialeInfos.Count == p) AddRow(); } } } } }); StartMotionCommand = new RelayCommand(() => { switch (ButtonContext) { case "开始监控": TabName = "当前值"; CurrentVisibility = Visibility.Visible; RemoveButVisiblity = Visibility.Collapsed; ButtonContext = "停止监控"; break; case "停止监控": TabName = "操作"; CurrentVisibility = Visibility.Collapsed; RemoveButVisiblity = Visibility.Visible; ButtonContext = "开始监控"; break; default: break; } }); RemoveCommand = new RelayCommand((o) => { if (o != null && o is VariableInfo variable) { varialeInfos.Remove(variable); } for (int i = 0; i < varialeInfos.Count; i++) { varialeInfos.ElementAt(i).ID = i + 1; } }); } /// /// 添加行 /// private void AddRow() { App.Current.Dispatcher.Invoke(new Action(() => { if (varialeInfosIndex >= 0 && varialeInfosIndex < Json.Data.CommunicationDevices.Count) { varialeInfos.Add(new VariableInfo(DeviceName) { ID = varialeInfos.Count + 1 }); } })); } #region 属性 public double NameWidth { get { return _mNameWidth; } set { _mNameWidth = value; OnPropertyChanged(); } } public static double _mNameWidth; public double AddressWidth { get { return _mAddressWidth; } set { _mAddressWidth = value; OnPropertyChanged(); } } private static double _mAddressWidth; public string TabName { get { return _mTabName; } set { _mTabName = value; OnPropertyChanged(); } } private static string _mTabName = "操作"; public string ButtonContext { get { return _mButtonContext; } set { _mButtonContext = value; OnPropertyChanged(); } } private static string _mButtonContext = "开始监控"; public Visibility CurrentVisibility { get { return _mCurrentVisibility; } set { _mCurrentVisibility = value; OnPropertyChanged(); } } private static Visibility _mCurrentVisibility = Visibility.Collapsed; public Visibility RemoveButVisiblity { get { return _mRemoveButVisiblity; } set { _mRemoveButVisiblity = value; OnPropertyChanged(); } } private static Visibility _mRemoveButVisiblity = Visibility.Visible; #endregion #region 数据列表 /// /// 数据类型下拉列表 /// public ObservableCollection dataType { get; set; } = new ObservableCollection(); /// /// 报警设置窗离散量报警类型 /// public ObservableCollection PopupDiscreteAlarmType { get; set; } = new ObservableCollection(); /// /// 变量信息 /// public ObservableCollection varialeInfos { get; set; } #endregion #region Command public RelayCommand SaveCommand { get; set; } public RelayCommand StartMotionCommand { get; set; } public RelayCommand RemoveCommand { get; set; } #endregion /// /// 下拉列表初始化 /// private void ComboBoxItemInit() { dataType.Clear(); PopupDiscreteAlarmType.Clear(); string[] DataTypeNames = Enum.GetNames(typeof(EDataType)); foreach (var item in DataTypeNames) { dataType.Add(item); } string[] PopupAlarmTypes = Enum.GetNames(typeof(EAlongTriggerType)); foreach (var item in PopupAlarmTypes) { PopupDiscreteAlarmType.Add(item); } } } }