2 * Copyright (c) 2010-2020 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.OpenWeatherMapJsonDailyForecastData;
39 import org.openhab.binding.openweathermap.internal.dto.OpenWeatherMapJsonHourlyForecastData;
40 import org.openhab.binding.openweathermap.internal.dto.OpenWeatherMapJsonUVIndexData;
41 import org.openhab.binding.openweathermap.internal.dto.OpenWeatherMapJsonWeatherData;
42 import org.openhab.binding.openweathermap.internal.dto.onecall.OpenWeatherMapOneCallAPIData;
43 import org.openhab.binding.openweathermap.internal.dto.onecallhist.OpenWeatherMapOneCallHistAPIData;
44 import org.openhab.binding.openweathermap.internal.handler.OpenWeatherMapAPIHandler;
45 import org.openhab.core.cache.ByteArrayFileCache;
46 import org.openhab.core.cache.ExpiringCacheMap;
47 import org.openhab.core.io.net.http.HttpUtil;
48 import org.openhab.core.library.types.PointType;
49 import org.openhab.core.library.types.RawType;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
53 import com.google.gson.Gson;
54 import com.google.gson.JsonElement;
55 import com.google.gson.JsonObject;
56 import com.google.gson.JsonParser;
57 import com.google.gson.JsonSyntaxException;
60 * The {@link OpenWeatherMapConnection} is responsible for handling the connections to OpenWeatherMap API.
62 * @author Christoph Weitkamp - Initial contribution
65 public class OpenWeatherMapConnection {
67 private final Logger logger = LoggerFactory.getLogger(OpenWeatherMapConnection.class);
69 private static final String PROPERTY_MESSAGE = "message";
71 private static final String PNG_CONTENT_TYPE = "image/png";
73 private static final String PARAM_APPID = "appid";
74 private static final String PARAM_UNITS = "units";
75 private static final String PARAM_LAT = "lat";
76 private static final String PARAM_LON = "lon";
77 private static final String PARAM_LANG = "lang";
78 private static final String PARAM_FORECAST_CNT = "cnt";
79 private static final String PARAM_HISTORY_DATE = "dt";
80 private static final String PARAM_EXCLUDE = "exclude";
82 // Current weather data (see https://openweathermap.org/current)
83 private static final String WEATHER_URL = "https://api.openweathermap.org/data/2.5/weather";
84 // 5 day / 3 hour forecast (see https://openweathermap.org/forecast5)
85 private static final String THREE_HOUR_FORECAST_URL = "https://api.openweathermap.org/data/2.5/forecast";
86 // 16 day / daily forecast (see https://openweathermap.org/forecast16)
87 private static final String DAILY_FORECAST_URL = "https://api.openweathermap.org/data/2.5/forecast/daily";
88 // UV Index (see https://openweathermap.org/api/uvi)
89 private static final String UVINDEX_URL = "https://api.openweathermap.org/data/2.5/uvi";
90 private static final String UVINDEX_FORECAST_URL = "https://api.openweathermap.org/data/2.5/uvi/forecast";
91 // Weather icons (see https://openweathermap.org/weather-conditions)
92 private static final String ICON_URL = "https://openweathermap.org/img/w/%s.png";
93 // One Call API (see https://openweathermap.org/api/one-call-api )
94 private static final String ONECALL_URL = "https://api.openweathermap.org/data/2.5/onecall";
95 private static final String ONECALL_HISTORY_URL = "https://api.openweathermap.org/data/2.5/onecall/timemachine";
97 private final OpenWeatherMapAPIHandler handler;
98 private final HttpClient httpClient;
100 private static final ByteArrayFileCache IMAGE_CACHE = new ByteArrayFileCache("org.openhab.binding.openweathermap");
101 private final ExpiringCacheMap<String, String> cache;
103 private final JsonParser parser = new JsonParser();
104 private final Gson gson = new Gson();
106 public OpenWeatherMapConnection(OpenWeatherMapAPIHandler handler, HttpClient httpClient) {
107 this.handler = handler;
108 this.httpClient = httpClient;
110 OpenWeatherMapAPIConfiguration config = handler.getOpenWeatherMapAPIConfig();
111 cache = new ExpiringCacheMap<>(TimeUnit.MINUTES.toMillis(config.refreshInterval));
115 * Requests the current weather data for the given location (see https://openweathermap.org/current).
117 * @param location location represented as {@link PointType}
118 * @return the current weather data
119 * @throws JsonSyntaxException
120 * @throws OpenWeatherMapCommunicationException
121 * @throws OpenWeatherMapConfigurationException
123 public synchronized @Nullable OpenWeatherMapJsonWeatherData getWeatherData(@Nullable PointType location)
124 throws JsonSyntaxException, OpenWeatherMapCommunicationException, OpenWeatherMapConfigurationException {
125 return gson.fromJson(
126 getResponseFromCache(
127 buildURL(WEATHER_URL, getRequestParams(handler.getOpenWeatherMapAPIConfig(), location))),
128 OpenWeatherMapJsonWeatherData.class);
132 * Requests the hourly forecast data for the given location (see https://openweathermap.org/forecast5).
134 * @param location location represented as {@link PointType}
135 * @param count number of hours
136 * @return the hourly forecast data
137 * @throws JsonSyntaxException
138 * @throws OpenWeatherMapCommunicationException
139 * @throws OpenWeatherMapConfigurationException
141 public synchronized @Nullable OpenWeatherMapJsonHourlyForecastData getHourlyForecastData(
142 @Nullable PointType location, int count)
143 throws JsonSyntaxException, OpenWeatherMapCommunicationException, OpenWeatherMapConfigurationException {
145 throw new OpenWeatherMapConfigurationException("@text/offline.conf-error-not-supported-number-of-hours");
148 Map<String, String> params = getRequestParams(handler.getOpenWeatherMapAPIConfig(), location);
149 params.put(PARAM_FORECAST_CNT, Integer.toString(count));
151 return gson.fromJson(getResponseFromCache(buildURL(THREE_HOUR_FORECAST_URL, params)),
152 OpenWeatherMapJsonHourlyForecastData.class);
156 * Requests the daily forecast data for the given location (see https://openweathermap.org/forecast16).
158 * @param location location represented as {@link PointType}
159 * @param count number of days
160 * @return the daily forecast data
161 * @throws JsonSyntaxException
162 * @throws OpenWeatherMapCommunicationException
163 * @throws OpenWeatherMapConfigurationException
165 public synchronized @Nullable OpenWeatherMapJsonDailyForecastData getDailyForecastData(@Nullable PointType location,
167 throws JsonSyntaxException, OpenWeatherMapCommunicationException, OpenWeatherMapConfigurationException {
169 throw new OpenWeatherMapConfigurationException("@text/offline.conf-error-not-supported-number-of-days");
172 Map<String, String> params = getRequestParams(handler.getOpenWeatherMapAPIConfig(), location);
173 params.put(PARAM_FORECAST_CNT, Integer.toString(count));
175 return gson.fromJson(getResponseFromCache(buildURL(DAILY_FORECAST_URL, params)),
176 OpenWeatherMapJsonDailyForecastData.class);
180 * Requests the UV Index data for the given location (see https://api.openweathermap.org/data/2.5/uvi).
182 * @param location location represented as {@link PointType}
183 * @return the UV Index data
184 * @throws JsonSyntaxException
185 * @throws OpenWeatherMapCommunicationException
186 * @throws OpenWeatherMapConfigurationException
188 public synchronized @Nullable OpenWeatherMapJsonUVIndexData getUVIndexData(@Nullable PointType location)
189 throws JsonSyntaxException, OpenWeatherMapCommunicationException, OpenWeatherMapConfigurationException {
190 return gson.fromJson(
191 getResponseFromCache(
192 buildURL(UVINDEX_URL, getRequestParams(handler.getOpenWeatherMapAPIConfig(), location))),
193 OpenWeatherMapJsonUVIndexData.class);
197 * Requests the UV Index forecast data for the given location (see https://api.openweathermap.org/data/2.5/uvi).
199 * @param location location represented as {@link PointType}
200 * @return the UV Index forecast data
201 * @throws JsonSyntaxException
202 * @throws OpenWeatherMapCommunicationException
203 * @throws OpenWeatherMapConfigurationException
205 public synchronized @Nullable List<OpenWeatherMapJsonUVIndexData> getUVIndexForecastData(
206 @Nullable PointType location, int count)
207 throws JsonSyntaxException, OpenWeatherMapCommunicationException, OpenWeatherMapConfigurationException {
209 throw new OpenWeatherMapConfigurationException(
210 "@text/offline.conf-error-not-supported-uvindex-number-of-days");
213 Map<String, String> params = getRequestParams(handler.getOpenWeatherMapAPIConfig(), location);
214 params.put(PARAM_FORECAST_CNT, Integer.toString(count));
216 return Arrays.asList(gson.fromJson(getResponseFromCache(buildURL(UVINDEX_FORECAST_URL, params)),
217 OpenWeatherMapJsonUVIndexData[].class));
221 * Downloads the icon for the given icon id (see https://openweathermap.org/weather-conditions).
223 * @param iconId the id of the icon
224 * @return the weather icon as {@link RawType}
226 public static @Nullable RawType getWeatherIcon(String iconId) {
227 if (iconId.isEmpty()) {
228 throw new IllegalArgumentException("Cannot download weather icon as icon id is null.");
231 return downloadWeatherIconFromCache(String.format(ICON_URL, iconId));
234 private static @Nullable RawType downloadWeatherIconFromCache(String url) {
235 if (IMAGE_CACHE.containsKey(url)) {
237 return new RawType(IMAGE_CACHE.get(url), PNG_CONTENT_TYPE);
238 } catch (Exception e) {
239 LoggerFactory.getLogger(OpenWeatherMapConnection.class)
240 .trace("Failed to download the content of URL '{}'", url, e);
243 RawType image = downloadWeatherIcon(url);
245 IMAGE_CACHE.put(url, image.getBytes());
252 private static @Nullable RawType downloadWeatherIcon(String url) {
253 return HttpUtil.downloadImage(url);
257 * Get Weather data from the OneCall API for the given location. See https://openweathermap.org/api/one-call-api for
260 * @param location location represented as {@link PointType}
261 * @param excludeMinutely if true, will not fetch minutely forecast data from the server
262 * @param excludeHourly if true, will not fethh hourly forecast data from the server
263 * @param excludeDaily if true, will not fetch hourly forecast data from the server
265 * @throws JsonSyntaxException
266 * @throws OpenWeatherMapCommunicationException
267 * @throws OpenWeatherMapConfigurationException
269 public synchronized @Nullable OpenWeatherMapOneCallAPIData getOneCallAPIData(@Nullable PointType location,
270 boolean excludeMinutely, boolean excludeHourly, boolean excludeDaily)
271 throws JsonSyntaxException, OpenWeatherMapCommunicationException, OpenWeatherMapConfigurationException {
272 Map<String, String> params = getRequestParams(handler.getOpenWeatherMapAPIConfig(), location);
273 StringBuilder exclude = new StringBuilder("");
274 if (excludeMinutely) {
275 exclude.append("minutely");
278 exclude.append(exclude.length() > 0 ? "," : "").append("hourly");
281 exclude.append(exclude.length() > 0 ? "," : "").append("daily");
283 logger.debug("Exclude: '{}'", exclude);
284 if (exclude.length() > 0) {
285 params.put(PARAM_EXCLUDE, exclude.toString());
287 return gson.fromJson(getResponseFromCache(buildURL(ONECALL_URL, params)), OpenWeatherMapOneCallAPIData.class);
291 * Get the historical weather data from the OneCall API for the given location and the given number of days in the
293 * As of now, OpenWeatherMap supports this function for up to 5 days in the past. However, this may change in the
295 * so we don't enforce this limit here. See https://openweathermap.org/api/one-call-api for details
297 * @param location location represented as {@link PointType}
298 * @param days number of days in the past, relative to the current time.
300 * @throws JsonSyntaxException
301 * @throws OpenWeatherMapCommunicationException
302 * @throws OpenWeatherMapConfigurationException
304 public synchronized @Nullable OpenWeatherMapOneCallHistAPIData getOneCallHistAPIData(@Nullable PointType location,
306 throws JsonSyntaxException, OpenWeatherMapCommunicationException, OpenWeatherMapConfigurationException {
307 Map<String, String> params = getRequestParams(handler.getOpenWeatherMapAPIConfig(), location);
308 // the API requests the history as timestamp in Unix time format.
309 params.put(PARAM_HISTORY_DATE,
310 Long.toString(ZonedDateTime.now(ZoneId.of("UTC")).minusDays(days).toEpochSecond()));
311 return gson.fromJson(getResponseFromCache(buildURL(ONECALL_HISTORY_URL, params)),
312 OpenWeatherMapOneCallHistAPIData.class);
315 private Map<String, String> getRequestParams(OpenWeatherMapAPIConfiguration config, @Nullable PointType location) {
316 if (location == null) {
317 throw new OpenWeatherMapConfigurationException("@text/offline.conf-error-missing-location");
320 Map<String, String> params = new HashMap<>();
321 // API key (see http://openweathermap.org/appid)
322 String apikey = config.apikey;
323 if (apikey == null || (apikey = apikey.trim()).isEmpty()) {
324 throw new OpenWeatherMapConfigurationException("@text/offline.conf-error-missing-apikey");
326 params.put(PARAM_APPID, apikey);
328 // Units format (see https://openweathermap.org/current#data)
329 params.put(PARAM_UNITS, "metric");
331 // By geographic coordinates (see https://openweathermap.org/current#geo)
332 params.put(PARAM_LAT, location.getLatitude().toString());
333 params.put(PARAM_LON, location.getLongitude().toString());
335 // Multilingual support (see https://openweathermap.org/current#multi)
336 String language = config.language;
337 if (language != null && !(language = language.trim()).isEmpty()) {
338 params.put(PARAM_LANG, language.toLowerCase());
343 private String buildURL(String url, Map<String, String> requestParams) {
344 return requestParams.keySet().stream().map(key -> key + "=" + encodeParam(requestParams.get(key)))
345 .collect(joining("&", url + "?", ""));
348 private String encodeParam(@Nullable String value) {
353 return URLEncoder.encode(value, StandardCharsets.UTF_8.name());
354 } catch (UnsupportedEncodingException e) {
355 logger.debug("UnsupportedEncodingException occurred during execution: {}", e.getLocalizedMessage(), e);
360 private @Nullable String getResponseFromCache(String url) {
361 return cache.putIfAbsentAndGet(url, () -> getResponse(url));
364 private String getResponse(String url) {
366 if (logger.isTraceEnabled()) {
367 logger.trace("OpenWeatherMap request: URL = '{}'", uglifyApikey(url));
369 ContentResponse contentResponse = httpClient.newRequest(url).method(GET).timeout(10, TimeUnit.SECONDS)
371 int httpStatus = contentResponse.getStatus();
372 String content = contentResponse.getContentAsString();
373 String errorMessage = "";
374 logger.trace("OpenWeatherMap response: status = {}, content = '{}'", httpStatus, content);
375 switch (httpStatus) {
378 case BAD_REQUEST_400:
379 case UNAUTHORIZED_401:
381 errorMessage = getErrorMessage(content);
382 logger.debug("OpenWeatherMap server responded with status code {}: {}", httpStatus, errorMessage);
383 throw new OpenWeatherMapConfigurationException(errorMessage);
384 case TOO_MANY_REQUESTS_429:
385 // TODO disable refresh job temporarily (see https://openweathermap.org/appid#Accesslimitation)
387 errorMessage = getErrorMessage(content);
388 logger.debug("OpenWeatherMap server responded with status code {}: {}", httpStatus, errorMessage);
389 throw new OpenWeatherMapCommunicationException(errorMessage);
391 } catch (ExecutionException e) {
392 String errorMessage = e.getLocalizedMessage();
393 logger.trace("Exception occurred during execution: {}", errorMessage, e);
394 if (e.getCause() instanceof HttpResponseException) {
395 logger.debug("OpenWeatherMap server responded with status code {}: Invalid API key.", UNAUTHORIZED_401);
396 throw new OpenWeatherMapConfigurationException("@text/offline.conf-error-invalid-apikey", e.getCause());
398 throw new OpenWeatherMapCommunicationException(errorMessage, e.getCause());
400 } catch (InterruptedException | TimeoutException e) {
401 logger.debug("Exception occurred during execution: {}", e.getLocalizedMessage(), e);
402 throw new OpenWeatherMapCommunicationException(e.getLocalizedMessage(), e.getCause());
406 private String uglifyApikey(String url) {
407 return url.replaceAll("(appid=)+\\w+", "appid=*****");
410 private String getErrorMessage(String response) {
411 JsonElement jsonResponse = parser.parse(response);
412 if (jsonResponse.isJsonObject()) {
413 JsonObject json = jsonResponse.getAsJsonObject();
414 if (json.has(PROPERTY_MESSAGE)) {
415 return json.get(PROPERTY_MESSAGE).getAsString();