use of org.eclipse.smarthome.core.items.Item in project smarthome by eclipse.
the class ChannelItemProvider method createItemForLink.
private void createItemForLink(ItemChannelLink link) {
if (!enabled) {
return;
}
if (itemRegistry.get(link.getItemName()) != null) {
// there is already an item, we do not need to create one
return;
}
Channel channel = thingRegistry.getChannel(link.getLinkedUID());
if (channel != null) {
Item item = null;
// Only create an item for state channels
if (channel.getKind() == ChannelKind.STATE) {
for (ItemFactory itemFactory : itemFactories) {
item = itemFactory.createItem(channel.getAcceptedItemType(), link.getItemName());
if (item != null) {
break;
}
}
}
if (item instanceof GenericItem) {
GenericItem gItem = (GenericItem) item;
gItem.setLabel(getLabel(channel));
gItem.setCategory(getCategory(channel));
gItem.addTags(channel.getDefaultTags());
}
if (item != null) {
items.put(item.getName(), item);
for (ProviderChangeListener<Item> listener : listeners) {
listener.added(this, item);
}
}
}
}
use of org.eclipse.smarthome.core.items.Item in project smarthome by eclipse.
the class ItemUpdater method receiveCommand.
@Override
protected void receiveCommand(ItemCommandEvent commandEvent) {
try {
Item item = itemRegistry.getItem(commandEvent.getItemName());
if (item instanceof GroupItem) {
GroupItem groupItem = (GroupItem) item;
groupItem.send(commandEvent.getItemCommand());
}
} catch (ItemNotFoundException e) {
logger.debug("Received command for non-existing item: {}", e.getMessage());
}
}
use of org.eclipse.smarthome.core.items.Item in project smarthome by eclipse.
the class AbstractRuleBasedInterpreter method getMatchingItems.
/**
* Filters the item registry by matching each item's name with the provided name fragments.
* The item's label and its parent group's labels are tokenizend {@link tokenize} and then altogether looked up
* by each and every provided fragment.
* For the item to get included into the result list, every provided fragment has to be found among the label
* tokens.
* If a command type is provided, the item also has to support it.
* In case of channels and their owners being ambiguous due to sharing most of the label sequence, only the top
* most item with support for the
* given command type is kept.
*
* @param language Language information that is used for matching
* @param labelFragments label fragments that are used to match an item's label.
* For a positive match, the item's label has to contain every fragment - independently of their order.
* They are treated case insensitive.
* @param commandType optional command type that all items have to support.
* Provide {null} if there is no need for a certain command to be supported.
* @return All matching items from the item registry.
*/
protected ArrayList<Item> getMatchingItems(ResourceBundle language, String[] labelFragments, Class<?> commandType) {
ArrayList<Item> items = new ArrayList<Item>();
HashMap<Item, ArrayList<HashSet<String>>> map = getItemTokens(language.getLocale());
for (Item item : map.keySet()) {
for (HashSet<String> parts : map.get(item)) {
boolean allMatch = true;
for (String fragment : labelFragments) {
if (!parts.contains(fragment.toLowerCase(language.getLocale()))) {
allMatch = false;
break;
}
}
if (allMatch) {
if (commandType == null || item.getAcceptedCommandTypes().contains(commandType)) {
String name = item.getName();
boolean insert = true;
for (Item si : items) {
if (name.startsWith(si.getName())) {
insert = false;
}
}
if (insert) {
for (int i = 0; i < items.size(); i++) {
Item si = items.get(i);
if (si.getName().startsWith(name)) {
items.remove(i);
i--;
}
}
items.add(item);
}
}
}
}
}
return items;
}
use of org.eclipse.smarthome.core.items.Item in project smarthome by eclipse.
the class AbstractRuleBasedInterpreter method addItem.
private void addItem(Locale locale, HashMap<Item, ArrayList<HashSet<String>>> target, HashSet<String> tokens, Item item) {
HashSet<String> nt = new HashSet<String>(tokens);
nt.addAll(tokenize(locale, item.getLabel()));
ArrayList<HashSet<String>> list = target.get(item);
if (list == null) {
target.put(item, list = new ArrayList<HashSet<String>>());
}
list.add(nt);
if (item instanceof GroupItem) {
for (Item member : ((GroupItem) item).getMembers()) {
addItem(locale, target, nt, member);
}
}
}
use of org.eclipse.smarthome.core.items.Item in project smarthome by eclipse.
the class ItemConsoleCommandExtension method handleTags.
private <T> void handleTags(final Consumer<T> func, final T tag, GenericItem gItem, Console console) {
// allow adding/removing of tags only for managed items
if (managedItemProvider.get(gItem.getName()) != null) {
// add or remove tag method is passed here
func.accept(tag);
Item oldItem = itemRegistry.update(gItem);
if (oldItem != null) {
console.println("Successfully changed tag " + tag + " on item " + gItem.getName());
}
} else {
console.println("Error: Cannot change tag " + tag + " on item " + gItem.getName() + " because this item does not belong to a ManagedProvider");
}
}
Aggregations