WarningPopUp.xaml.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. namespace HistoryView.Controls
  2. {
  3. /// <summary>
  4. /// Interaction logic for WarningPopUp.xaml
  5. /// </summary>
  6. public partial class WarningPopUp : UserControl
  7. {
  8. public WarningPopUp()
  9. {
  10. InitializeComponent();
  11. _timer = new();
  12. _timer.Elapsed += new System.Timers.ElapsedEventHandler(TimerCallBack);
  13. _timer.Interval = 10000;
  14. _timer.AutoReset = false;
  15. _timer.Enabled = false;
  16. }
  17. private readonly System.Timers.Timer _timer;
  18. private void TimerCallBack(object? sender, System.Timers.ElapsedEventArgs e)
  19. {
  20. App.Current.Dispatcher.Invoke(() =>
  21. {
  22. this.IsOpen = false;
  23. });
  24. }
  25. private int Counter
  26. {
  27. get { return (int)GetValue(CounterProperty); }
  28. set { SetValue(CounterProperty, value); }
  29. }
  30. // Using a DependencyProperty as the backing store for Counter. This enables animation, styling, binding, etc...
  31. public static readonly DependencyProperty CounterProperty =
  32. DependencyProperty.Register("Counter", typeof(int), typeof(WarningPopUp), new PropertyMetadata(default));
  33. internal ObservableDictionary<byte, ObservableDictionary<byte, AlarmInfo>> AlarmSource
  34. {
  35. get { return (ObservableDictionary<byte, ObservableDictionary<byte, AlarmInfo>>)GetValue(AlarmSourceProperty); }
  36. set { SetValue(AlarmSourceProperty, value); }
  37. }
  38. // Using a DependencyProperty as the backing store for AlarmSource. This enables animation, styling, binding, etc...
  39. public static readonly DependencyProperty AlarmSourceProperty =
  40. DependencyProperty.Register("AlarmSource", typeof(ObservableDictionary<byte, ObservableDictionary<byte, AlarmInfo>>), typeof(WarningPopUp), new PropertyMetadata(default, AlarmsPropertyChangedCallback));
  41. private static WarningPopUp? _warning;
  42. static void AlarmsPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
  43. {
  44. if (d is not WarningPopUp warning)
  45. return;
  46. _warning = warning;
  47. if (e.NewValue is not ObservableDictionary<byte, ObservableDictionary<byte, AlarmInfo>> and || and is null)
  48. return;
  49. and.CollectionChanged += And_CollectionChanged;
  50. warning.Counter = and.Count;
  51. warning.WarningHint.Visibility = and.Count switch
  52. {
  53. 0 => Visibility.Collapsed,
  54. _ => Visibility.Visible,
  55. };
  56. }
  57. private static void And_CollectionChanged(object? sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
  58. {
  59. if (_warning is null)
  60. return;
  61. if (_warning.AlarmSource.Count == 0)
  62. {
  63. _warning.WarningHint.Visibility = Visibility.Collapsed;
  64. _warning.Counter = 0;
  65. //animationEvent.Reset();
  66. _warning.PopPos.Background = new SolidColorBrush(Colors.Transparent);
  67. }
  68. else
  69. {
  70. _warning.WarningHint.Visibility = Visibility.Visible;
  71. //_warning.IsOpen = _warning.Counter < _warning.AlarmSource.Count;
  72. _warning.Counter = _warning.AlarmSource.Count;
  73. //if (!animationEvent.WaitOne(0))
  74. //{
  75. // animationEvent.Set();
  76. // Task.Factory.StartNew(_warning.Animation);
  77. //}
  78. }
  79. }
  80. private readonly static ManualResetEvent animationEvent = new(false);
  81. private void Animation()
  82. {
  83. while (animationEvent.WaitOne(0))
  84. {
  85. try
  86. {
  87. App.Current.Dispatcher.Invoke(() =>
  88. {
  89. this.PopPos.Background = (Brush)App.Current.Resources["EmergencyColor"];
  90. });
  91. Thread.Sleep(1000);
  92. App.Current.Dispatcher.Invoke(() =>
  93. {
  94. this.PopPos.Background = new SolidColorBrush(Colors.Transparent);
  95. });
  96. Thread.Sleep(1000);
  97. }
  98. catch
  99. {
  100. }
  101. }
  102. }
  103. public ICommand ClCommand
  104. {
  105. get { return (ICommand)GetValue(ClCommandProperty); }
  106. set { SetValue(ClCommandProperty, value); }
  107. }
  108. // Using a DependencyProperty as the backing store for ClCommand. This enables animation, styling, binding, etc...
  109. public static readonly DependencyProperty ClCommandProperty =
  110. DependencyProperty.Register("ClCommand", typeof(ICommand), typeof(WarningPopUp), new PropertyMetadata(default));
  111. private bool IsOpen
  112. {
  113. get { return (bool)GetValue(IsOpenProperty); }
  114. set { SetValue(IsOpenProperty, value); }
  115. }
  116. public static readonly DependencyProperty IsOpenProperty =
  117. DependencyProperty.Register("IsOpen", typeof(bool), typeof(WarningPopUp), new PropertyMetadata(false, PropertyChangedCallback));
  118. private static void PropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
  119. {
  120. if (d is not WarningPopUp pop)
  121. return;
  122. if (e.NewValue is not bool b || b == false)
  123. return;
  124. pop._timer.Stop();
  125. pop._timer.Start();
  126. }
  127. private void Button_Click(object sender, RoutedEventArgs e)
  128. {
  129. this.IsOpen = false;
  130. this._timer.Stop();
  131. }
  132. }
  133. }