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();
}
}
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");
}
}
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");
}
}
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.");
}
}
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.");
}
}
Aggregations