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);
}
}
}
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();
}
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();
}
}
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;
}
}
}
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());
}
}
}
Aggregations