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

sampleproviders now return device specific data #206

This commit is contained in:
cpfeiffer 2016-06-27 20:41:20 +02:00
parent 358cd6df5e
commit e70a2290c3
22 changed files with 98 additions and 53 deletions

View File

@ -296,7 +296,7 @@ public abstract class AbstractChartFragment extends AbstractGBFragment {
protected SampleProvider<? extends AbstractActivitySample> getProvider(DBHandler db, GBDevice device) {
DeviceCoordinator coordinator = DeviceHelper.getInstance().getCoordinator(device);
return coordinator.getSampleProvider(db.getDaoSession());
return coordinator.getSampleProvider(device, db.getDaoSession());
}
/**

View File

@ -260,15 +260,20 @@ public class DBHelper {
return false;
}
public static Device getDevice(GBDevice gbDevice, DaoSession session) {
public static Device findDevice(GBDevice gbDevice, DaoSession session) {
DeviceDao deviceDao = session.getDeviceDao();
Query<Device> query = deviceDao.queryBuilder().where(DeviceDao.Properties.Identifier.eq(gbDevice.getAddress())).build();
List<Device> devices = query.list();
Device device;
if (devices.isEmpty()) {
if (devices.size() > 0) {
return devices.get(0);
}
return null;
}
public static Device getDevice(GBDevice gbDevice, DaoSession session) {
Device device = findDevice(gbDevice, session);
if (device == null) {
device = createDevice(session, gbDevice);
} else {
device = devices.get(0);
}
ensureDeviceAttributes(device, gbDevice, session);
@ -348,7 +353,7 @@ public class DBHelper {
try (SQLiteDatabase oldDB = oldDbHandler.getReadableDatabase()) {
User user = DBHelper.getUser(session);
for (DeviceCoordinator coordinator : DeviceHelper.getInstance().getAllCoordinators()) {
AbstractSampleProvider<? extends AbstractActivitySample> sampleProvider = (AbstractSampleProvider<? extends AbstractActivitySample>) coordinator.getSampleProvider(session);
AbstractSampleProvider<? extends AbstractActivitySample> sampleProvider = (AbstractSampleProvider<? extends AbstractActivitySample>) coordinator.getSampleProvider(targetDevice, session);
importActivitySamples(oldDB, targetDevice, session, sampleProvider, user);
}
}

View File

@ -1,13 +1,17 @@
package nodomain.freeyourgadget.gadgetbridge.devices;
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 de.greenrobot.dao.query.WhereCondition;
import nodomain.freeyourgadget.gadgetbridge.database.DBHelper;
import nodomain.freeyourgadget.gadgetbridge.entities.AbstractActivitySample;
import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession;
import nodomain.freeyourgadget.gadgetbridge.entities.Device;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.model.ActivityKind;
/**
@ -18,11 +22,17 @@ import nodomain.freeyourgadget.gadgetbridge.model.ActivityKind;
public abstract class AbstractSampleProvider<T extends AbstractActivitySample> implements SampleProvider<T> {
private static final WhereCondition[] NO_CONDITIONS = new WhereCondition[0];
private final DaoSession mSession;
private final GBDevice mDevice;
protected AbstractSampleProvider(DaoSession session) {
protected AbstractSampleProvider(GBDevice device, DaoSession session) {
mDevice = device;
mSession = session;
}
public GBDevice getmDevice() {
return mDevice;
}
public DaoSession getSession() {
return mSession;
}
@ -83,7 +93,13 @@ public abstract class AbstractSampleProvider<T extends AbstractActivitySample> i
protected List<T> getGBActivitySamples(int timestamp_from, int timestamp_to, int activityType) {
QueryBuilder<T> qb = getSampleDao().queryBuilder();
Property timestampProperty = getTimestampSampleProperty();
qb.where(timestampProperty.ge(timestamp_from))
Device dbDevice = DBHelper.findDevice(getmDevice(), getSession());
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), getClauseForActivityType(qb, activityType));
List<T> samples = qb.build().list();
for (T sample : samples) {
@ -131,4 +147,5 @@ public abstract class AbstractSampleProvider<T extends AbstractActivitySample> i
protected abstract Property getRawKindSampleProperty();
protected abstract Property getTimestampSampleProperty();
protected abstract Property getDeviceIdentifierSampleProperty();
}

View File

@ -86,7 +86,7 @@ public interface DeviceCoordinator {
*
* @return
*/
SampleProvider<? extends ActivitySample> getSampleProvider(DaoSession session);
SampleProvider<? extends ActivitySample> getSampleProvider(GBDevice device, DaoSession session);
/**
* Finds an install handler for the given uri that can install the given

View File

@ -111,7 +111,7 @@ public class UnknownDeviceCoordinator extends AbstractDeviceCoordinator {
}
@Override
public SampleProvider<?> getSampleProvider(DaoSession session) {
public SampleProvider<?> getSampleProvider(GBDevice device, DaoSession session) {
return new UnknownSampleProvider();
}

View File

@ -56,8 +56,8 @@ public class MiBandCoordinator extends AbstractDeviceCoordinator {
}
@Override
public SampleProvider<? extends AbstractActivitySample> getSampleProvider(DaoSession session) {
return new MiBandSampleProvider(session);
public SampleProvider<? extends AbstractActivitySample> getSampleProvider(GBDevice device, DaoSession session) {
return new MiBandSampleProvider(device, session);
}
@Override

View File

@ -7,6 +7,7 @@ import nodomain.freeyourgadget.gadgetbridge.devices.SampleProvider;
import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession;
import nodomain.freeyourgadget.gadgetbridge.entities.MiBandActivitySample;
import nodomain.freeyourgadget.gadgetbridge.entities.MiBandActivitySampleDao;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.model.ActivityKind;
public class MiBandSampleProvider extends AbstractSampleProvider<MiBandActivitySample> {
@ -28,8 +29,8 @@ public class MiBandSampleProvider extends AbstractSampleProvider<MiBandActivityS
// maybe this should be configurable 256 seems way off, though.
private final float movementDivisor = 180.0f; //256.0f;
public MiBandSampleProvider(DaoSession session) {
super(session);
public MiBandSampleProvider(GBDevice device, DaoSession session) {
super(device, session);
}
@Override
@ -88,6 +89,11 @@ public class MiBandSampleProvider extends AbstractSampleProvider<MiBandActivityS
return MiBandActivitySampleDao.Properties.Timestamp;
}
@Override
protected Property getDeviceIdentifierSampleProperty() {
return MiBandActivitySampleDao.Properties.DeviceId;
}
@Override
protected Property getRawKindSampleProperty() {
return MiBandActivitySampleDao.Properties.RawKind;

View File

@ -4,12 +4,14 @@ import de.greenrobot.dao.AbstractDao;
import de.greenrobot.dao.Property;
import nodomain.freeyourgadget.gadgetbridge.devices.AbstractSampleProvider;
import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession;
import nodomain.freeyourgadget.gadgetbridge.entities.MiBandActivitySampleDao;
import nodomain.freeyourgadget.gadgetbridge.entities.PebbleActivitySample;
import nodomain.freeyourgadget.gadgetbridge.entities.PebbleActivitySampleDao;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
public abstract class AbstractPebbleSampleProvider extends AbstractSampleProvider<PebbleActivitySample> {
protected AbstractPebbleSampleProvider(DaoSession session) {
super(session);
protected AbstractPebbleSampleProvider(GBDevice device, DaoSession session) {
super(device, session);
}
@Override
@ -27,6 +29,11 @@ public abstract class AbstractPebbleSampleProvider extends AbstractSampleProvide
return PebbleActivitySampleDao.Properties.RawKind;
}
@Override
protected Property getDeviceIdentifierSampleProperty() {
return PebbleActivitySampleDao.Properties.DeviceId;
}
@Override
public PebbleActivitySample createActivitySample() {
return new PebbleActivitySample();

View File

@ -3,6 +3,7 @@ package nodomain.freeyourgadget.gadgetbridge.devices.pebble;
import nodomain.freeyourgadget.gadgetbridge.devices.SampleProvider;
import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession;
import nodomain.freeyourgadget.gadgetbridge.entities.PebbleActivitySample;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.model.ActivityKind;
public class HealthSampleProvider extends AbstractPebbleSampleProvider {
@ -12,8 +13,8 @@ public class HealthSampleProvider extends AbstractPebbleSampleProvider {
protected final float movementDivisor = 8000f;
public HealthSampleProvider(DaoSession session) {
super(session);
public HealthSampleProvider(GBDevice device, DaoSession session) {
super(device, session);
}
@Override

View File

@ -2,13 +2,14 @@ package nodomain.freeyourgadget.gadgetbridge.devices.pebble;
import nodomain.freeyourgadget.gadgetbridge.devices.SampleProvider;
import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
public class MisfitSampleProvider extends AbstractPebbleSampleProvider {
protected final float movementDivisor = 300f;
public MisfitSampleProvider(DaoSession session) {
super(session);
public MisfitSampleProvider(GBDevice device, DaoSession session) {
super(device, session);
}
@Override

View File

@ -2,6 +2,7 @@ package nodomain.freeyourgadget.gadgetbridge.devices.pebble;
import nodomain.freeyourgadget.gadgetbridge.devices.SampleProvider;
import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.model.ActivityKind;
public class MorpheuzSampleProvider extends AbstractPebbleSampleProvider {
@ -13,8 +14,8 @@ public class MorpheuzSampleProvider extends AbstractPebbleSampleProvider {
protected float movementDivisor = 5000f;
public MorpheuzSampleProvider(DaoSession session) {
super(session);
public MorpheuzSampleProvider(GBDevice device, DaoSession session) {
super(device, session);
}
@Override

View File

@ -46,20 +46,20 @@ public class PebbleCoordinator extends AbstractDeviceCoordinator {
}
@Override
public SampleProvider<? extends AbstractActivitySample> getSampleProvider(DaoSession session) {
public SampleProvider<? extends AbstractActivitySample> getSampleProvider(GBDevice device, DaoSession session) {
Prefs prefs = GBApplication.getPrefs();
int activityTracker = prefs.getInt("pebble_activitytracker", SampleProvider.PROVIDER_PEBBLE_HEALTH);
switch (activityTracker) {
case SampleProvider.PROVIDER_PEBBLE_HEALTH:
return new HealthSampleProvider(session);
return new HealthSampleProvider(device, session);
case SampleProvider.PROVIDER_PEBBLE_MISFIT:
return new MisfitSampleProvider(session);
return new MisfitSampleProvider(device, session);
case SampleProvider.PROVIDER_PEBBLE_MORPHEUZ:
return new MorpheuzSampleProvider(session);
return new MorpheuzSampleProvider(device, session);
case SampleProvider.PROVIDER_PEBBLE_GADGETBRIDGE:
return new PebbleGadgetBridgeSampleProvider(session);
return new PebbleGadgetBridgeSampleProvider(device, session);
default:
return new HealthSampleProvider(session);
return new HealthSampleProvider(device, session);
}
}

View File

@ -2,10 +2,11 @@ package nodomain.freeyourgadget.gadgetbridge.devices.pebble;
import nodomain.freeyourgadget.gadgetbridge.devices.SampleProvider;
import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
public class PebbleGadgetBridgeSampleProvider extends MorpheuzSampleProvider {
public PebbleGadgetBridgeSampleProvider(DaoSession session) {
super(session);
public PebbleGadgetBridgeSampleProvider(GBDevice device, DaoSession session) {
super(device, session);
movementDivisor = 63.0f;
}

View File

@ -305,7 +305,7 @@ public class FetchActivityOperation extends AbstractMiBandOperation {
byte category, intensity, steps, heartrate = 0;
try (DBHandler dbHandler = GBApplication.acquireDB()){
MiBandSampleProvider provider = new MiBandSampleProvider(dbHandler.getDaoSession());
MiBandSampleProvider provider = new MiBandSampleProvider(getDevice(), dbHandler.getDaoSession());
Long userId = DBHelper.getUser(dbHandler.getDaoSession()).getId();
Long deviceId = DBHelper.getDevice(getDevice(), dbHandler.getDaoSession()).getId();
int minutes = 0;

View File

@ -58,7 +58,7 @@ public class AppMessageHandlerGBPebble extends AppMessageHandler {
try (DBHandler db = GBApplication.acquireDB()) {
User user = DBHelper.getUser(db.getDaoSession());
Device device = DBHelper.getDevice(getDevice(), db.getDaoSession());
PebbleGadgetBridgeSampleProvider sampleProvider = new PebbleGadgetBridgeSampleProvider(db.getDaoSession());
PebbleGadgetBridgeSampleProvider sampleProvider = new PebbleGadgetBridgeSampleProvider(getDevice(), db.getDaoSession());
PebbleActivitySample[] activitySamples = new PebbleActivitySample[samples_remaining];
int i = 0;
while (samples_remaining-- > 0) {

View File

@ -22,6 +22,7 @@ import nodomain.freeyourgadget.gadgetbridge.devices.pebble.MisfitSampleProvider;
import nodomain.freeyourgadget.gadgetbridge.entities.AbstractActivitySample;
import nodomain.freeyourgadget.gadgetbridge.entities.PebbleActivitySample;
import nodomain.freeyourgadget.gadgetbridge.impl.GBActivitySample;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.model.ActivityKind;
import nodomain.freeyourgadget.gadgetbridge.util.Prefs;
@ -51,6 +52,7 @@ public class AppMessageHandlerMisfit extends AppMessageHandler {
@Override
public GBDeviceEvent[] handleMessage(ArrayList<Pair<Integer, Object>> pairs) {
GBDevice device = getDevice();
for (Pair<Integer, Object> pair : pairs) {
switch (pair.first) {
case KEY_INCOMING_DATA_BEGIN:
@ -116,7 +118,7 @@ public class AppMessageHandlerMisfit extends AppMessageHandler {
}
LOG.info("total steps for above period: " + totalSteps);
MisfitSampleProvider sampleProvider = new MisfitSampleProvider(db.getDaoSession());
MisfitSampleProvider sampleProvider = new MisfitSampleProvider(device, db.getDaoSession());
sampleProvider.addGBActivitySamples(activitySamples);
} catch (Exception e) {
LOG.error("Error acquiring database", e);

View File

@ -102,7 +102,7 @@ public class AppMessageHandlerMorpheuz extends AppMessageHandler {
try (DBHandler db = GBApplication.acquireDB()) {
User user = DBHelper.getUser(db.getDaoSession());
Device device = DBHelper.getDevice(getDevice(), db.getDaoSession());
MorpheuzSampleProvider sampleProvider = new MorpheuzSampleProvider(db.getDaoSession());
MorpheuzSampleProvider sampleProvider = new MorpheuzSampleProvider(getDevice(), db.getDaoSession());
sampleProvider.addGBActivitySample(createSample(recording_base_timestamp + index * 600, intensity, 0, type, user, device));
} catch (Exception e) {
LOG.error("Error acquiring database", e);

View File

@ -10,6 +10,7 @@ import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.database.DBHandler;
import nodomain.freeyourgadget.gadgetbridge.devices.SampleProvider;
import nodomain.freeyourgadget.gadgetbridge.devices.pebble.HealthSampleProvider;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.model.ActivityKind;
import nodomain.freeyourgadget.gadgetbridge.util.GB;
@ -17,8 +18,8 @@ class DatalogSessionHealthOverlayData extends DatalogSessionPebbleHealth {
private static final Logger LOG = LoggerFactory.getLogger(DatalogSessionHealthOverlayData.class);
public DatalogSessionHealthOverlayData(byte id, UUID uuid, int tag, byte item_type, short item_size) {
super(id, uuid, tag, item_type, item_size);
public DatalogSessionHealthOverlayData(byte id, UUID uuid, int tag, byte item_type, short item_size, GBDevice device) {
super(id, uuid, tag, item_type, item_size, device);
taginfo = "(health - overlay data " + tag + " )";
}
@ -59,7 +60,7 @@ class DatalogSessionHealthOverlayData extends DatalogSessionPebbleHealth {
private boolean store(OverlayRecord[] overlayRecords) {
try (DBHandler dbHandler = GBApplication.acquireDB()) {
SampleProvider sampleProvider = new HealthSampleProvider(dbHandler.getDaoSession());
SampleProvider sampleProvider = new HealthSampleProvider(getDevice(), dbHandler.getDaoSession());
int latestTimestamp = sampleProvider.fetchLatestTimestamp();
for (OverlayRecord overlayRecord : overlayRecords) {
if (latestTimestamp < (overlayRecord.timestampStart + overlayRecord.durationSeconds))

View File

@ -10,6 +10,7 @@ import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.database.DBHandler;
import nodomain.freeyourgadget.gadgetbridge.devices.SampleProvider;
import nodomain.freeyourgadget.gadgetbridge.devices.pebble.HealthSampleProvider;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.model.ActivityKind;
import nodomain.freeyourgadget.gadgetbridge.util.GB;
@ -17,8 +18,8 @@ class DatalogSessionHealthSleep extends DatalogSessionPebbleHealth {
private static final Logger LOG = LoggerFactory.getLogger(DatalogSessionHealthSleep.class);
public DatalogSessionHealthSleep(byte id, UUID uuid, int tag, byte item_type, short item_size) {
super(id, uuid, tag, item_type, item_size);
public DatalogSessionHealthSleep(byte id, UUID uuid, int tag, byte item_type, short item_size, GBDevice device) {
super(id, uuid, tag, item_type, item_size, device);
taginfo = "(health - sleep " + tag + " )";
}
@ -58,7 +59,7 @@ class DatalogSessionHealthSleep extends DatalogSessionPebbleHealth {
private boolean store(SleepRecord[] sleepRecords) {
try (DBHandler dbHandler = GBApplication.acquireDB()) {
SampleProvider sampleProvider = new HealthSampleProvider(dbHandler.getDaoSession());
SampleProvider sampleProvider = new HealthSampleProvider(getDevice(), dbHandler.getDaoSession());
int latestTimestamp = sampleProvider.fetchLatestTimestamp();
for (SleepRecord sleepRecord : sleepRecords) {
if (latestTimestamp < sleepRecord.bedTimeEnd)

View File

@ -10,25 +10,19 @@ import java.util.UUID;
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.database.DBHandler;
import nodomain.freeyourgadget.gadgetbridge.database.DBHelper;
import nodomain.freeyourgadget.gadgetbridge.devices.SampleProvider;
import nodomain.freeyourgadget.gadgetbridge.devices.pebble.HealthSampleProvider;
import nodomain.freeyourgadget.gadgetbridge.entities.AbstractActivitySample;
import nodomain.freeyourgadget.gadgetbridge.entities.PebbleActivitySample;
import nodomain.freeyourgadget.gadgetbridge.impl.GBActivitySample;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.model.ActivityKind;
import nodomain.freeyourgadget.gadgetbridge.model.ActivitySample;
import nodomain.freeyourgadget.gadgetbridge.util.GB;
public class DatalogSessionHealthSteps extends DatalogSessionPebbleHealth {
private static final Logger LOG = LoggerFactory.getLogger(DatalogSessionHealthSteps.class);
private final GBDevice device;
public DatalogSessionHealthSteps(byte id, UUID uuid, int tag, byte item_type, short item_size, GBDevice device) {
super(id, uuid, tag, item_type, item_size);
super(id, uuid, tag, item_type, item_size, device);
taginfo = "(health - steps)";
this.device = device;
}
@Override
@ -81,11 +75,11 @@ public class DatalogSessionHealthSteps extends DatalogSessionPebbleHealth {
private void store(StepsRecord[] stepsRecords) {
try (DBHandler dbHandler = GBApplication.acquireDB()) {
HealthSampleProvider sampleProvider = new HealthSampleProvider(dbHandler.getDaoSession());
HealthSampleProvider sampleProvider = new HealthSampleProvider(getDevice(), dbHandler.getDaoSession());
PebbleActivitySample[] samples = new PebbleActivitySample[stepsRecords.length];
// TODO: user and device
Long userId = DBHelper.getUser(dbHandler.getDaoSession()).getId();
Long deviceId = DBHelper.getDevice(device, dbHandler.getDaoSession()).getId();
Long deviceId = DBHelper.getDevice(getDevice(), dbHandler.getDaoSession()).getId();
for (int j = 0; j < stepsRecords.length; j++) {
StepsRecord stepsRecord = stepsRecords[j];
samples[j] = new PebbleActivitySample(

View File

@ -3,12 +3,20 @@ package nodomain.freeyourgadget.gadgetbridge.service.devices.pebble;
import java.util.UUID;
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.util.Prefs;
abstract class DatalogSessionPebbleHealth extends DatalogSession {
DatalogSessionPebbleHealth(byte id, UUID uuid, int tag, byte itemType, short itemSize) {
private final GBDevice mDevice;
DatalogSessionPebbleHealth(byte id, UUID uuid, int tag, byte itemType, short itemSize, GBDevice device) {
super(id, uuid, tag, itemType, itemSize);
mDevice = device;
}
public GBDevice getDevice() {
return mDevice;
}
protected boolean isPebbleHealthEnabled() {

View File

@ -1999,9 +1999,9 @@ public class PebbleProtocol extends GBDeviceProtocol {
if (uuid.equals(UUID_ZERO) && log_tag == 81) {
mDatalogSessions.put(id, new DatalogSessionHealthSteps(id, uuid, log_tag, item_type, item_size, getDevice()));
} else if (uuid.equals(UUID_ZERO) && log_tag == 83) {
mDatalogSessions.put(id, new DatalogSessionHealthSleep(id, uuid, log_tag, item_type, item_size));
mDatalogSessions.put(id, new DatalogSessionHealthSleep(id, uuid, log_tag, item_type, item_size, getDevice()));
} else if (uuid.equals(UUID_ZERO) && log_tag == 84) {
mDatalogSessions.put(id, new DatalogSessionHealthOverlayData(id, uuid, log_tag, item_type, item_size));
mDatalogSessions.put(id, new DatalogSessionHealthOverlayData(id, uuid, log_tag, item_type, item_size, getDevice()));
} else {
mDatalogSessions.put(id, new DatalogSession(id, uuid, log_tag, item_type, item_size));
}