From 68b76aa5c521d411a5ab5703838c61fab8532da2 Mon Sep 17 00:00:00 2001 From: Andreas Shimokawa Date: Thu, 21 May 2015 18:17:39 +0200 Subject: [PATCH] Pebble: Get Morpheuz sleep data visualize through SleepMonitorActivity This very very experimental, and needs a complete overhaul. But it is a start ;) --- app/src/main/AndroidManifest.xml | 7 + .../gadgetbridge/AppManagerActivity.java | 14 +- .../gadgetbridge/SleepMonitorActivity.java | 116 ++++++++++++++ .../gadgetbridge/pebble/MorpheuzSupport.java | 144 ++++++++++++++++++ .../gadgetbridge/pebble/PebbleIoThread.java | 56 ++++--- .../gadgetbridge/pebble/PebbleProtocol.java | 85 ++++++++--- .../protocol/GBDeviceCommand.java | 1 + .../GBDeviceCommandSleepMonitorResult.java | 14 ++ .../main/res/layout/activity_sleepmonitor.xml | 16 ++ app/src/main/res/values/strings.xml | 2 + 10 files changed, 409 insertions(+), 46 deletions(-) create mode 100644 app/src/main/java/nodomain/freeyourgadget/gadgetbridge/SleepMonitorActivity.java create mode 100644 app/src/main/java/nodomain/freeyourgadget/gadgetbridge/pebble/MorpheuzSupport.java create mode 100644 app/src/main/java/nodomain/freeyourgadget/gadgetbridge/protocol/GBDeviceCommandSleepMonitorResult.java create mode 100644 app/src/main/res/layout/activity_sleepmonitor.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 591ab410..c22caf77 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -55,6 +55,13 @@ android:name="android.support.PARENT_ACTIVITY" android:value=".ControlCenter" /> + + + diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/AppManagerActivity.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/AppManagerActivity.java index f8779391..6cb86761 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/AppManagerActivity.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/AppManagerActivity.java @@ -22,6 +22,7 @@ import java.util.List; import java.util.UUID; import nodomain.freeyourgadget.gadgetbridge.adapter.GBDeviceAppAdapter; +import nodomain.freeyourgadget.gadgetbridge.pebble.MorpheuzSupport; public class AppManagerActivity extends Activity { @@ -67,10 +68,15 @@ public class AppManagerActivity extends Activity { appListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView parent, View v, int position, long id) { - Intent startIntent = new Intent(AppManagerActivity.this, BluetoothCommunicationService.class); - startIntent.setAction(BluetoothCommunicationService.ACTION_STARTAPP); - startIntent.putExtra("app_uuid", appList.get(position).getUUID().toString()); - startService(startIntent); + UUID uuid = appList.get(position).getUUID(); + Intent startAppIntent = new Intent(AppManagerActivity.this, BluetoothCommunicationService.class); + startAppIntent.setAction(BluetoothCommunicationService.ACTION_STARTAPP); + startAppIntent.putExtra("app_uuid", uuid.toString()); + startService(startAppIntent); + if (MorpheuzSupport.uuid.equals(uuid)) { + Intent startIntent = new Intent(AppManagerActivity.this, SleepMonitorActivity.class); + startActivity(startIntent); + } } }); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/SleepMonitorActivity.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/SleepMonitorActivity.java new file mode 100644 index 00000000..1ba33e78 --- /dev/null +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/SleepMonitorActivity.java @@ -0,0 +1,116 @@ +package nodomain.freeyourgadget.gadgetbridge; + +import android.app.Activity; +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.content.IntentFilter; +import android.graphics.Canvas; +import android.graphics.Color; +import android.graphics.Paint; +import android.graphics.Path; +import android.os.Bundle; +import android.support.v4.app.NavUtils; +import android.support.v4.content.LocalBroadcastManager; +import android.view.MenuItem; +import android.view.SurfaceHolder; +import android.view.SurfaceView; +import android.widget.TextView; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Date; + + +public class SleepMonitorActivity extends Activity { + public static final String ACTION_REFRESH + = "nodomain.freeyourgadget.gadgetbride.sleepmonitor.action.refresh"; + private static final Logger LOG = LoggerFactory.getLogger(SleepMonitorActivity.class); + + private SurfaceView surfaceView; + private TextView textView; + + private BroadcastReceiver mReceiver = new BroadcastReceiver() { + @Override + public void onReceive(Context context, Intent intent) { + String action = intent.getAction(); + if (action.equals(ControlCenter.ACTION_QUIT)) { + finish(); + } else if (action.equals(ACTION_REFRESH)) { + int smartalarm_from = intent.getIntExtra("smartalarm_from", -1); + int smartalarm_to = intent.getIntExtra("smartalarm_to", -1); + int recording_base_timestamp = intent.getIntExtra("recording_base_timestamp", -1); + int alarm_gone_off = intent.getIntExtra("alarm_gone_off", -1); + + Calendar cal = Calendar.getInstance(); + cal.setTimeInMillis((long) recording_base_timestamp * 1000L); + Date date = cal.getTime(); + String dateString = new SimpleDateFormat("dd.MM.yyyy HH:mm").format(date); + textView.setText(dateString + " till " + alarm_gone_off / 60 + ":" + alarm_gone_off % 60); + short[] points = intent.getShortArrayExtra("points"); + + SurfaceHolder surfaceHolder = surfaceView.getHolder(); + + if (surfaceHolder.getSurface().isValid() && points.length > 1) { + Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); + Canvas canvas = surfaceHolder.lockCanvas(); + paint.setColor(Color.WHITE); + paint.setStrokeWidth(2); + canvas.drawRGB(100, 100, 100); + int width = canvas.getWidth(); + int height = canvas.getHeight(); + + Path path = new Path(); + path.moveTo(0.0f, height); + float x = 0.0f; + for (int i = 0; i < points.length; i++) { + float y = (1.0f - (float) points[i] / 5000.0f) * height; + x = (float) i / (points.length - 1) * width; + path.lineTo(x, y); + } + path.lineTo(x, height); + path.close(); + canvas.drawPath(path, paint); + + surfaceHolder.unlockCanvasAndPost(canvas); + } + } + } + }; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_sleepmonitor); + + textView = (TextView) findViewById(R.id.textView); + surfaceView = (SurfaceView) findViewById(R.id.surfaceView); + + getActionBar().setDisplayHomeAsUpEnabled(true); + + IntentFilter filter = new IntentFilter(); + filter.addAction(ControlCenter.ACTION_QUIT); + filter.addAction(ACTION_REFRESH); + + LocalBroadcastManager.getInstance(this).registerReceiver(mReceiver, filter); + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + switch (item.getItemId()) { + case android.R.id.home: + NavUtils.navigateUpFromSameTask(this); + return true; + } + return super.onOptionsItemSelected(item); + } + + @Override + protected void onDestroy() { + LocalBroadcastManager.getInstance(this).unregisterReceiver(mReceiver); + super.onDestroy(); + } +} diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/pebble/MorpheuzSupport.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/pebble/MorpheuzSupport.java new file mode 100644 index 00000000..15a42eb9 --- /dev/null +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/pebble/MorpheuzSupport.java @@ -0,0 +1,144 @@ +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.SimpleTimeZone; +import java.util.TimeZone; +import java.util.UUID; + +import nodomain.freeyourgadget.gadgetbridge.protocol.GBDeviceCommand; +import nodomain.freeyourgadget.gadgetbridge.protocol.GBDeviceCommandSendBytes; +import nodomain.freeyourgadget.gadgetbridge.protocol.GBDeviceCommandSleepMonitorResult; + +public class MorpheuzSupport { + + public static final int KEY_POINT = 1; + public static final int KEY_CTRL = 2; + public static final int KEY_FROM = 3; + public static final int KEY_TO = 4; + public static final int KEY_BASE = 5; + public static final int KEY_VERSION = 6; + public static final int KEY_GONEOFF = 7; + public static final int KEY_TRANSMIT = 8; + public static final int CTRL_TRANSMIT_DONE = 1; + public static final int CTRL_VERSION_DONE = 2; + public static final int CTRL_GONEOFF_DONE = 4; + public static final int CTRL_DO_NEXT = 8; + 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; + + private boolean sent_to_gadgetbridge = false; + // data received from Morpheuz in native format + private short[] points = new short[54]; + private int points_last_valid = -1; + 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 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 static final Logger LOG = LoggerFactory.getLogger(MorpheuzSupport.class); + + public MorpheuzSupport(PebbleProtocol pebbleProtocol) { + mPebbleProtocol = pebbleProtocol; + } + + private byte[] encodeMorpheuzMessage(int key, int value) { + ArrayList> pairs = new ArrayList<>(); + pairs.add(new Pair(key, value)); + 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) { + for (Pair pair : pairs) { + int ctrl_message = 0; + switch (pair.first) { + case KEY_GONEOFF: + alarm_gone_off = (int) pair.second; + LOG.info("got gone off: " + alarm_gone_off / 60 + ":" + alarm_gone_off % 60); + /* super-ugly hack: if if did not notice GadgetBridge yet, do so and delay confirmation so Morpheuz + * will resend gone off data. The second time, we acknowledge it. + * + * this can be fixed by allowing to return multiple GBDeviceCommands + */ + if (sent_to_gadgetbridge) { + ctrl_message = MorpheuzSupport.CTRL_VERSION_DONE | MorpheuzSupport.CTRL_GONEOFF_DONE | MorpheuzSupport.CTRL_TRANSMIT_DONE | MorpheuzSupport.CTRL_SET_LAST_SENT; + } else { + GBDeviceCommandSleepMonitorResult sleepMonitorResult = new GBDeviceCommandSleepMonitorResult(); + sleepMonitorResult.points = new short[points_last_valid + 1]; + System.arraycopy(points, 0, sleepMonitorResult.points, 0, points_last_valid + 1); + sleepMonitorResult.smartalarm_from = smartalarm_from; + sleepMonitorResult.smartalarm_to = smartalarm_to; + sleepMonitorResult.alarm_gone_off = alarm_gone_off; + sleepMonitorResult.recording_base_timestamp = recording_base_timestamp; + sent_to_gadgetbridge = true; + return sleepMonitorResult; + } + break; + case KEY_POINT: + if (recording_base_timestamp == -1) { + // 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; + } else { + short index = (short) ((int) pair.second >> 16); + short data = (short) ((int) pair.second & 0xffff); + LOG.info("got point:" + index + " " + data); + if (index >= 0 && index < 54) { + points[index] = data; + points_last_valid = index; + } + + ctrl_message = MorpheuzSupport.CTRL_VERSION_DONE | MorpheuzSupport.CTRL_SET_LAST_SENT | MorpheuzSupport.CTRL_DO_NEXT; + } + break; + case KEY_FROM: + smartalarm_from = (int) pair.second; + 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; + break; + case KEY_TO: + smartalarm_to = (int) pair.second; + 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; + break; + case KEY_VERSION: + LOG.info("got version: " + ((float) ((int) pair.second) / 10.0f)); + ctrl_message = MorpheuzSupport.CTRL_VERSION_DONE | MorpheuzSupport.CTRL_SET_LAST_SENT; + sent_to_gadgetbridge = false; + break; + case KEY_BASE: + // fix timestamp + TimeZone tz = SimpleTimeZone.getDefault(); + recording_base_timestamp = (int) pair.second - (tz.getOffset(System.currentTimeMillis())) / 1000; + LOG.info("got base: " + recording_base_timestamp); + ctrl_message = MorpheuzSupport.CTRL_VERSION_DONE | MorpheuzSupport.CTRL_SET_LAST_SENT | MorpheuzSupport.CTRL_DO_NEXT; + break; + default: + LOG.info("unhandled key: " + pair.first); + break; + } + if (ctrl_message > 0) { + GBDeviceCommandSendBytes sendBytes = new GBDeviceCommandSendBytes(); + sendBytes.encodedBytes = encodeMorpheuzMessage(MorpheuzSupport.KEY_CTRL, ctrl_message); + return sendBytes; + } + } + GBDeviceCommandSendBytes sendBytes = new GBDeviceCommandSendBytes(); + sendBytes.encodedBytes = mPebbleProtocol.encodeApplicationMessageAck(uuid, mPebbleProtocol.last_id); + return sendBytes; + } +} diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/pebble/PebbleIoThread.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/pebble/PebbleIoThread.java index 58862d5a..25fae854 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/pebble/PebbleIoThread.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/pebble/PebbleIoThread.java @@ -31,33 +31,21 @@ import nodomain.freeyourgadget.gadgetbridge.GBDevice; import nodomain.freeyourgadget.gadgetbridge.GBDeviceIoThread; import nodomain.freeyourgadget.gadgetbridge.GBMusicControlReceiver; import nodomain.freeyourgadget.gadgetbridge.R; +import nodomain.freeyourgadget.gadgetbridge.SleepMonitorActivity; import nodomain.freeyourgadget.gadgetbridge.protocol.GBDeviceCommand; import nodomain.freeyourgadget.gadgetbridge.protocol.GBDeviceCommandAppInfo; import nodomain.freeyourgadget.gadgetbridge.protocol.GBDeviceCommandAppManagementResult; import nodomain.freeyourgadget.gadgetbridge.protocol.GBDeviceCommandCallControl; import nodomain.freeyourgadget.gadgetbridge.protocol.GBDeviceCommandMusicControl; import nodomain.freeyourgadget.gadgetbridge.protocol.GBDeviceCommandSendBytes; +import nodomain.freeyourgadget.gadgetbridge.protocol.GBDeviceCommandSleepMonitorResult; import nodomain.freeyourgadget.gadgetbridge.protocol.GBDeviceCommandVersionInfo; import nodomain.freeyourgadget.gadgetbridge.protocol.GBDeviceProtocol; public class PebbleIoThread extends GBDeviceIoThread { private static final Logger LOG = LoggerFactory.getLogger(PebbleIoThread.class); private static final int NOTIFICATION_ID = 2; - - private enum PebbleAppInstallState { - UNKNOWN, - APP_WAIT_SLOT, - APP_START_INSTALL, - APP_WAIT_TOKEN, - APP_UPLOAD_CHUNK, - APP_UPLOAD_COMMIT, - APP_WAIT_COMMIT, - APP_UPLOAD_COMPLETE, - APP_REFRESH, - } - private final PebbleProtocol mPebbleProtocol; - private BluetoothAdapter mBtAdapter = null; private BluetoothSocket mBtSocket = null; private InputStream mInStream = null; @@ -66,7 +54,6 @@ public class PebbleIoThread extends GBDeviceIoThread { private boolean mIsConnected = false; private boolean mIsInstalling = false; private int mConnectionAttempts = 0; - /* app installation */ private Uri mInstallURI = null; private PBWReader mPBWReader = null; @@ -80,6 +67,12 @@ public class PebbleIoThread extends GBDeviceIoThread { private int mBinarySize = -1; private int mBytesWritten = -1; + public PebbleIoThread(GBDevice gbDevice, GBDeviceProtocol gbDeviceProtocol, BluetoothAdapter btAdapter, Context context) { + super(gbDevice, context); + mPebbleProtocol = (PebbleProtocol) gbDeviceProtocol; + mBtAdapter = btAdapter; + } + public static Notification createInstallNotification(String text, boolean ongoing, int percentage, Context context) { Intent notificationIntent = new Intent(context, AppManagerActivity.class); @@ -111,12 +104,6 @@ public class PebbleIoThread extends GBDeviceIoThread { nm.notify(NOTIFICATION_ID, notification); } - public PebbleIoThread(GBDevice gbDevice, GBDeviceProtocol gbDeviceProtocol, BluetoothAdapter btAdapter, Context context) { - super(gbDevice, context); - mPebbleProtocol = (PebbleProtocol) gbDeviceProtocol; - mBtAdapter = btAdapter; - } - @Override protected boolean connect(String btDeviceAddress) { BluetoothDevice btDevice = mBtAdapter.getRemoteDevice(btDeviceAddress); @@ -368,7 +355,7 @@ public class PebbleIoThread extends GBDeviceIoThread { case APP_INFO: LOG.info("Got command for APP_INFO"); GBDeviceCommandAppInfo appInfoCmd = (GBDeviceCommandAppInfo) deviceCmd; - setInstallSlot(appInfoCmd.freeSlot); + setInstallSlot(appInfoCmd.freeSlot); // FIXME: Pebble specific Intent appInfoIntent = new Intent(AppManagerActivity.ACTION_REFRESH_APPLIST); int appCount = appInfoCmd.apps.length; @@ -381,6 +368,19 @@ public class PebbleIoThread extends GBDeviceIoThread { } LocalBroadcastManager.getInstance(context).sendBroadcast(appInfoIntent); break; + case SLEEP_MONITOR_RES: + LOG.info("Got command for SLEEP_MONIOR_RES"); + GBDeviceCommandSleepMonitorResult sleepMonitorResult = (GBDeviceCommandSleepMonitorResult) deviceCmd; + + Intent sleepMontiorIntent = new Intent(SleepMonitorActivity.ACTION_REFRESH); + sleepMontiorIntent.putExtra("smartalarm_from", sleepMonitorResult.smartalarm_from); + sleepMontiorIntent.putExtra("smartalarm_to", sleepMonitorResult.smartalarm_to); + sleepMontiorIntent.putExtra("recording_base_timestamp", sleepMonitorResult.recording_base_timestamp); + sleepMontiorIntent.putExtra("alarm_gone_off", sleepMonitorResult.alarm_gone_off); + sleepMontiorIntent.putExtra("points", sleepMonitorResult.points); + + LocalBroadcastManager.getInstance(context).sendBroadcast(sleepMontiorIntent); + break; case SEND_BYTES: GBDeviceCommandSendBytes sendBytes = (GBDeviceCommandSendBytes) deviceCmd; write(sendBytes.encodedBytes); @@ -492,4 +492,16 @@ public class PebbleIoThread extends GBDeviceIoThread { } } } + + private enum PebbleAppInstallState { + UNKNOWN, + APP_WAIT_SLOT, + APP_START_INSTALL, + APP_WAIT_TOKEN, + APP_UPLOAD_CHUNK, + APP_UPLOAD_COMMIT, + APP_WAIT_COMMIT, + APP_UPLOAD_COMPLETE, + APP_REFRESH, + } } \ No newline at end of file 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 395b0122..a00c7288 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/pebble/PebbleProtocol.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/pebble/PebbleProtocol.java @@ -8,6 +8,7 @@ import org.slf4j.LoggerFactory; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.util.ArrayList; +import java.util.Arrays; import java.util.Random; import java.util.SimpleTimeZone; import java.util.TimeZone; @@ -146,6 +147,10 @@ public class PebbleProtocol extends GBDeviceProtocol { static final byte PHONEVERSION_REMOTE_OS_LINUX = 4; static final byte PHONEVERSION_REMOTE_OS_WINDOWS = 5; + static final byte TYPE_BYTEARRAY = 0; + static final byte TYPE_CSTRING = 1; + static final byte TYPE_UINT32 = 2; + static final byte TYPE_INT32 = 3; static final short LENGTH_PREFIX = 4; static final short LENGTH_SETTIME = 5; @@ -164,9 +169,10 @@ public class PebbleProtocol extends GBDeviceProtocol { private static Random mRandom = new Random(); - private byte last_id = -1; + byte last_id = -1; 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"); @@ -539,33 +545,69 @@ public class PebbleProtocol extends GBDeviceProtocol { return buf.array(); } - private byte[] encodeApplicationMessageTest() { + private byte[] encodeApplicationMessageWeatherNeatTest() { // encode push message for WeatherNeat ArrayList> pairs = new ArrayList<>(); - pairs.add(new Pair<>(1, (Object) "Berlin")); // city - pairs.add(new Pair<>(2, (Object) "22 C")); // temperature - pairs.add(new Pair<>(3, (Object) "windy")); // condition + 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 acknowledgeing the last message) - buf.order(ByteOrder.BIG_ENDIAN); - buf.putShort((short) 18); - buf.putShort(ENDPOINT_APPLICATIONMESSAGE); - buf.put(APPLICATIONMESSAGE_ACK); - buf.put((byte) (last_id - 1)); - buf.putLong(WeatherNeatUUID.getMostSignificantBits()); - buf.putLong(WeatherNeatUUID.getLeastSignificantBits()); - + // 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 - public byte[] encodeApplicationMessagePush(short endpoint, UUID uuid, ArrayList> pairs) { + buf.order(ByteOrder.BIG_ENDIAN); + buf.putShort((short) 18); + buf.putShort(ENDPOINT_APPLICATIONMESSAGE); + buf.put(APPLICATIONMESSAGE_ACK); + buf.put(id); + buf.putLong(uuid.getMostSignificantBits()); + buf.putLong(uuid.getMostSignificantBits()); + + return buf.array(); + } + + + private ArrayList> decodeDict(ByteBuffer buf) { + ArrayList> dict = new ArrayList>(); + buf.order(ByteOrder.LITTLE_ENDIAN); + byte dictSize = buf.get(); + while (dictSize-- > 0) { + Integer key = buf.getInt(); + byte type = buf.get(); + short length = buf.getShort(); // length + switch (type) { + case TYPE_INT32: + case TYPE_UINT32: + dict.add(new Pair(key, buf.getInt())); + break; + case TYPE_CSTRING: + case TYPE_BYTEARRAY: + byte[] bytes = new byte[length]; + buf.get(bytes); + if (type == TYPE_BYTEARRAY) { + dict.add(new Pair(key, bytes)); + } else { + dict.add(new Pair(key, Arrays.toString(bytes))); + } + break; + default: + } + } + return dict; + } + + byte[] encodeApplicationMessagePush(short endpoint, UUID uuid, ArrayList> pairs) { int length = 16 + 3; // UUID + (PUSH + id + length of dict) for (Pair pair : pairs) { length += 7; // key + type + length @@ -589,11 +631,11 @@ public class PebbleProtocol extends GBDeviceProtocol { for (Pair pair : pairs) { buf.putInt(pair.first); if (pair.second instanceof Integer) { - buf.put((byte) 0); + buf.put(TYPE_INT32); buf.putShort((short) 4); // length of int buf.putInt((int) pair.second); } else if (pair.second instanceof String) { - buf.put((byte) 1); + buf.put(TYPE_CSTRING); buf.putShort((short) (((String) pair.second).length() + 1)); buf.put(((String) pair.second).getBytes()); buf.put((byte) 0); @@ -761,16 +803,19 @@ public class PebbleProtocol extends GBDeviceProtocol { last_id = buf.get(); long uuid_high = buf.getLong(); long uuid_low = buf.getLong(); - byte dictSize = buf.get(); + switch (pebbleCmd) { case APPLICATIONMESSAGE_PUSH: UUID uuid = new UUID(uuid_high, uuid_low); - LOG.info("got APPLICATIONMESSAGE PUSH from UUID " + uuid + " , dict size " + dictSize); + 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 = encodeApplicationMessageTest(); + sendBytes.encodedBytes = encodeApplicationMessageWeatherNeatTest(); cmd = sendBytes; + } else if (MorpheuzSupport.uuid.equals(uuid)) { + ArrayList> dict = decodeDict(buf); + cmd = mMorpheuzSupport.handleMessage(dict); } break; case APPLICATIONMESSAGE_ACK: diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/protocol/GBDeviceCommand.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/protocol/GBDeviceCommand.java index 6775e37f..f74bc7ff 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/protocol/GBDeviceCommand.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/protocol/GBDeviceCommand.java @@ -12,6 +12,7 @@ public abstract class GBDeviceCommand { VERSION_INFO, APP_MANAGEMENT_RES, SEND_BYTES, + SLEEP_MONITOR_RES, } } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/protocol/GBDeviceCommandSleepMonitorResult.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/protocol/GBDeviceCommandSleepMonitorResult.java new file mode 100644 index 00000000..3937448a --- /dev/null +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/protocol/GBDeviceCommandSleepMonitorResult.java @@ -0,0 +1,14 @@ +package nodomain.freeyourgadget.gadgetbridge.protocol; + +public class GBDeviceCommandSleepMonitorResult extends GBDeviceCommand { + // FIXME: this is just the low-level data from Morpheuz, we need something generic + public short[] points; + public int smartalarm_from = -1; // time in minutes relative from 0:00 for smart alarm (earliest) + public int smartalarm_to = -1;// time in minutes relative from 0:00 for smart alarm (latest) + public int recording_base_timestamp = -1; // timestamp for the first "point", all folowing are +10 minutes offset each + public int alarm_gone_off = -1; // time in minutes relative from 0:00 when alarm gone off + + public GBDeviceCommandSleepMonitorResult() { + commandClass = CommandClass.SLEEP_MONITOR_RES; + } +} diff --git a/app/src/main/res/layout/activity_sleepmonitor.xml b/app/src/main/res/layout/activity_sleepmonitor.xml new file mode 100644 index 00000000..35021c22 --- /dev/null +++ b/app/src/main/res/layout/activity_sleepmonitor.xml @@ -0,0 +1,16 @@ + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index ab1941ac..1d5a6f4a 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -100,4 +100,6 @@ Weight in kg Vibration Count + Sleep Monitor +