1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-09-27 16:56:57 +02:00

Introduce generic TimeSamples for recorded data

This commit is contained in:
José Rebelo 2023-05-20 23:31:21 +01:00 committed by José Rebelo
parent 788cb15500
commit 25038d965f
6 changed files with 307 additions and 1 deletions

View File

@ -0,0 +1,140 @@
/* Copyright (C) 2023 Andreas Shimokawa, Carsten Pfeiffer, Daniele
Gobbetti, José Rebelo
This file is part of Gadgetbridge.
Gadgetbridge is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Gadgetbridge is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
package nodomain.freeyourgadget.gadgetbridge.devices;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
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.entities.AbstractTimeSample;
import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession;
import nodomain.freeyourgadget.gadgetbridge.entities.Device;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
/**
* Base class for all time sample providers. A Sample provider is device specific and provides
* access to the device specific samples. There are both read and write operations.
*
* @param <T> the sample type
*/
public abstract class AbstractTimeSampleProvider<T extends AbstractTimeSample> implements TimeSampleProvider<T> {
private final DaoSession mSession;
private final GBDevice mDevice;
protected AbstractTimeSampleProvider(final GBDevice device, final DaoSession session) {
mDevice = device;
mSession = session;
}
public GBDevice getDevice() {
return mDevice;
}
public DaoSession getSession() {
return mSession;
}
@NonNull
@Override
public List<T> getAllSamples(final long timestampFrom, final long timestampTo) {
final QueryBuilder<T> qb = getSampleDao().queryBuilder();
final Property timestampProperty = getTimestampSampleProperty();
final Device dbDevice = DBHelper.findDevice(getDevice(), getSession());
if (dbDevice == null) {
// no device, no samples
return Collections.emptyList();
}
final Property deviceProperty = getDeviceIdentifierSampleProperty();
qb.where(deviceProperty.eq(dbDevice.getId()), timestampProperty.ge(timestampFrom))
.where(timestampProperty.le(timestampTo));
final List<T> samples = qb.build().list();
detachFromSession();
return samples;
}
@Override
public void addSample(final T activitySample) {
getSampleDao().insertOrReplace(activitySample);
}
@Override
public void addSamples(final List<T> activitySamples) {
getSampleDao().insertOrReplaceInTx(activitySamples);
}
@Nullable
@Override
public T getLatestSample() {
final QueryBuilder<T> qb = getSampleDao().queryBuilder();
final Device dbDevice = DBHelper.findDevice(getDevice(), getSession());
if (dbDevice == null) {
// no device, no sample
return null;
}
final Property deviceProperty = getDeviceIdentifierSampleProperty();
qb.where(deviceProperty.eq(dbDevice.getId())).orderDesc(getTimestampSampleProperty()).limit(1);
final List<T> samples = qb.build().list();
if (samples.isEmpty()) {
return null;
}
return samples.get(0);
}
@Nullable
@Override
public T getFirstSample() {
final QueryBuilder<T> qb = getSampleDao().queryBuilder();
final Device dbDevice = DBHelper.findDevice(getDevice(), getSession());
if (dbDevice == null) {
// no device, no sample
return null;
}
final Property deviceProperty = getDeviceIdentifierSampleProperty();
qb.where(deviceProperty.eq(dbDevice.getId())).orderAsc(getTimestampSampleProperty()).limit(1);
final List<T> samples = qb.build().list();
if (samples.isEmpty()) {
return null;
}
return samples.get(0);
}
/**
* Detaches all samples of this type from the session. Changes to them may not be
* written back to the database.
* <p>
* Subclasses should call this method after performing custom queries.
*/
protected void detachFromSession() {
getSampleDao().detachAll();
}
@NonNull
public abstract AbstractDao<T, ?> getSampleDao();
@NonNull
protected abstract Property getTimestampSampleProperty();
@NonNull
protected abstract Property getDeviceIdentifierSampleProperty();
}

View File

@ -0,0 +1,89 @@
/* Copyright (C) 2023 José Rebelo
This file is part of Gadgetbridge.
Gadgetbridge is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Gadgetbridge is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
package nodomain.freeyourgadget.gadgetbridge.devices;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.util.List;
import nodomain.freeyourgadget.gadgetbridge.model.TimeSample;
/**
* Interface to retrieve samples from the database, and also create and add samples to the database.
* There are multiple device specific implementations, this interface defines the generic access.
* <p>
* Note that the provided samples must typically be considered read-only, because they are immediately
* removed from the session before they are returned.
* <p>
* This differs from SampleProvider by assuming milliseconds for timestamps instead of seconds, as well as
* not enforcing an all-in-once ActivitySample interface for data that might be completely unrelated with
* activity data.
*
* @param <T> the device/provider specific sample type (must extend TimeSample).
*/
public interface TimeSampleProvider<T extends TimeSample> {
/**
* Returns the list of all samples, of any type, within the given time span.
*
* @param timestampFrom the start timestamp, in milliseconds
* @param timestampTo the end timestamp, in milliseconds
* @return the list of samples of any type
*/
@NonNull
List<T> getAllSamples(long timestampFrom, long timestampTo);
/**
* Adds the given sample to the database. An existing sample with the same
* timestamp will be overwritten.
*
* @param timeSample the sample to add
*/
void addSample(T timeSample);
/**
* Adds the given samples to the database. Existing samples with the same
* timestamp (and a potential combination of other attributes, depending on the sample implementation)
* will be overwritten.
*
* @param timeSamples the samples to add
*/
void addSamples(List<T> timeSamples);
/**
* Factory method to creates an empty sample of the correct type for this sample provider.
*
* @return the newly created "empty" sample
*/
T createSample();
/**
* Returns the sample with the highest timestamp, or null if none.
*
* @return the latest sample, or null if none is found.
*/
@Nullable
T getLatestSample();
/**
* Returns the sample with the oldest timestamp, or null if none.
*
* @return the oldest sample, or null if none is found
*/
@Nullable
T getFirstSample();
}

View File

@ -1 +1,2 @@
*.java
*.java
!Abstract*.java

View File

@ -0,0 +1,44 @@
/* Copyright (C) 2023 José Rebelo
This file is part of Gadgetbridge.
Gadgetbridge is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Gadgetbridge is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
package nodomain.freeyourgadget.gadgetbridge.entities;
import androidx.annotation.NonNull;
import nodomain.freeyourgadget.gadgetbridge.model.TimeSample;
import nodomain.freeyourgadget.gadgetbridge.util.DateTimeUtils;
public abstract class AbstractTimeSample implements TimeSample {
public abstract void setTimestamp(long timestamp);
public abstract long getUserId();
public abstract void setUserId(long userId);
public abstract long getDeviceId();
public abstract void setDeviceId(long deviceId);
@NonNull
@Override
public String toString() {
return getClass().getSimpleName() + "{" +
"timestamp=" + DateTimeUtils.formatDateTime(DateTimeUtils.parseTimestampMillis(getTimestamp())) +
", userId=" + getUserId() +
", deviceId=" + getDeviceId() +
"}";
}
}

View File

@ -0,0 +1,26 @@
/* Copyright (C) 2023 José Rebelo
This file is part of Gadgetbridge.
Gadgetbridge is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Gadgetbridge is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
package nodomain.freeyourgadget.gadgetbridge.model;
import nodomain.freeyourgadget.gadgetbridge.devices.TimeSampleProvider;
public interface TimeSample {
/**
* Unix timestamp of the sample, i.e. the number of milliseconds since 1970-01-01 00:00:00 UTC.
*/
long getTimestamp();
}

View File

@ -125,6 +125,12 @@ public class DateTimeUtils {
return cal.getTime();
}
public static Date parseTimestampMillis(long timestamp) {
GregorianCalendar cal = (GregorianCalendar) GregorianCalendar.getInstance();
cal.setTimeInMillis(timestamp);
return cal.getTime();
}
public static String dayToString(Date date) {
return DAY_STORAGE_FORMAT.format(date);
}