AlarmEventItem.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 void SetIgnoreError(bool ignore)
  30. {
  31. if (_ignoreAlarm == ignore)
  32. return;
  33. _ignoreAlarm = ignore;
  34. if (ignore)
  35. {
  36. EV.PostWarningLog(Source, $"{Source} {EventEnum} error will be ignored");
  37. if (IsTriggered)
  38. {
  39. IsAcknowledged = true;
  40. if (_alarmHandler != null)
  41. _alarmHandler.AlarmStateChanged(this);
  42. }
  43. }
  44. else
  45. {
  46. Reset();
  47. }
  48. }
  49. public void Reset()
  50. {
  51. if (_ignoreAlarm)
  52. return;
  53. if (!IsTriggered)
  54. return;
  55. if (ResetChecker == null || ResetChecker())
  56. {
  57. EV.PostInfoLog(Source, $"{Source} {EventEnum} is cleared");
  58. IsAcknowledged = true;
  59. if (_alarmHandler != null)
  60. _alarmHandler.AlarmStateChanged(this);
  61. }
  62. }
  63. public void Set()
  64. {
  65. Set(null);
  66. }
  67. public void Set(string error)
  68. {
  69. if (_ignoreAlarm)
  70. return;
  71. if (IsTriggered)
  72. return;
  73. if (!string.IsNullOrEmpty(error))
  74. Description = error;
  75. IsAcknowledged = false;
  76. OccuringTime = DateTime.Now;
  77. if (_alarmHandler != null)
  78. _alarmHandler.AlarmStateChanged(this);
  79. }
  80. }
  81. }