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.ArrayList;
19 import java.util.Collection;
20 import java.util.List;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jetty.client.HttpClient;
24 import org.openhab.binding.boschindego.internal.IndegoController;
25 import org.openhab.binding.boschindego.internal.discovery.IndegoDiscoveryService;
26 import org.openhab.binding.boschindego.internal.dto.response.DevicePropertiesResponse;
27 import org.openhab.binding.boschindego.internal.exceptions.IndegoAuthenticationException;
28 import org.openhab.binding.boschindego.internal.exceptions.IndegoException;
29 import org.openhab.core.auth.client.oauth2.AccessTokenResponse;
30 import org.openhab.core.auth.client.oauth2.OAuthClientService;
31 import org.openhab.core.auth.client.oauth2.OAuthException;
32 import org.openhab.core.auth.client.oauth2.OAuthFactory;
33 import org.openhab.core.auth.client.oauth2.OAuthResponseException;
34 import org.openhab.core.thing.Bridge;
35 import org.openhab.core.thing.ChannelUID;
36 import org.openhab.core.thing.ThingStatus;
37 import org.openhab.core.thing.ThingStatusDetail;
38 import org.openhab.core.thing.binding.BaseBridgeHandler;
39 import org.openhab.core.thing.binding.ThingHandlerService;
40 import org.openhab.core.types.Command;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
45 * The {@link BoschAccountHandler} is responsible for handling commands, which are
46 * sent to one of the channels.
48 * @author Jacob Laursen - Initial contribution
51 public class BoschAccountHandler extends BaseBridgeHandler {
53 private final Logger logger = LoggerFactory.getLogger(BoschAccountHandler.class);
54 private final OAuthFactory oAuthFactory;
56 private OAuthClientService oAuthClientService;
57 private IndegoController controller;
59 public BoschAccountHandler(Bridge bridge, HttpClient httpClient, OAuthFactory oAuthFactory) {
62 this.oAuthFactory = oAuthFactory;
64 oAuthClientService = oAuthFactory.createOAuthClientService(getThing().getUID().getAsString(), BSK_TOKEN_URI,
65 BSK_AUTH_URI, BSK_CLIENT_ID, null, BSK_SCOPE, false);
66 controller = new IndegoController(httpClient, oAuthClientService);
70 public void initialize() {
71 updateStatus(ThingStatus.UNKNOWN);
73 scheduler.execute(() -> {
75 AccessTokenResponse accessTokenResponse = this.oAuthClientService.getAccessTokenResponse();
76 if (accessTokenResponse == null) {
77 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.CONFIGURATION_ERROR,
78 "@text/offline.conf-error.oauth2-unauthorized");
80 updateStatus(ThingStatus.ONLINE);
82 } catch (OAuthException | OAuthResponseException e) {
83 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.CONFIGURATION_ERROR,
84 "@text/offline.conf-error.oauth2-unauthorized");
85 } catch (IOException e) {
86 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR,
87 "@text/offline.comm-error.oauth2-authorization-failed");
93 public void dispose() {
94 oAuthFactory.ungetOAuthService(this.getThing().getUID().getAsString());
98 public void handleCommand(ChannelUID channelUID, Command command) {
102 public Collection<Class<? extends ThingHandlerService>> getServices() {
103 return List.of(IndegoDiscoveryService.class);
106 public void authorize(String authCode) throws IndegoAuthenticationException {
107 logger.info("Attempting to authorize using authorization code");
110 oAuthClientService.getAccessTokenResponseByAuthorizationCode(authCode, BSK_REDIRECT_URI);
111 } catch (OAuthException | OAuthResponseException | IOException e) {
112 throw new IndegoAuthenticationException("Failed to authorize by authorization code " + authCode, e);
115 logger.info("Authorization completed successfully");
117 updateStatus(ThingStatus.ONLINE);
120 public OAuthClientService getOAuthClientService() {
121 return oAuthClientService;
124 public Collection<DevicePropertiesResponse> getDevices() throws IndegoException {
125 Collection<String> serialNumbers = controller.getSerialNumbers();
126 List<DevicePropertiesResponse> devices = new ArrayList<DevicePropertiesResponse>(serialNumbers.size());
128 for (String serialNumber : serialNumbers) {
129 DevicePropertiesResponse properties = controller.getDeviceProperties(serialNumber);
130 if (properties.serialNumber == null) {
131 properties.serialNumber = serialNumber;
133 devices.add(properties);