Пример #1
0
 public void DeleteRule(int ruleId)
 {
     using (var rulesDb = new RulesDbEntities())
     {
         rulesDb.Rule.Remove(rulesDb.Rule.FirstOrDefault(r => r.Id == ruleId));
         rulesDb.SaveChanges();
     }
 }
Пример #2
0
        public IEnumerable<global::Scheduler.Types.Rule> GetRules()
        {
            List<global::Scheduler.Types.Rule> rules;

            using (var rulesDb = new RulesDbEntities())
            {
                rulesDb.Database.Connection.Open();

                rules = rulesDb.Rule.Select(r => new global::Scheduler.Types.Rule
                {
                    Name = r.Name,
                    Id = r.Id,
                    Enabled = r.Enabled,
                    IfConditions =
                        rulesDb.IfCondition.Where(i => i.RuleId == r.Id)
                            .Select(ic => new global::Scheduler.Types.IfCondition
                            {
                                Id = ic.Id,
                                Text = ic.Text,
                                EqualsText = ic.EqualsText,
                                Type = ic.Type,
                                EqualsType = ic.EqualsType,
                                Recurs = ic.Recurs,
                                TokenHeader = ic.TokenHeader
                            }).ToList(),
                    ThenActions =
                        rulesDb.ThenAction.Where(t => t.RuleId == r.Id)
                            .Select(a => new global::Scheduler.Types.ThenAction
                            {
                                Id = a.Id,
                                Type = a.Type,
                                ThenActionParams =
                                    rulesDb.ThenActionParams.Where(ap => ap.ThenActionId == a.Id)
                                        .Select(p => new ThenActionParam
                                        {
                                            Id = p.Id,
                                            Name = p.Name,
                                            Text = p.Text
                                        }).ToList()
                            }).ToList()
                }).ToList();

                rulesDb.Database.Connection.Close();
            }

            return rules;
        }
Пример #3
0
        public void LoadSchedules()
        {
            using (var rulesDb = new RulesDbEntities())
            {
                foreach (var rule in rulesDb.Rule)
                {
                    var conditions = rulesDb.IfCondition.Where(i => i.RuleId == rule.Id);
                    var then = rulesDb.ThenAction.FirstOrDefault(t => t.RuleId == rule.Id);

                    if (!conditions.Any() || then == null) continue;

                    foreach (var condition in conditions)
                    {
                        var ifCondition = condition.Text;
                        var ifEquals = condition.EqualsText;

                        TimeSpan? ifRecurs = null;

                        if (!string.IsNullOrEmpty(condition.Recurs))
                        {
                            TimeSpan safeTimeSpan;

                            TimeSpan.TryParse(condition.Recurs, out safeTimeSpan);

                            ifRecurs = safeTimeSpan == TimeSpan.Zero ? new TimeSpan(0, 10, 0) : safeTimeSpan;
                        }

                        if (ifCondition == null) continue;

                        var scheduleName = rule.Name;

                        var schedule = new Schedule(scheduleName, () => DateTime.Now.TimeOfDay, false)
                        {
                            Enabled = rule.Enabled,
                            Recurs = ifRecurs
                        };

                        Func<TestResult<object>> test = null;
                        Func<TimeSpan> runTime;

                        switch (condition.Type)
                        {
                            case "builtIn":
                                switch (condition.Text)
                                {
                                    case "Sunrise":
                                        runTime = new WeatherService().GetSunrise;
                                        break;
                                    case "Sunset":
                                        runTime = new WeatherService().GetSunset;
                                        break;
                                    case "IPChanged":
                                        test = IPChanged;
                                        runTime = () => DateTime.Now.TimeOfDay;
                                        break;
                                    case "WeatherAlert":
                                        if (ifEquals == null)
                                        {
                                            runTime = () => TimeSpan.Zero;
                                            break;
                                        }
                                        if (_scheduledProcesses.All(s => s.Name != scheduleName))
                                        {
                                            test = () => WeatherAlert((condition.EqualsText.Split(',')));
                                            runTime = () => DateTime.Now.TimeOfDay;
                                        }
                                        else
                                        {
                                            runTime = () => TimeSpan.Zero;
                                        }

                                        break;
                                    default:
                                        runTime = () => TimeSpan.Zero;
                                        break;
                                }
                                break;
                            case "time":
                                try
                                {
                                    runTime = () => DateTime.Parse(condition.Text).TimeOfDay;
                                }
                                catch (Exception)
                                {
                                    runTime = () => TimeSpan.Zero;
                                }
                                break;
                            case "serviceMethod":
                                runTime = () => DateTime.Now.TimeOfDay;
                                if (ifEquals != null)
                                {
                                    test = () => ServiceValueEquals(condition.Text, condition.Type, condition.EqualsText, condition.TokenHeader);
                                }
                                break;
                            default:
                                runTime = () => TimeSpan.Zero;
                                break;
                        }

                        schedule.RunTime = runTime;
                        schedule.Test = test;

                        var thenActions = rulesDb.ThenAction.Where(a => a.RuleId == rule.Id);

                        var actionFuncs = new List<FunctionDef>();

                        foreach (var thenAction in thenActions)
                        {
                            var thenActionParams = rulesDb.ThenActionParams.Where(p => p.ThenActionId == thenAction.Id);

                            var actionType = thenAction.Type;

                            var parameters = thenActionParams.ToDictionary(p => p.Name, p => p.Text);

                            switch (actionType)
                            {
                                case "serviceMethod":

                                    actionFuncs.Add(new FunctionDef { Function = ServiceMethod, Parameters = parameters });

                                    break;
                            }
                        }

                        schedule.Actions = new List<ScheduleAction>();

                        foreach (var action in actionFuncs)
                        {
                            schedule.Actions.Add(new ScheduleAction { ActionDef = action });
                        }

                        schedule.OnScheduledProcessError += ScheduleProcessorOnScheduledProcessError;
                        schedule.OnScheduledProcessProgress += ScheduleProcessorOnScheduledProcessProgress;

                        _scheduledProcesses.Add(schedule);
                    }
                }
            }
        }
Пример #4
0
        public int UpsertRule(global::Scheduler.Types.Rule rule)
        {
            using (var rulesDb = new RulesDbEntities())
            {
                rulesDb.Database.Connection.Open();

                var upsertRule = rule.Id == 0
                    ? rulesDb.Rule.Add(new Rule())
                    : rulesDb.Rule.FirstOrDefault(r => r.Id == rule.Id);

                if (upsertRule == null) return 0;

                upsertRule.Enabled = rule.Enabled;
                upsertRule.Name = rule.Name;

                rulesDb.SaveChanges();

                foreach (var ifCondition in rule.IfConditions)
                {
                    var upsertIfCondition = rule.Id == 0
                        ? rulesDb.IfCondition.Add(new IfCondition())
                        : rulesDb.IfCondition.FirstOrDefault(i => i.Id == ifCondition.Id);

                    if (upsertIfCondition == null) return 0;

                    upsertIfCondition.RuleId = upsertRule.Id;
                    upsertIfCondition.Recurs = ifCondition.Recurs;
                    upsertIfCondition.EqualsText = ifCondition.EqualsText;
                    upsertIfCondition.EqualsType = ifCondition.EqualsType;
                    upsertIfCondition.Text = ifCondition.Text;
                    upsertIfCondition.Type = ifCondition.Type;
                    upsertIfCondition.TokenHeader = ifCondition.TokenHeader;

                    rulesDb.SaveChanges();
                }

                foreach (var thenAction in rule.ThenActions)
                {
                    var upsertThenAction = rule.Id == 0
                        ? rulesDb.ThenAction.Add(new ThenAction())
                        : rulesDb.ThenAction.FirstOrDefault(a => a.Id == thenAction.Id);

                    if (upsertThenAction == null) return 0;

                    upsertThenAction.RuleId = upsertRule.Id;
                    upsertThenAction.Type = thenAction.Type;

                    rulesDb.SaveChanges();

                    foreach (var thenActionParam in thenAction.ThenActionParams)
                    {
                        var upsertThenActionParam = rule.Id == 0
                            ? rulesDb.ThenActionParams.Add(new ThenActionParams())
                            : rulesDb.ThenActionParams.FirstOrDefault(p => p.Id == thenActionParam.Id);

                        if (upsertThenActionParam == null) return 0;

                        upsertThenActionParam.Name = thenActionParam.Name;
                        upsertThenActionParam.Text = thenActionParam.Text;
                        upsertThenActionParam.ThenActionId = upsertThenAction.Id;

                        rulesDb.SaveChanges();
                    }
                }

                rulesDb.Database.Connection.Close();
                return upsertRule.Id;
            }
        }