AlarmEventItem.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.Serialization;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using Aitex.Core.RT.Event;
  8. using Aitex.Core.RT.Log;
  9. namespace MECF.Framework.Common.Event
  10. {
  11. public interface IAlarmHandler
  12. {
  13. void AlarmStateChanged(AlarmEventItem item);
  14. }
  15. [DataContract]
  16. [Serializable]
  17. public class AlarmEventItem : EventItem
  18. {
  19. public Func<bool> ResetChecker { get; set; }
  20. private IAlarmHandler _alarmHandler;
  21. private bool _ignoreAlarm;
  22. public AlarmEventItem(string source, string name, string description, Func<bool> resetChecker, IAlarmHandler handler) : base(source, name, description,
  23. EventLevel.Alarm, EventType.EventUI_Notify)
  24. {
  25. ResetChecker = resetChecker;
  26. IsAcknowledged = true;
  27. _alarmHandler = handler;
  28. }
  29. public AlarmEventItem()
  30. {
  31. }
  32. public void SetIgnoreError(bool ignore)
  33. {
  34. if (_ignoreAlarm == ignore)
  35. return;
  36. _ignoreAlarm = ignore;
  37. if (ignore)
  38. {
  39. EV.PostWarningLog(Source, $"{Source} {EventEnum} error will be ignored");
  40. if (IsTriggered)
  41. {
  42. IsAcknowledged = true;
  43. if (_alarmHandler != null)
  44. _alarmHandler.AlarmStateChanged(this);
  45. }
  46. }
  47. else
  48. {
  49. Reset();
  50. }
  51. }
  52. public void Reset()
  53. {
  54. if (_ignoreAlarm)
  55. return;
  56. if (!IsTriggered)
  57. return;
  58. if (ResetChecker == null || ResetChecker())
  59. {
  60. EV.PostInfoLog(Source, $"{Source} {EventEnum} is cleared");
  61. IsAcknowledged = true;
  62. if (_alarmHandler != null)
  63. _alarmHandler.AlarmStateChanged(this);
  64. }
  65. }
  66. public void Set()
  67. {
  68. Set(null);
  69. }
  70. public void Set(string error)
  71. {
  72. if (_ignoreAlarm)
  73. return;
  74. if (IsTriggered)
  75. return;
  76. if (!string.IsNullOrEmpty(error))
  77. {
  78. Description = error;
  79. AdditionalDescription = error;
  80. }
  81. IsAcknowledged = false;
  82. OccuringTime = DateTime.Now;
  83. if (_alarmHandler != null)
  84. _alarmHandler.AlarmStateChanged(this);
  85. }
  86. }
  87. }