Pebble: Cleanup AppMessage handlers

This commit is contained in:
Andreas Shimokawa 2015-10-04 22:53:11 +09:00
parent a4a59f5df4
commit d21b5e68b5
5 changed files with 75 additions and 46 deletions

View File

@ -0,0 +1,28 @@
package nodomain.freeyourgadget.gadgetbridge.service.devices.pebble;
import android.util.Pair;
import java.util.ArrayList;
import java.util.UUID;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEvent;
public class AppMessageHandler {
protected final PebbleProtocol mPebbleProtocol;
protected final UUID mUUID;
AppMessageHandler(UUID uuid, PebbleProtocol pebbleProtocol) {
mUUID = uuid;
mPebbleProtocol = pebbleProtocol;
}
public UUID getUUID() {
return mUUID;
}
public GBDeviceEvent[] handleMessage(ArrayList<Pair<Integer, Object>> pairs) {
return null;
}
}

View File

@ -19,20 +19,18 @@ import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEvent;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventSendBytes; import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventSendBytes;
import nodomain.freeyourgadget.gadgetbridge.devices.SampleProvider; import nodomain.freeyourgadget.gadgetbridge.devices.SampleProvider;
public class GadgetbridgePblSupport { public class AppMessageHandlerGBPebble extends AppMessageHandler {
public static final int KEY_TIMESTAMP = 1; public static final int KEY_TIMESTAMP = 1;
public static final int KEY_SAMPLES = 2; public static final int KEY_SAMPLES = 2;
public static final UUID uuid = UUID.fromString("61476764-7465-7262-6469-656775527a6c"); private static final Logger LOG = LoggerFactory.getLogger(AppMessageHandlerGBPebble.class);
private final PebbleProtocol mPebbleProtocol;
private static final Logger LOG = LoggerFactory.getLogger(GadgetbridgePblSupport.class); AppMessageHandlerGBPebble(UUID uuid, PebbleProtocol pebbleProtocol) {
super(uuid, pebbleProtocol);
public GadgetbridgePblSupport(PebbleProtocol pebbleProtocol) {
mPebbleProtocol = pebbleProtocol;
} }
@Override
public GBDeviceEvent[] handleMessage(ArrayList<Pair<Integer, Object>> pairs) { public GBDeviceEvent[] handleMessage(ArrayList<Pair<Integer, Object>> pairs) {
int timestamp = 0; int timestamp = 0;
for (Pair<Integer, Object> pair : pairs) { for (Pair<Integer, Object> pair : pairs) {
@ -74,7 +72,7 @@ public class GadgetbridgePblSupport {
} }
} }
GBDeviceEventSendBytes sendBytes = new GBDeviceEventSendBytes(); GBDeviceEventSendBytes sendBytes = new GBDeviceEventSendBytes();
sendBytes.encodedBytes = mPebbleProtocol.encodeApplicationMessageAck(uuid, mPebbleProtocol.last_id); sendBytes.encodedBytes = mPebbleProtocol.encodeApplicationMessageAck(mUUID, mPebbleProtocol.last_id);
return new GBDeviceEvent[]{sendBytes}; return new GBDeviceEvent[]{sendBytes};
} }
} }

View File

@ -19,7 +19,7 @@ import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventSleepMonit
import nodomain.freeyourgadget.gadgetbridge.devices.SampleProvider; import nodomain.freeyourgadget.gadgetbridge.devices.SampleProvider;
import nodomain.freeyourgadget.gadgetbridge.devices.pebble.MorpheuzSampleProvider; import nodomain.freeyourgadget.gadgetbridge.devices.pebble.MorpheuzSampleProvider;
public class MorpheuzSupport { public class AppMessageHandlerMorpheuz extends AppMessageHandler {
public static final int KEY_POINT = 1; public static final int KEY_POINT = 1;
public static final int KEY_CTRL = 2; public static final int KEY_CTRL = 2;
@ -37,28 +37,26 @@ public class MorpheuzSupport {
public static final int CTRL_DO_NEXT = 8; public static final int CTRL_DO_NEXT = 8;
public static final int CTRL_SET_LAST_SENT = 16; public static final int CTRL_SET_LAST_SENT = 16;
public static final UUID uuid = UUID.fromString("5be44f1d-d262-4ea6-aa30-ddbec1e3cab2");
private final PebbleProtocol mPebbleProtocol;
// data received from Morpheuz in native format // data received from Morpheuz in native format
private int smartalarm_from = -1; // time in minutes relative from 0:00 for smart alarm (earliest) private int smartalarm_from = -1; // time in minutes relative from 0:00 for smart alarm (earliest)
private int smartalarm_to = -1;// time in minutes relative from 0:00 for smart alarm (latest) private int smartalarm_to = -1;// time in minutes relative from 0:00 for smart alarm (latest)
private int recording_base_timestamp = -1; // timestamp for the first "point", all folowing are +10 minutes offset each private int recording_base_timestamp = -1; // timestamp for the first "point", all folowing are +10 minutes offset each
private int alarm_gone_off = -1; // time in minutes relative from 0:00 when alarm gone off private int alarm_gone_off = -1; // time in minutes relative from 0:00 when alarm gone off
private static final Logger LOG = LoggerFactory.getLogger(MorpheuzSupport.class); private static final Logger LOG = LoggerFactory.getLogger(AppMessageHandlerMorpheuz.class);
public MorpheuzSupport(PebbleProtocol pebbleProtocol) { public AppMessageHandlerMorpheuz(UUID uuid, PebbleProtocol pebbleProtocol) {
mPebbleProtocol = pebbleProtocol; super(uuid, pebbleProtocol);
} }
private byte[] encodeMorpheuzMessage(int key, int value) { private byte[] encodeMorpheuzMessage(int key, int value) {
ArrayList<Pair<Integer, Object>> pairs = new ArrayList<>(); ArrayList<Pair<Integer, Object>> pairs = new ArrayList<>();
pairs.add(new Pair<Integer, Object>(key, value)); pairs.add(new Pair<Integer, Object>(key, value));
return mPebbleProtocol.encodeApplicationMessagePush(PebbleProtocol.ENDPOINT_APPLICATIONMESSAGE, uuid, pairs); return mPebbleProtocol.encodeApplicationMessagePush(PebbleProtocol.ENDPOINT_APPLICATIONMESSAGE, mUUID, pairs);
} }
@Override
public GBDeviceEvent[] handleMessage(ArrayList<Pair<Integer, Object>> pairs) { public GBDeviceEvent[] handleMessage(ArrayList<Pair<Integer, Object>> pairs) {
int ctrl_message = 0; int ctrl_message = 0;
GBDeviceEventSleepMonitorResult sleepMonitorResult = null; GBDeviceEventSleepMonitorResult sleepMonitorResult = null;
@ -80,7 +78,7 @@ public class MorpheuzSupport {
case KEY_POINT: case KEY_POINT:
if (recording_base_timestamp == -1) { if (recording_base_timestamp == -1) {
// we have no base timestamp but received points, stop this // we have no base timestamp but received points, stop this
ctrl_message = MorpheuzSupport.CTRL_VERSION_DONE | MorpheuzSupport.CTRL_GONEOFF_DONE | MorpheuzSupport.CTRL_TRANSMIT_DONE | MorpheuzSupport.CTRL_SET_LAST_SENT; ctrl_message = AppMessageHandlerMorpheuz.CTRL_VERSION_DONE | AppMessageHandlerMorpheuz.CTRL_GONEOFF_DONE | AppMessageHandlerMorpheuz.CTRL_TRANSMIT_DONE | AppMessageHandlerMorpheuz.CTRL_SET_LAST_SENT;
} else { } else {
short index = (short) ((int) pair.second >> 16); short index = (short) ((int) pair.second >> 16);
short intensity = (short) ((int) pair.second & 0xffff); short intensity = (short) ((int) pair.second & 0xffff);
@ -105,32 +103,32 @@ public class MorpheuzSupport {
} }
} }
ctrl_message = MorpheuzSupport.CTRL_VERSION_DONE | MorpheuzSupport.CTRL_SET_LAST_SENT | MorpheuzSupport.CTRL_DO_NEXT; ctrl_message = AppMessageHandlerMorpheuz.CTRL_VERSION_DONE | AppMessageHandlerMorpheuz.CTRL_SET_LAST_SENT | AppMessageHandlerMorpheuz.CTRL_DO_NEXT;
} }
break; break;
case KEY_FROM: case KEY_FROM:
smartalarm_from = (int) pair.second; smartalarm_from = (int) pair.second;
LOG.info("got from: " + smartalarm_from / 60 + ":" + smartalarm_from % 60); LOG.info("got from: " + smartalarm_from / 60 + ":" + smartalarm_from % 60);
ctrl_message = MorpheuzSupport.CTRL_VERSION_DONE | MorpheuzSupport.CTRL_SET_LAST_SENT | MorpheuzSupport.CTRL_DO_NEXT; ctrl_message = AppMessageHandlerMorpheuz.CTRL_VERSION_DONE | AppMessageHandlerMorpheuz.CTRL_SET_LAST_SENT | AppMessageHandlerMorpheuz.CTRL_DO_NEXT;
break; break;
case KEY_TO: case KEY_TO:
smartalarm_to = (int) pair.second; smartalarm_to = (int) pair.second;
LOG.info("got from: " + smartalarm_to / 60 + ":" + smartalarm_to % 60); LOG.info("got from: " + smartalarm_to / 60 + ":" + smartalarm_to % 60);
ctrl_message = MorpheuzSupport.CTRL_VERSION_DONE | MorpheuzSupport.CTRL_SET_LAST_SENT | MorpheuzSupport.CTRL_DO_NEXT; ctrl_message = AppMessageHandlerMorpheuz.CTRL_VERSION_DONE | AppMessageHandlerMorpheuz.CTRL_SET_LAST_SENT | AppMessageHandlerMorpheuz.CTRL_DO_NEXT;
break; break;
case KEY_VERSION: case KEY_VERSION:
LOG.info("got version: " + ((float) ((int) pair.second) / 10.0f)); LOG.info("got version: " + ((float) ((int) pair.second) / 10.0f));
ctrl_message = MorpheuzSupport.CTRL_VERSION_DONE | MorpheuzSupport.CTRL_SET_LAST_SENT; ctrl_message = AppMessageHandlerMorpheuz.CTRL_VERSION_DONE | AppMessageHandlerMorpheuz.CTRL_SET_LAST_SENT;
break; break;
case KEY_BASE: case KEY_BASE:
// fix timestamp // fix timestamp
TimeZone tz = SimpleTimeZone.getDefault(); TimeZone tz = SimpleTimeZone.getDefault();
recording_base_timestamp = (int) pair.second - (tz.getOffset(System.currentTimeMillis())) / 1000; recording_base_timestamp = (int) pair.second - (tz.getOffset(System.currentTimeMillis())) / 1000;
LOG.info("got base: " + recording_base_timestamp); LOG.info("got base: " + recording_base_timestamp);
ctrl_message = MorpheuzSupport.CTRL_VERSION_DONE | MorpheuzSupport.CTRL_SET_LAST_SENT | MorpheuzSupport.CTRL_DO_NEXT; ctrl_message = AppMessageHandlerMorpheuz.CTRL_VERSION_DONE | AppMessageHandlerMorpheuz.CTRL_SET_LAST_SENT | AppMessageHandlerMorpheuz.CTRL_DO_NEXT;
break; break;
case KEY_AUTO_RESET: case KEY_AUTO_RESET:
ctrl_message = MorpheuzSupport.CTRL_VERSION_DONE | MorpheuzSupport.CTRL_SET_LAST_SENT | MorpheuzSupport.CTRL_DO_NEXT; ctrl_message = AppMessageHandlerMorpheuz.CTRL_VERSION_DONE | AppMessageHandlerMorpheuz.CTRL_SET_LAST_SENT | AppMessageHandlerMorpheuz.CTRL_DO_NEXT;
break; break;
default: default:
LOG.info("unhandled key: " + pair.first); LOG.info("unhandled key: " + pair.first);
@ -140,13 +138,13 @@ public class MorpheuzSupport {
// always ack // always ack
GBDeviceEventSendBytes sendBytesAck = new GBDeviceEventSendBytes(); GBDeviceEventSendBytes sendBytesAck = new GBDeviceEventSendBytes();
sendBytesAck.encodedBytes = mPebbleProtocol.encodeApplicationMessageAck(uuid, mPebbleProtocol.last_id); sendBytesAck.encodedBytes = mPebbleProtocol.encodeApplicationMessageAck(mUUID, mPebbleProtocol.last_id);
// sometimes send control message // sometimes send control message
GBDeviceEventSendBytes sendBytesCtrl = null; GBDeviceEventSendBytes sendBytesCtrl = null;
if (ctrl_message > 0) { if (ctrl_message > 0) {
sendBytesCtrl = new GBDeviceEventSendBytes(); sendBytesCtrl = new GBDeviceEventSendBytes();
sendBytesCtrl.encodedBytes = encodeMorpheuzMessage(MorpheuzSupport.KEY_CTRL, ctrl_message); sendBytesCtrl.encodedBytes = encodeMorpheuzMessage(AppMessageHandlerMorpheuz.KEY_CTRL, ctrl_message);
} }
// ctrl and sleep monitor might be null, thats okay // ctrl and sleep monitor might be null, thats okay

View File

@ -12,7 +12,7 @@ import java.util.UUID;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEvent; import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEvent;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventSendBytes; import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventSendBytes;
public class WeatherNeatSupport { 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;
@ -20,13 +20,10 @@ public class WeatherNeatSupport {
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;
public static final UUID uuid = UUID.fromString("3684003b-a685-45f9-a713-abc6364ba051"); private static final Logger LOG = LoggerFactory.getLogger(AppMessageHandlerWeatherNeat.class);
private final PebbleProtocol mPebbleProtocol;
private static final Logger LOG = LoggerFactory.getLogger(WeatherNeatSupport.class); public AppMessageHandlerWeatherNeat(UUID uuid, PebbleProtocol pebbleProtocol) {
super(uuid, pebbleProtocol);
public WeatherNeatSupport(PebbleProtocol pebbleProtocol) {
mPebbleProtocol = pebbleProtocol;
} }
private byte[] encodeWeatherNeatMessage(String city, String temperature, String condition, int light_time) { private byte[] encodeWeatherNeatMessage(String city, String temperature, String condition, int light_time) {
@ -36,8 +33,8 @@ public class WeatherNeatSupport {
pairs.add(new Pair<>(3, (Object) condition)); pairs.add(new Pair<>(3, (Object) condition));
pairs.add(new Pair<>(5, (Object) light_time)); // seconds for backlight on shake pairs.add(new Pair<>(5, (Object) light_time)); // seconds for backlight on shake
byte[] ackMessage = mPebbleProtocol.encodeApplicationMessageAck(uuid, mPebbleProtocol.last_id); byte[] ackMessage = mPebbleProtocol.encodeApplicationMessageAck(mUUID, mPebbleProtocol.last_id);
byte[] testMessage = mPebbleProtocol.encodeApplicationMessagePush(PebbleProtocol.ENDPOINT_APPLICATIONMESSAGE, uuid, pairs); byte[] testMessage = mPebbleProtocol.encodeApplicationMessagePush(PebbleProtocol.ENDPOINT_APPLICATIONMESSAGE, mUUID, pairs);
ByteBuffer buf = ByteBuffer.allocate(ackMessage.length + testMessage.length); ByteBuffer buf = ByteBuffer.allocate(ackMessage.length + testMessage.length);
@ -48,6 +45,7 @@ public class WeatherNeatSupport {
return buf.array(); return buf.array();
} }
@Override
public GBDeviceEvent[] handleMessage(ArrayList<Pair<Integer, Object>> pairs) { public GBDeviceEvent[] handleMessage(ArrayList<Pair<Integer, Object>> pairs) {
GBDeviceEventSendBytes sendBytes = new GBDeviceEventSendBytes(); GBDeviceEventSendBytes sendBytes = new GBDeviceEventSendBytes();
sendBytes.encodedBytes = encodeWeatherNeatMessage("Berlin", "22 C", "cloudy", 0); sendBytes.encodedBytes = encodeWeatherNeatMessage("Berlin", "22 C", "cloudy", 0);

View File

@ -13,6 +13,8 @@ import java.nio.ByteBuffer;
import java.nio.ByteOrder; import java.nio.ByteOrder;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Random; import java.util.Random;
import java.util.SimpleTimeZone; import java.util.SimpleTimeZone;
import java.util.UUID; import java.util.UUID;
@ -327,9 +329,19 @@ public class PebbleProtocol extends GBDeviceProtocol {
byte last_id = -1; byte last_id = -1;
private ArrayList<UUID> tmpUUIDS = new ArrayList<>(); private ArrayList<UUID> tmpUUIDS = new ArrayList<>();
private MorpheuzSupport mMorpheuzSupport = new MorpheuzSupport(PebbleProtocol.this); private static final UUID UUID_GBPEBBLE = UUID.fromString("61476764-7465-7262-6469-656775527a6c");
private WeatherNeatSupport mWeatherNeatSupport = new WeatherNeatSupport(PebbleProtocol.this); private static final UUID UUID_MORPHEUZ = UUID.fromString("5be44f1d-d262-4ea6-aa30-ddbec1e3cab2");
private GadgetbridgePblSupport mGadgetbridgePblSupport = new GadgetbridgePblSupport(PebbleProtocol.this); private static final UUID UUID_WHETHERNEAT = UUID.fromString("3684003b-a685-45f9-a713-abc6364ba051");
private static final UUID UUID_MISFIT = UUID.fromString("0b73b76a-cd65-4dc2-9585-aaa213320858");
private static Map<UUID, AppMessageHandler> mAppMessageHandlers = new HashMap<>();
{
mAppMessageHandlers.put(UUID_GBPEBBLE, new AppMessageHandlerGBPebble(UUID_GBPEBBLE, PebbleProtocol.this));
mAppMessageHandlers.put(UUID_MORPHEUZ, new AppMessageHandlerMorpheuz(UUID_MORPHEUZ, PebbleProtocol.this));
mAppMessageHandlers.put(UUID_WHETHERNEAT, new AppMessageHandlerWeatherNeat(UUID_WHETHERNEAT, PebbleProtocol.this));
//mAppMessageHandlers.put(UUID_MISFIT,new AppMessageHandlerMisfit(UUID_MISFIT,PebbleProtocol.this));
}
private static byte[] encodeSimpleMessage(short endpoint, byte command) { private static byte[] encodeSimpleMessage(short endpoint, byte command) {
ByteBuffer buf = ByteBuffer.allocate(LENGTH_PREFIX + LENGTH_SIMPLEMESSAGE); ByteBuffer buf = ByteBuffer.allocate(LENGTH_PREFIX + LENGTH_SIMPLEMESSAGE);
@ -1453,7 +1465,7 @@ public class PebbleProtocol extends GBDeviceProtocol {
buf.order(ByteOrder.BIG_ENDIAN); buf.order(ByteOrder.BIG_ENDIAN);
long uuid_high = buf.getLong(); long uuid_high = buf.getLong();
long uuid_low = buf.getLong(); long uuid_low = buf.getLong();
int id = (int) (uuid_low & 0xffffffff); int id = (int) (uuid_low & 0xffffffffL);
byte action = buf.get(); byte action = buf.get();
if (action >= 0x01 && action <= 0x04) { if (action >= 0x01 && action <= 0x04) {
GBDeviceEventNotificationControl dismissNotification = new GBDeviceEventNotificationControl(); GBDeviceEventNotificationControl dismissNotification = new GBDeviceEventNotificationControl();
@ -1729,15 +1741,10 @@ public class PebbleProtocol extends GBDeviceProtocol {
case APPLICATIONMESSAGE_PUSH: case APPLICATIONMESSAGE_PUSH:
UUID uuid = new UUID(uuid_high, uuid_low); UUID uuid = new UUID(uuid_high, uuid_low);
LOG.info("got APPLICATIONMESSAGE PUSH from UUID " + uuid); LOG.info("got APPLICATIONMESSAGE PUSH from UUID " + uuid);
if (WeatherNeatSupport.uuid.equals(uuid)) { AppMessageHandler handler = mAppMessageHandlers.get(uuid);
if (handler != null) {
ArrayList<Pair<Integer, Object>> dict = decodeDict(buf); ArrayList<Pair<Integer, Object>> dict = decodeDict(buf);
devEvts = mWeatherNeatSupport.handleMessage(dict); devEvts = handler.handleMessage(dict);
} else if (MorpheuzSupport.uuid.equals(uuid)) {
ArrayList<Pair<Integer, Object>> dict = decodeDict(buf);
devEvts = mMorpheuzSupport.handleMessage(dict);
} else if (GadgetbridgePblSupport.uuid.equals(uuid)) {
ArrayList<Pair<Integer, Object>> dict = decodeDict(buf);
devEvts = mGadgetbridgePblSupport.handleMessage(dict);
} else { } else {
try { try {
devEvts = decodeDictToJSONAppMessage(uuid, buf); devEvts = decodeDictToJSONAppMessage(uuid, buf);