1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-04 01:09:47 +01:00

refactoring: add generic support for manually filtering samples by acticty kind in AbstractSampleProvider

This allows a lot of simplifications for Morpheuz and Misfit
This commit is contained in:
Andreas Shimokawa 2016-08-07 11:45:09 +02:00
parent 6b2565e4c9
commit c4f83d68cd
6 changed files with 61 additions and 168 deletions

View File

@ -162,7 +162,7 @@ public class GBDaoGenerator {
private static Entity addPebbleMorpheuzActivitySample(Schema schema, Entity user, Entity device) {
Entity activitySample = addEntity(schema, "PebbleMorpheuzSample");
addCommonActivitySampleProperties("AbstractActivitySample", activitySample, user, device);
addCommonActivitySampleProperties("AbstractPebbleMorpheuzActivitySample", activitySample, user, device);
activitySample.addIntProperty("rawIntensity").notNull();
addCommonActivitySampleProperties2(activitySample, user, device);
return activitySample;

View File

@ -1,5 +1,6 @@
package nodomain.freeyourgadget.gadgetbridge.devices;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@ -44,12 +45,20 @@ public abstract class AbstractSampleProvider<T extends AbstractActivitySample> i
@Override
public List<T> getActivitySamples(int timestamp_from, int timestamp_to) {
if (getRawKindSampleProperty() != null) {
return getGBActivitySamples(timestamp_from, timestamp_to, ActivityKind.TYPE_ACTIVITY);
} else {
return getActivitySamplesByActivityFilter(timestamp_from, timestamp_to, ActivityKind.TYPE_ACTIVITY);
}
}
@Override
public List<T> getSleepSamples(int timestamp_from, int timestamp_to) {
if (getRawKindSampleProperty() != null) {
return getGBActivitySamples(timestamp_from, timestamp_to, ActivityKind.TYPE_SLEEP);
} else {
return getActivitySamplesByActivityFilter(timestamp_from, timestamp_to, ActivityKind.TYPE_SLEEP);
}
}
@Override
@ -147,6 +156,18 @@ public abstract class AbstractSampleProvider<T extends AbstractActivitySample> i
trailingConditions);
}
private List<T> getActivitySamplesByActivityFilter(int timestamp_from, int timestamp_to, int activityFilter) {
List<T> samples = getAllActivitySamples(timestamp_from, timestamp_to);
List<T> filteredSamples = new ArrayList<>();
for (T sample : samples) {
if ((sample.getKind() & activityFilter) != 0) {
filteredSamples.add(sample);
}
}
return filteredSamples;
}
public abstract AbstractDao<T,?> getSampleDao();
protected abstract Property getRawKindSampleProperty();

View File

@ -1,30 +1,20 @@
package nodomain.freeyourgadget.gadgetbridge.devices.pebble;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import de.greenrobot.dao.AbstractDao;
import de.greenrobot.dao.Property;
import de.greenrobot.dao.query.QueryBuilder;
import nodomain.freeyourgadget.gadgetbridge.database.DBHelper;
import nodomain.freeyourgadget.gadgetbridge.devices.AbstractSampleProvider;
import nodomain.freeyourgadget.gadgetbridge.devices.SampleProvider;
import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession;
import nodomain.freeyourgadget.gadgetbridge.entities.Device;
import nodomain.freeyourgadget.gadgetbridge.entities.PebbleMisfitSample;
import nodomain.freeyourgadget.gadgetbridge.entities.PebbleMisfitSampleDao;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.model.ActivityKind;
public class PebbleMisfitSampleProvider implements SampleProvider<PebbleMisfitSample> {
private final DaoSession mSession;
private final GBDevice mDevice;
public class PebbleMisfitSampleProvider extends AbstractSampleProvider<PebbleMisfitSample> {
protected final float movementDivisor = 300f;
public PebbleMisfitSampleProvider(GBDevice device, DaoSession session) {
mSession = session;
mDevice = device;
super(device, session);
}
@Override
@ -53,73 +43,13 @@ public class PebbleMisfitSampleProvider implements SampleProvider<PebbleMisfitSa
}
@Override
public List<PebbleMisfitSample> getAllActivitySamples(int timestamp_from, int timestamp_to) {
return getGBActivitySamples(timestamp_from, timestamp_to, ActivityKind.TYPE_ALL);
}
@Override
public List<PebbleMisfitSample> getActivitySamples(int timestamp_from, int timestamp_to) {
return getGBActivitySamples(timestamp_from, timestamp_to, ActivityKind.TYPE_ACTIVITY);
}
@Override
public List<PebbleMisfitSample> getSleepSamples(int timestamp_from, int timestamp_to) {
return getGBActivitySamples(timestamp_from, timestamp_to, ActivityKind.TYPE_SLEEP);
}
@Override
public int fetchLatestTimestamp() {
QueryBuilder<PebbleMisfitSample> qb = getSampleDao().queryBuilder();
qb.orderDesc(getTimestampSampleProperty());
qb.limit(1);
List<PebbleMisfitSample> list = qb.build().list();
if (list.size() >= 1) {
return list.get(0).getTimestamp();
}
return -1;
}
@Override
public void addGBActivitySample(PebbleMisfitSample activitySample) {
getSampleDao().insertOrReplace(activitySample);
}
@Override
public void addGBActivitySamples(PebbleMisfitSample[] activitySamples) {
getSampleDao().insertOrReplaceInTx(activitySamples);
}
public void changeStoredSamplesType(int timestampFrom, int timestampTo, int kind) {
}
public void changeStoredSamplesType(int timestampFrom, int timestampTo, int fromKind, int toKind) {
}
protected List<PebbleMisfitSample> getGBActivitySamples(int timestamp_from, int timestamp_to, int activityType) {
QueryBuilder<PebbleMisfitSample> qb = getSampleDao().queryBuilder();
Property timestampProperty = getTimestampSampleProperty();
Device dbDevice = DBHelper.findDevice(mDevice, mSession);
if (dbDevice == null) {
// no device, no samples
return Collections.emptyList();
}
Property deviceProperty = getDeviceIdentifierSampleProperty();
qb.where(deviceProperty.eq(dbDevice.getId()), timestampProperty.ge(timestamp_from))
.where(timestampProperty.le(timestamp_to));
List<PebbleMisfitSample> samples = qb.build().list();
List<PebbleMisfitSample> filteredSamples = new ArrayList<>();
for (PebbleMisfitSample sample : samples) {
if ((sample.getRawKind() & activityType) != 0) {
sample.setProvider(this);
filteredSamples.add(sample);
}
}
return filteredSamples;
}
public AbstractDao<PebbleMisfitSample, ?> getSampleDao() {
return mSession.getPebbleMisfitSampleDao();
return getSession().getPebbleMisfitSampleDao();
}
@Override
protected Property getRawKindSampleProperty() {
return null;
}
protected Property getTimestampSampleProperty() {
@ -129,5 +59,4 @@ public class PebbleMisfitSampleProvider implements SampleProvider<PebbleMisfitSa
protected Property getDeviceIdentifierSampleProperty() {
return PebbleMisfitSampleDao.Properties.DeviceId;
}
}

View File

@ -1,8 +1,5 @@
package nodomain.freeyourgadget.gadgetbridge.devices.pebble;
import java.util.ArrayList;
import java.util.List;
import de.greenrobot.dao.AbstractDao;
import de.greenrobot.dao.Property;
import nodomain.freeyourgadget.gadgetbridge.devices.AbstractSampleProvider;
@ -11,14 +8,8 @@ import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession;
import nodomain.freeyourgadget.gadgetbridge.entities.PebbleMorpheuzSample;
import nodomain.freeyourgadget.gadgetbridge.entities.PebbleMorpheuzSampleDao;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.model.ActivityKind;
public class PebbleMorpheuzSampleProvider extends AbstractSampleProvider<PebbleMorpheuzSample> {
// raw types
public static final int TYPE_DEEP_SLEEP = 5;
public static final int TYPE_LIGHT_SLEEP = 4;
public static final int TYPE_ACTIVITY = 1;
public static final int TYPE_UNKNOWN = 0;
protected float movementDivisor = 5000f;
@ -51,73 +42,24 @@ public class PebbleMorpheuzSampleProvider extends AbstractSampleProvider<PebbleM
return new PebbleMorpheuzSample();
}
@Override
public int normalizeType(int rawType) {
switch (rawType) {
case TYPE_DEEP_SLEEP:
return ActivityKind.TYPE_DEEP_SLEEP;
case TYPE_LIGHT_SLEEP:
return ActivityKind.TYPE_LIGHT_SLEEP;
case TYPE_ACTIVITY:
return ActivityKind.TYPE_ACTIVITY;
default:
return ActivityKind.TYPE_UNKNOWN;
}
}
@Override
public int toRawActivityKind(int activityKind) {
switch (activityKind) {
case ActivityKind.TYPE_ACTIVITY:
return TYPE_ACTIVITY;
case ActivityKind.TYPE_DEEP_SLEEP:
return TYPE_DEEP_SLEEP;
case ActivityKind.TYPE_LIGHT_SLEEP:
return TYPE_LIGHT_SLEEP;
default:
return TYPE_UNKNOWN;
}
}
@Override
public float normalizeIntensity(int rawIntensity) {
return rawIntensity / movementDivisor;
}
@Override
public List<PebbleMorpheuzSample> getActivitySamples(int timestamp_from, int timestamp_to) {
List<PebbleMorpheuzSample> samples = getAllActivitySamples(timestamp_from, timestamp_to);
List<PebbleMorpheuzSample> filteredSamples = new ArrayList<>();
for (PebbleMorpheuzSample sample : samples) {
if (sample.getRawIntensity() > 1000) {
sample.setRawKind(ActivityKind.TYPE_ACTIVITY);
filteredSamples.add(sample);
}
}
return filteredSamples;
}
@Override
public List<PebbleMorpheuzSample> getSleepSamples(int timestamp_from, int timestamp_to) {
List<PebbleMorpheuzSample> samples = getAllActivitySamples(timestamp_from, timestamp_to);
List<PebbleMorpheuzSample> filteredSamples = new ArrayList<>();
for (PebbleMorpheuzSample sample : samples) {
if (sample.getRawIntensity() < 1000) {
if (sample.getRawIntensity() <= 120) {
sample.setRawKind(ActivityKind.TYPE_DEEP_SLEEP);
} else {
sample.setRawKind(ActivityKind.TYPE_LIGHT_SLEEP);
}
filteredSamples.add(sample);
}
}
return filteredSamples;
}
@Override
public int getID() {
return SampleProvider.PROVIDER_PEBBLE_MORPHEUZ;
}
@Override
public int normalizeType(int rawType) {
return rawType;
}
@Override
public int toRawActivityKind(int activityKind) {
return activityKind;
}
}

View File

@ -41,7 +41,7 @@ public abstract class AbstractPebbleMisfitActivitySample extends AbstractActivit
}
@Override
public int getRawKind() {
public int getKind() {
calculate();
return activityKind;
}
@ -51,20 +51,4 @@ public abstract class AbstractPebbleMisfitActivitySample extends AbstractActivit
calculate();
return intensity;
}
@Override
public void setRawKind(int kind) {
}
@Override
public void setRawIntensity(int intensity) {
}
@Override
public void setSteps(int steps) {
}
}

View File

@ -0,0 +1,17 @@
package nodomain.freeyourgadget.gadgetbridge.entities;
import nodomain.freeyourgadget.gadgetbridge.model.ActivityKind;
public abstract class AbstractPebbleMorpheuzActivitySample extends AbstractActivitySample {
@Override
public int getKind() {
int rawIntensity = getRawIntensity();
if (rawIntensity <= 120) {
return ActivityKind.TYPE_DEEP_SLEEP;
} else if (rawIntensity <= 1000) {
return ActivityKind.TYPE_LIGHT_SLEEP;
}
return ActivityKind.TYPE_ACTIVITY;
}
}