Esempio n. 1
0
        private bool WillProgramRun()
        {
            bool isConditionSatisfied = false;

            // evaluate and get result from the code
            lock (programBlock.OperationLock)
                try
                {
                    programBlock.WillRun = false;
                    //
                    var result = programBlock.EvaluateCondition();
                    if (result != null && result.Exception != null)
                    {
                        // runtime error occurred, script is being disabled
                        // so user can notice and fix it
                        List <ProgramError> error = new List <ProgramError>()
                        {
                            programBlock.GetFormattedError(result.Exception, true)
                        };
                        programBlock.ScriptErrors = JsonConvert.SerializeObject(error);
                        programBlock.IsEnabled    = false;
                        homegenie.ProgramManager.RaiseProgramModuleEvent(programBlock, Properties.RuntimeError, "TC: " + result.Exception.Message.Replace('\n', ' ').Replace('\r', ' '));
                    }
                    else
                    {
                        isConditionSatisfied = (result != null ? (bool)result.ReturnValue : false);
                    }
                    //
                    bool lastResult = programBlock.LastConditionEvaluationResult;
                    programBlock.LastConditionEvaluationResult = isConditionSatisfied;
                    //
                    if (programBlock.ConditionType == ConditionType.OnSwitchTrue)
                    {
                        isConditionSatisfied = (isConditionSatisfied == true && isConditionSatisfied != lastResult);
                    }
                    else if (programBlock.ConditionType == ConditionType.OnSwitchFalse)
                    {
                        isConditionSatisfied = (isConditionSatisfied == false && isConditionSatisfied != lastResult);
                    }
                    else if (programBlock.ConditionType == ConditionType.OnTrue || programBlock.ConditionType == ConditionType.Once)
                    {
                        // noop
                    }
                    else if (programBlock.ConditionType == ConditionType.OnFalse)
                    {
                        isConditionSatisfied = !isConditionSatisfied;
                    }
                }
                catch (Exception ex)
                {
                    // a runtime error occured
                    if (!ex.GetType().Equals(typeof(System.Reflection.TargetException)))
                    {
                        List <ProgramError> error = new List <ProgramError>()
                        {
                            programBlock.GetFormattedError(ex, true)
                        };
                        programBlock.ScriptErrors = JsonConvert.SerializeObject(error);
                        programBlock.IsEnabled    = false;
                        homegenie.ProgramManager.RaiseProgramModuleEvent(programBlock, Properties.RuntimeError, "TC: " + ex.Message.Replace('\n', ' ').Replace('\r', ' '));
                    }
                }
            return(isConditionSatisfied && programBlock.IsEnabled);
        }
Esempio n. 2
0
        private bool WillProgramRun()
        {
            bool isConditionSatisfied = false;

            // evaluate and get result from the code
            lock (ProgramBlock.OperationLock)
            {
                try
                {
                    ProgramBlock.WillRun = false;
                    //
                    var result = ProgramBlock.EvaluateCondition();
                    if (result != null && result.Exception != null)
                    {
                        // runtime error occurred, script is being disabled
                        // so user can notice and fix it
                        var error = new List <ProgramError> {
                            ProgramBlock.GetFormattedError(result.Exception, true)
                        };
                        ProgramBlock.ScriptErrors = JsonConvert.SerializeObject(error);
                        _log.Error(result.Exception, "Error while evaluating condition in program {0}",
                                   ProgramBlock.Address);
                        Homegenie.ProgramManager.RaiseProgramModuleEvent(ProgramBlock, Properties.RuntimeError,
                                                                         PrepareExceptionMessage(CodeBlockEnum.TC, result.Exception));

                        TryToAutoRestart();
                    }
                    else
                    {
                        isConditionSatisfied = (result != null ? (bool)result.ReturnValue : false);
                    }
                    //
                    bool lastResult = ProgramBlock.LastConditionEvaluationResult;
                    ProgramBlock.LastConditionEvaluationResult = isConditionSatisfied;
                    //
                    if (ProgramBlock.ConditionType == ConditionType.OnSwitchTrue)
                    {
                        isConditionSatisfied = (isConditionSatisfied == true && isConditionSatisfied != lastResult);
                    }
                    else if (ProgramBlock.ConditionType == ConditionType.OnSwitchFalse)
                    {
                        isConditionSatisfied = (isConditionSatisfied == false && isConditionSatisfied != lastResult);
                    }
                    else if (ProgramBlock.ConditionType == ConditionType.OnTrue ||
                             ProgramBlock.ConditionType == ConditionType.Once)
                    {
                        // noop
                    }
                    else if (ProgramBlock.ConditionType == ConditionType.OnFalse)
                    {
                        isConditionSatisfied = !isConditionSatisfied;
                    }
                }
                catch (Exception ex)
                {
                    // a runtime error occured
                    if (!ex.GetType().Equals(typeof(System.Reflection.TargetException)) &&
                        !ex.GetType().Equals(typeof(ThreadAbortException)))
                    {
                        List <ProgramError> error = new List <ProgramError>()
                        {
                            ProgramBlock.GetFormattedError(ex, true)
                        };
                        ProgramBlock.ScriptErrors = JsonConvert.SerializeObject(error);
                        Homegenie.ProgramManager.RaiseProgramModuleEvent(ProgramBlock, Properties.RuntimeError,
                                                                         PrepareExceptionMessage(CodeBlockEnum.TC, ex));
                        TryToAutoRestart();
                    }
                }
            }

            return(isConditionSatisfied && ProgramBlock.IsEnabled);
        }