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 ElectricalCalculator
{
private ILog logger = LogManager.GetLogger("AppLog");
private WindPowerStation station;
private EDosProxy eDos;
public ElectricalCalculator(WindPowerStation wt)
{
station = wt;
eDos = EDosProxyFactory.Instance.Proxy;
}
///
/// 计算电气所有关联规则的值,并保存
///
/// 电气报警测点的值,关联到AlertRule表
public void Calculate()
{
IList interpreters = InterpreterFactory.Instance.GetInterpretersByElectricalId(station.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(station.Id, AlertObjectType.Electrical);
AlertSnap snap = AlertSnapRepository.GetAlertSnap(AlertObjectType.Electrical, station.Id, ari.AlertRule.EdnaValue);
if (ruleResult)
{
double interval = 0;
if (snap == null)
{
snap = AlertSnapFactory.CreateAlertSnap(ari.AlertRule, station);
}
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 = GetWindPowerStationAlertPoint(station.Id);
logger.InfoFormat("写报警测点,key={0}, value={1}", key, ari.AlertRule.EdnaValue);
eDos.SendSinglePoint(key, ari.AlertRule.EdnaValue);
logger.InfoFormat("触发报警!电气:{0},{1},规则:{2},{3}",
station.Id, station.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.LastUpdatePerson = "system";
snap.LastCloseTime = DateTime.Now;
AlertSnapRepository.SaveAlertSnap(snap);
logger.InfoFormat("解除报警!电气:{0},{1},规则:{2},{3}",
station.Id, station.Name, ari.AlertRule.Id, ari.AlertRule.Expression);
}
}
}
}
}
private string GetWindPowerStationAlertPoint(string id)
{
string ret = WindPowerStationRepository.GetWindPowerStationPointId(id, "FJBJ3");
return BuildPointId(ret);
}
private string BuildPointId(string PointID)
{
string[] ids = PointID.Split('.');
if (ids != null && ids.Length == 3)
{
PointID = ids[1] + "_" + ids[2];
}
return PointID;
}
}
}