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