Search in sources :

Example 21 with ThingHandler

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));
}
Also used : ManagedItemChannelLinkProvider(org.eclipse.smarthome.core.thing.link.ManagedItemChannelLinkProvider) ChannelUID(org.eclipse.smarthome.core.thing.ChannelUID) ThingUID(org.eclipse.smarthome.core.thing.ThingUID) Channel(org.eclipse.smarthome.core.thing.Channel) ItemChannelLink(org.eclipse.smarthome.core.thing.link.ItemChannelLink) ThingHandler(org.eclipse.smarthome.core.thing.binding.ThingHandler) NtpHandler(org.eclipse.smarthome.binding.ntp.handler.NtpHandler) DateTimeItem(org.eclipse.smarthome.core.library.items.DateTimeItem) StringItem(org.eclipse.smarthome.core.library.items.StringItem)

Example 22 with ThingHandler

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);
        }
    }
}
Also used : ThingHandler(org.eclipse.smarthome.core.thing.binding.ThingHandler) Thing(org.eclipse.smarthome.core.thing.Thing)

Example 23 with ThingHandler

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);
}
Also used : ThingHandler(org.eclipse.smarthome.core.thing.binding.ThingHandler) Thing(org.eclipse.smarthome.core.thing.Thing) HashSet(java.util.HashSet)

Example 24 with ThingHandler

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);
    }
}
Also used : ThingHandler(org.eclipse.smarthome.core.thing.binding.ThingHandler) ThingStatusInfo(org.eclipse.smarthome.core.thing.ThingStatusInfo)

Example 25 with ThingHandler

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

Aggregations

ThingHandler (org.eclipse.smarthome.core.thing.binding.ThingHandler)30 Thing (org.eclipse.smarthome.core.thing.Thing)14 BaseThingHandler (org.eclipse.smarthome.core.thing.binding.BaseThingHandler)12 Bridge (org.eclipse.smarthome.core.thing.Bridge)9 ChannelUID (org.eclipse.smarthome.core.thing.ChannelUID)6 JavaOSGiTest (org.eclipse.smarthome.test.java.JavaOSGiTest)6 Test (org.junit.Test)6 ThingUID (org.eclipse.smarthome.core.thing.ThingUID)4 ThingHandlerFactory (org.eclipse.smarthome.core.thing.binding.ThingHandlerFactory)4 Command (org.eclipse.smarthome.core.types.Command)4 Semaphore (java.util.concurrent.Semaphore)3 Lock (java.util.concurrent.locks.Lock)3 ReentrantLock (java.util.concurrent.locks.ReentrantLock)3 Channel (org.eclipse.smarthome.core.thing.Channel)3 BaseBridgeHandler (org.eclipse.smarthome.core.thing.binding.BaseBridgeHandler)3 List (java.util.List)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 NonNull (org.eclipse.jdt.annotation.NonNull)2 ThingStatusInfo (org.eclipse.smarthome.core.thing.ThingStatusInfo)2 MethodHandles (java.lang.invoke.MethodHandles)1