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);
}
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);
}
}
});
}
}
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();
}
}
}
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);
}
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());
}
}
Aggregations