Pebble: actually display current whether in WeatherNeat

This commit is contained in:
Andreas Shimokawa 2015-12-29 22:10:38 +01:00
parent c962dbbac2
commit e533fdbaa6
3 changed files with 34 additions and 12 deletions

View File

@ -3,6 +3,8 @@ package nodomain.freeyourgadget.gadgetbridge.externalevents;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -28,8 +30,14 @@ public class WeatherNotificationReceiver extends BroadcastReceiver {
} }
if (weather != null) { if (weather != null) {
LOG.info("weather in " + weather.location + " is " + (weather.currentTemp - 273) + "°C"); LOG.info("weather in " + weather.location + " is " + weather.currentCondition + " (" + (weather.currentTemp - 273) + "°C)");
}
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor edit = sharedPrefs.edit();
edit.putString("weather_location", weather.location);
edit.putString("weather_current_condition", weather.currentCondition);
edit.putInt("weather_current_temp", weather.currentTemp);
edit.apply();
}
} }
} }

View File

@ -1,5 +1,7 @@
package nodomain.freeyourgadget.gadgetbridge.service.devices.pebble; package nodomain.freeyourgadget.gadgetbridge.service.devices.pebble;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Pair; import android.util.Pair;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -9,6 +11,7 @@ import java.nio.ByteBuffer;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.UUID; import java.util.UUID;
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEvent; import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEvent;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventSendBytes; import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventSendBytes;
@ -16,7 +19,7 @@ public class AppMessageHandlerWeatherNeat extends AppMessageHandler {
public static final int KEY_REQUEST = 0; public static final int KEY_REQUEST = 0;
public static final int KEY_CITY = 1; public static final int KEY_CITY = 1;
public static final int KEY_TEMPERATUR = 2; public static final int KEY_TEMPERATURE = 2;
public static final int KEY_CONDITION = 3; public static final int KEY_CONDITION = 3;
public static final int KEY_LIGHT_TIME = 5; public static final int KEY_LIGHT_TIME = 5;
@ -28,10 +31,10 @@ public class AppMessageHandlerWeatherNeat extends AppMessageHandler {
private byte[] encodeWeatherNeatMessage(String city, String temperature, String condition, int light_time) { private byte[] encodeWeatherNeatMessage(String city, String temperature, String condition, int light_time) {
ArrayList<Pair<Integer, Object>> pairs = new ArrayList<>(4); ArrayList<Pair<Integer, Object>> pairs = new ArrayList<>(4);
pairs.add(new Pair<>(1, (Object) city)); pairs.add(new Pair<>(KEY_CITY, (Object) city));
pairs.add(new Pair<>(2, (Object) temperature)); pairs.add(new Pair<>(KEY_TEMPERATURE, (Object) temperature));
pairs.add(new Pair<>(3, (Object) condition)); pairs.add(new Pair<>(KEY_CONDITION, (Object) condition));
pairs.add(new Pair<>(5, (Object) light_time)); // seconds for backlight on shake pairs.add(new Pair<>(KEY_LIGHT_TIME, (Object) light_time)); // seconds for backlight on shake
byte[] ackMessage = mPebbleProtocol.encodeApplicationMessageAck(mUUID, mPebbleProtocol.last_id); byte[] ackMessage = mPebbleProtocol.encodeApplicationMessageAck(mUUID, mPebbleProtocol.last_id);
byte[] testMessage = mPebbleProtocol.encodeApplicationMessagePush(PebbleProtocol.ENDPOINT_APPLICATIONMESSAGE, mUUID, pairs); byte[] testMessage = mPebbleProtocol.encodeApplicationMessagePush(PebbleProtocol.ENDPOINT_APPLICATIONMESSAGE, mUUID, pairs);
@ -47,8 +50,12 @@ public class AppMessageHandlerWeatherNeat extends AppMessageHandler {
@Override @Override
public GBDeviceEvent[] handleMessage(ArrayList<Pair<Integer, Object>> pairs) { public GBDeviceEvent[] handleMessage(ArrayList<Pair<Integer, Object>> pairs) {
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(GBApplication.getContext());
String currentTemp = (sharedPrefs.getInt("weather_current_temp", 0) - 273) + "°C";
String location = sharedPrefs.getString("weather_location", "unknown");
String condition = sharedPrefs.getString("weather_current_condition", "unknown");
GBDeviceEventSendBytes sendBytes = new GBDeviceEventSendBytes(); GBDeviceEventSendBytes sendBytes = new GBDeviceEventSendBytes();
sendBytes.encodedBytes = encodeWeatherNeatMessage("Berlin", "22 C", "cloudy", 0); sendBytes.encodedBytes = encodeWeatherNeatMessage(location, currentTemp, condition, 3);
return new GBDeviceEvent[]{sendBytes}; return new GBDeviceEvent[]{sendBytes};
} }
} }

View File

@ -17,24 +17,31 @@ public class ParcelableWeather2 implements Parcelable {
public int version = 0; public int version = 0;
public String location = ""; public String location = "";
public int currentTemp = 0; public int currentTemp = 0;
public String currentCondition = "";
private ParcelableWeather2(Parcel in) { private ParcelableWeather2(Parcel in) {
int version = in.readInt(); int version = in.readInt();
if (version != 2) { if (version != 2) {
return; return;
} }
Bundle bundle = in.readBundle(this.getClass().getClassLoader()); Bundle bundle = in.readBundle();
location = bundle.getString("weather_location"); location = bundle.getString("weather_location");
time = bundle.getLong("weather_time"); time = bundle.getLong("weather_time");
queryTime = bundle.getLong("weather_query_time"); queryTime = bundle.getLong("weather_query_time");
bundle.getString("weather_forecast_url"); bundle.getString("weather_forecast_url");
int conditions = bundle.getInt("weather_conditions"); int conditions = bundle.getInt("weather_conditions");
if (conditions > 0) { if (conditions > 0) {
Bundle conditionBundle = in.readBundle(this.getClass().getClassLoader()); Bundle conditionBundle = in.readBundle();
conditionBundle.getString("weather_condition_text"); currentCondition = conditionBundle.getString("weather_condition_text");
conditionBundle.getStringArray("weather_condition_types"); conditionBundle.getStringArray("weather_condition_types");
currentTemp = conditionBundle.getInt("weather_current_temp"); currentTemp = conditionBundle.getInt("weather_current_temp");
}
// get the rest
while(--conditions > 0) {
Bundle conditionBundle = in.readBundle();
conditionBundle.getString("weather_condition_text");
conditionBundle.getStringArray("weather_condition_types");
conditionBundle.getInt("weather_current_temp");
} }
} }