From 477c27dcf0ba61f32b30700536a1a0bbe4d6ba17 Mon Sep 17 00:00:00 2001 From: Daniele Gobbetti Date: Sat, 14 Apr 2018 21:16:13 +0200 Subject: [PATCH] Weather: get wind speed and direction from the Weather Notification app The wind direction is approximate because we only get the quadrant, not a precise value --- .../notification/ParcelableWeather2.java | 30 ++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/ru/gelin/android/weather/notification/ParcelableWeather2.java b/app/src/main/java/ru/gelin/android/weather/notification/ParcelableWeather2.java index 5f5f5dd01..77e403dc9 100644 --- a/app/src/main/java/ru/gelin/android/weather/notification/ParcelableWeather2.java +++ b/app/src/main/java/ru/gelin/android/weather/notification/ParcelableWeather2.java @@ -56,6 +56,10 @@ public class ParcelableWeather2 implements Parcelable { conditionBundle.getStringArray("weather_condition_types"); weatherSpec.currentTemp = conditionBundle.getInt("weather_current_temp"); + weatherSpec.windDirection = mapDirToDeg(conditionBundle.getString("weather_wind_direction")); + weatherSpec.windSpeed = getSpeedInKMH(conditionBundle.getInt("weather_wind_speed"), + conditionBundle.getString("weather_wind_speed_unit")); + String[] currentConditionType = conditionBundle.getStringArray("weather_condition_types"); if (currentConditionType != null) { weatherSpec.currentConditionCode = weatherConditionTypesToOpenWeatherMapIds(currentConditionType[0]); @@ -254,4 +258,28 @@ public class ParcelableWeather2 implements Parcelable { return 3200; } -} + private int getSpeedInKMH(int speed, String incomingUnit) { + float kmhSpeed = 0; + switch (incomingUnit) { + case "MPS": + kmhSpeed = speed * 3.6f; + break; + case "MPH": + kmhSpeed = speed * 1.6093f; + break; + case "KPH": + kmhSpeed = speed; + break; + } + return Math.round(kmhSpeed); + } + + private int mapDirToDeg(String dir) { + return Math.round(WindDirection.valueOf(dir).ordinal() * 22.5f); + } + + private enum WindDirection { // see upstream code, we can't be more precise than getting the quadrant + N, NNE, NE, ENE, E, ESE, SE, SSE, S, SSW, SW, WSW, W, WNW, NW, NNW + } + +} \ No newline at end of file