Search in sources :

Example 91 with Thing

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

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

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

the class FSInternetRadioHandlerJavaTest method offlineIfZeroPort.

/**
 * Verify OFFLINE Thing status when the PORT is zero.
 */
@Test
public void offlineIfZeroPort() {
    Configuration config = createConfiguration(DEFAULT_CONFIG_PROPERTY_IP, DEFAULT_CONFIG_PROPERTY_PIN, "0", DEFAULT_CONFIG_PROPERTY_REFRESH);
    Thing radioThingWithZeroPort = initializeRadioThing(config);
    testRadioThingConsideringConfiguration(radioThingWithZeroPort);
}
Also used : Configuration(org.eclipse.smarthome.config.core.Configuration) Thing(org.eclipse.smarthome.core.thing.Thing) Test(org.junit.Test) JavaTest(org.eclipse.smarthome.test.java.JavaTest)

Example 94 with Thing

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

the class FSInternetRadioHandlerJavaTest method powerChannelUpdated.

/**
 * Verify the power channel is updated.
 */
@Test
public void powerChannelUpdated() {
    Thing radioThing = initializeRadioThing(DEFAULT_COMPLETE_CONFIGURATION);
    testRadioThingConsideringConfiguration(radioThing);
    ChannelUID powerChannelUID = powerChannel.getUID();
    initializeItem(powerChannelUID, DEFAULT_TEST_ITEM_NAME, acceptedItemTypes.get(FSInternetRadioBindingConstants.CHANNEL_POWER));
    radioHandler.handleCommand(powerChannelUID, OnOffType.ON);
    waitForAssert(() -> {
        assertTrue("We should be able to turn on the radio", radioServiceDummy.containsRequestParameter(1, CHANNEL_POWER));
        radioServiceDummy.clearRequestParameters();
    });
    radioHandler.handleCommand(powerChannelUID, OnOffType.OFF);
    waitForAssert(() -> {
        assertTrue("We should be able to turn off the radio", radioServiceDummy.containsRequestParameter(0, CHANNEL_POWER));
        radioServiceDummy.clearRequestParameters();
    });
    /*
         * Setting the needed boolean variable to true, so we can be sure
         * that an invalid value will be returned in the XML response
         */
    radioHandler.handleCommand(powerChannelUID, OnOffType.ON);
    waitForAssert(() -> {
        assertTrue("We should be able to turn on the radio", radioServiceDummy.containsRequestParameter(1, CHANNEL_POWER));
        radioServiceDummy.clearRequestParameters();
    });
}
Also used : ChannelUID(org.eclipse.smarthome.core.thing.ChannelUID) Thing(org.eclipse.smarthome.core.thing.Thing) Test(org.junit.Test) JavaTest(org.eclipse.smarthome.test.java.JavaTest)

Example 95 with Thing

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

the class FSInternetRadioHandlerJavaTest method playInfoTextChannelUpdated.

/**
 * Verify the playInfoText channel is updated.
 */
@Test
public void playInfoTextChannelUpdated() {
    String playInfoTextChannelID = FSInternetRadioBindingConstants.CHANNEL_PLAY_INFO_TEXT;
    String acceptedItemType = acceptedItemTypes.get(playInfoTextChannelID);
    createChannel(DEFAULT_THING_UID, playInfoTextChannelID, acceptedItemType);
    Thing radioThing = initializeRadioThingWithMockedHandler(DEFAULT_COMPLETE_CONFIGURATION);
    testRadioThingConsideringConfiguration(radioThing);
    turnTheRadioOn(radioThing);
    ChannelUID playInfoTextChannelUID = getChannelUID(radioThing, FSInternetRadioBindingConstants.CHANNEL_PLAY_INFO_TEXT);
    initializeItem(playInfoTextChannelUID, DEFAULT_TEST_ITEM_NAME, acceptedItemType);
    waitForAssert(() -> {
        verifyOnlineStatusIsSet();
    });
}
Also used : ChannelUID(org.eclipse.smarthome.core.thing.ChannelUID) Thing(org.eclipse.smarthome.core.thing.Thing) Test(org.junit.Test) JavaTest(org.eclipse.smarthome.test.java.JavaTest)

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