123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using GDNXFD.Data;
- using GDNXFD.Data.Repositories;
- using log4net;
- namespace GDNXFD.Alert.Interpreter
- {
- public class InterpreterFactory
- {
- private ILog logger = LogManager.GetLogger("AppLog");
- private InterpreterFactory()
- {
- }
- public static InterpreterFactory Instance
- {
- get { return SingletonCreator.instance; }
- }
- class SingletonCreator
- {
- internal static readonly InterpreterFactory instance = new InterpreterFactory();
- }
- private IDataProvider dataProvider;
- public IDataProvider DataProvider
- {
- get
- {
- if (dataProvider == null)
- dataProvider = new DefaultDataProvider();
- return dataProvider;
- }
- }
- private IList<AlertRuleInterpreter> interpreters;
- public IList<AlertRuleInterpreter> Interpreters
- {
- get
- {
- if (interpreters == null)
- interpreters = CreateInterpreters();
- return interpreters;
- }
- }
- private IList<AlertRuleInterpreter> CreateInterpreters()
- {
- IList<AlertRule> lstRule = AlertRuleRepository.GetEnabledAlertRules();
- if (lstRule != null && lstRule.Count > 0)
- {
- IList<AlertRuleInterpreter> lstInterpreter = new List<AlertRuleInterpreter>();
- foreach (AlertRule ar in lstRule)
- {
- try
- {
- ExpressionCompilerResults er = ExpressionCompiler.CompileExpression(ar);
- if (er.HasErrors)
- {
- logger.ErrorFormat("规则无效!id={0}, expression={1}", ar.Id, ar.Expression);
- }
- else if (er.ExpInterpreter != null)
- lstInterpreter.Add(er.ExpInterpreter);
- }
- catch (Exception ex)
- {
- logger.ErrorFormat("规则生成失败!", ex.Message);
- }
- }
- return lstInterpreter;
- }
- return null;
- }
- public void ReloadInterpreters()
- {
- interpreters = null;
- }
- public IList<AlertRuleInterpreter> GetInterpretersByWindturbineId(string wid)
- {
- return InterpreterFactory.Instance.Interpreters.Where(q => q.Category == "1" && q.Windturbines.Contains(wid)).ToList();
- }
- public IList<AlertRuleInterpreter> GetInterpretersByStationId(string id)
- {
- return InterpreterFactory.Instance.Interpreters.Where(q => q.Category == "2" && q.Stations.Contains(id)).ToList();
- }
- public IList<AlertRuleInterpreter> GetInterpretersByProjectId(string id)
- {
- return InterpreterFactory.Instance.Interpreters.Where(q => q.Category == "3" && q.Projects.Contains(id)).ToList();
- }
- public IList<AlertRuleInterpreter> GetInterpretersByLineId(string id)
- {
- return InterpreterFactory.Instance.Interpreters.Where(q => q.Category == "4" && q.Lines.Contains(id)).ToList();
- }
- public IList<AlertRuleInterpreter> GetInterpretersByElectricalId(string id)
- {
- return InterpreterFactory.Instance.Interpreters.Where(q => q.Category == "5" && q.Electricals.Contains(id)).ToList();
- }
- }
- }
|