use of org.eclipse.smarthome.core.thing.type.ThingType in project smarthome by eclipse.
the class PersistentInboxTest method configureConfigDescriptionRegistryMock.
private void configureConfigDescriptionRegistryMock(String paramName, Type type) throws URISyntaxException {
URI configDescriptionURI = new URI("thing-type:test:test");
ThingType thingType = ThingTypeBuilder.instance(THING_TYPE_UID, "Test").withConfigDescriptionURI(configDescriptionURI).build();
ConfigDescriptionParameter param = ConfigDescriptionParameterBuilder.create(paramName, type).build();
ConfigDescription configDesc = new ConfigDescription(configDescriptionURI, Collections.singletonList(param));
when(thingTypeRegistry.getThingType(THING_TYPE_UID)).thenReturn(thingType);
when(configDescriptionRegistry.getConfigDescription(eq(configDescriptionURI))).thenReturn(configDesc);
}
use of org.eclipse.smarthome.core.thing.type.ThingType in project smarthome by eclipse.
the class ThingManager method migrateThingType.
@Override
public void migrateThingType(final Thing thing, final ThingTypeUID thingTypeUID, final Configuration configuration) {
final ThingType thingType = thingTypeRegistry.getThingType(thingTypeUID);
if (thingType == null) {
throw new RuntimeException(MessageFormat.format("No thing type {0} registered, cannot change thing type for thing {1}", thingTypeUID.getAsString(), thing.getUID().getAsString()));
}
scheduler.schedule(new Runnable() {
@Override
public void run() {
Lock lock = getLockForThing(thing.getUID());
try {
lock.lock();
ThingUID thingUID = thing.getUID();
waitForRunningHandlerRegistrations(thingUID);
// Remove the ThingHandler, if any
final ThingHandlerFactory oldThingHandlerFactory = findThingHandlerFactory(thing.getThingTypeUID());
if (oldThingHandlerFactory != null) {
ThingHandler thingHandler = thing.getHandler();
unregisterAndDisposeHandler(oldThingHandlerFactory, thing, thingHandler);
waitUntilHandlerUnregistered(thing, 60 * 1000);
} else {
logger.debug("No ThingHandlerFactory available that can handle {}", thing.getThingTypeUID());
}
// Set the new channels
List<Channel> channels = ThingFactoryHelper.createChannels(thingType, thingUID, configDescriptionRegistry);
((ThingImpl) thing).setChannels(channels);
// Set the given configuration
ThingFactoryHelper.applyDefaultConfiguration(configuration, thingType, configDescriptionRegistry);
((ThingImpl) thing).setConfiguration(configuration);
// Change the ThingType
((ThingImpl) thing).setThingTypeUID(thingTypeUID);
// Register the new Handler - ThingManager.updateThing() is going to take care of that
thingRegistry.update(thing);
ThingHandler handler = thing.getHandler();
String handlerString = "NO HANDLER";
if (handler != null) {
handlerString = handler.toString();
}
logger.debug("Changed ThingType of Thing {} to {}. New ThingHandler is {}.", thing.getUID().toString(), thing.getThingTypeUID(), handlerString);
} finally {
lock.unlock();
}
}
private void waitUntilHandlerUnregistered(final Thing thing, int timeout) {
for (int i = 0; i < timeout / 100; i++) {
if (thing.getHandler() == null && thingHandlers.get(thing.getUID()) == null) {
return;
}
try {
Thread.sleep(100);
logger.debug("Waiting for handler deregistration to complete for thing {}. Took already {}ms.", thing.getUID().getAsString(), (i + 1) * 100);
} catch (InterruptedException e) {
return;
}
}
String message = MessageFormat.format("Thing type migration failed for {0}. The handler deregistration did not complete within {1}ms.", thing.getUID().getAsString(), timeout);
logger.error(message);
throw new RuntimeException(message);
}
private void waitForRunningHandlerRegistrations(ThingUID thingUID) {
for (int i = 0; i < 10 * 10; i++) {
if (!registerHandlerLock.contains(thingUID)) {
return;
}
try {
// Wait a little to give running handler registrations a chance to complete...
Thread.sleep(100);
} catch (InterruptedException e) {
return;
}
}
String message = MessageFormat.format("Thing type migration failed for {0}. Could not obtain lock for hander registration.", thingUID.getAsString());
logger.error(message);
throw new RuntimeException(message);
}
}, 0, TimeUnit.MILLISECONDS);
}
use of org.eclipse.smarthome.core.thing.type.ThingType in project smarthome by eclipse.
the class ThingTypeI18nTest method channelTypeShouldBeLocalized.
@Test
public void channelTypeShouldBeLocalized() throws Exception {
int initialNumberOfThingTypes = thingTypeProvider.getThingTypes(null).size();
// install test bundle
Bundle bundle = SyntheticBundleInstaller.install(bundleContext, TEST_BUNDLE_NAME);
assertThat(bundle, is(notNullValue()));
Collection<ThingType> thingTypes = thingTypeProvider.getThingTypes(Locale.GERMAN);
assertThat(thingTypes.size(), is(initialNumberOfThingTypes + 2));
ThingType weatherType = thingTypes.stream().filter(it -> it.toString().equals("yahooweather:weather")).findFirst().get();
assertThat(weatherType, is(notNullValue()));
assertThat(weatherType.getChannelDefinitions().size(), is(2));
ChannelType temperatureChannelType = channelTypeRegistry.getChannelType(weatherType.getChannelDefinitions().stream().filter(it -> it.getId().equals("temperature")).findFirst().get().getChannelTypeUID(), Locale.GERMAN);
assertThat(temperatureChannelType, is(notNullValue()));
assertThat(temperatureChannelType.getLabel(), is("Temperatur"));
assertThat(temperatureChannelType.getDescription(), is("Aktuelle Temperatur in Grad Celsius (Metrisch) oder Fahrenheit (Imperial)."));
assertThat(temperatureChannelType.getState().getPattern(), is("%d Grad Celsius"));
assertThat(temperatureChannelType.getState().getOptions().get(0).getLabel(), is("Mein String"));
}
use of org.eclipse.smarthome.core.thing.type.ThingType in project smarthome by eclipse.
the class ThingTypeI18nTest method thingTypeShouldBeLocalized.
@Test
public void thingTypeShouldBeLocalized() throws Exception {
int initialNumberOfThingTypes = thingTypeProvider.getThingTypes(null).size();
// install test bundle
Bundle bundle = SyntheticBundleInstaller.install(bundleContext, TEST_BUNDLE_NAME);
assertThat(bundle, is(notNullValue()));
Collection<ThingType> thingTypes = thingTypeProvider.getThingTypes(Locale.GERMAN);
assertThat(thingTypes.size(), is(initialNumberOfThingTypes + 2));
ThingType weatherType = thingTypes.stream().filter(it -> it.toString().equals("yahooweather:weather")).findFirst().get();
assertThat(weatherType, is(notNullValue()));
assertThat(weatherType.getLabel(), is("Wetterinformation"));
assertThat(weatherType.getDescription(), is("Stellt verschiedene Wetterdaten vom yahoo Wetterdienst bereit"));
}
use of org.eclipse.smarthome.core.thing.type.ThingType in project smarthome by eclipse.
the class ThingResource method normalizeConfiguration.
private Map<String, Object> normalizeConfiguration(Map<String, Object> properties, ThingTypeUID thingTypeUID, ThingUID thingUID) {
if (properties == null || properties.isEmpty()) {
return properties;
}
ThingType thingType = thingTypeRegistry.getThingType(thingTypeUID);
if (thingType == null) {
return properties;
}
List<ConfigDescription> configDescriptions = new ArrayList<>(2);
if (thingType.getConfigDescriptionURI() != null) {
ConfigDescription typeConfigDesc = configDescRegistry.getConfigDescription(thingType.getConfigDescriptionURI());
if (typeConfigDesc != null) {
configDescriptions.add(typeConfigDesc);
}
}
if (getConfigDescriptionURI(thingUID) != null) {
ConfigDescription thingConfigDesc = configDescRegistry.getConfigDescription(getConfigDescriptionURI(thingUID));
if (thingConfigDesc != null) {
configDescriptions.add(thingConfigDesc);
}
}
if (configDescriptions.isEmpty()) {
return properties;
}
return ConfigUtil.normalizeTypes(properties, configDescriptions);
}
Aggregations