using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace IStation.Calculation
{
///
/// 监测报警计算辅助类
///
public class MonitorAlarmCalcuHelper
{
///
/// 验证是否有报警
///
public static bool Verify
(
Model.MonitorAlarmConfigure configure,//报警配置
Model.MonitorBasicRecord record,//信号记录(可能为空)
Func getLastRecord,//获取最后一条记录
Func getFirstUnChangedRecord//获取第一条数据不变的记录
)
{
var result = false;
switch (configure.AlarmType)
{
case Alarm.Type_Upper://数据越上限
{
if (record != null)
{
if (double.TryParse(record.DataValue, out double dataValue))
{
if (!string.IsNullOrEmpty(configure.ThresholdValue))
{
if (double.TryParse(configure.ThresholdValue, out double thresholdValue))
{
if (dataValue > thresholdValue)
{
result = true;
}
}
}
}
}
}
break;
case Alarm.Type_Lower://数据越下限
{
if (record != null)
{
if (double.TryParse(record.DataValue, out double dataValue))
{
if (!string.IsNullOrEmpty(configure.ThresholdValue))
{
if (double.TryParse(configure.ThresholdValue, out double thresholdValue))
{
if (dataValue < thresholdValue)
{
result = true;
}
}
}
}
}
}
break;
case Alarm.Type_Lacked://缺数据
{
if (!string.IsNullOrEmpty(configure.ThresholdValue))
{
if (double.TryParse(configure.ThresholdValue, out double thresholdValue))
{
var lastRecord = getLastRecord();
if (lastRecord != null)
{
var dataTime = record == null ? DateTime.Now : record.DataTime;
var fValue = (int)Math.Floor(Math.Abs(dataTime.Subtract(lastRecord.DataTime).TotalSeconds));
if (fValue > thresholdValue)
{
return true;
}
}
}
}
}
break;
case Alarm.Type_UnChanged://数据不变
{
if (record != null)
{
if (!string.IsNullOrEmpty(configure.ThresholdValue))
{
if (double.TryParse(configure.ThresholdValue, out double thresholdValue))
{
var firstUnChangedRecord = getFirstUnChangedRecord();
if (firstUnChangedRecord != null)
{
if (record.DataValue == firstUnChangedRecord.DataValue)
{
var fValue = (int)Math.Floor(Math.Abs(record.DataTime.Subtract(firstUnChangedRecord.DataTime).TotalSeconds));
if (fValue > thresholdValue)
{
return true;
}
}
}
}
}
}
}
break;
case Alarm.Type_Mutation://数据突变
{
if (record != null)
{
if (double.TryParse(record.DataValue, out double dataValue))
{
if (!string.IsNullOrEmpty(configure.ThresholdValue))
{
if (double.TryParse(configure.ThresholdValue, out double thresholdValue))
{
var lastRecord = getLastRecord();
if (lastRecord != null)
{
if (double.TryParse(lastRecord.DataValue, out double lastDataValue))
{
var fValue = Math.Abs(dataValue - lastDataValue);
var sub = fValue / lastDataValue * 100;
if (sub > thresholdValue)
{
return true;
}
}
}
}
}
}
}
}
break;
case Alarm.Type_UnReasonable://数据不合理
{
if (record != null)
{
if (double.TryParse(record.DataValue, out double dataValue))
{
if (!string.IsNullOrEmpty(configure.ThresholdValue))
{
var thresholdModel = Model.MonitorAlarmConfigure.ReasonableSpace.ToModel(configure.ThresholdValue);
if (thresholdModel != null)
{
if (!thresholdModel.Verify(dataValue))
{
result = true;
}
}
}
}
}
}
break;
case Alarm.Type_Interrupted://数据中断
{
if (record == null)
{
if (!string.IsNullOrEmpty(configure.ThresholdValue))
{
if (double.TryParse(configure.ThresholdValue, out double thresholdValue))
{
var lastRecord = getLastRecord();
if (lastRecord != null)
{
var fValue = (int)Math.Floor(Math.Abs(DateTime.Now.Subtract(lastRecord.DataTime).TotalSeconds));
if (fValue > thresholdValue)
{
return true;
}
}
}
}
}
}
break;
default: break;
}
return result;
}
///
/// 创建
///
public static Model.MonitorAlarmRecord Create
(
Model.MonitorPoint monitor,//测点
Model.Signal signal,//信号(可能为空)
Model.MonitorAlarmConfigure configure,//报警配置
Model.MonitorBasicRecord record//记录(可能为空)
)
{
var alarm = new Model.MonitorAlarmRecord();
alarm.CorpID = configure.CorpID;
alarm.ConfigureID = configure.ID;
alarm.MonitorPointID = monitor.ID;
alarm.SignalID = signal?.ID;
alarm.AlarmType=configure.AlarmType;
alarm.AlarmLevel= configure.AlarmLevel;
alarm.ThresholdValue = configure.ThresholdValue;
alarm.AlarmTime = DateTime.Now;
alarm.AlarmContent = configure.AlarmType;
alarm.DataTime = record?.DataTime;
alarm.DataValue = record?.DataValue;
alarm.HandleStatus = Alarm.HandleStatus_None;
return alarm;
}
}
}