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