using EDosApiWrapper;
using GDNXFD.Data;
using GDNXFD.Data.Repositories;
using log4net;
using System;
using System.Collections.Generic;
using System.Linq;
namespace GDNXFD.Alert.Interpreter
{
public class WindTurbineCalculator
{
private ILog logger = LogManager.GetLogger("AppLog");
private WindTurbine windTurbine;
private EDosProxy eDos;
public WindTurbineCalculator(WindTurbine wt)
{
windTurbine = wt;
eDos = EDosProxyFactory.Instance.Proxy;
}
///
/// 计算风机所有关联规则的值,并保存
///
/// 风机报警测点的值,关联到AlertRule表
public void Calculate()
{
IList interpreters = InterpreterFactory.Instance.GetInterpretersByWindturbineId(windTurbine.Id);
if (interpreters != null && interpreters.Count > 0)
{
logger.Debug("适用规则数:" + interpreters.Count);
foreach (AlertRuleInterpreter ari in interpreters)
{
logger.Debug("处理报警规则:" + ari.AlertRule.Expression);
ari.DataInfo = "";
bool ruleResult = ari.Interpret(windTurbine.Id, AlertObjectType.WindTurbine);
AlertSnap snap = AlertSnapRepository.GetAlertSnap(AlertObjectType.WindTurbine, windTurbine.Id, ari.AlertRule.EdnaValue);
if (ruleResult)
{
double interval = 0;
if (snap == null)
{
snap = AlertSnapFactory.CreateAlertSnap(ari.AlertRule, windTurbine);
}
else if (snap.IsOpened)
{
TimeSpan ts = DateTime.Now.Subtract((DateTime)snap.LastUpdateTime);
interval = ts.TotalHours;
}
;
if (snap.IsOpened == false || interval > 1)
{
snap.IsOpened = true;
snap.DataInfo = CommonMethod.StringCopy(ari.DataInfo, 2000);
snap.LastUpdatePerson = "system";
snap.LastUpdateTime = DateTime.Now;
if (snap.IsConfirmed == true && interval > 8)
snap.IsConfirmed = false;
AlertSnapRepository.SaveAlertSnap(snap);
//写实时库
string key = GetWindTurbineAlertPoint(windTurbine.Id);
logger.InfoFormat("写报警测点,key={0}, value={1}", key, ari.AlertRule.EdnaValue);
eDos.SendSinglePoint(key, ari.AlertRule.EdnaValue);
logger.InfoFormat("触发报警!风机:{0},{1},规则:{2},{3}",
windTurbine.Id, windTurbine.Name, ari.AlertRule.Id, ari.AlertRule.Expression);
}
}
else
{
//解除报警
if (snap != null && snap.IsOpened)
{
snap.IsOpened = false;
snap.DataInfo = CommonMethod.StringCopy(ari.DataInfo, 2000);
snap.IsConfirmed = false;
snap.LastClosePerson = "system";
snap.LastCloseTime = DateTime.Now;
AlertSnapRepository.SaveAlertSnap(snap);
logger.InfoFormat("解除报警!风机:{0},{1},规则:{2},{3}",
windTurbine.Id, windTurbine.Name, ari.AlertRule.Id, ari.AlertRule.Expression);
}
}
}
}
}
private string GetWindTurbineAlertPoint(string windTurbineId)
{
string ret;
WindTurbineTestingPointAI point = DataCache.Instance.WindTurbineAlertPoints
.Where(q => q.WindturbineId == windTurbineId)
.FirstOrDefault();
if (point == null)
{
ret = WindTurbineRepository.GetWindTurbineAIPointId(windTurbineId, "FJBJ3");
if (ret == null)
logger.InfoFormat("风机{0}没有首出报警测点", windTurbineId);
}
else
ret = point.Id;
return BuildPointId(ret);
}
private string BuildPointId(string PointID)
{
if (PointID == null)
PointID = "";
else
{
string[] ids = PointID.Split('.');
if (ids != null && ids.Length == 3)
{
PointID = ids[1] + "_" + ids[2];
}
}
return PointID;
}
}
}