Sensor data: use monotonic clock.

Part of #1966.
This commit is contained in:
Dennis Guse
2024-12-15 17:57:04 +01:00
parent a2a9132658
commit 333bc6f31b
11 changed files with 33 additions and 25 deletions
@@ -33,7 +33,7 @@ public class BluetoothHandlerBarometricPressure implements SensorHandlerInterfac
AtmosphericPressure value = parseEnvironmentalSensing(characteristic);
if (value == null) return;
observer.onChange(new Raw<>(value));
observer.onChange(new Raw<>(observer.getNow(), value));
}
/**
@@ -33,7 +33,7 @@ public class BluetoothHandlerCyclingCadence implements SensorHandlerInterface {
if (serviceMeasurementUUID.equals(BluetoothHandlerManagerCyclingPower.CYCLING_POWER)) {
BluetoothHandlerManagerCyclingPower.Data data = BluetoothHandlerManagerCyclingPower.parseCyclingPower(characteristic);
if (data != null && data.crank() != null) {
observer.onChange(new Raw<>(data.crank()));
observer.onChange(new Raw<>(observer.getNow(), data.crank()));
}
return;
}
@@ -42,7 +42,7 @@ public class BluetoothHandlerCyclingCadence implements SensorHandlerInterface {
Pair<BluetoothHandlerCyclingDistanceSpeed.WheelData, CrankData> data = BluetoothHandlerCyclingDistanceSpeed.parseCyclingCrankAndWheel(address, sensorName, characteristic);
if (data != null && data.second != null) {
observer.onChange(new Raw<>(data.second));
observer.onChange(new Raw<>(observer.getNow(), data.second));
}
return;
}
@@ -34,7 +34,7 @@ public class BluetoothHandlerCyclingDistanceSpeed implements SensorHandlerInterf
public void handlePayload(SensorManager.SensorDataChangedObserver observer, ServiceMeasurementUUID serviceMeasurementUUID, String sensorName, String address, BluetoothGattCharacteristic characteristic) {
Pair<WheelData, BluetoothHandlerCyclingCadence.CrankData> data = parseCyclingCrankAndWheel(address, sensorName, characteristic);
if (data.first != null) {
observer.onChange(new Raw<>(data.first));
observer.onChange(new Raw<>(observer.getNow(), data.first));
}
}
@@ -35,7 +35,7 @@ public class BluetoothHandlerManagerCyclingPower implements SensorHandlerInterfa
Data cyclingPower = parseCyclingPower(characteristic);
if (cyclingPower != null) {
observer.onChange(new Raw<>(cyclingPower));
observer.onChange(new Raw<>(observer.getNow(), cyclingPower));
}
}
@@ -45,7 +45,7 @@ public class BluetoothHandlerManagerHeartRate implements SensorHandlerInterface
HeartRate heartRate = parseHeartRate(characteristic);
if (heartRate != null) {
observer.onChange(new Raw<>(heartRate));
observer.onChange(new Raw<>(observer.getNow(), heartRate));
}
}
@@ -36,7 +36,7 @@ public class BluetoothHandlerRunningSpeedAndCadence implements SensorHandlerInte
@Override
public void handlePayload(SensorManager.SensorDataChangedObserver observer, @NonNull ServiceMeasurementUUID serviceMeasurementUUID, String sensorName, String address, BluetoothGattCharacteristic characteristic) {
Data data = parseRunningSpeedAndCadence(sensorName, characteristic);
observer.onChange(new Raw<>(data));
observer.onChange(new Raw<>(observer.getNow(), data));
}
@VisibleForTesting
@@ -8,6 +8,8 @@ import android.util.Log;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import java.time.Instant;
import de.dennisguse.opentracks.data.models.TrackPoint;
import de.dennisguse.opentracks.sensors.sensorData.Aggregator;
import de.dennisguse.opentracks.sensors.sensorData.Raw;
@@ -47,6 +49,11 @@ public class SensorManager implements SharedPreferences.OnSharedPreferenceChange
public void onRemove(Aggregator<?, ?> aggregator) {
sensorDataSet.remove(aggregator);
}
@Override
public Instant getNow() {
return observer.createNow();
}
};
private BluetoothRemoteSensorManager bluetoothSensorManager;
@@ -138,5 +145,7 @@ public class SensorManager implements SharedPreferences.OnSharedPreferenceChange
void onDisconnect(Aggregator<?, ?> sensorData);
void onRemove(Aggregator<?, ?> sensorData);
Instant getNow();
}
}
@@ -22,7 +22,7 @@ public class BarometerInternal implements Driver {
private static final int SAMPLING_PERIOD = (int) TimeUnit.SECONDS.toMicros(5);
private final SensorManager.SensorDataChangedObserver listener;
private final SensorManager.SensorDataChangedObserver observer;
private Context context;
@@ -34,7 +34,7 @@ public class BarometerInternal implements Driver {
return;
}
listener.onChange(new Raw<>(AtmosphericPressure.ofHPA(event.values[0])));
observer.onChange(new Raw<>(observer.getNow(), AtmosphericPressure.ofHPA(event.values[0])));
}
@Override
@@ -43,8 +43,8 @@ public class BarometerInternal implements Driver {
}
};
public BarometerInternal(@NonNull SensorManager.SensorDataChangedObserver listener) {
this.listener = listener;
public BarometerInternal(@NonNull SensorManager.SensorDataChangedObserver observer) {
this.observer = observer;
}
@@ -60,7 +60,7 @@ public class BarometerInternal implements Driver {
if (sensorManager.registerListener(sensorEventListener, pressureSensor, SAMPLING_PERIOD, handler)) {
this.context = context;
listener.onConnect(new AggregatorBarometer("internal", null));
observer.onConnect(new AggregatorBarometer("internal", null));
return;
}
@@ -6,11 +6,11 @@ import androidx.annotation.NonNull;
import java.time.Instant;
public record Raw<T>(
@NonNull T value,
@NonNull Instant time
@NonNull Instant time,
@NonNull T value
) {
@Deprecated
public Raw(@NonNull T value) {
this(value, Instant.now()); //TODO We should be using the MonotonicClock
this(Instant.now(), value); //TODO We should be using the MonotonicClock
}
}