Search in sources :

Example 6 with ThingHandlerFactory

use of org.eclipse.smarthome.core.thing.binding.ThingHandlerFactory in project smarthome by eclipse.

the class ThingManager method thingRemoved.

@Override
public void thingRemoved(final Thing thing, ThingTrackerEvent thingTrackerEvent) {
    logger.debug("Thing '{}' is no longer tracked by ThingManager.", thing.getUID());
    ThingHandler thingHandler = thingHandlers.get(thing.getUID());
    if (thingHandler != null) {
        final ThingHandlerFactory thingHandlerFactory = findThingHandlerFactory(thing.getThingTypeUID());
        if (thingHandlerFactory != null) {
            unregisterAndDisposeHandler(thingHandlerFactory, thing, thingHandler);
            if (thingTrackerEvent == ThingTrackerEvent.THING_REMOVED) {
                safeCaller.create(thingHandlerFactory, ThingHandlerFactory.class).build().removeThing(thing.getUID());
            }
        } else {
            logger.warn("Cannot unregister handler. No handler factory for thing '{}' found.", thing.getUID());
        }
    }
    this.things.remove(thing);
}
Also used : ThingHandler(org.eclipse.smarthome.core.thing.binding.ThingHandler) ThingHandlerFactory(org.eclipse.smarthome.core.thing.binding.ThingHandlerFactory)

Example 7 with ThingHandlerFactory

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

use of org.eclipse.smarthome.core.thing.binding.ThingHandlerFactory in project smarthome by eclipse.

the class ThingRegistryImpl method createThingOfType.

@Override
public Thing createThingOfType(ThingTypeUID thingTypeUID, ThingUID thingUID, ThingUID bridgeUID, String label, Configuration configuration) {
    logger.debug("Creating thing for type '{}'.", thingTypeUID);
    for (ThingHandlerFactory thingHandlerFactory : thingHandlerFactories) {
        if (thingHandlerFactory.supportsThingType(thingTypeUID)) {
            Thing thing = thingHandlerFactory.createThing(thingTypeUID, configuration, thingUID, bridgeUID);
            thing.setLabel(label);
            return thing;
        }
    }
    logger.warn("Cannot create thing. No binding found that supports creating a thing of type '{}'.", thingTypeUID);
    return null;
}
Also used : ThingHandlerFactory(org.eclipse.smarthome.core.thing.binding.ThingHandlerFactory) Thing(org.eclipse.smarthome.core.thing.Thing)

Aggregations

ThingHandlerFactory (org.eclipse.smarthome.core.thing.binding.ThingHandlerFactory)8 Thing (org.eclipse.smarthome.core.thing.Thing)5 ThingHandler (org.eclipse.smarthome.core.thing.binding.ThingHandler)4 ThingTypeUID (org.eclipse.smarthome.core.thing.ThingTypeUID)3 ThingUID (org.eclipse.smarthome.core.thing.ThingUID)3 BaseThingHandlerFactory (org.eclipse.smarthome.core.thing.binding.BaseThingHandlerFactory)3 ArrayList (java.util.ArrayList)2 Lock (java.util.concurrent.locks.Lock)2 ReentrantLock (java.util.concurrent.locks.ReentrantLock)2 NonNull (org.eclipse.jdt.annotation.NonNull)2 Nullable (org.eclipse.jdt.annotation.Nullable)2 Channel (org.eclipse.smarthome.core.thing.Channel)2 JavaOSGiTest (org.eclipse.smarthome.test.java.JavaOSGiTest)2 Collection (java.util.Collection)1 List (java.util.List)1 Locale (java.util.Locale)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Configuration (org.eclipse.smarthome.config.core.Configuration)1 Item (org.eclipse.smarthome.core.items.Item)1