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