use of org.eclipse.smarthome.core.items.GenericItem in project smarthome by eclipse.
the class GenericItemProvider method getItemsFromModel.
private Collection<Item> getItemsFromModel(String modelName) {
logger.debug("Read items from model '{}'", modelName);
List<Item> items = new ArrayList<Item>();
if (modelRepository != null) {
ItemModel model = (ItemModel) modelRepository.getModel(modelName);
if (model != null) {
for (ModelItem modelItem : model.getItems()) {
Item item = createItemFromModelItem(modelItem);
if (item != null) {
for (String groupName : modelItem.getGroups()) {
((GenericItem) item).addGroupName(groupName);
}
items.add(item);
}
}
}
}
return items;
}
use of org.eclipse.smarthome.core.items.GenericItem in project smarthome by eclipse.
the class WebAppServlet method waitForChanges.
/**
* This method only returns when a change has occurred to any item on the page to display
*
* @param widgets the widgets of the page to observe
*/
private boolean waitForChanges(EList<Widget> widgets) {
long startTime = (new Date()).getTime();
boolean timeout = false;
BlockingStateChangeListener listener = new BlockingStateChangeListener();
// let's get all items for these widgets
Set<GenericItem> items = getAllItems(widgets);
for (GenericItem item : items) {
item.addStateChangeListener(listener);
}
while (!listener.hasChangeOccurred() && !timeout) {
timeout = (new Date()).getTime() - startTime > TIMEOUT_IN_MS;
try {
Thread.sleep(300);
} catch (InterruptedException e) {
timeout = true;
break;
}
}
for (GenericItem item : items) {
item.removeStateChangeListener(listener);
}
return !timeout;
}
use of org.eclipse.smarthome.core.items.GenericItem 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);
}
}
}
}
use of org.eclipse.smarthome.core.items.GenericItem in project smarthome by eclipse.
the class ItemUpdater method receiveUpdate.
@Override
protected void receiveUpdate(ItemStateEvent updateEvent) {
String itemName = updateEvent.getItemName();
State newState = updateEvent.getItemState();
try {
GenericItem item = (GenericItem) itemRegistry.getItem(itemName);
boolean isAccepted = false;
if (item.getAcceptedDataTypes().contains(newState.getClass())) {
isAccepted = true;
} else {
// Look for class hierarchy
for (Class<? extends State> state : item.getAcceptedDataTypes()) {
try {
if (!state.isEnum() && state.newInstance().getClass().isAssignableFrom(newState.getClass())) {
isAccepted = true;
break;
}
} catch (InstantiationException e) {
// Should never happen
logger.warn("InstantiationException on {}", e.getMessage());
} catch (IllegalAccessException e) {
// Should never happen
logger.warn("IllegalAccessException on {}", e.getMessage());
}
}
}
if (isAccepted) {
item.setState(newState);
} else {
logger.debug("Received update of a not accepted type ({}) for item {}", newState.getClass().getSimpleName(), itemName);
}
} catch (ItemNotFoundException e) {
logger.debug("Received update for non-existing item: {}", e.getMessage());
}
}
use of org.eclipse.smarthome.core.items.GenericItem 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");
}
}
Aggregations