Search in sources :

Example 1 with ItemNotUniqueException

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

the class StatusConsoleCommandExtension method execute.

@Override
public void execute(String[] args, Console console) {
    if (args.length > 0) {
        String itemName = args[0];
        try {
            Item item = this.itemRegistry.getItemByPattern(itemName);
            console.println(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 {
        printUsage(console);
    }
}
Also used : Item(org.eclipse.smarthome.core.items.Item) ItemNotUniqueException(org.eclipse.smarthome.core.items.ItemNotUniqueException) ItemNotFoundException(org.eclipse.smarthome.core.items.ItemNotFoundException)

Example 2 with ItemNotUniqueException

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

the class UpdateConsoleCommandExtension method execute.

@Override
public void execute(String[] args, Console console) {
    if (args.length > 0) {
        String itemName = args[0];
        try {
            Item item = this.itemRegistry.getItemByPattern(itemName);
            if (args.length > 1) {
                String stateName = args[1];
                State state = TypeParser.parseState(item.getAcceptedDataTypes(), stateName);
                if (state != null) {
                    eventPublisher.post(ItemEventFactory.createStateEvent(item.getName(), state));
                    console.println("Update has been sent successfully.");
                } else {
                    console.println("Error: State '" + stateName + "' is not valid for item '" + itemName + "'");
                    console.print("Valid data types are: ( ");
                    for (Class<? extends State> acceptedType : item.getAcceptedDataTypes()) {
                        console.print(acceptedType.getSimpleName() + " ");
                    }
                    console.println(")");
                }
            } else {
                printUsage(console);
            }
        } 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 {
        printUsage(console);
    }
}
Also used : Item(org.eclipse.smarthome.core.items.Item) State(org.eclipse.smarthome.core.types.State) ItemNotUniqueException(org.eclipse.smarthome.core.items.ItemNotUniqueException) ItemNotFoundException(org.eclipse.smarthome.core.items.ItemNotFoundException)

Example 3 with ItemNotUniqueException

use of org.eclipse.smarthome.core.items.ItemNotUniqueException 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 4 with ItemNotUniqueException

use of org.eclipse.smarthome.core.items.ItemNotUniqueException 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 5 with ItemNotUniqueException

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

the class SendConsoleCommandExtension method execute.

@Override
public void execute(String[] args, Console console) {
    if (args.length > 0) {
        String itemName = args[0];
        try {
            Item item = this.itemRegistry.getItemByPattern(itemName);
            if (args.length > 1) {
                String commandName = args[1];
                Command command = TypeParser.parseCommand(item.getAcceptedCommandTypes(), commandName);
                if (command != null) {
                    eventPublisher.post(ItemEventFactory.createCommandEvent(itemName, command));
                    console.println("Command has been sent successfully.");
                } else {
                    console.println("Error: Command '" + commandName + "' is not valid for item '" + itemName + "'");
                    console.println("Valid command types are:");
                    for (Class<? extends Command> acceptedType : item.getAcceptedCommandTypes()) {
                        console.print("  " + acceptedType.getSimpleName());
                        if (acceptedType.isEnum()) {
                            console.print(": ");
                            for (Object e : acceptedType.getEnumConstants()) {
                                console.print(e + " ");
                            }
                        }
                        console.println("");
                    }
                }
            } else {
                printUsage(console);
            }
        } 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 {
        printUsage(console);
    }
}
Also used : Item(org.eclipse.smarthome.core.items.Item) Command(org.eclipse.smarthome.core.types.Command) ItemNotUniqueException(org.eclipse.smarthome.core.items.ItemNotUniqueException) ItemNotFoundException(org.eclipse.smarthome.core.items.ItemNotFoundException)

Aggregations

Item (org.eclipse.smarthome.core.items.Item)5 ItemNotFoundException (org.eclipse.smarthome.core.items.ItemNotFoundException)5 ItemNotUniqueException (org.eclipse.smarthome.core.items.ItemNotUniqueException)5 GenericItem (org.eclipse.smarthome.core.items.GenericItem)1 GroupItem (org.eclipse.smarthome.core.items.GroupItem)1 Command (org.eclipse.smarthome.core.types.Command)1 State (org.eclipse.smarthome.core.types.State)1