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.openweathermap.internal.connection;
15 import static org.eclipse.jetty.http.HttpMethod.GET;
16 import static org.eclipse.jetty.http.HttpStatus.*;
18 import java.net.URLEncoder;
19 import java.nio.charset.StandardCharsets;
20 import java.time.ZoneId;
21 import java.time.ZonedDateTime;
22 import java.util.ArrayList;
23 import java.util.Arrays;
24 import java.util.HashMap;
25 import java.util.List;
27 import java.util.concurrent.ExecutionException;
28 import java.util.concurrent.TimeUnit;
29 import java.util.concurrent.TimeoutException;
30 import java.util.stream.Collectors;
32 import org.eclipse.jdt.annotation.NonNullByDefault;
33 import org.eclipse.jdt.annotation.Nullable;
34 import org.eclipse.jetty.client.HttpClient;
35 import org.eclipse.jetty.client.HttpResponseException;
36 import org.eclipse.jetty.client.api.ContentResponse;
37 import org.openhab.binding.openweathermap.internal.config.OpenWeatherMapAPIConfiguration;
38 import org.openhab.binding.openweathermap.internal.dto.OpenWeatherMapJsonAirPollutionData;
39 import org.openhab.binding.openweathermap.internal.dto.OpenWeatherMapJsonDailyForecastData;
40 import org.openhab.binding.openweathermap.internal.dto.OpenWeatherMapJsonHourlyForecastData;
41 import org.openhab.binding.openweathermap.internal.dto.OpenWeatherMapJsonUVIndexData;
42 import org.openhab.binding.openweathermap.internal.dto.OpenWeatherMapJsonWeatherData;
43 import org.openhab.binding.openweathermap.internal.dto.OpenWeatherMapOneCallAPIData;
44 import org.openhab.binding.openweathermap.internal.dto.OpenWeatherMapOneCallHistAPIData;
45 import org.openhab.binding.openweathermap.internal.handler.OpenWeatherMapAPIHandler;
46 import org.openhab.core.cache.ByteArrayFileCache;
47 import org.openhab.core.cache.ExpiringCacheMap;
48 import org.openhab.core.i18n.CommunicationException;
49 import org.openhab.core.i18n.ConfigurationException;
50 import org.openhab.core.io.net.http.HttpUtil;
51 import org.openhab.core.library.types.PointType;
52 import org.openhab.core.library.types.RawType;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
56 import com.google.gson.Gson;
57 import com.google.gson.JsonElement;
58 import com.google.gson.JsonObject;
59 import com.google.gson.JsonParser;
60 import com.google.gson.JsonSyntaxException;
63 * The {@link OpenWeatherMapConnection} is responsible for handling the connections to OpenWeatherMap API.
65 * @author Christoph Weitkamp - Initial contribution
68 public class OpenWeatherMapConnection {
70 private final Logger logger = LoggerFactory.getLogger(OpenWeatherMapConnection.class);
72 private static final String PROPERTY_MESSAGE = "message";
74 private static final String PNG_CONTENT_TYPE = "image/png";
76 private static final String PARAM_APPID = "appid";
77 private static final String PARAM_UNITS = "units";
78 private static final String PARAM_LAT = "lat";
79 private static final String PARAM_LON = "lon";
80 private static final String PARAM_LANG = "lang";
81 private static final String PARAM_FORECAST_CNT = "cnt";
82 private static final String PARAM_HISTORY_DATE = "dt";
83 private static final String PARAM_EXCLUDE = "exclude";
85 // Current weather data (see https://openweathermap.org/current)
86 private static final String WEATHER_URL = "https://api.openweathermap.org/data/2.5/weather";
87 // 5 day / 3 hour forecast (see https://openweathermap.org/forecast5)
88 private static final String THREE_HOUR_FORECAST_URL = "https://api.openweathermap.org/data/2.5/forecast";
89 // 16 day / daily forecast (see https://openweathermap.org/forecast16)
90 private static final String DAILY_FORECAST_URL = "https://api.openweathermap.org/data/2.5/forecast/daily";
91 // UV Index (see https://openweathermap.org/api/uvi)
92 private static final String UVINDEX_URL = "https://api.openweathermap.org/data/2.5/uvi";
93 private static final String UVINDEX_FORECAST_URL = "https://api.openweathermap.org/data/2.5/uvi/forecast";
94 // Air Pollution (see https://openweathermap.org/api/air-pollution)
95 private static final String AIR_POLLUTION_URL = "https://api.openweathermap.org/data/2.5/air_pollution";
96 private static final String AIR_POLLUTION_FORECAST_URL = "https://api.openweathermap.org/data/2.5/air_pollution/forecast";
97 // Weather icons (see https://openweathermap.org/weather-conditions)
98 private static final String ICON_URL = "https://openweathermap.org/img/w/%s.png";
99 // One Call API (see https://openweathermap.org/api/one-call-api )
100 private static final String ONECALL_URL = "https://api.openweathermap.org/data";
101 private static final String ONECALL_DATA_SUFFIX_URL = "onecall";
102 private static final String ONECALL_HISTORY_SUFFIX_URL = "onecall/timemachine";
104 private final OpenWeatherMapAPIHandler handler;
105 private final HttpClient httpClient;
107 private static final ByteArrayFileCache IMAGE_CACHE = new ByteArrayFileCache("org.openhab.binding.openweathermap");
108 private final ExpiringCacheMap<String, String> cache;
110 private final Gson gson = new Gson();
112 public OpenWeatherMapConnection(OpenWeatherMapAPIHandler handler, HttpClient httpClient) {
113 this.handler = handler;
114 this.httpClient = httpClient;
116 OpenWeatherMapAPIConfiguration config = handler.getOpenWeatherMapAPIConfig();
117 cache = new ExpiringCacheMap<>(TimeUnit.MINUTES.toMillis(config.refreshInterval));
121 * Requests the current weather data for the given location (see https://openweathermap.org/current).
123 * @param location location represented as {@link PointType}
124 * @return the current weather data
125 * @throws JsonSyntaxException
126 * @throws CommunicationException
127 * @throws ConfigurationException
129 public synchronized @Nullable OpenWeatherMapJsonWeatherData getWeatherData(@Nullable PointType location)
130 throws JsonSyntaxException, CommunicationException, ConfigurationException {
131 return gson.fromJson(
132 getResponseFromCache(
133 buildURL(WEATHER_URL, getRequestParams(handler.getOpenWeatherMapAPIConfig(), location))),
134 OpenWeatherMapJsonWeatherData.class);
138 * Requests the hourly forecast data for the given location (see https://openweathermap.org/forecast5).
140 * @param location location represented as {@link PointType}
141 * @param count number of hours
142 * @return the hourly forecast data
143 * @throws JsonSyntaxException
144 * @throws CommunicationException
145 * @throws ConfigurationException
147 public synchronized @Nullable OpenWeatherMapJsonHourlyForecastData getHourlyForecastData(
148 @Nullable PointType location, int count)
149 throws JsonSyntaxException, CommunicationException, ConfigurationException {
151 throw new ConfigurationException("@text/offline.conf-error-not-supported-number-of-hours");
154 Map<String, String> params = getRequestParams(handler.getOpenWeatherMapAPIConfig(), location);
155 params.put(PARAM_FORECAST_CNT, Integer.toString(count));
157 return gson.fromJson(getResponseFromCache(buildURL(THREE_HOUR_FORECAST_URL, params)),
158 OpenWeatherMapJsonHourlyForecastData.class);
162 * Requests the daily forecast data for the given location (see https://openweathermap.org/forecast16).
164 * @param location location represented as {@link PointType}
165 * @param count number of days
166 * @return the daily forecast data
167 * @throws JsonSyntaxException
168 * @throws CommunicationException
169 * @throws ConfigurationException
171 public synchronized @Nullable OpenWeatherMapJsonDailyForecastData getDailyForecastData(@Nullable PointType location,
172 int count) throws JsonSyntaxException, CommunicationException, ConfigurationException {
174 throw new ConfigurationException("@text/offline.conf-error-not-supported-number-of-days");
177 Map<String, String> params = getRequestParams(handler.getOpenWeatherMapAPIConfig(), location);
178 params.put(PARAM_FORECAST_CNT, Integer.toString(count));
180 return gson.fromJson(getResponseFromCache(buildURL(DAILY_FORECAST_URL, params)),
181 OpenWeatherMapJsonDailyForecastData.class);
185 * Requests the UV Index data for the given location (see https://openweathermap.org/api/uvi).
187 * @param location location represented as {@link PointType}
188 * @return the UV Index data
189 * @throws JsonSyntaxException
190 * @throws CommunicationException
191 * @throws ConfigurationException
193 public synchronized @Nullable OpenWeatherMapJsonUVIndexData getUVIndexData(@Nullable PointType location)
194 throws JsonSyntaxException, CommunicationException, ConfigurationException {
195 return gson.fromJson(
196 getResponseFromCache(
197 buildURL(UVINDEX_URL, getRequestParams(handler.getOpenWeatherMapAPIConfig(), location))),
198 OpenWeatherMapJsonUVIndexData.class);
202 * Requests the UV Index forecast data for the given location (see https://openweathermap.org/api/uvi).
204 * @param location location represented as {@link PointType}
205 * @return the UV Index forecast data
206 * @throws JsonSyntaxException
207 * @throws CommunicationException
208 * @throws ConfigurationException
210 public synchronized @Nullable List<OpenWeatherMapJsonUVIndexData> getUVIndexForecastData(
211 @Nullable PointType location, int count)
212 throws JsonSyntaxException, CommunicationException, ConfigurationException {
214 throw new ConfigurationException("@text/offline.conf-error-not-supported-uvindex-number-of-days");
217 Map<String, String> params = getRequestParams(handler.getOpenWeatherMapAPIConfig(), location);
218 params.put(PARAM_FORECAST_CNT, Integer.toString(count));
220 return Arrays.asList(gson.fromJson(getResponseFromCache(buildURL(UVINDEX_FORECAST_URL, params)),
221 OpenWeatherMapJsonUVIndexData[].class));
225 * Requests the Air Pollution data for the given location (see https://openweathermap.org/api/air-pollution).
227 * @param location location represented as {@link PointType}
228 * @return the Air Pollution data
229 * @throws JsonSyntaxException
230 * @throws CommunicationException
231 * @throws ConfigurationException
233 public synchronized @Nullable OpenWeatherMapJsonAirPollutionData getAirPollutionData(@Nullable PointType location)
234 throws JsonSyntaxException, CommunicationException, ConfigurationException {
235 return gson.fromJson(
236 getResponseFromCache(
237 buildURL(AIR_POLLUTION_URL, getRequestParams(handler.getOpenWeatherMapAPIConfig(), location))),
238 OpenWeatherMapJsonAirPollutionData.class);
242 * Requests the Air Pollution forecast data for the given location (see
243 * https://openweathermap.org/api/air-pollution).
245 * @param location location represented as {@link PointType}
246 * @return the Air Pollution forecast data
247 * @throws JsonSyntaxException
248 * @throws CommunicationException
249 * @throws ConfigurationException
251 public synchronized @Nullable OpenWeatherMapJsonAirPollutionData getAirPollutionForecastData(
252 @Nullable PointType location) throws JsonSyntaxException, CommunicationException, ConfigurationException {
253 return gson.fromJson(
254 getResponseFromCache(buildURL(AIR_POLLUTION_FORECAST_URL,
255 getRequestParams(handler.getOpenWeatherMapAPIConfig(), location))),
256 OpenWeatherMapJsonAirPollutionData.class);
260 * Downloads the icon for the given icon id (see https://openweathermap.org/weather-conditions).
262 * @param iconId the id of the icon
263 * @return the weather icon as {@link RawType}
265 public static @Nullable RawType getWeatherIcon(String iconId) {
266 if (iconId.isEmpty()) {
267 throw new IllegalArgumentException("Cannot download weather icon as icon id is null.");
270 return downloadWeatherIconFromCache(String.format(ICON_URL, iconId));
273 private static @Nullable RawType downloadWeatherIconFromCache(String url) {
274 if (IMAGE_CACHE.containsKey(url)) {
276 return new RawType(IMAGE_CACHE.get(url), PNG_CONTENT_TYPE);
277 } catch (Exception e) {
278 LoggerFactory.getLogger(OpenWeatherMapConnection.class)
279 .trace("Failed to download the content of URL '{}'", url, e);
282 RawType image = downloadWeatherIcon(url);
284 IMAGE_CACHE.put(url, image.getBytes());
291 private static @Nullable RawType downloadWeatherIcon(String url) {
292 return HttpUtil.downloadImage(url);
296 * Get Weather data from the One Call API for the given location. See https://openweathermap.org/api/one-call-api
299 * @param location location represented as {@link PointType}
300 * @param excludeMinutely if true, will not fetch minutely forecast data from the server
301 * @param excludeHourly if true, will not fetch hourly forecast data from the server
302 * @param excludeDaily if true, will not fetch hourly forecast data from the server
304 * @throws JsonSyntaxException
305 * @throws CommunicationException
306 * @throws ConfigurationException
308 public synchronized @Nullable OpenWeatherMapOneCallAPIData getOneCallAPIData(@Nullable PointType location,
309 boolean excludeMinutely, boolean excludeHourly, boolean excludeDaily, boolean excludeAlerts)
310 throws JsonSyntaxException, CommunicationException, ConfigurationException {
311 Map<String, String> params = getRequestParams(handler.getOpenWeatherMapAPIConfig(), location);
312 List<String> exclude = new ArrayList<>();
313 if (excludeMinutely) {
314 exclude.add("minutely");
317 exclude.add("hourly");
320 exclude.add("daily");
323 exclude.add("alerts");
325 logger.debug("Exclude: '{}'", exclude);
326 if (!exclude.isEmpty()) {
327 params.put(PARAM_EXCLUDE, exclude.stream().collect(Collectors.joining(",")));
329 return gson.fromJson(getResponseFromCache(buildURL(buildOneCallURL(), params)),
330 OpenWeatherMapOneCallAPIData.class);
334 * Get the historical weather data from the One Call API for the given location and the given number of days in the
335 * past. As of now, OpenWeatherMap supports this function for up to 5 days in the past. However, this may change in
336 * the future, so we don't enforce this limit here. See https://openweathermap.org/api/one-call-api for details.
338 * @param location location represented as {@link PointType}
339 * @param days number of days in the past, relative to the current time.
341 * @throws JsonSyntaxException
342 * @throws CommunicationException
343 * @throws ConfigurationException
345 public synchronized @Nullable OpenWeatherMapOneCallHistAPIData getOneCallHistAPIData(@Nullable PointType location,
346 int days) throws JsonSyntaxException, CommunicationException, ConfigurationException {
347 Map<String, String> params = getRequestParams(handler.getOpenWeatherMapAPIConfig(), location);
348 // the API requests the history as timestamp in Unix time format.
349 params.put(PARAM_HISTORY_DATE,
350 Long.toString(ZonedDateTime.now(ZoneId.of("UTC")).minusDays(days).toEpochSecond()));
351 return gson.fromJson(getResponseFromCache(buildURL(buildOneCallHistoryURL(), params)),
352 OpenWeatherMapOneCallHistAPIData.class);
355 private Map<String, String> getRequestParams(OpenWeatherMapAPIConfiguration config, @Nullable PointType location) {
356 if (location == null) {
357 throw new ConfigurationException("@text/offline.conf-error-missing-location");
360 Map<String, String> params = new HashMap<>();
361 // API key (see https://openweathermap.org/appid)
362 String apikey = config.apikey;
363 if (apikey == null || (apikey = apikey.trim()).isEmpty()) {
364 throw new ConfigurationException("@text/offline.conf-error-missing-apikey");
366 params.put(PARAM_APPID, apikey);
368 // Units format (see https://openweathermap.org/current#data)
369 params.put(PARAM_UNITS, "metric");
371 // By geographic coordinates (see https://openweathermap.org/current#geo)
372 params.put(PARAM_LAT, location.getLatitude().toString());
373 params.put(PARAM_LON, location.getLongitude().toString());
375 // Multilingual support (see https://openweathermap.org/current#multi)
376 String language = config.language;
377 if (language != null && !(language = language.trim()).isEmpty()) {
378 params.put(PARAM_LANG, language.toLowerCase());
383 private String buildURL(String url, Map<String, String> requestParams) {
384 return requestParams.keySet().stream().map(key -> key + "=" + encodeParam(requestParams.get(key)))
385 .collect(Collectors.joining("&", url + "?", ""));
388 private String buildOneCallURL() {
389 var config = handler.getOpenWeatherMapAPIConfig();
390 return ONECALL_URL + "/" + config.apiVersion + "/" + ONECALL_DATA_SUFFIX_URL;
393 private String buildOneCallHistoryURL() {
394 var config = handler.getOpenWeatherMapAPIConfig();
395 return ONECALL_URL + "/" + config.apiVersion + "/" + ONECALL_HISTORY_SUFFIX_URL;
398 private String encodeParam(@Nullable String value) {
399 return value == null ? "" : URLEncoder.encode(value, StandardCharsets.UTF_8);
402 private @Nullable String getResponseFromCache(String url) {
403 return cache.putIfAbsentAndGet(url, () -> getResponse(url));
406 private String getResponse(String url) {
408 if (logger.isTraceEnabled()) {
409 logger.trace("OpenWeatherMap request: URL = '{}'", uglifyApikey(url));
411 ContentResponse contentResponse = httpClient.newRequest(url).method(GET).timeout(10, TimeUnit.SECONDS)
413 int httpStatus = contentResponse.getStatus();
414 String content = contentResponse.getContentAsString();
415 String errorMessage = "";
416 logger.trace("OpenWeatherMap response: status = {}, content = '{}'", httpStatus, content);
417 switch (httpStatus) {
420 case BAD_REQUEST_400:
421 case UNAUTHORIZED_401:
423 errorMessage = getErrorMessage(content);
424 logger.debug("OpenWeatherMap server responded with status code {}: {}", httpStatus, errorMessage);
425 throw new ConfigurationException(errorMessage);
426 case TOO_MANY_REQUESTS_429:
427 // TODO disable refresh job temporarily (see https://openweathermap.org/appid#Accesslimitation)
429 errorMessage = getErrorMessage(content);
430 logger.debug("OpenWeatherMap server responded with status code {}: {}", httpStatus, errorMessage);
431 throw new CommunicationException(errorMessage);
433 } catch (ExecutionException e) {
434 String errorMessage = e.getMessage();
435 logger.debug("ExecutionException occurred during execution: {}", errorMessage, e);
436 if (e.getCause() instanceof HttpResponseException) {
437 logger.debug("OpenWeatherMap server responded with status code {}: Invalid API key.", UNAUTHORIZED_401);
438 throw new ConfigurationException("@text/offline.conf-error-invalid-apikey", e.getCause());
440 throw new CommunicationException(
441 errorMessage == null ? "@text/offline.communication-error" : errorMessage, e.getCause());
443 } catch (TimeoutException e) {
444 String errorMessage = e.getMessage();
445 logger.debug("TimeoutException occurred during execution: {}", errorMessage, e);
446 throw new CommunicationException(errorMessage == null ? "@text/offline.communication-error" : errorMessage,
448 } catch (InterruptedException e) {
449 String errorMessage = e.getMessage();
450 logger.debug("InterruptedException occurred during execution: {}", errorMessage, e);
451 Thread.currentThread().interrupt();
452 throw new CommunicationException(errorMessage == null ? "@text/offline.communication-error" : errorMessage,
457 private String uglifyApikey(String url) {
458 return url.replaceAll("(appid=)+\\w+", "appid=*****");
461 private String getErrorMessage(String response) {
462 JsonElement jsonResponse = JsonParser.parseString(response);
463 if (jsonResponse.isJsonObject()) {
464 JsonObject json = jsonResponse.getAsJsonObject();
465 if (json.has(PROPERTY_MESSAGE)) {
466 return json.get(PROPERTY_MESSAGE).getAsString();