Sensor data: separate aggregation and TTL explicitly.

Part of #1995.
This commit is contained in:
Dennis Guse
2025-02-17 21:17:55 +01:00
parent d4e9b5d3d7
commit b9ed5e28e0
12 changed files with 120 additions and 61 deletions
@@ -37,7 +37,11 @@ public abstract class Aggregator<Input, Output> {
protected abstract void computeValue(Raw<Input> current);
public boolean hasAggregatedValue() {
/**
* @return did we process data from a sensor.
* NOTE: for some sensors this may require more than one measurement.
*/
public boolean hasReceivedData() {
return aggregatedValue != null;
}
@@ -46,15 +50,14 @@ public abstract class Aggregator<Input, Output> {
@NonNull
public Output getAggregatedValue(Instant now) {
if (!hasAggregatedValue()) {
if (!hasReceivedData()) {
return getNoneValue();
}
//TODO This should only affect measured data (like heartrate), but not aggregated values.
//Remove current measurements, but provide aggregates?
if (isRecent(now)) {
return aggregatedValue;
if (isOutdated(now)) {
resetImmediate();
}
return getNoneValue();
return aggregatedValue;
}
@NonNull
@@ -63,20 +66,28 @@ public abstract class Aggregator<Input, Output> {
}
/**
* Reset long term aggregated values (more than derived from previous SensorData). e.g. overall distance.
* Reset short-term (i.e., non-aggregated) values that were directly derived from sensor data.
*/
public abstract void reset();
protected abstract void resetImmediate();
/**
* Reset long-term (i.e., aggregated) values (more than derived from previous SensorData) like overall distance.
*/
public abstract void resetAggregated();
/**
* Is the data recent considering the current time.
*/
private boolean isRecent(Instant now) {
private boolean isOutdated(Instant now) {
if (previous == null) {
return false;
return true;
}
return now
.isBefore(previous.time().plus(BluetoothRemoteSensorManager.MAX_SENSOR_DATE_SET_AGE)); //TODO Per Sensor!
.isAfter(
previous.time()
.plus(BluetoothRemoteSensorManager.MAX_SENSOR_DATE_SET_AGE)
);
}
@NonNull
@@ -32,7 +32,11 @@ public class AggregatorBarometer extends Aggregator<AtmosphericPressure, Altitud
}
@Override
public void reset() {
protected void resetImmediate() {
}
@Override
public void resetAggregated() {
aggregatedValue = getNoneValue();
}
@@ -47,7 +47,12 @@ public class AggregatorCyclingCadence extends Aggregator<BluetoothHandlerCycling
}
@Override
public void reset() {
protected void resetImmediate() {
aggregatedValue = getNoneValue();
}
@Override
public void resetAggregated() {
}
@NonNull
@@ -55,7 +55,12 @@ public class AggregatorCyclingDistanceSpeed extends Aggregator<BluetoothHandlerC
}
@Override
public void reset() {
protected void resetImmediate() {
aggregatedValue = new Data(Distance.of(0), aggregatedValue.distanceOverall, Speed.zero());
}
@Override
public void resetAggregated() {
if (aggregatedValue != null) {
aggregatedValue = new Data(aggregatedValue.distance, Distance.of(0), aggregatedValue.speed);
}
@@ -64,16 +69,16 @@ public class AggregatorCyclingDistanceSpeed extends Aggregator<BluetoothHandlerC
@NonNull
@Override
protected Data getNoneValue() {
if (aggregatedValue != null) {
return new Data(aggregatedValue.distance, aggregatedValue.distanceOverall, Speed.zero());
} else {
return new Data(Distance.of(0), Distance.of(0), Speed.zero());
}
return new Data(Distance.of(0), Distance.of(0), Speed.zero());
}
public void setWheelCircumference(Distance wheelCircumference) {
this.wheelCircumference = wheelCircumference;
}
public record Data(Distance distance, Distance distanceOverall, Speed speed) {}
public record Data(
Distance distance, //Only used for debugging
Distance distanceOverall,
Speed speed) {
}
}
@@ -17,7 +17,12 @@ public class AggregatorCyclingPower extends Aggregator<BluetoothHandlerManagerCy
}
@Override
public void reset() {
protected void resetImmediate() {
aggregatedValue = getNoneValue();
}
@Override
public void resetAggregated() {
}
@NonNull
@@ -17,7 +17,19 @@ public class AggregatorGPS extends Aggregator<Position, Position> {
}
@Override
public void reset() {
protected void resetImmediate() {
aggregatedValue = Position.empty();
}
@Override
public void resetAggregated() {
/*
* GPS data is not an aggregated value, but for now we want to ensure to only save the data once.
* The data is too large to save it more often than needed (i.e., duplicated values).
* TODO: this behavior can be changed if TrackRecordingManager.insertTrackPoint() would strip GPS data if it was already saved. This would simplify TrackPointCreator.createCurrentTrackPoint()
*/
aggregatedValue = Position.empty();
}
@NonNull
@@ -16,7 +16,12 @@ public class AggregatorHeartRate extends Aggregator<HeartRate, HeartRate> {
}
@Override
public void reset() {
protected void resetImmediate() {
aggregatedValue = getNoneValue();
}
@Override
public void resetAggregated() {
}
@NonNull
@@ -36,7 +36,12 @@ public final class AggregatorRunning extends Aggregator<BluetoothHandlerRunningS
}
@Override
public void reset() {
protected void resetImmediate() {
aggregatedValue = new Data(Speed.zero(), Cadence.of(0f), aggregatedValue.distance);
}
@Override
public void resetAggregated() {
if (aggregatedValue != null) {
aggregatedValue = new Data(aggregatedValue.speed, aggregatedValue.cadence, Distance.of(0));
}
@@ -45,11 +50,7 @@ public final class AggregatorRunning extends Aggregator<BluetoothHandlerRunningS
@NonNull
@Override
protected Data getNoneValue() {
if (aggregatedValue != null) {
return new Data(Speed.zero(), Cadence.of(0f), aggregatedValue.distance);
} else {
return new Data(Speed.zero(), Cadence.of(0f), Distance.of(0));
}
return new Data(Speed.zero(), Cadence.of(0f), Distance.of(0));
}
public record Data(Speed speed, Cadence cadence, @NonNull Distance distance) {
@@ -75,7 +75,7 @@ public class SensorDataSet {
return cyclingCadence.getAggregatedValueWithSensorName(trackPointCreator.createNow());
}
if (runningDistanceSpeedCadence != null && runningDistanceSpeedCadence.hasAggregatedValue() && runningDistanceSpeedCadence.aggregatedValue.cadence() != null) {
if (runningDistanceSpeedCadence != null && runningDistanceSpeedCadence.hasReceivedData() && runningDistanceSpeedCadence.aggregatedValue.cadence() != null) {
return new Pair<>(runningDistanceSpeedCadence.aggregatedValue.cadence(), runningDistanceSpeedCadence.getSensorNameOrAddress());
}
@@ -83,11 +83,11 @@ public class SensorDataSet {
}
public Pair<Speed, String> getSpeed() {
if (cyclingDistanceSpeed != null && cyclingDistanceSpeed.hasAggregatedValue() && cyclingDistanceSpeed.getAggregatedValue(trackPointCreator.createNow()).speed() != null) {
if (cyclingDistanceSpeed != null && cyclingDistanceSpeed.hasReceivedData() && cyclingDistanceSpeed.getAggregatedValue(trackPointCreator.createNow()).speed() != null) {
return new Pair<>(cyclingDistanceSpeed.getAggregatedValue(trackPointCreator.createNow()).speed(), cyclingDistanceSpeed.getSensorNameOrAddress());
}
if (runningDistanceSpeedCadence != null && runningDistanceSpeedCadence.hasAggregatedValue() && runningDistanceSpeedCadence.getAggregatedValue(trackPointCreator.createNow()).speed() != null) {
if (runningDistanceSpeedCadence != null && runningDistanceSpeedCadence.hasReceivedData() && runningDistanceSpeedCadence.getAggregatedValue(trackPointCreator.createNow()).speed() != null) {
return new Pair<>(runningDistanceSpeedCadence.aggregatedValue.speed(), runningDistanceSpeedCadence.getSensorNameOrAddress());
}
@@ -156,7 +156,7 @@ public class SensorDataSet {
}
public void fillTrackPoint(TrackPoint trackPoint) {
if (gps != null && gps.hasAggregatedValue()) {
if (gps != null && gps.hasReceivedData()) {
trackPoint.setPosition(gps.getAggregatedValue(trackPointCreator.createNow()));
}
@@ -172,19 +172,19 @@ public class SensorDataSet {
trackPoint.setSpeed(getSpeed().first);
}
if (cyclingDistanceSpeed != null && cyclingDistanceSpeed.hasAggregatedValue()) {
if (cyclingDistanceSpeed != null && cyclingDistanceSpeed.hasReceivedData()) {
trackPoint.setSensorDistance(cyclingDistanceSpeed.getAggregatedValue(trackPointCreator.createNow()).distanceOverall());
}
if (cyclingPower != null && cyclingPower.hasAggregatedValue()) {
if (cyclingPower != null && cyclingPower.hasReceivedData()) {
trackPoint.setPower(cyclingPower.getAggregatedValue(trackPointCreator.createNow()));
}
if (runningDistanceSpeedCadence != null && runningDistanceSpeedCadence.hasAggregatedValue()) {
if (runningDistanceSpeedCadence != null && runningDistanceSpeedCadence.hasReceivedData()) {
trackPoint.setSensorDistance(runningDistanceSpeedCadence.getAggregatedValue(trackPointCreator.createNow()).distance());
}
if (barometer != null && barometer.hasAggregatedValue()) {
if (barometer != null && barometer.hasReceivedData()) {
trackPoint.setAltitudeGain(barometer.getAggregatedValue(trackPointCreator.createNow()).gain_m());
trackPoint.setAltitudeLoss(barometer.getAggregatedValue(trackPointCreator.createNow()).loss_m());
}
@@ -193,13 +193,13 @@ public class SensorDataSet {
public void reset() {
Log.i(TAG, "Resetting data");
if (heartRate != null) heartRate.reset();
if (cyclingCadence != null) cyclingCadence.reset();
if (cyclingDistanceSpeed != null) cyclingDistanceSpeed.reset();
if (cyclingPower != null) cyclingPower.reset();
if (runningDistanceSpeedCadence != null) runningDistanceSpeedCadence.reset();
if (barometer != null) barometer.reset();
if (gps != null) gps.reset();
if (heartRate != null) heartRate.resetAggregated();
if (cyclingCadence != null) cyclingCadence.resetAggregated();
if (cyclingDistanceSpeed != null) cyclingDistanceSpeed.resetAggregated();
if (cyclingPower != null) cyclingPower.resetAggregated();
if (runningDistanceSpeedCadence != null) runningDistanceSpeedCadence.resetAggregated();
if (barometer != null) barometer.resetAggregated();
if (gps != null) gps.resetAggregated();
}
private void set(@NonNull Aggregator<?, ?> type, @Nullable Aggregator<?, ?> sensorData) {