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 java.util.stream.Collectors.joining;
16 import static org.eclipse.jetty.http.HttpMethod.GET;
17 import static org.eclipse.jetty.http.HttpStatus.*;
19 import java.io.UnsupportedEncodingException;
20 import java.net.URLEncoder;
21 import java.nio.charset.StandardCharsets;
22 import java.time.ZoneId;
23 import java.time.ZonedDateTime;
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;
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.onecall.OpenWeatherMapOneCallAPIData;
44 import org.openhab.binding.openweathermap.internal.dto.onecallhist.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.io.net.http.HttpUtil;
49 import org.openhab.core.library.types.PointType;
50 import org.openhab.core.library.types.RawType;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
54 import com.google.gson.Gson;
55 import com.google.gson.JsonElement;
56 import com.google.gson.JsonObject;
57 import com.google.gson.JsonParser;
58 import com.google.gson.JsonSyntaxException;
61 * The {@link OpenWeatherMapConnection} is responsible for handling the connections to OpenWeatherMap API.
63 * @author Christoph Weitkamp - Initial contribution
66 public class OpenWeatherMapConnection {
68 private final Logger logger = LoggerFactory.getLogger(OpenWeatherMapConnection.class);
70 private static final String PROPERTY_MESSAGE = "message";
72 private static final String PNG_CONTENT_TYPE = "image/png";
74 private static final String PARAM_APPID = "appid";
75 private static final String PARAM_UNITS = "units";
76 private static final String PARAM_LAT = "lat";
77 private static final String PARAM_LON = "lon";
78 private static final String PARAM_LANG = "lang";
79 private static final String PARAM_FORECAST_CNT = "cnt";
80 private static final String PARAM_HISTORY_DATE = "dt";
81 private static final String PARAM_EXCLUDE = "exclude";
83 // Current weather data (see https://openweathermap.org/current)
84 private static final String WEATHER_URL = "https://api.openweathermap.org/data/2.5/weather";
85 // 5 day / 3 hour forecast (see https://openweathermap.org/forecast5)
86 private static final String THREE_HOUR_FORECAST_URL = "https://api.openweathermap.org/data/2.5/forecast";
87 // 16 day / daily forecast (see https://openweathermap.org/forecast16)
88 private static final String DAILY_FORECAST_URL = "https://api.openweathermap.org/data/2.5/forecast/daily";
89 // UV Index (see https://openweathermap.org/api/uvi)
90 private static final String UVINDEX_URL = "https://api.openweathermap.org/data/2.5/uvi";
91 private static final String UVINDEX_FORECAST_URL = "https://api.openweathermap.org/data/2.5/uvi/forecast";
92 // Air Pollution (see https://openweathermap.org/api/air-pollution)
93 private static final String AIR_POLLUTION_URL = "https://api.openweathermap.org/data/2.5/air_pollution";
94 private static final String AIR_POLLUTION_FORECAST_URL = "https://api.openweathermap.org/data/2.5/air_pollution/forecast";
95 // Weather icons (see https://openweathermap.org/weather-conditions)
96 private static final String ICON_URL = "https://openweathermap.org/img/w/%s.png";
97 // One Call API (see https://openweathermap.org/api/one-call-api )
98 private static final String ONECALL_URL = "https://api.openweathermap.org/data/2.5/onecall";
99 private static final String ONECALL_HISTORY_URL = "https://api.openweathermap.org/data/2.5/onecall/timemachine";
101 private final OpenWeatherMapAPIHandler handler;
102 private final HttpClient httpClient;
104 private static final ByteArrayFileCache IMAGE_CACHE = new ByteArrayFileCache("org.openhab.binding.openweathermap");
105 private final ExpiringCacheMap<String, String> cache;
107 private final Gson gson = new Gson();
109 public OpenWeatherMapConnection(OpenWeatherMapAPIHandler handler, HttpClient httpClient) {
110 this.handler = handler;
111 this.httpClient = httpClient;
113 OpenWeatherMapAPIConfiguration config = handler.getOpenWeatherMapAPIConfig();
114 cache = new ExpiringCacheMap<>(TimeUnit.MINUTES.toMillis(config.refreshInterval));
118 * Requests the current weather data for the given location (see https://openweathermap.org/current).
120 * @param location location represented as {@link PointType}
121 * @return the current weather data
122 * @throws JsonSyntaxException
123 * @throws OpenWeatherMapCommunicationException
124 * @throws OpenWeatherMapConfigurationException
126 public synchronized @Nullable OpenWeatherMapJsonWeatherData getWeatherData(@Nullable PointType location)
127 throws JsonSyntaxException, OpenWeatherMapCommunicationException, OpenWeatherMapConfigurationException {
128 return gson.fromJson(
129 getResponseFromCache(
130 buildURL(WEATHER_URL, getRequestParams(handler.getOpenWeatherMapAPIConfig(), location))),
131 OpenWeatherMapJsonWeatherData.class);
135 * Requests the hourly forecast data for the given location (see https://openweathermap.org/forecast5).
137 * @param location location represented as {@link PointType}
138 * @param count number of hours
139 * @return the hourly forecast data
140 * @throws JsonSyntaxException
141 * @throws OpenWeatherMapCommunicationException
142 * @throws OpenWeatherMapConfigurationException
144 public synchronized @Nullable OpenWeatherMapJsonHourlyForecastData getHourlyForecastData(
145 @Nullable PointType location, int count)
146 throws JsonSyntaxException, OpenWeatherMapCommunicationException, OpenWeatherMapConfigurationException {
148 throw new OpenWeatherMapConfigurationException("@text/offline.conf-error-not-supported-number-of-hours");
151 Map<String, String> params = getRequestParams(handler.getOpenWeatherMapAPIConfig(), location);
152 params.put(PARAM_FORECAST_CNT, Integer.toString(count));
154 return gson.fromJson(getResponseFromCache(buildURL(THREE_HOUR_FORECAST_URL, params)),
155 OpenWeatherMapJsonHourlyForecastData.class);
159 * Requests the daily forecast data for the given location (see https://openweathermap.org/forecast16).
161 * @param location location represented as {@link PointType}
162 * @param count number of days
163 * @return the daily forecast data
164 * @throws JsonSyntaxException
165 * @throws OpenWeatherMapCommunicationException
166 * @throws OpenWeatherMapConfigurationException
168 public synchronized @Nullable OpenWeatherMapJsonDailyForecastData getDailyForecastData(@Nullable PointType location,
170 throws JsonSyntaxException, OpenWeatherMapCommunicationException, OpenWeatherMapConfigurationException {
172 throw new OpenWeatherMapConfigurationException("@text/offline.conf-error-not-supported-number-of-days");
175 Map<String, String> params = getRequestParams(handler.getOpenWeatherMapAPIConfig(), location);
176 params.put(PARAM_FORECAST_CNT, Integer.toString(count));
178 return gson.fromJson(getResponseFromCache(buildURL(DAILY_FORECAST_URL, params)),
179 OpenWeatherMapJsonDailyForecastData.class);
183 * Requests the UV Index data for the given location (see https://openweathermap.org/api/uvi).
185 * @param location location represented as {@link PointType}
186 * @return the UV Index data
187 * @throws JsonSyntaxException
188 * @throws OpenWeatherMapCommunicationException
189 * @throws OpenWeatherMapConfigurationException
191 public synchronized @Nullable OpenWeatherMapJsonUVIndexData getUVIndexData(@Nullable PointType location)
192 throws JsonSyntaxException, OpenWeatherMapCommunicationException, OpenWeatherMapConfigurationException {
193 return gson.fromJson(
194 getResponseFromCache(
195 buildURL(UVINDEX_URL, getRequestParams(handler.getOpenWeatherMapAPIConfig(), location))),
196 OpenWeatherMapJsonUVIndexData.class);
200 * Requests the UV Index forecast data for the given location (see https://openweathermap.org/api/uvi).
202 * @param location location represented as {@link PointType}
203 * @return the UV Index forecast data
204 * @throws JsonSyntaxException
205 * @throws OpenWeatherMapCommunicationException
206 * @throws OpenWeatherMapConfigurationException
208 public synchronized @Nullable List<OpenWeatherMapJsonUVIndexData> getUVIndexForecastData(
209 @Nullable PointType location, int count)
210 throws JsonSyntaxException, OpenWeatherMapCommunicationException, OpenWeatherMapConfigurationException {
212 throw new OpenWeatherMapConfigurationException(
213 "@text/offline.conf-error-not-supported-uvindex-number-of-days");
216 Map<String, String> params = getRequestParams(handler.getOpenWeatherMapAPIConfig(), location);
217 params.put(PARAM_FORECAST_CNT, Integer.toString(count));
219 return Arrays.asList(gson.fromJson(getResponseFromCache(buildURL(UVINDEX_FORECAST_URL, params)),
220 OpenWeatherMapJsonUVIndexData[].class));
224 * Requests the Air Pollution data for the given location (see https://openweathermap.org/api/air-pollution).
226 * @param location location represented as {@link PointType}
227 * @return the Air Pollution data
228 * @throws JsonSyntaxException
229 * @throws OpenWeatherMapCommunicationException
230 * @throws OpenWeatherMapConfigurationException
232 public synchronized @Nullable OpenWeatherMapJsonAirPollutionData getAirPollutionData(@Nullable PointType location)
233 throws JsonSyntaxException, OpenWeatherMapCommunicationException, OpenWeatherMapConfigurationException {
234 return gson.fromJson(
235 getResponseFromCache(
236 buildURL(AIR_POLLUTION_URL, getRequestParams(handler.getOpenWeatherMapAPIConfig(), location))),
237 OpenWeatherMapJsonAirPollutionData.class);
241 * Requests the Air Pollution forecast data for the given location (see
242 * https://openweathermap.org/api/air-pollution).
244 * @param location location represented as {@link PointType}
245 * @return the Air Pollution forecast data
246 * @throws JsonSyntaxException
247 * @throws OpenWeatherMapCommunicationException
248 * @throws OpenWeatherMapConfigurationException
250 public synchronized @Nullable OpenWeatherMapJsonAirPollutionData getAirPollutionForecastData(
251 @Nullable PointType location)
252 throws JsonSyntaxException, OpenWeatherMapCommunicationException, OpenWeatherMapConfigurationException {
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 OneCall API for the given location. See https://openweathermap.org/api/one-call-api for
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 fethh 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 OpenWeatherMapCommunicationException
306 * @throws OpenWeatherMapConfigurationException
308 public synchronized @Nullable OpenWeatherMapOneCallAPIData getOneCallAPIData(@Nullable PointType location,
309 boolean excludeMinutely, boolean excludeHourly, boolean excludeDaily)
310 throws JsonSyntaxException, OpenWeatherMapCommunicationException, OpenWeatherMapConfigurationException {
311 Map<String, String> params = getRequestParams(handler.getOpenWeatherMapAPIConfig(), location);
312 StringBuilder exclude = new StringBuilder("");
313 if (excludeMinutely) {
314 exclude.append("minutely");
317 exclude.append(exclude.length() > 0 ? "," : "").append("hourly");
320 exclude.append(exclude.length() > 0 ? "," : "").append("daily");
322 logger.debug("Exclude: '{}'", exclude);
323 if (exclude.length() > 0) {
324 params.put(PARAM_EXCLUDE, exclude.toString());
326 return gson.fromJson(getResponseFromCache(buildURL(ONECALL_URL, params)), OpenWeatherMapOneCallAPIData.class);
330 * Get the historical weather data from the OneCall API for the given location and the given number of days in the
332 * As of now, OpenWeatherMap supports this function for up to 5 days in the past. However, this may change in the
334 * so we don't enforce this limit here. See https://openweathermap.org/api/one-call-api for details
336 * @param location location represented as {@link PointType}
337 * @param days number of days in the past, relative to the current time.
339 * @throws JsonSyntaxException
340 * @throws OpenWeatherMapCommunicationException
341 * @throws OpenWeatherMapConfigurationException
343 public synchronized @Nullable OpenWeatherMapOneCallHistAPIData getOneCallHistAPIData(@Nullable PointType location,
345 throws JsonSyntaxException, OpenWeatherMapCommunicationException, OpenWeatherMapConfigurationException {
346 Map<String, String> params = getRequestParams(handler.getOpenWeatherMapAPIConfig(), location);
347 // the API requests the history as timestamp in Unix time format.
348 params.put(PARAM_HISTORY_DATE,
349 Long.toString(ZonedDateTime.now(ZoneId.of("UTC")).minusDays(days).toEpochSecond()));
350 return gson.fromJson(getResponseFromCache(buildURL(ONECALL_HISTORY_URL, params)),
351 OpenWeatherMapOneCallHistAPIData.class);
354 private Map<String, String> getRequestParams(OpenWeatherMapAPIConfiguration config, @Nullable PointType location) {
355 if (location == null) {
356 throw new OpenWeatherMapConfigurationException("@text/offline.conf-error-missing-location");
359 Map<String, String> params = new HashMap<>();
360 // API key (see https://openweathermap.org/appid)
361 String apikey = config.apikey;
362 if (apikey == null || (apikey = apikey.trim()).isEmpty()) {
363 throw new OpenWeatherMapConfigurationException("@text/offline.conf-error-missing-apikey");
365 params.put(PARAM_APPID, apikey);
367 // Units format (see https://openweathermap.org/current#data)
368 params.put(PARAM_UNITS, "metric");
370 // By geographic coordinates (see https://openweathermap.org/current#geo)
371 params.put(PARAM_LAT, location.getLatitude().toString());
372 params.put(PARAM_LON, location.getLongitude().toString());
374 // Multilingual support (see https://openweathermap.org/current#multi)
375 String language = config.language;
376 if (language != null && !(language = language.trim()).isEmpty()) {
377 params.put(PARAM_LANG, language.toLowerCase());
382 private String buildURL(String url, Map<String, String> requestParams) {
383 return requestParams.keySet().stream().map(key -> key + "=" + encodeParam(requestParams.get(key)))
384 .collect(joining("&", url + "?", ""));
387 private String encodeParam(@Nullable String value) {
392 return URLEncoder.encode(value, StandardCharsets.UTF_8.name());
393 } catch (UnsupportedEncodingException e) {
394 logger.debug("UnsupportedEncodingException occurred during execution: {}", e.getLocalizedMessage(), e);
399 private @Nullable String getResponseFromCache(String url) {
400 return cache.putIfAbsentAndGet(url, () -> getResponse(url));
403 private String getResponse(String url) {
405 if (logger.isTraceEnabled()) {
406 logger.trace("OpenWeatherMap request: URL = '{}'", uglifyApikey(url));
408 ContentResponse contentResponse = httpClient.newRequest(url).method(GET).timeout(10, TimeUnit.SECONDS)
410 int httpStatus = contentResponse.getStatus();
411 String content = contentResponse.getContentAsString();
412 String errorMessage = "";
413 logger.trace("OpenWeatherMap response: status = {}, content = '{}'", httpStatus, content);
414 switch (httpStatus) {
417 case BAD_REQUEST_400:
418 case UNAUTHORIZED_401:
420 errorMessage = getErrorMessage(content);
421 logger.debug("OpenWeatherMap server responded with status code {}: {}", httpStatus, errorMessage);
422 throw new OpenWeatherMapConfigurationException(errorMessage);
423 case TOO_MANY_REQUESTS_429:
424 // TODO disable refresh job temporarily (see https://openweathermap.org/appid#Accesslimitation)
426 errorMessage = getErrorMessage(content);
427 logger.debug("OpenWeatherMap server responded with status code {}: {}", httpStatus, errorMessage);
428 throw new OpenWeatherMapCommunicationException(errorMessage);
430 } catch (ExecutionException e) {
431 String errorMessage = e.getLocalizedMessage();
432 logger.trace("Exception occurred during execution: {}", errorMessage, e);
433 if (e.getCause() instanceof HttpResponseException) {
434 logger.debug("OpenWeatherMap server responded with status code {}: Invalid API key.", UNAUTHORIZED_401);
435 throw new OpenWeatherMapConfigurationException("@text/offline.conf-error-invalid-apikey", e.getCause());
437 throw new OpenWeatherMapCommunicationException(errorMessage, e.getCause());
439 } catch (InterruptedException | TimeoutException e) {
440 logger.debug("Exception occurred during execution: {}", e.getLocalizedMessage(), e);
441 throw new OpenWeatherMapCommunicationException(e.getLocalizedMessage(), e.getCause());
445 private String uglifyApikey(String url) {
446 return url.replaceAll("(appid=)+\\w+", "appid=*****");
449 private String getErrorMessage(String response) {
450 JsonElement jsonResponse = JsonParser.parseString(response);
451 if (jsonResponse.isJsonObject()) {
452 JsonObject json = jsonResponse.getAsJsonObject();
453 if (json.has(PROPERTY_MESSAGE)) {
454 return json.get(PROPERTY_MESSAGE).getAsString();