Search in sources :

Example 1 with Script

use of org.eclipse.smarthome.model.script.engine.Script in project smarthome by eclipse.

the class RuleEngineImpl method runStartupRules.

private void runStartupRules() {
    if (triggerManager != null) {
        Iterable<Rule> startupRules = triggerManager.getRules(STARTUP);
        for (Rule rule : startupRules) {
            scheduler.execute(() -> {
                try {
                    Script script = scriptEngine.newScriptFromXExpression(rule.getScript());
                    logger.debug("Executing startup rule '{}'", rule.getName());
                    RuleEvaluationContext context = new RuleEvaluationContext();
                    context.setGlobalContext(RuleContextHelper.getContext(rule, injector));
                    script.execute(context);
                    triggerManager.removeRule(STARTUP, rule);
                } catch (ScriptExecutionException e) {
                    if (!e.getMessage().contains("cannot be resolved to an item or type")) {
                        logger.error("Error during the execution of startup rule '{}': {}", rule.getName(), e.getCause().getMessage());
                        triggerManager.removeRule(STARTUP, rule);
                    } else {
                        logger.debug("Execution of startup rule '{}' has been postponed as items are still missing: {}", rule.getName(), e.getMessage());
                    }
                }
            });
        }
        // now that we have scheduled the startup rules, we are ready for others as well
        starting = false;
        triggerManager.startTimerRuleExecution();
    }
}
Also used : Script(org.eclipse.smarthome.model.script.engine.Script) ScriptExecutionException(org.eclipse.smarthome.model.script.engine.ScriptExecutionException) Rule(org.eclipse.smarthome.model.rule.rules.Rule)

Example 2 with Script

use of org.eclipse.smarthome.model.script.engine.Script in project smarthome by eclipse.

the class ScriptExecution method callScript.

/**
 * Calls a script which must be located in the configurations/scripts folder.
 *
 * @param scriptName the name of the script (if the name does not end with
 *            the .script file extension it is added)
 *
 * @return the return value of the script
 * @throws ScriptExecutionException if an error occurs during the execution
 */
public static Object callScript(String scriptName) throws ScriptExecutionException {
    ModelRepository repo = ScriptServiceUtil.getModelRepository();
    if (repo != null) {
        String scriptNameWithExt = scriptName;
        if (!StringUtils.endsWith(scriptName, Script.SCRIPT_FILEEXT)) {
            scriptNameWithExt = scriptName + "." + Script.SCRIPT_FILEEXT;
        }
        XExpression expr = (XExpression) repo.getModel(scriptNameWithExt);
        if (expr != null) {
            ScriptEngine scriptEngine = ScriptServiceUtil.getScriptEngine();
            if (scriptEngine != null) {
                Script script = scriptEngine.newScriptFromXExpression(expr);
                return script.execute();
            } else {
                throw new ScriptExecutionException("Script engine is not available.");
            }
        } else {
            throw new ScriptExecutionException("Script '" + scriptName + "' cannot be found.");
        }
    } else {
        throw new ScriptExecutionException("Model repository is not available.");
    }
}
Also used : ModelRepository(org.eclipse.smarthome.model.core.ModelRepository) Script(org.eclipse.smarthome.model.script.engine.Script) ScriptExecutionException(org.eclipse.smarthome.model.script.engine.ScriptExecutionException) XExpression(org.eclipse.xtext.xbase.XExpression) ScriptEngine(org.eclipse.smarthome.model.script.engine.ScriptEngine)

Example 3 with Script

use of org.eclipse.smarthome.model.script.engine.Script in project smarthome by eclipse.

the class ScriptEngineConsoleCommandExtension method execute.

@Override
public void execute(String[] args, Console console) {
    if (scriptEngine != null) {
        String scriptString = Arrays.stream(args).collect(Collectors.joining(" "));
        Script script;
        try {
            script = scriptEngine.newScriptFromString(scriptString);
            Object result = script.execute();
            if (result != null) {
                console.println(result.toString());
            } else {
                console.println("OK");
            }
        } catch (ScriptParsingException e) {
            console.println(e.getMessage());
        } catch (ScriptExecutionException e) {
            console.println(e.getMessage());
        }
    } else {
        console.println("Script engine is not available.");
    }
}
Also used : Script(org.eclipse.smarthome.model.script.engine.Script) ScriptParsingException(org.eclipse.smarthome.model.script.engine.ScriptParsingException) ScriptExecutionException(org.eclipse.smarthome.model.script.engine.ScriptExecutionException)

Example 4 with Script

use of org.eclipse.smarthome.model.script.engine.Script in project smarthome by eclipse.

the class RuleEngineImpl method executeRule.

protected synchronized void executeRule(Rule rule, RuleEvaluationContext context) {
    scheduler.execute(() -> {
        Script script = scriptEngine.newScriptFromXExpression(rule.getScript());
        logger.debug("Executing rule '{}'", rule.getName());
        context.setGlobalContext(RuleContextHelper.getContext(rule, injector));
        try {
            script.execute(context);
        } catch (Exception e) {
            String msg = e.getMessage();
            if (msg == null) {
                logger.error("Rule '{}'", rule.getName(), e.getCause());
            } else {
                logger.error("Rule '{}': {}", rule.getName(), msg);
            }
        }
    });
}
Also used : Script(org.eclipse.smarthome.model.script.engine.Script) ItemNotFoundException(org.eclipse.smarthome.core.items.ItemNotFoundException) ScriptExecutionException(org.eclipse.smarthome.model.script.engine.ScriptExecutionException)

Example 5 with Script

use of org.eclipse.smarthome.model.script.engine.Script in project smarthome by eclipse.

the class ExecuteRuleJob method execute.

@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
    String modelName = (String) context.getJobDetail().getJobDataMap().get(JOB_DATA_RULEMODEL);
    String ruleName = (String) context.getJobDetail().getJobDataMap().get(JOB_DATA_RULENAME);
    if (modelRepository != null && scriptEngine != null) {
        EObject model = modelRepository.getModel(modelName);
        if (model instanceof RuleModel) {
            RuleModel ruleModel = (RuleModel) model;
            Rule rule = getRule(ruleModel, ruleName);
            if (rule != null) {
                Script script = scriptEngine.newScriptFromXExpression(rule.getScript());
                logger.debug("Executing scheduled rule '{}'", rule.getName());
                try {
                    script.execute(RuleContextHelper.getContext(rule, injector));
                } catch (ScriptExecutionException e) {
                    logger.error("Error during the execution of rule '{}': {}", rule.getName(), e.getMessage());
                }
            } else {
                logger.debug("Scheduled rule '{}' does not exist", ruleName);
            }
        } else {
            logger.debug("Rule file '{}' does not exist", modelName);
        }
    }
}
Also used : Script(org.eclipse.smarthome.model.script.engine.Script) ScriptExecutionException(org.eclipse.smarthome.model.script.engine.ScriptExecutionException) EObject(org.eclipse.emf.ecore.EObject) Rule(org.eclipse.smarthome.model.rule.rules.Rule) RuleModel(org.eclipse.smarthome.model.rule.rules.RuleModel)

Aggregations

Script (org.eclipse.smarthome.model.script.engine.Script)5 ScriptExecutionException (org.eclipse.smarthome.model.script.engine.ScriptExecutionException)5 Rule (org.eclipse.smarthome.model.rule.rules.Rule)2 EObject (org.eclipse.emf.ecore.EObject)1 ItemNotFoundException (org.eclipse.smarthome.core.items.ItemNotFoundException)1 ModelRepository (org.eclipse.smarthome.model.core.ModelRepository)1 RuleModel (org.eclipse.smarthome.model.rule.rules.RuleModel)1 ScriptEngine (org.eclipse.smarthome.model.script.engine.ScriptEngine)1 ScriptParsingException (org.eclipse.smarthome.model.script.engine.ScriptParsingException)1 XExpression (org.eclipse.xtext.xbase.XExpression)1