Search in sources :

Example 6 with ThingBuilder

use of org.eclipse.smarthome.core.thing.binding.builder.ThingBuilder 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 7 with ThingBuilder

use of org.eclipse.smarthome.core.thing.binding.builder.ThingBuilder in project smarthome by eclipse.

the class ThingBuilderTest method testWithoutChannel_missing.

@Test
public void testWithoutChannel_missing() {
    ThingBuilder thingBuilder = ThingBuilder.create(THING_TYPE_UID, THING_UID);
    // 
    thingBuilder.withChannels(// 
    ChannelBuilder.create(new ChannelUID(THING_UID, "channel1"), "").build(), ChannelBuilder.create(new ChannelUID(THING_UID, "channel2"), "").build());
    thingBuilder.withoutChannel(new ChannelUID(THING_UID, "channel3"));
    assertThat(thingBuilder.build().getChannels().size(), is(equalTo(2)));
}
Also used : ThingBuilder(org.eclipse.smarthome.core.thing.binding.builder.ThingBuilder) ChannelUID(org.eclipse.smarthome.core.thing.ChannelUID) Test(org.junit.Test)

Example 8 with ThingBuilder

use of org.eclipse.smarthome.core.thing.binding.builder.ThingBuilder in project smarthome by eclipse.

the class ThingBuilderTest method testWithChannel_duplicates.

@Test(expected = IllegalArgumentException.class)
public void testWithChannel_duplicates() {
    ThingBuilder thingBuilder = ThingBuilder.create(THING_TYPE_UID, THING_UID);
    thingBuilder.withChannel(ChannelBuilder.create(new ChannelUID(THING_UID, "channel1"), "").build());
    thingBuilder.withChannel(ChannelBuilder.create(new ChannelUID(THING_UID, "channel1"), "").build());
}
Also used : ThingBuilder(org.eclipse.smarthome.core.thing.binding.builder.ThingBuilder) ChannelUID(org.eclipse.smarthome.core.thing.ChannelUID) Test(org.junit.Test)

Example 9 with ThingBuilder

use of org.eclipse.smarthome.core.thing.binding.builder.ThingBuilder in project smarthome by eclipse.

the class ThingBuilderTest method testWithoutChannel.

@Test
public void testWithoutChannel() {
    ThingBuilder thingBuilder = ThingBuilder.create(THING_TYPE_UID, THING_UID);
    // 
    thingBuilder.withChannels(// 
    ChannelBuilder.create(new ChannelUID(THING_UID, "channel1"), "").build(), ChannelBuilder.create(new ChannelUID(THING_UID, "channel2"), "").build());
    thingBuilder.withoutChannel(new ChannelUID(THING_UID, "channel1"));
    assertThat(thingBuilder.build().getChannels().size(), is(equalTo(1)));
    assertThat(thingBuilder.build().getChannels().get(0).getUID().getId(), is(equalTo("channel2")));
}
Also used : ThingBuilder(org.eclipse.smarthome.core.thing.binding.builder.ThingBuilder) ChannelUID(org.eclipse.smarthome.core.thing.ChannelUID) Test(org.junit.Test)

Example 10 with ThingBuilder

use of org.eclipse.smarthome.core.thing.binding.builder.ThingBuilder in project smarthome by eclipse.

the class DeviceHandler method loadOutputChannel.

private void loadOutputChannel(ChannelTypeUID channelTypeUID, String acceptedItemType) {
    if (channelTypeUID == null || acceptedItemType == null) {
        return;
    }
    currentChannel = channelTypeUID.getId();
    List<Channel> channelList = new LinkedList<Channel>(this.getThing().getChannels());
    boolean channelIsAlreadyLoaded = false;
    boolean channelListChanged = false;
    if (!channelList.isEmpty()) {
        Iterator<Channel> channelInter = channelList.iterator();
        while (channelInter.hasNext()) {
            Channel eshChannel = channelInter.next();
            if (DsChannelTypeProvider.isOutputChannel(eshChannel.getUID().getId())) {
                if (!eshChannel.getUID().getId().equals(currentChannel) && !(device.isShade() && eshChannel.getUID().getId().equals(DsChannelTypeProvider.SHADE))) {
                    channelInter.remove();
                    channelListChanged = true;
                } else {
                    if (!eshChannel.getUID().getId().equals(DsChannelTypeProvider.SHADE)) {
                        channelIsAlreadyLoaded = true;
                    }
                }
            }
        }
    }
    if (!channelIsAlreadyLoaded && currentChannel != null) {
        Channel channel = ChannelBuilder.create(new ChannelUID(this.getThing().getUID(), channelTypeUID.getId()), acceptedItemType).withType(channelTypeUID).build();
        channelList.add(channel);
        channelListChanged = true;
    }
    if (channelListChanged) {
        ThingBuilder thingBuilder = editThing();
        thingBuilder.withChannels(channelList);
        updateThing(thingBuilder.build());
        logger.debug("load channel: {} with item: {}", channelTypeUID.getAsString(), acceptedItemType);
    }
}
Also used : ThingBuilder(org.eclipse.smarthome.core.thing.binding.builder.ThingBuilder) ChannelUID(org.eclipse.smarthome.core.thing.ChannelUID) Channel(org.eclipse.smarthome.core.thing.Channel) LinkedList(java.util.LinkedList)

Aggregations

ThingBuilder (org.eclipse.smarthome.core.thing.binding.builder.ThingBuilder)11 ChannelUID (org.eclipse.smarthome.core.thing.ChannelUID)9 Test (org.junit.Test)6 Channel (org.eclipse.smarthome.core.thing.Channel)4 LinkedList (java.util.LinkedList)2 ThingUID (org.eclipse.smarthome.core.thing.ThingUID)2 ArrayList (java.util.ArrayList)1 DeviceBinarayInputEnum (org.eclipse.smarthome.binding.digitalstrom.internal.lib.structure.devices.deviceparameters.constants.DeviceBinarayInputEnum)1 SensorEnum (org.eclipse.smarthome.binding.digitalstrom.internal.lib.structure.devices.deviceparameters.constants.SensorEnum)1 DeviceBinaryInput (org.eclipse.smarthome.binding.digitalstrom.internal.lib.structure.devices.deviceparameters.impl.DeviceBinaryInput)1 Configuration (org.eclipse.smarthome.config.core.Configuration)1 Bridge (org.eclipse.smarthome.core.thing.Bridge)1 Thing (org.eclipse.smarthome.core.thing.Thing)1 ChannelDTO (org.eclipse.smarthome.core.thing.dto.ChannelDTO)1 BridgeImpl (org.eclipse.smarthome.core.thing.internal.BridgeImpl)1 ChannelTypeUID (org.eclipse.smarthome.core.thing.type.ChannelTypeUID)1