IoAlarmSignal.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. using Aitex.Core.Common.DeviceData;
  2. using Aitex.Core.RT.DataCenter;
  3. using Aitex.Core.RT.Event;
  4. using Aitex.Core.RT.IOCore;
  5. using Aitex.Core.RT.Log;
  6. using Aitex.Core.RT.SCCore;
  7. using Aitex.Core.Util;
  8. using MECF.Framework.Common.Event;
  9. using System;
  10. using System.Diagnostics;
  11. using System.Xml;
  12. namespace Aitex.Core.RT.Device.Unit
  13. {
  14. /// <summary>
  15. /// DO reset
  16. /// DI alarm signal
  17. /// </summary>
  18. public class IoAlarmSignal : BaseDevice, IDevice
  19. {
  20. private bool SignalFeedback
  21. {
  22. get
  23. {
  24. if (_diSignal != null)
  25. return _diSignal.Value;
  26. if(_aiSignal != null && !string.IsNullOrEmpty(_condition))
  27. {
  28. if (_condition == "H")
  29. return _aiSignal.FloatValue > _limitValue;
  30. if (_condition == "L")
  31. return _aiSignal.FloatValue < _limitValue;
  32. }
  33. return false;
  34. }
  35. set
  36. {
  37. }
  38. }
  39. private bool SignalSetPoint
  40. {
  41. get
  42. {
  43. if (_doReset != null)
  44. return _doReset.Value;
  45. return false;
  46. }
  47. set
  48. {
  49. if (_doReset != null)
  50. _doReset.Value = value;
  51. }
  52. }
  53. private AIAccessor _aiSignal = null;
  54. private DIAccessor _diSignal = null;
  55. private DOAccessor _doReset = null;
  56. private Stopwatch _resetTimer = new Stopwatch();
  57. public AlarmEventItem AlarmTriggered { get; set; }
  58. public AlarmEventItem AlarmRecovery { get; set; }
  59. private RD_TRIG _trigSignalOn = new RD_TRIG();
  60. public RD_TRIG RrigSignalOn => _trigSignalOn;
  61. private bool _alarmTrigValue;
  62. public bool AlarmTrigValue => _alarmTrigValue;
  63. private DeviceTimer _alarmMonitorTimer = new DeviceTimer();
  64. public bool IsAlarmAutoRecovery => _isAlarmAutoRecovery;
  65. public bool _isAlarmAutoRecovery;
  66. public string Level { get; set; }
  67. private float _delayTime;//大于0时,此时间段内一直满足条件才报警
  68. private Stopwatch _delayTimer = new Stopwatch();
  69. private float _limitValue;//大于0时,此时间段内一直满足条件才报警
  70. private string _condition;
  71. public AITSensorData DeviceData
  72. {
  73. get
  74. {
  75. AITSensorData data = new AITSensorData()
  76. {
  77. DeviceName = Name,
  78. DeviceSchematicId = DeviceID,
  79. DisplayName = Display,
  80. Value = SignalFeedback,
  81. };
  82. return data;
  83. }
  84. }
  85. public bool Value => SignalFeedback;
  86. public IoAlarmSignal(string module,string name,DIAccessor diSignal,bool alarmTrigValue=true,bool isAlarmAutoRecovery=false)
  87. {
  88. base.Module = module;
  89. base.Name = name;
  90. _diSignal = diSignal;
  91. _alarmTrigValue = alarmTrigValue;
  92. _isAlarmAutoRecovery = isAlarmAutoRecovery;
  93. Initialize();
  94. }
  95. public IoAlarmSignal(string module, XmlElement node, string ioModule = "")
  96. {
  97. base.Module = string.IsNullOrEmpty(node.GetAttribute("module")) ? module : node.GetAttribute("module");
  98. base.Name = node.GetAttribute("id");
  99. base.Display = node.GetAttribute("display");
  100. base.DeviceID = node.GetAttribute("schematicId");
  101. Level = node.GetAttribute("level");
  102. _doReset = ParseDoNode("doReset", node, ioModule);
  103. _diSignal = ParseDiNode("diSignal", node, ioModule);
  104. _aiSignal = ParseAiNode("aiSignal", node, ioModule);
  105. if (_diSignal == null)
  106. _diSignal = ParseDiNode("di", node, ioModule);
  107. _alarmTrigValue = Convert.ToBoolean(string.IsNullOrEmpty(node.GetAttribute("alarmTrigValue")) ? "true" : node.GetAttribute("alarmTrigValue"));
  108. _isAlarmAutoRecovery = Convert.ToBoolean(string.IsNullOrEmpty(node.GetAttribute("alarmAutoRecovery")) ? "false" : node.GetAttribute("alarmAutoRecovery"));
  109. _delayTime = Convert.ToSingle(string.IsNullOrEmpty(node.GetAttribute("delay")) ? "0" : node.GetAttribute("delay"));
  110. _limitValue = Convert.ToSingle(string.IsNullOrEmpty(node.GetAttribute("limitValue")) ? "0" : node.GetAttribute("limitValue"));
  111. _condition = node.GetAttribute("condition");
  112. }
  113. public bool Initialize()
  114. {
  115. //AlarmTriggered = SubscribeAlarm($"{Module}.{Name}.AlarmTriggered", $"{Name} alarm triggered", ResetAlarmChecker);
  116. DATA.Subscribe($"{Module}.{Name}.DeviceData", () => DeviceData);
  117. DATA.Subscribe($"{Module}.{Name}.Value", () => SignalFeedback);
  118. _alarmMonitorTimer.Start(2000);
  119. return true;
  120. }
  121. private bool ResetAlarmChecker()
  122. {
  123. if (SignalFeedback)
  124. {
  125. if (!SignalSetPoint)
  126. {
  127. _resetTimer.Restart();
  128. SignalSetPoint = true;
  129. }
  130. }
  131. return !SignalFeedback;
  132. }
  133. public void Terminate()
  134. {
  135. }
  136. public void Monitor()
  137. {
  138. if (SC.ContainsItem("System.BypassInterlock") && SC.GetValue<bool>("System.BypassInterlock"))
  139. return;
  140. //两秒后才检测,防止有报警是false值的时候,PLC还没读取数值就报警
  141. if (!_alarmMonitorTimer.IsTimeout())
  142. return;
  143. if (AlarmTriggered != null)
  144. {
  145. if(_delayTime > 0)
  146. {
  147. if(_alarmTrigValue == SignalFeedback)
  148. {
  149. if(!_delayTimer.IsRunning)
  150. _delayTimer.Restart();
  151. if (_delayTimer.ElapsedMilliseconds > _delayTime * 1000)
  152. _trigSignalOn.CLK = true;
  153. else
  154. _trigSignalOn.CLK = false;
  155. }
  156. else
  157. {
  158. _trigSignalOn.CLK = false;
  159. _delayTimer.Stop();
  160. }
  161. }
  162. else
  163. {
  164. _trigSignalOn.CLK = _alarmTrigValue ? SignalFeedback : !SignalFeedback;
  165. }
  166. if (_trigSignalOn.R)
  167. {
  168. AlarmTriggered?.Set();
  169. }
  170. if (_trigSignalOn.T)
  171. {
  172. AlarmTriggered?.Set();
  173. EV.ClearAlarmEvent(AlarmTriggered.EventEnum);
  174. LOG.Write($"{AlarmTriggered.EventEnum} is auto recovered");
  175. }
  176. if (SignalSetPoint)
  177. {
  178. if (!SignalFeedback)
  179. {
  180. AlarmTriggered.Reset();
  181. SignalSetPoint = false;
  182. _resetTimer.Stop();
  183. }
  184. if (SignalFeedback && _resetTimer.IsRunning &&
  185. _resetTimer.ElapsedMilliseconds > 5 * 1000)
  186. {
  187. _resetTimer.Stop();
  188. EV.PostWarningLog(Module, $"Can not reset {Display} in 5 seconds");
  189. SignalSetPoint = false;
  190. }
  191. }
  192. }
  193. }
  194. public void Reset()
  195. {
  196. _trigSignalOn.RST = true;
  197. if (AlarmTriggered != null)
  198. AlarmTriggered.IsAcknowledged = true;
  199. }
  200. public void SetIgnoreError(bool ignore)
  201. {
  202. AlarmTriggered.SetIgnoreError(ignore);
  203. }
  204. }
  205. }