Search in sources :

Example 1 with RuleStatusInfoEvent

use of org.openhab.core.automation.events.RuleStatusInfoEvent in project openhab-core by openhab.

the class RuleEventTest method testRuleEvents.

@Test
public void testRuleEvents() throws ItemNotFoundException {
    // Registering eventSubscriber
    List<Event> ruleEvents = new ArrayList<>();
    EventSubscriber ruleEventHandler = new EventSubscriber() {

        @Override
        public Set<String> getSubscribedEventTypes() {
            return Set.of(RuleAddedEvent.TYPE, RuleRemovedEvent.TYPE, RuleStatusInfoEvent.TYPE, RuleUpdatedEvent.TYPE);
        }

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

        @Override
        public void receive(Event event) {
            logger.info("RuleEvent: {}", event.getTopic());
            ruleEvents.add(event);
        }
    };
    registerService(ruleEventHandler);
    // Creation of RULE
    Configuration triggerConfig = new Configuration(Map.ofEntries(entry("eventSource", "myMotionItem2"), entry("eventTopic", "openhab/*"), entry("eventTypes", "ItemStateEvent")));
    Configuration actionConfig = new Configuration(Map.ofEntries(entry("itemName", "myLampItem2"), entry("command", "ON")));
    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("RuleEventTestingRule").build();
    logger.info("Rule created: {}", rule.getUID());
    RuleRegistry ruleRegistry = getService(RuleRegistry.class);
    RuleManager ruleEngine = getService(RuleManager.class);
    ruleRegistry.add(rule);
    ruleEngine.setEnabled(rule.getUID(), true);
    waitForAssert(() -> {
        assertThat(ruleEngine.getStatusInfo(rule.getUID()).getStatus(), is(RuleStatus.IDLE));
    });
    // TEST RULE
    EventPublisher eventPublisher = getService(EventPublisher.class);
    ItemRegistry itemRegistry = getService(ItemRegistry.class);
    SwitchItem myMotionItem = (SwitchItem) itemRegistry.getItem("myMotionItem2");
    assertNotNull(myMotionItem);
    eventPublisher.post(ItemEventFactory.createStateEvent("myPresenceItem2", OnOffType.ON));
    EventSubscriber itemEventHandler = new EventSubscriber() {

        @Override
        public void receive(Event event) {
            logger.info("Event: {}", event.getTopic());
            if (event instanceof ItemCommandEvent && event.getTopic().contains("myLampItem2")) {
                itemEvent = event;
            }
        }

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

        @Override
        @Nullable
        public EventFilter getEventFilter() {
            return null;
        }
    };
    registerService(itemEventHandler);
    eventPublisher.post(ItemEventFactory.createStateEvent("myMotionItem2", OnOffType.ON));
    waitForAssert(() -> assertThat(itemEvent, is(notNullValue())));
    assertThat(itemEvent.getTopic(), is(equalTo("openhab/items/myLampItem2/command")));
    assertThat(((ItemCommandEvent) itemEvent).getItemCommand(), is(OnOffType.ON));
    assertThat(ruleEvents.size(), is(not(0)));
    assertThat(ruleEvents.stream().filter(e -> "openhab/rules/myRule21/added".equals(e.getTopic())).findFirst().isPresent(), is(true));
    assertThat(ruleEvents.stream().filter(e -> "openhab/rules/myRule21/state".equals(e.getTopic())).findFirst().isPresent(), is(true));
    List<Event> stateEvents = ruleEvents.stream().filter(e -> "openhab/rules/myRule21/state".equals(e.getTopic())).collect(Collectors.toList());
    assertThat(stateEvents, is(notNullValue()));
    Optional<Event> runningEvent = stateEvents.stream().filter(e -> ((RuleStatusInfoEvent) e).getStatusInfo().getStatus() == RuleStatus.RUNNING).findFirst();
    assertThat(runningEvent.isPresent(), is(true));
    EventSubscriber ruleRemovedEventHandler = new EventSubscriber() {

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

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

        @Override
        public void receive(Event event) {
            logger.info("RuleRemovedEvent: {}", event.getTopic());
            ruleRemovedEvent = event;
        }
    };
    registerService(ruleRemovedEventHandler);
    ruleRegistry.remove("myRule21");
    waitForAssert(() -> {
        assertThat(ruleRemovedEvent, is(notNullValue()));
        assertThat(ruleRemovedEvent.getTopic(), is(equalTo("openhab/rules/myRule21/removed")));
    });
}
Also used : ItemEventFactory(org.openhab.core.items.events.ItemEventFactory) CoreMatchers(org.hamcrest.CoreMatchers) BeforeEach(org.junit.jupiter.api.BeforeEach) EventSubscriber(org.openhab.core.events.EventSubscriber) RuleRegistry(org.openhab.core.automation.RuleRegistry) SwitchItem(org.openhab.core.library.items.SwitchItem) RuleManager(org.openhab.core.automation.RuleManager) RuleBuilder(org.openhab.core.automation.util.RuleBuilder) LoggerFactory(org.slf4j.LoggerFactory) OnOffType(org.openhab.core.library.types.OnOffType) Checks.requireNonNull(org.eclipse.jdt.annotation.Checks.requireNonNull) Trigger(org.openhab.core.automation.Trigger) RuleEngineImpl(org.openhab.core.automation.internal.RuleEngineImpl) ArrayList(java.util.ArrayList) Nullable(org.eclipse.jdt.annotation.Nullable) Configuration(org.openhab.core.config.core.Configuration) Map(java.util.Map) ModuleBuilder(org.openhab.core.automation.util.ModuleBuilder) EventFilter(org.openhab.core.events.EventFilter) Map.entry(java.util.Map.entry) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) RuleStatusInfoEvent(org.openhab.core.automation.events.RuleStatusInfoEvent) EventPublisher(org.openhab.core.events.EventPublisher) ItemCommandEvent(org.openhab.core.items.events.ItemCommandEvent) RuleRemovedEvent(org.openhab.core.automation.events.RuleRemovedEvent) NonNullByDefault(org.eclipse.jdt.annotation.NonNullByDefault) Logger(org.slf4j.Logger) ItemProvider(org.openhab.core.items.ItemProvider) Collection(java.util.Collection) Action(org.openhab.core.automation.Action) Event(org.openhab.core.events.Event) ItemNotFoundException(org.openhab.core.items.ItemNotFoundException) Set(java.util.Set) ReadyMarker(org.openhab.core.service.ReadyMarker) Collectors(java.util.stream.Collectors) Rule(org.openhab.core.automation.Rule) Item(org.openhab.core.items.Item) Test(org.junit.jupiter.api.Test) ItemRegistry(org.openhab.core.items.ItemRegistry) RuleStatus(org.openhab.core.automation.RuleStatus) List(java.util.List) RuleUpdatedEvent(org.openhab.core.automation.events.RuleUpdatedEvent) RuleAddedEvent(org.openhab.core.automation.events.RuleAddedEvent) JavaOSGiTest(org.openhab.core.test.java.JavaOSGiTest) Assertions(org.junit.jupiter.api.Assertions) Optional(java.util.Optional) ProviderChangeListener(org.openhab.core.common.registry.ProviderChangeListener) 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) ItemCommandEvent(org.openhab.core.items.events.ItemCommandEvent) RuleRegistry(org.openhab.core.automation.RuleRegistry) ArrayList(java.util.ArrayList) ItemRegistry(org.openhab.core.items.ItemRegistry) RuleStatusInfoEvent(org.openhab.core.automation.events.RuleStatusInfoEvent) Trigger(org.openhab.core.automation.Trigger) RuleManager(org.openhab.core.automation.RuleManager) RuleStatusInfoEvent(org.openhab.core.automation.events.RuleStatusInfoEvent) ItemCommandEvent(org.openhab.core.items.events.ItemCommandEvent) RuleRemovedEvent(org.openhab.core.automation.events.RuleRemovedEvent) Event(org.openhab.core.events.Event) RuleUpdatedEvent(org.openhab.core.automation.events.RuleUpdatedEvent) RuleAddedEvent(org.openhab.core.automation.events.RuleAddedEvent) 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)

Example 2 with RuleStatusInfoEvent

use of org.openhab.core.automation.events.RuleStatusInfoEvent in project openhab-core by openhab.

the class AutomationIntegrationTest method assertThatARuleWithConnectionsIsExecuted.

@Test
public void assertThatARuleWithConnectionsIsExecuted() {
    logger.info("assert that a rule with connections is executed");
    Map<String, Object> params = new HashMap<>();
    params.put("eventSource", "myMotionItem3");
    params.put("eventTopic", "openhab/*");
    params.put("eventTypes", "ItemStateEvent");
    Configuration triggerConfig = new Configuration(params);
    params = new HashMap<>();
    params.put("eventTopic", "openhab/*");
    Configuration condition1Config = new Configuration(params);
    params = new HashMap<>();
    params.put("itemName", "myLampItem3");
    params.put("command", "ON");
    Configuration actionConfig = new Configuration(params);
    List<Trigger> triggers = List.of(ModuleBuilder.createTrigger().withId("ItemStateChangeTrigger").withTypeUID("core.GenericEventTrigger").withConfiguration(triggerConfig).build());
    Map<String, String> inputs = new HashMap<>();
    inputs.put("topic", "ItemStateChangeTrigger.topic");
    inputs.put("event", "ItemStateChangeTrigger.event");
    List<Condition> conditions = List.of(ModuleBuilder.createCondition().withId("EventCondition_2").withTypeUID("core.GenericEventCondition").withConfiguration(condition1Config).withInputs(inputs).build());
    List<Action> actions = List.of(ModuleBuilder.createAction().withId("ItemPostCommandAction2").withTypeUID("core.ItemCommandAction").withConfiguration(actionConfig).build());
    Rule rule = RuleBuilder.create("myRule21_ConnectionTest").withTriggers(triggers).withConditions(conditions).withActions(actions).withName("RuleByJAVA_API" + new Random().nextInt()).build();
    ruleRegistry.add(rule);
    logger.info("Rule created and added: {}", rule.getUID());
    List<RuleStatusInfoEvent> ruleEvents = new CopyOnWriteArrayList<>();
    EventSubscriber ruleEventHandler = new EventSubscriber() {

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

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

        @Override
        public void receive(Event e) {
            logger.info("RuleEvent: {}", e.getTopic());
            ruleEvents.add((RuleStatusInfoEvent) e);
        }
    };
    registerService(ruleEventHandler);
    eventPublisher.post(ItemEventFactory.createStateEvent("myMotionItem3", OnOffType.ON));
    waitForAssert(() -> {
        assertTrue(ruleEvents.stream().filter(r -> r.getStatusInfo().getStatus() == RuleStatus.RUNNING).findFirst().isPresent());
    });
    waitForAssert(() -> {
        assertThat(ruleEngine.getStatusInfo(rule.getUID()).getStatus(), is(not(RuleStatus.RUNNING)));
    });
}
Also used : Condition(org.openhab.core.automation.Condition) RuleTemplateProvider(org.openhab.core.automation.template.RuleTemplateProvider) ItemEventFactory(org.openhab.core.items.events.ItemEventFactory) CoreMatchers(org.hamcrest.CoreMatchers) BeforeEach(org.junit.jupiter.api.BeforeEach) RuleManager(org.openhab.core.automation.RuleManager) RuleBuilder(org.openhab.core.automation.util.RuleBuilder) LoggerFactory(org.slf4j.LoggerFactory) OnOffType(org.openhab.core.library.types.OnOffType) Random(java.util.Random) ModuleType(org.openhab.core.automation.type.ModuleType) Trigger(org.openhab.core.automation.Trigger) RuleEngineImpl(org.openhab.core.automation.internal.RuleEngineImpl) ManagedRuleProvider(org.openhab.core.automation.ManagedRuleProvider) StorageService(org.openhab.core.storage.StorageService) Nullable(org.eclipse.jdt.annotation.Nullable) Configuration(org.openhab.core.config.core.Configuration) Locale(java.util.Locale) Map(java.util.Map) EventPublisher(org.openhab.core.events.EventPublisher) RuleRemovedEvent(org.openhab.core.automation.events.RuleRemovedEvent) NonNullByDefault(org.eclipse.jdt.annotation.NonNullByDefault) Collection(java.util.Collection) Action(org.openhab.core.automation.Action) Set(java.util.Set) RuleProvider(org.openhab.core.automation.RuleProvider) ConfigDescriptionParameter(org.openhab.core.config.core.ConfigDescriptionParameter) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) RuleStatus(org.openhab.core.automation.RuleStatus) List(java.util.List) TemplateRegistry(org.openhab.core.automation.template.TemplateRegistry) ConfigDescriptionParameterBuilder(org.openhab.core.config.core.ConfigDescriptionParameterBuilder) Stream(java.util.stream.Stream) RuleAddedEvent(org.openhab.core.automation.events.RuleAddedEvent) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) TriggerType(org.openhab.core.automation.type.TriggerType) EventSubscriber(org.openhab.core.events.EventSubscriber) RuleRegistry(org.openhab.core.automation.RuleRegistry) SwitchItem(org.openhab.core.library.items.SwitchItem) HashMap(java.util.HashMap) HashSet(java.util.HashSet) ModuleBuilder(org.openhab.core.automation.util.ModuleBuilder) EventFilter(org.openhab.core.events.EventFilter) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) RuleStatusInfoEvent(org.openhab.core.automation.events.RuleStatusInfoEvent) Condition(org.openhab.core.automation.Condition) Visibility(org.openhab.core.automation.Visibility) ItemCommandEvent(org.openhab.core.items.events.ItemCommandEvent) Logger(org.slf4j.Logger) ItemProvider(org.openhab.core.items.ItemProvider) Event(org.openhab.core.events.Event) ItemNotFoundException(org.openhab.core.items.ItemNotFoundException) ReadyMarker(org.openhab.core.service.ReadyMarker) Rule(org.openhab.core.automation.Rule) Item(org.openhab.core.items.Item) ItemRegistry(org.openhab.core.items.ItemRegistry) AfterEach(org.junit.jupiter.api.AfterEach) RuleUpdatedEvent(org.openhab.core.automation.events.RuleUpdatedEvent) ActionType(org.openhab.core.automation.type.ActionType) JavaOSGiTest(org.openhab.core.test.java.JavaOSGiTest) ModuleTypeProvider(org.openhab.core.automation.type.ModuleTypeProvider) ProviderChangeListener(org.openhab.core.common.registry.ProviderChangeListener) Collections(java.util.Collections) Template(org.openhab.core.automation.template.Template) ModuleTypeRegistry(org.openhab.core.automation.type.ModuleTypeRegistry) RuleTemplate(org.openhab.core.automation.template.RuleTemplate) EventSubscriber(org.openhab.core.events.EventSubscriber) Action(org.openhab.core.automation.Action) Configuration(org.openhab.core.config.core.Configuration) HashMap(java.util.HashMap) RuleStatusInfoEvent(org.openhab.core.automation.events.RuleStatusInfoEvent) 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) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Test(org.junit.jupiter.api.Test) JavaOSGiTest(org.openhab.core.test.java.JavaOSGiTest)

Example 3 with RuleStatusInfoEvent

use of org.openhab.core.automation.events.RuleStatusInfoEvent in project openhab-core by openhab.

the class RuntimeRuleTest method ruleTriggeredByCompositeTrigger.

@Test
public void ruleTriggeredByCompositeTrigger() throws ItemNotFoundException, InterruptedException {
    final Configuration triggerConfig = new Configuration(Map.of("itemName", "myMotionItem3"));
    final Configuration actionConfig = new Configuration(Map.ofEntries(entry("itemName", "myLampItem3"), entry("command", "ON")));
    final Rule rule = RuleBuilder.create("myRule21" + new Random().nextInt() + "_COMPOSITE").withTriggers(ModuleBuilder.createTrigger().withId("ItemStateChangeTrigger3").withTypeUID("core.ItemStateChangeTrigger").withConfiguration(triggerConfig).build()).withActions(ModuleBuilder.createAction().withId("ItemPostCommandAction3").withTypeUID("core.ItemCommandAction").withConfiguration(actionConfig).build()).withName("RuleByJAVA_API_WithCompositeTrigger").build();
    logger.info("RuleImpl created: {}", rule.getUID());
    final RuleRegistry ruleRegistry = getService(RuleRegistry.class);
    final RuleManager ruleEngine = getService(RuleManager.class);
    ruleRegistry.add(rule);
    // Test rule
    waitForAssert(() -> {
        assertEquals(RuleStatus.IDLE, ruleEngine.getStatusInfo(rule.getUID()).getStatus());
    });
    final Queue<Event> events = new LinkedList<>();
    subscribeToEvents(RuleStatusInfoEvent.TYPE, events);
    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 : RuleStatusInfoEvent(org.openhab.core.automation.events.RuleStatusInfoEvent) Configuration(org.openhab.core.config.core.Configuration) Random(java.util.Random) EventPublisher(org.openhab.core.events.EventPublisher) RuleRegistry(org.openhab.core.automation.RuleRegistry) RuleManager(org.openhab.core.automation.RuleManager) RuleStatusInfoEvent(org.openhab.core.automation.events.RuleStatusInfoEvent) ItemCommandEvent(org.openhab.core.items.events.ItemCommandEvent) Event(org.openhab.core.events.Event) Rule(org.openhab.core.automation.Rule) LinkedList(java.util.LinkedList) Test(org.junit.jupiter.api.Test) JavaOSGiTest(org.openhab.core.test.java.JavaOSGiTest)

Aggregations

Test (org.junit.jupiter.api.Test)3 Rule (org.openhab.core.automation.Rule)3 RuleManager (org.openhab.core.automation.RuleManager)3 RuleRegistry (org.openhab.core.automation.RuleRegistry)3 RuleStatusInfoEvent (org.openhab.core.automation.events.RuleStatusInfoEvent)3 Configuration (org.openhab.core.config.core.Configuration)3 Event (org.openhab.core.events.Event)3 EventPublisher (org.openhab.core.events.EventPublisher)3 ItemCommandEvent (org.openhab.core.items.events.ItemCommandEvent)3 JavaOSGiTest (org.openhab.core.test.java.JavaOSGiTest)3 Collection (java.util.Collection)2 List (java.util.List)2 Map (java.util.Map)2 Random (java.util.Random)2 Set (java.util.Set)2 Collectors (java.util.stream.Collectors)2 NonNullByDefault (org.eclipse.jdt.annotation.NonNullByDefault)2 Nullable (org.eclipse.jdt.annotation.Nullable)2 CoreMatchers (org.hamcrest.CoreMatchers)2 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)2