Search in sources :

Example 1 with GroupFunction

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

the class ItemDTOMapper method map.

/**
 * Maps item DTO into item object.
 *
 * @param itemDTO the DTO
 * @param itemFactories the item factories in order to create the items
 * @return the item object
 */
@Nullable
public static ActiveItem map(ItemDTO itemDTO, Set<ItemFactory> itemFactories) {
    if (itemDTO == null) {
        throw new IllegalArgumentException("The argument 'itemDTO' must no be null.");
    }
    if (itemFactories == null) {
        throw new IllegalArgumentException("The argument 'itemFactories' must no be null.");
    }
    GenericItem newItem = null;
    if (itemDTO.type != null) {
        if (itemDTO instanceof GroupItemDTO && itemDTO.type.equals(GroupItem.TYPE)) {
            GroupItemDTO groupItemDTO = (GroupItemDTO) itemDTO;
            GenericItem baseItem = null;
            if (!StringUtils.isEmpty(groupItemDTO.groupType)) {
                baseItem = createItem(groupItemDTO.groupType, itemDTO.name, itemFactories);
            }
            GroupFunction function = new GroupFunction.Equality();
            if (groupItemDTO.function != null) {
                function = mapFunction(baseItem, groupItemDTO.function);
            }
            newItem = new GroupItem(itemDTO.name, baseItem, function);
        } else {
            String itemType = itemDTO.type;
            newItem = createItem(itemType, itemDTO.name, itemFactories);
        }
        if (newItem != null) {
            if (itemDTO.label != null) {
                newItem.setLabel(itemDTO.label);
            }
            if (itemDTO.category != null) {
                newItem.setCategory(itemDTO.category);
            }
            if (itemDTO.groupNames != null) {
                newItem.addGroupNames(itemDTO.groupNames);
            }
            if (itemDTO.tags != null) {
                newItem.addTags(itemDTO.tags);
            }
        }
    }
    return newItem;
}
Also used : GroupFunction(org.eclipse.smarthome.core.items.GroupFunction) GenericItem(org.eclipse.smarthome.core.items.GenericItem) GroupItem(org.eclipse.smarthome.core.items.GroupItem) Nullable(org.eclipse.jdt.annotation.Nullable)

Example 2 with GroupFunction

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

the class GenericItemProvider method applyGroupFunction.

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

Example 3 with GroupFunction

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

the class GenericItemProvider method hasGroupItemChanged.

private boolean hasGroupItemChanged(Item item1, Item item2) {
    GroupItem gItem1 = null;
    GroupItem gItem2 = null;
    if (item1 instanceof GroupItem) {
        gItem1 = (GroupItem) item1;
    }
    if (item2 instanceof GroupItem) {
        gItem2 = (GroupItem) item2;
    }
    if (gItem1 == null && gItem2 == null) {
        return false;
    }
    if ((gItem1 != null && gItem2 == null) || (gItem1 == null && gItem2 != null)) {
        return true;
    }
    boolean sameBaseItemClass = Objects.equals(gItem1.getBaseItem(), gItem2.getBaseItem());
    boolean sameFunction = false;
    GroupFunction gf1 = gItem1.getFunction();
    GroupFunction gf2 = gItem2.getFunction();
    if (gf1 != null && gf2 != null) {
        if (Objects.equals(gf1.getClass(), gf2.getClass())) {
            sameFunction = Arrays.equals(gf1.getParameters(), gf2.getParameters());
        }
    } else if (gf1 == null && gf2 == null) {
        sameFunction = true;
    }
    return !(sameBaseItemClass && sameFunction);
}
Also used : GroupFunction(org.eclipse.smarthome.core.items.GroupFunction) ModelGroupFunction(org.eclipse.smarthome.model.items.ModelGroupFunction) GroupItem(org.eclipse.smarthome.core.items.GroupItem) ModelGroupItem(org.eclipse.smarthome.model.items.ModelGroupItem)

Example 4 with GroupFunction

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

the class ItemDTOMapperTest method testMapFunctionWithNumberItemAndCountFunction.

@Test
public void testMapFunctionWithNumberItemAndCountFunction() {
    // testing Group:Number:Count(".*hello.*")
    NumberItem item1 = new NumberItem("item1");
    GroupFunctionDTO gFuncDTO = new GroupFunctionDTO();
    gFuncDTO.name = "COUNT";
    gFuncDTO.params = new String[] { ".*hello.*" };
    GroupFunction gFunc = ItemDTOMapper.mapFunction(item1, gFuncDTO);
    assertThat(gFunc, instanceOf(ArithmeticGroupFunction.Count.class));
    assertThat(gFunc.getParameters().length, is(1));
    assertThat(gFunc.getParameters()[0], instanceOf(StringType.class));
}
Also used : NumberItem(org.eclipse.smarthome.core.library.items.NumberItem) ArithmeticGroupFunction(org.eclipse.smarthome.core.library.types.ArithmeticGroupFunction) GroupFunction(org.eclipse.smarthome.core.items.GroupFunction) StringType(org.eclipse.smarthome.core.library.types.StringType) Test(org.junit.Test)

Aggregations

GroupFunction (org.eclipse.smarthome.core.items.GroupFunction)4 GroupItem (org.eclipse.smarthome.core.items.GroupItem)3 ModelGroupFunction (org.eclipse.smarthome.model.items.ModelGroupFunction)2 ModelGroupItem (org.eclipse.smarthome.model.items.ModelGroupItem)2 Nullable (org.eclipse.jdt.annotation.Nullable)1 GenericItem (org.eclipse.smarthome.core.items.GenericItem)1 GroupFunctionDTO (org.eclipse.smarthome.core.items.dto.GroupFunctionDTO)1 NumberItem (org.eclipse.smarthome.core.library.items.NumberItem)1 ArithmeticGroupFunction (org.eclipse.smarthome.core.library.types.ArithmeticGroupFunction)1 StringType (org.eclipse.smarthome.core.library.types.StringType)1 Test (org.junit.Test)1