Search in sources :

Example 6 with ThingTypeUID

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

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

the class ThingTypeResource method getFirmwares.

@GET
@Path("/{thingTypeUID}/firmwares")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Get all available firmwares for provided thingType", response = StrippedThingTypeDTO.class, responseContainer = "Set")
@ApiResponses(value = { @ApiResponse(code = 200, message = "OK"), @ApiResponse(code = 204, message = "No firmwares found.") })
public Response getFirmwares(@PathParam("thingTypeUID") @ApiParam(value = "thingTypeUID") String thingTypeUID, @HeaderParam(HttpHeaders.ACCEPT_LANGUAGE) @ApiParam(value = HttpHeaders.ACCEPT_LANGUAGE) String language) {
    ThingTypeUID athingTypeUID = new ThingTypeUID(thingTypeUID);
    Collection<Firmware> firmwares = firmwareRegistry.getFirmwares(athingTypeUID, LocaleUtil.getLocale(language));
    if (firmwares.isEmpty()) {
        return Response.status(Status.NO_CONTENT).build();
    }
    Stream<FirmwareDTO> firmwareStream = firmwares.stream().map(this::convertToFirmwareDTO);
    return Response.ok(null, MediaType.TEXT_PLAIN).entity(new Stream2JSONInputStream(firmwareStream)).build();
}
Also used : FirmwareDTO(org.eclipse.smarthome.core.thing.firmware.dto.FirmwareDTO) ThingTypeUID(org.eclipse.smarthome.core.thing.ThingTypeUID) Firmware(org.eclipse.smarthome.core.thing.binding.firmware.Firmware) Stream2JSONInputStream(org.eclipse.smarthome.io.rest.Stream2JSONInputStream) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 8 with ThingTypeUID

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

the class EnrichedThingDTOMapperTest method setup.

@Before
public void setup() {
    initMocks(this);
    when(thing.getThingTypeUID()).thenReturn(new ThingTypeUID(THING_TYPE_UID));
    when(thing.getUID()).thenReturn(new ThingUID(UID));
    when(thing.getLabel()).thenReturn(THING_LABEL);
    when(thing.getChannels()).thenReturn(mockChannels());
    when(thing.getConfiguration()).thenReturn(configuration);
    when(thing.getProperties()).thenReturn(properties);
    when(thing.getLocation()).thenReturn(LOCATION);
}
Also used : ThingUID(org.eclipse.smarthome.core.thing.ThingUID) ThingTypeUID(org.eclipse.smarthome.core.thing.ThingTypeUID) Before(org.junit.Before)

Example 9 with ThingTypeUID

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

the class ZonePlayerDiscoveryParticipant method getThingUID.

@Override
public ThingUID getThingUID(RemoteDevice device) {
    if (device.getDetails().getManufacturerDetails().getManufacturer() != null) {
        if (device.getDetails().getManufacturerDetails().getManufacturer().toUpperCase().contains("SONOS")) {
            String modelName = getModelName(device);
            if (modelName.equals("ZP80")) {
                modelName = "CONNECT";
            } else if (modelName.equals("ZP100")) {
                modelName = "CONNECTAMP";
            }
            ThingTypeUID thingUID = new ThingTypeUID(SonosBindingConstants.BINDING_ID, modelName);
            // In case a new "unknown" Sonos player is discovered a generic ThingTypeUID will be used
            if (!SonosBindingConstants.SUPPORTED_KNOWN_THING_TYPES_UIDS.contains(thingUID)) {
                thingUID = SonosBindingConstants.ZONEPLAYER_THING_TYPE_UID;
            }
            logger.debug("Discovered a Sonos '{}' thing with UDN '{}'", thingUID, device.getIdentity().getUdn().getIdentifierString());
            return new ThingUID(thingUID, device.getIdentity().getUdn().getIdentifierString());
        }
    }
    return null;
}
Also used : ThingUID(org.eclipse.smarthome.core.thing.ThingUID) ThingTypeUID(org.eclipse.smarthome.core.thing.ThingTypeUID)

Example 10 with ThingTypeUID

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

the class TradfriDiscoveryService method onUpdate.

@Override
public void onUpdate(String instanceId, JsonObject data) {
    ThingUID bridge = handler.getThing().getUID();
    try {
        if (data.has(INSTANCE_ID)) {
            int id = data.get(INSTANCE_ID).getAsInt();
            String type = data.get(TYPE).getAsString();
            JsonObject deviceInfo = data.get(DEVICE).getAsJsonObject();
            String model = deviceInfo.get(DEVICE_MODEL).getAsString();
            ThingUID thingId = null;
            if (TYPE_LIGHT.equals(type) && data.has(LIGHT)) {
                JsonObject state = data.get(LIGHT).getAsJsonArray().get(0).getAsJsonObject();
                // Color temperature light:
                // We do not always receive a COLOR attribute, even the light supports it - but the gateway does not
                // seem to have this information, if the bulb is unreachable. We therefore also check against
                // concrete model names.
                // Color light:
                // As the protocol does not distinguishes between color and full-color lights,
                // we check if the "CWS" identifier is given in the model name
                ThingTypeUID thingType = null;
                if (model != null && model.contains(COLOR_MODELS_IDENTIFIER)) {
                    thingType = THING_TYPE_COLOR_LIGHT;
                }
                if (// 
                thingType == null && (state.has(COLOR) || (model != null && Arrays.asList(COLOR_TEMP_MODELS).contains(model)))) {
                    thingType = THING_TYPE_COLOR_TEMP_LIGHT;
                }
                if (thingType == null) {
                    thingType = THING_TYPE_DIMMABLE_LIGHT;
                }
                thingId = new ThingUID(thingType, bridge, Integer.toString(id));
            } else if (TYPE_SWITCH.equals(type) && data.has(SWITCH)) {
                // Remote control and wireless dimmer
                // As protocol does not distinguishes between remote control and wireless dimmer,
                // we check for the whole model name
                ThingTypeUID thingType = (model != null && REMOTE_CONTROLLER_MODEL.equals(model)) ? THING_TYPE_REMOTE_CONTROL : THING_TYPE_DIMMER;
                thingId = new ThingUID(thingType, bridge, Integer.toString(id));
            } else if (TYPE_SENSOR.equals(type) && data.has(SENSOR)) {
                // Motion sensor
                thingId = new ThingUID(THING_TYPE_MOTION_SENSOR, bridge, Integer.toString(id));
            }
            if (thingId == null) {
                // we didn't identify any device, so let's quit
                return;
            }
            String label = data.get(NAME).getAsString();
            Map<String, Object> properties = new HashMap<>(1);
            properties.put("id", id);
            properties.put(PROPERTY_MODEL_ID, model);
            if (deviceInfo.get(DEVICE_VENDOR) != null) {
                properties.put(PROPERTY_VENDOR, deviceInfo.get(DEVICE_VENDOR).getAsString());
            }
            if (deviceInfo.get(DEVICE_FIRMWARE) != null) {
                properties.put(PROPERTY_FIRMWARE_VERSION, deviceInfo.get(DEVICE_FIRMWARE).getAsString());
            }
            logger.debug("Adding device {} to inbox", thingId);
            DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingId).withBridge(bridge).withLabel(label).withProperties(properties).withRepresentationProperty("id").build();
            thingDiscovered(discoveryResult);
        }
    } catch (JsonSyntaxException e) {
        logger.debug("JSON error during discovery: {}", e.getMessage());
    }
}
Also used : DiscoveryResult(org.eclipse.smarthome.config.discovery.DiscoveryResult) JsonSyntaxException(com.google.gson.JsonSyntaxException) HashMap(java.util.HashMap) ThingUID(org.eclipse.smarthome.core.thing.ThingUID) JsonObject(com.google.gson.JsonObject) ThingTypeUID(org.eclipse.smarthome.core.thing.ThingTypeUID) JsonObject(com.google.gson.JsonObject)

Aggregations

ThingTypeUID (org.eclipse.smarthome.core.thing.ThingTypeUID)63 ThingUID (org.eclipse.smarthome.core.thing.ThingUID)27 Test (org.junit.Test)27 JavaOSGiTest (org.eclipse.smarthome.test.java.JavaOSGiTest)25 Thing (org.eclipse.smarthome.core.thing.Thing)12 DiscoveryResult (org.eclipse.smarthome.config.discovery.DiscoveryResult)11 Locale (java.util.Locale)10 HashMap (java.util.HashMap)9 Collection (java.util.Collection)5 DiscoveryService (org.eclipse.smarthome.config.discovery.DiscoveryService)5 Before (org.junit.Before)5 Configuration (org.eclipse.smarthome.config.core.Configuration)4 DiscoveryListener (org.eclipse.smarthome.config.discovery.DiscoveryListener)4 ApiOperation (io.swagger.annotations.ApiOperation)3 ApiResponses (io.swagger.annotations.ApiResponses)3 Nullable (org.eclipse.jdt.annotation.Nullable)3 ThingRegistry (org.eclipse.smarthome.core.thing.ThingRegistry)3 BaseThingHandlerFactory (org.eclipse.smarthome.core.thing.binding.BaseThingHandlerFactory)3 Firmware (org.eclipse.smarthome.core.thing.binding.firmware.Firmware)3 ArrayList (java.util.ArrayList)2