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);
}
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();
}
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);
}
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;
}
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());
}
}
Aggregations