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);
}
}
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());
});
}
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));
}
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);
}
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());
});
}
Aggregations