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 内部变量
///
/// 初次检测到变量异常的时间
///
private DateTime _abnormalStartTime;
///
/// 变量信号是否异常
///
private bool _isInAbnormalState = false;
///
/// 模块名
///
private string _module;
///
/// 异常项名
///
private string _itemName;
#endregion
public HoldoffTimeSignalMonitor(string Muodule,string ItemName)
{
_module = Muodule;
_itemName = ItemName;
}
///
/// 判断信号是否超出holdofftime时间的异常
///
///
///
///
///
///
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;
}
}
}