Aggregator: there are no default values anymore.

This commit is contained in:
Dennis Guse
2026-01-18 18:38:46 +01:00
parent b651a8ca6a
commit 14cebf3fec
12 changed files with 70 additions and 160 deletions
@@ -24,7 +24,7 @@ public class AggregatorBarometerTest {
addSensorValue(subject, new float[]{1015f, 1015.01f, 1015.02f, 1015.03f, 1015.04f, 1015.05f, 1015.06f, 1015.07f, 1015.08f, 1015.09f, 1015.10f, 1015.11f, 1015.12f, 1015.13f, 1018f, 1018.1f, 1018.1f, 1018.1f, 1018.1f}); addSensorValue(subject, new float[]{1015f, 1015.01f, 1015.02f, 1015.03f, 1015.04f, 1015.05f, 1015.06f, 1015.07f, 1015.08f, 1015.09f, 1015.10f, 1015.11f, 1015.12f, 1015.13f, 1018f, 1018.1f, 1018.1f, 1018.1f, 1018.1f});
// then // then
Assert.assertEquals(0f, subject.aggregatedValue.gain_m(), 0.01); Assert.assertEquals(0f, subject.output.gain_m(), 0.01);
Assert.assertEquals(15f, subject.aggregatedValue.loss_m(), 0.01); Assert.assertEquals(15f, subject.output.loss_m(), 0.01);
} }
} }
@@ -2,6 +2,7 @@ package de.dennisguse.opentracks.sensors.sensorData;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import androidx.test.ext.junit.runners.AndroidJUnit4; import androidx.test.ext.junit.runners.AndroidJUnit4;
@@ -12,7 +13,6 @@ import java.time.Instant;
import de.dennisguse.opentracks.data.models.Cadence; import de.dennisguse.opentracks.data.models.Cadence;
import de.dennisguse.opentracks.data.models.Distance; import de.dennisguse.opentracks.data.models.Distance;
import de.dennisguse.opentracks.data.models.Speed;
import de.dennisguse.opentracks.sensors.UintUtils; import de.dennisguse.opentracks.sensors.UintUtils;
import de.dennisguse.opentracks.sensors.driver.CyclingCadenceBluetooth; import de.dennisguse.opentracks.sensors.driver.CyclingCadenceBluetooth;
import de.dennisguse.opentracks.sensors.driver.CyclingDistanceSpeedBluetooth; import de.dennisguse.opentracks.sensors.driver.CyclingDistanceSpeedBluetooth;
@@ -93,7 +93,7 @@ public class SensorDataCyclingTest {
// then // then
// TODO See #953 // TODO See #953
// assertEquals(60, current.getValue().getRPM(), 0.01); // assertEquals(60, current.getValue().getRPM(), 0.01);
assertEquals(Cadence.of(0), current.getAggregatedValue(Instant.MIN)); assertNull(current.getAggregatedValue(Instant.MIN));
} }
@Test @Test
@@ -120,11 +120,10 @@ public class SensorDataCyclingTest {
current.add(Instant.MIN, new CyclingDistanceSpeedBluetooth.WheelData(UintUtils.UINT32_MAX - 1, 1024)); current.add(Instant.MIN, new CyclingDistanceSpeedBluetooth.WheelData(UintUtils.UINT32_MAX - 1, 1024));
current.add(Instant.MIN, new CyclingDistanceSpeedBluetooth.WheelData(0, 2048)); current.add(Instant.MIN, new CyclingDistanceSpeedBluetooth.WheelData(0, 2048));
// then // then
// TODO See #953 // TODO See #953
// assertEquals(2, current.getValue().getDistance().toM(), 0.01); // assertEquals(2, current.getValue().getDistance().toM(), 0.01);
// assertEquals(2, current.getValue().getSpeed().toMPS(), 0.01); // assertEquals(2, current.getValue().getSpeed().toMPS(), 0.01);
assertEquals(new AggregatorCyclingDistanceSpeed.Data(Distance.ZERO, Distance.ZERO, Speed.ZERO), current.getAggregatedValue(Instant.MIN)); assertNull(current.getAggregatedValue(Instant.MIN));
} }
} }
@@ -1,17 +1,20 @@
package de.dennisguse.opentracks.sensors.sensorData; package de.dennisguse.opentracks.sensors.sensorData;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.time.Duration; import java.time.Duration;
import java.time.Instant; import java.time.Instant;
public abstract class Aggregator<Input, Output> { public abstract class Aggregator<InputType, OutputType> {
private static final Duration MAX_SENSOR_DATE_SET_AGE = Duration.ofSeconds(5); private static final Duration MAX_SENSOR_DATE_SET_AGE = Duration.ofSeconds(5);
protected Raw<Input> previous; @Nullable
protected Raw<InputType> previous;
protected Output aggregatedValue; //TODO Make private
protected OutputType output;
private final String sensorAddress; private final String sensorAddress;
private final String sensorName; private final String sensorName;
@@ -25,57 +28,47 @@ public abstract class Aggregator<Input, Output> {
return sensorName != null ? sensorName : sensorAddress; return sensorName != null ? sensorName : sensorAddress;
} }
public final void add(Instant now, Input current) { public final void add(Instant now, InputType current) {
Raw<Input> next = new Raw<>(now, current); Raw<InputType> next = new Raw<>(now, current);
computeValue(next); computeValue(next);
previous = next; previous = next;
} }
protected abstract void computeValue(Raw<Input> current); protected abstract void computeValue(Raw<InputType> current);
/** /**
* @return did we process data from a sensor. * @return did we process data from a sensor.
* NOTE: for some sensors this may require more than one measurement. * NOTE: for some sensors this may require more than one measurement.
*/ */
public boolean hasReceivedData() { public boolean hasReceivedData() {
return aggregatedValue != null; return output != null;
} }
@Deprecated //TODO Remove: for sensor there is no default value. Make getAggregatedValue() @Nullable @Nullable
@NonNull protected OutputType getAggregatedValue(Instant now) {
protected abstract Output getNoneValue();
@Deprecated //TODO Removed; just implemented for refactoring. This code is bypassing core functionality of this class.
public Output getAggregatedValue() {
return aggregatedValue;
}
@NonNull
public Output getAggregatedValue(Instant now) {
if (!hasReceivedData()) {
return getNoneValue();
}
if (isOutdated(now)) { if (isOutdated(now)) {
resetImmediate(); resetOutdated();
} }
return aggregatedValue; return output;
} }
@NonNull @NonNull
public SensorData<Output> getAggregatedValueWithSensorName(Instant now) { public SensorData<OutputType> getAggregatedValueWithSensorName(Instant now) {
return new SensorData<>(getAggregatedValue(now), getSensorNameOrAddress()); return new SensorData<>(getAggregatedValue(now), getSensorNameOrAddress());
} }
/** /**
* Reset short-term (i.e., non-aggregated) values that were directly derived from sensor data. * Reset short-term (i.e., non-aggregated) values that were directly derived from sensor data.
*/ */
protected abstract void resetImmediate(); protected void resetOutdated() {
output = null;
};
/** /**
* Reset long-term (i.e., aggregated) values (more than derived from previous SensorData) like overall distance. * Reset long-term (i.e., aggregated) values (more than derived from previous SensorData) like overall distance.
*/ */
public abstract void resetAggregated(); public void resetAggregated() {};
/** /**
* Is the data recent considering the current time. * Is the data recent considering the current time.
@@ -99,7 +92,7 @@ public abstract class Aggregator<Input, Output> {
@NonNull @NonNull
@Override @Override
public String toString() { public String toString() {
return "sensorAddress=" + sensorAddress + " data=" + aggregatedValue; return "sensorAddress=" + sensorAddress + " data=" + output;
} }
protected record Raw<T>( protected record Raw<T>(
@@ -1,7 +1,5 @@
package de.dennisguse.opentracks.sensors.sensorData; package de.dennisguse.opentracks.sensors.sensorData;
import androidx.annotation.NonNull;
import de.dennisguse.opentracks.data.models.Altitude; import de.dennisguse.opentracks.data.models.Altitude;
import de.dennisguse.opentracks.data.models.AltitudeGainLoss; import de.dennisguse.opentracks.data.models.AltitudeGainLoss;
import de.dennisguse.opentracks.data.models.AtmosphericPressure; import de.dennisguse.opentracks.data.models.AtmosphericPressure;
@@ -19,31 +17,28 @@ public class AggregatorBarometer extends Aggregator<AtmosphericPressure, Altitud
protected void computeValue(Raw<AtmosphericPressure> current) { protected void computeValue(Raw<AtmosphericPressure> current) {
if (previous == null) { if (previous == null) {
lastAcceptedSensorValue = current.value(); lastAcceptedSensorValue = current.value();
aggregatedValue = getNoneValue(); output = new AltitudeGainLoss(0f, 0f);
return; return;
} }
PressureSensorUtils.AltitudeChange altitudeChange = PressureSensorUtils.computeChangesWithSmoothing_m(lastAcceptedSensorValue, previous.value(), current.value()); PressureSensorUtils.AltitudeChange altitudeChange = PressureSensorUtils.computeChangesWithSmoothing_m(lastAcceptedSensorValue, previous.value(), current.value());
if (altitudeChange != null) { if (altitudeChange != null) {
aggregatedValue = new AltitudeGainLoss(aggregatedValue.gain_m() + altitudeChange.getAltitudeGain_m(), aggregatedValue.loss_m() + altitudeChange.getAltitudeLoss_m()); if (output == null) {
output = new AltitudeGainLoss(0f, 0f);
}
output = new AltitudeGainLoss(output.gain_m() + altitudeChange.getAltitudeGain_m(), output.loss_m() + altitudeChange.getAltitudeLoss_m());
lastAcceptedSensorValue = altitudeChange.currentSensorValue(); lastAcceptedSensorValue = altitudeChange.currentSensorValue();
} }
} }
@Override @Override
protected void resetImmediate() { protected void resetOutdated() {
} }
@Override @Override
public void resetAggregated() { public void resetAggregated() {
aggregatedValue = getNoneValue(); output = null;
}
@NonNull
@Override
protected AltitudeGainLoss getNoneValue() {
return new AltitudeGainLoss(0f, 0f);
} }
public record Data(Altitude gain, Altitude loss) {} public record Data(Altitude gain, Altitude loss) {}
@@ -2,8 +2,6 @@ package de.dennisguse.opentracks.sensors.sensorData;
import android.util.Log; import android.util.Log;
import androidx.annotation.NonNull;
import java.time.Duration; import java.time.Duration;
import de.dennisguse.opentracks.data.models.Cadence; import de.dennisguse.opentracks.data.models.Cadence;
@@ -32,32 +30,18 @@ public class AggregatorCyclingCadence extends Aggregator<CyclingCadenceBluetooth
} }
if (timeDiff.isNegative()) { if (timeDiff.isNegative()) {
Log.e(TAG, "Timestamps difference is invalid: cannot compute cadence."); Log.e(TAG, "Timestamps difference is invalid: cannot compute cadence.");
aggregatedValue = null; output = null;
return; return;
} }
// TODO We have to treat with overflow according to the documentation: read https://codeberg.org/OpenTracksApp/OpenTracks/pulls/953#issuecomment-6466930 // TODO We have to treat with overflow according to the documentation: read https://codeberg.org/OpenTracksApp/OpenTracks/pulls/953#issuecomment-6466930
if (current.value().crankRevolutionsCount() < previous.value().crankRevolutionsCount()) { if (current.value().crankRevolutionsCount() < previous.value().crankRevolutionsCount()) {
Log.e(TAG, "Crank revolutions count difference is invalid: cannot compute cadence."); Log.e(TAG, "Crank revolutions count difference is invalid: cannot compute cadence.");
output = null;
return; return;
} }
long crankDiff = UintUtils.diff(current.value().crankRevolutionsCount(), previous.value().crankRevolutionsCount(), UintUtils.UINT32_MAX); long crankDiff = UintUtils.diff(current.value().crankRevolutionsCount(), previous.value().crankRevolutionsCount(), UintUtils.UINT32_MAX);
aggregatedValue = Cadence.of(crankDiff, timeDiff); output = Cadence.of(crankDiff, timeDiff);
}
@Override
protected void resetImmediate() {
aggregatedValue = getNoneValue();
}
@Override
public void resetAggregated() {
}
@NonNull
@Override
protected Cadence getNoneValue() {
return Cadence.of(0);
} }
} }
@@ -2,8 +2,6 @@ package de.dennisguse.opentracks.sensors.sensorData;
import android.util.Log; import android.util.Log;
import androidx.annotation.NonNull;
import java.time.Duration; import java.time.Duration;
import de.dennisguse.opentracks.data.models.Distance; import de.dennisguse.opentracks.data.models.Distance;
@@ -35,43 +33,39 @@ public class AggregatorCyclingDistanceSpeed extends Aggregator<CyclingDistanceSp
} }
if (timeDiff.isNegative()) { if (timeDiff.isNegative()) {
Log.e(TAG, "Timestamps difference is invalid: cannot compute cadence."); Log.e(TAG, "Timestamps difference is invalid: cannot compute cadence.");
aggregatedValue = null; output = null;
return; return;
} }
if (current.value().wheelRevolutionsCount() < previous.value().wheelRevolutionsCount()) { if (current.value().wheelRevolutionsCount() < previous.value().wheelRevolutionsCount()) {
Log.e(TAG, "Wheel revolutions count difference is invalid: cannot compute speed."); Log.e(TAG, "Wheel revolutions count difference is invalid: cannot compute speed.");
output = null;
return; return;
} }
long wheelDiff = UintUtils.diff(current.value().wheelRevolutionsCount(), previous.value().wheelRevolutionsCount(), UintUtils.UINT32_MAX); long wheelDiff = UintUtils.diff(current.value().wheelRevolutionsCount(), previous.value().wheelRevolutionsCount(), UintUtils.UINT32_MAX);
Distance distance = wheelCircumference.multipliedBy(wheelDiff); Distance distance = wheelCircumference.multipliedBy(wheelDiff);
Distance distanceOverall = distance; Distance distanceOverall = distance;
if (aggregatedValue != null) { if (output != null) {
distanceOverall = distance.plus(aggregatedValue.distanceOverall); distanceOverall = distance.plus(output.distanceOverall);
} }
Speed speed_mps = Speed.of(distance, timeDiff); Speed speed_mps = Speed.of(distance, timeDiff);
aggregatedValue = new Data(distance, distanceOverall, speed_mps); output = new Data(distance, distanceOverall, speed_mps);
} }
@Override @Override
protected void resetImmediate() { protected void resetOutdated() {
aggregatedValue = new Data(Distance.ZERO, aggregatedValue.distanceOverall, Speed.ZERO); Distance overallDistance = output != null ? output.distanceOverall : Distance.ZERO;
output = new Data(Distance.ZERO, overallDistance, Speed.ZERO);
} }
@Override @Override
public void resetAggregated() { public void resetAggregated() {
if (aggregatedValue != null) { if (output != null) {
aggregatedValue = new Data(aggregatedValue.distance, Distance.ZERO, aggregatedValue.speed); output = new Data(output.distance, Distance.ZERO, output.speed);
} }
} }
@NonNull
@Override
protected Data getNoneValue() {
return new Data(Distance.ZERO, Distance.ZERO, Speed.ZERO);
}
public void setWheelCircumference(Distance wheelCircumference) { public void setWheelCircumference(Distance wheelCircumference) {
this.wheelCircumference = wheelCircumference; this.wheelCircumference = wheelCircumference;
} }
@@ -1,7 +1,5 @@
package de.dennisguse.opentracks.sensors.sensorData; package de.dennisguse.opentracks.sensors.sensorData;
import androidx.annotation.NonNull;
import de.dennisguse.opentracks.data.models.Power; import de.dennisguse.opentracks.data.models.Power;
import de.dennisguse.opentracks.sensors.driver.CyclingPowerBluetooth; import de.dennisguse.opentracks.sensors.driver.CyclingPowerBluetooth;
@@ -13,21 +11,6 @@ public class AggregatorCyclingPower extends Aggregator<CyclingPowerBluetooth.Dat
@Override @Override
public void computeValue(Raw<CyclingPowerBluetooth.Data> current) { public void computeValue(Raw<CyclingPowerBluetooth.Data> current) {
this.aggregatedValue = current.value().power(); this.output = current.value().power();
}
@Override
protected void resetImmediate() {
aggregatedValue = getNoneValue();
}
@Override
public void resetAggregated() {
}
@NonNull
@Override
protected Power getNoneValue() {
return Power.of(0f);
} }
} }
@@ -10,19 +10,13 @@ import de.dennisguse.opentracks.data.models.Position;
public class AggregatorGPS extends Aggregator<Location, Position> { public class AggregatorGPS extends Aggregator<Location, Position> {
public AggregatorGPS(String sensorAddress, String sensorName) { public AggregatorGPS(String sensorAddress, String sensorName) {
super(sensorAddress, sensorName); super(sensorAddress, sensorName);
} }
@Override @Override
protected void computeValue(Raw<Location> current) { protected void computeValue(Raw<Location> current) {
aggregatedValue = Position.of(current.value(), current.time()); output = Position.of(current.value(), current.time());
}
@Override
protected void resetImmediate() {
aggregatedValue = Position.empty();
} }
@Override @Override
@@ -32,19 +26,15 @@ public class AggregatorGPS extends Aggregator<Location, Position> {
* The data is too large to save it more often than needed (i.e., duplicated values). * 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() * 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(); output = null;
}
@NonNull
@Override
protected Position getNoneValue() {
return Position.empty();
} }
@NonNull @NonNull
@Override @Override
public Position getAggregatedValue(Instant now) { public Position getAggregatedValue(Instant now) {
return super.getAggregatedValue(now) Position value = super.getAggregatedValue(now);
.with(now); if (value == null) value = Position.empty();
return value.with(now);
} }
} }
@@ -1,7 +1,5 @@
package de.dennisguse.opentracks.sensors.sensorData; package de.dennisguse.opentracks.sensors.sensorData;
import androidx.annotation.NonNull;
import de.dennisguse.opentracks.data.models.HeartRate; import de.dennisguse.opentracks.data.models.HeartRate;
public class AggregatorHeartRate extends Aggregator<HeartRate, HeartRate> { public class AggregatorHeartRate extends Aggregator<HeartRate, HeartRate> {
@@ -13,22 +11,7 @@ public class AggregatorHeartRate extends Aggregator<HeartRate, HeartRate> {
@Override @Override
protected void computeValue(Raw<HeartRate> current) { protected void computeValue(Raw<HeartRate> current) {
if (current.value().isValid()) { if (current.value().isValid()) {
this.aggregatedValue = current.value(); this.output = current.value();
} }
} }
@Override
protected void resetImmediate() {
aggregatedValue = getNoneValue();
}
@Override
public void resetAggregated() {
}
@NonNull
@Override
protected HeartRate getNoneValue() {
return HeartRate.of(0);
}
} }
@@ -27,33 +27,31 @@ public final class AggregatorRunning extends Aggregator<RunningSpeedAndCadenceBl
Distance distance = null; Distance distance = null;
if (previous.value().totalDistance() != null && current.value().totalDistance() != null) { if (previous.value().totalDistance() != null && current.value().totalDistance() != null) {
distance = current.value().totalDistance().minus(previous.value().totalDistance()); distance = current.value().totalDistance().minus(previous.value().totalDistance());
if (aggregatedValue != null) { if (output != null) {
distance = distance.plus(aggregatedValue.distance); distance = distance.plus(output.distance);
} }
} }
aggregatedValue = new Data(current.value().speed(), current.value().cadence(), distance); output = new Data(current.value().speed(), current.value().cadence(), distance);
} }
@Override @Override
protected void resetImmediate() { protected void resetOutdated() {
aggregatedValue = new Data(Speed.ZERO, Cadence.of(0f), aggregatedValue.distance); Distance distance = output != null ? output.distance : Distance.ZERO;
output = new Data(Speed.ZERO, Cadence.of(0f), distance);
} }
@Override @Override
public void resetAggregated() { public void resetAggregated() {
if (aggregatedValue != null) { if (output != null) {
aggregatedValue = new Data(aggregatedValue.speed, aggregatedValue.cadence, Distance.ZERO); output = new Data(output.speed, output.cadence, Distance.ZERO);
} }
} }
@NonNull public record Data(
@Override Speed speed,
protected Data getNoneValue() { Cadence cadence,
return new Data(Speed.ZERO, Cadence.of(0f), Distance.ZERO); @NonNull Distance distance) {
}
public record Data(Speed speed, Cadence cadence, @NonNull Distance distance) {
} }
} }
@@ -1,7 +1,5 @@
package de.dennisguse.opentracks.sensors.sensorData; package de.dennisguse.opentracks.sensors.sensorData;
import androidx.annotation.NonNull;
import java.time.Duration; import java.time.Duration;
import de.dennisguse.opentracks.data.models.Temperature; import de.dennisguse.opentracks.data.models.Temperature;
@@ -13,20 +11,9 @@ public class AggregatorTemperature extends Aggregator<Temperature, Temperature>
} }
protected void computeValue(Raw<Temperature> current) { protected void computeValue(Raw<Temperature> current) {
this.aggregatedValue = current.value(); this.output = current.value();
} }
@NonNull
protected Temperature getNoneValue() {
return Temperature.ofCentiCelsius(0);
}
protected void resetImmediate() {
aggregatedValue = getNoneValue();
}
public void resetAggregated() {};
@Override @Override
protected Duration getMaxSensorAge() { protected Duration getMaxSensorAge() {
return Duration.ofMinutes(1); return Duration.ofMinutes(1);
@@ -1,7 +1,11 @@
package de.dennisguse.opentracks.sensors.sensorData; package de.dennisguse.opentracks.sensors.sensorData;
import androidx.annotation.Nullable;
public record SensorData<T>( public record SensorData<T>(
@Nullable
T data, T data,
String sensorNameOrAddress String sensorNameOrAddress