use of org.eclipse.smarthome.core.thing.ThingStatusInfo 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.ThingStatusInfo in project smarthome by eclipse.
the class ThingStatusInfoI18nLocalizationService method getLocalizedThingStatusInfo.
/**
* Localizes the {@link ThingStatusInfo} for the given thing.
*
* @param thing the thing whose thing status info is to be localized (must not be null)
* @param locale the locale to be used (can be null)
* @return the localized thing status or the original thing status if
* <ul>
* <li>there is nothing to be localized</li>
* <li>the thing does not have a handler</li>
* </ul>
* @throws IllegalArgumentException if given thing is null
*/
public ThingStatusInfo getLocalizedThingStatusInfo(Thing thing, Locale locale) {
if (thing == null) {
throw new IllegalArgumentException("Thing must not be null.");
}
ThingHandler thingHandler = thing.getHandler();
if (thingHandler == null) {
return thing.getStatusInfo();
}
String description = thing.getStatusInfo().getDescription();
if (!I18nUtil.isConstant(description)) {
return thing.getStatusInfo();
}
Bundle bundle = FrameworkUtil.getBundle(thingHandler.getClass());
Description desc = new Description(bundle, locale, description, i18nProvider);
String translatedDescription = i18nProvider.getText(bundle, desc.key, description, locale, desc.args);
return new ThingStatusInfo(thing.getStatus(), thing.getStatusInfo().getStatusDetail(), translatedDescription);
}
use of org.eclipse.smarthome.core.thing.ThingStatusInfo in project smarthome by eclipse.
the class BaseThingHandler method updateStatus.
/**
* Updates the status of the thing.
*
* @param status the status
* @param statusDetail the detail of the status
* @param description the description of the status
* @throws IllegalStateException if handler is not initialized correctly, because no callback is present
*/
protected void updateStatus(ThingStatus status, ThingStatusDetail statusDetail, @Nullable String description) {
synchronized (this) {
if (this.callback != null) {
ThingStatusInfoBuilder statusBuilder = ThingStatusInfoBuilder.create(status, statusDetail);
ThingStatusInfo statusInfo = statusBuilder.withDescription(description).build();
this.callback.statusUpdated(this.thing, statusInfo);
} else {
throw new IllegalStateException("Could not update status, because callback is missing");
}
}
}
use of org.eclipse.smarthome.core.thing.ThingStatusInfo in project smarthome by eclipse.
the class ThingResource method getStatus.
@GET
@RolesAllowed({ Role.USER, Role.ADMIN })
@Path("/{thingUID}/status")
@ApiOperation(value = "Gets thing's status.")
@ApiResponses(value = { @ApiResponse(code = 200, message = "OK", response = String.class), @ApiResponse(code = 404, message = "Thing not found.") })
public Response getStatus(@HeaderParam(HttpHeaders.ACCEPT_LANGUAGE) String language, @PathParam("thingUID") @ApiParam(value = "thing") String thingUID) throws IOException {
ThingUID thingUIDObject = new ThingUID(thingUID);
// Check if the Thing exists, 404 if not
Thing thing = thingRegistry.get(thingUIDObject);
if (null == thing) {
logger.info("Received HTTP GET request for thing config status at '{}' for the unknown thing '{}'.", uriInfo.getPath(), thingUID);
return getThingNotFoundResponse(thingUID);
}
ThingStatusInfo thingStatusInfo = thingStatusInfoI18nLocalizationService.getLocalizedThingStatusInfo(thing, LocaleUtil.getLocale(language));
return Response.ok(null, MediaType.TEXT_PLAIN).entity(thingStatusInfo).build();
}
use of org.eclipse.smarthome.core.thing.ThingStatusInfo in project smarthome by eclipse.
the class HueLightDiscoveryServiceOSGiTest method startSearchIsCalled.
@Test
public void startSearchIsCalled() {
final AtomicBoolean searchHasBeenTriggered = new AtomicBoolean(false);
AsyncResultWrapper<String> addressWrapper = new AsyncResultWrapper<String>();
AsyncResultWrapper<String> bodyWrapper = new AsyncResultWrapper<String>();
MockedHttpClient mockedHttpClient = new MockedHttpClient() {
@Override
public Result put(String address, String body) throws IOException {
addressWrapper.set(address);
bodyWrapper.set(body);
return new Result("", 200);
}
@Override
public Result get(String address) throws IOException {
if (address.endsWith("testUserName")) {
String body = "{\"lights\":{}}";
return new Result(body, 200);
} else {
return new Result("", 404);
}
}
@Override
public Result post(String address, String body) throws IOException {
if (address.endsWith("lights")) {
String bodyReturn = "{\"success\": {\"/lights\": \"Searching for new devices\"}}";
searchHasBeenTriggered.set(true);
return new Result(bodyReturn, 200);
} else {
return new Result("", 404);
}
}
};
installHttpClientMock(hueBridgeHandler, mockedHttpClient);
ThingStatusInfo online = ThingStatusInfoBuilder.create(ThingStatus.ONLINE, ThingStatusDetail.NONE).build();
waitForAssert(() -> {
assertThat(hueBridge.getStatusInfo(), is(online));
});
discoveryService.startScan();
waitForAssert(() -> {
assertTrue(searchHasBeenTriggered.get());
});
}
Aggregations