Search in sources :

Example 1 with RuleStatusInfoEvent

use of org.eclipse.smarthome.automation.events.RuleStatusInfoEvent 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)

Aggregations

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