Search in sources :

Example 1 with ModelGroupItem

use of org.openhab.core.model.items.ModelGroupItem in project openhab-core by openhab.

the class GenericItemProvider method createItemFromModelItem.

@Nullable
private Item createItemFromModelItem(ModelItem modelItem) {
    Item item;
    if (modelItem instanceof ModelGroupItem) {
        ModelGroupItem modelGroupItem = (ModelGroupItem) modelItem;
        Item 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 instanceof ActiveItem) {
        String label = modelItem.getLabel();
        String format = extractFormat(label);
        if (format != null) {
            label = label.substring(0, label.indexOf("[")).trim();
            stateDescriptionFragments.put(modelItem.getName(), StateDescriptionFragmentBuilder.create().withPattern(format).build());
        }
        ((ActiveItem) item).setLabel(label);
        ((ActiveItem) item).setCategory(modelItem.getIcon());
        assignTags(modelItem, (ActiveItem) item);
        return item;
    } else {
        return null;
    }
}
Also used : GenericItem(org.openhab.core.items.GenericItem) ActiveItem(org.openhab.core.items.ActiveItem) ModelGroupItem(org.openhab.core.model.items.ModelGroupItem) ModelItem(org.openhab.core.model.items.ModelItem) ModelNormalItem(org.openhab.core.model.items.ModelNormalItem) GroupItem(org.openhab.core.items.GroupItem) Item(org.openhab.core.items.Item) ModelGroupItem(org.openhab.core.model.items.ModelGroupItem) ModelGroupFunction(org.openhab.core.model.items.ModelGroupFunction) ActiveItem(org.openhab.core.items.ActiveItem) ModelGroupItem(org.openhab.core.model.items.ModelGroupItem) GroupItem(org.openhab.core.items.GroupItem) ModelNormalItem(org.openhab.core.model.items.ModelNormalItem) Nullable(org.eclipse.jdt.annotation.Nullable)

Example 2 with ModelGroupItem

use of org.openhab.core.model.items.ModelGroupItem in project openhab-core by openhab.

the class GenericItemProvider method applyGroupFunction.

private GroupItem applyGroupFunction(Item baseItem, ModelGroupItem modelGroupItem, ModelGroupFunction function) {
    GroupFunctionDTO dto = new GroupFunctionDTO();
    dto.name = function.getName();
    dto.params = modelGroupItem.getArgs().toArray(new String[0]);
    GroupFunction groupFunction = ItemDTOMapper.mapFunction(baseItem, dto);
    return new GroupItem(modelGroupItem.getName(), baseItem, groupFunction);
}
Also used : GroupFunction(org.openhab.core.items.GroupFunction) ModelGroupFunction(org.openhab.core.model.items.ModelGroupFunction) ModelGroupItem(org.openhab.core.model.items.ModelGroupItem) GroupItem(org.openhab.core.items.GroupItem) GroupFunctionDTO(org.openhab.core.items.dto.GroupFunctionDTO)

Aggregations

GroupItem (org.openhab.core.items.GroupItem)2 ModelGroupFunction (org.openhab.core.model.items.ModelGroupFunction)2 ModelGroupItem (org.openhab.core.model.items.ModelGroupItem)2 Nullable (org.eclipse.jdt.annotation.Nullable)1 ActiveItem (org.openhab.core.items.ActiveItem)1 GenericItem (org.openhab.core.items.GenericItem)1 GroupFunction (org.openhab.core.items.GroupFunction)1 Item (org.openhab.core.items.Item)1 GroupFunctionDTO (org.openhab.core.items.dto.GroupFunctionDTO)1 ModelItem (org.openhab.core.model.items.ModelItem)1 ModelNormalItem (org.openhab.core.model.items.ModelNormalItem)1