Search in sources :

Example 1 with ScriptExecutionException

use of org.eclipse.smarthome.model.script.engine.ScriptExecutionException 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 ScriptExecutionException

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

the class ScriptImpl method execute.

@Override
public Object execute() throws ScriptExecutionException {
    if (xExpression != null) {
        Resource resource = xExpression.eResource();
        IEvaluationContext evaluationContext = null;
        if (resource instanceof XtextResource) {
            IResourceServiceProvider provider = ((XtextResource) resource).getResourceServiceProvider();
            evaluationContext = provider.get(IEvaluationContext.class);
        }
        return execute(evaluationContext);
    } else {
        throw new ScriptExecutionException("Script does not contain any expression");
    }
}
Also used : IResourceServiceProvider(org.eclipse.xtext.resource.IResourceServiceProvider) ScriptExecutionException(org.eclipse.smarthome.model.script.engine.ScriptExecutionException) XtextResource(org.eclipse.xtext.resource.XtextResource) Resource(org.eclipse.emf.ecore.resource.Resource) IEvaluationContext(org.eclipse.xtext.xbase.interpreter.IEvaluationContext) XtextResource(org.eclipse.xtext.resource.XtextResource)

Example 3 with ScriptExecutionException

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

the class ScriptImpl method execute.

@Override
public Object execute(final IEvaluationContext evaluationContext) throws ScriptExecutionException {
    if (xExpression != null) {
        Resource resource = xExpression.eResource();
        IExpressionInterpreter interpreter = null;
        if (resource instanceof XtextResource) {
            IResourceServiceProvider provider = ((XtextResource) resource).getResourceServiceProvider();
            interpreter = provider.get(IExpressionInterpreter.class);
        }
        if (interpreter == null) {
            throw new ScriptExecutionException("Script interpreter couldn't be obtain");
        }
        try {
            IEvaluationResult result = interpreter.evaluate(xExpression, evaluationContext, CancelIndicator.NullImpl);
            if (result == null) {
                // i.e. NEVER ;-)
                return null;
            }
            if (result.getException() != null) {
                throw new ScriptExecutionException(result.getException().getMessage(), result.getException());
            }
            return result.getResult();
        } catch (Throwable e) {
            if (e instanceof ScriptExecutionException) {
                throw (ScriptExecutionException) e;
            } else {
                throw new ScriptExecutionException("An error occurred during the script execution: " + e.getMessage(), e);
            }
        }
    } else {
        throw new ScriptExecutionException("Script does not contain any expression");
    }
}
Also used : IResourceServiceProvider(org.eclipse.xtext.resource.IResourceServiceProvider) IExpressionInterpreter(org.eclipse.xtext.xbase.interpreter.IExpressionInterpreter) ScriptExecutionException(org.eclipse.smarthome.model.script.engine.ScriptExecutionException) IEvaluationResult(org.eclipse.xtext.xbase.interpreter.IEvaluationResult) XtextResource(org.eclipse.xtext.resource.XtextResource) Resource(org.eclipse.emf.ecore.resource.Resource) XtextResource(org.eclipse.xtext.resource.XtextResource)

Example 4 with ScriptExecutionException

use of org.eclipse.smarthome.model.script.engine.ScriptExecutionException 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 5 with ScriptExecutionException

use of org.eclipse.smarthome.model.script.engine.ScriptExecutionException 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)

Aggregations

ScriptExecutionException (org.eclipse.smarthome.model.script.engine.ScriptExecutionException)7 Script (org.eclipse.smarthome.model.script.engine.Script)4 Resource (org.eclipse.emf.ecore.resource.Resource)2 Rule (org.eclipse.smarthome.model.rule.rules.Rule)2 RuleModel (org.eclipse.smarthome.model.rule.rules.RuleModel)2 ScriptEngine (org.eclipse.smarthome.model.script.engine.ScriptEngine)2 IResourceServiceProvider (org.eclipse.xtext.resource.IResourceServiceProvider)2 XtextResource (org.eclipse.xtext.resource.XtextResource)2 IEvaluationContext (org.eclipse.xtext.xbase.interpreter.IEvaluationContext)2 Adapter (org.eclipse.emf.common.notify.Adapter)1 EObject (org.eclipse.emf.ecore.EObject)1 EContentAdapter (org.eclipse.emf.ecore.util.EContentAdapter)1 ModelRepository (org.eclipse.smarthome.model.core.ModelRepository)1 VariableDeclaration (org.eclipse.smarthome.model.rule.rules.VariableDeclaration)1 ScriptParsingException (org.eclipse.smarthome.model.script.engine.ScriptParsingException)1 XExpression (org.eclipse.xtext.xbase.XExpression)1 IEvaluationResult (org.eclipse.xtext.xbase.interpreter.IEvaluationResult)1 IExpressionInterpreter (org.eclipse.xtext.xbase.interpreter.IExpressionInterpreter)1 Logger (org.slf4j.Logger)1