12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- using Aitex.Core.RT.Log;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Reflection;
- using System.Text;
- using System.Threading.Tasks;
- namespace CyberX8_RT.Modules
- {
- public class HoldoffTimeSignalMonitor
- {
- #region 内部变量
- /// <summary>
- /// 初次检测到变量异常的时间
- /// </summary>
- private DateTime _abnormalStartTime;
- /// <summary>
- /// 变量信号是否异常
- /// </summary>
- private bool _isInAbnormalState = false;
- /// <summary>
- /// 模块名
- /// </summary>
- private string _module;
- /// <summary>
- /// 异常项名
- /// </summary>
- private string _itemName;
- #endregion
- public HoldoffTimeSignalMonitor(string Muodule,string ItemName)
- {
- _module = Muodule;
- _itemName = ItemName;
- }
- /// <summary>
- /// 判断信号是否超出holdofftime时间的异常
- /// </summary>
- /// <param name="holdoffTime"></param>
- /// <param name="currentValue"></param>
- /// <param name="maxLimit"></param>
- /// <param name="minLimit"></param>
- /// <returns></returns>
- public bool IsSignalAbnormal(double holdoffTime,double currentValue,double? maxLimit,double? minLimit)
- {
- bool isAbnormal = false;
- if(CheckValueAbnormal(currentValue,maxLimit,minLimit)) //检测当前值是否异常
- {
- isAbnormal = true;
- }
- if(isAbnormal && !_isInAbnormalState)//首次检测到异常
- {
- LOG.WriteLog(eEvent.WARN_METAL, _module, $"{_module} {_itemName} is Abnormal,CurrentValue is :{currentValue}");
- _isInAbnormalState = true;
- _abnormalStartTime = DateTime.Now;
- }
- if(!isAbnormal && _isInAbnormalState) // holdofftime时间内恢复
- {
- LOG.WriteLog(eEvent.INFO_METAL, _module, $"{_module} {_itemName} is restore");
- _isInAbnormalState = false;
- }
- if((DateTime.Now - _abnormalStartTime).TotalSeconds > holdoffTime && _isInAbnormalState)//异常时间超过holdofftime
- {
- //_isInAbnormalState = false;//报错前把状态清掉,防止人工处理正常后该值一直为true
- return true;
- }
- return false;
- }
- private bool CheckValueAbnormal(double currentValue, double? maxLimit, double? minLimit)
- {
- if(maxLimit!=null && minLimit != null)
- {
- if(currentValue >= maxLimit || currentValue <= minLimit)
- {
- return true ;
- }
- }
- else if(maxLimit!=null && minLimit == null)
- {
- if (currentValue >= maxLimit)
- {
- return true;
- }
- }
- else if(maxLimit == null && minLimit != null)
- {
- if (currentValue <= minLimit)
- {
- return true;
- }
- }
- return false;
- }
- }
- }
|