IoAlarmSignal.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. public string AlarmSignalCategoryKey { get; set; }
  66. private DeviceTimer _alarmMonitorTimer = new DeviceTimer();
  67. public bool IsAlarmAutoRecovery => _isAlarmAutoRecovery;
  68. public bool _isAlarmAutoRecovery;
  69. public string Level { get; set; }
  70. bool _ignoreSaveDB = false;
  71. public bool ignoreSaveDB { get { return _ignoreSaveDB; } set { _ignoreSaveDB = value; } }
  72. private float _delayTime;//大于0时,此时间段内一直满足条件才报警
  73. private Stopwatch _delayTimer = new Stopwatch();
  74. private float _limitValue;//大于0时,此时间段内一直满足条件才报警
  75. private string _condition;
  76. public AITSensorData DeviceData
  77. {
  78. get
  79. {
  80. AITSensorData data = new AITSensorData()
  81. {
  82. DeviceName = Name,
  83. DeviceSchematicId = DeviceID,
  84. DisplayName = Display,
  85. Value = SignalFeedback,
  86. };
  87. return data;
  88. }
  89. }
  90. public bool Value => SignalFeedback;
  91. public IoAlarmSignal(string module, string name, DIAccessor diSignal, bool alarmTrigValue = true, bool isAlarmAutoRecovery = false)
  92. {
  93. base.Module = module;
  94. base.Name = name;
  95. _diSignal = diSignal;
  96. _alarmTrigValue = alarmTrigValue;
  97. _isAlarmAutoRecovery = isAlarmAutoRecovery;
  98. Initialize();
  99. }
  100. public IoAlarmSignal(string module, XmlElement node, string ioModule = "")
  101. {
  102. base.Module = string.IsNullOrEmpty(node.GetAttribute("module")) ? module : node.GetAttribute("module");
  103. base.Name = ioModule.ToLower().StartsWith("gasline") ? $"{ioModule}{node.GetAttribute("id")}" : node.GetAttribute("id");
  104. base.Display = node.GetAttribute("display");
  105. base.DeviceID = node.GetAttribute("schematicId");
  106. AlarmSignalCategoryKey = string.IsNullOrEmpty(node.GetAttribute("alarmCategory")) ? ((int)IoAlarmSignalCategory.DIAlarm).ToString() : node.GetAttribute("alarmCategory");
  107. Level = node.GetAttribute("level");
  108. ignoreSaveDB = string.IsNullOrEmpty(node.GetAttribute("ignoreSaveDB")) ? false : Convert.ToBoolean(node.GetAttribute("ignoreSaveDB"));
  109. _doReset = ParseDoNode("doReset", node, ioModule);
  110. _diSignal = ParseDiNode("diSignal", node, ioModule);
  111. _aiSignal = ParseAiNode("aiSignal", node, ioModule);
  112. if (_diSignal == null)
  113. _diSignal = ParseDiNode("di", node, ioModule);
  114. _alarmTrigValue = Convert.ToBoolean(string.IsNullOrEmpty(node.GetAttribute("alarmTrigValue")) ? "true" : node.GetAttribute("alarmTrigValue"));
  115. _isAlarmAutoRecovery = Convert.ToBoolean(string.IsNullOrEmpty(node.GetAttribute("alarmAutoRecovery")) ? "false" : node.GetAttribute("alarmAutoRecovery"));
  116. _delayTime = Convert.ToSingle(string.IsNullOrEmpty(node.GetAttribute("delay")) ? "0" : node.GetAttribute("delay"));
  117. _limitValue = Convert.ToSingle(string.IsNullOrEmpty(node.GetAttribute("limitValue")) ? "0" : node.GetAttribute("limitValue"));
  118. _condition = node.GetAttribute("condition");
  119. }
  120. public bool Initialize()
  121. {
  122. //AlarmTriggered = SubscribeAlarm($"{Module}.{Name}.AlarmTriggered", $"{Name} alarm triggered", ResetAlarmChecker);
  123. DATA.Subscribe($"{Module}.{Name}.DeviceData", () => DeviceData, FLAG.IgnoreSaveDB);
  124. DATA.Subscribe($"{Module}.{Name}.Value", () => SignalFeedback);
  125. // DATA.Subscribe($"{Module}.{Name}.ignoreSaveDB", () => ignoreSaveDB);
  126. _alarmMonitorTimer.Start(2000);
  127. return true;
  128. }
  129. private bool ResetAlarmChecker()
  130. {
  131. if (SignalFeedback)
  132. {
  133. if (!SignalSetPoint)
  134. {
  135. _resetTimer.Restart();
  136. SignalSetPoint = true;
  137. }
  138. }
  139. return !SignalFeedback;
  140. }
  141. public void Terminate()
  142. {
  143. }
  144. public void Monitor()
  145. {
  146. if (SC.ContainsItem("System.BypassInterlock") && SC.GetValue<bool>("System.BypassInterlock"))
  147. return;
  148. if (!Enum.TryParse<IoAlarmSignalCategory>(AlarmSignalCategoryKey, out var resultData))
  149. return;
  150. if (resultData != IoAlarmSignalCategory.DIAlarm)
  151. return;
  152. //两秒后才检测,防止有报警是false值的时候,PLC还没读取数值就报警
  153. if (!_alarmMonitorTimer.IsTimeout())
  154. return;
  155. if (AlarmTriggered != null)
  156. {
  157. if (_delayTime > 0)
  158. {
  159. if (_alarmTrigValue == SignalFeedback)
  160. {
  161. if (!_delayTimer.IsRunning)
  162. _delayTimer.Restart();
  163. if (_delayTimer.ElapsedMilliseconds > _delayTime * 1000)
  164. _trigSignalOn.CLK = true;
  165. else
  166. _trigSignalOn.CLK = false;
  167. }
  168. else
  169. {
  170. _trigSignalOn.CLK = false;
  171. _delayTimer.Stop();
  172. }
  173. }
  174. else
  175. {
  176. _trigSignalOn.CLK = _alarmTrigValue ? SignalFeedback : !SignalFeedback;
  177. }
  178. if (_trigSignalOn.R)
  179. {
  180. AlarmTriggered?.Set();
  181. }
  182. if (_trigSignalOn.T)
  183. {
  184. AlarmTriggered?.Set();
  185. if (!ignoreSaveDB)
  186. {
  187. EV.ClearAlarmEvent(AlarmTriggered.EventEnum);
  188. }
  189. LOG.Write($"{AlarmTriggered.EventEnum} is auto recovered");
  190. }
  191. if (SignalSetPoint)
  192. {
  193. if (!SignalFeedback)
  194. {
  195. AlarmTriggered.Reset();
  196. SignalSetPoint = false;
  197. _resetTimer.Stop();
  198. }
  199. if (SignalFeedback && _resetTimer.IsRunning &&
  200. _resetTimer.ElapsedMilliseconds > 5 * 1000)
  201. {
  202. _resetTimer.Stop();
  203. EV.PostWarningLog(Module, $"Can not reset {Display} in 5 seconds");
  204. SignalSetPoint = false;
  205. }
  206. }
  207. }
  208. }
  209. public void Reset()
  210. {
  211. _trigSignalOn.RST = true;
  212. if (AlarmTriggered != null)
  213. AlarmTriggered.IsAcknowledged = true;
  214. }
  215. public void SetIgnoreError(bool ignore)
  216. {
  217. AlarmTriggered.SetIgnoreError(ignore);
  218. }
  219. }
  220. }