use of org.eclipse.smarthome.core.items.Item in project smarthome by eclipse.
the class RunRuleModuleTest method before.
@Before
public void before() {
registerService(new ItemProvider() {
@Override
public void addProviderChangeListener(final ProviderChangeListener<Item> listener) {
}
@Override
public void removeProviderChangeListener(final ProviderChangeListener<Item> listener) {
}
@Override
public Collection<Item> getAll() {
return Arrays.asList(new Item[] { new SwitchItem("switch1"), new SwitchItem("switch2"), new SwitchItem("switch3"), new SwitchItem("ruleTrigger") });
}
});
registerService(volatileStorageService);
}
use of org.eclipse.smarthome.core.items.Item in project smarthome by eclipse.
the class RuntimeRuleTest method before.
@Before
public void before() {
registerService(new ItemProvider() {
@Override
public void addProviderChangeListener(final ProviderChangeListener<Item> listener) {
}
@Override
public void removeProviderChangeListener(final ProviderChangeListener<Item> listener) {
}
@Override
public Collection<Item> getAll() {
return Arrays.asList(new Item[] { new SwitchItem("myMotionItem"), new SwitchItem("myPresenceItem"), new SwitchItem("myLampItem"), new SwitchItem("myMotionItem2"), new SwitchItem("myPresenceItem2"), new SwitchItem("myLampItem2"), new SwitchItem("myMotionItem3"), new SwitchItem("myPresenceItem3"), new SwitchItem("myLampItem3"), new SwitchItem("myMotionItem4"), new SwitchItem("myPresenceItem4"), new SwitchItem("myLampItem4") });
}
});
registerService(volatileStorageService);
}
use of org.eclipse.smarthome.core.items.Item 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.items.Item in project smarthome by eclipse.
the class CommunicationManager method handleEvent.
private <T extends Type> void handleEvent(String itemName, T type, String source, Function<@Nullable String, @Nullable List<Class<? extends T>>> acceptedTypesFunction, ProfileAction<T> action) {
final Item item = getItem(itemName);
if (item == null) {
logger.debug("Received an event for item {} which does not exist", itemName);
return;
}
itemChannelLinkRegistry.stream().filter(link -> {
// all links for the item
return link.getItemName().equals(itemName);
}).filter(link -> {
// make sure the command event is not sent back to its source
return !link.getLinkedUID().toString().equals(source);
}).forEach(link -> {
ChannelUID channelUID = link.getLinkedUID();
Thing thing = getThing(channelUID.getThingUID());
if (thing != null) {
Channel channel = thing.getChannel(channelUID.getId());
if (channel != null) {
@Nullable T convertedType = toAcceptedType(type, channel, acceptedTypesFunction);
if (convertedType != null) {
if (thing.getHandler() != null) {
Profile profile = getProfile(link, item, thing);
action.handle(profile, thing, convertedType);
}
} else {
logger.debug("Received event '{}' ({}) could not be converted to any type accepted by item '{}' ({})", type, type.getClass().getSimpleName(), itemName, item.getType());
}
} else {
logger.debug("Received event '{}' for non-existing channel '{}', not forwarding it to the handler", type, channelUID);
}
} else {
logger.debug("Received event '{}' for non-existing thing '{}', not forwarding it to the handler", type, channelUID.getThingUID());
}
});
}
use of org.eclipse.smarthome.core.items.Item in project smarthome by eclipse.
the class ItemChannelLinkRegistry method getLinkedItems.
public Set<Item> getLinkedItems(UID uid) {
final Set<Item> linkedItems = new LinkedHashSet<>();
for (final AbstractLink link : getAll()) {
final String itemName = link.getItemName();
Item item = itemRegistry.get(itemName);
if (link.getLinkedUID().equals(uid) && item != null) {
linkedItems.add(item);
}
}
return linkedItems;
}
Aggregations