Пример #1
0
        public override MethodRunResult Setup()
        {
            MethodRunResult result = null;

            if (_scriptAssembly != null && CheckAppInstance())
            {
                result             = (MethodRunResult)_methodSetup.Invoke(_scriptInstance, null);
                result.ReturnValue = (bool)result.ReturnValue || ProgramBlock.WillRun;
            }
            return(result);
        }
Пример #2
0
        public MethodRunResult EvaluateCondition()
        {
            MethodRunResult result = null;

            if (appAssembly != null && CheckAppInstance())
            {
                result             = (MethodRunResult)methodEvaluateCondition.Invoke(assembly, null);
                result.ReturnValue = (bool)result.ReturnValue || programBlock.WillRun;
            }
            return(result);
        }
Пример #3
0
        public MethodRunResult Run(string options)
        {
            MethodRunResult result = null;

            if (appAssembly != null && CheckAppInstance())
            {
                result = (MethodRunResult)methodRun.Invoke(assembly, new object[1] {
                    options
                });
            }
            return(result);
        }
Пример #4
0
        public override MethodRunResult Run(string options)
        {
            MethodRunResult result = null;

            if (_scriptAssembly != null && CheckAppInstance())
            {
                result = (MethodRunResult)_methodRun.Invoke(_scriptInstance, new object[1] {
                    options
                });
            }
            return(result);
        }
Пример #5
0
        public MethodRunResult Run(string options)
        {
            var result = new MethodRunResult();

            try
            {
                ExecuteScript(programBlock.Commands);
            }
            catch (Exception e)
            {
                result.Exception = e;
            }
            return(result);
        }
Пример #6
0
        public override MethodRunResult Run(string options)
        {
            var result = new MethodRunResult();

            try
            {
                ExecuteScript(script.Commands);
            }
            catch (Exception e)
            {
                result.Exception = e;
            }
            return(result);
        }
Пример #7
0
        public MethodRunResult EvaluateCondition()
        {
            MethodRunResult result = null;

            result = new MethodRunResult();
            try
            {
                result.ReturnValue = EvaluateCondition(programBlock.Conditions);
            }
            catch (Exception e)
            {
                result.Exception = e;
            }
            return(result);
        }
Пример #8
0
        public override MethodRunResult Setup()
        {
            MethodRunResult result = null;

            result = new MethodRunResult();
            try
            {
                result.ReturnValue = EvaluateCondition(script.Conditions);
            }
            catch (Exception e)
            {
                result.Exception = e;
            }
            return(result);
        }
Пример #9
0
        public MethodRunResult Run(string options)
        {
            MethodRunResult result     = null;
            string          rubyScript = programBlock.ScriptSource;

            result = new MethodRunResult();
            try
            {
                scriptEngine.Execute(rubyScript, scriptScope);
            }
            catch (Exception e)
            {
                result.Exception = e;
            }
            return(result);
        }
Пример #10
0
        public MethodRunResult Run(string options)
        {
            var result = new MethodRunResult();

            result = new MethodRunResult();
            Homegenie.RaiseEvent(
                Domains.HomeGenie_System,
                Domains.HomeAutomation_HomeGenie_Automation,
                ProgramBlock.Address.ToString(),
                "Arduino Sketch Upload",
                "Arduino.UploadOutput",
                "Upload started"
                );
            string[] outputResult = ArduinoAppFactory.UploadSketch(Path.Combine(
                                                                       AppDomain.CurrentDomain.BaseDirectory,
                                                                       "programs",
                                                                       "arduino",
                                                                       ProgramBlock.Address.ToString()
                                                                       )).Split('\n');
            //
            for (int x = 0; x < outputResult.Length; x++)
            {
                if (!String.IsNullOrWhiteSpace(outputResult[x]))
                {
                    Homegenie.RaiseEvent(
                        Domains.HomeGenie_System,
                        Domains.HomeAutomation_HomeGenie_Automation,
                        ProgramBlock.Address.ToString(),
                        "Arduino Sketch",
                        "Arduino.UploadOutput",
                        outputResult[x]
                        );
                    Thread.Sleep(500);
                }
            }
            //
            Homegenie.RaiseEvent(
                Domains.HomeGenie_System,
                Domains.HomeAutomation_HomeGenie_Automation,
                ProgramBlock.Address.ToString(),
                "Arduino Sketch",
                "Arduino.UploadOutput",
                "Upload finished"
                );
            return(result);
        }
Пример #11
0
        public MethodRunResult Run(string options)
        {
            MethodRunResult result   = null;
            var             jsScript = initScript + ProgramBlock.ScriptSource;

            //scriptEngine.Options.AllowClr(false);
            result = new MethodRunResult();
            try
            {
                scriptEngine.Execute(jsScript);
            }
            catch (Exception e)
            {
                result.Exception = e;
            }
            return(result);
        }
Пример #12
0
        public MethodRunResult EvaluateCondition()
        {
            MethodRunResult result     = null;
            string          rubyScript = programBlock.ScriptCondition;

            result = new MethodRunResult();
            try
            {
                var sh = (scriptScope as dynamic).hg as ScriptingHost;
                scriptEngine.Execute(rubyScript, scriptScope);
                result.ReturnValue = sh.executeProgramCode || programBlock.WillRun;
            }
            catch (Exception e)
            {
                result.Exception = e;
            }
            return(result);
        }
Пример #13
0
        public MethodRunResult EvaluateCondition()
        {
            MethodRunResult result   = null;
            string          jsScript = initScript + ProgramBlock.ScriptCondition;

            result = new MethodRunResult();
            try
            {
                var sh = (scriptEngine.GetValue("hg").ToObject() as ScriptingHost);
                scriptEngine.Execute(jsScript);
                result.ReturnValue = sh.ExecuteProgramCode || ProgramBlock.WillRun;
            }
            catch (Exception e)
            {
                result.Exception = e;
            }
            return(result);
        }
Пример #14
0
        public override MethodRunResult Setup()
        {
            MethodRunResult result       = null;
            string          pythonScript = ProgramBlock.ScriptSetup;

            result = new MethodRunResult();
            try
            {
                var sh = (scriptScope as dynamic).hg as ScriptingHost;
                scriptEngine.Execute(pythonScript, scriptScope);
                result.ReturnValue = sh.ExecuteProgramCode || ProgramBlock.WillRun;
            }
            catch (Exception e)
            {
                result.Exception = e;
            }
            return(result);
        }
Пример #15
0
 public override MethodRunResult Run(string options)
 {
     var result = new MethodRunResult();
     HomeGenie.RaiseEvent(
         Domains.HomeGenie_System,
         Domains.HomeAutomation_HomeGenie_Automation,
         ProgramBlock.Address.ToString(),
         "Arduino Sketch Upload",
         "Arduino.UploadOutput",
         "Upload started"
     );
     string[] outputResult = ArduinoAppFactory.UploadSketch(Path.Combine(
         AppDomain.CurrentDomain.BaseDirectory,
         "programs",
         "arduino",
         ProgramBlock.Address.ToString()
     )).Split('\n');
     //
     foreach (var res in outputResult)
     {
         if (String.IsNullOrWhiteSpace(res)) continue;
         HomeGenie.RaiseEvent(
             Domains.HomeGenie_System,
             Domains.HomeAutomation_HomeGenie_Automation,
             ProgramBlock.Address.ToString(),
             "Arduino Sketch",
             "Arduino.UploadOutput",
             res
         );
         Thread.Sleep(500);
     }
     //
     HomeGenie.RaiseEvent(
         Domains.HomeGenie_System,
         Domains.HomeAutomation_HomeGenie_Automation,
         ProgramBlock.Address.ToString(),
         "Arduino Sketch",
         "Arduino.UploadOutput",
         "Upload finished"
     );
     return result;
 }
Пример #16
0
        // TODO: v1.1 !!!IMPORTANT!!! move thread allocation and starting to ProgramEngineBase.cs class
        public void Run(ProgramBlock program, string options)
        {
            if (program.IsRunning)
                return;

            if (program.Engine.ProgramThread != null)
            {
                program.Engine.Stop();
                program.IsRunning = false;
            }

            program.IsRunning = true;
            RaiseProgramModuleEvent(program, Properties.PROGRAM_STATUS, "Running");

            program.TriggerTime = DateTime.UtcNow;

            program.Engine.ProgramThread = new Thread(() =>
            {
                MethodRunResult result = null;
                try
                {
                    result = program.Run(options);
                }
                catch (Exception ex)
                {
                    result = new MethodRunResult();
                    result.Exception = ex;
                }
                //
                if (result != null && result.Exception != null && !result.Exception.GetType().Equals(typeof(System.Reflection.TargetException)))
                {
                    // runtime error occurred, script is being disabled
                    // so user can notice and fix it
                    List<ProgramError> error = new List<ProgramError>() { program.GetFormattedError(result.Exception, false) };
                    program.ScriptErrors = JsonConvert.SerializeObject(error);
                    program.IsEnabled = false;
                    RaiseProgramModuleEvent(program, Properties.RUNTIME_ERROR, "CR: " + result.Exception.Message.Replace('\n', ' ').Replace('\r', ' '));
                }
                program.IsRunning = false;
                program.Engine.ProgramThread = null;
                RaiseProgramModuleEvent(program, Properties.PROGRAM_STATUS, "Idle");
            });
            //
            if (program.ConditionType == ConditionType.Once)
            {
                program.IsEnabled = false;
            }
            //
            try
            {
                program.Engine.ProgramThread.Start();
            }
            catch
            {
                program.Engine.Stop();
                program.IsRunning = false;
                RaiseProgramModuleEvent(program, Properties.PROGRAM_STATUS, "Idle");
            }
            //
            //Thread.Sleep(100);
        }
Пример #17
0
        public void StartProgram(string options)
        {
            if (programBlock.IsRunning)
            {
                return;
            }

            // TODO: since if !program.IsRunning also thread should be null
            // TODO: so this is probably useless here and could be removed?
            if (programThread != null)
            {
                StopProgram();
            }

            programBlock.IsRunning = true;
            homegenie.ProgramManager.RaiseProgramModuleEvent(programBlock, Properties.ProgramStatus, "Running");

            programBlock.TriggerTime = DateTime.UtcNow;

            programThread = new Thread(() =>
            {
                try
                {
                    MethodRunResult result = null;
                    try
                    {
                        result = programBlock.Run(options);
                    }
                    catch (Exception ex)
                    {
                        result           = new MethodRunResult();
                        result.Exception = ex;
                    }
                    programThread          = null;
                    programBlock.IsRunning = false;
                    if (result != null && result.Exception != null && !result.Exception.GetType().Equals(typeof(System.Reflection.TargetException)))
                    {
                        // runtime error occurred, script is being disabled
                        // so user can notice and fix it
                        List <ProgramError> error = new List <ProgramError>()
                        {
                            programBlock.GetFormattedError(result.Exception, false)
                        };
                        programBlock.ScriptErrors = JsonConvert.SerializeObject(error);
                        programBlock.IsEnabled    = false;
                        homegenie.ProgramManager.RaiseProgramModuleEvent(programBlock, Properties.RuntimeError, "CR: " + result.Exception.Message.Replace('\n', ' ').Replace('\r', ' '));
                    }
                    homegenie.ProgramManager.RaiseProgramModuleEvent(programBlock, Properties.ProgramStatus, programBlock.IsEnabled ? "Idle" : "Stopped");
                }
                catch (ThreadAbortException)
                {
                    programThread          = null;
                    programBlock.IsRunning = false;
                    homegenie.ProgramManager.RaiseProgramModuleEvent(programBlock, Properties.ProgramStatus, "Interrupted");
                }
            });

            if (programBlock.ConditionType == ConditionType.Once)
            {
                programBlock.IsEnabled = false;
            }

            try
            {
                programThread.Start();
            }
            catch
            {
                StopProgram();
                homegenie.ProgramManager.RaiseProgramModuleEvent(programBlock, Properties.ProgramStatus, "Idle");
            }
        }
Пример #18
0
 public void Run(ProgramBlock program, string options)
 {
     if (program.IsRunning) return;
     //
     if (program.ProgramThread != null)
     {
         program.Stop();
         program.IsRunning = false;
     }
     //
     program.IsRunning = true;
     RaiseProgramModuleEvent(program, "Program.Status", "Running");
     //
     if (program.Type.ToLower() != "wizard")
     {
         if (program.Type.ToLower() == "csharp" && program.AppAssembly == null)
         {
             program.IsRunning = false;
         }
         else
         {
             program.TriggerTime = DateTime.UtcNow;
             program.ProgramThread = new Thread(() =>
             {
                 MethodRunResult result = null;
                 try
                 {
                     result = program.Run(options);
                 } catch (Exception ex) {
                     result = new MethodRunResult();
                     result.Exception = ex;
                 }
                 //
                 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>() { new ProgramError() {
                             CodeBlock = "CR",
                             Column = 0,
                             Line = 0,
                             ErrorNumber = "-1",
                             ErrorMessage = result.Exception.Message
                         }
                     };
                     program.ScriptErrors = JsonConvert.SerializeObject(error);
                     program.IsEnabled = false;
                     RaiseProgramModuleEvent(
                         program,
                         "Runtime.Error",
                         "CR: " + result.Exception.Message.Replace(
                             '\n',
                             ' '
                         )
                     );
                 }
                 program.IsRunning = false;
                 program.ProgramThread = null;
                 RaiseProgramModuleEvent(program, "Program.Status", "Idle");
             });
             //
             try
             {
                 program.ProgramThread.Start();
             }
             catch
             {
                 program.Stop();
                 program.IsRunning = false;
                 RaiseProgramModuleEvent(program, "Program.Status", "Idle");
             }
         }
     }
     else
     {
         program.TriggerTime = DateTime.UtcNow;
         if (program.ConditionType == ConditionType.Once)
         {
             program.IsEnabled = false;
         }
         //
         program.ProgramThread = new Thread(() =>
         {
             try
             {
                 ExecuteWizardScript(program);
             }
             catch (ThreadAbortException)
             {
                 program.IsRunning = false;
             }
             finally
             {
                 program.IsRunning = false;
             }
             RaiseProgramModuleEvent(program, "Program.Status", "Idle");
         });
         //
         program.ProgramThread.Start();
     }
     //
     Thread.Sleep(100);
 }
Пример #19
0
        public void StartProgram(string options = null)
        {
            if (ProgramBlock.IsRunning)
            {
                return;
            }

            // TODO: since if !program.IsRunning also thread should be null
            // TODO: so this is probably useless here and could be removed?
            if (_programThread != null)
            {
                Debugger.Break();
                StopProgram();
            }

            ProgramBlock.IsRunning = true;
            HomeGenie.ProgramManager.RaiseProgramModuleEvent(ProgramBlock, Properties.ProgramStatus, "Running");

            ProgramBlock.TriggerTime = DateTime.UtcNow;

            _programThread = new Thread(() =>
            {
                try
                {
                    MethodRunResult result;
                    try
                    {
                        result = Run(options);
                    }
                    catch (Exception ex)
                    {
                        result = new MethodRunResult {
                            Exception = ex
                        };
                    }
                    _programThread         = null;
                    ProgramBlock.IsRunning = false;
                    if (result != null && result.Exception != null &&
                        result.Exception.GetType() != typeof(TargetException))
                    {
                        // runtime error occurred, script is being disabled
                        // so user can notice and fix it
                        var error = new List <ProgramError> {
                            GetFormattedError(result.Exception, false)
                        };
                        ProgramBlock.ScriptErrors = JsonConvert.SerializeObject(error);
                        _log.Error(result.Exception, "Error while running program {0}", ProgramBlock.Address);
                        HomeGenie.ProgramManager.RaiseProgramModuleEvent(ProgramBlock, Properties.RuntimeError,
                                                                         PrepareExceptionMessage(CodeBlockEnum.CR, result.Exception));

                        TryToAutoRestart();
                    }
                    HomeGenie.ProgramManager.RaiseProgramModuleEvent(ProgramBlock, Properties.ProgramStatus,
                                                                     ProgramBlock.IsEnabled ? "Idle" : "Stopped");
                }
                catch (ThreadAbortException)
                {
                    _programThread         = null;
                    ProgramBlock.IsRunning = false;
                    if (HomeGenie.ProgramManager != null)
                    {
                        HomeGenie.ProgramManager.RaiseProgramModuleEvent(
                            ProgramBlock,
                            Properties.ProgramStatus,
                            "Interrupted"
                            );
                    }
                }
            });

            try
            {
                _programThread.Start();
            }
            catch
            {
                StopProgram();
                HomeGenie.ProgramManager.RaiseProgramModuleEvent(ProgramBlock, Properties.ProgramStatus, "Idle");
            }
            lastProgramRunTs = DateTime.Now;
        }