Search in sources :

Example 26 with Action

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

the class DSLRuleProvider method toRule.

private Rule toRule(String modelName, org.openhab.core.model.rule.rules.Rule rule, int index) {
    String name = rule.getName();
    String uid = modelName + "-" + index;
    // Create Triggers
    triggerId = 0;
    List<Trigger> triggers = new ArrayList<>();
    for (EventTrigger t : rule.getEventtrigger()) {
        Trigger trigger = mapTrigger(t);
        if (trigger != null) {
            triggers.add(trigger);
        }
    }
    // Create Action
    String context = DSLScriptContextProvider.CONTEXT_IDENTIFIER + modelName + "-" + index + "\n";
    XBlockExpression expression = rule.getScript();
    String script = NodeModelUtils.findActualNodeFor(expression).getText();
    Configuration cfg = new Configuration();
    cfg.put("script", context + removeIndentation(script));
    cfg.put("type", MIMETYPE_OPENHAB_DSL_RULE);
    List<Action> actions = List.of(ActionBuilder.create().withId("script").withTypeUID("script.ScriptAction").withConfiguration(cfg).build());
    return RuleBuilder.create(uid).withName(name).withTriggers(triggers).withActions(actions).build();
}
Also used : XBlockExpression(org.eclipse.xtext.xbase.XBlockExpression) Action(org.openhab.core.automation.Action) SystemOnStartupTrigger(org.openhab.core.model.rule.rules.SystemOnStartupTrigger) EventEmittedTrigger(org.openhab.core.model.rule.rules.EventEmittedTrigger) Trigger(org.openhab.core.automation.Trigger) TimerTrigger(org.openhab.core.model.rule.rules.TimerTrigger) ChangedEventTrigger(org.openhab.core.model.rule.rules.ChangedEventTrigger) CommandEventTrigger(org.openhab.core.model.rule.rules.CommandEventTrigger) SystemStartlevelTrigger(org.openhab.core.model.rule.rules.SystemStartlevelTrigger) GroupMemberChangedEventTrigger(org.openhab.core.model.rule.rules.GroupMemberChangedEventTrigger) GroupMemberCommandEventTrigger(org.openhab.core.model.rule.rules.GroupMemberCommandEventTrigger) ThingStateChangedEventTrigger(org.openhab.core.model.rule.rules.ThingStateChangedEventTrigger) SystemOnShutdownTrigger(org.openhab.core.model.rule.rules.SystemOnShutdownTrigger) UpdateEventTrigger(org.openhab.core.model.rule.rules.UpdateEventTrigger) ThingStateUpdateEventTrigger(org.openhab.core.model.rule.rules.ThingStateUpdateEventTrigger) GroupMemberUpdateEventTrigger(org.openhab.core.model.rule.rules.GroupMemberUpdateEventTrigger) EventTrigger(org.openhab.core.model.rule.rules.EventTrigger) Configuration(org.openhab.core.config.core.Configuration) ArrayList(java.util.ArrayList) ChangedEventTrigger(org.openhab.core.model.rule.rules.ChangedEventTrigger) CommandEventTrigger(org.openhab.core.model.rule.rules.CommandEventTrigger) GroupMemberChangedEventTrigger(org.openhab.core.model.rule.rules.GroupMemberChangedEventTrigger) GroupMemberCommandEventTrigger(org.openhab.core.model.rule.rules.GroupMemberCommandEventTrigger) ThingStateChangedEventTrigger(org.openhab.core.model.rule.rules.ThingStateChangedEventTrigger) UpdateEventTrigger(org.openhab.core.model.rule.rules.UpdateEventTrigger) ThingStateUpdateEventTrigger(org.openhab.core.model.rule.rules.ThingStateUpdateEventTrigger) GroupMemberUpdateEventTrigger(org.openhab.core.model.rule.rules.GroupMemberUpdateEventTrigger) EventTrigger(org.openhab.core.model.rule.rules.EventTrigger)

Example 27 with Action

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

the class AutomationIntegrationTest method assertThatRuleCanBeUpdated.

@Test
public void assertThatRuleCanBeUpdated() throws ItemNotFoundException {
    Configuration triggerConfig = new Configuration(Map.of("eventTopic", "runNowEventTopic/*"));
    Map<String, Object> params = new HashMap<>();
    params.put("itemName", "myLampItem3");
    params.put("command", "ON");
    Configuration actionConfig = new Configuration(params);
    List<Trigger> triggers = List.of(ModuleBuilder.createTrigger().withId("GenericEventTriggerId").withTypeUID("core.GenericEventTrigger").withConfiguration(triggerConfig).build());
    List<Action> actions = List.of(ModuleBuilder.createAction().withId("ItemPostCommandActionId").withTypeUID("core.ItemCommandAction").withConfiguration(actionConfig).build());
    String ruleId = "runNowRule" + new Random().nextInt();
    Rule rule = RuleBuilder.create(ruleId).withTriggers(triggers).withActions(actions).build();
    logger.info("Rule created: {}", rule.getUID());
    ruleRegistry.add(rule);
    // TEST RULE
    waitForAssert(() -> {
        assertThat(ruleEngine.getStatusInfo(rule.getUID()).getStatus(), is(RuleStatus.IDLE));
    }, 3000, 100);
    EventSubscriber itemEventHandler = new EventSubscriber() {

        @Override
        public Set<String> getSubscribedEventTypes() {
            return Set.of(ItemCommandEvent.TYPE);
        }

        @Override
        @Nullable
        public EventFilter getEventFilter() {
            return null;
        }

        @Override
        public void receive(Event e) {
            logger.info("Event: {}", e.getTopic());
            if (e.getTopic().contains("myLampItem3")) {
                itemEvent = e;
            }
        }
    };
    registerService(itemEventHandler);
    ruleEngine.runNow(rule.getUID());
    waitForAssert(() -> {
        assertThat(itemEvent, is(notNullValue()));
    }, 3000, 100);
    waitForAssert(() -> {
        assertThat(((ItemCommandEvent) itemEvent).getItemCommand(), is(OnOffType.ON));
    }, 3000, 100);
    params.put("command", "OFF");
    actionConfig = new Configuration(params);
    actions = List.of(ModuleBuilder.createAction().withId("ItemPostCommandActionId").withTypeUID("core.ItemCommandAction").withConfiguration(actionConfig).build());
    Rule updatedRule = RuleBuilder.create(ruleId).withTriggers(triggers).withActions(actions).build();
    ruleRegistry.update(updatedRule);
    waitForAssert(() -> {
        assertThat(ruleEngine.getStatusInfo(updatedRule.getUID()).getStatus(), is(RuleStatus.IDLE));
    }, 3000, 100);
    logger.info("Rule updated: {}", updatedRule.getUID());
    itemEvent = null;
    ruleEngine.runNow(updatedRule.getUID());
    waitForAssert(() -> {
        assertThat(itemEvent, is(notNullValue()));
    }, 3000, 100);
    waitForAssert(() -> {
        assertThat(((ItemCommandEvent) itemEvent).getItemCommand(), is(OnOffType.OFF));
    }, 3000, 100);
    ruleRegistry.remove(updatedRule.getUID());
}
Also used : EventSubscriber(org.openhab.core.events.EventSubscriber) Action(org.openhab.core.automation.Action) Configuration(org.openhab.core.config.core.Configuration) HashMap(java.util.HashMap) Trigger(org.openhab.core.automation.Trigger) Random(java.util.Random) RuleRemovedEvent(org.openhab.core.automation.events.RuleRemovedEvent) RuleAddedEvent(org.openhab.core.automation.events.RuleAddedEvent) RuleStatusInfoEvent(org.openhab.core.automation.events.RuleStatusInfoEvent) ItemCommandEvent(org.openhab.core.items.events.ItemCommandEvent) Event(org.openhab.core.events.Event) RuleUpdatedEvent(org.openhab.core.automation.events.RuleUpdatedEvent) Rule(org.openhab.core.automation.Rule) Test(org.junit.jupiter.api.Test) JavaOSGiTest(org.openhab.core.test.java.JavaOSGiTest)

Example 28 with Action

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

the class AutomationIntegrationTest method assertARuleAddedByApiIsExecutedAsExpected.

@Test
public void assertARuleAddedByApiIsExecutedAsExpected() {
    logger.info("assert a rule added by api is executed as expected");
    // Creation of RULE
    Map<String, Object> params = new HashMap<>();
    params.put("eventSource", "myMotionItem2");
    params.put("eventTopic", "openhab/*");
    params.put("eventTypes", "ItemStateEvent");
    Configuration triggerConfig = new Configuration(params);
    params = new HashMap<>();
    params.put("itemName", "myLampItem2");
    params.put("command", "ON");
    Configuration actionConfig = new Configuration(params);
    List<Trigger> triggers = List.of(ModuleBuilder.createTrigger().withId("ItemStateChangeTrigger2").withTypeUID("core.GenericEventTrigger").withConfiguration(triggerConfig).build());
    List<Action> actions = List.of(ModuleBuilder.createAction().withId("ItemPostCommandAction2").withTypeUID("core.ItemCommandAction").withConfiguration(actionConfig).build());
    Rule rule = RuleBuilder.create("myRule21").withTriggers(triggers).withActions(actions).withName("RuleByJAVA_API").withTags("myRule21").build();
    logger.info("Rule created: {}", rule.getUID());
    ruleRegistry.add(rule);
    ruleEngine.setEnabled(rule.getUID(), true);
    ruleRegistry.remove(rule.getUID());
}
Also used : Action(org.openhab.core.automation.Action) Trigger(org.openhab.core.automation.Trigger) Configuration(org.openhab.core.config.core.Configuration) HashMap(java.util.HashMap) Rule(org.openhab.core.automation.Rule) Test(org.junit.jupiter.api.Test) JavaOSGiTest(org.openhab.core.test.java.JavaOSGiTest)

Example 29 with Action

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

the class AutomationIntegrationTest method createSimpleRule.

/**
 * creates a simple rule
 */
private Rule createSimpleRule() {
    logger.info("createSimpleRule");
    int rand = new Random().nextInt();
    Map<String, Object> configs = new HashMap<>();
    configs.put("eventSource", "myMotionItem2");
    configs.put("eventTopic", "openhab/*");
    configs.put("eventTypes", "ItemStateEvent");
    Configuration triggerConfig = new Configuration(configs);
    configs = new HashMap<>();
    configs.put("itemName", "myLampItem2");
    configs.put("command", "ON");
    Configuration actionConfig = new Configuration(configs);
    String triggerUID = "ItemStateChangeTrigger_" + rand;
    List<Trigger> triggers = List.of(ModuleBuilder.createTrigger().withId(triggerUID).withTypeUID("core.GenericEventTrigger").withConfiguration(triggerConfig).build());
    List<Action> actions = List.of(ModuleBuilder.createAction().withId("ItemPostCommandAction_" + rand).withTypeUID("core.ItemCommandAction").withConfiguration(actionConfig).build());
    Rule rule = RuleBuilder.create("myRule_" + rand).withTriggers(triggers).withActions(actions).withName("RuleByJAVA_API_" + rand).build();
    logger.info("Rule created: {}", rule.getUID());
    return rule;
}
Also used : Action(org.openhab.core.automation.Action) Configuration(org.openhab.core.config.core.Configuration) HashMap(java.util.HashMap) Trigger(org.openhab.core.automation.Trigger) Random(java.util.Random) Rule(org.openhab.core.automation.Rule)

Example 30 with Action

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

the class AutomationIntegrationTest method assertARuleWithGenericConditionWorks.

@Test
public void assertARuleWithGenericConditionWorks() throws ItemNotFoundException {
    int random = new Random().nextInt(100000);
    logger.info("assert a rule with generic condition works");
    // Creation of RULE
    Map<String, Object> configs = new HashMap<>();
    configs.put("eventSource", "myMotionItem5");
    configs.put("eventTopic", "openhab/*");
    configs.put("eventTypes", "ItemStateEvent");
    Configuration triggerConfig = new Configuration(configs);
    configs = new HashMap<>();
    configs.put("operator", "matches");
    configs.put("right", ".*ON.*");
    configs.put("inputproperty", "payload");
    Configuration condition1Config = new Configuration(configs);
    configs = new HashMap<>();
    configs.put("operator", "=");
    configs.put("right", "myMotionItem5");
    configs.put("inputproperty", "itemName");
    Configuration condition2Config = new Configuration(configs);
    configs = new HashMap<>();
    configs.put("itemName", "myLampItem5");
    configs.put("command", "ON");
    Configuration actionConfig = new Configuration(configs);
    String triggerId = "ItemStateChangeTrigger" + random;
    List<Trigger> triggers = List.of(ModuleBuilder.createTrigger().withId(triggerId).withTypeUID("core.GenericEventTrigger").withConfiguration(triggerConfig).build());
    List<Condition> conditions = Stream.of(ModuleBuilder.createCondition().withId("ItemStateCondition" + random).withTypeUID("core.GenericCompareCondition").withConfiguration(condition1Config).withInputs(Map.of("input", triggerId + ".event")).build(), ModuleBuilder.createCondition().withId("ItemStateCondition" + (random + 1)).withTypeUID("core.GenericCompareCondition").withConfiguration(condition2Config).withInputs(Map.of("input", triggerId + ".event")).build()).collect(toList());
    List<Action> actions = List.of(ModuleBuilder.createAction().withId("ItemPostCommandAction" + random).withTypeUID("core.ItemCommandAction").withConfiguration(actionConfig).build());
    Rule rule = RuleBuilder.create("myRule_" + random).withTriggers(triggers).withConditions(conditions).withActions(actions).withName("RuleByJAVA_API" + random).withTags("myRule_" + random).build();
    logger.info("Rule created: {}", rule.getUID());
    ruleRegistry.add(rule);
    ruleEngine.setEnabled(rule.getUID(), true);
    // WAIT until Rule modules types are parsed and the rule becomes IDLE
    waitForAssert(() -> {
        assertThat(ruleRegistry.getAll().isEmpty(), is(false));
        Rule rule2 = ruleRegistry.get(rule.getUID());
        assertThat(rule2, is(notNullValue()));
        RuleStatus ruleStatus2 = ruleEngine.getStatusInfo(rule2.getUID()).getStatus();
        assertThat(ruleStatus2, is(RuleStatus.IDLE));
    }, 10000, 200);
    // TEST RULE
    EventPublisher eventPublisher = getService(EventPublisher.class);
    ItemRegistry itemRegistry = getService(ItemRegistry.class);
    SwitchItem myMotionItem = (SwitchItem) itemRegistry.getItem("myPresenceItem5");
    myMotionItem.setState(OnOffType.ON);
    EventSubscriber itemEventHandler = new EventSubscriber() {

        @Override
        public Set<String> getSubscribedEventTypes() {
            return Set.of(ItemCommandEvent.TYPE);
        }

        @Override
        @Nullable
        public EventFilter getEventFilter() {
            return null;
        }

        @Override
        public void receive(Event e) {
            logger.info("Event: {}", e.getTopic());
            if (e.getTopic().contains("myLampItem5")) {
                itemEvent = e;
            }
        }
    };
    registerService(itemEventHandler);
    eventPublisher.post(ItemEventFactory.createStateEvent("myMotionItem5", OnOffType.ON));
    waitForAssert(() -> {
        assertThat(itemEvent, is(notNullValue()));
    }, 3000, 100);
    assertThat(itemEvent.getTopic(), is(equalTo("openhab/items/myLampItem5/command")));
    assertThat(((ItemCommandEvent) itemEvent).getItemCommand(), is(OnOffType.ON));
}
Also used : Condition(org.openhab.core.automation.Condition) EventSubscriber(org.openhab.core.events.EventSubscriber) Action(org.openhab.core.automation.Action) Configuration(org.openhab.core.config.core.Configuration) EventPublisher(org.openhab.core.events.EventPublisher) HashMap(java.util.HashMap) ItemRegistry(org.openhab.core.items.ItemRegistry) Trigger(org.openhab.core.automation.Trigger) Random(java.util.Random) RuleRemovedEvent(org.openhab.core.automation.events.RuleRemovedEvent) RuleAddedEvent(org.openhab.core.automation.events.RuleAddedEvent) RuleStatusInfoEvent(org.openhab.core.automation.events.RuleStatusInfoEvent) ItemCommandEvent(org.openhab.core.items.events.ItemCommandEvent) Event(org.openhab.core.events.Event) RuleUpdatedEvent(org.openhab.core.automation.events.RuleUpdatedEvent) RuleStatus(org.openhab.core.automation.RuleStatus) Rule(org.openhab.core.automation.Rule) SwitchItem(org.openhab.core.library.items.SwitchItem) Test(org.junit.jupiter.api.Test) JavaOSGiTest(org.openhab.core.test.java.JavaOSGiTest)

Aggregations

Action (org.openhab.core.automation.Action)40 Trigger (org.openhab.core.automation.Trigger)27 Configuration (org.openhab.core.config.core.Configuration)22 Rule (org.openhab.core.automation.Rule)20 HashMap (java.util.HashMap)19 JavaOSGiTest (org.openhab.core.test.java.JavaOSGiTest)16 Test (org.junit.jupiter.api.Test)15 Condition (org.openhab.core.automation.Condition)15 ArrayList (java.util.ArrayList)14 Random (java.util.Random)13 Nullable (org.eclipse.jdt.annotation.Nullable)12 Event (org.openhab.core.events.Event)12 EventSubscriber (org.openhab.core.events.EventSubscriber)12 ItemCommandEvent (org.openhab.core.items.events.ItemCommandEvent)12 RuleRegistry (org.openhab.core.automation.RuleRegistry)9 RuleStatusInfoEvent (org.openhab.core.automation.events.RuleStatusInfoEvent)9 EventPublisher (org.openhab.core.events.EventPublisher)9 NonNullByDefault (org.eclipse.jdt.annotation.NonNullByDefault)8 Item (org.openhab.core.items.Item)8 Logger (org.slf4j.Logger)8