]> git.basschouten.com Git - openhab-addons.git/blob
8b30b621e26abe7d662d361337865a62a64f92e2
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.boschindego.internal.handler;
14
15 import static org.openhab.binding.boschindego.internal.BoschIndegoBindingConstants.*;
16
17 import java.io.IOException;
18 import java.util.Collection;
19 import java.util.List;
20
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;
41
42 /**
43  * The {@link BoschAccountHandler} is responsible for handling commands, which are
44  * sent to one of the channels.
45  *
46  * @author Jacob Laursen - Initial contribution
47  */
48 @NonNullByDefault
49 public class BoschAccountHandler extends BaseBridgeHandler {
50
51     private final Logger logger = LoggerFactory.getLogger(BoschAccountHandler.class);
52     private final OAuthFactory oAuthFactory;
53
54     private OAuthClientService oAuthClientService;
55     private IndegoController controller;
56
57     public BoschAccountHandler(Bridge bridge, HttpClient httpClient, OAuthFactory oAuthFactory) {
58         super(bridge);
59
60         this.oAuthFactory = oAuthFactory;
61
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);
65     }
66
67     @Override
68     public void initialize() {
69         updateStatus(ThingStatus.UNKNOWN);
70
71         scheduler.execute(() -> {
72             try {
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");
77                 } else {
78                     updateStatus(ThingStatus.ONLINE);
79                 }
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");
86             }
87         });
88     }
89
90     @Override
91     public void dispose() {
92         oAuthFactory.ungetOAuthService(this.getThing().getUID().getAsString());
93     }
94
95     @Override
96     public void handleCommand(ChannelUID channelUID, Command command) {
97     }
98
99     @Override
100     public Collection<Class<? extends ThingHandlerService>> getServices() {
101         return List.of(IndegoDiscoveryService.class);
102     }
103
104     public void authorize(String authCode) throws IndegoAuthenticationException {
105         logger.info("Attempting to authorize using authorization code");
106
107         try {
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);
111         }
112
113         logger.info("Authorization completed successfully");
114
115         updateStatus(ThingStatus.ONLINE);
116     }
117
118     public OAuthClientService getOAuthClientService() {
119         return oAuthClientService;
120     }
121
122     public Collection<String> getSerialNumbers() throws IndegoException {
123         return controller.getSerialNumbers();
124     }
125 }