Search in sources :

Example 21 with ThingType

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);
}
Also used : ConfigDescription(org.eclipse.smarthome.config.core.ConfigDescription) ConfigDescriptionParameter(org.eclipse.smarthome.config.core.ConfigDescriptionParameter) URI(java.net.URI) ThingType(org.eclipse.smarthome.core.thing.type.ThingType)

Example 22 with ThingType

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);
}
Also used : ThingHandler(org.eclipse.smarthome.core.thing.binding.ThingHandler) ThingType(org.eclipse.smarthome.core.thing.type.ThingType) ThingHandlerFactory(org.eclipse.smarthome.core.thing.binding.ThingHandlerFactory) ReentrantLock(java.util.concurrent.locks.ReentrantLock) Lock(java.util.concurrent.locks.Lock) ThingUID(org.eclipse.smarthome.core.thing.ThingUID) List(java.util.List) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) Thing(org.eclipse.smarthome.core.thing.Thing)

Example 23 with ThingType

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"));
}
Also used : Bundle(org.osgi.framework.Bundle) ThingType(org.eclipse.smarthome.core.thing.type.ThingType) ChannelType(org.eclipse.smarthome.core.thing.type.ChannelType) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Example 24 with ThingType

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"));
}
Also used : Bundle(org.osgi.framework.Bundle) ThingType(org.eclipse.smarthome.core.thing.type.ThingType) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Example 25 with ThingType

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);
}
Also used : ArrayList(java.util.ArrayList) ConfigDescription(org.eclipse.smarthome.config.core.ConfigDescription) ThingType(org.eclipse.smarthome.core.thing.type.ThingType)

Aggregations

ThingType (org.eclipse.smarthome.core.thing.type.ThingType)26 JavaOSGiTest (org.eclipse.smarthome.test.java.JavaOSGiTest)15 Test (org.junit.Test)15 Bundle (org.osgi.framework.Bundle)15 ChannelDefinition (org.eclipse.smarthome.core.thing.type.ChannelDefinition)7 List (java.util.List)3 Nullable (org.eclipse.jdt.annotation.Nullable)3 Thing (org.eclipse.smarthome.core.thing.Thing)3 ThingUID (org.eclipse.smarthome.core.thing.ThingUID)3 ThingTypeProvider (org.eclipse.smarthome.core.thing.binding.ThingTypeProvider)3 Before (org.junit.Before)3 URI (java.net.URI)2 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 Lock (java.util.concurrent.locks.Lock)2 ReentrantLock (java.util.concurrent.locks.ReentrantLock)2 ConfigDescription (org.eclipse.smarthome.config.core.ConfigDescription)2 ConfigDescriptionParameter (org.eclipse.smarthome.config.core.ConfigDescriptionParameter)2 Configuration (org.eclipse.smarthome.config.core.Configuration)2 ThingHandler (org.eclipse.smarthome.core.thing.binding.ThingHandler)2