IoAlarmSignal.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using Aitex.Core.RT.Event;
  2. using Aitex.Core.RT.IOCore;
  3. using Aitex.Core.Util;
  4. using MECF.Framework.Common.Event;
  5. using System.Diagnostics;
  6. using System.Xml;
  7. namespace Aitex.Core.RT.Device.Unit
  8. {
  9. /// <summary>
  10. /// DO reset
  11. /// DI alarm signal
  12. /// </summary>
  13. public class IoAlarmSignal : BaseDevice, IDevice
  14. {
  15. private bool SignalFeedback
  16. {
  17. get
  18. {
  19. if (_diSignal != null)
  20. return _diSignal.Value;
  21. return false;
  22. }
  23. set
  24. {
  25. }
  26. }
  27. private bool SignalSetPoint
  28. {
  29. get
  30. {
  31. if (_doReset != null)
  32. return _doReset.Value;
  33. return false;
  34. }
  35. set
  36. {
  37. if (_doReset != null)
  38. _doReset.Value = value;
  39. }
  40. }
  41. private DIAccessor _diSignal = null;
  42. private DOAccessor _doReset = null;
  43. private Stopwatch _timer = new Stopwatch();
  44. public AlarmEventItem AlarmTriggered { get; set; }
  45. private R_TRIG _trigSignalOn = new R_TRIG();
  46. public IoAlarmSignal(string module, XmlElement node, string ioModule = "")
  47. {
  48. base.Module = string.IsNullOrEmpty(node.GetAttribute("module")) ? module : node.GetAttribute("module");
  49. base.Name = node.GetAttribute("id");
  50. base.Display = node.GetAttribute("display");
  51. base.DeviceID = node.GetAttribute("schematicId");
  52. _doReset = ParseDoNode("doReset", node, ioModule);
  53. _diSignal = ParseDiNode("diSignal", node, ioModule);
  54. }
  55. public bool Initialize()
  56. {
  57. AlarmTriggered = SubscribeAlarm($"{Module}.{Name}.AlarmTriggered", $"{Name} alarm triggered", ResetAlarmChecker);
  58. return true;
  59. }
  60. private bool ResetAlarmChecker()
  61. {
  62. if (SignalFeedback)
  63. {
  64. if (!SignalSetPoint)
  65. {
  66. _timer.Restart();
  67. SignalSetPoint = true;
  68. }
  69. }
  70. return !SignalFeedback;
  71. }
  72. public void Terminate()
  73. {
  74. }
  75. public void Monitor()
  76. {
  77. _trigSignalOn.CLK = SignalFeedback ;
  78. if (_trigSignalOn.Q)
  79. {
  80. AlarmTriggered.Set(Display);
  81. }
  82. if (SignalSetPoint)
  83. {
  84. if (!SignalFeedback)
  85. {
  86. AlarmTriggered.Reset();
  87. SignalSetPoint = false;
  88. _timer.Stop();
  89. }
  90. if (SignalFeedback && _timer.IsRunning &&
  91. _timer.ElapsedMilliseconds > 5 * 1000)
  92. {
  93. _timer.Stop();
  94. EV.PostWarningLog(Module, $"Can not reset {Display} in 5 seconds");
  95. SignalSetPoint = false;
  96. }
  97. }
  98. }
  99. public void Reset()
  100. {
  101. _trigSignalOn.RST = true;
  102. AlarmTriggered.Reset();
  103. }
  104. public void SetIgnoreError(bool ignore)
  105. {
  106. AlarmTriggered.SetIgnoreError(ignore);
  107. }
  108. }
  109. }