Search in sources :

Example 96 with Item

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);
            }
        }
    }
}
Also used : GenericItem(org.eclipse.smarthome.core.items.GenericItem) Item(org.eclipse.smarthome.core.items.Item) GenericItem(org.eclipse.smarthome.core.items.GenericItem) ItemFactory(org.eclipse.smarthome.core.items.ItemFactory) Channel(org.eclipse.smarthome.core.thing.Channel)

Example 97 with 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());
    }
}
Also used : GenericItem(org.eclipse.smarthome.core.items.GenericItem) Item(org.eclipse.smarthome.core.items.Item) GroupItem(org.eclipse.smarthome.core.items.GroupItem) GroupItem(org.eclipse.smarthome.core.items.GroupItem) ItemNotFoundException(org.eclipse.smarthome.core.items.ItemNotFoundException)

Example 98 with Item

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;
}
Also used : Item(org.eclipse.smarthome.core.items.Item) GroupItem(org.eclipse.smarthome.core.items.GroupItem) ArrayList(java.util.ArrayList)

Example 99 with Item

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);
        }
    }
}
Also used : Item(org.eclipse.smarthome.core.items.Item) GroupItem(org.eclipse.smarthome.core.items.GroupItem) ArrayList(java.util.ArrayList) GroupItem(org.eclipse.smarthome.core.items.GroupItem) HashSet(java.util.HashSet)

Example 100 with Item

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");
    }
}
Also used : GenericItem(org.eclipse.smarthome.core.items.GenericItem) Item(org.eclipse.smarthome.core.items.Item)

Aggregations

Item (org.eclipse.smarthome.core.items.Item)111 GroupItem (org.eclipse.smarthome.core.items.GroupItem)42 GenericItem (org.eclipse.smarthome.core.items.GenericItem)39 ItemNotFoundException (org.eclipse.smarthome.core.items.ItemNotFoundException)37 SwitchItem (org.eclipse.smarthome.core.library.items.SwitchItem)35 NumberItem (org.eclipse.smarthome.core.library.items.NumberItem)31 State (org.eclipse.smarthome.core.types.State)26 Test (org.junit.Test)26 ItemRegistry (org.eclipse.smarthome.core.items.ItemRegistry)14 RollershutterItem (org.eclipse.smarthome.core.library.items.RollershutterItem)14 StringItem (org.eclipse.smarthome.core.library.items.StringItem)14 JavaOSGiTest (org.eclipse.smarthome.test.java.JavaOSGiTest)13 DimmerItem (org.eclipse.smarthome.core.library.items.DimmerItem)12 ColorItem (org.eclipse.smarthome.core.library.items.ColorItem)10 IntentInterpretation (org.openhab.ui.habot.nlp.IntentInterpretation)10 EventPublisher (org.eclipse.smarthome.core.events.EventPublisher)9 DecimalType (org.eclipse.smarthome.core.library.types.DecimalType)9 Thing (org.eclipse.smarthome.core.thing.Thing)9 Set (java.util.Set)8 Widget (org.eclipse.smarthome.model.sitemap.Widget)8