Search in sources :

Example 6 with RuleManager

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

the class RuntimeRuleTest method ruleTriggeredByCompositeTrigger.

@Test
public void ruleTriggeredByCompositeTrigger() throws ItemNotFoundException, InterruptedException {
    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 = 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(() -> {
        Assert.assertEquals(RuleStatus.IDLE, ruleEngine.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) 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) Random(java.util.Random) RuleManager(org.eclipse.smarthome.automation.RuleManager) 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 7 with RuleManager

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

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() {
            Set<String> types = new HashSet<>();
            types.add(RuleAddedEvent.TYPE);
            types.add(RuleRemovedEvent.TYPE);
            types.add(RuleStatusInfoEvent.TYPE);
            types.add(RuleUpdatedEvent.TYPE);
            return types;
        }

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

        @Override
        public void receive(Event event) {
            logger.info("RuleEvent: {}", event.getTopic());
            ruleEvents.add(event);
        }
    };
    registerService(ruleEventHandler);
    // Creation of RULE
    Map<String, Object> triggerCfgEntries = new HashMap<>();
    triggerCfgEntries.put("eventSource", "myMotionItem2");
    triggerCfgEntries.put("eventTopic", "smarthome/*");
    triggerCfgEntries.put("eventTypes", "ItemStateEvent");
    Configuration triggerConfig = new Configuration(triggerCfgEntries);
    Map<String, Object> actionCfgEntries = new HashMap<>();
    actionCfgEntries.put("itemName", "myLampItem2");
    actionCfgEntries.put("command", "ON");
    Configuration actionConfig = new Configuration(actionCfgEntries);
    List<Trigger> triggers = Collections.singletonList(ModuleBuilder.createTrigger().withId("ItemStateChangeTrigger2").withTypeUID("core.GenericEventTrigger").withConfiguration(triggerConfig).build());
    List<Action> actions = Collections.singletonList(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 Collections.singleton(ItemCommandEvent.TYPE);
        }

        @Override
        public EventFilter getEventFilter() {
            return null;
        }
    };
    registerService(itemEventHandler);
    eventPublisher.post(ItemEventFactory.createStateEvent("myMotionItem2", OnOffType.ON));
    waitForAssert(() -> assertThat(itemEvent, is(notNullValue())));
    assertThat(itemEvent.getTopic(), is(equalTo("smarthome/items/myLampItem2/command")));
    assertThat(((ItemCommandEvent) itemEvent).getItemCommand(), is(OnOffType.ON));
    assertThat(ruleEvents.size(), is(not(0)));
    assertThat(ruleEvents.stream().filter(e -> e.getTopic().equals("smarthome/rules/myRule21/added")).findFirst().isPresent(), is(true));
    assertThat(ruleEvents.stream().filter(e -> e.getTopic().equals("smarthome/rules/myRule21/state")).findFirst().isPresent(), is(true));
    List<Event> stateEvents = ruleEvents.stream().filter(e -> e.getTopic().equals("smarthome/rules/myRule21/state")).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 Collections.singleton(RuleRemovedEvent.TYPE);
        }

        @Override
        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("smarthome/rules/myRule21/removed")));
    });
}
Also used : Trigger(org.eclipse.smarthome.automation.Trigger) SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) CoreMatchers(org.hamcrest.CoreMatchers) Arrays(java.util.Arrays) ProviderChangeListener(org.eclipse.smarthome.core.common.registry.ProviderChangeListener) ItemCommandEvent(org.eclipse.smarthome.core.items.events.ItemCommandEvent) RuleBuilder(org.eclipse.smarthome.automation.core.util.RuleBuilder) LoggerFactory(org.slf4j.LoggerFactory) OnOffType(org.eclipse.smarthome.core.library.types.OnOffType) HashMap(java.util.HashMap) ItemProvider(org.eclipse.smarthome.core.items.ItemProvider) EventFilter(org.eclipse.smarthome.core.events.EventFilter) EventSubscriber(org.eclipse.smarthome.core.events.EventSubscriber) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) RuleManager(org.eclipse.smarthome.automation.RuleManager) RuleRemovedEvent(org.eclipse.smarthome.automation.events.RuleRemovedEvent) Map(java.util.Map) Configuration(org.eclipse.smarthome.config.core.Configuration) RuleRegistry(org.eclipse.smarthome.automation.RuleRegistry) Before(org.junit.Before) RuleStatus(org.eclipse.smarthome.automation.RuleStatus) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) VolatileStorageService(org.eclipse.smarthome.test.storage.VolatileStorageService) Logger(org.slf4j.Logger) ItemEventFactory(org.eclipse.smarthome.core.items.events.ItemEventFactory) RuleAddedEvent(org.eclipse.smarthome.automation.events.RuleAddedEvent) Collection(java.util.Collection) Set(java.util.Set) EventPublisher(org.eclipse.smarthome.core.events.EventPublisher) Test(org.junit.Test) Rule(org.eclipse.smarthome.automation.Rule) ItemNotFoundException(org.eclipse.smarthome.core.items.ItemNotFoundException) Collectors(java.util.stream.Collectors) Item(org.eclipse.smarthome.core.items.Item) ItemRegistry(org.eclipse.smarthome.core.items.ItemRegistry) RuleStatusInfoEvent(org.eclipse.smarthome.automation.events.RuleStatusInfoEvent) List(java.util.List) Optional(java.util.Optional) Action(org.eclipse.smarthome.automation.Action) ModuleBuilder(org.eclipse.smarthome.automation.core.util.ModuleBuilder) RuleUpdatedEvent(org.eclipse.smarthome.automation.events.RuleUpdatedEvent) Assert(org.junit.Assert) Event(org.eclipse.smarthome.core.events.Event) Collections(java.util.Collections) NonNull(org.eclipse.jdt.annotation.NonNull) Action(org.eclipse.smarthome.automation.Action) Configuration(org.eclipse.smarthome.config.core.Configuration) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ItemRegistry(org.eclipse.smarthome.core.items.ItemRegistry) RuleStatusInfoEvent(org.eclipse.smarthome.automation.events.RuleStatusInfoEvent) Trigger(org.eclipse.smarthome.automation.Trigger) SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) HashSet(java.util.HashSet) EventSubscriber(org.eclipse.smarthome.core.events.EventSubscriber) EventPublisher(org.eclipse.smarthome.core.events.EventPublisher) ItemCommandEvent(org.eclipse.smarthome.core.items.events.ItemCommandEvent) RuleRegistry(org.eclipse.smarthome.automation.RuleRegistry) RuleManager(org.eclipse.smarthome.automation.RuleManager) ItemCommandEvent(org.eclipse.smarthome.core.items.events.ItemCommandEvent) RuleRemovedEvent(org.eclipse.smarthome.automation.events.RuleRemovedEvent) RuleAddedEvent(org.eclipse.smarthome.automation.events.RuleAddedEvent) RuleStatusInfoEvent(org.eclipse.smarthome.automation.events.RuleStatusInfoEvent) RuleUpdatedEvent(org.eclipse.smarthome.automation.events.RuleUpdatedEvent) 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 8 with RuleManager

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

the class AutomationIntegrationJsonTest method before.

// rules imported from
// json files
@Before
public void before() {
    logger.info("@Before.begin");
    getService(ItemRegistry.class);
    ItemProvider itemProvider = new ItemProvider() {

        @Override
        public void addProviderChangeListener(@NonNull ProviderChangeListener<@NonNull Item> listener) {
        }

        @Override
        @NonNull
        public Collection<@NonNull Item> getAll() {
            HashSet<Item> items = new HashSet<>();
            items.add(new SwitchItem("myMotionItem"));
            items.add(new SwitchItem("myPresenceItem"));
            items.add(new SwitchItem("myLampItem"));
            items.add(new SwitchItem("myMotionItem2"));
            items.add(new SwitchItem("myPresenceItem2"));
            items.add(new SwitchItem("myLampItem2"));
            items.add(new SwitchItem("myMotionItem11"));
            items.add(new SwitchItem("myLampItem11"));
            items.add(new SwitchItem("myMotionItem3"));
            items.add(new SwitchItem("templ_MotionItem"));
            items.add(new SwitchItem("templ_LampItem"));
            return items;
        }

        @Override
        public void removeProviderChangeListener(@NonNull ProviderChangeListener<@NonNull Item> listener) {
        }
    };
    registerService(itemProvider);
    registerVolatileStorageService();
    EventSubscriber ruleEventHandler = new EventSubscriber() {

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

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

        @Override
        public void receive(@NonNull Event e) {
            logger.info("RuleEvent: " + e.getTopic() + " --> " + e.getPayload());
            System.out.println("RuleEvent: " + e.getTopic() + " --> " + e.getPayload());
            if (e.getPayload().contains("RUNNING")) {
                ruleEvent = e;
            }
        }
    };
    registerService(ruleEventHandler);
    StorageService storageService = getService(StorageService.class);
    managedRuleProvider = getService(ManagedRuleProvider.class);
    eventPublisher = getService(EventPublisher.class);
    itemRegistry = getService(ItemRegistry.class);
    ruleRegistry = getService(RuleRegistry.class);
    ruleManager = getService(RuleManager.class);
    moduleTypeRegistry = getService(ModuleTypeRegistry.class);
    waitForAssert(() -> {
        assertThat(storageService, is(notNullValue()));
        // sometimes assert fails because EventPublisher service is null
        assertThat(eventPublisher, is(notNullValue()));
        assertThat(itemRegistry, is(notNullValue()));
        assertThat(ruleRegistry, is(notNullValue()));
        assertThat(ruleManager, is(notNullValue()));
        assertThat(managedRuleProvider, is(notNullValue()));
        assertThat(moduleTypeRegistry, is(notNullValue()));
    }, 9000, 1000);
    logger.info("@Before.finish");
}
Also used : ItemProvider(org.eclipse.smarthome.core.items.ItemProvider) EventSubscriber(org.eclipse.smarthome.core.events.EventSubscriber) EventPublisher(org.eclipse.smarthome.core.events.EventPublisher) ModuleTypeRegistry(org.eclipse.smarthome.automation.type.ModuleTypeRegistry) RuleRegistry(org.eclipse.smarthome.automation.RuleRegistry) ManagedRuleProvider(org.eclipse.smarthome.automation.core.ManagedRuleProvider) ItemRegistry(org.eclipse.smarthome.core.items.ItemRegistry) StorageService(org.eclipse.smarthome.core.storage.StorageService) VolatileStorageService(org.eclipse.smarthome.test.storage.VolatileStorageService) SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) Item(org.eclipse.smarthome.core.items.Item) NonNull(org.eclipse.jdt.annotation.NonNull) ProviderChangeListener(org.eclipse.smarthome.core.common.registry.ProviderChangeListener) RuleManager(org.eclipse.smarthome.automation.RuleManager) ItemCommandEvent(org.eclipse.smarthome.core.items.events.ItemCommandEvent) RuleStatusInfoEvent(org.eclipse.smarthome.automation.events.RuleStatusInfoEvent) Event(org.eclipse.smarthome.core.events.Event) SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) HashSet(java.util.HashSet) Before(org.junit.Before)

Aggregations

RuleManager (org.eclipse.smarthome.automation.RuleManager)8 RuleRegistry (org.eclipse.smarthome.automation.RuleRegistry)8 EventPublisher (org.eclipse.smarthome.core.events.EventPublisher)7 Rule (org.eclipse.smarthome.automation.Rule)6 Event (org.eclipse.smarthome.core.events.Event)6 EventSubscriber (org.eclipse.smarthome.core.events.EventSubscriber)6 ItemCommandEvent (org.eclipse.smarthome.core.items.events.ItemCommandEvent)6 JavaOSGiTest (org.eclipse.smarthome.test.java.JavaOSGiTest)6 Test (org.junit.Test)6 Set (java.util.Set)5 EventFilter (org.eclipse.smarthome.core.events.EventFilter)5 ItemRegistry (org.eclipse.smarthome.core.items.ItemRegistry)5 SwitchItem (org.eclipse.smarthome.core.library.items.SwitchItem)5 RuleStatusInfoEvent (org.eclipse.smarthome.automation.events.RuleStatusInfoEvent)4 Configuration (org.eclipse.smarthome.config.core.Configuration)4 Item (org.eclipse.smarthome.core.items.Item)4 ItemProvider (org.eclipse.smarthome.core.items.ItemProvider)4 Before (org.junit.Before)4 LinkedList (java.util.LinkedList)3 NonNull (org.eclipse.jdt.annotation.NonNull)3