From c7c723134ea73d88e9dea9382fecd11303268455 Mon Sep 17 00:00:00 2001 From: danielegobbetti Date: Sun, 3 Jan 2016 18:28:32 +0100 Subject: [PATCH] Add weather singleton (to store the whole data passed by weather notification). Add weather info for TimeStylePebble. Add further fields to the ParcelableWeather class. Add reverse mapping to ParcelableWeather to get back the original OpenWeatherMap API condition codes. --- .../WeatherNotificationReceiver.java | 2 + .../gadgetbridge/model/Weather.java | 8 ++ .../AppMessageHandlerTimeStylePebble.java | 39 +++--- .../notification/ParcelableWeather2.java | 129 +++++++++++++++++- 4 files changed, 159 insertions(+), 19 deletions(-) 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 4a598aef..7a8e2aa7 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/WeatherNotificationReceiver.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/WeatherNotificationReceiver.java @@ -9,6 +9,7 @@ import android.preference.PreferenceManager; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import nodomain.freeyourgadget.gadgetbridge.model.Weather; import ru.gelin.android.weather.notification.ParcelableWeather2; @@ -30,6 +31,7 @@ public class WeatherNotificationReceiver extends BroadcastReceiver { } if (weather != null) { + Weather.getInstance().setWeather2(weather); LOG.info("weather in " + weather.location + " is " + weather.currentCondition + " (" + (weather.currentTemp - 273) + "°C)"); SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context); 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 b8be5cca..eedb9ac1 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/Weather.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/Weather.java @@ -1,6 +1,14 @@ package nodomain.freeyourgadget.gadgetbridge.model; +import ru.gelin.android.weather.notification.ParcelableWeather2; + public class Weather { + private ParcelableWeather2 weather2 = null; + public ParcelableWeather2 getWeather2() {return weather2;} + public void setWeather2(ParcelableWeather2 weather2) {this.weather2 = weather2;} + + private static final Weather weather = new Weather(); + public static Weather getInstance() {return weather;} public int mapToYahooCondition(int openWeatherMapCondition) { // openweathermap.org conditions: diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/AppMessageHandlerTimeStylePebble.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/AppMessageHandlerTimeStylePebble.java index 13bf8430..d0c4d89f 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/AppMessageHandlerTimeStylePebble.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/AppMessageHandlerTimeStylePebble.java @@ -12,6 +12,8 @@ import java.util.UUID; import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEvent; import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventSendBytes; +import nodomain.freeyourgadget.gadgetbridge.model.Weather; +import ru.gelin.android.weather.notification.ParcelableWeather2; public class AppMessageHandlerTimeStylePebble extends AppMessageHandler { public static final int KEY_SETTING_SIDEBAR_LEFT = 9; @@ -59,43 +61,44 @@ public class AppMessageHandlerTimeStylePebble extends AppMessageHandler { pairs.add(new Pair<>(KEY_SETTING_SHOW_LEADING_ZERO, (Object) 1)); pairs.add(new Pair<>(KEY_SETTING_LANGUAGE_ID, (Object) 2)); //2 = Deutsch pairs.add(new Pair<>(KEY_SETTING_USE_METRIC, (Object) 1)); + pairs.add(new Pair<>(KEY_SETTING_SHOW_BATTERY_PCT, (Object) 1)); pairs.add(new Pair<>(KEY_WIDGET_0_ID, (Object) 7)); //7 = current weather pairs.add(new Pair<>(KEY_WIDGET_1_ID, (Object) 2)); //2 = battery pairs.add(new Pair<>(KEY_WIDGET_2_ID, (Object) 4)); //4 = Date -/* - pairs.add(new Pair<>(KEY_TEMPERATURE, (Object) 6)); - pairs.add(new Pair<>(KEY_CONDITION_CODE, (Object) 25)); - pairs.add(new Pair<>(KEY_FORECAST_CONDITION, (Object) 2)); - pairs.add(new Pair<>(KEY_FORECAST_TEMP_HIGH, (Object) 12)); - pairs.add(new Pair<>(KEY_FORECAST_TEMP_LOW, (Object) 0)); -*/ - byte[] ackMessage = mPebbleProtocol.encodeApplicationMessageAck(mUUID, mPebbleProtocol.last_id); byte[] testMessage = mPebbleProtocol.encodeApplicationMessagePush(PebbleProtocol.ENDPOINT_APPLICATIONMESSAGE, mUUID, pairs); - //byte[] weatherMessage=encodeTimeStylePebbleWeather(); - - ByteBuffer buf = ByteBuffer.allocate(ackMessage.length + testMessage.length ); + byte[] weatherMessage=encodeTimeStylePebbleWeather(); + ByteBuffer buf = ByteBuffer.allocate(ackMessage.length + testMessage.length + weatherMessage.length); // encode ack and put in front of push message (hack for acknowledging the last message) buf.put(ackMessage); buf.put(testMessage); + buf.put(weatherMessage); return buf.array(); } private byte[] encodeTimeStylePebbleWeather() { ArrayList> pairs = new ArrayList<>(); - pairs.add(new Pair<>(KEY_TEMPERATURE, (Object) 6)); - pairs.add(new Pair<>(KEY_CONDITION_CODE, (Object) 1)); - pairs.add(new Pair<>(KEY_FORECAST_CONDITION, (Object) 2)); - pairs.add(new Pair<>(KEY_FORECAST_TEMP_HIGH, (Object) 12)); - pairs.add(new Pair<>(KEY_FORECAST_TEMP_LOW, (Object) 0)); + ParcelableWeather2 weather = Weather.getInstance().getWeather2(); + + if (weather != null) { + //TODO: use the night icons when night + pairs.add(new Pair<>(KEY_USE_NIGHT_ICON, (Object) 0)); + pairs.add(new Pair<>(KEY_TEMPERATURE, (Object) (weather.currentTemp - 273))); + pairs.add(new Pair<>(KEY_CONDITION_CODE, (Object) weather.currentConditionCode)); + pairs.add(new Pair<>(KEY_FORECAST_CONDITION, (Object) weather.forecastConditionCode)); + pairs.add(new Pair<>(KEY_FORECAST_TEMP_HIGH, (Object) (weather.highTemp - 273))); + pairs.add(new Pair<>(KEY_FORECAST_TEMP_LOW, (Object) (weather.lowTemp - 273))); + byte[] weatherMessage = mPebbleProtocol.encodeApplicationMessagePush(PebbleProtocol.ENDPOINT_APPLICATIONMESSAGE, mUUID, pairs); + return weatherMessage; + } + + return null; - byte[] weatherMessage = mPebbleProtocol.encodeApplicationMessagePush(PebbleProtocol.ENDPOINT_APPLICATIONMESSAGE, mUUID, pairs); - return weatherMessage; } @Override 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 ab04cace..d7b42674 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 @@ -19,6 +19,14 @@ public class ParcelableWeather2 implements Parcelable { public int currentTemp = 0; public String currentCondition = ""; + String[] currentConditionType = null; + public int currentConditionCode = 3200; + String[] forecastConditionType = null; + public int forecastConditionCode = 3200; + public int lowTemp = 0; + public int highTemp = 0; + + private ParcelableWeather2(Parcel in) { int version = in.readInt(); if (version != 2) { @@ -35,9 +43,20 @@ public class ParcelableWeather2 implements Parcelable { currentCondition = conditionBundle.getString("weather_condition_text"); conditionBundle.getStringArray("weather_condition_types"); currentTemp = conditionBundle.getInt("weather_current_temp"); + + currentConditionType = conditionBundle.getStringArray("weather_condition_types"); + currentConditionCode = weatherConditionTypesToOpenWeatherMapIds(currentConditionType[0]); + lowTemp = conditionBundle.getInt("weather_low_temp"); + highTemp = conditionBundle.getInt("weather_high_temp"); + //fetch immediate next forecast + if (--conditions > 0) { + Bundle forecastBundle = in.readBundle(); + forecastConditionType = forecastBundle.getStringArray("weather_condition_types"); + forecastConditionCode = weatherConditionTypesToOpenWeatherMapIds(currentConditionType[0]); + } } // get the rest - while(--conditions > 0) { + while (--conditions > 0) { Bundle conditionBundle = in.readBundle(); conditionBundle.getString("weather_condition_text"); conditionBundle.getStringArray("weather_condition_types"); @@ -66,4 +85,112 @@ public class ParcelableWeather2 implements Parcelable { public void writeToParcel(Parcel dest, int flags) { // we do not really want to use this at all } + + private int weatherConditionTypesToOpenWeatherMapIds(String weather_condition_type) { + switch (weather_condition_type) { + case "THUNDERSTORM_RAIN_LIGHT": + return 200; + case "THUNDERSTORM_RAIN": + return 201; + case "THUNDERSTORM_RAIN_HEAVY": + return 202; + case "THUNDERSTORM_LIGHT": + return 210; + case "THUNDERSTORM": + return 211; + case "THUNDERSTORM_HEAVY": + return 212; + case "THUNDERSTORM_RAGGED": + return 221; + case "THUNDERSTORM_DRIZZLE_LIGHT": + return 230; + case "THUNDERSTORM_DRIZZLE": + return 231; + case "THUNDERSTORM_DRIZZLE_HEAVY": + return 232; + + case "DRIZZLE_LIGHT": + return 300; + case "DRIZZLE": + return 301; + case "DRIZZLE_HEAVY": + return 302; + case "DRIZZLE_RAIN_LIGHT": + return 310; + case "DRIZZLE_RAIN": + return 311; + case "DRIZZLE_RAIN_HEAVY": + return 312; + case "DRIZZLE_SHOWER": + return 321; + + case "RAIN_LIGHT": + return 500; + case "RAIN": + return 501; + case "RAIN_HEAVY": + return 502; + case "RAIN_VERY_HEAVY": + return 503; + case "RAIN_EXTREME": + return 504; + case "RAIN_FREEZING": + return 511; + case "RAIN_SHOWER_LIGHT": + return 520; + case "RAIN_SHOWER": + return 521; + case "RAIN_SHOWER_HEAVY": + return 522; + + case "SNOW_LIGHT": + return 600; + case "SNOW": + return 601; + case "SNOW_HEAVY": + return 602; + case "SLEET": + return 611; + case "SNOW_SHOWER": + return 621; + + case "MIST": + return 701; + case "SMOKE": + return 711; + case "HAZE": + return 721; + case "SAND_WHIRLS": + return 731; + case "FOG": + return 741; + + case "CLOUDS_CLEAR": + return 800; + case "CLOUDS_FEW": + return 801; + case "CLOUDS_SCATTERED": + return 802; + case "CLOUDS_BROKEN": + return 803; + case "CLOUDS_OVERCAST": + return 804; + + case "TORNADO": + return 900; + case "TROPICAL_STORM": + return 901; + case "HURRICANE": + return 902; + case "COLD": + return 903; + case "HOT": + return 904; + case "WINDY": + return 905; + case "HAIL": + return 906; + } + return 3200; + } }