Search in sources :

Example 31 with Thing

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

the class ThingHelper method merge.

/**
 * Merges the content of a ThingDTO with an existing Thing.
 * Where ever the DTO has null values, the content of the original Thing is kept.
 * Where ever the DTO has non-null values, these are used.
 * In consequence, care must be taken when the content of a list (like configuration, properties or channels) is to
 * be updated - the DTO must contain the full list, otherwise entries will be deleted.
 *
 * @param thing the Thing instance to merge the new content into
 * @param updatedContents a DTO which carries the updated content
 * @return A Thing instance, which is the result of the merge
 */
public static Thing merge(Thing thing, ThingDTO updatedContents) {
    ThingBuilder builder;
    if (thing instanceof Bridge) {
        builder = BridgeBuilder.create(thing.getThingTypeUID(), thing.getUID());
    } else {
        builder = ThingBuilder.create(thing.getThingTypeUID(), thing.getUID());
    }
    // Update the label
    if (updatedContents.label != null) {
        builder.withLabel(updatedContents.label);
    } else {
        builder.withLabel(thing.getLabel());
    }
    // Update the location
    if (updatedContents.location != null) {
        builder.withLocation(updatedContents.location);
    } else {
        builder.withLocation(thing.getLocation());
    }
    // update bridge UID
    if (updatedContents.bridgeUID != null) {
        builder.withBridge(new ThingUID(updatedContents.bridgeUID));
    } else {
        builder.withBridge(thing.getBridgeUID());
    }
    // update thing configuration
    if (updatedContents.configuration != null && !updatedContents.configuration.keySet().isEmpty()) {
        builder.withConfiguration(new Configuration(updatedContents.configuration));
    } else {
        builder.withConfiguration(thing.getConfiguration());
    }
    // update thing properties
    if (updatedContents.properties != null) {
        builder.withProperties(updatedContents.properties);
    } else {
        builder.withProperties(thing.getProperties());
    }
    // Update the channels
    if (updatedContents.channels != null) {
        for (ChannelDTO channelDTO : updatedContents.channels) {
            builder.withChannel(ChannelDTOMapper.map(channelDTO));
        }
    } else {
        builder.withChannels(thing.getChannels());
    }
    if (updatedContents.location != null) {
        builder.withLocation(updatedContents.location);
    } else {
        builder.withLocation(thing.getLocation());
    }
    Thing mergedThing = builder.build();
    // keep all child things in place on a merged bridge
    if (mergedThing instanceof BridgeImpl && thing instanceof Bridge) {
        Bridge bridge = (Bridge) thing;
        BridgeImpl mergedBridge = (BridgeImpl) mergedThing;
        for (Thing child : bridge.getThings()) {
            mergedBridge.addThing(child);
        }
    }
    return mergedThing;
}
Also used : BridgeImpl(org.eclipse.smarthome.core.thing.internal.BridgeImpl) ThingBuilder(org.eclipse.smarthome.core.thing.binding.builder.ThingBuilder) Configuration(org.eclipse.smarthome.config.core.Configuration) ThingUID(org.eclipse.smarthome.core.thing.ThingUID) ChannelDTO(org.eclipse.smarthome.core.thing.dto.ChannelDTO) Bridge(org.eclipse.smarthome.core.thing.Bridge) Thing(org.eclipse.smarthome.core.thing.Thing)

Example 32 with Thing

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

the class FSInternetRadioHandlerJavaTest method modeChannelUdpated.

/**
 * Verify the mode channel is updated.
 */
@Test
public void modeChannelUdpated() {
    String modeChannelID = FSInternetRadioBindingConstants.CHANNEL_MODE;
    String acceptedItemType = acceptedItemTypes.get(modeChannelID);
    createChannel(DEFAULT_THING_UID, modeChannelID, acceptedItemType);
    Thing radioThing = initializeRadioThing(DEFAULT_COMPLETE_CONFIGURATION);
    testRadioThingConsideringConfiguration(radioThing);
    turnTheRadioOn(radioThing);
    ChannelUID modeChannelUID = getChannelUID(radioThing, modeChannelID);
    initializeItem(modeChannelUID, DEFAULT_TEST_ITEM_NAME, acceptedItemType);
    radioHandler.handleCommand(modeChannelUID, DecimalType.valueOf("1"));
    waitForAssert(() -> {
        assertTrue("We should be able to update the mode channel correctly", radioServiceDummy.containsRequestParameter(1, CHANNEL_MODE));
        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(modeChannelUID, DecimalType.valueOf("3"));
    waitForAssert(() -> {
        assertTrue("We should be able to update the mode channel correctly", radioServiceDummy.containsRequestParameter(3, CHANNEL_MODE));
        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 33 with Thing

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

the class FSInternetRadioHandlerJavaTest method offlineIfParseError.

/**
 * Verify OFFLINE Thing status when the HTTP response cannot be parsed correctly.
 */
@Test
public void offlineIfParseError() {
    // create a thing with two channels - the power channel and any of the others
    String modeChannelID = FSInternetRadioBindingConstants.CHANNEL_MODE;
    String acceptedItemType = acceptedItemTypes.get(modeChannelID);
    createChannel(DEFAULT_THING_UID, modeChannelID, acceptedItemType);
    Thing radioThing = initializeRadioThing(DEFAULT_COMPLETE_CONFIGURATION);
    testRadioThingConsideringConfiguration(radioThing);
    ChannelUID modeChannelUID = getChannelUID(radioThing, modeChannelID);
    /*
         * Setting the isInvalidResponseExpected variable to true
         * in order to get the incorrect XML response from the servlet
         */
    radioServiceDummy.setInvalidResponse(true);
    // try to handle a command
    radioHandler.handleCommand(modeChannelUID, DecimalType.valueOf("1"));
    waitForAssert(() -> {
        String exceptionMessage = "java.io.IOException: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 2;";
        verifyCommunicationError(exceptionMessage);
    });
    radioServiceDummy.setInvalidResponse(false);
}
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 34 with Thing

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

the class FSInternetRadioHandlerJavaTest method playInfoNameChannelUpdated.

/**
 * Verify the playInfoName channel is updated.
 */
@Test
public void playInfoNameChannelUpdated() {
    String playInfoNameChannelID = FSInternetRadioBindingConstants.CHANNEL_PLAY_INFO_NAME;
    String acceptedItemType = acceptedItemTypes.get(playInfoNameChannelID);
    createChannel(DEFAULT_THING_UID, playInfoNameChannelID, acceptedItemType);
    Thing radioThing = initializeRadioThingWithMockedHandler(DEFAULT_COMPLETE_CONFIGURATION);
    testRadioThingConsideringConfiguration(radioThing);
    turnTheRadioOn(radioThing);
    ChannelUID playInfoNameChannelUID = getChannelUID(radioThing, FSInternetRadioBindingConstants.CHANNEL_PLAY_INFO_NAME);
    initializeItem(playInfoNameChannelUID, 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)

Example 35 with Thing

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

the class FSInternetRadioHandlerJavaTest method validInvalidPercVolume.

/**
 * Verify the valid and invalid values when updating CHANNEL_VOLUME_PERCENT are handled correctly.
 */
@Test
public void validInvalidPercVolume() {
    String absoluteVolumeChannelID = FSInternetRadioBindingConstants.CHANNEL_VOLUME_ABSOLUTE;
    String absoluteAcceptedItemType = acceptedItemTypes.get(absoluteVolumeChannelID);
    createChannel(DEFAULT_THING_UID, absoluteVolumeChannelID, absoluteAcceptedItemType);
    String percentVolumeChannelID = FSInternetRadioBindingConstants.CHANNEL_VOLUME_PERCENT;
    String percentAcceptedItemType = acceptedItemTypes.get(percentVolumeChannelID);
    createChannel(DEFAULT_THING_UID, percentVolumeChannelID, percentAcceptedItemType);
    Thing radioThing = initializeRadioThing(DEFAULT_COMPLETE_CONFIGURATION);
    testRadioThingConsideringConfiguration(radioThing);
    turnTheRadioOn(radioThing);
    ChannelUID absoluteVolumeChannelUID = getChannelUID(radioThing, absoluteVolumeChannelID);
    initializeItem(absoluteVolumeChannelUID, DEFAULT_TEST_ITEM_NAME, absoluteAcceptedItemType);
    ChannelUID percentVolumeChannelUID = getChannelUID(radioThing, percentVolumeChannelID);
    /*
         * Giving the handler a valid percent value. According to the FrontierSiliconRadio's
         * documentation 100 percents correspond to 32 absolute value
         */
    radioHandler.handleCommand(percentVolumeChannelUID, PercentType.valueOf("50"));
    waitForAssert(() -> {
        assertTrue("We should be able to set the volume correctly using percentages.", radioServiceDummy.containsRequestParameter(16, VOLUME));
        radioServiceDummy.clearRequestParameters();
    });
    radioHandler.handleCommand(percentVolumeChannelUID, PercentType.valueOf("15"));
    waitForAssert(() -> {
        assertTrue("We should be able to set the volume correctly using percentages.", radioServiceDummy.containsRequestParameter(4, VOLUME));
        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)

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