HoldoffTimeSignalMonitor.cs 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using Aitex.Core.RT.Log;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace CyberX8_RT.Modules
  9. {
  10. public class HoldoffTimeSignalMonitor
  11. {
  12. #region 内部变量
  13. /// <summary>
  14. /// 初次检测到变量异常的时间
  15. /// </summary>
  16. private DateTime _abnormalStartTime;
  17. /// <summary>
  18. /// 变量信号是否异常
  19. /// </summary>
  20. private bool _isInAbnormalState = false;
  21. /// <summary>
  22. /// 模块名
  23. /// </summary>
  24. private string _module;
  25. /// <summary>
  26. /// 异常项名
  27. /// </summary>
  28. private string _itemName;
  29. #endregion
  30. public HoldoffTimeSignalMonitor(string Muodule,string ItemName)
  31. {
  32. _module = Muodule;
  33. _itemName = ItemName;
  34. }
  35. /// <summary>
  36. /// 判断信号是否超出holdofftime时间的异常
  37. /// </summary>
  38. /// <param name="holdoffTime"></param>
  39. /// <param name="currentValue"></param>
  40. /// <param name="maxLimit"></param>
  41. /// <param name="minLimit"></param>
  42. /// <returns></returns>
  43. public bool IsSignalAbnormal(double holdoffTime,double currentValue,double? maxLimit,double? minLimit)
  44. {
  45. bool isAbnormal = false;
  46. if(CheckValueAbnormal(currentValue,maxLimit,minLimit)) //检测当前值是否异常
  47. {
  48. isAbnormal = true;
  49. }
  50. if(isAbnormal && !_isInAbnormalState)//首次检测到异常
  51. {
  52. LOG.WriteLog(eEvent.WARN_METAL, _module, $"{_module} {_itemName} is Abnormal,CurrentValue is :{currentValue}");
  53. _isInAbnormalState = true;
  54. _abnormalStartTime = DateTime.Now;
  55. }
  56. if(!isAbnormal && _isInAbnormalState) // holdofftime时间内恢复
  57. {
  58. LOG.WriteLog(eEvent.INFO_METAL, _module, $"{_module} {_itemName} is restore");
  59. _isInAbnormalState = false;
  60. }
  61. if((DateTime.Now - _abnormalStartTime).TotalSeconds > holdoffTime && _isInAbnormalState)//异常时间超过holdofftime
  62. {
  63. //_isInAbnormalState = false;//报错前把状态清掉,防止人工处理正常后该值一直为true
  64. return true;
  65. }
  66. return false;
  67. }
  68. private bool CheckValueAbnormal(double currentValue, double? maxLimit, double? minLimit)
  69. {
  70. if(maxLimit!=null && minLimit != null)
  71. {
  72. if(currentValue >= maxLimit || currentValue <= minLimit)
  73. {
  74. return true ;
  75. }
  76. }
  77. else if(maxLimit!=null && minLimit == null)
  78. {
  79. if (currentValue >= maxLimit)
  80. {
  81. return true;
  82. }
  83. }
  84. else if(maxLimit == null && minLimit != null)
  85. {
  86. if (currentValue <= minLimit)
  87. {
  88. return true;
  89. }
  90. }
  91. return false;
  92. }
  93. }
  94. }