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);
                    }
                }
            }
        }
        private static TestResult<object> WeatherAlert(IEnumerable<string> alertTypes)
        {
            var alerts = new WeatherService().GetAlerts().ToList();

            var filteredAlert = new Dictionary<string, IEnumerable<Alert>>();

            foreach (var alertType in alertTypes)
            {
                if (alerts.Any(a => a.Type == (Enums.AlertType)Enum.Parse(typeof(Enums.AlertType), alertType, true)))
                {
                    var x =
                    alerts.Where(a => a.Type == (Enums.AlertType)Enum.Parse(typeof(Enums.AlertType), alertType, true))
                        .ToList();

                    filteredAlert.Add(alertType, x);
                }
            }

            return new TestResult<object>
            {
                ConditionMet = filteredAlert.Any(),
                Results =
                    filteredAlert.SelectMany(alertResponse => alertResponse.Value).ToList()
            };
        }
 public void GetCurrentObservation()
 {
     var current = new WeatherService().GetCurrentObservation();
 }
 public void GetForecast()
 {
     var forecast = new WeatherService().GetForecast();
 }
 public void GetAlerts()
 {
     var alerts = new WeatherService().GetAlerts();
 }