Search in sources :

Example 6 with Item

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

the class ItemResource method getItemData.

@GET
@RolesAllowed({ Role.USER, Role.ADMIN })
@Path("/{itemname: [a-zA-Z_0-9]*}")
@Produces({ MediaType.APPLICATION_JSON })
@ApiOperation(value = "Gets a single item.", response = EnrichedItemDTO.class)
@ApiResponses(value = { @ApiResponse(code = 200, message = "OK", response = EnrichedItemDTO.class), @ApiResponse(code = 404, message = "Item not found") })
public Response getItemData(@HeaderParam(HttpHeaders.ACCEPT_LANGUAGE) @ApiParam(value = "language") String language, @PathParam("itemname") @ApiParam(value = "item name", required = true) String itemname) {
    final Locale locale = LocaleUtil.getLocale(language);
    logger.debug("Received HTTP GET request at '{}'", uriInfo.getPath());
    // get item
    Item item = getItem(itemname);
    // if it exists
    if (item != null) {
        logger.debug("Received HTTP GET request at '{}'.", uriInfo.getPath());
        return getItemResponse(Status.OK, item, locale, null);
    } else {
        logger.info("Received HTTP GET request at '{}' for the unknown item '{}'.", uriInfo.getPath(), itemname);
        return getItemNotFoundResponse(itemname);
    }
}
Also used : Locale(java.util.Locale) 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) Path(javax.ws.rs.Path) RolesAllowed(javax.annotation.security.RolesAllowed) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 7 with Item

use of org.eclipse.smarthome.core.items.Item 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 8 with Item

use of org.eclipse.smarthome.core.items.Item 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 9 with Item

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

the class SitemapResource method getAllItems.

/**
 * Collects all items that are represented by a given list of widgets
 *
 * @param widgets
 *            the widget list to get the items for added to all bundles containing REST resources
 * @return all items that are represented by the list of widgets
 */
private Set<GenericItem> getAllItems(EList<Widget> widgets) {
    Set<GenericItem> items = new HashSet<GenericItem>();
    if (itemUIRegistry != null) {
        for (Widget widget : widgets) {
            String itemName = widget.getItem();
            if (itemName != null) {
                try {
                    Item item = itemUIRegistry.getItem(itemName);
                    if (item instanceof GenericItem) {
                        items.add((GenericItem) item);
                    }
                } catch (ItemNotFoundException e) {
                // ignore
                }
            }
            // Consider all items inside the frame
            if (widget instanceof Frame) {
                items.addAll(getAllItems(((Frame) widget).getChildren()));
            }
            // Consider items involved in any visibility, labelcolor and valuecolor condition
            items.addAll(getItemsInVisibilityCond(widget.getVisibility()));
            items.addAll(getItemsInColorCond(widget.getLabelColor()));
            items.addAll(getItemsInColorCond(widget.getValueColor()));
        }
    }
    return items;
}
Also used : GenericItem(org.eclipse.smarthome.core.items.GenericItem) Item(org.eclipse.smarthome.core.items.Item) Frame(org.eclipse.smarthome.model.sitemap.Frame) GenericItem(org.eclipse.smarthome.core.items.GenericItem) Widget(org.eclipse.smarthome.model.sitemap.Widget) LinkableWidget(org.eclipse.smarthome.model.sitemap.LinkableWidget) HashSet(java.util.HashSet) ItemNotFoundException(org.eclipse.smarthome.core.items.ItemNotFoundException)

Example 10 with Item

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

the class PageChangeListener method addItemWithName.

private void addItemWithName(Set<Item> items, String itemName) {
    if (itemName != null) {
        try {
            Item item = itemUIRegistry.getItem(itemName);
            items.add(item);
        } catch (ItemNotFoundException e) {
        // ignore
        }
    }
}
Also used : GenericItem(org.eclipse.smarthome.core.items.GenericItem) Item(org.eclipse.smarthome.core.items.Item) GroupItem(org.eclipse.smarthome.core.items.GroupItem) ItemNotFoundException(org.eclipse.smarthome.core.items.ItemNotFoundException)

Aggregations

Item (org.eclipse.smarthome.core.items.Item)111 GroupItem (org.eclipse.smarthome.core.items.GroupItem)42 GenericItem (org.eclipse.smarthome.core.items.GenericItem)39 ItemNotFoundException (org.eclipse.smarthome.core.items.ItemNotFoundException)37 SwitchItem (org.eclipse.smarthome.core.library.items.SwitchItem)35 NumberItem (org.eclipse.smarthome.core.library.items.NumberItem)31 State (org.eclipse.smarthome.core.types.State)26 Test (org.junit.Test)26 ItemRegistry (org.eclipse.smarthome.core.items.ItemRegistry)14 RollershutterItem (org.eclipse.smarthome.core.library.items.RollershutterItem)14 StringItem (org.eclipse.smarthome.core.library.items.StringItem)14 JavaOSGiTest (org.eclipse.smarthome.test.java.JavaOSGiTest)13 DimmerItem (org.eclipse.smarthome.core.library.items.DimmerItem)12 ColorItem (org.eclipse.smarthome.core.library.items.ColorItem)10 IntentInterpretation (org.openhab.ui.habot.nlp.IntentInterpretation)10 EventPublisher (org.eclipse.smarthome.core.events.EventPublisher)9 DecimalType (org.eclipse.smarthome.core.library.types.DecimalType)9 Thing (org.eclipse.smarthome.core.thing.Thing)9 Set (java.util.Set)8 Widget (org.eclipse.smarthome.model.sitemap.Widget)8