Search in sources :

Example 71 with Thing

use of org.eclipse.smarthome.core.thing.Thing in project smarthome by eclipse.

the class ThingManager method removeThingHandlerFactory.

protected void removeThingHandlerFactory(ThingHandlerFactory thingHandlerFactory) {
    logger.debug("Thing handler factory '{}' removed", thingHandlerFactory.getClass().getSimpleName());
    Set<ThingHandler> handlers = new HashSet<>(thingHandlersByFactory.get(thingHandlerFactory));
    for (ThingHandler thingHandler : handlers) {
        Thing thing = thingHandler.getThing();
        if (thing != null && isHandlerRegistered(thing)) {
            unregisterAndDisposeHandler(thingHandlerFactory, thing, thingHandler);
        }
    }
    thingHandlersByFactory.removeAll(thingHandlerFactory);
    thingHandlerFactories.remove(thingHandlerFactory);
}
Also used : ThingHandler(org.eclipse.smarthome.core.thing.binding.ThingHandler) Thing(org.eclipse.smarthome.core.thing.Thing) HashSet(java.util.HashSet)

Example 72 with Thing

use of org.eclipse.smarthome.core.thing.Thing in project smarthome by eclipse.

the class ThingManager method registerChildHandlers.

private void registerChildHandlers(final Bridge bridge) {
    for (final Thing child : bridge.getThings()) {
        logger.debug("Register and initialize child '{}' of bridge '{}'.", child.getUID(), bridge.getUID());
        ThreadPoolManager.getPool(THING_MANAGER_THREADPOOL_NAME).execute(new Runnable() {

            @Override
            public void run() {
                try {
                    registerAndInitializeHandler(child, getThingHandlerFactory(child));
                } catch (Exception ex) {
                    logger.error("Registration resp. initialization of child '{}' of bridge '{}' has been failed: {}", child.getUID(), bridge.getUID(), ex.getMessage(), ex);
                }
            }
        });
    }
}
Also used : Thing(org.eclipse.smarthome.core.thing.Thing)

Example 73 with Thing

use of org.eclipse.smarthome.core.thing.Thing in project smarthome by eclipse.

the class ThingManager method thingUpdated.

@Override
public void thingUpdated(final Thing thing, ThingTrackerEvent thingTrackerEvent) {
    ThingUID thingUID = thing.getUID();
    if (thingUpdatedLock.contains(thingUID)) {
        // called from the thing handler itself, therefore
        // it exists, is initializing/initialized and
        // must not be informed (in order to prevent infinite loops)
        replaceThing(getThing(thingUID), thing);
    } else {
        Lock lock1 = getLockForThing(thing.getUID());
        try {
            lock1.lock();
            Thing oldThing = getThing(thingUID);
            ThingHandler thingHandler = replaceThing(oldThing, thing);
            if (thingHandler != null) {
                if (ThingHandlerHelper.isHandlerInitialized(thing) || isInitializing(thing)) {
                    if (oldThing != null) {
                        oldThing.setHandler(null);
                    }
                    thing.setHandler(thingHandler);
                    safeCaller.create(thingHandler, ThingHandler.class).build().thingUpdated(thing);
                } else {
                    logger.debug("Cannot notify handler about updated thing '{}', because handler is not initialized (thing must be in status UNKNOWN, ONLINE or OFFLINE).", thing.getThingTypeUID());
                    if (thingHandler.getThing() == thing) {
                        logger.debug("Initializing handler of thing '{}'", thing.getThingTypeUID());
                        if (oldThing != null) {
                            oldThing.setHandler(null);
                        }
                        thing.setHandler(thingHandler);
                        initializeHandler(thing);
                    } else {
                        logger.debug("Replacing uninitialized handler for updated thing '{}'", thing.getThingTypeUID());
                        ThingHandlerFactory thingHandlerFactory = getThingHandlerFactory(thing);
                        unregisterHandler(thingHandler.getThing(), thingHandlerFactory);
                        registerAndInitializeHandler(thing, thingHandlerFactory);
                    }
                }
            } else {
                registerAndInitializeHandler(thing, getThingHandlerFactory(thing));
            }
        } finally {
            lock1.unlock();
        }
    }
}
Also used : ThingUID(org.eclipse.smarthome.core.thing.ThingUID) ThingHandler(org.eclipse.smarthome.core.thing.binding.ThingHandler) ThingHandlerFactory(org.eclipse.smarthome.core.thing.binding.ThingHandlerFactory) Thing(org.eclipse.smarthome.core.thing.Thing) ReentrantLock(java.util.concurrent.locks.ReentrantLock) Lock(java.util.concurrent.locks.Lock)

Example 74 with Thing

use of org.eclipse.smarthome.core.thing.Thing 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 75 with Thing

use of org.eclipse.smarthome.core.thing.Thing in project smarthome by eclipse.

the class ThingRegistryImpl method preserveDynamicState.

private void preserveDynamicState(Thing thing) {
    final Thing existingThing = get(thing.getUID());
    if (existingThing != null) {
        thing.setHandler(existingThing.getHandler());
        thing.setStatusInfo(existingThing.getStatusInfo());
    }
}
Also used : Thing(org.eclipse.smarthome.core.thing.Thing)

Aggregations

Thing (org.eclipse.smarthome.core.thing.Thing)98 Test (org.junit.Test)43 ThingUID (org.eclipse.smarthome.core.thing.ThingUID)28 ChannelUID (org.eclipse.smarthome.core.thing.ChannelUID)24 Configuration (org.eclipse.smarthome.config.core.Configuration)19 JavaOSGiTest (org.eclipse.smarthome.test.java.JavaOSGiTest)19 JavaTest (org.eclipse.smarthome.test.java.JavaTest)18 ThingHandler (org.eclipse.smarthome.core.thing.binding.ThingHandler)14 ThingTypeUID (org.eclipse.smarthome.core.thing.ThingTypeUID)13 ApiOperation (io.swagger.annotations.ApiOperation)9 ApiResponses (io.swagger.annotations.ApiResponses)9 Item (org.eclipse.smarthome.core.items.Item)9 Path (javax.ws.rs.Path)8 Nullable (org.eclipse.jdt.annotation.Nullable)8 Locale (java.util.Locale)7 RolesAllowed (javax.annotation.security.RolesAllowed)7 Channel (org.eclipse.smarthome.core.thing.Channel)7 ThingHandlerCallback (org.eclipse.smarthome.core.thing.binding.ThingHandlerCallback)7 Command (org.eclipse.smarthome.core.types.Command)7 List (java.util.List)6