Search in sources :

Example 16 with GenericItem

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

the class GenericItemProvider method createItemFromModelItem.

private Item createItemFromModelItem(ModelItem modelItem) {
    GenericItem item = null;
    if (modelItem instanceof ModelGroupItem) {
        ModelGroupItem modelGroupItem = (ModelGroupItem) modelItem;
        GenericItem baseItem;
        try {
            baseItem = createItemOfType(modelGroupItem.getType(), modelGroupItem.getName());
        } catch (IllegalArgumentException e) {
            logger.debug("Error creating base item for group item '{}', item will be ignored: {}", modelGroupItem.getName(), e.getMessage());
            return null;
        }
        if (baseItem != null) {
            // if the user did not specify a function the first value of the enum in xtext (EQUAL) will be used
            ModelGroupFunction function = modelGroupItem.getFunction();
            item = applyGroupFunction(baseItem, modelGroupItem, function);
        } else {
            item = new GroupItem(modelGroupItem.getName());
        }
    } else {
        ModelNormalItem normalItem = (ModelNormalItem) modelItem;
        try {
            item = createItemOfType(normalItem.getType(), normalItem.getName());
        } catch (IllegalArgumentException e) {
            logger.debug("Error creating item '{}', item will be ignored: {}", normalItem.getName(), e.getMessage());
            return null;
        }
    }
    if (item != null) {
        String label = modelItem.getLabel();
        String format = StringUtils.substringBetween(label, "[", "]");
        if (format != null) {
            label = StringUtils.substringBefore(label, "[").trim();
            stateDescriptions.put(modelItem.getName(), new StateDescription(null, null, null, format, false, null));
        }
        item.setLabel(label);
        item.setCategory(modelItem.getIcon());
        assignTags(modelItem, item);
        return item;
    } else {
        return null;
    }
}
Also used : ModelGroupItem(org.eclipse.smarthome.model.items.ModelGroupItem) GenericItem(org.eclipse.smarthome.core.items.GenericItem) ModelGroupFunction(org.eclipse.smarthome.model.items.ModelGroupFunction) GroupItem(org.eclipse.smarthome.core.items.GroupItem) ModelGroupItem(org.eclipse.smarthome.model.items.ModelGroupItem) ModelNormalItem(org.eclipse.smarthome.model.items.ModelNormalItem) StateDescription(org.eclipse.smarthome.core.types.StateDescription)

Example 17 with GenericItem

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

the class CoreItemFactoryTest method shouldCreateItems.

@Test
public void shouldCreateItems() {
    for (String itemTypeName : itemTypeNames) {
        GenericItem item = coreItemFactory.createItem(itemTypeName, itemTypeName.toLowerCase());
        assertThat(item.getType(), is(itemTypeName));
        assertThat(item.getName(), is(itemTypeName.toLowerCase()));
    }
}
Also used : GenericItem(org.eclipse.smarthome.core.items.GenericItem) Test(org.junit.Test)

Example 18 with GenericItem

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

the class CoreItemFactoryTest method shouldReturnNullForUnsupportedItemTypeName.

@Test
public void shouldReturnNullForUnsupportedItemTypeName() {
    GenericItem item = coreItemFactory.createItem("NoValidItemTypeName", "IWantMyItem");
    assertThat(item, is(nullValue()));
}
Also used : GenericItem(org.eclipse.smarthome.core.items.GenericItem) Test(org.junit.Test)

Example 19 with GenericItem

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

the class AutoUpdateBinding method postUpdate.

private void postUpdate(String itemName, State newState) {
    if (itemRegistry != null) {
        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(), e);
                    } catch (IllegalAccessException e) {
                        // Should never happen
                        logger.warn("IllegalAccessException on {}", e.getMessage(), e);
                    }
                }
            }
            if (isAccepted) {
                eventPublisher.post(ItemEventFactory.createStateEvent(itemName, newState, "org.eclipse.smarthome.core.autoupdate"));
            } 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) ItemNotFoundException(org.eclipse.smarthome.core.items.ItemNotFoundException)

Example 20 with GenericItem

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

the class RuleEngineImpl method internalItemAdded.

private void internalItemAdded(Item item) {
    if (item instanceof GenericItem) {
        GenericItem genericItem = (GenericItem) item;
        genericItem.addStateChangeListener(this);
    }
}
Also used : GenericItem(org.eclipse.smarthome.core.items.GenericItem)

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