Java Code Examples for org.eclipse.smarthome.core.thing.Thing#setHandler()

The following examples show how to use org.eclipse.smarthome.core.thing.Thing#setHandler() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CommunicationManagerTest.java    From smarthome with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testItemCommandEvent_typeDowncast() {
    Thing thing = ThingBuilder.create(THING_TYPE_UID, THING_UID)
            .withChannels(ChannelBuilder.create(STATE_CHANNEL_UID_2, "Dimmer").withKind(ChannelKind.STATE).build())
            .build();
    thing.setHandler(mockHandler);
    when(thingRegistry.get(eq(THING_UID))).thenReturn(thing);

    manager.receive(ItemEventFactory.createCommandEvent(ITEM_NAME_2, HSBType.fromRGB(128, 128, 128)));
    waitForAssert(() -> {
        ArgumentCaptor<Command> commandCaptor = ArgumentCaptor.forClass(Command.class);
        verify(stateProfile).onCommandFromItem(commandCaptor.capture());
        Command command = commandCaptor.getValue();
        assertNotNull(command);
        assertEquals(PercentType.class, command.getClass());
    });
    verifyNoMoreInteractions(stateProfile);
    verifyNoMoreInteractions(triggerProfile);
}
 
Example 2
Source File: CommunicationManagerTest.java    From smarthome with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testItemStateEvent_typeDowncast() {
    Thing thing = ThingBuilder.create(THING_TYPE_UID, THING_UID)
            .withChannels(ChannelBuilder.create(STATE_CHANNEL_UID_2, "Dimmer").withKind(ChannelKind.STATE).build())
            .build();
    thing.setHandler(mockHandler);
    when(thingRegistry.get(eq(THING_UID))).thenReturn(thing);

    manager.receive(ItemEventFactory.createStateEvent(ITEM_NAME_2, HSBType.fromRGB(128, 128, 128)));
    waitForAssert(() -> {
        ArgumentCaptor<State> stateCaptor = ArgumentCaptor.forClass(State.class);
        verify(stateProfile).onStateUpdateFromItem(stateCaptor.capture());
        State state = stateCaptor.getValue();
        assertNotNull(state);
        assertEquals(PercentType.class, state.getClass());
    });
    verifyNoMoreInteractions(stateProfile);
    verifyNoMoreInteractions(triggerProfile);
}
 
Example 3
Source File: ThingManagerImpl.java    From smarthome with Eclipse Public License 2.0 6 votes vote down vote up
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(ThingManagerImpl.this.thingHandlerCallback);
        thing.setHandler(thingHandler);
        thingHandlers.put(thing.getUID(), thingHandler);
        synchronized (thingHandlersByFactory) {
            thingHandlersByFactory.computeIfAbsent(thingHandlerFactory, unused -> new HashSet<>())
                    .add(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);
    }
}
 
Example 4
Source File: ThingRegistryImpl.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
private void preserveDynamicState(Thing thing) {
    final Thing existingThing = get(thing.getUID());
    if (existingThing != null) {
        thing.setHandler(existingThing.getHandler());
        thing.setStatusInfo(existingThing.getStatusInfo());
    }
}
 
Example 5
Source File: ThingManagerImpl.java    From smarthome with Eclipse Public License 2.0 4 votes vote down vote up
@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();
        }
    }
}