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