IoAlarmSignal.cs 8.6 KB

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