forked from upstream-mirrors/OpenTracks
Aggregator: there are no default values anymore.
This commit is contained in:
+2
-2
@@ -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});
|
||||
|
||||
// then
|
||||
Assert.assertEquals(0f, subject.aggregatedValue.gain_m(), 0.01);
|
||||
Assert.assertEquals(15f, subject.aggregatedValue.loss_m(), 0.01);
|
||||
Assert.assertEquals(0f, subject.output.gain_m(), 0.01);
|
||||
Assert.assertEquals(15f, subject.output.loss_m(), 0.01);
|
||||
}
|
||||
}
|
||||
+3
-4
@@ -2,6 +2,7 @@ package de.dennisguse.opentracks.sensors.sensorData;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
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.Distance;
|
||||
import de.dennisguse.opentracks.data.models.Speed;
|
||||
import de.dennisguse.opentracks.sensors.UintUtils;
|
||||
import de.dennisguse.opentracks.sensors.driver.CyclingCadenceBluetooth;
|
||||
import de.dennisguse.opentracks.sensors.driver.CyclingDistanceSpeedBluetooth;
|
||||
@@ -93,7 +93,7 @@ public class SensorDataCyclingTest {
|
||||
// then
|
||||
// TODO See #953
|
||||
// assertEquals(60, current.getValue().getRPM(), 0.01);
|
||||
assertEquals(Cadence.of(0), current.getAggregatedValue(Instant.MIN));
|
||||
assertNull(current.getAggregatedValue(Instant.MIN));
|
||||
}
|
||||
|
||||
@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(0, 2048));
|
||||
|
||||
|
||||
// then
|
||||
// TODO See #953
|
||||
// assertEquals(2, current.getValue().getDistance().toM(), 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;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import java.time.Duration;
|
||||
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);
|
||||
|
||||
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 sensorName;
|
||||
@@ -25,57 +28,47 @@ public abstract class Aggregator<Input, Output> {
|
||||
return sensorName != null ? sensorName : sensorAddress;
|
||||
}
|
||||
|
||||
public final void add(Instant now, Input current) {
|
||||
Raw<Input> next = new Raw<>(now, current);
|
||||
public final void add(Instant now, InputType current) {
|
||||
Raw<InputType> next = new Raw<>(now, current);
|
||||
computeValue(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.
|
||||
* NOTE: for some sensors this may require more than one measurement.
|
||||
*/
|
||||
public boolean hasReceivedData() {
|
||||
return aggregatedValue != null;
|
||||
return output != null;
|
||||
}
|
||||
|
||||
@Deprecated //TODO Remove: for sensor there is no default value. Make getAggregatedValue() @Nullable
|
||||
@NonNull
|
||||
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();
|
||||
}
|
||||
@Nullable
|
||||
protected OutputType getAggregatedValue(Instant now) {
|
||||
if (isOutdated(now)) {
|
||||
resetImmediate();
|
||||
resetOutdated();
|
||||
}
|
||||
|
||||
return aggregatedValue;
|
||||
return output;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public SensorData<Output> getAggregatedValueWithSensorName(Instant now) {
|
||||
public SensorData<OutputType> getAggregatedValueWithSensorName(Instant now) {
|
||||
return new SensorData<>(getAggregatedValue(now), getSensorNameOrAddress());
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
public abstract void resetAggregated();
|
||||
public void resetAggregated() {};
|
||||
|
||||
/**
|
||||
* Is the data recent considering the current time.
|
||||
@@ -99,7 +92,7 @@ public abstract class Aggregator<Input, Output> {
|
||||
@NonNull
|
||||
@Override
|
||||
public String toString() {
|
||||
return "sensorAddress=" + sensorAddress + " data=" + aggregatedValue;
|
||||
return "sensorAddress=" + sensorAddress + " data=" + output;
|
||||
}
|
||||
|
||||
protected record Raw<T>(
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
package de.dennisguse.opentracks.sensors.sensorData;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
import de.dennisguse.opentracks.data.models.Altitude;
|
||||
import de.dennisguse.opentracks.data.models.AltitudeGainLoss;
|
||||
import de.dennisguse.opentracks.data.models.AtmosphericPressure;
|
||||
@@ -19,31 +17,28 @@ public class AggregatorBarometer extends Aggregator<AtmosphericPressure, Altitud
|
||||
protected void computeValue(Raw<AtmosphericPressure> current) {
|
||||
if (previous == null) {
|
||||
lastAcceptedSensorValue = current.value();
|
||||
aggregatedValue = getNoneValue();
|
||||
output = new AltitudeGainLoss(0f, 0f);
|
||||
return;
|
||||
}
|
||||
|
||||
PressureSensorUtils.AltitudeChange altitudeChange = PressureSensorUtils.computeChangesWithSmoothing_m(lastAcceptedSensorValue, previous.value(), current.value());
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void resetImmediate() {
|
||||
protected void resetOutdated() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void resetAggregated() {
|
||||
aggregatedValue = getNoneValue();
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
protected AltitudeGainLoss getNoneValue() {
|
||||
return new AltitudeGainLoss(0f, 0f);
|
||||
output = null;
|
||||
}
|
||||
|
||||
public record Data(Altitude gain, Altitude loss) {}
|
||||
|
||||
+3
-19
@@ -2,8 +2,6 @@ package de.dennisguse.opentracks.sensors.sensorData;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
import java.time.Duration;
|
||||
|
||||
import de.dennisguse.opentracks.data.models.Cadence;
|
||||
@@ -32,32 +30,18 @@ public class AggregatorCyclingCadence extends Aggregator<CyclingCadenceBluetooth
|
||||
}
|
||||
if (timeDiff.isNegative()) {
|
||||
Log.e(TAG, "Timestamps difference is invalid: cannot compute cadence.");
|
||||
aggregatedValue = null;
|
||||
output = null;
|
||||
return;
|
||||
}
|
||||
|
||||
// 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()) {
|
||||
Log.e(TAG, "Crank revolutions count difference is invalid: cannot compute cadence.");
|
||||
output = null;
|
||||
return;
|
||||
}
|
||||
|
||||
long crankDiff = UintUtils.diff(current.value().crankRevolutionsCount(), previous.value().crankRevolutionsCount(), UintUtils.UINT32_MAX);
|
||||
aggregatedValue = Cadence.of(crankDiff, timeDiff);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void resetImmediate() {
|
||||
aggregatedValue = getNoneValue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void resetAggregated() {
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
protected Cadence getNoneValue() {
|
||||
return Cadence.of(0);
|
||||
output = Cadence.of(crankDiff, timeDiff);
|
||||
}
|
||||
}
|
||||
|
||||
+10
-16
@@ -2,8 +2,6 @@ package de.dennisguse.opentracks.sensors.sensorData;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
import java.time.Duration;
|
||||
|
||||
import de.dennisguse.opentracks.data.models.Distance;
|
||||
@@ -35,43 +33,39 @@ public class AggregatorCyclingDistanceSpeed extends Aggregator<CyclingDistanceSp
|
||||
}
|
||||
if (timeDiff.isNegative()) {
|
||||
Log.e(TAG, "Timestamps difference is invalid: cannot compute cadence.");
|
||||
aggregatedValue = null;
|
||||
output = null;
|
||||
return;
|
||||
}
|
||||
|
||||
if (current.value().wheelRevolutionsCount() < previous.value().wheelRevolutionsCount()) {
|
||||
Log.e(TAG, "Wheel revolutions count difference is invalid: cannot compute speed.");
|
||||
output = null;
|
||||
return;
|
||||
}
|
||||
long wheelDiff = UintUtils.diff(current.value().wheelRevolutionsCount(), previous.value().wheelRevolutionsCount(), UintUtils.UINT32_MAX);
|
||||
|
||||
Distance distance = wheelCircumference.multipliedBy(wheelDiff);
|
||||
Distance distanceOverall = distance;
|
||||
if (aggregatedValue != null) {
|
||||
distanceOverall = distance.plus(aggregatedValue.distanceOverall);
|
||||
if (output != null) {
|
||||
distanceOverall = distance.plus(output.distanceOverall);
|
||||
}
|
||||
Speed speed_mps = Speed.of(distance, timeDiff);
|
||||
aggregatedValue = new Data(distance, distanceOverall, speed_mps);
|
||||
output = new Data(distance, distanceOverall, speed_mps);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void resetImmediate() {
|
||||
aggregatedValue = new Data(Distance.ZERO, aggregatedValue.distanceOverall, Speed.ZERO);
|
||||
protected void resetOutdated() {
|
||||
Distance overallDistance = output != null ? output.distanceOverall : Distance.ZERO;
|
||||
output = new Data(Distance.ZERO, overallDistance, Speed.ZERO);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void resetAggregated() {
|
||||
if (aggregatedValue != null) {
|
||||
aggregatedValue = new Data(aggregatedValue.distance, Distance.ZERO, aggregatedValue.speed);
|
||||
if (output != null) {
|
||||
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) {
|
||||
this.wheelCircumference = wheelCircumference;
|
||||
}
|
||||
|
||||
+1
-18
@@ -1,7 +1,5 @@
|
||||
package de.dennisguse.opentracks.sensors.sensorData;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
import de.dennisguse.opentracks.data.models.Power;
|
||||
import de.dennisguse.opentracks.sensors.driver.CyclingPowerBluetooth;
|
||||
|
||||
@@ -13,21 +11,6 @@ public class AggregatorCyclingPower extends Aggregator<CyclingPowerBluetooth.Dat
|
||||
|
||||
@Override
|
||||
public void computeValue(Raw<CyclingPowerBluetooth.Data> current) {
|
||||
this.aggregatedValue = current.value().power();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void resetImmediate() {
|
||||
aggregatedValue = getNoneValue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void resetAggregated() {
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
protected Power getNoneValue() {
|
||||
return Power.of(0f);
|
||||
this.output = current.value().power();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,19 +10,13 @@ import de.dennisguse.opentracks.data.models.Position;
|
||||
|
||||
public class AggregatorGPS extends Aggregator<Location, Position> {
|
||||
|
||||
|
||||
public AggregatorGPS(String sensorAddress, String sensorName) {
|
||||
super(sensorAddress, sensorName);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void computeValue(Raw<Location> current) {
|
||||
aggregatedValue = Position.of(current.value(), current.time());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void resetImmediate() {
|
||||
aggregatedValue = Position.empty();
|
||||
output = Position.of(current.value(), current.time());
|
||||
}
|
||||
|
||||
@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).
|
||||
* 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
|
||||
@Override
|
||||
protected Position getNoneValue() {
|
||||
return Position.empty();
|
||||
output = null;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public Position getAggregatedValue(Instant now) {
|
||||
return super.getAggregatedValue(now)
|
||||
.with(now);
|
||||
Position value = super.getAggregatedValue(now);
|
||||
if (value == null) value = Position.empty();
|
||||
|
||||
return value.with(now);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
package de.dennisguse.opentracks.sensors.sensorData;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
import de.dennisguse.opentracks.data.models.HeartRate;
|
||||
|
||||
public class AggregatorHeartRate extends Aggregator<HeartRate, HeartRate> {
|
||||
@@ -13,22 +11,7 @@ public class AggregatorHeartRate extends Aggregator<HeartRate, HeartRate> {
|
||||
@Override
|
||||
protected void computeValue(Raw<HeartRate> current) {
|
||||
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;
|
||||
if (previous.value().totalDistance() != null && current.value().totalDistance() != null) {
|
||||
distance = current.value().totalDistance().minus(previous.value().totalDistance());
|
||||
if (aggregatedValue != null) {
|
||||
distance = distance.plus(aggregatedValue.distance);
|
||||
if (output != null) {
|
||||
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
|
||||
protected void resetImmediate() {
|
||||
aggregatedValue = new Data(Speed.ZERO, Cadence.of(0f), aggregatedValue.distance);
|
||||
protected void resetOutdated() {
|
||||
Distance distance = output != null ? output.distance : Distance.ZERO;
|
||||
output = new Data(Speed.ZERO, Cadence.of(0f), distance);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void resetAggregated() {
|
||||
if (aggregatedValue != null) {
|
||||
aggregatedValue = new Data(aggregatedValue.speed, aggregatedValue.cadence, Distance.ZERO);
|
||||
if (output != null) {
|
||||
output = new Data(output.speed, output.cadence, Distance.ZERO);
|
||||
}
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
protected Data getNoneValue() {
|
||||
return new Data(Speed.ZERO, Cadence.of(0f), Distance.ZERO);
|
||||
}
|
||||
|
||||
public record Data(Speed speed, Cadence cadence, @NonNull Distance distance) {
|
||||
public record Data(
|
||||
Speed speed,
|
||||
Cadence cadence,
|
||||
@NonNull Distance distance) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+1
-14
@@ -1,7 +1,5 @@
|
||||
package de.dennisguse.opentracks.sensors.sensorData;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
import java.time.Duration;
|
||||
|
||||
import de.dennisguse.opentracks.data.models.Temperature;
|
||||
@@ -13,20 +11,9 @@ public class AggregatorTemperature extends Aggregator<Temperature, Temperature>
|
||||
}
|
||||
|
||||
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
|
||||
protected Duration getMaxSensorAge() {
|
||||
return Duration.ofMinutes(1);
|
||||
|
||||
@@ -1,7 +1,11 @@
|
||||
package de.dennisguse.opentracks.sensors.sensorData;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
public record SensorData<T>(
|
||||
|
||||
|
||||
@Nullable
|
||||
T data,
|
||||
|
||||
String sensorNameOrAddress
|
||||
|
||||
Reference in New Issue
Block a user