Search in sources :

Example 21 with GenericItem

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;
}
Also used : GroupItem(org.eclipse.smarthome.core.items.GroupItem) ModelGroupItem(org.eclipse.smarthome.model.items.ModelGroupItem) GenericItem(org.eclipse.smarthome.core.items.GenericItem) Item(org.eclipse.smarthome.core.items.Item) ModelItem(org.eclipse.smarthome.model.items.ModelItem) ModelNormalItem(org.eclipse.smarthome.model.items.ModelNormalItem) ItemModel(org.eclipse.smarthome.model.items.ItemModel) GenericItem(org.eclipse.smarthome.core.items.GenericItem) ArrayList(java.util.ArrayList) ModelItem(org.eclipse.smarthome.model.items.ModelItem)

Example 22 with GenericItem

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;
}
Also used : GenericItem(org.eclipse.smarthome.core.items.GenericItem) Date(java.util.Date)

Example 23 with GenericItem

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);
            }
        }
    }
}
Also used : GenericItem(org.eclipse.smarthome.core.items.GenericItem) Item(org.eclipse.smarthome.core.items.Item) GenericItem(org.eclipse.smarthome.core.items.GenericItem) ItemFactory(org.eclipse.smarthome.core.items.ItemFactory) Channel(org.eclipse.smarthome.core.thing.Channel)

Example 24 with GenericItem

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());
    }
}
Also used : GenericItem(org.eclipse.smarthome.core.items.GenericItem) State(org.eclipse.smarthome.core.types.State) ItemNotFoundException(org.eclipse.smarthome.core.items.ItemNotFoundException)

Example 25 with GenericItem

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");
    }
}
Also used : GenericItem(org.eclipse.smarthome.core.items.GenericItem) Item(org.eclipse.smarthome.core.items.Item)

Aggregations

GenericItem (org.eclipse.smarthome.core.items.GenericItem)27 GroupItem (org.eclipse.smarthome.core.items.GroupItem)10 Item (org.eclipse.smarthome.core.items.Item)10 ItemNotFoundException (org.eclipse.smarthome.core.items.ItemNotFoundException)6 Test (org.junit.Test)4 ApiOperation (io.swagger.annotations.ApiOperation)2 ApiResponses (io.swagger.annotations.ApiResponses)2 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 RolesAllowed (javax.annotation.security.RolesAllowed)2 Path (javax.ws.rs.Path)2 ActiveItem (org.eclipse.smarthome.core.items.ActiveItem)2 RollershutterItem (org.eclipse.smarthome.core.library.items.RollershutterItem)2 SwitchItem (org.eclipse.smarthome.core.library.items.SwitchItem)2 ModelGroupItem (org.eclipse.smarthome.model.items.ModelGroupItem)2 ModelNormalItem (org.eclipse.smarthome.model.items.ModelNormalItem)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 HashSet (java.util.HashSet)1 List (java.util.List)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1