use of org.eclipse.smarthome.core.thing.binding.ThingHandler 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);
}
use of org.eclipse.smarthome.core.thing.binding.ThingHandler 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.binding.ThingHandler in project smarthome by eclipse.
the class ProfileCallbackImpl method handleCommand.
@Override
public void handleCommand(Command command) {
Thing thing = thingProvider.apply(link.getLinkedUID().getThingUID());
if (thing != null) {
final ThingHandler handler = thing.getHandler();
if (handler != null) {
if (ThingHandlerHelper.isHandlerInitialized(thing)) {
logger.debug("Delegating command '{}' for item '{}' to handler for channel '{}'", command, link.getItemName(), link.getLinkedUID());
safeCaller.create(handler, ThingHandler.class).withTimeout(CommunicationManager.THINGHANDLER_EVENT_TIMEOUT).onTimeout(() -> {
logger.warn("Handler for thing '{}' takes more than {}ms for handling a command", handler.getThing().getUID(), CommunicationManager.THINGHANDLER_EVENT_TIMEOUT);
}).build().handleCommand(link.getLinkedUID(), command);
} else {
logger.debug("Not delegating command '{}' for item '{}' to handler for channel '{}', " + "because handler is not initialized (thing must be in status UNKNOWN, ONLINE or OFFLINE but was {}).", command, link.getItemName(), link.getLinkedUID(), thing.getStatus());
}
} else {
logger.warn("Cannot delegate command '{}' for item '{}' to handler for channel '{}', " + "because no handler is assigned. Maybe the binding is not installed or not " + "propertly initialized.", command, link.getItemName(), link.getLinkedUID());
}
} else {
logger.warn("Cannot delegate command '{}' for item '{}' to handler for channel '{}', " + "because no thing with the UID '{}' could be found.", command, link.getItemName(), link.getLinkedUID(), link.getLinkedUID().getThingUID());
}
}
use of org.eclipse.smarthome.core.thing.binding.ThingHandler in project smarthome by eclipse.
the class ThingStatusInfoI18nLocalizationService method getLocalizedThingStatusInfo.
/**
* Localizes the {@link ThingStatusInfo} for the given thing.
*
* @param thing the thing whose thing status info is to be localized (must not be null)
* @param locale the locale to be used (can be null)
* @return the localized thing status or the original thing status if
* <ul>
* <li>there is nothing to be localized</li>
* <li>the thing does not have a handler</li>
* </ul>
* @throws IllegalArgumentException if given thing is null
*/
public ThingStatusInfo getLocalizedThingStatusInfo(Thing thing, Locale locale) {
if (thing == null) {
throw new IllegalArgumentException("Thing must not be null.");
}
ThingHandler thingHandler = thing.getHandler();
if (thingHandler == null) {
return thing.getStatusInfo();
}
String description = thing.getStatusInfo().getDescription();
if (!I18nUtil.isConstant(description)) {
return thing.getStatusInfo();
}
Bundle bundle = FrameworkUtil.getBundle(thingHandler.getClass());
Description desc = new Description(bundle, locale, description, i18nProvider);
String translatedDescription = i18nProvider.getText(bundle, desc.key, description, locale, desc.args);
return new ThingStatusInfo(thing.getStatus(), thing.getStatusInfo().getStatusDetail(), translatedDescription);
}
use of org.eclipse.smarthome.core.thing.binding.ThingHandler in project smarthome by eclipse.
the class HueLightHandler method getHueBridgeHandler.
@Nullable
private synchronized HueBridgeHandler getHueBridgeHandler() {
if (this.bridgeHandler == null) {
Bridge bridge = getBridge();
if (bridge == null) {
return null;
}
ThingHandler handler = bridge.getHandler();
if (handler instanceof HueBridgeHandler) {
this.bridgeHandler = (HueBridgeHandler) handler;
this.bridgeHandler.registerLightStatusListener(this);
} else {
return null;
}
}
return this.bridgeHandler;
}
Aggregations