From 5ac7c363c6c8438ff3dd7fe33156d42f80df13ec Mon Sep 17 00:00:00 2001 From: Chris Watts Date: Sat, 27 Jan 2018 12:20:53 +0000 Subject: [PATCH] Add WeatherCode implementation to forecast.io --- .../forecastio/ForecastIOCodeProvider.java | 33 ++++++++++++++++++- .../forecastio/ForecastIOWeatherProvider.java | 11 +++++-- 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/lib/src/main/java/com/survivingwithandroid/weather/lib/provider/forecastio/ForecastIOCodeProvider.java b/lib/src/main/java/com/survivingwithandroid/weather/lib/provider/forecastio/ForecastIOCodeProvider.java index f684249..3b6b1ff 100644 --- a/lib/src/main/java/com/survivingwithandroid/weather/lib/provider/forecastio/ForecastIOCodeProvider.java +++ b/lib/src/main/java/com/survivingwithandroid/weather/lib/provider/forecastio/ForecastIOCodeProvider.java @@ -24,11 +24,42 @@ * ForecastIO code provider. It converts the owm weather code to the unified code system provided by this library * * @author Francesco Azzola +* @author Chris Watts * */ public class ForecastIOCodeProvider implements IWeatherCodeProvider { @Override public WeatherCode getWeatherCode(String weatherCode) { - return null; + if (weatherCode == null) + return WeatherCode.NOT_AVAILABLE; + + if (weatherCode.equalsIgnoreCase("clear-day")) + return WeatherCode.SUNNY; + else if (weatherCode.equalsIgnoreCase("clear-night")) + return WeatherCode.CLEAR_NIGHT; + else if (weatherCode.equalsIgnoreCase("rain")) + return WeatherCode.SHOWERS; + else if (weatherCode.equalsIgnoreCase("snow")) + return WeatherCode.SNOW; + else if (weatherCode.equalsIgnoreCase("sleet")) + return WeatherCode.SLEET; + else if (weatherCode.equalsIgnoreCase("wind")) + return WeatherCode.WINDY; + else if (weatherCode.equalsIgnoreCase("fog")) + return WeatherCode.FOGGY; + else if (weatherCode.equalsIgnoreCase("cloudy")) + return WeatherCode.CLOUDY; + else if (weatherCode.equalsIgnoreCase("partly-cloudy-day")) + return WeatherCode.PARTLY_CLOUDY_DAY; + else if (weatherCode.equalsIgnoreCase("partly-cloudy-night")) + return WeatherCode.PARTLY_CLOUDY_NIGHT; + else if (weatherCode.equalsIgnoreCase("hail")) + return WeatherCode.HAIL; + else if (weatherCode.equalsIgnoreCase("thunderstorm")) + return WeatherCode.THUNDERSTORMS; + else if (weatherCode.equalsIgnoreCase("tornado")) + return WeatherCode.TORNADO; + else + return WeatherCode.NOT_AVAILABLE; } diff --git a/lib/src/main/java/com/survivingwithandroid/weather/lib/provider/forecastio/ForecastIOWeatherProvider.java b/lib/src/main/java/com/survivingwithandroid/weather/lib/provider/forecastio/ForecastIOWeatherProvider.java index d4f7e2d..8e3b8e3 100644 --- a/lib/src/main/java/com/survivingwithandroid/weather/lib/provider/forecastio/ForecastIOWeatherProvider.java +++ b/lib/src/main/java/com/survivingwithandroid/weather/lib/provider/forecastio/ForecastIOWeatherProvider.java @@ -50,6 +50,7 @@ public class ForecastIOWeatherProvider implements IWeatherProvider { private CurrentWeather cWeather; private WeatherHourForecast whf; private WeatherForecast forecast; + private IWeatherCodeProvider codeProvider; private long lastUpdate; @@ -136,7 +137,7 @@ public void setConfig(WeatherConfig config) { @Override public void setWeatherCodeProvider(IWeatherCodeProvider codeProvider) { - + this.codeProvider = codeProvider; } @Override @@ -243,7 +244,13 @@ private Weather parseWeather(JSONObject jsonWeather) throws JSONException { weather.currentCondition.setDescr(jsonWeather.optString("summary")); weather.currentCondition.setIcon(jsonWeather.optString("icon")); - + if (codeProvider != null) { + try { + weather.currentCondition.setWeatherCode(codeProvider.getWeatherCode(weather.currentCondition.getIcon())); + } catch (Throwable t) { + weather.currentCondition.setWeatherCode(WeatherCode.NOT_AVAILABLE); + } + } weather.rain[0].setAmmount((float) jsonWeather.optDouble("precipIntensity"));