Search in sources :

Example 76 with Rule

use of org.openhab.core.automation.Rule in project openhab-core by openhab.

the class ScriptedAutomationManager method addUnmanagedRule.

public Rule addUnmanagedRule(Rule element) {
    RuleBuilder builder = RuleBuilder.create(element.getUID());
    String name = element.getName();
    if (name == null || name.isEmpty()) {
        name = element.getClass().getSimpleName();
        if (name.contains("$")) {
            name = name.substring(0, name.indexOf('$'));
        }
    }
    builder.withName(name).withDescription(element.getDescription()).withTags(element.getTags());
    // used for numbering the modules of the rule
    int moduleIndex = 1;
    try {
        List<Condition> conditions = new ArrayList<>();
        for (Condition cond : element.getConditions()) {
            Condition toAdd = cond;
            if (cond.getId().isEmpty()) {
                toAdd = ModuleBuilder.createCondition().withId(Integer.toString(moduleIndex++)).withTypeUID(cond.getTypeUID()).withConfiguration(cond.getConfiguration()).withInputs(cond.getInputs()).build();
            }
            conditions.add(toAdd);
        }
        builder.withConditions(conditions);
    } catch (Exception ex) {
    // conditions are optional
    }
    try {
        List<Trigger> triggers = new ArrayList<>();
        for (Trigger trigger : element.getTriggers()) {
            Trigger toAdd = trigger;
            if (trigger.getId().isEmpty()) {
                toAdd = ModuleBuilder.createTrigger().withId(Integer.toString(moduleIndex++)).withTypeUID(trigger.getTypeUID()).withConfiguration(trigger.getConfiguration()).build();
            }
            triggers.add(toAdd);
        }
        builder.withTriggers(triggers);
    } catch (Exception ex) {
    // triggers are optional
    }
    List<Action> actions = new ArrayList<>();
    actions.addAll(element.getActions());
    if (element instanceof SimpleRuleActionHandler) {
        String privId = addPrivateActionHandler(new SimpleRuleActionHandlerDelegate((SimpleRuleActionHandler) element));
        Action scriptedAction = ActionBuilder.create().withId(Integer.toString(moduleIndex++)).withTypeUID("jsr223.ScriptedAction").withConfiguration(new Configuration()).build();
        scriptedAction.getConfiguration().put("privId", privId);
        actions.add(scriptedAction);
    }
    builder.withConfiguration(element.getConfiguration());
    builder.withActions(actions);
    Rule rule = builder.build();
    return rule;
}
Also used : Condition(org.openhab.core.automation.Condition) Action(org.openhab.core.automation.Action) Configuration(org.openhab.core.config.core.Configuration) SimpleRuleActionHandlerDelegate(org.openhab.core.automation.module.script.rulesupport.shared.simple.SimpleRuleActionHandlerDelegate) ArrayList(java.util.ArrayList) Trigger(org.openhab.core.automation.Trigger) RuleBuilder(org.openhab.core.automation.util.RuleBuilder) Rule(org.openhab.core.automation.Rule) SimpleRuleActionHandler(org.openhab.core.automation.module.script.rulesupport.shared.simple.SimpleRuleActionHandler)

Aggregations

Rule (org.openhab.core.automation.Rule)76 Test (org.junit.jupiter.api.Test)47 JavaOSGiTest (org.openhab.core.test.java.JavaOSGiTest)36 Configuration (org.openhab.core.config.core.Configuration)27 Trigger (org.openhab.core.automation.Trigger)23 Action (org.openhab.core.automation.Action)21 Event (org.openhab.core.events.Event)19 ItemCommandEvent (org.openhab.core.items.events.ItemCommandEvent)19 EventSubscriber (org.openhab.core.events.EventSubscriber)16 RuleStatusInfoEvent (org.openhab.core.automation.events.RuleStatusInfoEvent)15 RuleRegistry (org.openhab.core.automation.RuleRegistry)14 HashMap (java.util.HashMap)13 Random (java.util.Random)13 EventPublisher (org.openhab.core.events.EventPublisher)13 ArrayList (java.util.ArrayList)12 Response (javax.ws.rs.core.Response)12 Operation (io.swagger.v3.oas.annotations.Operation)10 Path (javax.ws.rs.Path)10 RuleManager (org.openhab.core.automation.RuleManager)10 RuleAddedEvent (org.openhab.core.automation.events.RuleAddedEvent)10