Search in sources :

Example 1 with ScriptEngine

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

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

the class RuleContextHelper method getContext.

/**
 * Retrieves the evaluation context (= set of variables) for a rule. The context is shared with all rules in the
 * same model (= rule file).
 *
 * @param rule the rule to get the context for
 * @return the evaluation context
 */
public static synchronized IEvaluationContext getContext(Rule rule, Injector injector) {
    Logger logger = LoggerFactory.getLogger(RuleContextHelper.class);
    RuleModel ruleModel = (RuleModel) rule.eContainer();
    // check if a context already exists on the resource
    for (Adapter adapter : ruleModel.eAdapters()) {
        if (adapter instanceof RuleContextAdapter) {
            return ((RuleContextAdapter) adapter).getContext();
        }
    }
    Provider<IEvaluationContext> contextProvider = injector.getProvider(IEvaluationContext.class);
    // no evaluation context found, so create a new one
    ScriptEngine scriptEngine = injector.getInstance(ScriptEngine.class);
    if (scriptEngine != null) {
        IEvaluationContext evaluationContext = contextProvider.get();
        for (VariableDeclaration var : ruleModel.getVariables()) {
            try {
                Object initialValue = var.getRight() == null ? null : scriptEngine.newScriptFromXExpression(var.getRight()).execute();
                evaluationContext.newValue(QualifiedName.create(var.getName()), initialValue);
            } catch (ScriptExecutionException e) {
                logger.warn("Variable '{}' on rule file '{}' cannot be initialized with value '{}': {}", new Object[] { var.getName(), ruleModel.eResource().getURI().path(), var.getRight().toString(), e.getMessage() });
            }
        }
        ruleModel.eAdapters().add(new RuleContextAdapter(evaluationContext));
        return evaluationContext;
    } else {
        logger.debug("Rule variables of rule {} cannot be evaluated as no scriptengine is available!", ruleModel.eResource().getURI().path());
        return contextProvider.get();
    }
}
Also used : ScriptExecutionException(org.eclipse.smarthome.model.script.engine.ScriptExecutionException) IEvaluationContext(org.eclipse.xtext.xbase.interpreter.IEvaluationContext) EContentAdapter(org.eclipse.emf.ecore.util.EContentAdapter) Adapter(org.eclipse.emf.common.notify.Adapter) VariableDeclaration(org.eclipse.smarthome.model.rule.rules.VariableDeclaration) Logger(org.slf4j.Logger) RuleModel(org.eclipse.smarthome.model.rule.rules.RuleModel) ScriptEngine(org.eclipse.smarthome.model.script.engine.ScriptEngine)

Example 3 with ScriptEngine

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

the class ServiceModule method configure.

@Override
public void configure(Binder binder) {
    binder.bind(ItemRegistry.class).toInstance(scriptServiceUtil.getItemRegistryInstance());
    binder.bind(ThingRegistry.class).toInstance(scriptServiceUtil.getThingRegistryInstance());
    binder.bind(ModelRepository.class).toInstance(scriptServiceUtil.getModelRepositoryInstance());
    binder.bind(ScriptEngine.class).toInstance(scriptEngine);
    binder.bind(IActionServiceProvider.class).toInstance(new ServiceTrackerActionServiceProvider(scriptServiceUtil));
    binder.bind(Script.class).to(ScriptImpl.class);
}
Also used : ModelRepository(org.eclipse.smarthome.model.core.ModelRepository) Script(org.eclipse.smarthome.model.script.script.Script) ServiceTrackerActionServiceProvider(org.eclipse.smarthome.model.script.internal.engine.ServiceTrackerActionServiceProvider) IActionServiceProvider(org.eclipse.smarthome.model.script.engine.IActionServiceProvider) ItemRegistry(org.eclipse.smarthome.core.items.ItemRegistry) ScriptEngine(org.eclipse.smarthome.model.script.engine.ScriptEngine) ThingRegistry(org.eclipse.smarthome.core.thing.ThingRegistry)

Aggregations

ScriptEngine (org.eclipse.smarthome.model.script.engine.ScriptEngine)3 ModelRepository (org.eclipse.smarthome.model.core.ModelRepository)2 ScriptExecutionException (org.eclipse.smarthome.model.script.engine.ScriptExecutionException)2 Adapter (org.eclipse.emf.common.notify.Adapter)1 EContentAdapter (org.eclipse.emf.ecore.util.EContentAdapter)1 ItemRegistry (org.eclipse.smarthome.core.items.ItemRegistry)1 ThingRegistry (org.eclipse.smarthome.core.thing.ThingRegistry)1 RuleModel (org.eclipse.smarthome.model.rule.rules.RuleModel)1 VariableDeclaration (org.eclipse.smarthome.model.rule.rules.VariableDeclaration)1 IActionServiceProvider (org.eclipse.smarthome.model.script.engine.IActionServiceProvider)1 Script (org.eclipse.smarthome.model.script.engine.Script)1 ServiceTrackerActionServiceProvider (org.eclipse.smarthome.model.script.internal.engine.ServiceTrackerActionServiceProvider)1 Script (org.eclipse.smarthome.model.script.script.Script)1 XExpression (org.eclipse.xtext.xbase.XExpression)1 IEvaluationContext (org.eclipse.xtext.xbase.interpreter.IEvaluationContext)1 Logger (org.slf4j.Logger)1