forked from upstream-mirrors/OpenTracks
Created CyclingPowerHandler.
This commit is contained in:
@@ -0,0 +1,57 @@
|
|||||||
|
package de.dennisguse.opentracks.sensors;
|
||||||
|
|
||||||
|
import androidx.annotation.NonNull;
|
||||||
|
|
||||||
|
import de.dennisguse.opentracks.R;
|
||||||
|
import de.dennisguse.opentracks.sensors.driver.Driver;
|
||||||
|
import de.dennisguse.opentracks.sensors.driver.DriverObserver0;
|
||||||
|
import de.dennisguse.opentracks.sensors.sensorData.Aggregator;
|
||||||
|
import de.dennisguse.opentracks.sensors.sensorData.AggregatorCyclingPower;
|
||||||
|
|
||||||
|
public class CyclingPowerHandler extends SensorHandler<BluetoothHandlerCyclingPower.Data> {
|
||||||
|
|
||||||
|
protected CyclingPowerHandler(SensorManager sensorManager) {
|
||||||
|
super(sensorManager);
|
||||||
|
}
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
@Override
|
||||||
|
protected Driver createDriverBluetooth(String address) {
|
||||||
|
return new BluetoothConnectionManager(
|
||||||
|
BluetoothUtils.getAdapter(getContext()),
|
||||||
|
new DriverObserver0() {
|
||||||
|
@Override
|
||||||
|
public void onConnect(Aggregator<?, ?> emptySensorData) {
|
||||||
|
driverObserver.onConnect();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onDisconnect(Aggregator<?, ?> emptySensorData) {
|
||||||
|
driverObserver.onDisconnect();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onRemove(Aggregator<?, ?> emptySensorData) {
|
||||||
|
driverObserver.onRemove();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onChange(Record value) {
|
||||||
|
driverObserver.onData((BluetoothHandlerCyclingPower.Data) value);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
new BluetoothHandlerCyclingPower()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
@Override
|
||||||
|
protected AggregatorCyclingPower createAggregator() {
|
||||||
|
return new AggregatorCyclingPower("", null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected int getSensorPreferenceKey() {
|
||||||
|
return R.string.settings_sensor_bluetooth_cycling_power_key;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -19,7 +19,6 @@ import de.dennisguse.opentracks.data.models.Distance;
|
|||||||
import de.dennisguse.opentracks.data.models.HeartRate;
|
import de.dennisguse.opentracks.data.models.HeartRate;
|
||||||
import de.dennisguse.opentracks.data.models.Position;
|
import de.dennisguse.opentracks.data.models.Position;
|
||||||
import de.dennisguse.opentracks.data.models.Speed;
|
import de.dennisguse.opentracks.data.models.Speed;
|
||||||
import de.dennisguse.opentracks.data.models.Temperature;
|
|
||||||
import de.dennisguse.opentracks.sensors.driver.DriverObserver0;
|
import de.dennisguse.opentracks.sensors.driver.DriverObserver0;
|
||||||
import de.dennisguse.opentracks.sensors.sensorData.Aggregator;
|
import de.dennisguse.opentracks.sensors.sensorData.Aggregator;
|
||||||
import de.dennisguse.opentracks.sensors.sensorData.AggregatorBarometer;
|
import de.dennisguse.opentracks.sensors.sensorData.AggregatorBarometer;
|
||||||
@@ -54,7 +53,7 @@ public class SensorManager implements SharedPreferences.OnSharedPreferenceChange
|
|||||||
private TemperatureHandler temperatureHandler;
|
private TemperatureHandler temperatureHandler;
|
||||||
private CyclingCadenceHandler cyclingCadenceHandler;
|
private CyclingCadenceHandler cyclingCadenceHandler;
|
||||||
private CyclingDistanceSpeedHandler cyclingDistanceSpeedHandler;
|
private CyclingDistanceSpeedHandler cyclingDistanceSpeedHandler;
|
||||||
private BluetoothConnectionManager cyclingPowerManager;
|
private CyclingPowerHandler cyclingPowerHandler;
|
||||||
private BluetoothConnectionManager runningSpeedAndCadenceManager;
|
private BluetoothConnectionManager runningSpeedAndCadenceManager;
|
||||||
|
|
||||||
private GainManager altitudeSumManager;
|
private GainManager altitudeSumManager;
|
||||||
@@ -80,9 +79,9 @@ public class SensorManager implements SharedPreferences.OnSharedPreferenceChange
|
|||||||
this.temperatureHandler = new TemperatureHandler(this);
|
this.temperatureHandler = new TemperatureHandler(this);
|
||||||
this.cyclingCadenceHandler = new CyclingCadenceHandler(this);
|
this.cyclingCadenceHandler = new CyclingCadenceHandler(this);
|
||||||
this.cyclingDistanceSpeedHandler = new CyclingDistanceSpeedHandler(this);
|
this.cyclingDistanceSpeedHandler = new CyclingDistanceSpeedHandler(this);
|
||||||
|
this.cyclingPowerHandler = new CyclingPowerHandler(this);
|
||||||
|
|
||||||
BluetoothAdapter bluetoothAdapter = BluetoothUtils.getAdapter(context);
|
BluetoothAdapter bluetoothAdapter = BluetoothUtils.getAdapter(context);
|
||||||
this.cyclingPowerManager = new BluetoothConnectionManager(bluetoothAdapter, this, new BluetoothHandlerCyclingPower());
|
|
||||||
this.runningSpeedAndCadenceManager = new BluetoothConnectionManager(bluetoothAdapter, this, new BluetoothHandlerRunningSpeedAndCadence());
|
this.runningSpeedAndCadenceManager = new BluetoothConnectionManager(bluetoothAdapter, this, new BluetoothHandlerRunningSpeedAndCadence());
|
||||||
|
|
||||||
altitudeSumManager.start(context, handler);
|
altitudeSumManager.start(context, handler);
|
||||||
@@ -90,6 +89,7 @@ public class SensorManager implements SharedPreferences.OnSharedPreferenceChange
|
|||||||
temperatureHandler.start(context, handler);
|
temperatureHandler.start(context, handler);
|
||||||
cyclingCadenceHandler.start(context, handler);
|
cyclingCadenceHandler.start(context, handler);
|
||||||
cyclingDistanceSpeedHandler.start(context, handler);
|
cyclingDistanceSpeedHandler.start(context, handler);
|
||||||
|
cyclingPowerHandler.start(context, handler);
|
||||||
onSharedPreferenceChanged(null, null);
|
onSharedPreferenceChanged(null, null);
|
||||||
|
|
||||||
gpsManager.start(context, handler);
|
gpsManager.start(context, handler);
|
||||||
@@ -110,8 +110,8 @@ public class SensorManager implements SharedPreferences.OnSharedPreferenceChange
|
|||||||
cyclingDistanceSpeedHandler.stop();
|
cyclingDistanceSpeedHandler.stop();
|
||||||
cyclingDistanceSpeedHandler = null;
|
cyclingDistanceSpeedHandler = null;
|
||||||
|
|
||||||
cyclingPowerManager.disconnect();
|
cyclingPowerHandler.stop();
|
||||||
cyclingPowerManager = null;
|
cyclingPowerHandler = null;
|
||||||
|
|
||||||
runningSpeedAndCadenceManager.disconnect();
|
runningSpeedAndCadenceManager.disconnect();
|
||||||
runningSpeedAndCadenceManager = null;
|
runningSpeedAndCadenceManager = null;
|
||||||
@@ -140,7 +140,7 @@ public class SensorManager implements SharedPreferences.OnSharedPreferenceChange
|
|||||||
if (temperatureHandler.getAggregator() != null) temperatureHandler.getAggregator().resetAggregated();
|
if (temperatureHandler.getAggregator() != null) temperatureHandler.getAggregator().resetAggregated();
|
||||||
if (cyclingCadenceHandler.getAggregator() != null) cyclingCadenceHandler.getAggregator().resetAggregated();
|
if (cyclingCadenceHandler.getAggregator() != null) cyclingCadenceHandler.getAggregator().resetAggregated();
|
||||||
if (cyclingDistanceSpeedHandler.getAggregator() != null) cyclingDistanceSpeedHandler.getAggregator().resetAggregated();
|
if (cyclingDistanceSpeedHandler.getAggregator() != null) cyclingDistanceSpeedHandler.getAggregator().resetAggregated();
|
||||||
if (cyclingPower != null) cyclingPower.resetAggregated();
|
if (cyclingPowerHandler.getAggregator() != null) cyclingPowerHandler.getAggregator().resetAggregated();
|
||||||
if (runningDistanceSpeedCadence != null) runningDistanceSpeedCadence.resetAggregated();
|
if (runningDistanceSpeedCadence != null) runningDistanceSpeedCadence.resetAggregated();
|
||||||
if (altitudeSumManager.getAggregator() != null) altitudeSumManager.getAggregator().resetAggregated();
|
if (altitudeSumManager.getAggregator() != null) altitudeSumManager.getAggregator().resetAggregated();
|
||||||
if (gpsManager.getAggregatorGPS() != null) gpsManager.getAggregatorGPS().resetAggregated();
|
if (gpsManager.getAggregatorGPS() != null) gpsManager.getAggregatorGPS().resetAggregated();
|
||||||
@@ -191,10 +191,8 @@ public class SensorManager implements SharedPreferences.OnSharedPreferenceChange
|
|||||||
cyclingDistanceSpeedHandler.connect();
|
cyclingDistanceSpeedHandler.connect();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (PreferencesUtils.isKey(R.string.settings_sensor_bluetooth_cycling_power_key, key)) {
|
if (PreferencesUtils.isKey(cyclingCadenceHandler.getSensorPreferenceKey(), key)) {
|
||||||
String address = PreferencesUtils.getBluetoothCyclingPowerSensorAddress();
|
cyclingPowerHandler.connect();
|
||||||
|
|
||||||
connect(cyclingPowerManager, address);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (PreferencesUtils.isKey(R.string.settings_sensor_bluetooth_running_speed_and_cadence_key, key)) {
|
if (PreferencesUtils.isKey(R.string.settings_sensor_bluetooth_running_speed_and_cadence_key, key)) {
|
||||||
@@ -225,9 +223,6 @@ public class SensorManager implements SharedPreferences.OnSharedPreferenceChange
|
|||||||
return observer.getNow();
|
return observer.getNow();
|
||||||
}
|
}
|
||||||
|
|
||||||
@VisibleForTesting
|
|
||||||
public AggregatorCyclingPower cyclingPower;
|
|
||||||
|
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
public AggregatorRunning runningDistanceSpeedCadence;
|
public AggregatorRunning runningDistanceSpeedCadence;
|
||||||
|
|
||||||
@@ -256,7 +251,7 @@ public class SensorManager implements SharedPreferences.OnSharedPreferenceChange
|
|||||||
|
|
||||||
private SensorData<Distance> getDistance(Instant now) {
|
private SensorData<Distance> getDistance(Instant now) {
|
||||||
if (cyclingDistanceSpeedHandler.getAggregator() != null && cyclingDistanceSpeedHandler.getAggregator().hasReceivedData()) {
|
if (cyclingDistanceSpeedHandler.getAggregator() != null && cyclingDistanceSpeedHandler.getAggregator().hasReceivedData()) {
|
||||||
return new SensorData<>(((AggregatorCyclingDistanceSpeed)cyclingDistanceSpeedHandler.getAggregator()).getAggregatedValue(now).distanceOverall(), cyclingDistanceSpeedHandler.getAggregator().getSensorNameOrAddress());
|
return new SensorData<>(((AggregatorCyclingDistanceSpeed) cyclingDistanceSpeedHandler.getAggregator()).getAggregatedValue(now).distanceOverall(), cyclingDistanceSpeedHandler.getAggregator().getSensorNameOrAddress());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (runningDistanceSpeedCadence != null && runningDistanceSpeedCadence.hasReceivedData()) {
|
if (runningDistanceSpeedCadence != null && runningDistanceSpeedCadence.hasReceivedData()) {
|
||||||
@@ -270,8 +265,8 @@ public class SensorManager implements SharedPreferences.OnSharedPreferenceChange
|
|||||||
//TOOD simplify?
|
//TOOD simplify?
|
||||||
private SensorData<Speed> getSpeed(Instant now) {
|
private SensorData<Speed> getSpeed(Instant now) {
|
||||||
|
|
||||||
if (cyclingDistanceSpeedHandler.getAggregator() != null && cyclingDistanceSpeedHandler.getAggregator().hasReceivedData() && ((AggregatorCyclingDistanceSpeed)cyclingDistanceSpeedHandler.getAggregator()).getAggregatedValue(now).speed() != null) {
|
if (cyclingDistanceSpeedHandler.getAggregator() != null && cyclingDistanceSpeedHandler.getAggregator().hasReceivedData() && ((AggregatorCyclingDistanceSpeed) cyclingDistanceSpeedHandler.getAggregator()).getAggregatedValue(now).speed() != null) {
|
||||||
return new SensorData<>(((AggregatorCyclingDistanceSpeed)cyclingDistanceSpeedHandler.getAggregator()).getAggregatedValue(now).speed(), cyclingDistanceSpeedHandler.getAggregator().getSensorNameOrAddress());
|
return new SensorData<>(((AggregatorCyclingDistanceSpeed) cyclingDistanceSpeedHandler.getAggregator()).getAggregatedValue(now).speed(), cyclingDistanceSpeedHandler.getAggregator().getSensorNameOrAddress());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (runningDistanceSpeedCadence != null && runningDistanceSpeedCadence.hasReceivedData() && runningDistanceSpeedCadence.getAggregatedValue(now).speed() != null) {
|
if (runningDistanceSpeedCadence != null && runningDistanceSpeedCadence.hasReceivedData() && runningDistanceSpeedCadence.getAggregatedValue(now).speed() != null) {
|
||||||
@@ -301,6 +296,11 @@ public class SensorManager implements SharedPreferences.OnSharedPreferenceChange
|
|||||||
cyclingDistanceSpeedHandler.setAggregator((AggregatorCyclingDistanceSpeed) data);
|
cyclingDistanceSpeedHandler.setAggregator((AggregatorCyclingDistanceSpeed) data);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if (data instanceof AggregatorCyclingPower) {
|
||||||
|
cyclingPowerHandler.setAggregator((Aggregator<BluetoothHandlerCyclingPower.Data, ?>) data);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
set(data, data);
|
set(data, data);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -323,10 +323,6 @@ public class SensorManager implements SharedPreferences.OnSharedPreferenceChange
|
|||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (value instanceof BluetoothHandlerCyclingPower.Data) {
|
|
||||||
this.cyclingPower.add(now, (BluetoothHandlerCyclingPower.Data) value);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
throw new UnsupportedOperationException(value.getClass().getCanonicalName());
|
throw new UnsupportedOperationException(value.getClass().getCanonicalName());
|
||||||
}
|
}
|
||||||
@@ -340,7 +336,6 @@ public class SensorManager implements SharedPreferences.OnSharedPreferenceChange
|
|||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
public void clear() {
|
public void clear() {
|
||||||
Log.i(TAG, "Removing all aggregators");
|
Log.i(TAG, "Removing all aggregators");
|
||||||
this.cyclingPower = null;
|
|
||||||
this.runningDistanceSpeedCadence = null;
|
this.runningDistanceSpeedCadence = null;
|
||||||
|
|
||||||
//TODO This is to ExportImportTest alive; refactor test.
|
//TODO This is to ExportImportTest alive; refactor test.
|
||||||
@@ -359,6 +354,9 @@ public class SensorManager implements SharedPreferences.OnSharedPreferenceChange
|
|||||||
if (cyclingDistanceSpeedHandler != null) {
|
if (cyclingDistanceSpeedHandler != null) {
|
||||||
cyclingDistanceSpeedHandler.setAggregator(null);
|
cyclingDistanceSpeedHandler.setAggregator(null);
|
||||||
}
|
}
|
||||||
|
if (cyclingPowerHandler != null) {
|
||||||
|
cyclingPowerHandler.setAggregator(null);
|
||||||
|
}
|
||||||
if (this.gpsManager != null) {
|
if (this.gpsManager != null) {
|
||||||
this.gpsManager.setAggregatorGPS();
|
this.gpsManager.setAggregatorGPS();
|
||||||
}
|
}
|
||||||
@@ -373,9 +371,9 @@ public class SensorManager implements SharedPreferences.OnSharedPreferenceChange
|
|||||||
getSpeed(now),
|
getSpeed(now),
|
||||||
getDistance(now),
|
getDistance(now),
|
||||||
getHeartRate(now),
|
getHeartRate(now),
|
||||||
temperatureHandler.getAggregator() != null && temperatureHandler.getAggregator().hasReceivedData() ? (SensorData<Temperature>) temperatureHandler.getAggregator().getAggregatedValueWithSensorName(now) : null,
|
temperatureHandler.getAggregator() != null && temperatureHandler.getAggregator().hasReceivedData() ? ((AggregatorTemperature) temperatureHandler.getAggregator()).getAggregatedValueWithSensorName(now) : null,
|
||||||
getCadence(now),
|
getCadence(now),
|
||||||
cyclingPower != null && cyclingPower.hasReceivedData() ? cyclingPower.getAggregatedValueWithSensorName(now) : null,
|
cyclingPowerHandler.getAggregator() != null && cyclingPowerHandler.getAggregator().hasReceivedData() ? ((AggregatorCyclingPower) cyclingPowerHandler.getAggregator()).getAggregatedValueWithSensorName(now) : null,
|
||||||
barometer != null && barometer.hasReceivedData() ? barometer.getAggregatedValueWithSensorName(now) : null
|
barometer != null && barometer.hasReceivedData() ? barometer.getAggregatedValueWithSensorName(now) : null
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -384,10 +382,6 @@ public class SensorManager implements SharedPreferences.OnSharedPreferenceChange
|
|||||||
private void set(@NonNull Aggregator<?, ?> type, @Nullable Aggregator<?, ?> sensorData) {
|
private void set(@NonNull Aggregator<?, ?> type, @Nullable Aggregator<?, ?> sensorData) {
|
||||||
Log.i(TAG, "Setting aggregator " + type.getClass().getCanonicalName() + " to " + sensorData);
|
Log.i(TAG, "Setting aggregator " + type.getClass().getCanonicalName() + " to " + sensorData);
|
||||||
|
|
||||||
if (type instanceof AggregatorCyclingPower) {
|
|
||||||
cyclingPower = (AggregatorCyclingPower) sensorData;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (type instanceof AggregatorRunning) {
|
if (type instanceof AggregatorRunning) {
|
||||||
runningDistanceSpeedCadence = (AggregatorRunning) sensorData;
|
runningDistanceSpeedCadence = (AggregatorRunning) sensorData;
|
||||||
return;
|
return;
|
||||||
|
|||||||
@@ -269,10 +269,6 @@ public class PreferencesUtils {
|
|||||||
return Distance.ofMM(getInt(R.string.settings_sensor_bluetooth_cycling_speed_wheel_circumference_key, DEFAULT));
|
return Distance.ofMM(getInt(R.string.settings_sensor_bluetooth_cycling_speed_wheel_circumference_key, DEFAULT));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String getBluetoothCyclingPowerSensorAddress() {
|
|
||||||
return getString(R.string.settings_sensor_bluetooth_cycling_power_key, getBluetoothSensorAddressNone());
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String getBluetoothRunningSpeedAndCadenceAddress() {
|
public static String getBluetoothRunningSpeedAndCadenceAddress() {
|
||||||
return getString(R.string.settings_sensor_bluetooth_running_speed_and_cadence_key, getBluetoothSensorAddressNone());
|
return getString(R.string.settings_sensor_bluetooth_running_speed_and_cadence_key, getBluetoothSensorAddressNone());
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user