终端一体化运控平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

201 lines
7.2 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using Microsoft.Toolkit.Mvvm.ComponentModel;
  8. using Microsoft.Toolkit.Mvvm.Input;
  9. using BPASmart.Model;
  10. using System.Diagnostics;
  11. using Microsoft.EntityFrameworkCore;
  12. using BPASmartClient.Helper;
  13. using System.Text.Json.Serialization;
  14. using Microsoft.EntityFrameworkCore.Metadata.Internal;
  15. using Ubiety.Dns.Core.Records;
  16. using System.Windows;
  17. namespace BPASmart.VariableManager.ViewModels
  18. {
  19. public class VariableConfigViewModel : NoticeBase
  20. {
  21. private int varialeInfosIndex = -1;
  22. string DeviceName = string.Empty;
  23. public VariableConfigViewModel(string o)
  24. {
  25. DeviceName = o;
  26. varialeInfosIndex = Array.FindIndex(Json<CommunicationPar>.Data.CommunicationDevices.ToArray(), p => p.DeviceName == o);
  27. if (varialeInfosIndex >= 0 && varialeInfosIndex < Json<CommunicationPar>.Data.CommunicationDevices.Count)
  28. {
  29. varialeInfos = Json<CommunicationPar>.Data.CommunicationDevices.ElementAt(varialeInfosIndex).VarTableModels;
  30. if (varialeInfos.Count <= 0) AddRow();
  31. }
  32. ComboBoxItemInit();
  33. ActionManage.GetInstance.Register(new Action<object[]>((p) =>
  34. {
  35. if (p != null && p.Length == 2)
  36. {
  37. NameWidth = Convert.ToDouble(p[0]);
  38. AddressWidth = Convert.ToDouble(p[1]);
  39. }
  40. }), "TabGridSizeChanged");
  41. DelegationNotifi.GetInstance.VarNameChanged = new Action<int>((p) =>
  42. {
  43. var res = varialeInfos.FirstOrDefault(w => w.ID == p);
  44. var temp = varialeInfos.Where(s => s.VarName == res?.VarName)?.ToList();
  45. if (temp != null && temp.Count > 1)
  46. {
  47. temp.ForEach(item =>
  48. {
  49. int index = Array.FindIndex(varialeInfos.ToArray(), t => t.VarName == item.VarName && t.ID == item.ID);
  50. if (index >= 0 && index < varialeInfos.Count)
  51. {
  52. varialeInfos.ElementAt(index).IsRedundant = true;
  53. }
  54. });
  55. }
  56. else
  57. {
  58. for (int i = 0; i < varialeInfos.Count; i++)
  59. {
  60. varialeInfos.ElementAt(i).IsRedundant = false;
  61. }
  62. }
  63. if ((p - 1) >= 0)
  64. {
  65. if (varialeInfos?.Count >= p)
  66. {
  67. if (varialeInfos.ElementAt(p - 1).VarName != null)
  68. {
  69. if (varialeInfos.ElementAt(p - 1).VarName.Length > 0)
  70. {
  71. if (varialeInfos.Count == p) AddRow();
  72. }
  73. }
  74. }
  75. }
  76. });
  77. StartMotionCommand = new RelayCommand(() =>
  78. {
  79. switch (ButtonContext)
  80. {
  81. case "开始监控":
  82. TabName = "当前值";
  83. CurrentVisibility = Visibility.Visible;
  84. RemoveButVisiblity = Visibility.Collapsed;
  85. ButtonContext = "停止监控";
  86. break;
  87. case "停止监控":
  88. TabName = "操作";
  89. CurrentVisibility = Visibility.Collapsed;
  90. RemoveButVisiblity = Visibility.Visible;
  91. ButtonContext = "开始监控";
  92. break;
  93. default:
  94. break;
  95. }
  96. });
  97. RemoveCommand = new RelayCommand<object>((o) =>
  98. {
  99. if (o != null && o is VariableInfo variable)
  100. {
  101. varialeInfos.Remove(variable);
  102. }
  103. for (int i = 0; i < varialeInfos.Count; i++)
  104. {
  105. varialeInfos.ElementAt(i).ID = i + 1;
  106. }
  107. });
  108. }
  109. /// <summary>
  110. /// 添加行
  111. /// </summary>
  112. private void AddRow()
  113. {
  114. App.Current.Dispatcher.Invoke(new Action(() =>
  115. {
  116. if (varialeInfosIndex >= 0 && varialeInfosIndex < Json<CommunicationPar>.Data.CommunicationDevices.Count)
  117. {
  118. varialeInfos.Add(new VariableInfo(DeviceName) { ID = varialeInfos.Count + 1 });
  119. }
  120. }));
  121. }
  122. #region 属性
  123. public double NameWidth { get { return _mNameWidth; } set { _mNameWidth = value; OnPropertyChanged(); } }
  124. public static double _mNameWidth;
  125. public double AddressWidth { get { return _mAddressWidth; } set { _mAddressWidth = value; OnPropertyChanged(); } }
  126. private static double _mAddressWidth;
  127. public string TabName { get { return _mTabName; } set { _mTabName = value; OnPropertyChanged(); } }
  128. private static string _mTabName = "操作";
  129. public string ButtonContext { get { return _mButtonContext; } set { _mButtonContext = value; OnPropertyChanged(); } }
  130. private static string _mButtonContext = "开始监控";
  131. public Visibility CurrentVisibility { get { return _mCurrentVisibility; } set { _mCurrentVisibility = value; OnPropertyChanged(); } }
  132. private static Visibility _mCurrentVisibility = Visibility.Collapsed;
  133. public Visibility RemoveButVisiblity { get { return _mRemoveButVisiblity; } set { _mRemoveButVisiblity = value; OnPropertyChanged(); } }
  134. private static Visibility _mRemoveButVisiblity = Visibility.Visible;
  135. #endregion
  136. #region 数据列表
  137. /// <summary>
  138. /// 数据类型下拉列表
  139. /// </summary>
  140. public ObservableCollection<string> dataType { get; set; } = new ObservableCollection<string>();
  141. /// <summary>
  142. /// 报警设置窗离散量报警类型
  143. /// </summary>
  144. public ObservableCollection<string> PopupDiscreteAlarmType { get; set; } = new ObservableCollection<string>();
  145. /// <summary>
  146. /// 变量信息
  147. /// </summary>
  148. public ObservableCollection<VariableInfo> varialeInfos { get; set; }
  149. #endregion
  150. #region Command
  151. public RelayCommand SaveCommand { get; set; }
  152. public RelayCommand StartMotionCommand { get; set; }
  153. public RelayCommand<object> RemoveCommand { get; set; }
  154. #endregion
  155. /// <summary>
  156. /// 下拉列表初始化
  157. /// </summary>
  158. private void ComboBoxItemInit()
  159. {
  160. dataType.Clear();
  161. PopupDiscreteAlarmType.Clear();
  162. string[] DataTypeNames = Enum.GetNames(typeof(EDataType));
  163. foreach (var item in DataTypeNames) { dataType.Add(item); }
  164. string[] PopupAlarmTypes = Enum.GetNames(typeof(EAlongTriggerType));
  165. foreach (var item in PopupAlarmTypes) { PopupDiscreteAlarmType.Add(item); }
  166. }
  167. }
  168. }