mirror of
https://codeberg.org/Freeyourgadget/Gadgetbridge
synced 2024-11-28 21:06:50 +01:00
Pebble: dynamic appKey suppoort for Morpheuz
This commit is contained in:
parent
746eeda777
commit
fb7db523c7
@ -1,11 +1,18 @@
|
||||
package nodomain.freeyourgadget.gadgetbridge.service.devices.pebble;
|
||||
|
||||
import android.util.Pair;
|
||||
import android.widget.Toast;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Objects;
|
||||
import java.util.SimpleTimeZone;
|
||||
import java.util.TimeZone;
|
||||
import java.util.UUID;
|
||||
@ -18,31 +25,21 @@ import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventSendBytes;
|
||||
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventSleepMonitorResult;
|
||||
import nodomain.freeyourgadget.gadgetbridge.devices.pebble.PebbleMorpheuzSampleProvider;
|
||||
import nodomain.freeyourgadget.gadgetbridge.entities.PebbleMorpheuzSample;
|
||||
import nodomain.freeyourgadget.gadgetbridge.util.GB;
|
||||
import nodomain.freeyourgadget.gadgetbridge.util.Prefs;
|
||||
|
||||
class AppMessageHandlerMorpheuz extends AppMessageHandler {
|
||||
|
||||
private static final int KEY_POINT = 1;
|
||||
private static final int KEY_POINT_46 = 10000;
|
||||
private static final int KEY_CTRL = 2;
|
||||
private static final int KEY_CTRL_46 = 10001;
|
||||
private static final int KEY_FROM = 3;
|
||||
private static final int KEY_FROM_46 = 10002;
|
||||
private static final int KEY_TO = 4;
|
||||
private static final int KEY_TO_46 = 10003;
|
||||
private static final int KEY_BASE = 5;
|
||||
private static final int KEY_BASE_46 = 10004;
|
||||
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 Integer keyPoint;
|
||||
private Integer keyCtrl;
|
||||
private Integer keyFrom;
|
||||
private Integer keyTo;
|
||||
private Integer keyBase;
|
||||
private Integer keyVersion;
|
||||
private Integer keyGoneoff;
|
||||
private Integer keyTransmit;
|
||||
private Integer keyAutoReset;
|
||||
private Integer keySnoozes;
|
||||
private Integer keyFault;
|
||||
|
||||
private static final int CTRL_TRANSMIT_DONE = 1;
|
||||
private static final int CTRL_VERSION_DONE = 2;
|
||||
@ -53,7 +50,6 @@ class AppMessageHandlerMorpheuz extends AppMessageHandler {
|
||||
private static final int CTRL_SNOOZES_DONE = 64;
|
||||
|
||||
// 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_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
|
||||
@ -63,6 +59,53 @@ class AppMessageHandlerMorpheuz extends AppMessageHandler {
|
||||
|
||||
public AppMessageHandlerMorpheuz(UUID uuid, PebbleProtocol 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) {
|
||||
@ -84,88 +127,66 @@ class AppMessageHandlerMorpheuz extends AppMessageHandler {
|
||||
GBDeviceEventSleepMonitorResult sleepMonitorResult = null;
|
||||
|
||||
for (Pair<Integer, Object> pair : pairs) {
|
||||
switch (pair.first) {
|
||||
case KEY_TRANSMIT:
|
||||
case KEY_TRANSMIT_46:
|
||||
sleepMonitorResult = new GBDeviceEventSleepMonitorResult();
|
||||
sleepMonitorResult.smartalarm_from = smartalarm_from;
|
||||
sleepMonitorResult.smartalarm_to = smartalarm_to;
|
||||
sleepMonitorResult.alarm_gone_off = alarm_gone_off;
|
||||
sleepMonitorResult.recording_base_timestamp = recording_base_timestamp;
|
||||
ctrl_message |= CTRL_TRANSMIT_DONE;
|
||||
break;
|
||||
case KEY_GONEOFF:
|
||||
case KEY_GONEOFF_46:
|
||||
alarm_gone_off = (int) pair.second;
|
||||
LOG.info("got gone off: " + alarm_gone_off / 60 + ":" + alarm_gone_off % 60);
|
||||
ctrl_message |= CTRL_DO_NEXT | CTRL_GONEOFF_DONE;
|
||||
break;
|
||||
case KEY_POINT:
|
||||
case KEY_POINT_46:
|
||||
if (recording_base_timestamp == -1) {
|
||||
// we have no base timestamp but received points, stop this
|
||||
ctrl_message = CTRL_VERSION_DONE | CTRL_GONEOFF_DONE | CTRL_TRANSMIT_DONE | CTRL_SET_LAST_SENT;
|
||||
} else {
|
||||
int index = ((int) pair.second >> 16);
|
||||
int intensity = ((int) pair.second & 0xffff);
|
||||
LOG.info("got point:" + index + " " + intensity);
|
||||
if (index >= 0) {
|
||||
try (DBHandler db = GBApplication.acquireDB()) {
|
||||
Long userId = DBHelper.getUser(db.getDaoSession()).getId();
|
||||
Long deviceId = DBHelper.getDevice(getDevice(), db.getDaoSession()).getId();
|
||||
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);
|
||||
}
|
||||
if (Objects.equals(pair.first, keyTransmit)) {
|
||||
sleepMonitorResult = new GBDeviceEventSleepMonitorResult();
|
||||
sleepMonitorResult.smartalarm_from = smartalarm_from;
|
||||
sleepMonitorResult.smartalarm_to = smartalarm_to;
|
||||
sleepMonitorResult.alarm_gone_off = alarm_gone_off;
|
||||
sleepMonitorResult.recording_base_timestamp = recording_base_timestamp;
|
||||
ctrl_message |= CTRL_TRANSMIT_DONE;
|
||||
} else if (pair.first.equals(keyGoneoff)) {
|
||||
alarm_gone_off = (int) pair.second;
|
||||
LOG.info("got gone off: " + alarm_gone_off / 60 + ":" + alarm_gone_off % 60);
|
||||
ctrl_message |= CTRL_DO_NEXT | CTRL_GONEOFF_DONE;
|
||||
} else if (pair.first.equals(keyPoint)) {
|
||||
if (recording_base_timestamp == -1) {
|
||||
// we have no base timestamp but received points, stop this
|
||||
ctrl_message = CTRL_VERSION_DONE | CTRL_GONEOFF_DONE | CTRL_TRANSMIT_DONE | CTRL_SET_LAST_SENT;
|
||||
} else {
|
||||
int index = ((int) pair.second >> 16);
|
||||
int intensity = ((int) pair.second & 0xffff);
|
||||
LOG.info("got point:" + index + " " + intensity);
|
||||
if (index >= 0) {
|
||||
try (DBHandler db = GBApplication.acquireDB()) {
|
||||
Long userId = DBHelper.getUser(db.getDaoSession()).getId();
|
||||
Long deviceId = DBHelper.getDevice(getDevice(), db.getDaoSession()).getId();
|
||||
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;
|
||||
break;
|
||||
case KEY_TO:
|
||||
case KEY_TO_46:
|
||||
smartalarm_to = (int) pair.second;
|
||||
LOG.info("got to: " + smartalarm_to / 60 + ":" + smartalarm_to % 60);
|
||||
ctrl_message |= CTRL_SET_LAST_SENT | CTRL_DO_NEXT;
|
||||
break;
|
||||
case KEY_VERSION:
|
||||
case KEY_VERSION_46:
|
||||
version = (int) pair.second;
|
||||
LOG.info("got version: " + ((float) version / 10.0f));
|
||||
ctrl_message |= CTRL_VERSION_DONE;
|
||||
break;
|
||||
case KEY_BASE:
|
||||
case KEY_BASE_46:
|
||||
// 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 |= CTRL_SET_LAST_SENT | CTRL_DO_NEXT;
|
||||
break;
|
||||
case KEY_AUTO_RESET:
|
||||
case KEY_AUTO_RESET_46:
|
||||
ctrl_message |= CTRL_SET_LAST_SENT | CTRL_DO_NEXT;
|
||||
break;
|
||||
case KEY_SNOOZES:
|
||||
case KEY_SNOOZES_46:
|
||||
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;
|
||||
}
|
||||
} else if (pair.first.equals(keyFrom)) {
|
||||
smartalarm_from = (int) pair.second;
|
||||
LOG.info("got from: " + smartalarm_from / 60 + ":" + smartalarm_from % 60);
|
||||
ctrl_message |= CTRL_SET_LAST_SENT | CTRL_DO_NEXT;
|
||||
} else if (pair.first.equals(keyTo)) {
|
||||
smartalarm_to = (int) pair.second;
|
||||
LOG.info("got to: " + smartalarm_to / 60 + ":" + smartalarm_to % 60);
|
||||
ctrl_message |= CTRL_SET_LAST_SENT | CTRL_DO_NEXT;
|
||||
} else if (pair.first.equals(keyVersion)) {
|
||||
int version = (int) pair.second;
|
||||
LOG.info("got version: " + ((float) version / 10.0f));
|
||||
ctrl_message |= CTRL_VERSION_DONE;
|
||||
} else if (pair.first.equals(keyBase)) {// 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 |= CTRL_SET_LAST_SENT | CTRL_DO_NEXT;
|
||||
} else if (pair.first.equals(keyAutoReset)) {
|
||||
ctrl_message |= CTRL_SET_LAST_SENT | CTRL_DO_NEXT;
|
||||
} else if (pair.first.equals(keySnoozes)) {
|
||||
ctrl_message |= CTRL_SNOOZES_DONE | CTRL_DO_NEXT;
|
||||
} else if (pair.first.equals(keyFault)) {
|
||||
LOG.info("fault code: " + (int) pair.second);
|
||||
ctrl_message |= CTRL_DO_NEXT;
|
||||
} else {
|
||||
LOG.info("unhandled key: " + pair.first);
|
||||
}
|
||||
}
|
||||
|
||||
@ -177,11 +198,7 @@ class AppMessageHandlerMorpheuz extends AppMessageHandler {
|
||||
GBDeviceEventSendBytes sendBytesCtrl = null;
|
||||
if (ctrl_message > 0) {
|
||||
sendBytesCtrl = new GBDeviceEventSendBytes();
|
||||
int ctrlkey = KEY_CTRL;
|
||||
if (version >= 46) {
|
||||
ctrlkey = KEY_CTRL_46;
|
||||
}
|
||||
sendBytesCtrl.encodedBytes = encodeMorpheuzMessage(ctrlkey, ctrl_message);
|
||||
sendBytesCtrl.encodedBytes = encodeMorpheuzMessage(keyCtrl, ctrl_message);
|
||||
}
|
||||
|
||||
// ctrl and sleep monitor might be null, thats okay
|
||||
|
Loading…
Reference in New Issue
Block a user