Pebble: Put WeatherNeat test in separate file

live-sensor-data
Andreas Shimokawa 2015-05-30 22:24:53 +02:00
parent 603d31a59e
commit 81b1d1d28d
2 changed files with 60 additions and 26 deletions

View File

@ -172,9 +172,7 @@ public class PebbleProtocol extends GBDeviceProtocol {
private ArrayList<UUID> 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<Pair<Integer, Object>> 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<Pair<Integer, Object>> dict = decodeDict(buf);
cmd = mWeatherNeatSupport.handleMessage(dict);
} else if (MorpheuzSupport.uuid.equals(uuid)) {
ArrayList<Pair<Integer, Object>> dict = decodeDict(buf);
cmd = mMorpheuzSupport.handleMessage(dict);

View File

@ -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<Pair<Integer, Object>> 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<Pair<Integer, Object>> pairs) {
GBDeviceCommandSendBytes sendBytes = new GBDeviceCommandSendBytes();
sendBytes.encodedBytes = encodeWeatherNeatMessage("Berlin", "22 C", "cloudy", 0);
return sendBytes;
}
}