Search in sources :

Example 41 with Item

use of org.eclipse.smarthome.core.items.Item in project smarthome by eclipse.

the class VoiceConsoleCommandExtension method say.

private void say(String[] args, Console console) {
    StringBuilder msg = new StringBuilder();
    for (String word : args) {
        if (word.startsWith("%") && word.endsWith("%") && word.length() > 2) {
            String itemName = word.substring(1, word.length() - 1);
            try {
                Item item = this.itemRegistry.getItemByPattern(itemName);
                msg.append(item.getState().toString());
            } catch (ItemNotFoundException e) {
                console.println("Error: Item '" + itemName + "' does not exist.");
            } catch (ItemNotUniqueException e) {
                console.print("Error: Multiple items match this pattern: ");
                for (Item item : e.getMatchingItems()) {
                    console.print(item.getName() + " ");
                }
            }
        } else {
            msg.append(word);
        }
        msg.append(" ");
    }
    voiceManager.say(msg.toString());
}
Also used : Item(org.eclipse.smarthome.core.items.Item) ItemNotUniqueException(org.eclipse.smarthome.core.items.ItemNotUniqueException) ItemNotFoundException(org.eclipse.smarthome.core.items.ItemNotFoundException)

Example 42 with Item

use of org.eclipse.smarthome.core.items.Item in project smarthome by eclipse.

the class ItemRegistryImpl method getItemsByTag.

@Override
@SuppressWarnings("unchecked")
public <T extends GenericItem> Collection<T> getItemsByTag(Class<T> typeFilter, String... tags) {
    Collection<T> filteredItems = new ArrayList<T>();
    Collection<Item> items = getItemsByTag(tags);
    for (Item item : items) {
        if (typeFilter.isInstance(item)) {
            filteredItems.add((T) item);
        }
    }
    return filteredItems;
}
Also used : GroupItem(org.eclipse.smarthome.core.items.GroupItem) GenericItem(org.eclipse.smarthome.core.items.GenericItem) Item(org.eclipse.smarthome.core.items.Item) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Example 43 with Item

use of org.eclipse.smarthome.core.items.Item in project smarthome by eclipse.

the class ItemRegistryImpl method getItemByPattern.

@Override
public Item getItemByPattern(String name) throws ItemNotFoundException, ItemNotUniqueException {
    Collection<Item> items = getItems(name);
    if (items.isEmpty()) {
        throw new ItemNotFoundException(name);
    }
    if (items.size() > 1) {
        throw new ItemNotUniqueException(name, items);
    }
    Item item = items.iterator().next();
    if (item == null) {
        throw new ItemNotFoundException(name);
    } else {
        return item;
    }
}
Also used : GroupItem(org.eclipse.smarthome.core.items.GroupItem) GenericItem(org.eclipse.smarthome.core.items.GenericItem) Item(org.eclipse.smarthome.core.items.Item) ItemNotUniqueException(org.eclipse.smarthome.core.items.ItemNotUniqueException) ItemNotFoundException(org.eclipse.smarthome.core.items.ItemNotFoundException)

Example 44 with Item

use of org.eclipse.smarthome.core.items.Item in project smarthome by eclipse.

the class ItemRegistryImpl method getItems.

@Override
public Collection<Item> getItems(String pattern) {
    String regex = pattern.replace("?", ".?").replace("*", ".*?");
    Collection<Item> matchedItems = new ArrayList<Item>();
    for (Item item : getItems()) {
        if (item.getName().matches(regex)) {
            matchedItems.add(item);
        }
    }
    return matchedItems;
}
Also used : GroupItem(org.eclipse.smarthome.core.items.GroupItem) GenericItem(org.eclipse.smarthome.core.items.GenericItem) Item(org.eclipse.smarthome.core.items.Item) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Example 45 with Item

use of org.eclipse.smarthome.core.items.Item in project smarthome by eclipse.

the class GenericItemProvider2Test method testStableReloadOrder.

@Test
public void testStableReloadOrder() {
    assertThat(itemRegistry.getAll().size(), is(0));
    String model = // 
    "Group testGroup " + // 
    "Number number1 (testGroup) " + // 
    "Number number2 (testGroup) " + // 
    "Number number3 (testGroup) " + // 
    "Number number4 (testGroup) " + // 
    "Number number5 (testGroup) " + // 
    "Number number6 (testGroup) " + // 
    "Number number7 (testGroup) " + // 
    "Number number8 (testGroup) " + "Number number9 (testGroup) ";
    modelRepository.addOrRefreshModel(TESTMODEL_NAME, new ByteArrayInputStream(model.getBytes()));
    assertThat(itemRegistry.getAll().size(), is(10));
    model = // 
    "Group testGroup " + // 
    "Number number1 (testGroup) " + // 
    "Number number2 (testGroup) " + // 
    "Number number3 (testGroup) " + // 
    "Number number4 (testGroup) " + // 
    "Number number5 (testGroup) " + // 
    "Number number6 (testGroup) " + // 
    "Number number7 \"Number Seven\" (testGroup) " + // 
    "Number number8 (testGroup) " + "Number number9 (testGroup) ";
    modelRepository.addOrRefreshModel(TESTMODEL_NAME, new ByteArrayInputStream(model.getBytes()));
    GroupItem groupItem = (GroupItem) itemRegistry.get("testGroup");
    assertNotNull(groupItem);
    int number = 0;
    Iterator<Item> it = groupItem.getMembers().iterator();
    while (it.hasNext()) {
        Item item = it.next();
        assertEquals("number" + (++number), item.getName());
        if (number == 7) {
            assertEquals("Number Seven", item.getLabel());
        }
    }
}
Also used : SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) GenericItem(org.eclipse.smarthome.core.items.GenericItem) NumberItem(org.eclipse.smarthome.core.library.items.NumberItem) Item(org.eclipse.smarthome.core.items.Item) GroupItem(org.eclipse.smarthome.core.items.GroupItem) ByteArrayInputStream(java.io.ByteArrayInputStream) GroupItem(org.eclipse.smarthome.core.items.GroupItem) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

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