ModuleAlarmViewModel.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. using System;
  2. using Aitex.Core.RT.Event;
  3. using Aitex.Core.UI.MVVM;
  4. using MECF.Framework.Common.CommonData;
  5. using MECF.Framework.Common.DataCenter;
  6. using MECF.Framework.Common.Event;
  7. using MECF.Framework.UI.Client.ClientBase;
  8. using System.Collections.Generic;
  9. using System.Collections.ObjectModel;
  10. using System.Linq;
  11. using System.Runtime.InteropServices;
  12. using System.Windows.Input;
  13. using MECF.Framework.Common.OperationCenter;
  14. namespace MECF.Framework.UI.Client.CenterViews.Alarms.ModuleAlarm
  15. {
  16. public class ModuleAlarmViewModel : UiViewModelBase
  17. {
  18. public class UIModuleItem : NotifiableItem
  19. {
  20. public string ModuleName { get; set; }
  21. public bool IsSelected { get; set; }
  22. }
  23. public class UIAlarmItem : NotifiableItem
  24. {
  25. public string OccuringTime { get; set; }
  26. public string Description { get; set; }
  27. public string Type { get; set; }
  28. public int EventId { get; set; }
  29. public string EventEnum { get; set; }
  30. public string Explaination { get; set; }
  31. public string Solution { get; set; }
  32. public string Source { get; set; }
  33. public bool IsEqualTo(UIAlarmItem item)
  34. {
  35. return item.OccuringTime == OccuringTime &&
  36. item.EventEnum == EventEnum &&
  37. item.Description == Description &&
  38. item.Source == Source &&
  39. item.Type == Type;
  40. }
  41. }
  42. public ObservableCollection<UIAlarmItem> FilteredAlarms { get; set; }
  43. public ObservableCollection<UIModuleItem> AllModules { get; set; }
  44. public ObservableCollection<UIModuleItem> SelectedModules { get; set; }
  45. public ICommand SelectionChanged { get; set; }
  46. public ICommand SelectAllChanged { get; set; }
  47. public bool IsAllSelected { get; set; }
  48. public ModuleAlarmViewModel()
  49. {
  50. Subscribe("System.ActiveAlarm");
  51. SelectionChanged = new DelegateCommand<object>(DoSelectionChanged);
  52. SelectAllChanged = new DelegateCommand<object>(DoSelectAllChanged);
  53. AllModules = new ObservableCollection<UIModuleItem>();
  54. FilteredAlarms = new ObservableCollection<UIAlarmItem>();
  55. }
  56. protected override void OnInitialize()
  57. {
  58. base.OnInitialize();
  59. var data = QueryDataClient.Instance.Service.GetData("System.Modules");
  60. if (data != null)
  61. {
  62. foreach (var module in (List<string>)data)
  63. {
  64. AllModules.Add(new UIModuleItem()
  65. {
  66. IsSelected = true,
  67. ModuleName = module,
  68. });
  69. }
  70. }
  71. InvokePropertyChanged(nameof(AllModules));
  72. IsAllSelected = true;
  73. SelectedModules = new ObservableCollection<UIModuleItem>(AllModules);
  74. }
  75. private void DoSelectAllChanged(object obj)
  76. {
  77. if (IsAllSelected)
  78. {
  79. var modules = new ObservableCollection<UIModuleItem>(AllModules);
  80. foreach (var uiModuleItem in modules)
  81. {
  82. uiModuleItem.IsSelected = true;
  83. }
  84. SelectedModules = modules;
  85. NotifyOfPropertyChange(nameof(SelectedModules));
  86. }
  87. else
  88. {
  89. SelectedModules.Clear();
  90. }
  91. }
  92. private void DoSelectionChanged(object obj)
  93. {
  94. bool allSelected = true;
  95. foreach (var uiModuleItem in AllModules)
  96. {
  97. if (!uiModuleItem.IsSelected && uiModuleItem.ModuleName != "All")
  98. {
  99. allSelected = false;
  100. break;
  101. }
  102. }
  103. IsAllSelected = allSelected;
  104. NotifyOfPropertyChange(nameof(IsAllSelected));
  105. }
  106. protected override void OnActivate()
  107. {
  108. base.OnActivate();
  109. }
  110. public void ResetAlarm(UIAlarmItem item)
  111. {
  112. InvokeClient.Instance.Service.DoOperation("System.ResetAlarm", item.Source, item.EventEnum);
  113. }
  114. protected override void InvokeAfterUpdateProperty(Dictionary<string, object> data)
  115. {
  116. if (data.ContainsKey("System.ActiveAlarm"))
  117. UpdateAlarmEvent((List<AlarmEventItem>)data["System.ActiveAlarm"]);
  118. }
  119. public void UpdateAlarmEvent(List<AlarmEventItem> evItems)
  120. {
  121. List<UIAlarmItem> removeList = new List<UIAlarmItem>();
  122. foreach (var filteredItem in FilteredAlarms)
  123. {
  124. if (SelectedModules.FirstOrDefault(x => x.ModuleName == filteredItem.Source) == null && !IsAllSelected)
  125. {
  126. removeList.Add(filteredItem);
  127. continue;
  128. }
  129. if (!evItems.Exists(x => x.Source == filteredItem.Source && x.EventEnum == filteredItem.EventEnum))
  130. {
  131. removeList.Add(filteredItem);
  132. continue;
  133. }
  134. }
  135. foreach (var uiAlarmItem in removeList)
  136. {
  137. FilteredAlarms.Remove(uiAlarmItem);
  138. }
  139. foreach (AlarmEventItem item in evItems)
  140. {
  141. var newItem = new UIAlarmItem()
  142. {
  143. Type = item.Level == EventLevel.Alarm ? "Alarm" : (item.Level == EventLevel.Information ? "Info" : "Warning"),
  144. OccuringTime = item.OccuringTime.ToString("yyyy-MM-dd HH:mm:ss"),
  145. Description = item.Description,
  146. EventEnum = item.EventEnum,
  147. EventId = item.Id,
  148. Explaination = item.Explaination,
  149. Solution = item.Solution,
  150. Source = item.Source,
  151. };
  152. if (FilteredAlarms.FirstOrDefault(x => x.IsEqualTo(newItem)) == null &&
  153. (SelectedModules.FirstOrDefault(x => x.ModuleName == newItem.Source) != null || IsAllSelected))
  154. {
  155. FilteredAlarms.Add(newItem);
  156. }
  157. }
  158. }
  159. }
  160. }