Search in sources :

Example 86 with Thing

use of org.eclipse.smarthome.core.thing.Thing in project smarthome by eclipse.

the class FirmwareUpdateService method cancelFirmwareUpdate.

/**
 * Cancels the firmware update of the thing having the given thing UID by invoking the operation
 * {@link FirmwareUpdateHandler#cancel()} of the thingĀ“s firmware update handler.
 *
 * @param thingUID the thing UID (must not be null)
 */
public void cancelFirmwareUpdate(final ThingUID thingUID) {
    Objects.requireNonNull(thingUID, "Thing UID must not be null.");
    final FirmwareUpdateHandler firmwareUpdateHandler = getFirmwareUpdateHandler(thingUID);
    if (firmwareUpdateHandler == null) {
        throw new IllegalArgumentException(String.format("There is no firmware update handler for thing with UID %s.", thingUID));
    }
    final ProgressCallbackImpl progressCallback = getProgressCallback(thingUID);
    logger.debug("Cancelling firmware update for thing with UID {}.", thingUID);
    safeCaller.create(firmwareUpdateHandler, FirmwareUpdateHandler.class).withTimeout(timeout).withAsync().onTimeout(() -> {
        logger.error("Timeout occurred while cancelling firmware update of thing with UID {}.", thingUID);
        progressCallback.failedInternal("timeout-error-during-cancel");
    }).onException(e -> {
        logger.error("Unexpected exception occurred while cancelling firmware update of thing with UID {}.", thingUID, e.getCause());
        progressCallback.failedInternal("unexpected-handler-error-during-cancel");
    }).withIdentifier(new Object()).build().cancel();
}
Also used : ScheduledFuture(java.util.concurrent.ScheduledFuture) URISyntaxException(java.net.URISyntaxException) ProgressCallback(org.eclipse.smarthome.core.thing.binding.firmware.ProgressCallback) LoggerFactory(org.slf4j.LoggerFactory) Firmware(org.eclipse.smarthome.core.thing.binding.firmware.Firmware) EventFilter(org.eclipse.smarthome.core.events.EventFilter) EventSubscriber(org.eclipse.smarthome.core.events.EventSubscriber) FirmwareUpdateBackgroundTransferHandler(org.eclipse.smarthome.core.thing.binding.firmware.FirmwareUpdateBackgroundTransferHandler) ThingStatusInfoChangedEvent(org.eclipse.smarthome.core.thing.events.ThingStatusInfoChangedEvent) Component(org.osgi.service.component.annotations.Component) FirmwareStatusInfo(org.eclipse.smarthome.core.thing.firmware.FirmwareStatusInfo) SafeCaller(org.eclipse.smarthome.core.common.SafeCaller) Locale(java.util.Locale) FirmwareUID(org.eclipse.smarthome.core.thing.binding.firmware.FirmwareUID) Map(java.util.Map) Thing(org.eclipse.smarthome.core.thing.Thing) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Activate(org.osgi.service.component.annotations.Activate) URI(java.net.URI) FirmwareUpdateHandler(org.eclipse.smarthome.core.thing.binding.firmware.FirmwareUpdateHandler) ThreadPoolManager(org.eclipse.smarthome.core.common.ThreadPoolManager) TranslationProvider(org.eclipse.smarthome.core.i18n.TranslationProvider) ConfigValidationException(org.eclipse.smarthome.config.core.validation.ConfigValidationException) Logger(org.slf4j.Logger) ImmutableSet(com.google.common.collect.ImmutableSet) Deactivate(org.osgi.service.component.annotations.Deactivate) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) ConfigDescriptionValidator(org.eclipse.smarthome.config.core.validation.ConfigDescriptionValidator) EventPublisher(org.eclipse.smarthome.core.events.EventPublisher) ReferencePolicy(org.osgi.service.component.annotations.ReferencePolicy) Objects(java.util.Objects) TimeUnit(java.util.concurrent.TimeUnit) ReferenceCardinality(org.osgi.service.component.annotations.ReferenceCardinality) List(java.util.List) Modified(org.osgi.service.component.annotations.Modified) LocaleProvider(org.eclipse.smarthome.core.i18n.LocaleProvider) Event(org.eclipse.smarthome.core.events.Event) Reference(org.osgi.service.component.annotations.Reference) Collections(java.util.Collections) ThingStatus(org.eclipse.smarthome.core.thing.ThingStatus) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ThingUID(org.eclipse.smarthome.core.thing.ThingUID) FirmwareUpdateHandler(org.eclipse.smarthome.core.thing.binding.firmware.FirmwareUpdateHandler)

Example 87 with Thing

use of org.eclipse.smarthome.core.thing.Thing in project smarthome by eclipse.

the class InboxResource method approve.

@POST
@Path("/{thingUID}/approve")
@Consumes(MediaType.TEXT_PLAIN)
@ApiOperation(value = "Approves the discovery result by adding the thing to the registry.")
@ApiResponses(value = { @ApiResponse(code = 200, message = "OK"), @ApiResponse(code = 404, message = "Thing not found in the inbox."), @ApiResponse(code = 409, message = "No binding found that supports this thing.") })
public Response approve(@HeaderParam(HttpHeaders.ACCEPT_LANGUAGE) @ApiParam(value = "language") String language, @PathParam("thingUID") @ApiParam(value = "thingUID", required = true) String thingUID, @ApiParam(value = "thing label") String label) {
    ThingUID thingUIDObject = new ThingUID(thingUID);
    String notEmptyLabel = label != null && !label.isEmpty() ? label : null;
    Thing thing = null;
    try {
        thing = inbox.approve(thingUIDObject, notEmptyLabel);
    } catch (IllegalArgumentException e) {
        return JSONResponse.createErrorResponse(Status.NOT_FOUND, "Thing not found in inbox");
    }
    // inbox.approve returns null if no handler is found that supports this thing
    if (thing == null) {
        return JSONResponse.createErrorResponse(Status.CONFLICT, "No binding found that can create the thing");
    }
    return Response.ok(null, MediaType.TEXT_PLAIN).build();
}
Also used : ThingUID(org.eclipse.smarthome.core.thing.ThingUID) Thing(org.eclipse.smarthome.core.thing.Thing) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 88 with Thing

use of org.eclipse.smarthome.core.thing.Thing in project smarthome by eclipse.

the class ThingResource method getByUID.

@GET
@RolesAllowed({ Role.ADMIN })
@Path("/{thingUID}")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Gets thing by UID.")
@ApiResponses(value = { @ApiResponse(code = 200, message = "OK", response = ThingDTO.class), @ApiResponse(code = 404, message = "Thing not found.") })
public Response getByUID(@HeaderParam(HttpHeaders.ACCEPT_LANGUAGE) @ApiParam(value = "language") String language, @PathParam("thingUID") @ApiParam(value = "thingUID") String thingUID) {
    final Locale locale = LocaleUtil.getLocale(language);
    Thing thing = thingRegistry.get((new ThingUID(thingUID)));
    // return Thing data if it does exist
    if (thing != null) {
        return getThingResponse(Status.OK, thing, locale, null);
    } else {
        return getThingNotFoundResponse(thingUID);
    }
}
Also used : Locale(java.util.Locale) ThingUID(org.eclipse.smarthome.core.thing.ThingUID) Thing(org.eclipse.smarthome.core.thing.Thing) 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 89 with Thing

use of org.eclipse.smarthome.core.thing.Thing in project smarthome by eclipse.

the class ThingResource method update.

/**
 * Update Thing.
 *
 * @param thingUID
 * @param thingBean
 * @return Response with the updated Thing or error information
 * @throws IOException
 */
@PUT
@RolesAllowed({ Role.ADMIN })
@Path("/{thingUID}")
@Consumes(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Updates a thing.")
@ApiResponses(value = { @ApiResponse(code = 200, message = "OK", response = ThingDTO.class), @ApiResponse(code = 404, message = "Thing not found."), @ApiResponse(code = 409, message = "Thing could not be updated as it is not editable.") })
public Response update(@HeaderParam(HttpHeaders.ACCEPT_LANGUAGE) @ApiParam(value = "language") String language, @PathParam("thingUID") @ApiParam(value = "thingUID") String thingUID, @ApiParam(value = "thing", required = true) ThingDTO thingBean) throws IOException {
    final Locale locale = LocaleUtil.getLocale(language);
    ThingUID thingUIDObject = new ThingUID(thingUID);
    // ask whether the Thing exists at all, 404 otherwise
    Thing thing = thingRegistry.get(thingUIDObject);
    if (null == thing) {
        logger.info("Received HTTP PUT request for update at '{}' for the unknown thing '{}'.", uriInfo.getPath(), thingUID);
        return getThingNotFoundResponse(thingUID);
    }
    // ask whether the Thing exists as a managed thing, so it can get
    // updated, 409 otherwise
    Thing managed = managedThingProvider.get(thingUIDObject);
    if (null == managed) {
        logger.info("Received HTTP PUT request for update at '{}' for an unmanaged thing '{}'.", uriInfo.getPath(), thingUID);
        return getThingResponse(Status.CONFLICT, thing, locale, "Cannot update Thing " + thingUID + " as it is not editable.");
    }
    // check configuration
    thingBean.configuration = normalizeConfiguration(thingBean.configuration, thing.getThingTypeUID(), thing.getUID());
    normalizeChannels(thingBean, thing.getUID());
    thing = ThingHelper.merge(thing, thingBean);
    // update, returns null in case Thing cannot be found
    Thing oldthing = managedThingProvider.update(thing);
    if (null == oldthing) {
        return getThingNotFoundResponse(thingUID);
    }
    // everything went well
    return getThingResponse(Status.OK, thing, locale, null);
}
Also used : Locale(java.util.Locale) ThingUID(org.eclipse.smarthome.core.thing.ThingUID) Thing(org.eclipse.smarthome.core.thing.Thing) 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 90 with Thing

use of org.eclipse.smarthome.core.thing.Thing in project smarthome by eclipse.

the class ThingResource method remove.

/**
 * Delete a Thing, if possible. Thing deletion might be impossible if the
 * Thing is not managed, will return CONFLICT. Thing deletion might happen
 * delayed, will return ACCEPTED.
 *
 * @param thingUID
 * @param force
 * @return Response with status/error information
 */
@DELETE
@RolesAllowed({ Role.ADMIN })
@Path("/{thingUID}")
@ApiOperation(value = "Removes a thing from the registry. Set \'force\' to __true__ if you want the thing te be removed immediately.")
@ApiResponses(value = { @ApiResponse(code = 200, message = "OK, was deleted."), @ApiResponse(code = 202, message = "ACCEPTED for asynchronous deletion."), @ApiResponse(code = 404, message = "Thing not found."), @ApiResponse(code = 409, message = "Thing could not be deleted because it's not editable.") })
public Response remove(@HeaderParam(HttpHeaders.ACCEPT_LANGUAGE) @ApiParam(value = "language") String language, @PathParam("thingUID") @ApiParam(value = "thingUID") String thingUID, @DefaultValue("false") @QueryParam("force") @ApiParam(value = "force") boolean force) {
    final Locale locale = LocaleUtil.getLocale(language);
    ThingUID thingUIDObject = new ThingUID(thingUID);
    // check whether thing exists and throw 404 if not
    Thing thing = thingRegistry.get(thingUIDObject);
    if (thing == null) {
        logger.info("Received HTTP DELETE request for update at '{}' for the unknown thing '{}'.", uriInfo.getPath(), thingUID);
        return getThingNotFoundResponse(thingUID);
    }
    // ask whether the Thing exists as a managed thing, so it can get
    // updated, 409 otherwise
    Thing managed = managedThingProvider.get(thingUIDObject);
    if (null == managed) {
        logger.info("Received HTTP DELETE request for update at '{}' for an unmanaged thing '{}'.", uriInfo.getPath(), thingUID);
        return getThingResponse(Status.CONFLICT, thing, locale, "Cannot delete Thing " + thingUID + " as it is not editable.");
    }
    // only move on if Thing is known to be managed, so it can get updated
    if (force) {
        if (null == thingRegistry.forceRemove(thingUIDObject)) {
            return getThingResponse(Status.INTERNAL_SERVER_ERROR, thing, locale, "Cannot delete Thing " + thingUID + " for unknown reasons.");
        }
    } else {
        if (null != thingRegistry.remove(thingUIDObject)) {
            return getThingResponse(Status.ACCEPTED, thing, locale, null);
        }
    }
    return Response.ok(null, MediaType.TEXT_PLAIN).build();
}
Also used : Locale(java.util.Locale) ThingUID(org.eclipse.smarthome.core.thing.ThingUID) Thing(org.eclipse.smarthome.core.thing.Thing) 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)

Aggregations

Thing (org.eclipse.smarthome.core.thing.Thing)98 Test (org.junit.Test)43 ThingUID (org.eclipse.smarthome.core.thing.ThingUID)28 ChannelUID (org.eclipse.smarthome.core.thing.ChannelUID)24 Configuration (org.eclipse.smarthome.config.core.Configuration)19 JavaOSGiTest (org.eclipse.smarthome.test.java.JavaOSGiTest)19 JavaTest (org.eclipse.smarthome.test.java.JavaTest)18 ThingHandler (org.eclipse.smarthome.core.thing.binding.ThingHandler)14 ThingTypeUID (org.eclipse.smarthome.core.thing.ThingTypeUID)13 ApiOperation (io.swagger.annotations.ApiOperation)9 ApiResponses (io.swagger.annotations.ApiResponses)9 Item (org.eclipse.smarthome.core.items.Item)9 Path (javax.ws.rs.Path)8 Nullable (org.eclipse.jdt.annotation.Nullable)8 Locale (java.util.Locale)7 RolesAllowed (javax.annotation.security.RolesAllowed)7 Channel (org.eclipse.smarthome.core.thing.Channel)7 ThingHandlerCallback (org.eclipse.smarthome.core.thing.binding.ThingHandlerCallback)7 Command (org.eclipse.smarthome.core.types.Command)7 List (java.util.List)6