2 * Copyright (c) 2010-2024 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.netatmo.internal.handler;
15 import static java.util.Comparator.*;
16 import static org.openhab.binding.netatmo.internal.NetatmoBindingConstants.*;
18 import java.io.ByteArrayInputStream;
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.lang.reflect.Constructor;
23 import java.nio.charset.StandardCharsets;
24 import java.time.LocalDateTime;
25 import java.util.ArrayDeque;
26 import java.util.Collection;
27 import java.util.Deque;
28 import java.util.HashMap;
30 import java.util.Optional;
32 import java.util.concurrent.ExecutionException;
33 import java.util.concurrent.ScheduledFuture;
34 import java.util.concurrent.TimeUnit;
35 import java.util.concurrent.TimeoutException;
36 import java.util.function.BiFunction;
38 import javax.ws.rs.core.UriBuilder;
40 import org.eclipse.jdt.annotation.NonNullByDefault;
41 import org.eclipse.jdt.annotation.Nullable;
42 import org.eclipse.jetty.client.HttpClient;
43 import org.eclipse.jetty.client.api.ContentResponse;
44 import org.eclipse.jetty.client.api.Request;
45 import org.eclipse.jetty.client.util.InputStreamContentProvider;
46 import org.eclipse.jetty.http.HttpField;
47 import org.eclipse.jetty.http.HttpHeader;
48 import org.eclipse.jetty.http.HttpMethod;
49 import org.eclipse.jetty.http.HttpStatus;
50 import org.eclipse.jetty.http.HttpStatus.Code;
51 import org.openhab.binding.netatmo.internal.api.AircareApi;
52 import org.openhab.binding.netatmo.internal.api.ApiError;
53 import org.openhab.binding.netatmo.internal.api.AuthenticationApi;
54 import org.openhab.binding.netatmo.internal.api.HomeApi;
55 import org.openhab.binding.netatmo.internal.api.ListBodyResponse;
56 import org.openhab.binding.netatmo.internal.api.NetatmoException;
57 import org.openhab.binding.netatmo.internal.api.RestManager;
58 import org.openhab.binding.netatmo.internal.api.SecurityApi;
59 import org.openhab.binding.netatmo.internal.api.WeatherApi;
60 import org.openhab.binding.netatmo.internal.api.data.NetatmoConstants.FeatureArea;
61 import org.openhab.binding.netatmo.internal.api.data.NetatmoConstants.Scope;
62 import org.openhab.binding.netatmo.internal.api.data.NetatmoConstants.ServiceError;
63 import org.openhab.binding.netatmo.internal.api.dto.HomeData;
64 import org.openhab.binding.netatmo.internal.api.dto.HomeDataModule;
65 import org.openhab.binding.netatmo.internal.api.dto.NAMain;
66 import org.openhab.binding.netatmo.internal.api.dto.NAModule;
67 import org.openhab.binding.netatmo.internal.config.ApiHandlerConfiguration;
68 import org.openhab.binding.netatmo.internal.config.BindingConfiguration;
69 import org.openhab.binding.netatmo.internal.config.ConfigurationLevel;
70 import org.openhab.binding.netatmo.internal.deserialization.AccessTokenResponseDeserializer;
71 import org.openhab.binding.netatmo.internal.deserialization.NADeserializer;
72 import org.openhab.binding.netatmo.internal.discovery.NetatmoDiscoveryService;
73 import org.openhab.binding.netatmo.internal.servlet.GrantServlet;
74 import org.openhab.binding.netatmo.internal.servlet.WebhookServlet;
75 import org.openhab.core.auth.client.oauth2.AccessTokenResponse;
76 import org.openhab.core.auth.client.oauth2.OAuthClientService;
77 import org.openhab.core.auth.client.oauth2.OAuthException;
78 import org.openhab.core.auth.client.oauth2.OAuthFactory;
79 import org.openhab.core.auth.client.oauth2.OAuthResponseException;
80 import org.openhab.core.library.types.DecimalType;
81 import org.openhab.core.thing.Bridge;
82 import org.openhab.core.thing.ChannelUID;
83 import org.openhab.core.thing.Thing;
84 import org.openhab.core.thing.ThingStatus;
85 import org.openhab.core.thing.ThingStatusDetail;
86 import org.openhab.core.thing.ThingUID;
87 import org.openhab.core.thing.binding.BaseBridgeHandler;
88 import org.openhab.core.thing.binding.ThingHandlerService;
89 import org.openhab.core.types.Command;
90 import org.osgi.service.http.HttpService;
91 import org.slf4j.Logger;
92 import org.slf4j.LoggerFactory;
94 import com.google.gson.GsonBuilder;
97 * {@link ApiBridgeHandler} is the handler for a Netatmo API and connects it to the framework.
99 * @author Gaƫl L'hopital - Initial contribution
100 * @author Jacob Laursen - Refactored to use standard OAuth2 implementation
103 public class ApiBridgeHandler extends BaseBridgeHandler {
104 private static final int TIMEOUT_S = 20;
106 private final Logger logger = LoggerFactory.getLogger(ApiBridgeHandler.class);
107 private final AuthenticationApi connectApi = new AuthenticationApi(this);
108 private final Map<Class<? extends RestManager>, RestManager> managers = new HashMap<>();
109 private final Deque<LocalDateTime> requestsTimestamps = new ArrayDeque<>(200);
110 private final BindingConfiguration bindingConf;
111 private final HttpClient httpClient;
112 private final OAuthFactory oAuthFactory;
113 private final NADeserializer deserializer;
114 private final HttpService httpService;
115 private final ChannelUID requestCountChannelUID;
117 private @Nullable OAuthClientService oAuthClientService;
118 private Optional<ScheduledFuture<?>> connectJob = Optional.empty();
119 private Optional<WebhookServlet> webHookServlet = Optional.empty();
120 private Optional<GrantServlet> grantServlet = Optional.empty();
122 public ApiBridgeHandler(Bridge bridge, HttpClient httpClient, NADeserializer deserializer,
123 BindingConfiguration configuration, HttpService httpService, OAuthFactory oAuthFactory) {
125 this.bindingConf = configuration;
126 this.httpClient = httpClient;
127 this.deserializer = deserializer;
128 this.httpService = httpService;
129 this.oAuthFactory = oAuthFactory;
131 requestCountChannelUID = new ChannelUID(thing.getUID(), GROUP_MONITORING, CHANNEL_REQUEST_COUNT);
135 public void initialize() {
136 logger.debug("Initializing Netatmo API bridge handler.");
138 ApiHandlerConfiguration configuration = getConfiguration();
140 if (configuration.clientId.isBlank()) {
141 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
142 ConfigurationLevel.EMPTY_CLIENT_ID.message);
146 if (configuration.clientSecret.isBlank()) {
147 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
148 ConfigurationLevel.EMPTY_CLIENT_SECRET.message);
152 oAuthClientService = oAuthFactory
153 .createOAuthClientService(this.getThing().getUID().getAsString(),
154 AuthenticationApi.TOKEN_URI.toString(), AuthenticationApi.AUTH_URI.toString(),
155 configuration.clientId, configuration.clientSecret, FeatureArea.ALL_SCOPES, false)
156 .withGsonBuilder(new GsonBuilder().registerTypeAdapter(AccessTokenResponse.class,
157 new AccessTokenResponseDeserializer()));
159 updateStatus(ThingStatus.UNKNOWN);
161 scheduler.execute(() -> openConnection(null, null));
164 public void openConnection(@Nullable String code, @Nullable String redirectUri) {
165 if (!authenticate(code, redirectUri)) {
169 logger.debug("Connecting to Netatmo API.");
171 ApiHandlerConfiguration configuration = getConfiguration();
172 if (!configuration.webHookUrl.isBlank()) {
173 SecurityApi securityApi = getRestManager(SecurityApi.class);
174 if (securityApi != null) {
175 webHookServlet.ifPresent(servlet -> servlet.dispose());
176 WebhookServlet servlet = new WebhookServlet(this, httpService, deserializer, securityApi,
177 configuration.webHookUrl, configuration.webHookPostfix);
178 servlet.startListening();
179 this.webHookServlet = Optional.of(servlet);
183 updateStatus(ThingStatus.ONLINE);
185 getThing().getThings().stream().filter(Thing::isEnabled).map(Thing::getHandler)
186 .filter(CommonInterface.class::isInstance).map(CommonInterface.class::cast)
187 .forEach(CommonInterface::expireData);
190 private boolean authenticate(@Nullable String code, @Nullable String redirectUri) {
191 OAuthClientService oAuthClientService = this.oAuthClientService;
192 if (oAuthClientService == null) {
193 logger.debug("ApiBridgeHandler is not ready, OAuthClientService not initialized");
197 AccessTokenResponse accessTokenResponse;
200 accessTokenResponse = oAuthClientService.getAccessTokenResponseByAuthorizationCode(code, redirectUri);
202 // Dispose grant servlet upon completion of authorization flow.
203 grantServlet.ifPresent(servlet -> servlet.dispose());
204 grantServlet = Optional.empty();
206 accessTokenResponse = oAuthClientService.getAccessTokenResponse();
208 } catch (OAuthException | OAuthResponseException e) {
209 logger.debug("Failed to load access token: {}", e.getMessage());
210 startAuthorizationFlow();
212 } catch (IOException e) {
213 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage());
214 prepareReconnection(code, redirectUri);
218 if (accessTokenResponse == null) {
219 logger.debug("Authorization failed, restarting authorization flow");
220 startAuthorizationFlow();
224 connectApi.setAccessToken(accessTokenResponse.getAccessToken());
225 connectApi.setScope(accessTokenResponse.getScope());
230 private void startAuthorizationFlow() {
231 GrantServlet servlet = new GrantServlet(this, httpService);
232 servlet.startListening();
233 grantServlet = Optional.of(servlet);
234 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
235 ConfigurationLevel.REFRESH_TOKEN_NEEDED.message.formatted(servlet.getPath()));
238 public ApiHandlerConfiguration getConfiguration() {
239 return getConfigAs(ApiHandlerConfiguration.class);
242 private void prepareReconnection(@Nullable String code, @Nullable String redirectUri) {
243 connectApi.dispose();
245 connectJob = Optional.of(scheduler.schedule(() -> openConnection(code, redirectUri),
246 getConfiguration().reconnectInterval, TimeUnit.SECONDS));
249 private void freeConnectJob() {
250 connectJob.ifPresent(j -> j.cancel(true));
251 connectJob = Optional.empty();
255 public void dispose() {
256 logger.debug("Shutting down Netatmo API bridge handler.");
258 webHookServlet.ifPresent(servlet -> servlet.dispose());
259 webHookServlet = Optional.empty();
261 grantServlet.ifPresent(servlet -> servlet.dispose());
262 grantServlet = Optional.empty();
264 connectApi.dispose();
267 oAuthFactory.ungetOAuthService(this.getThing().getUID().getAsString());
273 public void handleRemoval() {
274 oAuthFactory.deleteServiceAndAccessToken(this.getThing().getUID().getAsString());
275 super.handleRemoval();
279 public void handleCommand(ChannelUID channelUID, Command command) {
280 logger.debug("Netatmo Bridge is read-only and does not handle commands");
283 @SuppressWarnings("unchecked")
284 public <T extends RestManager> @Nullable T getRestManager(Class<T> clazz) {
285 if (!managers.containsKey(clazz)) {
287 Constructor<T> constructor = clazz.getConstructor(getClass());
288 T instance = constructor.newInstance(this);
289 Set<Scope> expected = instance.getRequiredScopes();
290 if (connectApi.matchesScopes(expected)) {
291 managers.put(clazz, instance);
293 logger.info("Unable to instantiate {}, expected scope {} is not active", clazz, expected);
295 } catch (SecurityException | ReflectiveOperationException e) {
296 logger.warn("Error invoking RestManager constructor for class {}: {}", clazz, e.getMessage());
299 return (T) managers.get(clazz);
302 public synchronized <T> T executeUri(URI uri, HttpMethod method, Class<T> clazz, @Nullable String payload,
303 @Nullable String contentType, int retryCount) throws NetatmoException {
305 logger.debug("executeUri {} {} ", method.toString(), uri);
307 Request request = httpClient.newRequest(uri).method(method).timeout(TIMEOUT_S, TimeUnit.SECONDS);
309 if (!authenticate(null, null)) {
310 prepareReconnection(null, null);
311 throw new NetatmoException("Not authenticated");
313 connectApi.getAuthorization().ifPresent(auth -> request.header(HttpHeader.AUTHORIZATION, auth));
315 if (payload != null && contentType != null
316 && (HttpMethod.POST.equals(method) || HttpMethod.PUT.equals(method))) {
317 InputStream stream = new ByteArrayInputStream(payload.getBytes(StandardCharsets.UTF_8));
318 try (InputStreamContentProvider inputStreamContentProvider = new InputStreamContentProvider(stream)) {
319 request.content(inputStreamContentProvider, contentType);
320 request.header(HttpHeader.ACCEPT, "application/json");
322 logger.trace(" -with payload: {} ", payload);
325 if (isLinked(requestCountChannelUID)) {
326 LocalDateTime now = LocalDateTime.now();
327 LocalDateTime oneHourAgo = now.minusHours(1);
328 requestsTimestamps.addLast(now);
329 while (requestsTimestamps.getFirst().isBefore(oneHourAgo)) {
330 requestsTimestamps.removeFirst();
332 updateState(requestCountChannelUID, new DecimalType(requestsTimestamps.size()));
334 logger.trace(" -with headers: {} ",
335 String.join(", ", request.getHeaders().stream().map(HttpField::toString).toList()));
336 ContentResponse response = request.send();
338 Code statusCode = HttpStatus.getCode(response.getStatus());
339 String responseBody = new String(response.getContent(), StandardCharsets.UTF_8);
340 logger.trace(" -returned: code {} body {}", statusCode, responseBody);
342 if (statusCode == Code.OK) {
343 updateStatus(ThingStatus.ONLINE);
344 return deserializer.deserialize(clazz, responseBody);
347 NetatmoException exception;
349 exception = new NetatmoException(deserializer.deserialize(ApiError.class, responseBody));
350 } catch (NetatmoException e) {
351 exception = new NetatmoException("Error deserializing error: %s".formatted(statusCode.getMessage()));
354 } catch (NetatmoException e) {
355 if (e.getStatusCode() == ServiceError.MAXIMUM_USAGE_REACHED) {
356 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "@text/maximum-usage-reached");
357 prepareReconnection(null, null);
360 } catch (InterruptedException e) {
361 Thread.currentThread().interrupt();
362 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage());
363 throw new NetatmoException("Request interrupted");
364 } catch (TimeoutException | ExecutionException e) {
365 if (retryCount > 0) {
366 logger.debug("Request timedout, retry counter: {}", retryCount);
367 return executeUri(uri, method, clazz, payload, contentType, retryCount - 1);
369 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "@text/request-time-out");
370 prepareReconnection(null, null);
371 throw new NetatmoException(String.format("%s: \"%s\"", e.getClass().getName(), e.getMessage()));
375 public void identifyAllModulesAndApplyAction(BiFunction<NAModule, ThingUID, Optional<ThingUID>> action) {
376 ThingUID accountUID = getThing().getUID();
378 AircareApi airCareApi = getRestManager(AircareApi.class);
379 if (airCareApi != null) { // Search Healthy Home Coaches
380 ListBodyResponse<NAMain> body = airCareApi.getHomeCoachData(null).getBody();
382 body.getElements().stream().forEach(homeCoach -> action.apply(homeCoach, accountUID));
385 WeatherApi weatherApi = getRestManager(WeatherApi.class);
386 if (weatherApi != null) { // Search owned or favorite stations
387 weatherApi.getFavoriteAndGuestStationsData().stream().forEach(station -> {
388 if (!station.isReadOnly() || getReadFriends()) {
389 action.apply(station, accountUID).ifPresent(stationUID -> station.getModules().values().stream()
390 .forEach(module -> action.apply(module, stationUID)));
394 HomeApi homeApi = getRestManager(HomeApi.class);
395 if (homeApi != null) { // Search those depending from a home that has modules + not only weather modules
396 homeApi.getHomesData(null, null).stream()
397 .filter(h -> !(h.getFeatures().isEmpty()
398 || h.getFeatures().contains(FeatureArea.WEATHER) && h.getFeatures().size() == 1))
400 action.apply(home, accountUID).ifPresent(homeUID -> {
401 if (home instanceof HomeData.Security securityData) {
402 securityData.getKnownPersons().forEach(person -> action.apply(person, homeUID));
404 Map<String, ThingUID> bridgesUids = new HashMap<>();
406 home.getRooms().values().stream().forEach(room -> {
407 room.getModuleIds().stream().map(id -> home.getModules().get(id))
408 .map(m -> m != null ? m.getType().feature : FeatureArea.NONE)
409 .filter(f -> FeatureArea.ENERGY.equals(f)).findAny()
410 .ifPresent(f -> action.apply(room, homeUID)
411 .ifPresent(roomUID -> bridgesUids.put(room.getId(), roomUID)));
414 // Creating modules that have no bridge first, avoiding weather station itself
415 home.getModules().values().stream()
416 .filter(module -> module.getType().feature != FeatureArea.WEATHER)
417 .sorted(comparing(HomeDataModule::getBridge, nullsFirst(naturalOrder())))
419 String bridgeId = module.getBridge();
420 if (bridgeId == null) {
421 action.apply(module, homeUID).ifPresent(
422 moduleUID -> bridgesUids.put(module.getId(), moduleUID));
424 action.apply(module, bridgesUids.getOrDefault(bridgeId, homeUID));
430 } catch (NetatmoException e) {
431 logger.warn("Error while identifying all modules: {}", e.getMessage());
435 public boolean getReadFriends() {
436 return bindingConf.readFriends;
439 public boolean isConnected() {
440 return connectApi.isConnected();
443 public String getId() {
444 return (String) getThing().getConfiguration().get(ApiHandlerConfiguration.CLIENT_ID);
447 public UriBuilder formatAuthorizationUrl() {
448 return AuthenticationApi.getAuthorizationBuilder(getId());
452 public Collection<Class<? extends ThingHandlerService>> getServices() {
453 return Set.of(NetatmoDiscoveryService.class);
456 public Optional<WebhookServlet> getWebHookServlet() {
457 return webHookServlet;