Search in sources :

Example 1 with ApiBridgeConfiguration

use of org.openhab.binding.homeconnect.internal.configuration.ApiBridgeConfiguration in project openhab-addons by openhab.

the class HomeConnectBridgeHandler method initialize.

@Override
public void initialize() {
    // let the bridge configuration servlet know about this handler
    homeConnectServlet.addBridgeHandler(this);
    // create oAuth service
    ApiBridgeConfiguration config = getConfiguration();
    String tokenUrl = (config.isSimulator() ? API_SIMULATOR_BASE_URL : API_BASE_URL) + OAUTH_TOKEN_PATH;
    String authorizeUrl = (config.isSimulator() ? API_SIMULATOR_BASE_URL : API_BASE_URL) + OAUTH_AUTHORIZE_PATH;
    String oAuthServiceHandleId = thing.getUID().getAsString() + (config.isSimulator() ? "simulator" : "");
    oAuthClientService = oAuthFactory.createOAuthClientService(oAuthServiceHandleId, tokenUrl, authorizeUrl, config.getClientId(), config.getClientSecret(), OAUTH_SCOPE, true);
    this.oAuthServiceHandleId = oAuthServiceHandleId;
    logger.debug("Initialize oAuth client service. tokenUrl={}, authorizeUrl={}, oAuthServiceHandleId={}, scope={}, oAuthClientService={}", tokenUrl, authorizeUrl, oAuthServiceHandleId, OAUTH_SCOPE, oAuthClientService);
    // create api client
    apiClient = new HomeConnectApiClient(httpClient, oAuthClientService, config.isSimulator(), apiRequestHistory, config);
    eventSourceClient = new HomeConnectEventSourceClient(clientBuilder, eventSourceFactory, oAuthClientService, config.isSimulator(), scheduler, eventHistory);
    updateStatus(ThingStatus.UNKNOWN);
    scheduler.submit(() -> {
        try {
            @Nullable AccessTokenResponse accessTokenResponse = oAuthClientService.getAccessTokenResponse();
            if (accessTokenResponse == null) {
                updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_PENDING, "Please authenticate your account at http(s)://[YOUROPENHAB]:[YOURPORT]/homeconnect (e.g. http://192.168.178.100:8080/homeconnect).");
            } else {
                apiClient.getHomeAppliances();
                updateStatus(ThingStatus.ONLINE);
            }
        } catch (OAuthException | IOException | OAuthResponseException | CommunicationException | AuthorizationException e) {
            ZonedDateTime nextReinitializeDateTime = ZonedDateTime.now().plusSeconds(REINITIALIZATION_DELAY_SEC);
            String offlineMessage = String.format("Home Connect service is not reachable or a problem occurred! Retrying at %s (%s). bridge=%s", nextReinitializeDateTime.format(DateTimeFormatter.RFC_1123_DATE_TIME), e.getMessage(), getThing().getLabel());
            updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, offlineMessage);
            scheduleReinitialize();
        }
    });
}
Also used : OAuthResponseException(org.openhab.core.auth.client.oauth2.OAuthResponseException) CommunicationException(org.openhab.binding.homeconnect.internal.client.exception.CommunicationException) AuthorizationException(org.openhab.binding.homeconnect.internal.client.exception.AuthorizationException) OAuthException(org.openhab.core.auth.client.oauth2.OAuthException) IOException(java.io.IOException) HomeConnectApiClient(org.openhab.binding.homeconnect.internal.client.HomeConnectApiClient) HomeConnectEventSourceClient(org.openhab.binding.homeconnect.internal.client.HomeConnectEventSourceClient) ApiBridgeConfiguration(org.openhab.binding.homeconnect.internal.configuration.ApiBridgeConfiguration) ZonedDateTime(java.time.ZonedDateTime) AccessTokenResponse(org.openhab.core.auth.client.oauth2.AccessTokenResponse) Nullable(org.eclipse.jdt.annotation.Nullable)

Aggregations

IOException (java.io.IOException)1 ZonedDateTime (java.time.ZonedDateTime)1 Nullable (org.eclipse.jdt.annotation.Nullable)1 HomeConnectApiClient (org.openhab.binding.homeconnect.internal.client.HomeConnectApiClient)1 HomeConnectEventSourceClient (org.openhab.binding.homeconnect.internal.client.HomeConnectEventSourceClient)1 AuthorizationException (org.openhab.binding.homeconnect.internal.client.exception.AuthorizationException)1 CommunicationException (org.openhab.binding.homeconnect.internal.client.exception.CommunicationException)1 ApiBridgeConfiguration (org.openhab.binding.homeconnect.internal.configuration.ApiBridgeConfiguration)1 AccessTokenResponse (org.openhab.core.auth.client.oauth2.AccessTokenResponse)1 OAuthException (org.openhab.core.auth.client.oauth2.OAuthException)1 OAuthResponseException (org.openhab.core.auth.client.oauth2.OAuthResponseException)1