Search in sources :

Example 86 with ThingUID

use of org.eclipse.smarthome.core.thing.ThingUID 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 87 with ThingUID

use of org.eclipse.smarthome.core.thing.ThingUID 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)

Example 88 with ThingUID

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

the class ThingResource method updateConfiguration.

/**
 * Updates Thing configuration.
 *
 * @param thingUID
 * @param configurationParameters
 * @return Response with the updated Thing or error information
 * @throws IOException
 */
@PUT
@RolesAllowed({ Role.ADMIN })
@Path("/{thingUID}/config")
@Consumes(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Updates thing's configuration.")
@ApiResponses(value = { @ApiResponse(code = 200, message = "OK", response = Thing.class), @ApiResponse(code = 400, message = "Configuration of the thing is not valid."), @ApiResponse(code = 404, message = "Thing not found"), @ApiResponse(code = 409, message = "Thing could not be updated as it is not editable.") })
public Response updateConfiguration(@HeaderParam(HttpHeaders.ACCEPT_LANGUAGE) String language, @PathParam("thingUID") @ApiParam(value = "thing") String thingUID, @ApiParam(value = "configuration parameters") Map<String, Object> configurationParameters) 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 configuration 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 configuration at '{}' for an unmanaged thing '{}'.", uriInfo.getPath(), thingUID);
        return getThingResponse(Status.CONFLICT, thing, locale, "Cannot update Thing " + thingUID + " as it is not editable.");
    }
    // only move on if Thing is known to be managed, so it can get updated
    try {
        // note that we create a Configuration instance here in order to
        // have normalized types
        thingRegistry.updateConfiguration(thingUIDObject, new Configuration(normalizeConfiguration(configurationParameters, thing.getThingTypeUID(), thing.getUID())).getProperties());
    } catch (ConfigValidationException ex) {
        logger.debug("Config description validation exception occurred for thingUID {} - Messages: {}", thingUID, ex.getValidationMessages());
        return Response.status(Status.BAD_REQUEST).entity(ex.getValidationMessages(locale)).build();
    } catch (Exception ex) {
        logger.error("Exception during HTTP PUT request for update config at '{}'", uriInfo.getPath(), ex);
        return JSONResponse.createResponse(Status.INTERNAL_SERVER_ERROR, null, ex.getMessage());
    }
    return getThingResponse(Status.OK, thing, locale, null);
}
Also used : Locale(java.util.Locale) Configuration(org.eclipse.smarthome.config.core.Configuration) ThingUID(org.eclipse.smarthome.core.thing.ThingUID) ConfigValidationException(org.eclipse.smarthome.config.core.validation.ConfigValidationException) Thing(org.eclipse.smarthome.core.thing.Thing) URISyntaxException(java.net.URISyntaxException) BadRequestException(javax.ws.rs.BadRequestException) ConfigValidationException(org.eclipse.smarthome.config.core.validation.ConfigValidationException) IOException(java.io.IOException) 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 89 with ThingUID

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

the class ThingResource method getStatus.

@GET
@RolesAllowed({ Role.USER, Role.ADMIN })
@Path("/{thingUID}/status")
@ApiOperation(value = "Gets thing's status.")
@ApiResponses(value = { @ApiResponse(code = 200, message = "OK", response = String.class), @ApiResponse(code = 404, message = "Thing not found.") })
public Response getStatus(@HeaderParam(HttpHeaders.ACCEPT_LANGUAGE) String language, @PathParam("thingUID") @ApiParam(value = "thing") String thingUID) throws IOException {
    ThingUID thingUIDObject = new ThingUID(thingUID);
    // Check if the Thing exists, 404 if not
    Thing thing = thingRegistry.get(thingUIDObject);
    if (null == thing) {
        logger.info("Received HTTP GET request for thing config status at '{}' for the unknown thing '{}'.", uriInfo.getPath(), thingUID);
        return getThingNotFoundResponse(thingUID);
    }
    ThingStatusInfo thingStatusInfo = thingStatusInfoI18nLocalizationService.getLocalizedThingStatusInfo(thing, LocaleUtil.getLocale(language));
    return Response.ok(null, MediaType.TEXT_PLAIN).entity(thingStatusInfo).build();
}
Also used : ThingUID(org.eclipse.smarthome.core.thing.ThingUID) ThingStatusInfo(org.eclipse.smarthome.core.thing.ThingStatusInfo) Thing(org.eclipse.smarthome.core.thing.Thing) Path(javax.ws.rs.Path) RolesAllowed(javax.annotation.security.RolesAllowed) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 90 with ThingUID

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

the class HueLightDiscoveryServiceOSGiTest method hueLightRegistration.

@Test
public void hueLightRegistration() {
    FullLight light = new FullLight();
    light.setId("1");
    light.setModelID("LCT001");
    light.setType("Extended color light");
    AsyncResultWrapper<DiscoveryResult> resultWrapper = new AsyncResultWrapper<DiscoveryResult>();
    registerDiscoveryListener(new DiscoveryListener() {

        @Override
        public void thingDiscovered(DiscoveryService source, DiscoveryResult result) {
            resultWrapper.set(result);
        }

        @Override
        public void thingRemoved(DiscoveryService source, ThingUID thingUID) {
        }

        @Override
        public Collection<ThingUID> removeOlderResults(DiscoveryService source, long timestamp, Collection<ThingTypeUID> thingTypeUIDs, ThingUID bridgeUID) {
            return null;
        }
    });
    discoveryService.onLightAdded(null, light);
    waitForAssert(() -> {
        assertTrue(resultWrapper.isSet());
    });
    final DiscoveryResult result = resultWrapper.getWrappedObject();
    assertThat(result.getFlag(), is(DiscoveryResultFlag.NEW));
    assertThat(result.getThingUID().toString(), is("hue:0210:testBridge:" + light.getId()));
    assertThat(result.getThingTypeUID(), is(THING_TYPE_EXTENDED_COLOR_LIGHT));
    assertThat(result.getBridgeUID(), is(hueBridge.getUID()));
    assertThat(result.getProperties().get(LIGHT_ID), is(light.getId()));
}
Also used : DiscoveryResult(org.eclipse.smarthome.config.discovery.DiscoveryResult) AsyncResultWrapper(org.eclipse.smarthome.test.AsyncResultWrapper) ThingUID(org.eclipse.smarthome.core.thing.ThingUID) Collection(java.util.Collection) ThingTypeUID(org.eclipse.smarthome.core.thing.ThingTypeUID) DiscoveryService(org.eclipse.smarthome.config.discovery.DiscoveryService) HueLightDiscoveryService(org.eclipse.smarthome.binding.hue.internal.discovery.HueLightDiscoveryService) DiscoveryListener(org.eclipse.smarthome.config.discovery.DiscoveryListener) AbstractHueOSGiTest(org.eclipse.smarthome.binding.hue.test.AbstractHueOSGiTest) Test(org.junit.Test)

Aggregations

ThingUID (org.eclipse.smarthome.core.thing.ThingUID)99 DiscoveryResult (org.eclipse.smarthome.config.discovery.DiscoveryResult)29 ThingTypeUID (org.eclipse.smarthome.core.thing.ThingTypeUID)27 Thing (org.eclipse.smarthome.core.thing.Thing)26 Test (org.junit.Test)25 HashMap (java.util.HashMap)21 JavaOSGiTest (org.eclipse.smarthome.test.java.JavaOSGiTest)14 ApiOperation (io.swagger.annotations.ApiOperation)10 ApiResponses (io.swagger.annotations.ApiResponses)10 Path (javax.ws.rs.Path)9 JsonObject (com.google.gson.JsonObject)8 JsonParser (com.google.gson.JsonParser)7 RolesAllowed (javax.annotation.security.RolesAllowed)7 Locale (java.util.Locale)6 Nullable (org.eclipse.jdt.annotation.Nullable)6 Consumes (javax.ws.rs.Consumes)5 Configuration (org.eclipse.smarthome.config.core.Configuration)5 Collection (java.util.Collection)4 GET (javax.ws.rs.GET)4 InboxPredicates.forThingUID (org.eclipse.smarthome.config.discovery.inbox.InboxPredicates.forThingUID)4