コード例 #1
0
        internal MethodRunResult EvaluateConditionStatement(HomeGenieService homegenieref)
        {
            if (_scriptassembly == null)
            {
                return(null);
            }
            //
            MethodRunResult res = null;

            //
            if (_checkinstance(homegenieref))
            {
                res = (MethodRunResult)_program_method_evaluatecondition.Invoke(_program_assembly, null);
            }
            //
            return(res);
        }
コード例 #2
0
        internal MethodRunResult RunScript(HomeGenieService homegenieref, string options)
        {
            if (_scriptassembly == null)
            {
                return(null);
            }
            //
            MethodRunResult res = null;

            //
            if (_checkinstance(homegenieref))
            {
                res = (MethodRunResult)_program_method_run.Invoke(_program_assembly, new object[1] {
                    options
                });
            }
            //
            return(res);
        }
コード例 #3
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, Properties.PROGRAM_STATUS, "Running");
     //
     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>()
             {
                 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.ProgramThread = null;
         RaiseProgramModuleEvent(program, Properties.PROGRAM_STATUS, "Idle");
     });
     //
     if (program.ConditionType == ConditionType.Once)
     {
         program.IsEnabled = false;
     }
     //
     try
     {
         program.ProgramThread.Start();
     }
     catch
     {
         program.Stop();
         program.IsRunning = false;
         RaiseProgramModuleEvent(program, Properties.PROGRAM_STATUS, "Idle");
     }
     //
     //Thread.Sleep(100);
 }
コード例 #4
0
ファイル: ProgramManager.cs プロジェクト: xzflin/HomeGenie
        // 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.ProgramStatus, "Running");

            program.TriggerTime = DateTime.UtcNow;

            program.Engine.ProgramThread = new Thread(() =>
            {
                try
                {
                    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.RuntimeError, "CR: " + result.Exception.Message.Replace('\n', ' ').Replace('\r', ' '));
                    }
                    RaiseProgramModuleEvent(program, Properties.ProgramStatus, "Idle");
                }
                catch (ThreadAbortException e)
                {
                    // nothing to be done here
                    RaiseProgramModuleEvent(program, Properties.ProgramStatus, "Interrupted");
                }
                finally
                {
                    program.IsRunning            = false;
                    program.Engine.ProgramThread = null;
                }
            });
            //
            if (program.ConditionType == ConditionType.Once)
            {
                program.IsEnabled = false;
            }
            //
            try
            {
                program.Engine.ProgramThread.Start();
            }
            catch
            {
                program.Engine.Stop();
                program.IsRunning = false;
                RaiseProgramModuleEvent(program, Properties.ProgramStatus, "Idle");
            }
            //
            //Thread.Sleep(100);
        }
コード例 #5
0
ファイル: ProgramEngine.cs プロジェクト: macitynet/HomeGenie
 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);
 }
コード例 #6
0
        internal MethodRunResult EvaluateCondition()
        {
            MethodRunResult result = null;

            switch (codeType.ToLower())
            {
            case "python":
                string       pythonScript = this.ScriptCondition;
                ScriptEngine pythonEngine = (scriptEngine as ScriptEngine);
                result = new MethodRunResult();
                try
                {
                    pythonEngine.Execute(pythonScript, scriptScope);
                    result.ReturnValue = (scriptScope as dynamic).hg.executeCodeToRun;
                }
                catch (Exception e)
                {
                    result.Exception = e;
                }
                break;

            case "ruby":
                string       rubyScript = this.ScriptCondition;
                ScriptEngine rubyEngine = (scriptEngine as ScriptEngine);
                result = new MethodRunResult();
                try
                {
                    rubyEngine.Execute(rubyScript, scriptScope);
                    result.ReturnValue = (scriptScope as dynamic).hg.executeCodeToRun;
                }
                catch (Exception e)
                {
                    result.Exception = e;
                }
                break;

            case "javascript":
                string      jsScript = this.ScriptCondition;
                Jint.Engine engine   = (scriptEngine as Jint.Engine);
                result = new MethodRunResult();
                try
                {
                    engine.Execute(jsScript);
                    result.ReturnValue = (engine.GetValue("hg").ToObject() as ScriptingHost).executeCodeToRun;
                }
                catch (Exception e)
                {
                    result.Exception = e;
                }
                break;

            case "csharp":
                if (appAssembly != null && CheckAppInstance())
                {
                    result = (MethodRunResult)methodEvaluateCondition.Invoke(assembly, null);
                }
                break;
            }
            //
            return(result);
        }
コード例 #7
0
        internal MethodRunResult Run(string options)
        {
            MethodRunResult result = null;

            switch (codeType.ToLower())
            {
            case "python":
                string       pythonScript = this.ScriptSource;
                ScriptEngine pythonEngine = (scriptEngine as ScriptEngine);
                result = new MethodRunResult();
                try
                {
                    pythonEngine.Execute(pythonScript, scriptScope);
                }
                catch (Exception e)
                {
                    result.Exception = e;
                }
                break;

            case "ruby":
                string       rubyScript = this.ScriptSource;
                ScriptEngine rubyEngine = (scriptEngine as ScriptEngine);
                result = new MethodRunResult();
                try
                {
                    rubyEngine.Execute(rubyScript, scriptScope);
                }
                catch (Exception e)
                {
                    result.Exception = e;
                }
                break;

            case "javascript":
                string      jsScript = this.ScriptSource;
                Jint.Engine engine   = (scriptEngine as Jint.Engine);
                //engine.Options.AllowClr(false);
                result = new MethodRunResult();
                try
                {
                    engine.Execute(jsScript);
                }
                catch (Exception e)
                {
                    result.Exception = e;
                }
                break;

            case "csharp":
                if (appAssembly != null && CheckAppInstance())
                {
                    result = (MethodRunResult)methodRun.Invoke(assembly, new object[1] {
                        options
                    });
                }
                break;
            }
            //
            return(result);
        }
コード例 #8
0
        public void EvaluateProgramConditionAsync(ProgramBlock p, ConditionEvaluationCallback callback)
        {
            p.IsEvaluatingConditionBlock = true;
            Thread evaluatorthread = new Thread(new ThreadStart(delegate()
            {
                bool conditionsatisfied = false;
                //
                while (_enginerunning)
                {
                    if (p.IsRunning || !p.IsEnabled || !_engineenabled)
                    {
                        Thread.Sleep(500); continue;
                    }
                    //
                    System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
                    sw.Start();
                    try
                    {
                        conditionsatisfied = false;
                        //
                        if (p.Type.ToLower() == "csharp")
                        {
                            MethodRunResult res = p.EvaluateConditionStatement(_homegenie);
                            if (res != null && res.Exception != null)
                            {
                                // runtime error occurred, script is being disabled
                                // so user can notice and fix it
                                p.ScriptErrors = res.Exception.Message + "\n" + res.Exception.StackTrace;
                                p.IsEnabled    = false;
                            }
                            else
                            {
                                conditionsatisfied = (bool)res.ReturnValue;
                            }
                        }
                        else
                        {
                            // it is a Wizard Script
                            conditionsatisfied = (p.Conditions.Count > 0);
                            for (int c = 0; c < p.Conditions.Count; c++)
                            {
                                bool res           = _mcp_verifyCondition(p.Conditions[c]);
                                conditionsatisfied = (conditionsatisfied && res);
                            }
                        }
                        //
                        bool lasteval = p.LastConditionEvaluationResult;
                        p.LastConditionEvaluationResult = conditionsatisfied;
                        //
                        if (p.ConditionType == ConditionType.OnSwitchTrue)
                        {
                            conditionsatisfied = (conditionsatisfied == true && conditionsatisfied != lasteval);
                        }
                        else if (p.ConditionType == ConditionType.OnSwitchFalse)
                        {
                            conditionsatisfied = (conditionsatisfied == false && conditionsatisfied != lasteval);
                        }
                        else if (p.ConditionType == ConditionType.OnTrue || p.ConditionType == ConditionType.Once)
                        {
                            // noop
                        }
                        else if (p.ConditionType == ConditionType.OnFalse)
                        {
                            conditionsatisfied = !conditionsatisfied;
                        }
                    }
                    catch (Exception ex)
                    {
                        // a runtime error occured
                        p.ScriptErrors = ex.Message + "\n" + ex.StackTrace;
                        p.IsEnabled    = false;
                    }
                    //
                    sw.Stop();
                    //
                    callback(p, conditionsatisfied);
                    //
                    int delaynext = (int)(400 + (sw.ElapsedMilliseconds > 400 ? sw.ElapsedMilliseconds - 400 : 0));
                    if (delaynext > 500)
                    {
                        delaynext = 500;
                    }
                    //
                    Thread.Sleep(delaynext);
                }
                p.IsEvaluatingConditionBlock = false;
            }));

            //evaluatorthread.Priority = ThreadPriority.AboveNormal;
            evaluatorthread.Start();
        }
コード例 #9
0
 public void Run(ProgramBlock pb, string options)
 {
     if (pb.IsRunning)
     {
         return;
     }
     //
     if (pb.ProgramThread != null)
     {
         pb.Stop();
         pb.IsRunning = false;
     }
     //
     lock (_lock)
     {
         pb.IsRunning = true;
         //
         if (pb.Type.ToLower() == "csharp")
         {
             if (pb.ScriptAssembly != null)
             {
                 pb.TriggerTime   = DateTime.UtcNow;
                 pb.ProgramThread = new Thread(new ThreadStart(delegate()
                 {
                     MethodRunResult res = pb.RunScript(_homegenie, options);
                     if (res != null && res.Exception != null)
                     {
                         // runtime error occurred, script is being disabled
                         // so user can notice and fix it
                         pb.ScriptErrors = res.Exception.Message + "\n" + res.Exception.StackTrace;
                         pb.IsEnabled    = false;
                     }
                     pb.IsRunning = false;
                 }));
                 pb.ProgramThread.Priority = ThreadPriority.BelowNormal;
                 try
                 {
                     pb.ProgramThread.Start();
                 }
                 catch
                 {
                     pb.Stop();
                     pb.IsRunning = false;
                 }
             }
             else
             {
                 pb.IsRunning = false;
             }
         }
         else
         {
             pb.TriggerTime = DateTime.UtcNow;
             if (pb.ConditionType == ConditionType.Once)
             {
                 pb.IsEnabled = false;
             }
             //
             pb.ProgramThread = new Thread(new ThreadStart(delegate()
             {
                 try
                 {
                     ExecuteWizardScript(pb);
                 }
                 catch (ThreadAbortException)
                 {
                     pb.IsRunning = false;
                 }
                 finally
                 {
                     pb.IsRunning = false;
                 }
             }));
             pb.ProgramThread.Priority = ThreadPriority.Lowest;
             pb.ProgramThread.Start();
         }
         //
         Thread.Sleep(100);
     }
 }
コード例 #10
0
        internal MethodRunResult Run(string options)
        {
            MethodRunResult result = null;

            switch (codeType.ToLower())
            {
            case "python":
                string       pythonScript = this.ScriptSource;
                ScriptEngine pythonEngine = (scriptEngine as ScriptEngine);
                result = new MethodRunResult();
                try
                {
                    pythonEngine.Execute(pythonScript, scriptScope);
                }
                catch (Exception e)
                {
                    result.Exception = e;
                }
                break;

            case "ruby":
                string       rubyScript = this.ScriptSource;
                ScriptEngine rubyEngine = (scriptEngine as ScriptEngine);
                result = new MethodRunResult();
                try
                {
                    rubyEngine.Execute(rubyScript, scriptScope);
                }
                catch (Exception e)
                {
                    result.Exception = e;
                }
                break;

            case "javascript":
                string      jsScript = this.ScriptSource;
                Jint.Engine engine   = (scriptEngine as Jint.Engine);
                //engine.Options.AllowClr(false);
                result = new MethodRunResult();
                try
                {
                    engine.Execute(jsScript);
                }
                catch (Exception e)
                {
                    result.Exception = e;
                }
                break;

            case "csharp":
                if (appAssembly != null && CheckAppInstance())
                {
                    result = (MethodRunResult)methodRun.Invoke(assembly, new object[1] {
                        options
                    });
                }
                break;

            case "arduino":
                result = new MethodRunResult();
                homegenie.LogBroadcastEvent(
                    Domains.HomeAutomation_HomeGenie_Automation,
                    this.Address.ToString(),
                    "Arduino Sketch Upload",
                    "Arduino.UploadOutput",
                    "Upload started"
                    );
                string[] outputResult = ArduinoAppFactory.UploadSketch(Path.Combine(
                                                                           AppDomain.CurrentDomain.BaseDirectory,
                                                                           "programs",
                                                                           "arduino",
                                                                           this.Address.ToString()
                                                                           )).Split('\n');
                //
                for (int x = 0; x < outputResult.Length; x++)
                {
                    if (!String.IsNullOrWhiteSpace(outputResult[x]))
                    {
                        homegenie.LogBroadcastEvent(
                            Domains.HomeAutomation_HomeGenie_Automation,
                            this.Address.ToString(),
                            "Arduino Sketch",
                            "Arduino.UploadOutput",
                            outputResult[x]
                            );
                        Thread.Sleep(500);
                    }
                }
                //
                homegenie.LogBroadcastEvent(
                    Domains.HomeAutomation_HomeGenie_Automation,
                    this.Address.ToString(),
                    "Arduino Sketch",
                    "Arduino.UploadOutput",
                    "Upload finished"
                    );
                break;
            }
            //
            return(result);
        }