IoAlarmSignal.cs 8.7 KB

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