From 948002dc7c231090148f92c944026dd8bd20df74 Mon Sep 17 00:00:00 2001 From: Andreas Shimokawa Date: Thu, 14 Dec 2017 14:54:09 +0100 Subject: [PATCH] Weather: recreate OWM fake reply from weatherSpec, no not directly create in in Weather Notification receiver This should bring CM weather on par with Weather Notficication when using background js --- .../externalevents/CMWeatherReceiver.java | 1 - .../WeatherNotificationReceiver.java | 1 - .../gadgetbridge/model/Weather.java | 41 ++++++++++++++++--- .../gadgetbridge/model/WeatherSpec.java | 9 +++- .../devices/pebble/webview/GBWebClient.java | 7 +--- .../notification/ParcelableWeather2.java | 36 ++++------------ 6 files changed, 52 insertions(+), 43 deletions(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/CMWeatherReceiver.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/CMWeatherReceiver.java index e0daa3729..c70d250f5 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/CMWeatherReceiver.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/CMWeatherReceiver.java @@ -132,7 +132,6 @@ public class CMWeatherReceiver extends BroadcastReceiver implements CMWeatherMan weatherSpec.todayMaxTemp = (int) weatherInfo.getTodaysHigh() + 273; weatherSpec.todayMinTemp = (int) weatherInfo.getTodaysLow() + 273; } - weatherSpec.currentConditionCode = Weather.mapToOpenWeatherMapCondition(CMtoYahooCondintion(weatherInfo.getConditionCode())); weatherSpec.currentCondition = Weather.getConditionString(weatherSpec.currentConditionCode); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/WeatherNotificationReceiver.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/WeatherNotificationReceiver.java index 102bd22a5..f016b61e5 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/WeatherNotificationReceiver.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/WeatherNotificationReceiver.java @@ -48,7 +48,6 @@ public class WeatherNotificationReceiver extends BroadcastReceiver { if (parcelableWeather2 != null) { Weather weather = Weather.getInstance(); - weather.setReconstructedOWMWeather(parcelableWeather2.reconstructedOWMWeather); weather.setReconstructedOWMForecast(parcelableWeather2.reconstructedOWMForecast); WeatherSpec weatherSpec = parcelableWeather2.weatherSpec; 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 fd758aa86..410804503 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/Weather.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/Weather.java @@ -16,12 +16,17 @@ along with this program. If not, see . */ package nodomain.freeyourgadget.gadgetbridge.model; +import org.json.JSONArray; +import org.json.JSONException; import org.json.JSONObject; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class Weather { + private static final Logger LOG = LoggerFactory.getLogger(Weather.class); + private WeatherSpec weatherSpec = null; - private JSONObject reconstructedOWMWeather = null; private JSONObject reconstructedOWMForecast = null; public WeatherSpec getWeatherSpec() { @@ -32,12 +37,36 @@ public class Weather { this.weatherSpec = weatherSpec; } - public JSONObject getReconstructedOWMWeather() { - return reconstructedOWMWeather; - } + public JSONObject createReconstructedOWMWeatherReply() { + if (weatherSpec == null) { + return null; + } + JSONObject reconstructedOWMWeather = new JSONObject(); + JSONArray weather = new JSONArray(); + JSONObject condition = new JSONObject(); + JSONObject main = new JSONObject(); - public void setReconstructedOWMWeather(JSONObject reconstructedOWMWeather) { - this.reconstructedOWMWeather = reconstructedOWMWeather; + try { + condition.put("id", weatherSpec.currentConditionCode); + condition.put("main", weatherSpec.currentCondition); + 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); + + reconstructedOWMWeather.put("weather", weather); + reconstructedOWMWeather.put("main", main); + + } catch (JSONException e) { + LOG.error("Error while reconstructing OWM weather reply"); + return null; + } + LOG.debug("Weather JSON for WEBVIEW: " + reconstructedOWMWeather.toString()); + return reconstructedOWMWeather; } public JSONObject getReconstructedOWMForecast() { 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 8ea43c05d..5f84eb423 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/WeatherSpec.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/WeatherSpec.java @@ -39,6 +39,7 @@ public class WeatherSpec implements Parcelable { public int currentTemp; public int currentConditionCode = 3200; public String currentCondition; + public int currentHumidity; public int todayMaxTemp; public int todayMinTemp; public ArrayList forecasts = new ArrayList<>(); @@ -53,6 +54,7 @@ public class WeatherSpec implements Parcelable { currentTemp = in.readInt(); currentConditionCode = in.readInt(); currentCondition = in.readString(); + currentHumidity = in.readInt(); todayMaxTemp = in.readInt(); todayMinTemp = in.readInt(); in.readList(forecasts, Forecast.class.getClassLoader()); @@ -70,6 +72,7 @@ public class WeatherSpec implements Parcelable { dest.writeInt(currentTemp); dest.writeInt(currentConditionCode); dest.writeString(currentCondition); + dest.writeInt(currentHumidity); dest.writeInt(todayMaxTemp); dest.writeInt(todayMinTemp); dest.writeList(forecasts); @@ -90,20 +93,23 @@ public class WeatherSpec implements Parcelable { public int minTemp; public int maxTemp; public int conditionCode; + public int humidity; public Forecast() { } - public Forecast(int minTemp, int maxTemp, int conditionCode) { + public Forecast(int minTemp, int maxTemp, int conditionCode, int humidity) { this.minTemp = minTemp; this.maxTemp = maxTemp; this.conditionCode = conditionCode; + this.humidity = humidity; } Forecast(Parcel in) { minTemp = in.readInt(); maxTemp = in.readInt(); conditionCode = in.readInt(); + humidity = in.readInt(); } @Override @@ -116,6 +122,7 @@ public class WeatherSpec implements Parcelable { dest.writeInt(minTemp); dest.writeInt(maxTemp); dest.writeInt(conditionCode); + dest.writeInt(humidity); } } } 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 311c33dca..ca0ecaf44 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 @@ -140,11 +140,8 @@ public class GBWebClient extends WebViewClient { CurrentPosition currentPosition = new CurrentPosition(); try { - JSONObject resp; - - if ("/data/2.5/weather".equals(type) && Weather.getInstance().getReconstructedOWMWeather() != null) { - resp = new JSONObject(Weather.getInstance().getReconstructedOWMWeather().toString()); - + JSONObject resp = Weather.getInstance().createReconstructedOWMWeatherReply(); + if ("/data/2.5/weather".equals(type) && resp != null) { JSONObject main = resp.getJSONObject("main"); convertTemps(main, units); //caller might want different units 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 6707939f5..d30a2707a 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 @@ -35,7 +35,6 @@ public class ParcelableWeather2 implements Parcelable { // getters and setters suck ;) public WeatherSpec weatherSpec = new WeatherSpec(); - public JSONObject reconstructedOWMWeather = null; public JSONObject reconstructedOWMForecast = null; private ParcelableWeather2(Parcel in) { @@ -53,11 +52,6 @@ public class ParcelableWeather2 implements Parcelable { int conditions = bundle.getInt("weather_conditions"); if (conditions > 0) { Bundle conditionBundle = in.readBundle(getClass().getClassLoader()); - reconstructedOWMWeather = new JSONObject(); - JSONArray weather = new JSONArray(); - JSONObject condition = new JSONObject(); - JSONObject main = new JSONObject(); - weatherSpec.currentCondition = conditionBundle.getString("weather_condition_text"); conditionBundle.getStringArray("weather_condition_types"); weatherSpec.currentTemp = conditionBundle.getInt("weather_current_temp"); @@ -68,25 +62,8 @@ public class ParcelableWeather2 implements Parcelable { } weatherSpec.todayMinTemp = conditionBundle.getInt("weather_low_temp"); weatherSpec.todayMaxTemp = conditionBundle.getInt("weather_high_temp"); - try { - condition.put("id", weatherSpec.currentConditionCode); - condition.put("main", weatherSpec.currentCondition); - condition.put("icon", Weather.mapToOpenWeatherMapIcon(weatherSpec.currentConditionCode)); - weather.put(condition); + weatherSpec.currentHumidity = conditionBundle.getInt("weather_humidity_value"); - main.put("temp", weatherSpec.currentTemp); - main.put("humidity", conditionBundle.getInt("weather_humidity_value")); - main.put("temp_min", weatherSpec.todayMinTemp); - main.put("temp_max", weatherSpec.todayMaxTemp); - main.put("name", weatherSpec.location); - - reconstructedOWMWeather.put("weather", weather); - reconstructedOWMWeather.put("main", main); - - } catch (JSONException e) { - e.printStackTrace(); - } - LOG.debug("Weather JSON for WEBVIEW: " + reconstructedOWMWeather.toString()); //fetch forecasts int timeOffset = 0; @@ -95,9 +72,9 @@ public class ParcelableWeather2 implements Parcelable { while (--conditions > 0) { timeOffset += 86400000; //manually determined JSONObject item = new JSONObject(); - condition = new JSONObject(); - main = new JSONObject(); - weather = new JSONArray(); + JSONObject condition = new JSONObject(); + JSONObject main = new JSONObject(); + JSONArray weather = new JSONArray(); Bundle forecastBundle = in.readBundle(getClass().getClassLoader()); String[] forecastConditionType = forecastBundle.getStringArray("weather_condition_types"); int forecastConditionCode = 0; @@ -106,7 +83,8 @@ public class ParcelableWeather2 implements Parcelable { } int forecastLowTemp = forecastBundle.getInt("weather_low_temp"); int forecastHighTemp = forecastBundle.getInt("weather_high_temp"); - weatherSpec.forecasts.add(new WeatherSpec.Forecast(forecastLowTemp, forecastHighTemp, forecastConditionCode)); + int forecastHumidity = forecastBundle.getInt("weather_humidity_value"); + weatherSpec.forecasts.add(new WeatherSpec.Forecast(forecastLowTemp, forecastHighTemp, forecastConditionCode, forecastHumidity)); try { condition.put("id", forecastConditionCode); condition.put("main", forecastBundle.getString("weather_condition_text")); @@ -114,7 +92,7 @@ public class ParcelableWeather2 implements Parcelable { weather.put(condition); main.put("temp", forecastBundle.getInt("weather_current_temp")); - main.put("humidity", forecastBundle.getInt("weather_humidity_value")); + main.put("humidity", forecastHumidity); main.put("temp_min", forecastLowTemp); main.put("temp_max", forecastHighTemp);