IoAlarmSignal.cs 8.5 KB

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