InterpreterFactory.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using GDNXFD.Data;
  7. using GDNXFD.Data.Repositories;
  8. using log4net;
  9. namespace GDNXFD.Alert.Interpreter
  10. {
  11. public class InterpreterFactory
  12. {
  13. private ILog logger = LogManager.GetLogger("AppLog");
  14. private InterpreterFactory()
  15. {
  16. }
  17. public static InterpreterFactory Instance
  18. {
  19. get { return SingletonCreator.instance; }
  20. }
  21. class SingletonCreator
  22. {
  23. internal static readonly InterpreterFactory instance = new InterpreterFactory();
  24. }
  25. private IDataProvider dataProvider;
  26. public IDataProvider DataProvider
  27. {
  28. get
  29. {
  30. if (dataProvider == null)
  31. dataProvider = new DefaultDataProvider();
  32. return dataProvider;
  33. }
  34. }
  35. private IList<AlertRuleInterpreter> interpreters;
  36. public IList<AlertRuleInterpreter> Interpreters
  37. {
  38. get
  39. {
  40. if (interpreters == null)
  41. interpreters = CreateInterpreters();
  42. return interpreters;
  43. }
  44. }
  45. private IList<AlertRuleInterpreter> CreateInterpreters()
  46. {
  47. IList<AlertRule> lstRule = AlertRuleRepository.GetEnabledAlertRules();
  48. if (lstRule != null && lstRule.Count > 0)
  49. {
  50. IList<AlertRuleInterpreter> lstInterpreter = new List<AlertRuleInterpreter>();
  51. foreach (AlertRule ar in lstRule)
  52. {
  53. try
  54. {
  55. ExpressionCompilerResults er = ExpressionCompiler.CompileExpression(ar);
  56. if (er.HasErrors)
  57. {
  58. logger.ErrorFormat("规则无效!id={0}, expression={1}", ar.Id, ar.Expression);
  59. }
  60. else if (er.ExpInterpreter != null)
  61. lstInterpreter.Add(er.ExpInterpreter);
  62. }
  63. catch (Exception ex)
  64. {
  65. logger.ErrorFormat("规则生成失败!", ex.Message);
  66. }
  67. }
  68. return lstInterpreter;
  69. }
  70. return null;
  71. }
  72. public void ReloadInterpreters()
  73. {
  74. interpreters = null;
  75. }
  76. public IList<AlertRuleInterpreter> GetInterpretersByWindturbineId(string wid)
  77. {
  78. return InterpreterFactory.Instance.Interpreters.Where(q => q.Category == "1" && q.Windturbines.Contains(wid)).ToList();
  79. }
  80. public IList<AlertRuleInterpreter> GetInterpretersByStationId(string id)
  81. {
  82. return InterpreterFactory.Instance.Interpreters.Where(q => q.Category == "2" && q.Stations.Contains(id)).ToList();
  83. }
  84. public IList<AlertRuleInterpreter> GetInterpretersByProjectId(string id)
  85. {
  86. return InterpreterFactory.Instance.Interpreters.Where(q => q.Category == "3" && q.Projects.Contains(id)).ToList();
  87. }
  88. public IList<AlertRuleInterpreter> GetInterpretersByLineId(string id)
  89. {
  90. return InterpreterFactory.Instance.Interpreters.Where(q => q.Category == "4" && q.Lines.Contains(id)).ToList();
  91. }
  92. public IList<AlertRuleInterpreter> GetInterpretersByElectricalId(string id)
  93. {
  94. return InterpreterFactory.Instance.Interpreters.Where(q => q.Category == "5" && q.Electricals.Contains(id)).ToList();
  95. }
  96. }
  97. }