2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.boschindego.internal.handler;
15 import static org.openhab.binding.boschindego.internal.BoschIndegoBindingConstants.*;
17 import java.io.IOException;
18 import java.util.Collection;
19 import java.util.List;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jetty.client.HttpClient;
23 import org.openhab.binding.boschindego.internal.IndegoController;
24 import org.openhab.binding.boschindego.internal.discovery.IndegoDiscoveryService;
25 import org.openhab.binding.boschindego.internal.exceptions.IndegoAuthenticationException;
26 import org.openhab.binding.boschindego.internal.exceptions.IndegoException;
27 import org.openhab.core.auth.client.oauth2.AccessTokenResponse;
28 import org.openhab.core.auth.client.oauth2.OAuthClientService;
29 import org.openhab.core.auth.client.oauth2.OAuthException;
30 import org.openhab.core.auth.client.oauth2.OAuthFactory;
31 import org.openhab.core.auth.client.oauth2.OAuthResponseException;
32 import org.openhab.core.thing.Bridge;
33 import org.openhab.core.thing.ChannelUID;
34 import org.openhab.core.thing.ThingStatus;
35 import org.openhab.core.thing.ThingStatusDetail;
36 import org.openhab.core.thing.binding.BaseBridgeHandler;
37 import org.openhab.core.thing.binding.ThingHandlerService;
38 import org.openhab.core.types.Command;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
43 * The {@link BoschAccountHandler} is responsible for handling commands, which are
44 * sent to one of the channels.
46 * @author Jacob Laursen - Initial contribution
49 public class BoschAccountHandler extends BaseBridgeHandler {
51 private final Logger logger = LoggerFactory.getLogger(BoschAccountHandler.class);
52 private final OAuthFactory oAuthFactory;
54 private OAuthClientService oAuthClientService;
55 private IndegoController controller;
57 public BoschAccountHandler(Bridge bridge, HttpClient httpClient, OAuthFactory oAuthFactory) {
60 this.oAuthFactory = oAuthFactory;
62 oAuthClientService = oAuthFactory.createOAuthClientService(getThing().getUID().getAsString(), BSK_TOKEN_URI,
63 BSK_AUTH_URI, BSK_CLIENT_ID, null, BSK_SCOPE, false);
64 controller = new IndegoController(httpClient, oAuthClientService);
68 public void initialize() {
69 updateStatus(ThingStatus.UNKNOWN);
71 scheduler.execute(() -> {
73 AccessTokenResponse accessTokenResponse = this.oAuthClientService.getAccessTokenResponse();
74 if (accessTokenResponse == null) {
75 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.CONFIGURATION_ERROR,
76 "@text/offline.conf-error.oauth2-unauthorized");
78 updateStatus(ThingStatus.ONLINE);
80 } catch (OAuthException | OAuthResponseException e) {
81 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.CONFIGURATION_ERROR,
82 "@text/offline.conf-error.oauth2-unauthorized");
83 } catch (IOException e) {
84 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR,
85 "@text/offline.comm-error.oauth2-authorization-failed");
91 public void dispose() {
92 oAuthFactory.ungetOAuthService(this.getThing().getUID().getAsString());
96 public void handleCommand(ChannelUID channelUID, Command command) {
100 public Collection<Class<? extends ThingHandlerService>> getServices() {
101 return List.of(IndegoDiscoveryService.class);
104 public void authorize(String authCode) throws IndegoAuthenticationException {
105 logger.info("Attempting to authorize using authorization code");
108 oAuthClientService.getAccessTokenResponseByAuthorizationCode(authCode, BSK_REDIRECT_URI);
109 } catch (OAuthException | OAuthResponseException | IOException e) {
110 throw new IndegoAuthenticationException("Failed to authorize by authorization code " + authCode, e);
113 logger.info("Authorization completed successfully");
115 updateStatus(ThingStatus.ONLINE);
118 public OAuthClientService getOAuthClientService() {
119 return oAuthClientService;
122 public Collection<String> getSerialNumbers() throws IndegoException {
123 return controller.getSerialNumbers();