From cc83021ae3da0ee50c173e54ab0ca0ff53f2c0c0 Mon Sep 17 00:00:00 2001 From: Daniele Gobbetti Date: Sat, 14 Apr 2018 19:42:05 +0200 Subject: [PATCH] Weather (and Pebble Webview): fix name and add wind information to weather data Wind information are stored and put in the reconstructed OWM response. A long standing bug (having the "name" field inside "main" instead of at the root level of the json) has been fixed Lineage OS receiver and if possible weather notification app receiver will be added in further commits. See #482 --- .../externalevents/OmniJawsObserver.java | 13 ++++++++++++- .../freeyourgadget/gadgetbridge/model/Weather.java | 8 +++++++- .../gadgetbridge/model/WeatherSpec.java | 3 +++ .../service/devices/pebble/webview/GBWebClient.java | 10 ++++++++++ 4 files changed, 32 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/OmniJawsObserver.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/OmniJawsObserver.java index 35bedcc20..83b21a2e9 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/OmniJawsObserver.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/OmniJawsObserver.java @@ -60,7 +60,9 @@ public class OmniJawsObserver extends ContentObserver { "forecast_condition", "forecast_condition_code", "time_stamp", - "forecast_date" + "forecast_date", + "wind_speed", + "wind_direction" }; private final String[] SETTINGS_PROJECTION = new String[]{ @@ -116,6 +118,9 @@ public class OmniJawsObserver extends ContentObserver { weatherSpec.currentTemp = toKelvin(c.getFloat(3)); weatherSpec.currentHumidity = (int) c.getFloat(4); + + weatherSpec.windSpeed = toKmh(c.getFloat(11)); + weatherSpec.windDirection = c.getInt(12); weatherSpec.timestamp = (int) (Long.valueOf(c.getString(9)) / 1000); } else if (i == 1) { weatherSpec.todayMinTemp = toKelvin(c.getFloat(5)); @@ -177,4 +182,10 @@ public class OmniJawsObserver extends ContentObserver { return (int) ((temperature - 32) * 0.5555555555555556D + 273.15); } + private float toKmh(float speed) { + if (mMetric) { + return speed; + } + return (speed * 1.61f); + } } \ No newline at end of file diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/Weather.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/Weather.java index 6159be3a6..347bef6e8 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/Weather.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/Weather.java @@ -45,6 +45,7 @@ public class Weather { JSONArray weather = new JSONArray(); JSONObject condition = new JSONObject(); JSONObject main = new JSONObject(); + JSONObject wind = new JSONObject(); try { condition.put("id", weatherSpec.currentConditionCode); @@ -53,14 +54,19 @@ public class Weather { condition.put("icon", Weather.mapToOpenWeatherMapIcon(weatherSpec.currentConditionCode)); weather.put(condition); + main.put("temp", weatherSpec.currentTemp); main.put("humidity", weatherSpec.currentHumidity); main.put("temp_min", weatherSpec.todayMinTemp); main.put("temp_max", weatherSpec.todayMaxTemp); - main.put("name", weatherSpec.location); + + wind.put("speed", (weatherSpec.windSpeed / 3.6f)); //meter per second + wind.put("deg", weatherSpec.windDirection); reconstructedOWMWeather.put("weather", weather); reconstructedOWMWeather.put("main", main); + reconstructedOWMWeather.put("name", weatherSpec.location); + reconstructedOWMWeather.put("wind", wind); } catch (JSONException e) { LOG.error("Error while reconstructing OWM weather reply"); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/WeatherSpec.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/WeatherSpec.java index 385cb6a2b..ce2072873 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/WeatherSpec.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/WeatherSpec.java @@ -42,6 +42,9 @@ public class WeatherSpec implements Parcelable { public int currentHumidity; public int todayMaxTemp; public int todayMinTemp; + public float windSpeed; //km per hour + public int windDirection; //deg + public ArrayList forecasts = new ArrayList<>(); public WeatherSpec() { diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/webview/GBWebClient.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/webview/GBWebClient.java index e8e49b4d6..b0a5d9453 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/webview/GBWebClient.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/webview/GBWebClient.java @@ -160,6 +160,8 @@ public class GBWebClient extends WebViewClient { JSONObject main = resp.getJSONObject("main"); convertTemps(main, units); //caller might want different units + JSONObject wind = resp.getJSONObject("wind"); + convertSpeeds(wind, units); resp.put("cod", 200); resp.put("coord", coordObject(currentPosition)); @@ -215,6 +217,14 @@ public class GBWebClient extends WebViewClient { return sys; } + private static void convertSpeeds(JSONObject wind, String units) throws JSONException { + if ("metric".equals(units)) { + wind.put("speed", (wind.getDouble("speed") * 3.6f) ); + } else if ("imperial".equals(units)) { //it's 2018... this is so sad + wind.put("speed", (wind.getDouble("speed") * 2.237f) ); + } + } + private static void convertTemps(JSONObject main, String units) throws JSONException { if ("metric".equals(units)) { main.put("temp", (int) main.get("temp") - 273);