use of org.eclipse.smarthome.core.items.events.ItemCommandEvent 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.items.events.ItemCommandEvent 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.items.events.ItemCommandEvent in project smarthome by eclipse.
the class ItemCommandActionHandler method execute.
@Override
public Map<String, Object> execute(Map<String, Object> inputs) {
String itemName = (String) module.getConfiguration().get(ITEM_NAME);
String command = (String) module.getConfiguration().get(COMMAND);
if (itemName != null && command != null && eventPublisher != null && itemRegistry != null) {
try {
Item item = itemRegistry.getItem(itemName);
Command commandObj = TypeParser.parseCommand(item.getAcceptedCommandTypes(), command);
ItemCommandEvent itemCommandEvent = ItemEventFactory.createCommandEvent(itemName, commandObj);
logger.debug("Executing ItemCommandAction on Item {} with Command {}", itemCommandEvent.getItemName(), itemCommandEvent.getItemCommand());
eventPublisher.post(itemCommandEvent);
} catch (ItemNotFoundException e) {
logger.error("Item with name {} not found in ItemRegistry.", itemName);
}
} else {
logger.error("Command was not posted because either the configuration was not correct or a service was missing: ItemName: {}, Command: {}, eventPublisher: {}, ItemRegistry: {}", itemName, command, eventPublisher, itemRegistry);
}
return null;
}
use of org.eclipse.smarthome.core.items.events.ItemCommandEvent 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 = new Rule("myRule21" + new Random().nextInt());
rule.setTriggers(Arrays.asList(new Trigger[] { new Trigger("ItemStateChangeTrigger2", "core.GenericEventTrigger", triggerConfig) }));
rule.setActions(Arrays.asList(new Action[] { new Action("ItemPostCommandAction2", "core.ItemCommandAction", actionConfig, null) }));
// I would expect the factory to create the UID of the rule and the name to be in the list of parameters.
rule.setName("RuleByJAVA_API");
logger.info("Rule created: {}", rule.getUID());
final RuleRegistry ruleRegistry = getService(RuleRegistry.class);
ruleRegistry.add(rule);
ruleRegistry.setEnabled(rule.getUID(), true);
waitForAssert(() -> {
Assert.assertEquals(RuleStatus.IDLE, ruleRegistry.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());
});
}
use of org.eclipse.smarthome.core.items.events.ItemCommandEvent in project smarthome by eclipse.
the class ItemCommandTriggerHandler method receive.
@Override
public void receive(Event event) {
if (ruleEngineCallback != null) {
logger.trace("Received Event: Source: {} Topic: {} Type: {} Payload: {}", event.getSource(), event.getTopic(), event.getType(), event.getPayload());
Map<String, Object> values = new HashMap<>();
if (event instanceof ItemCommandEvent) {
Command command = ((ItemCommandEvent) event).getItemCommand();
if (this.command == null || this.command.equals(command.toFullString())) {
values.put("command", command);
values.put("event", event);
ruleEngineCallback.triggered(this.module, values);
}
}
}
}
Aggregations