AlarmShowView.xaml.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. using NEIntelligentControl2.Models;
  2. using NEIntelligentControl2.Models.Alarm;
  3. using NEIntelligentControl2.Models.Messages;
  4. using NEIntelligentControl2.Service.Voice;
  5. using NEIntelligentControl2.Service.WebSocket;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Collections.ObjectModel;
  9. using System.ComponentModel;
  10. using System.Linq;
  11. using System.Runtime.CompilerServices;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. using System.Windows;
  15. using System.Windows.Controls;
  16. using System.Windows.Data;
  17. using System.Windows.Documents;
  18. using System.Windows.Input;
  19. using System.Windows.Media;
  20. using System.Windows.Media.Imaging;
  21. using System.Windows.Navigation;
  22. using System.Windows.Shapes;
  23. namespace NEIntelligentControl2.Views.Alarm
  24. {
  25. /// <summary>
  26. /// 报警弹窗
  27. /// </summary>
  28. public partial class AlarmShowView : UserControl
  29. {
  30. // ----------依赖属性-----------
  31. public static readonly DependencyProperty IsFixedProperty = DependencyProperty.Register("IsFixed", typeof(bool), typeof(AlarmShowView));
  32. /// <summary>
  33. /// 是否固定
  34. /// </summary>
  35. public bool IsFixed { get => (bool)GetValue(IsFixedProperty); set => SetValue(IsFixedProperty, value); }
  36. /// <summary>
  37. /// 报警数据
  38. /// </summary>
  39. public ObservableCollection<AlarmCardInfo> Alarms { get; set; }
  40. private FaultPopupBridge _FaultPopupBridge;
  41. private MessageBridge _MessageBridge;
  42. private VoiceManager _VoiceManager;
  43. private bool _IsFirst = true;
  44. private Dictionary<long, AlarmCardInfo> _ActivedFaults;
  45. private AlarmType _AlarmType; // 要显示的报警类型
  46. private ListBox _LBMain;
  47. public AlarmShowView()
  48. {
  49. InitializeComponent();
  50. Alarms = new ObservableCollection<AlarmCardInfo>();
  51. _FaultPopupBridge = new FaultPopupBridge() { Messaged = OnAlarmMessage };
  52. _MessageBridge = App.ServiceProvider.GetService(typeof(MessageBridge)) as MessageBridge;
  53. _VoiceManager = App.ServiceProvider.GetService(typeof(VoiceManager)) as VoiceManager;
  54. _ActivedFaults = new Dictionary<long, AlarmCardInfo>();
  55. try
  56. {
  57. _AlarmType = (AlarmType)Enum.Parse(typeof(AlarmType), "AlarmType".GetConfiguration(), true);
  58. }
  59. catch { }
  60. _MessageBridge.Register(_FaultPopupBridge);
  61. }
  62. private void OnAlarmMessage(List<FaultInfo> obj)
  63. {
  64. if (obj == null || obj.Count == 0) return;
  65. obj = FiletAlarms(obj);
  66. // 新的报警列表
  67. var lsnew = obj.Select(fi => fi.SnapID).ToList();
  68. // 已经解除的报警
  69. var unopenedls = Alarms.Where(a => !lsnew.Contains(a.Info.SnapID)).ToList();
  70. Dispatcher.Invoke(() =>
  71. {
  72. AddAlarm(obj);
  73. RemoveAlarm(unopenedls);
  74. if (_IsFirst && obj.Count > 0)
  75. {
  76. IsFixed = true;
  77. _VoiceManager.Add($"报警数量:{obj.Count}");
  78. }
  79. _IsFirst = false;
  80. });
  81. }
  82. /// <summary>
  83. /// 移除报警
  84. /// </summary>
  85. private void RemoveAlarm(List<AlarmCardInfo> unopenedls)
  86. {
  87. if (unopenedls == null || unopenedls.Count == 0) return;
  88. DateTime dt = DateTime.Now;
  89. foreach (var v in unopenedls)
  90. {
  91. // 解除超过10分钟的报警移除
  92. if (!v.IsOpened && (dt - v.UpdateTime).Minutes >= 10)
  93. {
  94. Alarms.Remove(v);
  95. _ActivedFaults.Remove(v.Info.SnapID);
  96. }
  97. else if (v.IsOpened)
  98. {
  99. v.IsOpened = false;
  100. v.UpdateTime = DateTime.Now;
  101. }
  102. }
  103. }
  104. /// <summary>
  105. /// 添加报警
  106. /// </summary>
  107. private void AddAlarm(List<FaultInfo> obj)
  108. {
  109. foreach (var v in obj)
  110. {
  111. if (_ActivedFaults.ContainsKey(v.SnapID))
  112. {
  113. var af = _ActivedFaults[v.SnapID];
  114. // 已经解除重新触发的报警
  115. if (!af.IsOpened)
  116. {
  117. af.IsOpened = true;
  118. af.UpdateTime = DateTime.Now;
  119. }
  120. continue;
  121. }
  122. var ac = new AlarmCardInfo(v);
  123. Alarms.Insert(0, ac);
  124. if (!this.IsMouseOver)
  125. {
  126. _LBMain?.ScrollIntoView(_LBMain.Items[0]);
  127. }
  128. _ActivedFaults.Add(v.SnapID, ac);
  129. if (!_IsFirst)
  130. {
  131. IsFixed = true;
  132. _VoiceManager.Add(ac.Message);
  133. }
  134. }
  135. }
  136. private List<FaultInfo> FiletAlarms(List<FaultInfo> obj)
  137. {
  138. if (_IsFirst)
  139. {
  140. obj.Sort((x, y) =>
  141. {
  142. var v = x.LastUpdateTime?.CompareTo(y.LastUpdateTime.Value);
  143. if (v == null) return 0;
  144. return v.Value;
  145. });
  146. }
  147. if (_AlarmType == AlarmType.WindTurbine)
  148. {
  149. return obj.Where(fi => fi.Category1 != "SYZ").ToList();
  150. }
  151. else if (_AlarmType == AlarmType.BoosterStation)
  152. {
  153. return obj.Where(fi => fi.Category1 == "SYZ").ToList();
  154. }
  155. return obj;
  156. }
  157. private void cc_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  158. {
  159. IsFixed = !IsFixed;
  160. }
  161. private void Button_Click(object sender, RoutedEventArgs e)
  162. {
  163. var v = ((Control)sender).DataContext as AlarmCardInfo;
  164. if (v == null||v.Info==null) return;
  165. if (!v.IsOpened)
  166. {
  167. Alarms.Remove(v);
  168. _ActivedFaults.Remove(v.Info.SnapID);
  169. return;
  170. }
  171. var b = Windows.Alarm.DeviceDetailAlarmWindow.Show(v.Info, true);
  172. if (b)
  173. {
  174. Alarms.Remove(v);
  175. _ActivedFaults.Remove(v.Info.SnapID);
  176. }
  177. }
  178. private void UserControl_Loaded(object sender, RoutedEventArgs e)
  179. {
  180. var cp = FindVisualChild<ContentPresenter>(this);
  181. if (cp == null) return;
  182. _LBMain = this.ContentTemplate.FindName("_LBMain", cp) as ListBox;
  183. }
  184. private childItem FindVisualChild<childItem>(DependencyObject obj) where childItem : DependencyObject
  185. {
  186. for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
  187. {
  188. DependencyObject child = VisualTreeHelper.GetChild(obj, i);
  189. if (child != null && child is childItem)
  190. return (childItem)child;
  191. else
  192. {
  193. childItem childOfChild = FindVisualChild<childItem>(child);
  194. if (childOfChild != null)
  195. return childOfChild;
  196. }
  197. }
  198. return null;
  199. }
  200. }
  201. public class AlarmCardInfo : INotifyPropertyChanged
  202. {
  203. /// <summary>
  204. /// 标题
  205. /// </summary>
  206. public string Title { get; set; }
  207. /// <summary>
  208. /// 时间
  209. /// </summary>
  210. public string Time { get; set; }
  211. private string message;
  212. /// <summary>
  213. /// 信息
  214. /// </summary>
  215. public string Message { get => IsOpened ? message : $"{message}--已解除"; set { message = value; OnPropertyChanged(); } }
  216. private bool isOpened;
  217. /// <summary>
  218. /// 是否未解除
  219. /// </summary>
  220. public bool IsOpened { get => isOpened; set { isOpened = value; OnPropertyChanged(); } }
  221. /// <summary>
  222. /// 信息更新时间
  223. /// </summary>
  224. public DateTime UpdateTime { get; set; }
  225. public FaultInfo Info { get; set; }
  226. public event PropertyChangedEventHandler PropertyChanged;
  227. protected void OnPropertyChanged([CallerMemberName] string name = null)
  228. {
  229. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
  230. }
  231. public AlarmCardInfo(FaultInfo info)
  232. {
  233. Info = info;
  234. if (info == null) return;
  235. Title = info.ObjectName;
  236. Time = info.LastUpdateTime == null ? info.FaultTime?.ToString("yyyy-MM-dd HH:mm:ss") : info.LastUpdateTime?.ToString("yyyy-MM-dd HH:mm:ss");
  237. Message = info.AlertText;
  238. IsOpened = info.IsOpened;
  239. UpdateTime = DateTime.Now;
  240. }
  241. }
  242. /// <summary>
  243. /// 报警类型
  244. /// </summary>
  245. public enum AlarmType
  246. {
  247. /// <summary>
  248. /// 所有类型
  249. /// </summary>
  250. All,
  251. /// <summary>
  252. /// 升压站
  253. /// </summary>
  254. BoosterStation,
  255. /// <summary>
  256. /// 风机
  257. /// </summary>
  258. WindTurbine
  259. }
  260. }