Search in sources :

Example 1 with ModelRepository

use of org.eclipse.smarthome.model.core.ModelRepository 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)

Aggregations

ModelRepository (org.eclipse.smarthome.model.core.ModelRepository)1 Script (org.eclipse.smarthome.model.script.engine.Script)1 ScriptEngine (org.eclipse.smarthome.model.script.engine.ScriptEngine)1 ScriptExecutionException (org.eclipse.smarthome.model.script.engine.ScriptExecutionException)1 XExpression (org.eclipse.xtext.xbase.XExpression)1