Search in sources :

Example 1 with RuleManager

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

the class RuntimeRuleTest method ruleEnableHandlerWorks.

@Test
@Ignore
public void ruleEnableHandlerWorks() throws ItemNotFoundException {
    final RuleRegistry ruleRegistry = getService(RuleRegistry.class);
    final RuleManager ruleEngine = getService(RuleManager.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 = RuleBuilder.create(firstRuleAction).withTriggers(ModuleBuilder.createTrigger().withId("ItemStateChangeTrigger3").withTypeUID("core.ItemStateChangeTrigger").withConfiguration(triggerConfig).build()).withActions(ModuleBuilder.createAction().withId("RuleAction").withTypeUID("core.RuleEnablementAction").withConfiguration(actionConfig).build()).build();
        ruleRegistry.add(RuleBuilder.create(firstRuleUID).build());
        ruleRegistry.add(RuleBuilder.create(secondRuleUID).build());
        ruleRegistry.add(RuleBuilder.create(thirdRuleUID).build());
        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(RuleStatusDetail.DISABLED, ruleEngine.getStatusInfo(firstRuleUID).getStatusDetail());
            Assert.assertEquals(RuleStatusDetail.DISABLED, ruleEngine.getStatusInfo(secondRuleUID).getStatusDetail());
            Assert.assertEquals(RuleStatus.IDLE, ruleEngine.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 = RuleBuilder.create(secondRuleAction).withTriggers(ModuleBuilder.createTrigger().withId("ItemStateChangeTrigger3").withTypeUID("core.ItemStateChangeTrigger").withConfiguration(triggerConfig2).build()).withActions(ModuleBuilder.createAction().withId("RuleAction").withTypeUID("core.RuleEnablementAction").withConfiguration(actionConfig2).build()).build();
        ruleRegistry.add(rule2);
        eventPublisher.post(ItemEventFactory.createCommandEvent("myMotionItem3", TypeParser.parseCommand(myMotionItem.getAcceptedCommandTypes(), "OFF")));
        waitForAssert(() -> {
            Assert.assertEquals(RuleStatus.IDLE, ruleEngine.getStatus(firstRuleUID));
            Assert.assertEquals(RuleStatusDetail.DISABLED, ruleEngine.getStatusInfo(secondRuleUID).getStatusDetail());
            Assert.assertEquals(RuleStatus.IDLE, ruleEngine.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) Configuration(org.eclipse.smarthome.config.core.Configuration) EventPublisher(org.eclipse.smarthome.core.events.EventPublisher) RuleRegistry(org.eclipse.smarthome.automation.RuleRegistry) RuleManager(org.eclipse.smarthome.automation.RuleManager) 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 2 with RuleManager

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

the class RunRuleModuleTest method sceneActivatedByRule.

@Test
public void sceneActivatedByRule() throws ItemNotFoundException, InterruptedException {
    final RuleRegistry ruleRegistry = getService(RuleRegistry.class);
    final RuleManager ruleEngine = getService(RuleManager.class);
    Assert.assertNotNull(ruleRegistry);
    // Scene rule
    final Rule sceneRule = createSceneRule();
    logger.info("SceneRule created: {}", sceneRule.getUID());
    ruleRegistry.add(sceneRule);
    ruleEngine.setEnabled(sceneRule.getUID(), true);
    waitForAssert(() -> {
        Assert.assertEquals(RuleStatus.IDLE, ruleEngine.getStatusInfo(sceneRule.getUID()).getStatus());
    });
    // Outer rule
    final Rule outerRule = createOuterRule();
    logger.info("SceneActivationRule created: {}", outerRule.getUID());
    ruleRegistry.add(outerRule);
    ruleEngine.setEnabled(outerRule.getUID(), true);
    waitForAssert(() -> {
        Assert.assertEquals(RuleStatus.IDLE, ruleEngine.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) ItemRegistry(org.eclipse.smarthome.core.items.ItemRegistry) EventFilter(org.eclipse.smarthome.core.events.EventFilter) LinkedList(java.util.LinkedList) RuleManager(org.eclipse.smarthome.automation.RuleManager) ItemCommandEvent(org.eclipse.smarthome.core.items.events.ItemCommandEvent) 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 3 with RuleManager

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

the class ScriptRuleTest method testPredefinedRule.

@Test
public void testPredefinedRule() throws ItemNotFoundException {
    EventPublisher eventPublisher = getService(EventPublisher.class);
    ItemRegistry itemRegistry = getService(ItemRegistry.class);
    RuleRegistry ruleRegistry = getService(RuleRegistry.class);
    RuleManager ruleEngine = getService(RuleManager.class);
    // WAIT until Rule modules types are parsed and the rule becomes IDLE
    waitForAssert(() -> {
        assertThat(ruleRegistry.getAll().isEmpty(), is(false));
        Rule rule2 = ruleRegistry.get("javascript.rule1");
        assertThat(rule2, is(notNullValue()));
        RuleStatusInfo ruleStatus2 = ruleEngine.getStatusInfo(rule2.getUID());
        assertThat(ruleStatus2, is(notNullValue()));
        assertThat(ruleStatus2.getStatus(), is(RuleStatus.IDLE));
    }, 10000, 200);
    Rule rule = ruleRegistry.get("javascript.rule1");
    assertThat(rule, is(notNullValue()));
    assertThat(rule.getName(), is("DemoScriptRule"));
    Optional<? extends Trigger> trigger = rule.getTriggers().stream().filter(t -> t.getId().equals("trigger")).findFirst();
    assertThat(trigger.isPresent(), is(true));
    assertThat(trigger.get().getTypeUID(), is("core.GenericEventTrigger"));
    assertThat(trigger.get().getConfiguration().get("eventSource"), is("MyTrigger"));
    assertThat(trigger.get().getConfiguration().get("eventTopic"), is("smarthome/items/MyTrigger/state"));
    assertThat(trigger.get().getConfiguration().get("eventTypes"), is("ItemStateEvent"));
    Optional<? extends Condition> condition1 = rule.getConditions().stream().filter(c -> c.getId().equals("condition")).findFirst();
    assertThat(condition1.isPresent(), is(true));
    assertThat(condition1.get().getTypeUID(), is("script.ScriptCondition"));
    assertThat(condition1.get().getConfiguration().get("type"), is("application/javascript"));
    assertThat(condition1.get().getConfiguration().get("script"), is("event.itemState==ON"));
    Optional<? extends Action> action = rule.getActions().stream().filter(a -> a.getId().equals("action")).findFirst();
    assertThat(action.isPresent(), is(true));
    assertThat(action.get().getTypeUID(), is("script.ScriptAction"));
    assertThat(action.get().getConfiguration().get("type"), is("application/javascript"));
    assertThat(action.get().getConfiguration().get("script"), is("print(items.MyTrigger), print(things.getAll()), print(ctx.get('trigger.event')), events.sendCommand('ScriptItem', 'ON')"));
    RuleStatusInfo ruleStatus = ruleEngine.getStatusInfo(rule.getUID());
    assertThat(ruleStatus.getStatus(), is(RuleStatus.IDLE));
    SwitchItem myTriggerItem = (SwitchItem) itemRegistry.getItem("MyTrigger");
    logger.info("Triggering item: {}", myTriggerItem.getName());
    eventPublisher.post(ItemEventFactory.createStateEvent("MyTrigger", OnOffType.ON));
    waitForAssert(() -> {
        assertThat(receivedEvent, notNullValue());
    });
    assertThat(receivedEvent.getItemName(), is(equalTo("ScriptItem")));
    assertThat(receivedEvent.getItemCommand(), is(OnOffType.ON));
}
Also used : Trigger(org.eclipse.smarthome.automation.Trigger) SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) CoreMatchers(org.hamcrest.CoreMatchers) Arrays(java.util.Arrays) Condition(org.eclipse.smarthome.automation.Condition) ProviderChangeListener(org.eclipse.smarthome.core.common.registry.ProviderChangeListener) ItemCommandEvent(org.eclipse.smarthome.core.items.events.ItemCommandEvent) LoggerFactory(org.slf4j.LoggerFactory) OnOffType(org.eclipse.smarthome.core.library.types.OnOffType) ItemProvider(org.eclipse.smarthome.core.items.ItemProvider) EventFilter(org.eclipse.smarthome.core.events.EventFilter) EventSubscriber(org.eclipse.smarthome.core.events.EventSubscriber) Assert.assertThat(org.junit.Assert.assertThat) RuleManager(org.eclipse.smarthome.automation.RuleManager) 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) RuleStatusInfo(org.eclipse.smarthome.automation.RuleStatusInfo) ItemEventFactory(org.eclipse.smarthome.core.items.events.ItemEventFactory) 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) Item(org.eclipse.smarthome.core.items.Item) ItemRegistry(org.eclipse.smarthome.core.items.ItemRegistry) Optional(java.util.Optional) Action(org.eclipse.smarthome.automation.Action) Event(org.eclipse.smarthome.core.events.Event) Collections(java.util.Collections) NonNull(org.eclipse.jdt.annotation.NonNull) RuleStatusInfo(org.eclipse.smarthome.automation.RuleStatusInfo) EventPublisher(org.eclipse.smarthome.core.events.EventPublisher) RuleRegistry(org.eclipse.smarthome.automation.RuleRegistry) RuleManager(org.eclipse.smarthome.automation.RuleManager) Rule(org.eclipse.smarthome.automation.Rule) ItemRegistry(org.eclipse.smarthome.core.items.ItemRegistry) SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Example 4 with RuleManager

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

the class RuntimeRuleTest method before.

@Before
public void before() {
    ItemProvider itemProvider = new TestItemProvider(Collections.singleton(new SwitchItem("myLampItem")));
    registerService(itemProvider);
    registerService(volatileStorageService);
    waitForAssert(() -> {
        ruleRegistry = getService(RuleRegistry.class);
        assertThat("RuleRegistry service not found", ruleRegistry, is(notNullValue()));
    }, 3000, 100);
    waitForAssert(() -> {
        ruleEngine = getService(RuleManager.class);
        assertThat("RuleManager service not found", ruleEngine, is(notNullValue()));
    }, 3000, 100);
}
Also used : ItemProvider(org.eclipse.smarthome.core.items.ItemProvider) RuleRegistry(org.eclipse.smarthome.automation.RuleRegistry) RuleManager(org.eclipse.smarthome.automation.RuleManager) SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) Before(org.junit.Before)

Example 5 with RuleManager

use of org.eclipse.smarthome.automation.RuleManager 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 = RuleBuilder.create("myRule21" + new Random().nextInt()).withTriggers(ModuleBuilder.createTrigger().withId("ItemStateChangeTrigger2").withTypeUID("core.GenericEventTrigger").withConfiguration(triggerConfig).build()).withActions(ModuleBuilder.createAction().withId("ItemPostCommandAction2").withTypeUID("core.ItemCommandAction").withConfiguration(actionConfig).build()).withName("RuleByJAVA_API").build();
    logger.info("RuleImpl created: {}", rule.getUID());
    final RuleRegistry ruleRegistry = getService(RuleRegistry.class);
    final RuleManager ruleEngine = getService(RuleManager.class);
    ruleRegistry.add(rule);
    ruleEngine.setEnabled(rule.getUID(), true);
    waitForAssert(() -> {
        Assert.assertEquals(RuleStatus.IDLE, ruleEngine.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) 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) 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)

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