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 interpreters; public IList Interpreters { get { if (interpreters == null) interpreters = CreateInterpreters(); return interpreters; } } private IList CreateInterpreters() { IList lstRule = AlertRuleRepository.GetEnabledAlertRules(); if (lstRule != null && lstRule.Count > 0) { IList lstInterpreter = new List(); 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 GetInterpretersByWindturbineId(string wid) { return InterpreterFactory.Instance.Interpreters.Where(q => q.Category == "1" && q.Windturbines.Contains(wid)).ToList(); } public IList GetInterpretersByStationId(string id) { return InterpreterFactory.Instance.Interpreters.Where(q => q.Category == "2" && q.Stations.Contains(id)).ToList(); } public IList GetInterpretersByProjectId(string id) { return InterpreterFactory.Instance.Interpreters.Where(q => q.Category == "3" && q.Projects.Contains(id)).ToList(); } public IList GetInterpretersByLineId(string id) { return InterpreterFactory.Instance.Interpreters.Where(q => q.Category == "4" && q.Lines.Contains(id)).ToList(); } public IList GetInterpretersByElectricalId(string id) { return InterpreterFactory.Instance.Interpreters.Where(q => q.Category == "5" && q.Electricals.Contains(id)).ToList(); } } }