Search in sources :

Example 21 with ThingStatusInfo

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

Example 22 with ThingStatusInfo

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

Example 23 with ThingStatusInfo

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

Example 24 with ThingStatusInfo

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();
}
Also used : ThingUID(org.eclipse.smarthome.core.thing.ThingUID) ThingStatusInfo(org.eclipse.smarthome.core.thing.ThingStatusInfo) Thing(org.eclipse.smarthome.core.thing.Thing) Path(javax.ws.rs.Path) RolesAllowed(javax.annotation.security.RolesAllowed) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 25 with ThingStatusInfo

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());
    });
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AsyncResultWrapper(org.eclipse.smarthome.test.AsyncResultWrapper) ThingStatusInfo(org.eclipse.smarthome.core.thing.ThingStatusInfo) DiscoveryResult(org.eclipse.smarthome.config.discovery.DiscoveryResult) AbstractHueOSGiTest(org.eclipse.smarthome.binding.hue.test.AbstractHueOSGiTest) Test(org.junit.Test)

Aggregations

ThingStatusInfo (org.eclipse.smarthome.core.thing.ThingStatusInfo)26 Test (org.junit.Test)8 DiscoveryResult (org.eclipse.smarthome.config.discovery.DiscoveryResult)5 ThingUID (org.eclipse.smarthome.core.thing.ThingUID)4 Thing (org.eclipse.smarthome.core.thing.Thing)3 ThingStatusInfoBuilder (org.eclipse.smarthome.core.thing.binding.builder.ThingStatusInfoBuilder)3 ThingHandler (org.eclipse.smarthome.core.thing.binding.ThingHandler)2 ApiOperation (io.swagger.annotations.ApiOperation)1 ApiResponses (io.swagger.annotations.ApiResponses)1 Duration (java.time.Duration)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 RolesAllowed (javax.annotation.security.RolesAllowed)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1 Circuit (org.eclipse.smarthome.binding.digitalstrom.internal.lib.structure.devices.Circuit)1 Device (org.eclipse.smarthome.binding.digitalstrom.internal.lib.structure.devices.Device)1 AbstractHueOSGiTest (org.eclipse.smarthome.binding.hue.test.AbstractHueOSGiTest)1 Bridge (org.eclipse.smarthome.core.thing.Bridge)1 ChannelTypeUID (org.eclipse.smarthome.core.thing.type.ChannelTypeUID)1 EnrichedThingDTO (org.eclipse.smarthome.io.rest.core.thing.EnrichedThingDTO)1