Search in sources :

Example 1 with ChannelDTO

use of org.eclipse.smarthome.core.thing.dto.ChannelDTO in project smarthome by eclipse.

the class ThingResource method create.

/**
 * create a new Thing
 *
 * @param thingBean
 * @return Response holding the newly created Thing or error information
 */
@POST
@RolesAllowed({ Role.ADMIN })
@Consumes(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Creates a new thing and adds it to the registry.")
@ApiResponses(value = { @ApiResponse(code = 201, message = "Created", response = String.class), @ApiResponse(code = 400, message = "A uid must be provided, if no binding can create a thing of this type."), @ApiResponse(code = 409, message = "A thing with the same uid already exists.") })
public Response create(@HeaderParam(HttpHeaders.ACCEPT_LANGUAGE) @ApiParam(value = "language") String language, @ApiParam(value = "thing data", required = true) ThingDTO thingBean) {
    final Locale locale = LocaleUtil.getLocale(language);
    ThingUID thingUID = thingBean.UID == null ? null : new ThingUID(thingBean.UID);
    ThingTypeUID thingTypeUID = new ThingTypeUID(thingBean.thingTypeUID);
    if (thingUID != null) {
        // check if a thing with this UID already exists
        Thing thing = thingRegistry.get(thingUID);
        if (thing != null) {
            // report a conflict
            return getThingResponse(Status.CONFLICT, thing, locale, "Thing " + thingUID.toString() + " already exists!");
        }
    }
    ThingUID bridgeUID = null;
    if (thingBean.bridgeUID != null) {
        bridgeUID = new ThingUID(thingBean.bridgeUID);
    }
    // turn the ThingDTO's configuration into a Configuration
    Configuration configuration = new Configuration(normalizeConfiguration(thingBean.configuration, thingTypeUID, thingUID));
    normalizeChannels(thingBean, thingUID);
    Thing thing = thingRegistry.createThingOfType(thingTypeUID, thingUID, bridgeUID, thingBean.label, configuration);
    if (thing != null) {
        if (thingBean.properties != null) {
            for (Entry<String, String> entry : thingBean.properties.entrySet()) {
                thing.setProperty(entry.getKey(), entry.getValue());
            }
        }
        if (thingBean.channels != null) {
            List<Channel> channels = new ArrayList<>();
            for (ChannelDTO channelDTO : thingBean.channels) {
                channels.add(ChannelDTOMapper.map(channelDTO));
            }
            ThingHelper.addChannelsToThing(thing, channels);
        }
        if (thingBean.location != null) {
            thing.setLocation(thingBean.location);
        }
    } else if (thingUID != null) {
        // if there wasn't any ThingFactory capable of creating the thing,
        // we create the Thing exactly the way we received it, i.e. we
        // cannot take its thing type into account for automatically
        // populating channels and properties.
        thing = ThingDTOMapper.map(thingBean);
    } else {
        return getThingResponse(Status.BAD_REQUEST, thing, locale, "A UID must be provided, since no binding can create the thing!");
    }
    thingRegistry.add(thing);
    return getThingResponse(Status.CREATED, thing, locale, null);
}
Also used : Locale(java.util.Locale) Configuration(org.eclipse.smarthome.config.core.Configuration) ThingUID(org.eclipse.smarthome.core.thing.ThingUID) Channel(org.eclipse.smarthome.core.thing.Channel) ChannelDTO(org.eclipse.smarthome.core.thing.dto.ChannelDTO) ArrayList(java.util.ArrayList) ThingTypeUID(org.eclipse.smarthome.core.thing.ThingTypeUID) Thing(org.eclipse.smarthome.core.thing.Thing) RolesAllowed(javax.annotation.security.RolesAllowed) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 2 with ChannelDTO

use of org.eclipse.smarthome.core.thing.dto.ChannelDTO in project smarthome by eclipse.

the class EnrichedThingDTOMapper method map.

/**
 * Maps thing into enriched thing data transfer object.
 *
 * @param thing the thing
 * @param thingStatusInfo the thing status information to be used for the enriched object
 * @param firmwareStatus the firmwareStatus to be used for the enriched object
 * @param linkedItemsMap the map of linked items to be injected into the enriched object
 * @param editable true if this thing can be edited
 * @return the enriched thing DTO object
 */
public static EnrichedThingDTO map(Thing thing, ThingStatusInfo thingStatusInfo, FirmwareStatusDTO firmwareStatus, Map<String, Set<String>> linkedItemsMap, boolean editable) {
    ThingDTO thingDTO = ThingDTOMapper.map(thing);
    List<ChannelDTO> channels = new ArrayList<>();
    for (ChannelDTO channel : thingDTO.channels) {
        Set<String> linkedItems = linkedItemsMap != null ? linkedItemsMap.get(channel.id) : Collections.emptySet();
        channels.add(new EnrichedChannelDTO(channel, linkedItems));
    }
    return new EnrichedThingDTO(thingDTO, channels, thingStatusInfo, firmwareStatus, editable);
}
Also used : ThingDTO(org.eclipse.smarthome.core.thing.dto.ThingDTO) ChannelDTO(org.eclipse.smarthome.core.thing.dto.ChannelDTO) ArrayList(java.util.ArrayList)

Example 3 with ChannelDTO

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

Aggregations

ChannelDTO (org.eclipse.smarthome.core.thing.dto.ChannelDTO)3 ArrayList (java.util.ArrayList)2 Configuration (org.eclipse.smarthome.config.core.Configuration)2 Thing (org.eclipse.smarthome.core.thing.Thing)2 ThingUID (org.eclipse.smarthome.core.thing.ThingUID)2 ApiOperation (io.swagger.annotations.ApiOperation)1 ApiResponses (io.swagger.annotations.ApiResponses)1 Locale (java.util.Locale)1 RolesAllowed (javax.annotation.security.RolesAllowed)1 Consumes (javax.ws.rs.Consumes)1 POST (javax.ws.rs.POST)1 Bridge (org.eclipse.smarthome.core.thing.Bridge)1 Channel (org.eclipse.smarthome.core.thing.Channel)1 ThingTypeUID (org.eclipse.smarthome.core.thing.ThingTypeUID)1 ThingBuilder (org.eclipse.smarthome.core.thing.binding.builder.ThingBuilder)1 ThingDTO (org.eclipse.smarthome.core.thing.dto.ThingDTO)1 BridgeImpl (org.eclipse.smarthome.core.thing.internal.BridgeImpl)1