1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-12-25 18:15:49 +01:00

Unify SleepMonitor with code from PR #59. Thanks Daniele!

This commit is contained in:
Andreas Shimokawa 2015-06-05 19:32:09 +02:00
parent 2f1908e480
commit 813a02d5c7
3 changed files with 59 additions and 21 deletions

View File

@ -4,7 +4,9 @@ public class GBActivitySample {
public static final byte PROVIDER_MIBAND = 0;
public static final byte PROVIDER_PEBBLE_MORPHEUZ = 1;
public static final byte TYPE_SLEEP = 0; //FIXME: we could just adapt to Mi Band (Dont know the correct values)
public static final byte TYPE_DEEP_SLEEP = 5;
public static final byte TYPE_LIGHT_SLEEP = 4;
public static final byte TYPE_UNKNOWN = -1;
// add more here
private final int timestamp;

View File

@ -96,9 +96,27 @@ public class SleepMonitorActivity extends Activity implements SurfaceHolder.Call
int height = canvas.getHeight();
RectF r = new RectF(0.0f, 0.0f, 0.0f, height);
short last_movement = 5000;
float movement_divisor;
boolean annotate;
boolean use_steps_as_movement;
switch (provider) {
case GBActivitySample.PROVIDER_MIBAND:
movement_divisor = 256.0f;
annotate = false; // sample density to high?
use_steps_as_movement = true;
break;
default: // Morpheuz
movement_divisor = 5000.0f;
annotate = true;
use_steps_as_movement = false;
break;
}
byte last_type = GBActivitySample.TYPE_UNKNOWN;
for (int i = 0; i < samples.size(); i++) {
GBActivitySample sample = samples.get(i);
byte type = sample.getType();
if (i == 0) {
cal.setTimeInMillis((long) sample.getTimestamp() * 1000L);
@ -109,30 +127,42 @@ public class SleepMonitorActivity extends Activity implements SurfaceHolder.Call
date = cal.getTime();
dateStringTo = new SimpleDateFormat("dd.MM.yyyy HH:mm").format(date);
}
boolean annotate = false;
short movement = sample.getIntensity();
r.left = r.right;
r.right = (float) (i + 1) / (samples.size()) * width;
r.top = (1.0f - (float) movement / 5000.0f) * height;
if (movement > 1000) {
paint.setColor(Color.RED);
if (last_movement <= 1000) {
annotate = true;
}
} else if (movement > 120) {
paint.setColor(Color.YELLOW);
if (type == GBActivitySample.TYPE_DEEP_SLEEP) {
paint.setColor(Color.BLUE);
r.top = 0.98f * height;
} else {
paint.setColor(Color.GREEN);
if (type == GBActivitySample.TYPE_LIGHT_SLEEP) {
paint.setColor(Color.CYAN);
} else {
if (use_steps_as_movement) {
movement = sample.getSteps();
}
paint.setColor(Color.YELLOW);
}
r.top = (1.0f - (float) movement / movement_divisor) * height;
}
canvas.drawRect(r, paint);
boolean annotate_this = false;
if (annotate) {
cal.setTimeInMillis((long) (sample.getTimestamp()) * 1000L);
date = cal.getTime();
String dateString = new SimpleDateFormat("HH:mm").format(date);
paint.setColor(Color.WHITE);
canvas.drawText(dateString, r.left - 20, r.top - 20, paint);
if (type != GBActivitySample.TYPE_DEEP_SLEEP && type != GBActivitySample.TYPE_LIGHT_SLEEP &&
(last_type == GBActivitySample.TYPE_DEEP_SLEEP || last_type == GBActivitySample.TYPE_LIGHT_SLEEP)) {
// seems that we woke up
annotate_this = true;
}
if (annotate_this) {
cal.setTimeInMillis((long) (sample.getTimestamp()) * 1000L);
date = cal.getTime();
String dateString = new SimpleDateFormat("HH:mm").format(date);
paint.setColor(Color.WHITE);
canvas.drawText(dateString, r.left - 20, r.top - 20, paint);
}
last_type = type;
}
last_movement = movement;
}
textView.setText(dateStringFrom + " to " + dateStringTo);

View File

@ -98,10 +98,16 @@ public class MorpheuzSupport {
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);
short intensity = (short) ((int) pair.second & 0xffff);
LOG.info("got point:" + index + " " + intensity);
byte type = GBActivitySample.TYPE_UNKNOWN;
if (intensity <= 120) {
type = GBActivitySample.TYPE_DEEP_SLEEP;
} else if (intensity <= 1000) {
type = GBActivitySample.TYPE_LIGHT_SLEEP;
}
if (index >= 0 && index < 54) {
GBApplication.getActivityDatabaseHandler().addGBActivitySample(recording_base_timestamp + index * 600, GBActivitySample.PROVIDER_PEBBLE_MORPHEUZ, data, (byte) 0, GBActivitySample.TYPE_SLEEP);
GBApplication.getActivityDatabaseHandler().addGBActivitySample(recording_base_timestamp + index * 600, GBActivitySample.PROVIDER_PEBBLE_MORPHEUZ, intensity, (byte) 0, type);
}
ctrl_message = MorpheuzSupport.CTRL_VERSION_DONE | MorpheuzSupport.CTRL_SET_LAST_SENT | MorpheuzSupport.CTRL_DO_NEXT;