Search in sources :

Example 16 with ItemNotFoundException

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

the class AbstractWidgetRenderer method getStateAsNumber.

protected String getStateAsNumber(Widget w) {
    String itemName = w.getItem();
    if (itemName != null) {
        try {
            Item item = itemUIRegistry.getItem(itemName);
            State state = item.getState();
            if (item.getAcceptedDataTypes().contains(PercentType.class)) {
                state = item.getStateAs(PercentType.class);
            } else {
                state = item.getStateAs(DecimalType.class);
            }
            if (state != null) {
                return escapeURLPath(state.toString());
            } else {
                logger.debug("State '{}' of item '{}' is not a number!", item.getState(), itemName);
            }
        } catch (ItemNotFoundException e) {
            logger.error("Cannot retrieve item '{}' for widget {}", new Object[] { itemName, w.eClass().getInstanceTypeName() });
        }
    }
    return "NULL";
}
Also used : Item(org.eclipse.smarthome.core.items.Item) State(org.eclipse.smarthome.core.types.State) DecimalType(org.eclipse.smarthome.core.library.types.DecimalType) PercentType(org.eclipse.smarthome.core.library.types.PercentType) ItemNotFoundException(org.eclipse.smarthome.core.items.ItemNotFoundException)

Example 17 with ItemNotFoundException

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

the class ScriptBusEvent method sendCommand.

/**
 * Sends a command for a specified item to the event bus.
 *
 * @param itemName the name of the item to send the command to
 * @param commandString the command to send
 */
public Object sendCommand(String itemName, String commandString) {
    if (eventPublisher != null && itemRegistry != null) {
        try {
            Item item = itemRegistry.getItem(itemName);
            Command command = TypeParser.parseCommand(item.getAcceptedCommandTypes(), commandString);
            eventPublisher.post(ItemEventFactory.createCommandEvent(itemName, command));
        } catch (ItemNotFoundException e) {
            LoggerFactory.getLogger(ScriptBusEvent.class).warn("Item '{}' does not exist.", itemName);
        }
    }
    return null;
}
Also used : Item(org.eclipse.smarthome.core.items.Item) GroupItem(org.eclipse.smarthome.core.items.GroupItem) Command(org.eclipse.smarthome.core.types.Command) ItemNotFoundException(org.eclipse.smarthome.core.items.ItemNotFoundException)

Example 18 with ItemNotFoundException

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

the class ItemCommandActionHandler method execute.

@Override
public Map<String, Object> execute(Map<String, Object> inputs) {
    String itemName = (String) module.getConfiguration().get(ITEM_NAME);
    String command = (String) module.getConfiguration().get(COMMAND);
    if (itemName != null && command != null && eventPublisher != null && itemRegistry != null) {
        try {
            Item item = itemRegistry.getItem(itemName);
            Command commandObj = TypeParser.parseCommand(item.getAcceptedCommandTypes(), command);
            ItemCommandEvent itemCommandEvent = ItemEventFactory.createCommandEvent(itemName, commandObj);
            logger.debug("Executing ItemCommandAction on Item {} with Command {}", itemCommandEvent.getItemName(), itemCommandEvent.getItemCommand());
            eventPublisher.post(itemCommandEvent);
        } catch (ItemNotFoundException e) {
            logger.error("Item with name {} not found in ItemRegistry.", itemName);
        }
    } else {
        logger.error("Command was not posted because either the configuration was not correct or a service was missing: ItemName: {}, Command: {}, eventPublisher: {}, ItemRegistry: {}", itemName, command, eventPublisher, itemRegistry);
    }
    return null;
}
Also used : Item(org.eclipse.smarthome.core.items.Item) ItemCommandEvent(org.eclipse.smarthome.core.items.events.ItemCommandEvent) Command(org.eclipse.smarthome.core.types.Command) ItemNotFoundException(org.eclipse.smarthome.core.items.ItemNotFoundException)

Example 19 with ItemNotFoundException

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

the class BusEvent method sendCommand.

/**
 * Sends a command for a specified item to the event bus.
 *
 * @param itemName the name of the item to send the command to
 * @param commandString the command to send
 */
public static Object sendCommand(String itemName, String commandString) {
    ItemRegistry registry = ScriptServiceUtil.getItemRegistry();
    EventPublisher publisher = ScriptServiceUtil.getEventPublisher();
    if (publisher != null && registry != null) {
        try {
            Item item = registry.getItem(itemName);
            Command command = TypeParser.parseCommand(item.getAcceptedCommandTypes(), commandString);
            if (command != null) {
                publisher.post(ItemEventFactory.createCommandEvent(itemName, command));
            } else {
                LoggerFactory.getLogger(BusEvent.class).warn("Cannot convert '{}' to a command type which item '{}' accepts: {}.", commandString, itemName, getAcceptedCommandNames(item));
            }
        } catch (ItemNotFoundException e) {
            LoggerFactory.getLogger(BusEvent.class).warn("Item '{}' does not exist.", itemName);
        }
    }
    return null;
}
Also used : Item(org.eclipse.smarthome.core.items.Item) GroupItem(org.eclipse.smarthome.core.items.GroupItem) EventPublisher(org.eclipse.smarthome.core.events.EventPublisher) Command(org.eclipse.smarthome.core.types.Command) ItemRegistry(org.eclipse.smarthome.core.items.ItemRegistry) ItemNotFoundException(org.eclipse.smarthome.core.items.ItemNotFoundException)

Example 20 with ItemNotFoundException

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

the class NtpOSGiTest method getItemState.

private State getItemState(String acceptedItemType) {
    final Item testItem = waitForAssert(() -> {
        Item tmp;
        try {
            tmp = itemRegistry.getItem(TEST_ITEM_NAME);
        } catch (ItemNotFoundException e) {
            tmp = null;
        }
        assertNotNull(tmp);
        return tmp;
    });
    return waitForAssert(() -> {
        final State testItemState = testItem.getState();
        if (acceptedItemType.equals(ACCEPTED_ITEM_TYPE_STRING)) {
            assertThat(testItemState, is(instanceOf(StringType.class)));
        } else if (acceptedItemType.equals(ACCEPTED_ITEM_TYPE_DATE_TIME)) {
            assertThat(testItemState, is(instanceOf(DateTimeType.class)));
        }
        return testItemState;
    }, 3 * DFL_TIMEOUT, 2 * DFL_SLEEP_TIME);
}
Also used : DateTimeItem(org.eclipse.smarthome.core.library.items.DateTimeItem) GenericItem(org.eclipse.smarthome.core.items.GenericItem) Item(org.eclipse.smarthome.core.items.Item) StringItem(org.eclipse.smarthome.core.library.items.StringItem) DateTimeType(org.eclipse.smarthome.core.library.types.DateTimeType) State(org.eclipse.smarthome.core.types.State) ItemNotFoundException(org.eclipse.smarthome.core.items.ItemNotFoundException)

Aggregations

ItemNotFoundException (org.eclipse.smarthome.core.items.ItemNotFoundException)41 Item (org.eclipse.smarthome.core.items.Item)36 GroupItem (org.eclipse.smarthome.core.items.GroupItem)22 GenericItem (org.eclipse.smarthome.core.items.GenericItem)17 State (org.eclipse.smarthome.core.types.State)15 NumberItem (org.eclipse.smarthome.core.library.items.NumberItem)8 RollershutterItem (org.eclipse.smarthome.core.library.items.RollershutterItem)8 SwitchItem (org.eclipse.smarthome.core.library.items.SwitchItem)8 Command (org.eclipse.smarthome.core.types.Command)7 ItemNotUniqueException (org.eclipse.smarthome.core.items.ItemNotUniqueException)5 QuantityType (org.eclipse.smarthome.core.library.types.QuantityType)5 Mapping (org.eclipse.smarthome.model.sitemap.Mapping)5 Date (java.util.Date)4 CallItem (org.eclipse.smarthome.core.library.items.CallItem)4 DateTimeItem (org.eclipse.smarthome.core.library.items.DateTimeItem)4 StringItem (org.eclipse.smarthome.core.library.items.StringItem)4 Widget (org.eclipse.smarthome.model.sitemap.Widget)4 ColorItem (org.eclipse.smarthome.core.library.items.ColorItem)3 ContactItem (org.eclipse.smarthome.core.library.items.ContactItem)3 DimmerItem (org.eclipse.smarthome.core.library.items.DimmerItem)3