use of org.eclipse.smarthome.automation.module.script.rulesupport.shared.simple.SimpleRuleActionHandlerDelegate in project smarthome by eclipse.
the class ScriptedAutomationManager method addRule.
public Rule addRule(Rule element) {
Rule rule = element.getUID() == null ? new Rule(generateUID()) : new Rule(element.getUID());
String name = element.getName();
if (name == null || name.isEmpty()) {
name = element.getClass().getSimpleName();
if (name.contains("$")) {
name = name.substring(0, name.indexOf('$'));
}
}
rule.setName(name);
rule.setDescription(element.getDescription());
rule.setTags(element.getTags());
// used for numbering the modules of the rule
int moduleIndex = 1;
try {
ArrayList<Condition> conditions = new ArrayList<>();
for (Condition cond : element.getConditions()) {
Condition toAdd = cond;
if (cond.getId() == null || cond.getId().isEmpty()) {
toAdd = new Condition(Integer.toString(moduleIndex++), cond.getTypeUID(), cond.getConfiguration(), cond.getInputs());
}
conditions.add(toAdd);
}
rule.setConditions(conditions);
} catch (Exception ex) {
// conditions are optional
}
try {
ArrayList<Trigger> triggers = new ArrayList<>();
for (Trigger trigger : element.getTriggers()) {
Trigger toAdd = trigger;
if (trigger.getId() == null || trigger.getId().isEmpty()) {
toAdd = new Trigger(Integer.toString(moduleIndex++), trigger.getTypeUID(), trigger.getConfiguration());
}
triggers.add(toAdd);
}
rule.setTriggers(triggers);
} catch (Exception ex) {
// triggers are optional
}
ArrayList<Action> actions = new ArrayList<>();
actions.addAll(element.getActions());
if (element instanceof SimpleRuleActionHandler) {
String privId = addPrivateActionHandler(new SimpleRuleActionHandlerDelegate((SimpleRuleActionHandler) element));
Action scriptedAction = new Action(Integer.toString(moduleIndex++), "jsr223.ScriptedAction", new Configuration(), null);
scriptedAction.getConfiguration().put("privId", privId);
actions.add(scriptedAction);
}
rule.setActions(actions);
ruleRegistryDelegate.add(rule);
return rule;
}
Aggregations