use of org.openhab.core.thing.type.BridgeType in project openhab-core by openhab.
the class ThingTypeResource method convertToThingTypeDTO.
private ThingTypeDTO convertToThingTypeDTO(ThingType thingType, Locale locale) {
final ConfigDescription configDescription;
final URI descURI = thingType.getConfigDescriptionURI();
configDescription = descURI == null ? null : configDescriptionRegistry.getConfigDescription(descURI, locale);
List<ConfigDescriptionParameterDTO> parameters;
List<ConfigDescriptionParameterGroupDTO> parameterGroups;
if (configDescription != null) {
ConfigDescriptionDTO configDescriptionDTO = ConfigDescriptionDTOMapper.map(configDescription);
parameters = configDescriptionDTO.parameters;
parameterGroups = configDescriptionDTO.parameterGroups;
} else {
parameters = new ArrayList<>(0);
parameterGroups = new ArrayList<>(0);
}
final List<ChannelDefinitionDTO> channelDefinitions = convertToChannelDefinitionDTOs(thingType.getChannelDefinitions(), locale);
return new ThingTypeDTO(thingType.getUID().toString(), thingType.getLabel(), thingType.getDescription(), thingType.getCategory(), thingType.isListed(), parameters, channelDefinitions, convertToChannelGroupDefinitionDTOs(thingType.getChannelGroupDefinitions(), locale), thingType.getSupportedBridgeTypeUIDs(), thingType.getProperties(), thingType instanceof BridgeType, parameterGroups, thingType.getExtensibleChannelTypeIds());
}
use of org.openhab.core.thing.type.BridgeType in project openhab-core by openhab.
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)
@Operation(operationId = "createThingInRegistry", summary = "Creates a new thing and adds it to the registry.", security = { @SecurityRequirement(name = "oauth2", scopes = { "admin" }) }, responses = { @ApiResponse(responseCode = "201", description = "Created", content = @Content(schema = @Schema(implementation = EnrichedThingDTO.class))), @ApiResponse(responseCode = "400", description = "Thing uid does not match bridge uid."), @ApiResponse(responseCode = "400", description = "A uid must be provided, if no binding can create a thing of this type."), @ApiResponse(responseCode = "409", description = "A thing with the same uid already exists.") })
public Response create(@HeaderParam(HttpHeaders.ACCEPT_LANGUAGE) @Parameter(description = "language") @Nullable String language, @Parameter(description = "thing data", required = true) ThingDTO thingBean) {
final Locale locale = localeService.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);
if (thingUID != null && (!thingUID.getBindingId().equals(bridgeUID.getBindingId()) || !thingUID.getBridgeIds().contains(bridgeUID.getId()))) {
return Response.status(Status.BAD_REQUEST).entity("Thing UID '" + thingUID + "' does not match bridge UID '" + bridgeUID + "'").build();
}
}
// turn the ThingDTO's configuration into a Configuration
Configuration configuration = new Configuration(normalizeConfiguration(thingBean.configuration, thingTypeUID, thingUID));
if (thingUID != null) {
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, thingTypeRegistry.getThingType(thingTypeUID) instanceof BridgeType);
} 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.openhab.core.thing.type.BridgeType in project openhab-core by openhab.
the class ThingFactoryTest method createSimpleBridge.
@Test
public void createSimpleBridge() {
BridgeType thingType = ThingTypeBuilder.instance("bindingId", "thingTypeId", "label").buildBridge();
Configuration configuration = new Configuration();
Thing thing = ThingFactory.createThing(thingType, new ThingUID(thingType.getUID(), "thingId"), configuration);
assertThat(thing, is(instanceOf(Bridge.class)));
assertThat(thing.getProperties(), is(not(nullValue())));
}
use of org.openhab.core.thing.type.BridgeType in project openhab-core by openhab.
the class ThingTypeI18nLocalizationService method createLocalizedThingType.
public ThingType createLocalizedThingType(Bundle bundle, ThingType thingType, @Nullable Locale locale) {
ThingTypeUID thingTypeUID = thingType.getUID();
String label = thingTypeI18nUtil.getLabel(bundle, thingTypeUID, thingType.getLabel(), locale);
String description = thingTypeI18nUtil.getDescription(bundle, thingTypeUID, thingType.getDescription(), locale);
List<ChannelDefinition> localizedChannelDefinitions = channelI18nUtil.createLocalizedChannelDefinitions(bundle, thingType.getChannelDefinitions(), channelDefinition -> thingTypeI18nUtil.getChannelLabel(bundle, thingTypeUID, channelDefinition, channelDefinition.getLabel(), locale), channelDefinition -> thingTypeI18nUtil.getChannelDescription(bundle, thingTypeUID, channelDefinition, channelDefinition.getDescription(), locale), locale);
List<ChannelGroupDefinition> localizedChannelGroupDefinitions = channelGroupI18nUtil.createLocalizedChannelGroupDefinitions(bundle, thingType.getChannelGroupDefinitions(), channelGroupDefinition -> thingTypeI18nUtil.getChannelGroupLabel(bundle, thingTypeUID, channelGroupDefinition, channelGroupDefinition.getLabel(), locale), channelGroupDefinition -> thingTypeI18nUtil.getChannelGroupDescription(bundle, thingTypeUID, channelGroupDefinition, channelGroupDefinition.getDescription(), locale), locale);
ThingTypeBuilder builder = ThingTypeBuilder.instance(thingType);
if (label != null) {
builder.withLabel(label);
}
if (description != null) {
builder.withDescription(description);
}
builder.withChannelDefinitions(localizedChannelDefinitions).withChannelGroupDefinitions(localizedChannelGroupDefinitions);
if (thingType instanceof BridgeType) {
return builder.buildBridge();
}
return builder.build();
}
use of org.openhab.core.thing.type.BridgeType in project openhab-core by openhab.
the class ThingTypesTest method thingTypesShouldLoad.
@Test
public void thingTypesShouldLoad() throws Exception {
try (final AutoCloseable unused = loadedTestBundle()) {
Collection<ThingType> thingTypes = thingTypeProvider.getThingTypes(null);
// HUE Bridge
BridgeType bridgeType = (BridgeType) thingTypes.stream().filter(it -> "hue:bridge".equals(it.toString())).findFirst().get();
assertThat(bridgeType, is(notNullValue()));
assertThat(bridgeType.getCategory(), is("NetworkAppliance"));
assertThat(bridgeType.isListed(), is(false));
assertThat(bridgeType.getLabel(), is("HUE Bridge"));
assertThat(bridgeType.getDescription(), is("The hue Bridge represents the Philips hue bridge."));
assertThat(bridgeType.getProperties().size(), is(1));
assertThat(bridgeType.getProperties().get("vendor"), is("Philips"));
assertThat(bridgeType.getRepresentationProperty(), is("serialNumber"));
// HUE Lamp
ThingType thingType = thingTypes.stream().filter(it -> "hue:lamp".equals(it.toString())).findFirst().get();
assertThat(thingType, is(notNullValue()));
assertThat(thingType.getCategory(), is("Lightbulb"));
assertThat(thingType.isListed(), is(false));
assertThat(thingType.getLabel(), is("HUE Lamp"));
assertThat(thingType.getDescription(), is("My own great HUE Lamp."));
assertThat(thingType.getSupportedBridgeTypeUIDs().size(), is(1));
assertThat(thingType.getSupportedBridgeTypeUIDs().get(0), is("hue:bridge"));
assertThat(thingType.getExtensibleChannelTypeIds(), containsInAnyOrder("alarm", "brightness"));
assertThat(thingType.getProperties().size(), is(2));
assertThat(thingType.getProperties().get("key1"), is("value1"));
assertThat(thingType.getProperties().get("key2"), is("value2"));
assertThat(thingType.getRepresentationProperty(), is("uniqueId"));
List<ChannelDefinition> channelDefinitions = thingType.getChannelDefinitions();
assertThat(channelDefinitions.size(), is(3));
ChannelDefinition colorChannel = channelDefinitions.stream().filter(it -> "color".equals(it.getId())).findFirst().get();
assertThat(colorChannel, is(notNullValue()));
assertThat(colorChannel.getProperties().size(), is(2));
assertThat(colorChannel.getProperties().get("chan.key1"), is("value1"));
assertThat(colorChannel.getProperties().get("chan.key2"), is("value2"));
ChannelType colorChannelType = channelTypeRegistry.getChannelType(colorChannel.getChannelTypeUID());
assertThat(colorChannelType, is(notNullValue()));
assertThat(colorChannelType.toString(), is("hue:color"));
assertThat(colorChannelType.getItemType(), is("ColorItem"));
assertThat(colorChannelType.getLabel(), is("HUE Lamp Color"));
assertThat(colorChannelType.getDescription(), is("The color channel allows to control the color of the hue lamp. It is also possible to dim values and switch the lamp on and off."));
Set<String> tags = colorChannelType.getTags();
assertThat(tags, is(notNullValue()));
assertThat(tags.contains("Hue"), is(true));
assertThat(tags.contains("ColorLamp"), is(true));
assertThat(tags.contains("AmbientLamp"), is(false));
assertThat(tags.contains("AlarmSystem"), is(false));
ChannelDefinition colorTemperatureChannel = channelDefinitions.stream().filter(it -> "color_temperature".equals(it.getId())).findFirst().get();
assertThat(colorTemperatureChannel, is(notNullValue()));
assertThat(colorTemperatureChannel.getProperties().size(), is(0));
ChannelType colorTemperatureChannelType = channelTypeRegistry.getChannelType(colorTemperatureChannel.getChannelTypeUID());
assertThat(colorTemperatureChannelType, is(notNullValue()));
assertThat(colorTemperatureChannelType.toString(), is("hue:color_temperature"));
assertThat(colorTemperatureChannelType.getItemType(), is("DimmerItem"));
assertThat(colorTemperatureChannelType.getLabel(), is("HUE Lamp Color Temperature"));
assertThat(colorTemperatureChannelType.getDescription(), is("The color temperature channel allows to set the color temperature from 0 (cold) to 100 (warm)."));
tags = colorTemperatureChannelType.getTags();
assertThat(tags, is(notNullValue()));
assertThat(tags.contains("Hue"), is(true));
assertThat(tags.contains("AmbientLamp"), is(true));
assertThat(tags.contains("ColorLamp"), is(false));
assertThat(tags.contains("AlarmSystem"), is(false));
ChannelDefinition alarmChannel = channelDefinitions.stream().filter(it -> "alarm".equals(it.getId())).findFirst().get();
assertThat(alarmChannel, is(notNullValue()));
ChannelType alarmChannelType = channelTypeRegistry.getChannelType(alarmChannel.getChannelTypeUID());
assertThat(alarmChannelType, is(notNullValue()));
assertThat(alarmChannelType.toString(), is("hue:alarm"));
assertThat(alarmChannelType.getItemType(), is(CoreItemFactory.NUMBER));
assertThat(alarmChannelType.getLabel(), is("Alarm System"));
assertThat(alarmChannelType.getDescription(), is("The light blinks if alarm is set."));
tags = alarmChannelType.getTags();
assertThat(tags, is(notNullValue()));
assertThat(tags.contains("Hue"), is(true));
assertThat(tags.contains("AlarmSystem"), is(true));
assertThat(tags.contains("AmbientLamp"), is(false));
assertThat(tags.contains("ColorLamp"), is(false));
assertThat(alarmChannelType.getCategory(), is(equalTo("ALARM")));
StateDescription state = alarmChannelType.getState();
assertThat(state.getMinimum(), is(BigDecimal.ZERO));
assertThat(state.getMaximum(), is(BigDecimal.valueOf(100.0)));
assertThat(state.getStep(), is(BigDecimal.valueOf(10.0)));
assertThat(state.getPattern(), is(equalTo("%d Peek")));
assertThat(state.isReadOnly(), is(true));
assertThat(state.getOptions().size(), is(2));
assertThat(state.getOptions().get(0).getValue(), is(equalTo("SOUND")));
assertThat(state.getOptions().get(0).getLabel(), is(equalTo("My great sound.")));
// HUE Lamp with group
thingType = thingTypes.stream().filter(it -> "hue:lamp-with-group".equals(it.toString())).findFirst().get();
assertThat(thingType, is(notNullValue()));
assertThat(thingType.getProperties().size(), is(0));
assertThat(thingType.getCategory(), is(nullValue()));
assertThat(thingType.isListed(), is(true));
assertThat(thingType.getExtensibleChannelTypeIds(), containsInAnyOrder("brightness", "alarm"));
List<ChannelGroupDefinition> channelGroupDefinitions = thingType.getChannelGroupDefinitions();
assertThat(channelGroupDefinitions.size(), is(2));
// Channel Group
ChannelGroupDefinition channelGroupDefinition = channelGroupDefinitions.stream().filter(it -> "lampgroup".equals(it.getId())).findFirst().get();
assertThat(channelGroupDefinition, is(notNullValue()));
ChannelGroupType channelGroupType = channelGroupTypeRegistry.getChannelGroupType(channelGroupDefinition.getTypeUID());
assertThat(channelGroupType, is(notNullValue()));
channelDefinitions = channelGroupType.getChannelDefinitions();
assertThat(channelDefinitions.size(), is(3));
// Channel Group without channels
channelGroupDefinition = channelGroupDefinitions.stream().filter(it -> "lampgroup-without-channels".equals(it.getId())).findFirst().get();
assertThat(channelGroupDefinition, is(notNullValue()));
channelGroupType = channelGroupTypeRegistry.getChannelGroupType(channelGroupDefinition.getTypeUID());
assertThat(channelGroupType, is(notNullValue()));
channelDefinitions = channelGroupType.getChannelDefinitions();
assertThat(channelDefinitions.size(), is(0));
// HUE Lamp without channels
thingType = thingTypes.stream().filter(it -> "hue:lamp-without-channels".equals(it.toString())).findFirst().get();
assertThat(thingType, is(notNullValue()));
channelDefinitions = thingType.getChannelDefinitions();
assertThat(channelDefinitions.size(), is(0));
}
}
Aggregations