Repository: incubator-unomi Updated Branches: refs/heads/master 7755bc402 -> 2f0c9edb2
Edit the Readme file, optimized the code Project: http://git-wip-us.apache.org/repos/asf/incubator-unomi/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-unomi/commit/0abd1437 Tree: http://git-wip-us.apache.org/repos/asf/incubator-unomi/tree/0abd1437 Diff: http://git-wip-us.apache.org/repos/asf/incubator-unomi/diff/0abd1437 Branch: refs/heads/master Commit: 0abd1437f856504bcd3c625217ae820919ab1b3c Parents: c94efbe Author: dinesh1996 <Dragonball21> Authored: Wed Aug 23 17:26:38 2017 +0200 Committer: dinesh1996 <Dragonball21> Committed: Wed Aug 23 17:26:38 2017 +0200 ---------------------------------------------------------------------- extensions/weather-update/README.md | 7 +- .../actions/WeatherUpdateAction.java | 161 ++++++++++--------- 2 files changed, 92 insertions(+), 76 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/0abd1437/extensions/weather-update/README.md ---------------------------------------------------------------------- diff --git a/extensions/weather-update/README.md b/extensions/weather-update/README.md index 3f89e23..0bf6e0b 100644 --- a/extensions/weather-update/README.md +++ b/extensions/weather-update/README.md @@ -31,13 +31,12 @@ This extension will retrieve the weather associated with the resolved location o https://home.openweathermap.org/api_keys -2. Configure the Apache Unomi Weather Update. In the etc/org.apache.unomi.weatherUpdate.cfg file +3. Configure the Apache Unomi Weather Update. In the etc/org.apache.unomi.weatherUpdate.cfg file change the following settings: weatherUpdate.apiKey=YOUR_WEATHER_APIKEY - - -3. Deploy into Apache Unomi using the following commands from the Apache Karaf shell: + +4. Deploy into Apache Unomi using the following commands from the Apache Karaf shell: feature:repo-add mvn:org.apache.unomi/unomi-weather-update-karaf-kar/${project.version}/xml/features feature:install unomi-weather-update-karaf-kar http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/0abd1437/extensions/weather-update/core/src/main/java/org/apache/unomi/weatherupdate/actions/WeatherUpdateAction.java ---------------------------------------------------------------------- diff --git a/extensions/weather-update/core/src/main/java/org/apache/unomi/weatherupdate/actions/WeatherUpdateAction.java b/extensions/weather-update/core/src/main/java/org/apache/unomi/weatherupdate/actions/WeatherUpdateAction.java index f5a5a5a..40031ab 100644 --- a/extensions/weather-update/core/src/main/java/org/apache/unomi/weatherupdate/actions/WeatherUpdateAction.java +++ b/extensions/weather-update/core/src/main/java/org/apache/unomi/weatherupdate/actions/WeatherUpdateAction.java @@ -44,8 +44,6 @@ import java.util.Map; */ public class WeatherUpdateAction implements ActionExecutor { - private static Logger logger = LoggerFactory.getLogger(WeatherUpdateAction.class); - private static final double KELVIN = 273.15; private static final double ROUND_TO_THE_TENTH = 0.5; private static final double SECOND_TO_HOUR = 3.6; @@ -60,7 +58,7 @@ public class WeatherUpdateAction implements ActionExecutor { private static final String WEATHER_LIKE = "weatherLike"; private static final String WEATHER_WIND_DIRECTION = "weatherWindDirection"; private static final String WEATHER_WIND_SPEED = "weatherWindSpeed"; - + private static Logger logger = LoggerFactory.getLogger(WeatherUpdateAction.class); private CloseableHttpClient httpClient; private String weatherApiKey; private String weatherUrlBase; @@ -73,74 +71,94 @@ public class WeatherUpdateAction implements ActionExecutor { } Session session = event.getSession(); - if (!(weatherApiKey == null || weatherUrlBase == null || weatherUrlAttributes == null)) { - Map<String, Object> sessionProperties = session.getProperties(); - if (sessionProperties.containsKey("location")) { - Map<String, Double> location = (Map<String, Double>) session.getProperty("location"); - HttpGet httpGet = new HttpGet(weatherUrlBase + "/" + weatherUrlAttributes + - "?lat=" + location.get("lat") + "&lon=" + location.get("lon") + "&appid=" + weatherApiKey); - JsonNode jsonNode = null; - CloseableHttpResponse response = null; - try { - response = httpClient.execute(httpGet); - if (response != null) { - HttpEntity entity = response.getEntity(); - String responseString; - if (entity != null) { - try { - responseString = EntityUtils.toString(entity); - ObjectMapper objectMapper = new ObjectMapper(); - jsonNode = objectMapper.readTree(responseString); - } catch (IOException e) { - logger.error("Error : With the API json response.", e.getMessage()); - } - } - } - } catch (IOException e) { - logger.error("Error : With the Http Request execution. Wrong parameters given", e.getMessage()); - } finally { - if (response != null) { - EntityUtils.consumeQuietly(response.getEntity()); - } - } + if (weatherApiKey == null || weatherUrlBase == null || weatherUrlAttributes == null) { + logger.warn("Configuration incomplete."); + return EventService.NO_CHANGE; + } + + Map<String, Object> sessionProperties = session.getProperties(); + if (!sessionProperties.containsKey("location")) { + logger.warn("No location info found in the session."); + return EventService.NO_CHANGE; + } + + Map<String, Double> location = (Map<String, Double>) session.getProperty("location"); - if (jsonNode.has(STATUS_CODE)) { - if (jsonNode.get(STATUS_CODE).asText().equals("200")) { - String temperature = extractTemperature(jsonNode); - String weatherLike = extractWeatherLike(jsonNode); - String windDirection = extractWindDirection(jsonNode); - String windSpeed = extractWindSpeed(jsonNode); - if (temperature != null) { - fillPropreties(session, WEATHER_TEMPERATURE, temperature); - } - if (weatherLike != null) { - fillPropreties(session, WEATHER_LIKE, weatherLike); - } - if (windDirection != null) { - fillPropreties(session, WEATHER_WIND_DIRECTION, windDirection); - } - if (windSpeed != null) { - fillPropreties(session, WEATHER_WIND_SPEED, windSpeed); - } - return EventService.SESSION_UPDATED; + JsonNode currentWeatherData = getWeather(location); + + + if (currentWeatherData.has(STATUS_CODE) && currentWeatherData.get(STATUS_CODE).asText().equals("200")) { + updateSessionWithWeatherData(currentWeatherData,session); + return EventService.SESSION_UPDATED; + }else { + if (currentWeatherData.has("message")) + logger.warn(currentWeatherData.get("message").asText()); + } + + logger.warn("No update made."); + return EventService.NO_CHANGE; + } + + private JsonNode getWeather(Map<String, Double> location) { + //Call to OpenWeatherMap + HttpGet httpGet = new HttpGet(weatherUrlBase + "/" + weatherUrlAttributes + + "?lat=" + location.get("lat") + "&lon=" + location.get("lon") + "&appid=" + weatherApiKey); + JsonNode currentWeatherData = null; + CloseableHttpResponse response = null; + try { + response = httpClient.execute(httpGet); + if (response != null) { + HttpEntity entity = response.getEntity(); + String responseString; + if (entity != null) { + try { + responseString = EntityUtils.toString(entity); + ObjectMapper objectMapper = new ObjectMapper(); + currentWeatherData = objectMapper.readTree(responseString); + } catch (IOException e) { + logger.error("Error : With the API json response.", e.getMessage()); } } } + } catch (IOException e) { + logger.error("Error : With the Http Request execution. Wrong parameters given", e.getMessage()); + } finally { + if (response != null) { + EntityUtils.consumeQuietly(response.getEntity()); + } + } + return currentWeatherData; + } + + private void updateSessionWithWeatherData(JsonNode currentWeatherData, Session session) { + String temperature = extractTemperature(currentWeatherData); + String weatherLike = extractWeatherLike(currentWeatherData); + String windDirection = extractWindDirection(currentWeatherData); + String windSpeed = extractWindSpeed(currentWeatherData); + if (temperature != null) { + fillPropreties(session, WEATHER_TEMPERATURE, temperature); + } + if (weatherLike != null) { + fillPropreties(session, WEATHER_LIKE, weatherLike); + } + if (windDirection != null) { + fillPropreties(session, WEATHER_WIND_DIRECTION, windDirection); + } + if (windSpeed != null) { + fillPropreties(session, WEATHER_WIND_SPEED, windSpeed); } - logger.info("No update made."); - return EventService.NO_CHANGE; } /** * Extract the temperature property from the response * - * @param jsonNode + * @param currentWeatherData * @return String temperature in celsius */ - private String extractTemperature(JsonNode jsonNode) { + private String extractTemperature(JsonNode currentWeatherData) { float temperature; - if (jsonNode.has(MAIN_INFO_WEATHER) && jsonNode.get(MAIN_INFO_WEATHER).has(TEMPERATURE_VALUE)) { - String responseString = jsonNode.get(MAIN_INFO_WEATHER).get(TEMPERATURE_VALUE).asText(); + if (currentWeatherData.has(MAIN_INFO_WEATHER) && currentWeatherData.get(MAIN_INFO_WEATHER).has(TEMPERATURE_VALUE)) { + String responseString = currentWeatherData.get(MAIN_INFO_WEATHER).get(TEMPERATURE_VALUE).asText(); temperature = Float.parseFloat(responseString); temperature -= KELVIN; int temperatureTreated = (int) temperature; @@ -157,13 +175,13 @@ public class WeatherUpdateAction implements ActionExecutor { /** * Extract the wind speed property from the response * - * @param jsonNode + * @param currentWeatherData * @return String wind speed in km/h */ - private String extractWindSpeed(JsonNode jsonNode) { + private String extractWindSpeed(JsonNode currentWeatherData) { JsonNode WindInfoSpeed; - if (jsonNode.has(WIND) && jsonNode.get(WIND).has(SPEED)) { - WindInfoSpeed = jsonNode.get(WIND).get(SPEED); + if (currentWeatherData.has(WIND) && currentWeatherData.get(WIND).has(SPEED)) { + WindInfoSpeed = currentWeatherData.get(WIND).get(SPEED); float speed = Float.parseFloat(WindInfoSpeed.toString()); speed *= SECOND_TO_HOUR; int speedTreated = (int) speed; @@ -178,15 +196,15 @@ public class WeatherUpdateAction implements ActionExecutor { /** * Extract the wind direction property from the response * - * @param jsonNode + * @param currentWeatherData * @return String wind direction in cardinal points format */ - private String extractWindDirection(JsonNode jsonNode) { + private String extractWindDirection(JsonNode currentWeatherData) { JsonNode windInfoDirection; String direction = ""; - if (jsonNode.has(WIND)) { - if (jsonNode.get(WIND).has(WIND_DIRECTION_INFO)) { - windInfoDirection = jsonNode.get(WIND).get(WIND_DIRECTION_INFO); + if (currentWeatherData.has(WIND)) { + if (currentWeatherData.get(WIND).has(WIND_DIRECTION_INFO)) { + windInfoDirection = currentWeatherData.get(WIND).get(WIND_DIRECTION_INFO); if (windInfoDirection != null) { float deg = Float.parseFloat(windInfoDirection.toString()); @@ -219,13 +237,13 @@ public class WeatherUpdateAction implements ActionExecutor { /** * Extract the weather like property from the response * - * @param jsonNode + * @param currentWeatherData * @return String weather like */ - private String extractWeatherLike(JsonNode jsonNode) { + private String extractWeatherLike(JsonNode currentWeatherData) { JsonNode weatherLike; - if (jsonNode.has(WEATHER_LIKE_INFO)) { - weatherLike = jsonNode.get(WEATHER_LIKE_INFO); + if (currentWeatherData.has(WEATHER_LIKE_INFO)) { + weatherLike = currentWeatherData.get(WEATHER_LIKE_INFO); if (weatherLike.size() > 0) { weatherLike = weatherLike.get(0).get(MAIN_INFO_WEATHER); logger.debug("Weather like: " + weatherLike); @@ -248,7 +266,6 @@ public class WeatherUpdateAction implements ActionExecutor { //Setters /** - * * Set the weatherApiKey * * @param weatherApiKey
