Search in sources :

Example 16 with Thing

use of org.eclipse.smarthome.core.thing.Thing in project smarthome by eclipse.

the class ProfileCallbackImpl method handleUpdate.

@Override
public void handleUpdate(State state) {
    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 update '{}' for item '{}' to handler for channel '{}'", state, 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 an update", handler.getThing().getUID(), CommunicationManager.THINGHANDLER_EVENT_TIMEOUT);
                }).build().handleUpdate(link.getLinkedUID(), state);
            } else {
                logger.debug("Not delegating update '{}' for item '{}' to handler for channel '{}', " + "because handler is not initialized (thing must be in status UNKNOWN, ONLINE or OFFLINE but was {}).", state, link.getItemName(), link.getLinkedUID(), thing.getStatus());
            }
        } else {
            logger.warn("Cannot delegate update '{}' for item '{}' to handler for channel '{}', " + "because no handler is assigned. Maybe the binding is not installed or not " + "propertly initialized.", state, link.getItemName(), link.getLinkedUID());
        }
    } else {
        logger.warn("Cannot delegate update '{}' for item '{}' to handler for channel '{}', " + "because no thing with the UID '{}' could be found.", state, link.getItemName(), link.getLinkedUID(), link.getLinkedUID().getThingUID());
    }
}
Also used : ThingHandler(org.eclipse.smarthome.core.thing.binding.ThingHandler) Thing(org.eclipse.smarthome.core.thing.Thing)

Example 17 with Thing

use of org.eclipse.smarthome.core.thing.Thing in project smarthome by eclipse.

the class BaseBridgeHandler method getThingByUID.

/**
 * Finds and returns a child thing for a given UID of this bridge.
 *
 * @param uid uid of the child thing
 * @return child thing with the given uid or null if thing was not found
 */
@Nullable
public Thing getThingByUID(ThingUID uid) {
    Bridge bridge = getThing();
    List<Thing> things = bridge.getThings();
    for (Thing thing : things) {
        if (thing.getUID().equals(uid)) {
            return thing;
        }
    }
    return null;
}
Also used : Bridge(org.eclipse.smarthome.core.thing.Bridge) Thing(org.eclipse.smarthome.core.thing.Thing) Nullable(org.eclipse.jdt.annotation.Nullable)

Example 18 with Thing

use of org.eclipse.smarthome.core.thing.Thing in project smarthome by eclipse.

the class BaseThingHandlerFactory method createThing.

/**
 * Creates a thing based on given thing type uid.
 *
 * @param thingTypeUID thing type uid (must not be null)
 * @param thingUID thingUID (can be null)
 * @param configuration (must not be null)
 * @param bridgeUID (can be null)
 * @return thing (can be null, if thing type is unknown)
 */
@Override
@Nullable
public Thing createThing(ThingTypeUID thingTypeUID, Configuration configuration, @Nullable ThingUID thingUID, @Nullable ThingUID bridgeUID) {
    ThingUID effectiveUID = thingUID != null ? thingUID : ThingFactory.generateRandomThingUID(thingTypeUID);
    ThingType thingType = getThingTypeByUID(thingTypeUID);
    if (thingType != null) {
        Thing thing = ThingFactory.createThing(thingType, effectiveUID, configuration, bridgeUID, getConfigDescriptionRegistry());
        return thing;
    } else {
        return null;
    }
}
Also used : ThingUID(org.eclipse.smarthome.core.thing.ThingUID) ThingType(org.eclipse.smarthome.core.thing.type.ThingType) Thing(org.eclipse.smarthome.core.thing.Thing) Nullable(org.eclipse.jdt.annotation.Nullable)

Example 19 with Thing

use of org.eclipse.smarthome.core.thing.Thing in project smarthome by eclipse.

the class ThingDTOMapper method map.

/**
 * Maps thing DTO into thing
 *
 * @param thingDTO the thingDTO
 * @return the corresponding thing
 */
public static Thing map(ThingDTO thingDTO) {
    ThingUID thingUID = new ThingUID(thingDTO.UID);
    ThingTypeUID thingTypeUID = thingDTO.thingTypeUID == null ? new ThingTypeUID("") : new ThingTypeUID(thingDTO.thingTypeUID);
    Thing thing = ThingBuilder.create(thingTypeUID, thingUID).build();
    return ThingHelper.merge(thing, thingDTO);
}
Also used : ThingUID(org.eclipse.smarthome.core.thing.ThingUID) ThingTypeUID(org.eclipse.smarthome.core.thing.ThingTypeUID) Thing(org.eclipse.smarthome.core.thing.Thing)

Example 20 with Thing

use of org.eclipse.smarthome.core.thing.Thing in project smarthome by eclipse.

the class FirmwareUpdateServiceTest method testBackgroundTransfer.

@Test
public void testBackgroundTransfer() throws Exception {
    Map<String, String> props = new HashMap<>();
    props.put(Thing.PROPERTY_FIRMWARE_VERSION, V111);
    Thing thing4 = ThingBuilder.create(THING_TYPE_UID3, THING4_ID).withProperties(props).build();
    FirmwareUpdateBackgroundTransferHandler handler4 = mock(FirmwareUpdateBackgroundTransferHandler.class);
    when(handler4.getThing()).thenReturn(thing4);
    doAnswer(invocation -> {
        return updateExecutable.get();
    }).when(handler4).isUpdateExecutable();
    doAnswer(invocation -> {
        Firmware firmware = (Firmware) invocation.getArguments()[0];
        thing4.setProperty(Thing.PROPERTY_FIRMWARE_VERSION, firmware.getVersion());
        updateExecutable.set(false);
        return null;
    }).when(handler4).updateFirmware(any(Firmware.class), any(ProgressCallback.class));
    firmwareUpdateService.addFirmwareUpdateHandler(handler4);
    doAnswer(invocation -> {
        try {
            Thread.sleep(25);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        updateExecutable.set(true);
        return null;
    }).when(handler4).transferFirmware(any(Firmware.class));
    assertThat(firmwareUpdateService.getFirmwareStatusInfo(THING4_UID), is(unknownInfo));
    ArgumentCaptor<Event> eventCaptor = ArgumentCaptor.forClass(Event.class);
    verify(mockPublisher, times(1)).post(eventCaptor.capture());
    assertFirmwareStatusInfoEvent(THING4_UID, eventCaptor.getAllValues().get(eventCaptor.getAllValues().size() - 1), unknownInfo);
    FirmwareProvider firmwareProvider2 = mock(FirmwareProvider.class);
    when(firmwareProvider2.getFirmware(eq(FW120_EN.getUID()), any(Locale.class))).thenReturn(FW120_EN);
    when(firmwareProvider2.getFirmwares(any(ThingTypeUID.class), any(Locale.class))).thenAnswer(invocation -> {
        ThingTypeUID thingTypeUID = (ThingTypeUID) invocation.getArguments()[0];
        if (THING_TYPE_UID3.equals(thingTypeUID)) {
            return Collections.singleton(FW120_EN);
        } else {
            return Collections.emptySet();
        }
    });
    firmwareRegistry.addFirmwareProvider(firmwareProvider2);
    assertThat(firmwareUpdateService.getFirmwareStatusInfo(THING4_UID), is(updateAvailableInfo));
    verify(mockPublisher, times(2)).post(eventCaptor.capture());
    assertFirmwareStatusInfoEvent(THING4_UID, eventCaptor.getAllValues().get(eventCaptor.getAllValues().size() - 1), updateAvailableInfo);
    waitForAssert(() -> {
        assertThat(firmwareUpdateService.getFirmwareStatusInfo(THING4_UID), is(updateExecutableInfoFw120));
        verify(mockPublisher, times(3)).post(eventCaptor.capture());
        assertFirmwareStatusInfoEvent(THING4_UID, eventCaptor.getAllValues().get(eventCaptor.getAllValues().size() - 1), updateExecutableInfoFw120);
    });
    firmwareUpdateService.updateFirmware(THING4_UID, FW120_EN.getUID(), null);
    waitForAssert(() -> {
        assertThat(thing4.getProperties().get(Thing.PROPERTY_FIRMWARE_VERSION), is(V120));
    });
    assertThat(firmwareUpdateService.getFirmwareStatusInfo(THING4_UID), is(upToDateInfo));
    verify(mockPublisher, times(4)).post(eventCaptor.capture());
    assertFirmwareStatusInfoEvent(THING4_UID, eventCaptor.getAllValues().get(eventCaptor.getAllValues().size() - 1), upToDateInfo);
    assertThat(handler4.isUpdateExecutable(), is(false));
}
Also used : Locale(java.util.Locale) FirmwareUpdateBackgroundTransferHandler(org.eclipse.smarthome.core.thing.binding.firmware.FirmwareUpdateBackgroundTransferHandler) HashMap(java.util.HashMap) ProgressCallback(org.eclipse.smarthome.core.thing.binding.firmware.ProgressCallback) Event(org.eclipse.smarthome.core.events.Event) ThingTypeUID(org.eclipse.smarthome.core.thing.ThingTypeUID) Firmware(org.eclipse.smarthome.core.thing.binding.firmware.Firmware) Thing(org.eclipse.smarthome.core.thing.Thing) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Aggregations

Thing (org.eclipse.smarthome.core.thing.Thing)98 Test (org.junit.Test)43 ThingUID (org.eclipse.smarthome.core.thing.ThingUID)28 ChannelUID (org.eclipse.smarthome.core.thing.ChannelUID)24 Configuration (org.eclipse.smarthome.config.core.Configuration)19 JavaOSGiTest (org.eclipse.smarthome.test.java.JavaOSGiTest)19 JavaTest (org.eclipse.smarthome.test.java.JavaTest)18 ThingHandler (org.eclipse.smarthome.core.thing.binding.ThingHandler)14 ThingTypeUID (org.eclipse.smarthome.core.thing.ThingTypeUID)13 ApiOperation (io.swagger.annotations.ApiOperation)9 ApiResponses (io.swagger.annotations.ApiResponses)9 Item (org.eclipse.smarthome.core.items.Item)9 Path (javax.ws.rs.Path)8 Nullable (org.eclipse.jdt.annotation.Nullable)8 Locale (java.util.Locale)7 RolesAllowed (javax.annotation.security.RolesAllowed)7 Channel (org.eclipse.smarthome.core.thing.Channel)7 ThingHandlerCallback (org.eclipse.smarthome.core.thing.binding.ThingHandlerCallback)7 Command (org.eclipse.smarthome.core.types.Command)7 List (java.util.List)6