From 81b1d1d28df1bf6fd81e7891b8e761254e029b06 Mon Sep 17 00:00:00 2001 From: Andreas Shimokawa Date: Sat, 30 May 2015 22:24:53 +0200 Subject: [PATCH] Pebble: Put WeatherNeat test in separate file --- .../gadgetbridge/pebble/PebbleProtocol.java | 30 ++-------- .../pebble/WeatherNeatSupport.java | 56 +++++++++++++++++++ 2 files changed, 60 insertions(+), 26 deletions(-) create mode 100644 app/src/main/java/nodomain/freeyourgadget/gadgetbridge/pebble/WeatherNeatSupport.java diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/pebble/PebbleProtocol.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/pebble/PebbleProtocol.java index 0796d9bd..e50823f0 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/pebble/PebbleProtocol.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/pebble/PebbleProtocol.java @@ -172,9 +172,7 @@ public class PebbleProtocol extends GBDeviceProtocol { private ArrayList tmpUUIDS = new ArrayList<>(); private MorpheuzSupport mMorpheuzSupport = new MorpheuzSupport(PebbleProtocol.this); - // FIXME: this does not belong here - static final UUID WeatherNeatUUID = UUID.fromString("3684003b-a685-45f9-a713-abc6364ba051"); - + private WeatherNeatSupport mWeatherNeatSupport = new WeatherNeatSupport(PebbleProtocol.this); private static byte[] encodeMessage(short endpoint, byte type, int cookie, String[] parts) { // Calculate length first @@ -543,24 +541,6 @@ public class PebbleProtocol extends GBDeviceProtocol { return buf.array(); } - private byte[] encodeApplicationMessageWeatherNeatTest() { - // encode push message for WeatherNeat - ArrayList> pairs = new ArrayList<>(4); - pairs.add(new Pair<>(1, (Object) "Gadgetbridge")); // city - pairs.add(new Pair<>(2, (Object) "-22C")); // temperature - pairs.add(new Pair<>(3, (Object) "this is just a stupid test")); // condition - pairs.add(new Pair<>(5, (Object) 3)); // seconds for backlight on shake - byte[] testMessage = encodeApplicationMessagePush(ENDPOINT_APPLICATIONMESSAGE, WeatherNeatUUID, pairs); - - ByteBuffer buf = ByteBuffer.allocate(testMessage.length + LENGTH_PREFIX + 18); // +ACK - - // encode ack and put in front of push message (hack for acknowledging the last message) - buf.put(encodeApplicationMessageAck(WeatherNeatUUID, (byte) (last_id - 1))); - buf.put(testMessage); - - return buf.array(); - } - byte[] encodeApplicationMessageAck(UUID uuid, byte id) { ByteBuffer buf = ByteBuffer.allocate(LENGTH_PREFIX + 18); // +ACK @@ -806,11 +786,9 @@ public class PebbleProtocol extends GBDeviceProtocol { case APPLICATIONMESSAGE_PUSH: UUID uuid = new UUID(uuid_high, uuid_low); LOG.info("got APPLICATIONMESSAGE PUSH from UUID " + uuid); - if (WeatherNeatUUID.equals(uuid)) { - LOG.info("We know you, you are WeatherNeat"); - GBDeviceCommandSendBytes sendBytes = new GBDeviceCommandSendBytes(); - sendBytes.encodedBytes = encodeApplicationMessageWeatherNeatTest(); - cmd = sendBytes; + if (WeatherNeatSupport.uuid.equals(uuid)) { + ArrayList> dict = decodeDict(buf); + cmd = mWeatherNeatSupport.handleMessage(dict); } else if (MorpheuzSupport.uuid.equals(uuid)) { ArrayList> dict = decodeDict(buf); cmd = mMorpheuzSupport.handleMessage(dict); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/pebble/WeatherNeatSupport.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/pebble/WeatherNeatSupport.java new file mode 100644 index 00000000..b3d11de9 --- /dev/null +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/pebble/WeatherNeatSupport.java @@ -0,0 +1,56 @@ +package nodomain.freeyourgadget.gadgetbridge.pebble; + +import android.util.Pair; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.UUID; + +import nodomain.freeyourgadget.gadgetbridge.protocol.GBDeviceCommand; +import nodomain.freeyourgadget.gadgetbridge.protocol.GBDeviceCommandSendBytes; + +public class WeatherNeatSupport { + + public static final int KEY_REQUEST = 0; + public static final int KEY_CITY = 1; + public static final int KEY_TEMPERATUR = 2; + public static final int KEY_CONDITION = 3; + public static final int KEY_LIGHT_TIME = 5; + + public static final UUID uuid = UUID.fromString("3684003b-a685-45f9-a713-abc6364ba051"); + private final PebbleProtocol mPebbleProtocol; + + private static final Logger LOG = LoggerFactory.getLogger(WeatherNeatSupport.class); + + public WeatherNeatSupport(PebbleProtocol pebbleProtocol) { + mPebbleProtocol = pebbleProtocol; + } + + private byte[] encodeWeatherNeatMessage(String city, String temperature, String condition, int light_time) { + ArrayList> pairs = new ArrayList<>(4); + pairs.add(new Pair<>(1, (Object) city)); + pairs.add(new Pair<>(2, (Object) temperature)); + pairs.add(new Pair<>(3, (Object) condition)); + pairs.add(new Pair<>(5, (Object) light_time)); // seconds for backlight on shake + + byte[] ackMessage = mPebbleProtocol.encodeApplicationMessageAck(uuid, mPebbleProtocol.last_id); + byte[] testMessage = mPebbleProtocol.encodeApplicationMessagePush(PebbleProtocol.ENDPOINT_APPLICATIONMESSAGE, uuid, pairs); + + ByteBuffer buf = ByteBuffer.allocate(ackMessage.length + testMessage.length); + + // encode ack and put in front of push message (hack for acknowledging the last message) + buf.put(ackMessage); + buf.put(testMessage); + + return buf.array(); + } + + public GBDeviceCommand handleMessage(ArrayList> pairs) { + GBDeviceCommandSendBytes sendBytes = new GBDeviceCommandSendBytes(); + sendBytes.encodedBytes = encodeWeatherNeatMessage("Berlin", "22 C", "cloudy", 0); + return sendBytes; + } +}