Skip to content

Commit

Permalink
Sensor data: use monotonic clock.
Browse files Browse the repository at this point in the history
Part of #1966.
  • Loading branch information
dennisguse committed Dec 15, 2024
1 parent aaf78e0 commit 8082877
Show file tree
Hide file tree
Showing 11 changed files with 33 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public void handlePayload(SensorManager.SensorDataChangedObserver observer, Serv
AtmosphericPressure value = parseEnvironmentalSensing(characteristic);
if (value == null) return;

observer.onChange(new Raw<>(value));
observer.onChange(new Raw<>(observer.getNow(), value));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public void handlePayload(SensorManager.SensorDataChangedObserver observer, Serv
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;
}
Expand All @@ -42,7 +42,7 @@ public void handlePayload(SensorManager.SensorDataChangedObserver observer, Serv
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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public AggregatorCyclingDistanceSpeed createEmptySensorData(String address, Stri
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));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void handlePayload(SensorManager.SensorDataChangedObserver observer, @Non
Data cyclingPower = parseCyclingPower(characteristic);

if (cyclingPower != null) {
observer.onChange(new Raw<>(cyclingPower));
observer.onChange(new Raw<>(observer.getNow(), cyclingPower));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void handlePayload(SensorManager.SensorDataChangedObserver observer, @Non
HeartRate heartRate = parseHeartRate(characteristic);

if (heartRate != null) {
observer.onChange(new Raw<>(heartRate));
observer.onChange(new Raw<>(observer.getNow(), heartRate));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public AggregatorRunning createEmptySensorData(String address, String name) {
@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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
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;
Expand Down Expand Up @@ -47,6 +49,11 @@ public void onDisconnect(Aggregator<?, ?> aggregator) {
public void onRemove(Aggregator<?, ?> aggregator) {
sensorDataSet.remove(aggregator);
}

@Override
public Instant getNow() {
return observer.createNow();
}
};

private BluetoothRemoteSensorManager bluetoothSensorManager;
Expand Down Expand Up @@ -138,5 +145,7 @@ public interface SensorDataChangedObserver {
void onDisconnect(Aggregator<?, ?> sensorData);

void onRemove(Aggregator<?, ?> sensorData);

Instant getNow();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -34,7 +34,7 @@ public void onSensorChanged(SensorEvent event) {
return;
}

listener.onChange(new Raw<>(AtmosphericPressure.ofHPA(event.values[0])));
observer.onChange(new Raw<>(observer.getNow(), AtmosphericPressure.ofHPA(event.values[0])));
}

@Override
Expand All @@ -43,8 +43,8 @@ public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
};

public BarometerInternal(@NonNull SensorManager.SensorDataChangedObserver listener) {
this.listener = listener;
public BarometerInternal(@NonNull SensorManager.SensorDataChangedObserver observer) {
this.observer = observer;

}

Expand All @@ -60,7 +60,7 @@ public void connect(Context context, Handler handler, String addressIgnored) {

if (sensorManager.registerListener(sensorEventListener, pressureSensor, SAMPLING_PERIOD, handler)) {
this.context = context;
listener.onConnect(new AggregatorBarometer("internal", null));
observer.onConnect(new AggregatorBarometer("internal", null));
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class GPSManager implements SensorConnector, LocationListenerCompat, GpsS

private TrackPointCreator trackPointCreator;

private SensorManager.SensorDataChangedObserver listener;
private SensorManager.SensorDataChangedObserver observer;
private Context context;
private Handler handler;

Expand All @@ -46,9 +46,9 @@ public class GPSManager implements SensorConnector, LocationListenerCompat, GpsS
private Duration gpsInterval;
private Distance thresholdHorizontalAccuracy;

public GPSManager(TrackPointCreator trackPointCreator, SensorManager.SensorDataChangedObserver listener) {
public GPSManager(TrackPointCreator trackPointCreator, SensorManager.SensorDataChangedObserver observer) {
this.trackPointCreator = trackPointCreator;
this.listener = listener;
this.observer = observer;
}

public void start(@NonNull Context context, @NonNull Handler handler) {
Expand All @@ -60,7 +60,7 @@ public void start(@NonNull Context context, @NonNull Handler handler) {
gpsStatusManager = new GpsStatusManager(context, this, handler);
locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

listener.onConnect(new AggregatorGPS("internal"));
observer.onConnect(new AggregatorGPS("internal"));

registerLocationListener();
gpsStatusManager.start();
Expand All @@ -82,8 +82,8 @@ public void stop(Context context) {
gpsStatusManager.stop();
gpsStatusManager = null;

listener.onDisconnect(new AggregatorGPS("internal"));
listener = null;
observer.onDisconnect(new AggregatorGPS("internal"));
observer = null;

trackPointCreator = null;
}
Expand Down Expand Up @@ -139,7 +139,7 @@ public void onLocationChanged(@NonNull Location location) {
return;
}

listener.onChange(new Raw<>(Position.of(location)));
observer.onChange(new Raw<>(observer.getNow(), Position.of(location)));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,7 @@ public Pair<TrackPoint, SensorDataSet> createCurrentTrackPoint(@Nullable TrackPo
return new Pair<>(currentTrackPoint, sensorDataSet);
}

@VisibleForTesting
Instant createNow() {
public Instant createNow() {
return Instant.now(clock);
}

Expand Down

0 comments on commit 8082877

Please sign in to comment.