WindTurbineCalculator.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using EDosApiWrapper;
  2. using GDNXFD.Data;
  3. using GDNXFD.Data.Repositories;
  4. using log4net;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. namespace GDNXFD.Alert.Interpreter
  9. {
  10. public class WindTurbineCalculator
  11. {
  12. private ILog logger = LogManager.GetLogger("AppLog");
  13. private WindTurbine windTurbine;
  14. private EDosProxy eDos;
  15. public WindTurbineCalculator(WindTurbine wt)
  16. {
  17. windTurbine = wt;
  18. eDos = EDosProxyFactory.Instance.Proxy;
  19. }
  20. /// <summary>
  21. /// 计算风机所有关联规则的值,并保存
  22. /// </summary>
  23. /// <returns>风机报警测点的值,关联到AlertRule表</returns>
  24. public void Calculate()
  25. {
  26. IList<AlertRuleInterpreter> interpreters = InterpreterFactory.Instance.GetInterpretersByWindturbineId(windTurbine.Id);
  27. if (interpreters != null && interpreters.Count > 0)
  28. {
  29. logger.Debug("适用规则数:" + interpreters.Count);
  30. foreach (AlertRuleInterpreter ari in interpreters)
  31. {
  32. logger.Debug("处理报警规则:" + ari.AlertRule.Expression);
  33. ari.DataInfo = "";
  34. bool ruleResult = ari.Interpret(windTurbine.Id, AlertObjectType.WindTurbine);
  35. AlertSnap snap = AlertSnapRepository.GetAlertSnap(AlertObjectType.WindTurbine, windTurbine.Id, ari.AlertRule.EdnaValue);
  36. if (ruleResult)
  37. {
  38. double interval = 0;
  39. if (snap == null)
  40. {
  41. snap = AlertSnapFactory.CreateAlertSnap(ari.AlertRule, windTurbine);
  42. }
  43. else if (snap.IsOpened)
  44. {
  45. TimeSpan ts = DateTime.Now.Subtract((DateTime)snap.LastUpdateTime);
  46. interval = ts.TotalHours;
  47. }
  48. ;
  49. if (snap.IsOpened == false || interval > 1)
  50. {
  51. snap.IsOpened = true;
  52. snap.DataInfo = CommonMethod.StringCopy(ari.DataInfo, 2000);
  53. snap.LastUpdatePerson = "system";
  54. snap.LastUpdateTime = DateTime.Now;
  55. if (snap.IsConfirmed == true && interval > 8)
  56. snap.IsConfirmed = false;
  57. AlertSnapRepository.SaveAlertSnap(snap);
  58. //写实时库
  59. string key = GetWindTurbineAlertPoint(windTurbine.Id);
  60. logger.InfoFormat("写报警测点,key={0}, value={1}", key, ari.AlertRule.EdnaValue);
  61. eDos.SendSinglePoint(key, ari.AlertRule.EdnaValue);
  62. logger.InfoFormat("触发报警!风机:{0},{1},规则:{2},{3}",
  63. windTurbine.Id, windTurbine.Name, ari.AlertRule.Id, ari.AlertRule.Expression);
  64. }
  65. }
  66. else
  67. {
  68. //解除报警
  69. if (snap != null && snap.IsOpened)
  70. {
  71. snap.IsOpened = false;
  72. snap.DataInfo = CommonMethod.StringCopy(ari.DataInfo, 2000);
  73. snap.IsConfirmed = false;
  74. snap.LastClosePerson = "system";
  75. snap.LastCloseTime = DateTime.Now;
  76. AlertSnapRepository.SaveAlertSnap(snap);
  77. logger.InfoFormat("解除报警!风机:{0},{1},规则:{2},{3}",
  78. windTurbine.Id, windTurbine.Name, ari.AlertRule.Id, ari.AlertRule.Expression);
  79. }
  80. }
  81. }
  82. }
  83. }
  84. private string GetWindTurbineAlertPoint(string windTurbineId)
  85. {
  86. string ret;
  87. WindTurbineTestingPointAI point = DataCache.Instance.WindTurbineAlertPoints
  88. .Where(q => q.WindturbineId == windTurbineId)
  89. .FirstOrDefault();
  90. if (point == null)
  91. {
  92. ret = WindTurbineRepository.GetWindTurbineAIPointId(windTurbineId, "FJBJ3");
  93. if (ret == null)
  94. logger.InfoFormat("风机{0}没有首出报警测点", windTurbineId);
  95. }
  96. else
  97. ret = point.Id;
  98. return BuildPointId(ret);
  99. }
  100. private string BuildPointId(string PointID)
  101. {
  102. if (PointID == null)
  103. PointID = "";
  104. else
  105. {
  106. string[] ids = PointID.Split('.');
  107. if (ids != null && ids.Length == 3)
  108. {
  109. PointID = ids[1] + "_" + ids[2];
  110. }
  111. }
  112. return PointID;
  113. }
  114. }
  115. }