Esempio n. 1
0
        private void SetErrorInfo(Exception ex, Automation auto, ErrorType type)
        {
            Globals.LastError      = ex;
            Globals.LastAutomation = auto;
            Globals.IsError        = true;
            auto.ErrorType         = type;

            AutomatronLogger.Log(string.Format("{0} caused an exception: {1}\n{2}", auto.Name, ex.Message, ex));
        }
Esempio n. 2
0
        private IEnumerator ExecuteAutomationsAsync()
        {
            AutomatronLogger.Start(Name);

            var autos = GetControls <Automation>();

            foreach (var item in autos)
            {
                item.Reset();
            }

            Globals.LastError      = null;
            Globals.LastAutomation = null;
            Globals.IsError        = false;
            Globals.IsExecuting    = true;

            var errors = GetControls <AutomationError>();

            foreach (var item in errors)
            {
                item.Remove();
            }

            yield return(null);

            var entries = new List <QueueStart>()
            {
                entryPoint
            };

            var entryPoints = GetControls <QueueStart>();

            foreach (var item in entryPoints)
            {
                if (!entries.Contains(item))
                {
                    entries.Add(item);
                }
            }

            foreach (var item in entries)
            {
                var automations = item.GetNextAutomations();
                var loops       = new List <LoopableAutomation>();

                while (true)
                {
                    if (automations == null || automations.Count == 0)
                    {
                        break;
                    }

                    foreach (var auto in automations)
                    {
                        AutomatronLogger.Log(string.Format("Start {0}", auto.Name));

                        auto.GetDependencies();
                        if (Globals.IsError)
                        {
                            break;
                        }

                        yield return(null);

                        if (auto is LoopableAutomation)
                        {
                            var l = (LoopableAutomation)auto;
                            if (!loops.Contains(l))
                            {
                                try {
                                    auto.PreExecute();
                                } catch (Exception ex) {
                                    SetErrorInfo(ex, auto, ErrorType.PreExecute);
                                    break;
                                }

                                loops.Add(l);
                            }
                            else
                            {
                                l.MoveNext();
                            }

                            l.ResetLoop();
                        }
                        else
                        {
                            try {
                                auto.PreExecute();
                            } catch (Exception ex) {
                                SetErrorInfo(ex, auto, ErrorType.PreExecute);
                                break;
                            }
                        }

                        if (AutomatronSettings.FocusActiveAutomation)
                        {
                            LookAtAutomationSmooth(auto);
                        }

                        IEnumerator routine = null;

                        try {
                            routine = auto.Execute();
                        } catch (Exception ex) {
                            SetErrorInfo(ex, auto, ErrorType.Execute);
                            break;
                        }

                        while (true)
                        {
                            var moveNext = false;

                            try {
                                moveNext = routine.MoveNext();
                            } catch (Exception ex) {
                                SetErrorInfo(ex, auto, ErrorType.Execute);
                                break;
                            }

                            if (!moveNext)
                            {
                                break;
                            }

                            yield return(routine.Current);
                        }

                        if (Globals.IsError)
                        {
                            break;
                        }
                        if (!(auto is LoopableAutomation))
                        {
                            try {
                                auto.PostExecute();
                            } catch (Exception ex) {
                                SetErrorInfo(ex, auto, ErrorType.PostExecute);
                                break;
                            }

                            auto.Progress = 1;
                        }

                        auto.HasRun = true;
                        AutomatronLogger.Log(string.Format("End {0}", auto.Name));
                    }

                    if (Globals.IsError)
                    {
                        break;
                    }

                    automations = automations[automations.Count - 1].GetNextAutomations();

                    if (automations.Count > 0 && loops.Count > 0)
                    {
                        var l = loops[loops.Count - 1];
                        if (l.IsDone())
                        {
                            try {
                                l.PostExecute();
                            } catch (Exception ex) {
                                SetErrorInfo(ex, l, ErrorType.PostExecute);
                                break;
                            }
                            l.Progress = 1;
                            loops.Remove(l);
                        }
                    }
                    else
                    {
                        while (automations.Count == 0 && loops.Count > 0)
                        {
                            var l = loops[loops.Count - 1];
                            if (l.IsDone())
                            {
                                try {
                                    l.PostExecute();
                                } catch (Exception ex) {
                                    SetErrorInfo(ex, l, ErrorType.PostExecute);
                                    break;
                                }
                                l.Progress  = 1;
                                automations = l.GetNextAutomations();
                                loops.Remove(l);
                            }
                            else
                            {
                                l.GetAutomations(ref automations, false);
                            }

                            yield return(null);
                        }
                    }

                    yield return(null);
                }

                if (Globals.IsError)
                {
                    break;
                }
                yield return(null);
            }

            if (Globals.IsError)
            {
                LookAtAutomationSmooth(Globals.LastAutomation);
                AddControl(new AutomationError(Globals.LastError));
            }
            else
            {
                ShowNotification("Automatron executed");
            }

            Globals.IsExecuting = false;

            if (AutomatronSettings.AutoLog)
            {
                AutomatronLogger.Save();
            }

            yield break;
        }