Search in sources :

Example 1 with GroupItem

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

the class PersistenceManagerImpl method appliesToItem.

/**
 * Checks if a given persistence configuration entry is relevant for an item
 *
 * @param config the persistence configuration entry
 * @param item to check if the configuration applies to
 * @return true, if the configuration applies to the item
 */
private boolean appliesToItem(SimpleItemConfiguration config, Item item) {
    for (SimpleConfig itemCfg : config.getItems()) {
        if (itemCfg instanceof SimpleAllConfig) {
            return true;
        }
        if (itemCfg instanceof SimpleItemConfig) {
            SimpleItemConfig singleItemConfig = (SimpleItemConfig) itemCfg;
            if (item.getName().equals(singleItemConfig.getItem())) {
                return true;
            }
        }
        if (itemCfg instanceof SimpleGroupConfig) {
            SimpleGroupConfig groupItemCfg = (SimpleGroupConfig) itemCfg;
            String groupName = groupItemCfg.getGroup();
            try {
                Item gItem = itemRegistry.getItem(groupName);
                if (gItem instanceof GroupItem) {
                    GroupItem groupItem = (GroupItem) gItem;
                    if (groupItem.getAllMembers().contains(item)) {
                        return true;
                    }
                }
            } catch (Exception e) {
            }
        }
    }
    return false;
}
Also used : SimpleConfig(org.eclipse.smarthome.core.persistence.config.SimpleConfig) SimpleGroupConfig(org.eclipse.smarthome.core.persistence.config.SimpleGroupConfig) GroupItem(org.eclipse.smarthome.core.items.GroupItem) HistoricItem(org.eclipse.smarthome.core.persistence.HistoricItem) GenericItem(org.eclipse.smarthome.core.items.GenericItem) Item(org.eclipse.smarthome.core.items.Item) SimpleAllConfig(org.eclipse.smarthome.core.persistence.config.SimpleAllConfig) SimpleItemConfig(org.eclipse.smarthome.core.persistence.config.SimpleItemConfig) GroupItem(org.eclipse.smarthome.core.items.GroupItem) ParseException(java.text.ParseException) ItemNotFoundException(org.eclipse.smarthome.core.items.ItemNotFoundException)

Example 2 with GroupItem

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

the class ItemResource method addMember.

@PUT
@RolesAllowed({ Role.ADMIN })
@Path("/{itemName: [a-zA-Z_0-9]*}/members/{memberItemName: [a-zA-Z_0-9]*}")
@ApiOperation(value = "Adds a new member to a group item.")
@ApiResponses(value = { @ApiResponse(code = 200, message = "OK"), @ApiResponse(code = 404, message = "Item or member item not found or item is not of type group item."), @ApiResponse(code = 405, message = "Member item is not editable.") })
public Response addMember(@PathParam("itemName") @ApiParam(value = "item name", required = true) String itemName, @PathParam("memberItemName") @ApiParam(value = "member item name", required = true) String memberItemName) {
    try {
        Item item = itemRegistry.getItem(itemName);
        if (!(item instanceof GroupItem)) {
            return Response.status(Status.NOT_FOUND).build();
        }
        GroupItem groupItem = (GroupItem) item;
        Item memberItem = itemRegistry.getItem(memberItemName);
        if (!(memberItem instanceof GenericItem)) {
            return Response.status(Status.NOT_FOUND).build();
        }
        if (managedItemProvider.get(memberItemName) == null) {
            return Response.status(Status.METHOD_NOT_ALLOWED).build();
        }
        GenericItem genericMemberItem = (GenericItem) memberItem;
        genericMemberItem.addGroupName(groupItem.getName());
        managedItemProvider.update(genericMemberItem);
        return Response.ok(null, MediaType.TEXT_PLAIN).build();
    } catch (ItemNotFoundException e) {
        return Response.status(Status.NOT_FOUND).build();
    }
}
Also used : ActiveItem(org.eclipse.smarthome.core.items.ActiveItem) SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) RollershutterItem(org.eclipse.smarthome.core.library.items.RollershutterItem) GroupItem(org.eclipse.smarthome.core.items.GroupItem) GenericItem(org.eclipse.smarthome.core.items.GenericItem) Item(org.eclipse.smarthome.core.items.Item) GenericItem(org.eclipse.smarthome.core.items.GenericItem) GroupItem(org.eclipse.smarthome.core.items.GroupItem) ItemNotFoundException(org.eclipse.smarthome.core.items.ItemNotFoundException) Path(javax.ws.rs.Path) RolesAllowed(javax.annotation.security.RolesAllowed) ApiOperation(io.swagger.annotations.ApiOperation) PUT(javax.ws.rs.PUT) ApiResponses(io.swagger.annotations.ApiResponses)

Example 3 with GroupItem

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

the class EnrichedItemDTOMapper method map.

private static EnrichedItemDTO map(Item item, ItemDTO itemDTO, URI uri, boolean drillDown, Predicate<Item> itemFilter, Locale locale) {
    String state = item.getState().toFullString();
    String transformedState = considerTransformation(state, item.getStateDescription(locale));
    if (transformedState != null && transformedState.equals(state)) {
        transformedState = null;
    }
    StateDescription stateDescription = considerTransformation(item.getStateDescription(locale));
    String link = null != uri ? uri.toASCIIString() + ItemResource.PATH_ITEMS + "/" + itemDTO.name : null;
    EnrichedItemDTO enrichedItemDTO = null;
    if (item instanceof GroupItem) {
        GroupItem groupItem = (GroupItem) item;
        EnrichedItemDTO[] memberDTOs;
        if (drillDown) {
            Collection<EnrichedItemDTO> members = new LinkedHashSet<>();
            for (Item member : groupItem.getMembers()) {
                if (itemFilter == null || itemFilter.test(member)) {
                    members.add(map(member, drillDown, itemFilter, uri, locale));
                }
            }
            memberDTOs = members.toArray(new EnrichedItemDTO[members.size()]);
        } else {
            memberDTOs = new EnrichedItemDTO[0];
        }
        enrichedItemDTO = new EnrichedGroupItemDTO(itemDTO, memberDTOs, link, state, transformedState, stateDescription);
    } else {
        enrichedItemDTO = new EnrichedItemDTO(itemDTO, link, state, transformedState, stateDescription);
    }
    return enrichedItemDTO;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Item(org.eclipse.smarthome.core.items.Item) GroupItem(org.eclipse.smarthome.core.items.GroupItem) GroupItem(org.eclipse.smarthome.core.items.GroupItem) StateDescription(org.eclipse.smarthome.core.types.StateDescription)

Example 4 with GroupItem

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

the class EnrichedItemDTOMapperTest method testFiltering.

@Test
public void testFiltering() {
    GroupItem group = new GroupItem("TestGroup");
    GroupItem subGroup = new GroupItem("TestSubGroup");
    GenericItem switchItem = itemFactory.createItem(CoreItemFactory.SWITCH, "TestSwitch");
    GenericItem numberItem = itemFactory.createItem(CoreItemFactory.NUMBER, "TestNumber");
    GenericItem stringItem = itemFactory.createItem(CoreItemFactory.STRING, "TestString");
    if (switchItem != null && numberItem != null && stringItem != null) {
        group.addMember(subGroup);
        group.addMember(switchItem);
        group.addMember(numberItem);
        subGroup.addMember(stringItem);
    }
    EnrichedGroupItemDTO dto = (EnrichedGroupItemDTO) EnrichedItemDTOMapper.map(group, false, null, URI.create(""), null);
    assertThat(dto.members.length, is(0));
    dto = (EnrichedGroupItemDTO) EnrichedItemDTOMapper.map(group, true, null, URI.create(""), null);
    assertThat(dto.members.length, is(3));
    assertThat(((EnrichedGroupItemDTO) dto.members[0]).members.length, is(1));
    dto = (EnrichedGroupItemDTO) EnrichedItemDTOMapper.map(group, true, i -> i.getType().equals(CoreItemFactory.NUMBER), URI.create(""), null);
    assertThat(dto.members.length, is(1));
    dto = (EnrichedGroupItemDTO) EnrichedItemDTOMapper.map(group, true, i -> i.getType().equals(CoreItemFactory.NUMBER) || i instanceof GroupItem, URI.create(""), null);
    assertThat(dto.members.length, is(2));
    assertThat(((EnrichedGroupItemDTO) dto.members[0]).members.length, is(0));
    dto = (EnrichedGroupItemDTO) EnrichedItemDTOMapper.map(group, true, i -> i.getType().equals(CoreItemFactory.NUMBER) || i instanceof GroupItem, URI.create(""), null);
    assertThat(dto.members.length, is(2));
    assertThat(((EnrichedGroupItemDTO) dto.members[0]).members.length, is(0));
    dto = (EnrichedGroupItemDTO) EnrichedItemDTOMapper.map(group, true, i -> i.getType().equals(CoreItemFactory.NUMBER) || i.getType().equals(CoreItemFactory.STRING) || i instanceof GroupItem, URI.create(""), null);
    assertThat(dto.members.length, is(2));
    assertThat(((EnrichedGroupItemDTO) dto.members[0]).members.length, is(1));
}
Also used : GenericItem(org.eclipse.smarthome.core.items.GenericItem) GroupItem(org.eclipse.smarthome.core.items.GroupItem) EnrichedGroupItemDTO(org.eclipse.smarthome.io.rest.core.item.EnrichedGroupItemDTO) Test(org.junit.Test) JavaTest(org.eclipse.smarthome.test.java.JavaTest)

Example 5 with GroupItem

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

the class ItemUIRegistryImpl method getDynamicGroupChildren.

/**
 * This method creates a list of children for a group dynamically.
 * If there are no explicit children defined in a sitemap, the children
 * can thus be created on the fly by iterating over the members of the group item.
 *
 * @param group The group widget to get children for
 * @return a list of default widgets provided for the member items
 */
private EList<Widget> getDynamicGroupChildren(Group group) {
    EList<Widget> children = new BasicEList<Widget>();
    String itemName = group.getItem();
    try {
        if (itemName != null) {
            Item item = getItem(itemName);
            if (item instanceof GroupItem) {
                GroupItem groupItem = (GroupItem) item;
                for (Item member : groupItem.getMembers()) {
                    Widget widget = getDefaultWidget(member.getClass(), member.getName());
                    if (widget != null) {
                        widget.setItem(member.getName());
                        children.add(widget);
                    }
                }
            } else {
                logger.warn("Item '{}' is not a group.", item.getName());
            }
        } else {
            logger.warn("Group does not specify an associated item - ignoring it.");
        }
    } catch (ItemNotFoundException e) {
        logger.warn("Dynamic group with label '{}' will be ignored, because its item '{}' does not exist.", group.getLabel(), itemName);
    }
    return children;
}
Also used : NumberItem(org.eclipse.smarthome.core.library.items.NumberItem) CallItem(org.eclipse.smarthome.core.library.items.CallItem) SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) DateTimeItem(org.eclipse.smarthome.core.library.items.DateTimeItem) RollershutterItem(org.eclipse.smarthome.core.library.items.RollershutterItem) GroupItem(org.eclipse.smarthome.core.items.GroupItem) ColorItem(org.eclipse.smarthome.core.library.items.ColorItem) LocationItem(org.eclipse.smarthome.core.library.items.LocationItem) ContactItem(org.eclipse.smarthome.core.library.items.ContactItem) GenericItem(org.eclipse.smarthome.core.items.GenericItem) Item(org.eclipse.smarthome.core.items.Item) StringItem(org.eclipse.smarthome.core.library.items.StringItem) PlayerItem(org.eclipse.smarthome.core.library.items.PlayerItem) ImageItem(org.eclipse.smarthome.core.library.items.ImageItem) DimmerItem(org.eclipse.smarthome.core.library.items.DimmerItem) BasicEList(org.eclipse.emf.common.util.BasicEList) Widget(org.eclipse.smarthome.model.sitemap.Widget) LinkableWidget(org.eclipse.smarthome.model.sitemap.LinkableWidget) GroupItem(org.eclipse.smarthome.core.items.GroupItem) ItemNotFoundException(org.eclipse.smarthome.core.items.ItemNotFoundException)

Aggregations

GroupItem (org.eclipse.smarthome.core.items.GroupItem)29 Item (org.eclipse.smarthome.core.items.Item)18 ItemNotFoundException (org.eclipse.smarthome.core.items.ItemNotFoundException)13 GenericItem (org.eclipse.smarthome.core.items.GenericItem)12 SwitchItem (org.eclipse.smarthome.core.library.items.SwitchItem)11 Test (org.junit.Test)8 NumberItem (org.eclipse.smarthome.core.library.items.NumberItem)7 JavaOSGiTest (org.eclipse.smarthome.test.java.JavaOSGiTest)7 RollershutterItem (org.eclipse.smarthome.core.library.items.RollershutterItem)5 ArithmeticGroupFunction (org.eclipse.smarthome.core.library.types.ArithmeticGroupFunction)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 Date (java.util.Date)3 GroupFunction (org.eclipse.smarthome.core.items.GroupFunction)3 HistoricItem (org.eclipse.smarthome.core.persistence.HistoricItem)3 ModelGroupFunction (org.eclipse.smarthome.model.items.ModelGroupFunction)3 ModelGroupItem (org.eclipse.smarthome.model.items.ModelGroupItem)3 ApiOperation (io.swagger.annotations.ApiOperation)2 ApiResponses (io.swagger.annotations.ApiResponses)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2