use of org.eclipse.smarthome.core.events.EventPublisher in project smarthome by eclipse.
the class RunRuleModuleTest method sceneActivatedByRule.
@Test
public void sceneActivatedByRule() throws ItemNotFoundException, InterruptedException {
final RuleRegistry ruleRegistry = getService(RuleRegistry.class);
Assert.assertNotNull(ruleRegistry);
// Scene rule
final Rule sceneRule = createSceneRule();
logger.info("SceneRule created: {}", sceneRule.getUID());
ruleRegistry.add(sceneRule);
ruleRegistry.setEnabled(sceneRule.getUID(), true);
waitForAssert(() -> {
Assert.assertEquals(RuleStatus.IDLE, ruleRegistry.getStatusInfo(sceneRule.getUID()).getStatus());
});
// Outer rule
final Rule outerRule = createOuterRule();
logger.info("SceneActivationRule created: {}", outerRule.getUID());
ruleRegistry.add(outerRule);
ruleRegistry.setEnabled(outerRule.getUID(), true);
waitForAssert(() -> {
Assert.assertEquals(RuleStatus.IDLE, ruleRegistry.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.core.events.EventPublisher in project smarthome by eclipse.
the class RuntimeRuleTest method testPredefinedRule.
@Test
@Ignore
public void testPredefinedRule() throws ItemNotFoundException, InterruptedException {
final EventPublisher eventPublisher = getService(EventPublisher.class);
// final ItemRegistry itemRegistry = getService(ItemRegistry.class);
// final SwitchItem myMotionItem = (SwitchItem) itemRegistry.getItem("myMotionItem");
// eventPublisher.post(ItemEventFactory.createStateEvent("myPresenceItem", 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("myMotionItem", OnOffType.ON));
waitForAssert(() -> {
assertFalse(events.isEmpty());
ItemCommandEvent event = (ItemCommandEvent) events.remove();
assertEquals("smarthome/items/myLampItem/command", event.getTopic());
assertEquals(OnOffType.ON, event.getItemCommand());
});
}
use of org.eclipse.smarthome.core.events.EventPublisher in project smarthome by eclipse.
the class RuntimeRuleTest method ruleEnableHandlerWorks.
@Test
@Ignore
public void ruleEnableHandlerWorks() throws ItemNotFoundException {
final RuleRegistry ruleRegistry = getService(RuleRegistry.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 = new Rule(firstRuleAction);
rule.setTriggers(Arrays.asList(new Trigger[] { new Trigger("ItemStateChangeTrigger3", "core.ItemStateChangeTrigger", triggerConfig) }));
rule.setActions(Arrays.asList(new Action[] { new Action("RuleAction", "core.RuleEnablementAction", actionConfig, null) }));
ruleRegistry.add(new Rule(firstRuleUID));
ruleRegistry.add(new Rule(secondRuleUID));
ruleRegistry.add(new Rule(thirdRuleUID));
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(RuleStatus.DISABLED, ruleRegistry.getStatus(firstRuleUID));
Assert.assertEquals(RuleStatus.DISABLED, ruleRegistry.getStatus(secondRuleUID));
Assert.assertEquals(RuleStatus.IDLE, ruleRegistry.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 = new Rule(secondRuleAction);
rule2.setTriggers(Arrays.asList(new Trigger[] { new Trigger("ItemStateChangeTrigger3", "core.ItemStateChangeTrigger", triggerConfig2) }));
rule2.setActions(Arrays.asList(new Action[] { new Action("RuleAction", "core.RuleEnablementAction", actionConfig2, null) }));
ruleRegistry.add(rule2);
eventPublisher.post(ItemEventFactory.createCommandEvent("myMotionItem3", TypeParser.parseCommand(myMotionItem.getAcceptedCommandTypes(), "OFF")));
waitForAssert(() -> {
Assert.assertEquals(RuleStatus.IDLE, ruleRegistry.getStatus(firstRuleUID));
Assert.assertEquals(RuleStatus.DISABLED, ruleRegistry.getStatus(secondRuleUID));
Assert.assertEquals(RuleStatus.IDLE, ruleRegistry.getStatus(thirdRuleUID));
});
} finally {
ruleRegistry.remove(firstRuleUID);
ruleRegistry.remove(secondRuleUID);
ruleRegistry.remove(thirdRuleUID);
ruleRegistry.remove(firstRuleAction);
ruleRegistry.remove(secondRuleAction);
}
}
use of org.eclipse.smarthome.core.events.EventPublisher in project smarthome by eclipse.
the class ScriptEngineOSGiTest method setup.
@Before
public void setup() {
registerVolatileStorageService();
EventPublisher eventPublisher = event -> {
};
registerService(eventPublisher);
itemRegistry = getService(ItemRegistry.class);
assertNotNull(itemRegistry);
itemProvider = new ItemProvider() {
@Override
public void addProviderChangeListener(ProviderChangeListener<Item> listener) {
}
@Override
public Collection<Item> getAll() {
return Lists.newArrayList(new SwitchItem(ITEM_NAME), createNumberItem(NUMBER_ITEM_TEMPERATURE, Temperature.class), createNumberItem(NUMBER_ITEM_LENGTH, Length.class), new NumberItem(NUMBER_ITEM_DECIMAL));
}
@Override
public void removeProviderChangeListener(ProviderChangeListener<Item> listener) {
}
};
registerService(itemProvider);
ScriptServiceUtil scriptServiceUtil = getService(ScriptServiceUtil.class);
assertNotNull(scriptServiceUtil);
scriptEngine = ScriptServiceUtil.getScriptEngine();
}
use of org.eclipse.smarthome.core.events.EventPublisher in project smarthome by eclipse.
the class BusEvent method sendCommand.
/**
* Sends a command for a specified item to the event bus.
*
* @param itemName the name of the item to send the command to
* @param commandString the command to send
*/
public static Object sendCommand(String itemName, String commandString) {
ItemRegistry registry = ScriptServiceUtil.getItemRegistry();
EventPublisher publisher = ScriptServiceUtil.getEventPublisher();
if (publisher != null && registry != null) {
try {
Item item = registry.getItem(itemName);
Command command = TypeParser.parseCommand(item.getAcceptedCommandTypes(), commandString);
if (command != null) {
publisher.post(ItemEventFactory.createCommandEvent(itemName, command));
} else {
LoggerFactory.getLogger(BusEvent.class).warn("Cannot convert '{}' to a command type which item '{}' accepts: {}.", commandString, itemName, getAcceptedCommandNames(item));
}
} catch (ItemNotFoundException e) {
LoggerFactory.getLogger(BusEvent.class).warn("Item '{}' does not exist.", itemName);
}
}
return null;
}
Aggregations