Search in sources :

Example 11 with MetadataKey

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

the class SemanticsMetadataProvider method processItem.

/**
 * Updates the semantic metadata for an item and notifies all listeners about changes
 *
 * @param item the item to update the metadata for
 */
@SuppressWarnings({ "null", "unused" })
private void processItem(Item item) {
    MetadataKey key = new MetadataKey(NAMESPACE, item.getName());
    Map<String, Object> configuration = new HashMap<>();
    Class<? extends Tag> type = SemanticTags.getSemanticType(item);
    if (type != null) {
        processProperties(item, configuration);
        processHierarchy(item, configuration);
        Metadata md = new Metadata(key, type.getAnnotation(TagInfo.class).id(), configuration);
        Metadata oldMd = semantics.put(item.getName(), md);
        if (oldMd == null) {
            notifyListenersAboutAddedElement(md);
        } else {
            notifyListenersAboutUpdatedElement(oldMd, md);
        }
    }
}
Also used : HashMap(java.util.HashMap) Metadata(org.eclipse.smarthome.core.items.Metadata) MetadataKey(org.eclipse.smarthome.core.items.MetadataKey)

Example 12 with MetadataKey

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

the class ItemResource method removeMetadata.

@DELETE
@RolesAllowed({ Role.ADMIN })
@Path("/{itemname: [a-zA-Z_0-9]*}/metadata/{namespace}")
@ApiOperation(value = "Removes metadata from an item.")
@ApiResponses(value = { @ApiResponse(code = 200, message = "OK"), @ApiResponse(code = 404, message = "Item not found."), @ApiResponse(code = 405, message = "Meta data not editable.") })
public Response removeMetadata(@PathParam("itemname") @ApiParam(value = "item name", required = true) String itemname, @PathParam("namespace") @ApiParam(value = "namespace", required = true) String namespace) {
    Item item = getItem(itemname);
    if (item == null) {
        logger.info("Received HTTP DELETE request at '{}' for the unknown item '{}'.", uriInfo.getPath(), itemname);
        return Response.status(Status.NOT_FOUND).build();
    }
    MetadataKey key = new MetadataKey(namespace, itemname);
    if (metadataRegistry.get(key) != null) {
        if (metadataRegistry.remove(key) == null) {
            logger.info("Received HTTP DELETE request at '{}' for unmanaged item meta-data '{}'.", uriInfo.getPath(), key);
            return Response.status(Status.CONFLICT).build();
        }
    } else {
        logger.info("Received HTTP DELETE request at '{}' for unknown item meta-data '{}'.", uriInfo.getPath(), key);
        return Response.status(Status.NOT_FOUND).build();
    }
    return Response.ok(null, MediaType.TEXT_PLAIN).build();
}
Also used : 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) MetadataKey(org.eclipse.smarthome.core.items.MetadataKey) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE) RolesAllowed(javax.annotation.security.RolesAllowed) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 13 with MetadataKey

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

the class ItemResource method addMetadata.

@PUT
@RolesAllowed({ Role.ADMIN })
@Path("/{itemname: [a-zA-Z_0-9]*}/metadata/{namespace}")
@Consumes(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Adds metadata to an item.")
@ApiResponses(value = { // 
@ApiResponse(code = 200, message = "OK"), // 
@ApiResponse(code = 201, message = "Created"), // 
@ApiResponse(code = 400, message = "Metadata value empty."), // 
@ApiResponse(code = 404, message = "Item not found."), @ApiResponse(code = 405, message = "Metadata not editable.") })
public Response addMetadata(@PathParam("itemname") @ApiParam(value = "item name", required = true) String itemname, @PathParam("namespace") @ApiParam(value = "namespace", required = true) String namespace, @ApiParam(value = "metadata", required = true) MetadataDTO metadata) {
    Item item = getItem(itemname);
    if (item == null) {
        logger.info("Received HTTP PUT request at '{}' for the unknown item '{}'.", uriInfo.getPath(), itemname);
        return Response.status(Status.NOT_FOUND).build();
    }
    String value = metadata.value;
    if (value == null || value.isEmpty()) {
        logger.info("Received HTTP PUT request at '{}' for item '{}' with empty metadata.", uriInfo.getPath(), itemname);
        return Response.status(Status.BAD_REQUEST).build();
    }
    MetadataKey key = new MetadataKey(namespace, itemname);
    Metadata md = new Metadata(key, value, metadata.config);
    if (metadataRegistry.get(key) == null) {
        metadataRegistry.add(md);
        return Response.status(Status.CREATED).type(MediaType.TEXT_PLAIN).build();
    } else {
        metadataRegistry.update(md);
        return Response.ok(null, MediaType.TEXT_PLAIN).build();
    }
}
Also used : 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) Metadata(org.eclipse.smarthome.core.items.Metadata) MetadataKey(org.eclipse.smarthome.core.items.MetadataKey) Path(javax.ws.rs.Path) RolesAllowed(javax.annotation.security.RolesAllowed) Consumes(javax.ws.rs.Consumes) ApiOperation(io.swagger.annotations.ApiOperation) PUT(javax.ws.rs.PUT) ApiResponses(io.swagger.annotations.ApiResponses)

Example 14 with MetadataKey

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

the class AutoUpdateManager method receiveCommand.

public void receiveCommand(ItemCommandEvent commandEvent, Item item) {
    if (!enabled) {
        return;
    }
    final String itemName = commandEvent.getItemName();
    final Command command = commandEvent.getItemCommand();
    if (command instanceof State) {
        final State state = (State) command;
        Recommendation autoUpdate = shouldAutoUpdate(itemName);
        // consider user-override via item meta-data
        MetadataKey key = new MetadataKey(AUTOUPDATE_KEY, itemName);
        Metadata metadata = metadataRegistry.get(key);
        if (metadata != null && !metadata.getValue().trim().isEmpty()) {
            boolean override = Boolean.parseBoolean(metadata.getValue());
            if (override) {
                logger.trace("Auto update strategy {} overriden by item metadata to REQUIRED", autoUpdate);
                autoUpdate = Recommendation.REQUIRED;
            } else {
                logger.trace("Auto update strategy {} overriden by item metadata to DONT", autoUpdate);
                autoUpdate = Recommendation.DONT;
            }
        }
        switch(autoUpdate) {
            case REQUIRED:
                logger.trace("Automatically updating item '{}' because no channel is linked", itemName);
                postUpdate(item, state, EVENT_SOURCE);
                break;
            case RECOMMENDED:
                logger.trace("Automatically updating item '{}' because no channel does it", itemName);
                postUpdate(item, state, EVENT_SOURCE);
                break;
            case OPTIMISTIC:
                logger.trace("Optimistically updating item '{}'", itemName);
                postPrediction(item, state, false);
                if (sendOptimisticUpdates) {
                    postUpdate(item, state, EVENT_SOURCE_OPTIMISTIC);
                }
                break;
            case DONT:
                logger.trace("Won't update item '{}' as it was vetoed.", itemName);
                break;
            case REVERT:
                logger.trace("Sending current item state to revert controls '{}'", itemName);
                postPrediction(item, item.getState(), true);
                break;
        }
    }
}
Also used : Command(org.eclipse.smarthome.core.types.Command) State(org.eclipse.smarthome.core.types.State) Metadata(org.eclipse.smarthome.core.items.Metadata) MetadataKey(org.eclipse.smarthome.core.items.MetadataKey)

Example 15 with MetadataKey

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

the class MetadataConsoleCommandExtension method addMetadata.

private void addMetadata(Console console, String itemName, String namespace, String value, @Nullable String config) {
    if (itemRegistry.get(itemName) == null) {
        console.println("Item " + itemName + " does not exist.");
    } else {
        MetadataKey key = new MetadataKey(namespace, itemName);
        Map<String, Object> configMap = getConfigMap(config);
        Metadata metadata = new Metadata(key, value, configMap);
        if (metadataRegistry.get(key) != null) {
            metadataRegistry.update(metadata);
            console.println("Updated: " + metadata.toString());
        } else {
            metadataRegistry.add(metadata);
            console.println("Added: " + metadata.toString());
        }
    }
}
Also used : Metadata(org.eclipse.smarthome.core.items.Metadata) MetadataKey(org.eclipse.smarthome.core.items.MetadataKey)

Aggregations

MetadataKey (org.eclipse.smarthome.core.items.MetadataKey)15 Metadata (org.eclipse.smarthome.core.items.Metadata)13 Test (org.junit.Test)6 SwitchItem (org.eclipse.smarthome.core.library.items.SwitchItem)5 GenericItem (org.eclipse.smarthome.core.items.GenericItem)4 GroupItem (org.eclipse.smarthome.core.items.GroupItem)4 Item (org.eclipse.smarthome.core.items.Item)4 JavaOSGiTest (org.eclipse.smarthome.test.java.JavaOSGiTest)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ApiOperation (io.swagger.annotations.ApiOperation)2 ApiResponses (io.swagger.annotations.ApiResponses)2 HashMap (java.util.HashMap)2 RolesAllowed (javax.annotation.security.RolesAllowed)2 Path (javax.ws.rs.Path)2 NumberItem (org.eclipse.smarthome.core.library.items.NumberItem)2 RollershutterItem (org.eclipse.smarthome.core.library.items.RollershutterItem)2 JsonObject (com.google.gson.JsonObject)1 BigDecimal (java.math.BigDecimal)1 Consumes (javax.ws.rs.Consumes)1 DELETE (javax.ws.rs.DELETE)1