using EDosApiWrapper; using GDNXFD.Data; using GDNXFD.Data.Repositories; using log4net; using System; using System.Collections.Generic; using System.Linq; using System.Threading; namespace GDNXFD.Alert.Interpreter { public class EDosBJMonitor { private ILog logger = LogManager.GetLogger("AppLog"); private IDictionary dictWtBJ; private IDictionary dictIFixBJ; private string[] arrWtPoints; private string[] arrFixPoints; private string eDosServerIP; private ushort eDosServerPort; private EDosProxy eDos; public EDosBJMonitor() { eDosServerIP = GlobalVar.EDosServerIP; eDosServerPort = GlobalVar.EDosServerPort; eDos = new EDosProxy(eDosServerIP, eDosServerPort); } public void WindTurbineBJMonitor() { if (dictWtBJ == null) { dictWtBJ = CreateDictWindturbineBJ(); arrWtPoints = dictWtBJ.Keys.ToArray(); } if (dictWtBJ == null || dictWtBJ.Count == 0) return; string[] narr = arrWtPoints;// dictWtBJ.Keys.ToArray(); int len = narr.Length; int currentTime = CommonMethod.ConvertDateTimeInt(DateTime.Now); eDOSApi.HISTORY_VALUE[] results = eDos.GetRTValueList(narr); for (int i = 0; i < results.Length; i++) { try { int valueTime = results[i].tTime; //有效时间外的值不处理,单位秒, 有效时间暂定义为1小时 if (currentTime - valueTime > 60 * 60) continue; //与上次扫描读取的值比对,如果没有更新则不处理 string key = narr[i]; int preTime = dictWtBJ[key].tTime; if (valueTime <= preTime) continue; //触发报警 int ednaValue = (int)results[i].dValue; if (ednaValue != 0) { double interval = 0; string wId = DataCache.Instance.DictWTP[key].WindturbineId; AlertSnap snap = AlertSnapRepository.GetWindturbineAlertSnap(wId, ednaValue); if (snap == null) { Warning warn = DataCache.Instance.DictWarning[ednaValue]; WindTurbine wt = (WindTurbine)DataCache.Instance.DictWindturbine2[wId]; snap = AlertSnapFactory.CreateAlertSnap(warn, wt); } if (snap.IsOpened) { TimeSpan ts = DateTime.Now.Subtract((DateTime)snap.LastUpdateTime); interval = ts.TotalHours; if (interval < 1) continue; } snap.IsOpened = true; snap.DataInfo = string.Format("EDNAVALUE={0}", ednaValue); snap.LastUpdatePerson = "system"; snap.LastUpdateTime = DateTime.Now; if (snap.IsConfirmed && interval > 8) snap.IsConfirmed = false; AlertSnapRepository.SaveAlertSnap(snap); logger.InfoFormat("收到报警:{0}, EDnaValue={1}", snap.AlertText, snap.AlertValue); } //更新DictBJ dictWtBJ[key] = results[i]; } catch (Exception ex) { logger.ErrorFormat("处理风机报警时异常:{0}", ex.Message); //System.Threading.Thread.Sleep(1000); } } } public void IFixBJMonitor() { if (dictIFixBJ == null) { dictIFixBJ = CreateDictIFixBJ2(); arrFixPoints = dictIFixBJ.Keys.ToArray(); } if (dictIFixBJ == null || dictIFixBJ.Count == 0) return; //string[] narr = arrFixPoints;// dictIFixBJ.Keys.ToArray(); int len = arrFixPoints.Length; for(int j=0;j len) { narr = new string[len - j]; Array.Copy(arrFixPoints, j, narr, 0, len - j); } else { narr = new string[200]; Array.Copy(arrFixPoints, j, narr, 0, 200); } #region 从实时库中取点比较变化 int currentTime = CommonMethod.ConvertDateTimeInt(DateTime.Now); eDOSApi.HISTORY_VALUE[] results = eDos.GetRTValueList(narr); for (int i = 0; i < results.Length; i++) { try { int valueTime = results[i].tTime; //有效时间外的值不处理,单位秒, 有效时间暂定义为1小时 //if (currentTime - valueTime > 60 * 60) // continue; //与上次扫描读取的值比对,如果没有更新则不处理 string key = narr[i]; int preTime = dictIFixBJ[key].tTime; if (valueTime <= preTime) continue; //触发报警 int ednaValue = (int)results[i].dValue; int preValue = (int)dictIFixBJ[key].dValue; if (ednaValue == preValue) continue; bool ifBJ = true;// ednaValue == 0 ? false : true; //if (ifBJ == false) // continue; AlertSnap snap = AlertSnapRepository.GetIFixAlertSnap(key); if (snap == null) { IFixBJTestingPointDI ftpd = DataCache.Instance.DictFixTP[key]; snap = AlertSnapFactory.CreateAlertSnap(ftpd); } snap.AlertValue = ednaValue; //double interval = 0; //if (snap.IsOpened) //{ // TimeSpan ts = DateTime.Now.Subtract((DateTime)snap.LastUpdateTime); // interval = ts.TotalHours; // //if (interval < 1) // // continue; //} if (ifBJ == true) { snap.IsOpened = true; snap.DataInfo = string.Format("{0}={1}", snap.TestingPointKey, ednaValue.ToString()); snap.LastUpdatePerson = "system"; snap.LastUpdateTime = DateTime.Now; if (snap.IsConfirmed) snap.IsConfirmed = false; } else { snap.IsOpened = false; snap.DataInfo = string.Format("{0}={1}", snap.TestingPointKey, ednaValue.ToString()); snap.IsConfirmed = false; snap.LastClosePerson = "system"; snap.LastCloseTime = DateTime.Now; } AlertSnapRepository.SaveAlertSnap(snap); if (ifBJ) logger.InfoFormat("收到报警:{0}, key={1}", snap.AlertText, snap.TestingPointKey); else { logger.InfoFormat("解除报警:{0}, key={1}", snap.AlertText, snap.TestingPointKey); } //} //更新DictBJ dictIFixBJ[key] = results[i]; } catch (Exception ex) { logger.ErrorFormat("处理升压站报警时异常:{0}", ex.Message); //System.Threading.Thread.Sleep(1000); continue; } } #endregion } } private IDictionary CreateDictWindturbineBJ() { IDictionary dictBJ = new Dictionary(); IList lstWTA = DataCache.Instance.WtBJPoints; foreach (WindTurbineTestingPointAI wai in lstWTA) { string pointA = CommonMethod.BuildPointId(wai.Id); if (string.IsNullOrWhiteSpace(pointA)) continue; dictBJ.Add(pointA, new eDOSApi.HISTORY_VALUE()); } return dictBJ; } private IDictionary CreateDictIFixBJ() { IDictionary dictBJ = new Dictionary(); IList lstWTA = DataCache.Instance.IFixBJPoints; foreach (IFixBJTestingPointDI wai in lstWTA) { //string pointA = CommonMethod.BuildPointId(wai.Id); //if (string.IsNullOrWhiteSpace(pointA)) // continue; var hv = new eDOSApi.HISTORY_VALUE(); AlertSnap snap = AlertSnapRepository.GetIFixAlertSnap(wai.PointKey); if (snap != null) { hv.dValue = snap.AlertValue; } dictBJ.Add(wai.PointKey, hv); } return dictBJ; } Dictionary dictSnapValue = null; private Dictionary CreateDictSnapValue() { dictSnapValue = new Dictionary(); var lst = AlertSnapRepository.GetTestingPointAlertSnap(); if (lst != null && lst.Count > 0) { foreach(var snap in lst) { if (dictSnapValue.ContainsKey(snap.TestingPointKey) == false) dictSnapValue.Add(snap.TestingPointKey, snap.AlertValue); } } return dictSnapValue; } private IDictionary CreateDictIFixBJ2() { if (dictSnapValue == null) dictSnapValue = CreateDictSnapValue(); //var obj = AlertSnapRepository.GetAlertSnaps() IDictionary dictBJ = new Dictionary(); IList lstWTA = DataCache.Instance.IFixBJPoints; foreach (IFixBJTestingPointDI wai in lstWTA) { //string pointA = CommonMethod.BuildPointId(wai.Id); //if (string.IsNullOrWhiteSpace(pointA)) // continue; var hv = new eDOSApi.HISTORY_VALUE(); if (dictSnapValue.ContainsKey(wai.PointKey)) hv.dValue = dictSnapValue[wai.PointKey]; //AlertSnap snap = AlertSnapRepository.GetIFixAlertSnap(wai.PointKey); //if (snap != null) //{ // hv.dValue = snap.AlertValue; //} dictBJ.Add(wai.PointKey, hv); } return dictBJ; } } }