Pebble: dynamic appKey suppoort for Morpheuz

This commit is contained in:
Andreas Shimokawa 2017-01-24 11:58:13 +01:00
parent 746eeda777
commit fb7db523c7
1 changed files with 124 additions and 107 deletions

View File

@ -1,11 +1,18 @@
package nodomain.freeyourgadget.gadgetbridge.service.devices.pebble; package nodomain.freeyourgadget.gadgetbridge.service.devices.pebble;
import android.util.Pair; import android.util.Pair;
import android.widget.Toast;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Objects;
import java.util.SimpleTimeZone; import java.util.SimpleTimeZone;
import java.util.TimeZone; import java.util.TimeZone;
import java.util.UUID; import java.util.UUID;
@ -18,31 +25,21 @@ import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventSendBytes;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventSleepMonitorResult; import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventSleepMonitorResult;
import nodomain.freeyourgadget.gadgetbridge.devices.pebble.PebbleMorpheuzSampleProvider; import nodomain.freeyourgadget.gadgetbridge.devices.pebble.PebbleMorpheuzSampleProvider;
import nodomain.freeyourgadget.gadgetbridge.entities.PebbleMorpheuzSample; import nodomain.freeyourgadget.gadgetbridge.entities.PebbleMorpheuzSample;
import nodomain.freeyourgadget.gadgetbridge.util.GB;
import nodomain.freeyourgadget.gadgetbridge.util.Prefs; import nodomain.freeyourgadget.gadgetbridge.util.Prefs;
class AppMessageHandlerMorpheuz extends AppMessageHandler { class AppMessageHandlerMorpheuz extends AppMessageHandler {
private Integer keyPoint;
private static final int KEY_POINT = 1; private Integer keyCtrl;
private static final int KEY_POINT_46 = 10000; private Integer keyFrom;
private static final int KEY_CTRL = 2; private Integer keyTo;
private static final int KEY_CTRL_46 = 10001; private Integer keyBase;
private static final int KEY_FROM = 3; private Integer keyVersion;
private static final int KEY_FROM_46 = 10002; private Integer keyGoneoff;
private static final int KEY_TO = 4; private Integer keyTransmit;
private static final int KEY_TO_46 = 10003; private Integer keyAutoReset;
private static final int KEY_BASE = 5; private Integer keySnoozes;
private static final int KEY_BASE_46 = 10004; private Integer keyFault;
private static final int KEY_VERSION = 6;
private static final int KEY_VERSION_46 = 10005;
private static final int KEY_GONEOFF = 7;
private static final int KEY_GONEOFF_46 = 10006;
private static final int KEY_TRANSMIT = 8;
private static final int KEY_TRANSMIT_46 = 10007;
private static final int KEY_AUTO_RESET = 9;
private static final int KEY_AUTO_RESET_46 = 10008;
private static final int KEY_SNOOZES = 10;
private static final int KEY_SNOOZES_46 = 10009;
private static final int KEY_FAULT_46 = 10010;
private static final int CTRL_TRANSMIT_DONE = 1; private static final int CTRL_TRANSMIT_DONE = 1;
private static final int CTRL_VERSION_DONE = 2; private static final int CTRL_VERSION_DONE = 2;
@ -53,7 +50,6 @@ class AppMessageHandlerMorpheuz extends AppMessageHandler {
private static final int CTRL_SNOOZES_DONE = 64; private static final int CTRL_SNOOZES_DONE = 64;
// data received from Morpheuz in native format // data received from Morpheuz in native format
private int version = 0;
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
@ -63,6 +59,53 @@ class AppMessageHandlerMorpheuz extends AppMessageHandler {
public AppMessageHandlerMorpheuz(UUID uuid, PebbleProtocol pebbleProtocol) { public AppMessageHandlerMorpheuz(UUID uuid, PebbleProtocol pebbleProtocol) {
super(uuid, pebbleProtocol); super(uuid, pebbleProtocol);
messageKeys = new HashMap<>();
try {
JSONObject appKeys = getAppKeys();
Iterator<String> appKeysIterator = appKeys.keys();
while (appKeysIterator.hasNext()) {
String current = appKeysIterator.next();
int appKey = appKeys.getInt(current);
switch (current) {
case "keyPoint":
keyPoint = appKey;
break;
case "keyCtrl":
keyCtrl = appKey;
break;
case "keyFrom":
keyFrom = appKey;
break;
case "keyTo":
keyTo = appKey;
break;
case "keyBase":
keyBase = appKey;
break;
case "keyVersion":
keyVersion = appKey;
break;
case "keyGoneoff":
keyGoneoff = appKey;
break;
case "keyTransmit":
keyTransmit = appKey;
break;
case "keyAutoReset":
keyAutoReset = appKey;
break;
case "keySnoozes":
keySnoozes = appKey;
break;
case "keyFault":
keyFault = appKey;
break;
}
}
} catch (IOException | JSONException e) {
GB.toast("There was an error accessing the watchface configuration.", Toast.LENGTH_LONG, GB.ERROR);
}
} }
private byte[] encodeMorpheuzMessage(int key, int value) { private byte[] encodeMorpheuzMessage(int key, int value) {
@ -84,88 +127,66 @@ class AppMessageHandlerMorpheuz extends AppMessageHandler {
GBDeviceEventSleepMonitorResult sleepMonitorResult = null; GBDeviceEventSleepMonitorResult sleepMonitorResult = null;
for (Pair<Integer, Object> pair : pairs) { for (Pair<Integer, Object> pair : pairs) {
switch (pair.first) { if (Objects.equals(pair.first, keyTransmit)) {
case KEY_TRANSMIT: sleepMonitorResult = new GBDeviceEventSleepMonitorResult();
case KEY_TRANSMIT_46: sleepMonitorResult.smartalarm_from = smartalarm_from;
sleepMonitorResult = new GBDeviceEventSleepMonitorResult(); sleepMonitorResult.smartalarm_to = smartalarm_to;
sleepMonitorResult.smartalarm_from = smartalarm_from; sleepMonitorResult.alarm_gone_off = alarm_gone_off;
sleepMonitorResult.smartalarm_to = smartalarm_to; sleepMonitorResult.recording_base_timestamp = recording_base_timestamp;
sleepMonitorResult.alarm_gone_off = alarm_gone_off; ctrl_message |= CTRL_TRANSMIT_DONE;
sleepMonitorResult.recording_base_timestamp = recording_base_timestamp; } else if (pair.first.equals(keyGoneoff)) {
ctrl_message |= CTRL_TRANSMIT_DONE; alarm_gone_off = (int) pair.second;
break; LOG.info("got gone off: " + alarm_gone_off / 60 + ":" + alarm_gone_off % 60);
case KEY_GONEOFF: ctrl_message |= CTRL_DO_NEXT | CTRL_GONEOFF_DONE;
case KEY_GONEOFF_46: } else if (pair.first.equals(keyPoint)) {
alarm_gone_off = (int) pair.second; if (recording_base_timestamp == -1) {
LOG.info("got gone off: " + alarm_gone_off / 60 + ":" + alarm_gone_off % 60); // we have no base timestamp but received points, stop this
ctrl_message |= CTRL_DO_NEXT | CTRL_GONEOFF_DONE; ctrl_message = CTRL_VERSION_DONE | CTRL_GONEOFF_DONE | CTRL_TRANSMIT_DONE | CTRL_SET_LAST_SENT;
break; } else {
case KEY_POINT: int index = ((int) pair.second >> 16);
case KEY_POINT_46: int intensity = ((int) pair.second & 0xffff);
if (recording_base_timestamp == -1) { LOG.info("got point:" + index + " " + intensity);
// we have no base timestamp but received points, stop this if (index >= 0) {
ctrl_message = CTRL_VERSION_DONE | CTRL_GONEOFF_DONE | CTRL_TRANSMIT_DONE | CTRL_SET_LAST_SENT; try (DBHandler db = GBApplication.acquireDB()) {
} else { Long userId = DBHelper.getUser(db.getDaoSession()).getId();
int index = ((int) pair.second >> 16); Long deviceId = DBHelper.getDevice(getDevice(), db.getDaoSession()).getId();
int intensity = ((int) pair.second & 0xffff); PebbleMorpheuzSampleProvider sampleProvider = new PebbleMorpheuzSampleProvider(getDevice(), db.getDaoSession());
LOG.info("got point:" + index + " " + intensity); PebbleMorpheuzSample sample = new PebbleMorpheuzSample(recording_base_timestamp + index * 600, deviceId, userId, intensity);
if (index >= 0) { sample.setProvider(sampleProvider);
try (DBHandler db = GBApplication.acquireDB()) { sampleProvider.addGBActivitySample(sample);
Long userId = DBHelper.getUser(db.getDaoSession()).getId(); } catch (Exception e) {
Long deviceId = DBHelper.getDevice(getDevice(), db.getDaoSession()).getId(); LOG.error("Error acquiring database", e);
PebbleMorpheuzSampleProvider sampleProvider = new PebbleMorpheuzSampleProvider(getDevice(), db.getDaoSession());
PebbleMorpheuzSample sample = new PebbleMorpheuzSample(recording_base_timestamp + index * 600, deviceId, userId, intensity);
sample.setProvider(sampleProvider);
sampleProvider.addGBActivitySample(sample);
} catch (Exception e) {
LOG.error("Error acquiring database", e);
}
} }
ctrl_message |= CTRL_SET_LAST_SENT | CTRL_DO_NEXT;
} }
break;
case KEY_FROM:
case KEY_FROM_46:
smartalarm_from = (int) pair.second;
LOG.info("got from: " + smartalarm_from / 60 + ":" + smartalarm_from % 60);
ctrl_message |= CTRL_SET_LAST_SENT | CTRL_DO_NEXT; ctrl_message |= CTRL_SET_LAST_SENT | CTRL_DO_NEXT;
break; }
case KEY_TO: } else if (pair.first.equals(keyFrom)) {
case KEY_TO_46: smartalarm_from = (int) pair.second;
smartalarm_to = (int) pair.second; LOG.info("got from: " + smartalarm_from / 60 + ":" + smartalarm_from % 60);
LOG.info("got to: " + smartalarm_to / 60 + ":" + smartalarm_to % 60); ctrl_message |= CTRL_SET_LAST_SENT | CTRL_DO_NEXT;
ctrl_message |= CTRL_SET_LAST_SENT | CTRL_DO_NEXT; } else if (pair.first.equals(keyTo)) {
break; smartalarm_to = (int) pair.second;
case KEY_VERSION: LOG.info("got to: " + smartalarm_to / 60 + ":" + smartalarm_to % 60);
case KEY_VERSION_46: ctrl_message |= CTRL_SET_LAST_SENT | CTRL_DO_NEXT;
version = (int) pair.second; } else if (pair.first.equals(keyVersion)) {
LOG.info("got version: " + ((float) version / 10.0f)); int version = (int) pair.second;
ctrl_message |= CTRL_VERSION_DONE; LOG.info("got version: " + ((float) version / 10.0f));
break; ctrl_message |= CTRL_VERSION_DONE;
case KEY_BASE: } else if (pair.first.equals(keyBase)) {// fix timestamp
case KEY_BASE_46: TimeZone tz = SimpleTimeZone.getDefault();
// fix timestamp recording_base_timestamp = (int) pair.second - (tz.getOffset(System.currentTimeMillis())) / 1000;
TimeZone tz = SimpleTimeZone.getDefault(); LOG.info("got base: " + recording_base_timestamp);
recording_base_timestamp = (int) pair.second - (tz.getOffset(System.currentTimeMillis())) / 1000; ctrl_message |= CTRL_SET_LAST_SENT | CTRL_DO_NEXT;
LOG.info("got base: " + recording_base_timestamp); } else if (pair.first.equals(keyAutoReset)) {
ctrl_message |= CTRL_SET_LAST_SENT | CTRL_DO_NEXT; ctrl_message |= CTRL_SET_LAST_SENT | CTRL_DO_NEXT;
break; } else if (pair.first.equals(keySnoozes)) {
case KEY_AUTO_RESET: ctrl_message |= CTRL_SNOOZES_DONE | CTRL_DO_NEXT;
case KEY_AUTO_RESET_46: } else if (pair.first.equals(keyFault)) {
ctrl_message |= CTRL_SET_LAST_SENT | CTRL_DO_NEXT; LOG.info("fault code: " + (int) pair.second);
break; ctrl_message |= CTRL_DO_NEXT;
case KEY_SNOOZES: } else {
case KEY_SNOOZES_46: LOG.info("unhandled key: " + pair.first);
ctrl_message |= CTRL_SNOOZES_DONE | CTRL_DO_NEXT;
break;
case KEY_FAULT_46:
LOG.info("fault code: " + (int) pair.second);
ctrl_message |= CTRL_DO_NEXT;
break;
default:
LOG.info("unhandled key: " + pair.first);
break;
} }
} }
@ -177,11 +198,7 @@ class AppMessageHandlerMorpheuz extends AppMessageHandler {
GBDeviceEventSendBytes sendBytesCtrl = null; GBDeviceEventSendBytes sendBytesCtrl = null;
if (ctrl_message > 0) { if (ctrl_message > 0) {
sendBytesCtrl = new GBDeviceEventSendBytes(); sendBytesCtrl = new GBDeviceEventSendBytes();
int ctrlkey = KEY_CTRL; sendBytesCtrl.encodedBytes = encodeMorpheuzMessage(keyCtrl, ctrl_message);
if (version >= 46) {
ctrlkey = KEY_CTRL_46;
}
sendBytesCtrl.encodedBytes = encodeMorpheuzMessage(ctrlkey, ctrl_message);
} }
// ctrl and sleep monitor might be null, thats okay // ctrl and sleep monitor might be null, thats okay