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";
}
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;
}
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;
}
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;
}
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);
}
Aggregations