Search in sources :

Example 1 with RuleRegistry

use of org.eclipse.smarthome.automation.RuleRegistry in project smarthome by eclipse.

the class RunRuleModuleTest method sceneActivatedByRule.

@Test
public void sceneActivatedByRule() throws ItemNotFoundException, InterruptedException {
    final RuleRegistry ruleRegistry = getService(RuleRegistry.class);
    Assert.assertNotNull(ruleRegistry);
    // Scene rule
    final Rule sceneRule = createSceneRule();
    logger.info("SceneRule created: {}", sceneRule.getUID());
    ruleRegistry.add(sceneRule);
    ruleRegistry.setEnabled(sceneRule.getUID(), true);
    waitForAssert(() -> {
        Assert.assertEquals(RuleStatus.IDLE, ruleRegistry.getStatusInfo(sceneRule.getUID()).getStatus());
    });
    // Outer rule
    final Rule outerRule = createOuterRule();
    logger.info("SceneActivationRule created: {}", outerRule.getUID());
    ruleRegistry.add(outerRule);
    ruleRegistry.setEnabled(outerRule.getUID(), true);
    waitForAssert(() -> {
        Assert.assertEquals(RuleStatus.IDLE, ruleRegistry.getStatusInfo(outerRule.getUID()).getStatus());
    });
    // Test rule
    final EventPublisher eventPublisher = getService(EventPublisher.class);
    Assert.assertNotNull(eventPublisher);
    final ItemRegistry itemRegistry = getService(ItemRegistry.class);
    Assert.assertNotNull(itemRegistry);
    final Queue<Event> events = new LinkedList<>();
    registerService(new EventSubscriber() {

        @Override
        public void receive(final Event event) {
            logger.info("Event: {}", event.getTopic());
            events.add(event);
        }

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

        @Override
        public EventFilter getEventFilter() {
            return null;
        }
    });
    // trigger rule by switching triggerItem ON
    eventPublisher.post(ItemEventFactory.createStateEvent("ruleTrigger", OnOffType.ON));
    waitForAssert(() -> {
        assertFalse(events.isEmpty());
        ItemCommandEvent event = (ItemCommandEvent) events.remove();
        assertEquals("smarthome/items/switch3/command", event.getTopic());
        assertEquals(OnOffType.ON, event.getItemCommand());
    });
}
Also used : EventSubscriber(org.eclipse.smarthome.core.events.EventSubscriber) Set(java.util.Set) EventPublisher(org.eclipse.smarthome.core.events.EventPublisher) ItemCommandEvent(org.eclipse.smarthome.core.items.events.ItemCommandEvent) RuleRegistry(org.eclipse.smarthome.automation.RuleRegistry) ItemCommandEvent(org.eclipse.smarthome.core.items.events.ItemCommandEvent) Event(org.eclipse.smarthome.core.events.Event) Rule(org.eclipse.smarthome.automation.Rule) ItemRegistry(org.eclipse.smarthome.core.items.ItemRegistry) EventFilter(org.eclipse.smarthome.core.events.EventFilter) LinkedList(java.util.LinkedList) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Example 2 with RuleRegistry

use of org.eclipse.smarthome.automation.RuleRegistry in project smarthome by eclipse.

the class RuntimeRuleTest method ruleEnableHandlerWorks.

@Test
@Ignore
public void ruleEnableHandlerWorks() throws ItemNotFoundException {
    final RuleRegistry ruleRegistry = getService(RuleRegistry.class);
    final String firstRuleUID = "FirstTestRule";
    final String secondRuleUID = "SecondTestRule";
    final String thirdRuleUID = "ThirdTestRule";
    final String[] firstConfig = new String[] { "FirstTestRule", "SecondTestRule" };
    final String[] secondConfig = new String[] { "FirstTestRule" };
    final String firstRuleAction = "firstRuleAction";
    final String secondRuleAction = "secondRuleAction";
    try {
        final Configuration triggerConfig = new Configuration(Stream.of(new SimpleEntry<>("itemName", "myMotionItem3")).collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue())));
        final Configuration actionConfig = new Configuration(Stream.of(new SimpleEntry<>("enable", false), new SimpleEntry<>("ruleUIDs", firstConfig)).collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue())));
        final Rule rule = new Rule(firstRuleAction);
        rule.setTriggers(Arrays.asList(new Trigger[] { new Trigger("ItemStateChangeTrigger3", "core.ItemStateChangeTrigger", triggerConfig) }));
        rule.setActions(Arrays.asList(new Action[] { new Action("RuleAction", "core.RuleEnablementAction", actionConfig, null) }));
        ruleRegistry.add(new Rule(firstRuleUID));
        ruleRegistry.add(new Rule(secondRuleUID));
        ruleRegistry.add(new Rule(thirdRuleUID));
        ruleRegistry.add(rule);
        final ItemRegistry itemRegistry = getService(ItemRegistry.class);
        final EventPublisher eventPublisher = getService(EventPublisher.class);
        final Item myMotionItem = itemRegistry.getItem("myMotionItem3");
        eventPublisher.post(ItemEventFactory.createCommandEvent("myMotionItem3", TypeParser.parseCommand(myMotionItem.getAcceptedCommandTypes(), "ON")));
        waitForAssert(() -> {
            Assert.assertEquals(RuleStatus.DISABLED, ruleRegistry.getStatus(firstRuleUID));
            Assert.assertEquals(RuleStatus.DISABLED, ruleRegistry.getStatus(secondRuleUID));
            Assert.assertEquals(RuleStatus.IDLE, ruleRegistry.getStatus(thirdRuleUID));
        });
        final Configuration triggerConfig2 = new Configuration(Stream.of(new SimpleEntry<>("itemName", "myMotionItem3")).collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue())));
        final Configuration actionConfig2 = new Configuration(Stream.of(new SimpleEntry<>("enable", true), new SimpleEntry<>("ruleUIDs", secondConfig)).collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue())));
        final Rule rule2 = new Rule(secondRuleAction);
        rule2.setTriggers(Arrays.asList(new Trigger[] { new Trigger("ItemStateChangeTrigger3", "core.ItemStateChangeTrigger", triggerConfig2) }));
        rule2.setActions(Arrays.asList(new Action[] { new Action("RuleAction", "core.RuleEnablementAction", actionConfig2, null) }));
        ruleRegistry.add(rule2);
        eventPublisher.post(ItemEventFactory.createCommandEvent("myMotionItem3", TypeParser.parseCommand(myMotionItem.getAcceptedCommandTypes(), "OFF")));
        waitForAssert(() -> {
            Assert.assertEquals(RuleStatus.IDLE, ruleRegistry.getStatus(firstRuleUID));
            Assert.assertEquals(RuleStatus.DISABLED, ruleRegistry.getStatus(secondRuleUID));
            Assert.assertEquals(RuleStatus.IDLE, ruleRegistry.getStatus(thirdRuleUID));
        });
    } finally {
        ruleRegistry.remove(firstRuleUID);
        ruleRegistry.remove(secondRuleUID);
        ruleRegistry.remove(thirdRuleUID);
        ruleRegistry.remove(firstRuleAction);
        ruleRegistry.remove(secondRuleAction);
    }
}
Also used : SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) Item(org.eclipse.smarthome.core.items.Item) Action(org.eclipse.smarthome.automation.Action) Trigger(org.eclipse.smarthome.automation.Trigger) Configuration(org.eclipse.smarthome.config.core.Configuration) EventPublisher(org.eclipse.smarthome.core.events.EventPublisher) RuleRegistry(org.eclipse.smarthome.automation.RuleRegistry) Rule(org.eclipse.smarthome.automation.Rule) ItemRegistry(org.eclipse.smarthome.core.items.ItemRegistry) Ignore(org.junit.Ignore) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Example 3 with RuleRegistry

use of org.eclipse.smarthome.automation.RuleRegistry in project smarthome by eclipse.

the class RuntimeRuleTest method ruleTriggeredByCompositeTrigger.

@Test
public void ruleTriggeredByCompositeTrigger() throws ItemNotFoundException, InterruptedException {
    // //Test the creation of a rule out of
    final Configuration triggerConfig = new Configuration(Stream.of(new SimpleEntry<>("itemName", "myMotionItem3")).collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue())));
    final Configuration actionConfig = new Configuration(Stream.of(new SimpleEntry<>("itemName", "myLampItem3"), new SimpleEntry<>("command", "ON")).collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue())));
    final Rule rule = new Rule("myRule21" + new Random().nextInt() + "_COMPOSITE");
    rule.setTriggers(Arrays.asList(new Trigger[] { new Trigger("ItemStateChangeTrigger3", "core.ItemStateChangeTrigger", triggerConfig) }));
    rule.setActions(Arrays.asList(new Action[] { new Action("ItemPostCommandAction3", "core.ItemCommandAction", actionConfig, null) }));
    rule.setName("RuleByJAVA_API_WithCompositeTrigger");
    logger.info("Rule created: {}", rule.getUID());
    final RuleRegistry ruleRegistry = getService(RuleRegistry.class);
    ruleRegistry.add(rule);
    // Test rule
    waitForAssert(() -> {
        Assert.assertEquals(RuleStatus.IDLE, ruleRegistry.getStatusInfo(rule.getUID()).getStatus());
    });
    final Queue<Event> events = new LinkedList<>();
    registerService(new EventSubscriber() {

        @Override
        public void receive(final Event event) {
            logger.info("RuleEvent: {}", event.getTopic());
            events.add(event);
        }

        @Override
        public Set<String> getSubscribedEventTypes() {
            return Collections.singleton(RuleStatusInfoEvent.TYPE);
        }

        @Override
        public EventFilter getEventFilter() {
            return null;
        }
    });
    final EventPublisher eventPublisher = getService(EventPublisher.class);
    eventPublisher.post(ItemEventFactory.createStateEvent("myPresenceItem3", OnOffType.ON));
    eventPublisher.post(ItemEventFactory.createStateEvent("myMotionItem3", OnOffType.ON));
    waitForAssert(() -> {
        assertFalse(events.isEmpty());
        RuleStatusInfoEvent event = (RuleStatusInfoEvent) events.remove();
        assertEquals(RuleStatus.RUNNING, event.getStatusInfo().getStatus());
    });
    waitForAssert(() -> {
        assertFalse(events.isEmpty());
        RuleStatusInfoEvent event = (RuleStatusInfoEvent) events.remove();
        assertEquals(RuleStatus.IDLE, event.getStatusInfo().getStatus());
    });
}
Also used : EventSubscriber(org.eclipse.smarthome.core.events.EventSubscriber) Action(org.eclipse.smarthome.automation.Action) Set(java.util.Set) Configuration(org.eclipse.smarthome.config.core.Configuration) EventPublisher(org.eclipse.smarthome.core.events.EventPublisher) RuleRegistry(org.eclipse.smarthome.automation.RuleRegistry) EventFilter(org.eclipse.smarthome.core.events.EventFilter) LinkedList(java.util.LinkedList) RuleStatusInfoEvent(org.eclipse.smarthome.automation.events.RuleStatusInfoEvent) Trigger(org.eclipse.smarthome.automation.Trigger) Random(java.util.Random) ItemCommandEvent(org.eclipse.smarthome.core.items.events.ItemCommandEvent) RuleStatusInfoEvent(org.eclipse.smarthome.automation.events.RuleStatusInfoEvent) Event(org.eclipse.smarthome.core.events.Event) Rule(org.eclipse.smarthome.automation.Rule) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Example 4 with RuleRegistry

use of org.eclipse.smarthome.automation.RuleRegistry in project smarthome by eclipse.

the class RuntimeRuleTest method itemStateUpdatedBySimpleRule.

@Test
public void itemStateUpdatedBySimpleRule() throws ItemNotFoundException, InterruptedException {
    final Configuration triggerConfig = new Configuration(Stream.of(new SimpleEntry<>("eventSource", "myMotionItem2"), new SimpleEntry<>("eventTopic", "smarthome/*"), new SimpleEntry<>("eventTypes", "ItemStateEvent")).collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue())));
    final Configuration actionConfig = new Configuration(Stream.of(new SimpleEntry<>("itemName", "myLampItem2"), new SimpleEntry<>("command", "ON")).collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue())));
    final Rule rule = new Rule("myRule21" + new Random().nextInt());
    rule.setTriggers(Arrays.asList(new Trigger[] { new Trigger("ItemStateChangeTrigger2", "core.GenericEventTrigger", triggerConfig) }));
    rule.setActions(Arrays.asList(new Action[] { new Action("ItemPostCommandAction2", "core.ItemCommandAction", actionConfig, null) }));
    // I would expect the factory to create the UID of the rule and the name to be in the list of parameters.
    rule.setName("RuleByJAVA_API");
    logger.info("Rule created: {}", rule.getUID());
    final RuleRegistry ruleRegistry = getService(RuleRegistry.class);
    ruleRegistry.add(rule);
    ruleRegistry.setEnabled(rule.getUID(), true);
    waitForAssert(() -> {
        Assert.assertEquals(RuleStatus.IDLE, ruleRegistry.getStatusInfo(rule.getUID()).getStatus());
    });
    // Test rule
    final EventPublisher eventPublisher = getService(EventPublisher.class);
    Assert.assertNotNull(eventPublisher);
    eventPublisher.post(ItemEventFactory.createStateEvent("myPresenceItem2", OnOffType.ON));
    final Queue<Event> events = new LinkedList<>();
    registerService(new EventSubscriber() {

        @Override
        public void receive(final Event event) {
            logger.info("Event: {}", event.getTopic());
            events.add(event);
        }

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

        @Override
        public EventFilter getEventFilter() {
            return null;
        }
    });
    eventPublisher.post(ItemEventFactory.createStateEvent("myMotionItem2", OnOffType.ON));
    waitForAssert(() -> {
        assertFalse(events.isEmpty());
        ItemCommandEvent event = (ItemCommandEvent) events.remove();
        assertEquals("smarthome/items/myLampItem2/command", event.getTopic());
        assertEquals(OnOffType.ON, event.getItemCommand());
    });
}
Also used : EventSubscriber(org.eclipse.smarthome.core.events.EventSubscriber) Action(org.eclipse.smarthome.automation.Action) Set(java.util.Set) Configuration(org.eclipse.smarthome.config.core.Configuration) EventPublisher(org.eclipse.smarthome.core.events.EventPublisher) ItemCommandEvent(org.eclipse.smarthome.core.items.events.ItemCommandEvent) RuleRegistry(org.eclipse.smarthome.automation.RuleRegistry) EventFilter(org.eclipse.smarthome.core.events.EventFilter) LinkedList(java.util.LinkedList) Trigger(org.eclipse.smarthome.automation.Trigger) Random(java.util.Random) ItemCommandEvent(org.eclipse.smarthome.core.items.events.ItemCommandEvent) RuleStatusInfoEvent(org.eclipse.smarthome.automation.events.RuleStatusInfoEvent) Event(org.eclipse.smarthome.core.events.Event) Rule(org.eclipse.smarthome.automation.Rule) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Aggregations

Rule (org.eclipse.smarthome.automation.Rule)4 RuleRegistry (org.eclipse.smarthome.automation.RuleRegistry)4 EventPublisher (org.eclipse.smarthome.core.events.EventPublisher)4 JavaOSGiTest (org.eclipse.smarthome.test.java.JavaOSGiTest)4 Test (org.junit.Test)4 LinkedList (java.util.LinkedList)3 Set (java.util.Set)3 Action (org.eclipse.smarthome.automation.Action)3 Trigger (org.eclipse.smarthome.automation.Trigger)3 Configuration (org.eclipse.smarthome.config.core.Configuration)3 Event (org.eclipse.smarthome.core.events.Event)3 EventFilter (org.eclipse.smarthome.core.events.EventFilter)3 EventSubscriber (org.eclipse.smarthome.core.events.EventSubscriber)3 ItemCommandEvent (org.eclipse.smarthome.core.items.events.ItemCommandEvent)3 Random (java.util.Random)2 RuleStatusInfoEvent (org.eclipse.smarthome.automation.events.RuleStatusInfoEvent)2 ItemRegistry (org.eclipse.smarthome.core.items.ItemRegistry)2 Item (org.eclipse.smarthome.core.items.Item)1 SwitchItem (org.eclipse.smarthome.core.library.items.SwitchItem)1 Ignore (org.junit.Ignore)1