2 * Copyright (c) 2010-2021 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.io.UnsupportedEncodingException;
19 import java.net.URLEncoder;
20 import java.nio.charset.StandardCharsets;
21 import java.time.ZoneId;
22 import java.time.ZonedDateTime;
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.HashMap;
26 import java.util.List;
28 import java.util.concurrent.ExecutionException;
29 import java.util.concurrent.TimeUnit;
30 import java.util.concurrent.TimeoutException;
31 import java.util.stream.Collectors;
33 import org.eclipse.jdt.annotation.NonNullByDefault;
34 import org.eclipse.jdt.annotation.Nullable;
35 import org.eclipse.jetty.client.HttpClient;
36 import org.eclipse.jetty.client.HttpResponseException;
37 import org.eclipse.jetty.client.api.ContentResponse;
38 import org.openhab.binding.openweathermap.internal.config.OpenWeatherMapAPIConfiguration;
39 import org.openhab.binding.openweathermap.internal.dto.OpenWeatherMapJsonAirPollutionData;
40 import org.openhab.binding.openweathermap.internal.dto.OpenWeatherMapJsonDailyForecastData;
41 import org.openhab.binding.openweathermap.internal.dto.OpenWeatherMapJsonHourlyForecastData;
42 import org.openhab.binding.openweathermap.internal.dto.OpenWeatherMapJsonUVIndexData;
43 import org.openhab.binding.openweathermap.internal.dto.OpenWeatherMapJsonWeatherData;
44 import org.openhab.binding.openweathermap.internal.dto.onecall.OpenWeatherMapOneCallAPIData;
45 import org.openhab.binding.openweathermap.internal.dto.onecallhist.OpenWeatherMapOneCallHistAPIData;
46 import org.openhab.binding.openweathermap.internal.handler.OpenWeatherMapAPIHandler;
47 import org.openhab.core.cache.ByteArrayFileCache;
48 import org.openhab.core.cache.ExpiringCacheMap;
49 import org.openhab.core.io.net.http.HttpUtil;
50 import org.openhab.core.library.types.PointType;
51 import org.openhab.core.library.types.RawType;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
55 import com.google.gson.Gson;
56 import com.google.gson.JsonElement;
57 import com.google.gson.JsonObject;
58 import com.google.gson.JsonParser;
59 import com.google.gson.JsonSyntaxException;
62 * The {@link OpenWeatherMapConnection} is responsible for handling the connections to OpenWeatherMap API.
64 * @author Christoph Weitkamp - Initial contribution
67 public class OpenWeatherMapConnection {
69 private final Logger logger = LoggerFactory.getLogger(OpenWeatherMapConnection.class);
71 private static final String PROPERTY_MESSAGE = "message";
73 private static final String PNG_CONTENT_TYPE = "image/png";
75 private static final String PARAM_APPID = "appid";
76 private static final String PARAM_UNITS = "units";
77 private static final String PARAM_LAT = "lat";
78 private static final String PARAM_LON = "lon";
79 private static final String PARAM_LANG = "lang";
80 private static final String PARAM_FORECAST_CNT = "cnt";
81 private static final String PARAM_HISTORY_DATE = "dt";
82 private static final String PARAM_EXCLUDE = "exclude";
84 // Current weather data (see https://openweathermap.org/current)
85 private static final String WEATHER_URL = "https://api.openweathermap.org/data/2.5/weather";
86 // 5 day / 3 hour forecast (see https://openweathermap.org/forecast5)
87 private static final String THREE_HOUR_FORECAST_URL = "https://api.openweathermap.org/data/2.5/forecast";
88 // 16 day / daily forecast (see https://openweathermap.org/forecast16)
89 private static final String DAILY_FORECAST_URL = "https://api.openweathermap.org/data/2.5/forecast/daily";
90 // UV Index (see https://openweathermap.org/api/uvi)
91 private static final String UVINDEX_URL = "https://api.openweathermap.org/data/2.5/uvi";
92 private static final String UVINDEX_FORECAST_URL = "https://api.openweathermap.org/data/2.5/uvi/forecast";
93 // Air Pollution (see https://openweathermap.org/api/air-pollution)
94 private static final String AIR_POLLUTION_URL = "https://api.openweathermap.org/data/2.5/air_pollution";
95 private static final String AIR_POLLUTION_FORECAST_URL = "https://api.openweathermap.org/data/2.5/air_pollution/forecast";
96 // Weather icons (see https://openweathermap.org/weather-conditions)
97 private static final String ICON_URL = "https://openweathermap.org/img/w/%s.png";
98 // One Call API (see https://openweathermap.org/api/one-call-api )
99 private static final String ONECALL_URL = "https://api.openweathermap.org/data/2.5/onecall";
100 private static final String ONECALL_HISTORY_URL = "https://api.openweathermap.org/data/2.5/onecall/timemachine";
102 private final OpenWeatherMapAPIHandler handler;
103 private final HttpClient httpClient;
105 private static final ByteArrayFileCache IMAGE_CACHE = new ByteArrayFileCache("org.openhab.binding.openweathermap");
106 private final ExpiringCacheMap<String, String> cache;
108 private final Gson gson = new Gson();
110 public OpenWeatherMapConnection(OpenWeatherMapAPIHandler handler, HttpClient httpClient) {
111 this.handler = handler;
112 this.httpClient = httpClient;
114 OpenWeatherMapAPIConfiguration config = handler.getOpenWeatherMapAPIConfig();
115 cache = new ExpiringCacheMap<>(TimeUnit.MINUTES.toMillis(config.refreshInterval));
119 * Requests the current weather data for the given location (see https://openweathermap.org/current).
121 * @param location location represented as {@link PointType}
122 * @return the current weather data
123 * @throws JsonSyntaxException
124 * @throws OpenWeatherMapCommunicationException
125 * @throws OpenWeatherMapConfigurationException
127 public synchronized @Nullable OpenWeatherMapJsonWeatherData getWeatherData(@Nullable PointType location)
128 throws JsonSyntaxException, OpenWeatherMapCommunicationException, OpenWeatherMapConfigurationException {
129 return gson.fromJson(
130 getResponseFromCache(
131 buildURL(WEATHER_URL, getRequestParams(handler.getOpenWeatherMapAPIConfig(), location))),
132 OpenWeatherMapJsonWeatherData.class);
136 * Requests the hourly forecast data for the given location (see https://openweathermap.org/forecast5).
138 * @param location location represented as {@link PointType}
139 * @param count number of hours
140 * @return the hourly forecast data
141 * @throws JsonSyntaxException
142 * @throws OpenWeatherMapCommunicationException
143 * @throws OpenWeatherMapConfigurationException
145 public synchronized @Nullable OpenWeatherMapJsonHourlyForecastData getHourlyForecastData(
146 @Nullable PointType location, int count)
147 throws JsonSyntaxException, OpenWeatherMapCommunicationException, OpenWeatherMapConfigurationException {
149 throw new OpenWeatherMapConfigurationException("@text/offline.conf-error-not-supported-number-of-hours");
152 Map<String, String> params = getRequestParams(handler.getOpenWeatherMapAPIConfig(), location);
153 params.put(PARAM_FORECAST_CNT, Integer.toString(count));
155 return gson.fromJson(getResponseFromCache(buildURL(THREE_HOUR_FORECAST_URL, params)),
156 OpenWeatherMapJsonHourlyForecastData.class);
160 * Requests the daily forecast data for the given location (see https://openweathermap.org/forecast16).
162 * @param location location represented as {@link PointType}
163 * @param count number of days
164 * @return the daily forecast data
165 * @throws JsonSyntaxException
166 * @throws OpenWeatherMapCommunicationException
167 * @throws OpenWeatherMapConfigurationException
169 public synchronized @Nullable OpenWeatherMapJsonDailyForecastData getDailyForecastData(@Nullable PointType location,
171 throws JsonSyntaxException, OpenWeatherMapCommunicationException, OpenWeatherMapConfigurationException {
173 throw new OpenWeatherMapConfigurationException("@text/offline.conf-error-not-supported-number-of-days");
176 Map<String, String> params = getRequestParams(handler.getOpenWeatherMapAPIConfig(), location);
177 params.put(PARAM_FORECAST_CNT, Integer.toString(count));
179 return gson.fromJson(getResponseFromCache(buildURL(DAILY_FORECAST_URL, params)),
180 OpenWeatherMapJsonDailyForecastData.class);
184 * Requests the UV Index data for the given location (see https://openweathermap.org/api/uvi).
186 * @param location location represented as {@link PointType}
187 * @return the UV Index data
188 * @throws JsonSyntaxException
189 * @throws OpenWeatherMapCommunicationException
190 * @throws OpenWeatherMapConfigurationException
192 public synchronized @Nullable OpenWeatherMapJsonUVIndexData getUVIndexData(@Nullable PointType location)
193 throws JsonSyntaxException, OpenWeatherMapCommunicationException, OpenWeatherMapConfigurationException {
194 return gson.fromJson(
195 getResponseFromCache(
196 buildURL(UVINDEX_URL, getRequestParams(handler.getOpenWeatherMapAPIConfig(), location))),
197 OpenWeatherMapJsonUVIndexData.class);
201 * Requests the UV Index forecast data for the given location (see https://openweathermap.org/api/uvi).
203 * @param location location represented as {@link PointType}
204 * @return the UV Index forecast data
205 * @throws JsonSyntaxException
206 * @throws OpenWeatherMapCommunicationException
207 * @throws OpenWeatherMapConfigurationException
209 public synchronized @Nullable List<OpenWeatherMapJsonUVIndexData> getUVIndexForecastData(
210 @Nullable PointType location, int count)
211 throws JsonSyntaxException, OpenWeatherMapCommunicationException, OpenWeatherMapConfigurationException {
213 throw new OpenWeatherMapConfigurationException(
214 "@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 OpenWeatherMapCommunicationException
231 * @throws OpenWeatherMapConfigurationException
233 public synchronized @Nullable OpenWeatherMapJsonAirPollutionData getAirPollutionData(@Nullable PointType location)
234 throws JsonSyntaxException, OpenWeatherMapCommunicationException, OpenWeatherMapConfigurationException {
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 OpenWeatherMapCommunicationException
249 * @throws OpenWeatherMapConfigurationException
251 public synchronized @Nullable OpenWeatherMapJsonAirPollutionData getAirPollutionForecastData(
252 @Nullable PointType location)
253 throws JsonSyntaxException, OpenWeatherMapCommunicationException, OpenWeatherMapConfigurationException {
254 return gson.fromJson(
255 getResponseFromCache(buildURL(AIR_POLLUTION_FORECAST_URL,
256 getRequestParams(handler.getOpenWeatherMapAPIConfig(), location))),
257 OpenWeatherMapJsonAirPollutionData.class);
261 * Downloads the icon for the given icon id (see https://openweathermap.org/weather-conditions).
263 * @param iconId the id of the icon
264 * @return the weather icon as {@link RawType}
266 public static @Nullable RawType getWeatherIcon(String iconId) {
267 if (iconId.isEmpty()) {
268 throw new IllegalArgumentException("Cannot download weather icon as icon id is null.");
271 return downloadWeatherIconFromCache(String.format(ICON_URL, iconId));
274 private static @Nullable RawType downloadWeatherIconFromCache(String url) {
275 if (IMAGE_CACHE.containsKey(url)) {
277 return new RawType(IMAGE_CACHE.get(url), PNG_CONTENT_TYPE);
278 } catch (Exception e) {
279 LoggerFactory.getLogger(OpenWeatherMapConnection.class)
280 .trace("Failed to download the content of URL '{}'", url, e);
283 RawType image = downloadWeatherIcon(url);
285 IMAGE_CACHE.put(url, image.getBytes());
292 private static @Nullable RawType downloadWeatherIcon(String url) {
293 return HttpUtil.downloadImage(url);
297 * Get Weather data from the One Call API for the given location. See https://openweathermap.org/api/one-call-api
300 * @param location location represented as {@link PointType}
301 * @param excludeMinutely if true, will not fetch minutely forecast data from the server
302 * @param excludeHourly if true, will not fetch hourly forecast data from the server
303 * @param excludeDaily if true, will not fetch hourly forecast data from the server
305 * @throws JsonSyntaxException
306 * @throws OpenWeatherMapCommunicationException
307 * @throws OpenWeatherMapConfigurationException
309 public synchronized @Nullable OpenWeatherMapOneCallAPIData getOneCallAPIData(@Nullable PointType location,
310 boolean excludeMinutely, boolean excludeHourly, boolean excludeDaily, boolean excludeAlerts)
311 throws JsonSyntaxException, OpenWeatherMapCommunicationException, OpenWeatherMapConfigurationException {
312 Map<String, String> params = getRequestParams(handler.getOpenWeatherMapAPIConfig(), location);
313 List<String> exclude = new ArrayList<>();
314 if (excludeMinutely) {
315 exclude.add("minutely");
318 exclude.add("hourly");
321 exclude.add("daily");
324 exclude.add("alerts");
326 logger.debug("Exclude: '{}'", exclude);
327 if (!exclude.isEmpty()) {
328 params.put(PARAM_EXCLUDE, exclude.stream().collect(Collectors.joining(",")));
330 return gson.fromJson(getResponseFromCache(buildURL(ONECALL_URL, params)), 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 OpenWeatherMapCommunicationException
343 * @throws OpenWeatherMapConfigurationException
345 public synchronized @Nullable OpenWeatherMapOneCallHistAPIData getOneCallHistAPIData(@Nullable PointType location,
347 throws JsonSyntaxException, OpenWeatherMapCommunicationException, OpenWeatherMapConfigurationException {
348 Map<String, String> params = getRequestParams(handler.getOpenWeatherMapAPIConfig(), location);
349 // the API requests the history as timestamp in Unix time format.
350 params.put(PARAM_HISTORY_DATE,
351 Long.toString(ZonedDateTime.now(ZoneId.of("UTC")).minusDays(days).toEpochSecond()));
352 return gson.fromJson(getResponseFromCache(buildURL(ONECALL_HISTORY_URL, params)),
353 OpenWeatherMapOneCallHistAPIData.class);
356 private Map<String, String> getRequestParams(OpenWeatherMapAPIConfiguration config, @Nullable PointType location) {
357 if (location == null) {
358 throw new OpenWeatherMapConfigurationException("@text/offline.conf-error-missing-location");
361 Map<String, String> params = new HashMap<>();
362 // API key (see https://openweathermap.org/appid)
363 String apikey = config.apikey;
364 if (apikey == null || (apikey = apikey.trim()).isEmpty()) {
365 throw new OpenWeatherMapConfigurationException("@text/offline.conf-error-missing-apikey");
367 params.put(PARAM_APPID, apikey);
369 // Units format (see https://openweathermap.org/current#data)
370 params.put(PARAM_UNITS, "metric");
372 // By geographic coordinates (see https://openweathermap.org/current#geo)
373 params.put(PARAM_LAT, location.getLatitude().toString());
374 params.put(PARAM_LON, location.getLongitude().toString());
376 // Multilingual support (see https://openweathermap.org/current#multi)
377 String language = config.language;
378 if (language != null && !(language = language.trim()).isEmpty()) {
379 params.put(PARAM_LANG, language.toLowerCase());
384 private String buildURL(String url, Map<String, String> requestParams) {
385 return requestParams.keySet().stream().map(key -> key + "=" + encodeParam(requestParams.get(key)))
386 .collect(Collectors.joining("&", url + "?", ""));
389 private String encodeParam(@Nullable String value) {
394 return URLEncoder.encode(value, StandardCharsets.UTF_8.name());
395 } catch (UnsupportedEncodingException e) {
396 logger.debug("UnsupportedEncodingException occurred during execution: {}", e.getLocalizedMessage(), e);
401 private @Nullable String getResponseFromCache(String url) {
402 return cache.putIfAbsentAndGet(url, () -> getResponse(url));
405 private String getResponse(String url) {
407 if (logger.isTraceEnabled()) {
408 logger.trace("OpenWeatherMap request: URL = '{}'", uglifyApikey(url));
410 ContentResponse contentResponse = httpClient.newRequest(url).method(GET).timeout(10, TimeUnit.SECONDS)
412 int httpStatus = contentResponse.getStatus();
413 String content = contentResponse.getContentAsString();
414 String errorMessage = "";
415 logger.trace("OpenWeatherMap response: status = {}, content = '{}'", httpStatus, content);
416 switch (httpStatus) {
419 case BAD_REQUEST_400:
420 case UNAUTHORIZED_401:
422 errorMessage = getErrorMessage(content);
423 logger.debug("OpenWeatherMap server responded with status code {}: {}", httpStatus, errorMessage);
424 throw new OpenWeatherMapConfigurationException(errorMessage);
425 case TOO_MANY_REQUESTS_429:
426 // TODO disable refresh job temporarily (see https://openweathermap.org/appid#Accesslimitation)
428 errorMessage = getErrorMessage(content);
429 logger.debug("OpenWeatherMap server responded with status code {}: {}", httpStatus, errorMessage);
430 throw new OpenWeatherMapCommunicationException(errorMessage);
432 } catch (ExecutionException e) {
433 String errorMessage = e.getLocalizedMessage();
434 logger.trace("Exception occurred during execution: {}", errorMessage, e);
435 if (e.getCause() instanceof HttpResponseException) {
436 logger.debug("OpenWeatherMap server responded with status code {}: Invalid API key.", UNAUTHORIZED_401);
437 throw new OpenWeatherMapConfigurationException("@text/offline.conf-error-invalid-apikey", e.getCause());
439 throw new OpenWeatherMapCommunicationException(errorMessage, e.getCause());
441 } catch (InterruptedException | TimeoutException e) {
442 logger.debug("Exception occurred during execution: {}", e.getLocalizedMessage(), e);
443 throw new OpenWeatherMapCommunicationException(e.getLocalizedMessage(), e.getCause());
447 private String uglifyApikey(String url) {
448 return url.replaceAll("(appid=)+\\w+", "appid=*****");
451 private String getErrorMessage(String response) {
452 JsonElement jsonResponse = JsonParser.parseString(response);
453 if (jsonResponse.isJsonObject()) {
454 JsonObject json = jsonResponse.getAsJsonObject();
455 if (json.has(PROPERTY_MESSAGE)) {
456 return json.get(PROPERTY_MESSAGE).getAsString();