use of org.eclipse.smarthome.core.thing.binding.ThingHandler in project smarthome by eclipse.
the class NtpOSGiTest method initialize.
private void initialize(Configuration configuration, String channelID, String acceptedItemType, Configuration channelConfiguration) {
configuration.put(NtpBindingConstants.PROPERTY_NTP_SERVER_PORT, TEST_PORT);
ThingUID ntpUid = new ThingUID(NtpBindingConstants.THING_TYPE_NTP, TEST_THING_ID);
ChannelUID channelUID = new ChannelUID(ntpUid, channelID);
Channel channel = ChannelBuilder.create(channelUID, acceptedItemType).withType(channelTypeUID).withConfiguration(channelConfiguration).withLabel("label").withKind(ChannelKind.STATE).build();
ntpThing = ThingBuilder.create(NtpBindingConstants.THING_TYPE_NTP, ntpUid).withConfiguration(configuration).withChannel(channel).build();
managedThingProvider.add(ntpThing);
// Wait for the NTP thing to be added to the ManagedThingProvider.
ntpHandler = waitForAssert(() -> {
final ThingHandler thingHandler = ntpThing.getHandler();
assertThat(thingHandler, is(instanceOf(NtpHandler.class)));
return (NtpHandler) thingHandler;
}, DFL_TIMEOUT * 3, DFL_SLEEP_TIME);
if (acceptedItemType.equals(ACCEPTED_ITEM_TYPE_STRING)) {
testItem = new StringItem(TEST_ITEM_NAME);
} else if (acceptedItemType.equals(ACCEPTED_ITEM_TYPE_DATE_TIME)) {
testItem = new DateTimeItem(TEST_ITEM_NAME);
}
itemRegistry.add(testItem);
// Wait for the item , linked to the NTP thing to be added to the
// ManagedThingProvider.
final ManagedItemChannelLinkProvider itemChannelLinkProvider = waitForAssert(() -> {
final ManagedItemChannelLinkProvider tmp = getService(ManagedItemChannelLinkProvider.class);
assertNotNull(tmp);
return tmp;
});
itemChannelLinkProvider.add(new ItemChannelLink(TEST_ITEM_NAME, channelUID));
}
use of org.eclipse.smarthome.core.thing.binding.ThingHandler in project smarthome by eclipse.
the class TradfriGatewayHandler method thingUpdated.
@Override
public void thingUpdated(Thing thing) {
super.thingUpdated(thing);
logger.info("Bridge configuration updated. Updating paired things (if any).");
for (Thing t : getThing().getThings()) {
final ThingHandler thingHandler = t.getHandler();
if (thingHandler != null) {
thingHandler.thingUpdated(t);
}
}
}
use of org.eclipse.smarthome.core.thing.binding.ThingHandler 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.binding.ThingHandler in project smarthome by eclipse.
the class ThingManager method doRegisterHandler.
private void doRegisterHandler(final Thing thing, final ThingHandlerFactory thingHandlerFactory) {
logger.debug("Calling '{}.registerHandler()' for thing '{}'.", thingHandlerFactory.getClass().getSimpleName(), thing.getUID());
try {
ThingHandler thingHandler = thingHandlerFactory.registerHandler(thing);
thingHandler.setCallback(ThingManager.this.thingHandlerCallback);
thing.setHandler(thingHandler);
thingHandlers.put(thing.getUID(), thingHandler);
thingHandlersByFactory.put(thingHandlerFactory, thingHandler);
} catch (Exception ex) {
ThingStatusInfo statusInfo = buildStatusInfo(ThingStatus.UNINITIALIZED, ThingStatusDetail.HANDLER_REGISTERING_ERROR, ex.getCause() != null ? ex.getCause().getMessage() : ex.getMessage());
setThingStatus(thing, statusInfo);
logger.error("Exception occurred while calling thing handler factory '{}': {}", thingHandlerFactory, ex.getMessage(), ex);
}
}
use of org.eclipse.smarthome.core.thing.binding.ThingHandler 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();
}
}
}
Aggregations