Skip to content

Commit

Permalink
TrackPoint: all sensors are the same - no need to distinguish the sou…
Browse files Browse the repository at this point in the history
…rce of a TrackPoint (BLE sensor vs GPS).

Part of #1187.
  • Loading branch information
dennisguse committed Aug 2, 2023
1 parent 02770b3 commit 0e3cc98
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
case 33 -> upgradeFrom32to33(db);
case 34 -> upgradeFrom33to34(db);
case 35 -> upgradeFrom34to35(db);
case 36 -> upgradeFrom35to36(db);
default -> throw new RuntimeException("Not implemented: upgrade to " + toVersion);
}
}
Expand All @@ -95,6 +96,7 @@ public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
case 32 -> downgradeFrom33to32(db);
case 33 -> downgradeFrom34to33(db);
case 34 -> downgradeFrom35to34(db);
case 35 -> downgradeFrom36to35(db);
default -> throw new RuntimeException("Not implemented: downgrade to " + toVersion);
}
}
Expand Down Expand Up @@ -532,4 +534,32 @@ private void downgradeFrom35to34(SQLiteDatabase db) {
db.setTransactionSuccessful();
db.endTransaction();
}

private void upgradeFrom35to36(SQLiteDatabase db) {
db.beginTransaction();

db.execSQL("ALTER TABLE trackpoints RENAME TO trackpoints_old");
db.execSQL("CREATE TABLE trackpoints (_id INTEGER PRIMARY KEY AUTOINCREMENT, trackid INTEGER NOT NULL, longitude INTEGER, latitude INTEGER, time INTEGER, elevation FLOAT, accuracy FLOAT, speed FLOAT, bearing FLOAT, sensor_heartrate FLOAT, sensor_cadence FLOAT, sensor_power FLOAT, elevation_gain FLOAT, elevation_loss FLOAT, type TEXT CHECK(type IN (-2, -1, 0, 1, 3)), sensor_distance FLOAT, accuracy_vertical FLOAT, FOREIGN KEY (trackid) REFERENCES tracks(_id) ON UPDATE CASCADE ON DELETE CASCADE)");
db.execSQL("INSERT INTO trackpoints SELECT _id, trackid, longitude, latitude, time, elevation, accuracy, speed, bearing, sensor_heartrate, sensor_cadence, sensor_power, elevation_gain, elevation_gain, type, sensor_distance, accuracy_vertical FROM trackpoints_old");
db.execSQL("DROP TABLE trackpoints_old");

db.execSQL("CREATE INDEX trackpoints_trackid_index ON trackpoints(trackid)");

db.setTransactionSuccessful();
db.endTransaction();
}

private void downgradeFrom36to35(SQLiteDatabase db) {
db.beginTransaction();

db.execSQL("ALTER TABLE trackpoints RENAME TO trackpoints_old");
db.execSQL("CREATE TABLE trackpoints (_id INTEGER PRIMARY KEY AUTOINCREMENT, trackid INTEGER NOT NULL, longitude INTEGER, latitude INTEGER, time INTEGER, elevation FLOAT, accuracy FLOAT, speed FLOAT, bearing FLOAT, sensor_heartrate FLOAT, sensor_cadence FLOAT, sensor_power FLOAT, elevation_gain FLOAT, elevation_loss FLOAT, type TEXT CHECK(type IN (-2, -1, 0, 1, 2)), sensor_distance FLOAT, accuracy_vertical FLOAT, FOREIGN KEY (trackid) REFERENCES tracks(_id) ON UPDATE CASCADE ON DELETE CASCADE)");
db.execSQL("INSERT INTO trackpoints SELECT _id, trackid, longitude, latitude, time, elevation, accuracy, speed, bearing, sensor_heartrate, sensor_cadence, sensor_power, elevation_gain, elevation_gain, type, sensor_distance, accuracy_vertical FROM trackpoints_old");
db.execSQL("DROP TABLE trackpoints_old");

db.execSQL("CREATE INDEX trackpoints_trackid_index ON trackpoints(trackid)");

db.setTransactionSuccessful();
db.endTransaction();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,11 @@ public enum Type {
SEGMENT_START_MANUAL(-2), //Start of a segment due to user interaction (start, resume)

SEGMENT_START_AUTOMATIC(-1), //Start of a segment due to too much distance from previous TrackPoint
TRACKPOINT(0), //Just GPS data and may contain BLE sensor data
SENSORPOINT(2), //Just BLE sensor data; may have speed and sensorDistance
TRACKPOINT(0), //Was created due to sensor data (may contain GPS or other BLE data)

// Was used to distinguish the source (i.e., GPS vs BLE sensor), but this was too complicated. Everything is now a TRACKPOINT again.
@Deprecated
SENSORPOINT(2),
IDLE(3), //Device became idle

SEGMENT_END_MANUAL(1); //End of a segment
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public interface TrackPointsColumns extends BaseColumns {
+ SENSOR_POWER + " FLOAT, "
+ ALTITUDE_GAIN + " FLOAT, "
+ ALTITUDE_LOSS + " FLOAT, "
+ TYPE + " TEXT CHECK(type IN (-2, -1, 0, 1, 2, 3)), "
+ TYPE + " TEXT CHECK(type IN (-2, -1, 0, 1, 3)), "
+ SENSOR_DISTANCE + " FLOAT, "
+ VERTICAL_ACCURACY + " FLOAT, "
+ "FOREIGN KEY (" + TRACKID + ") REFERENCES " + TracksColumns.TABLE_NAME + "(" + TracksColumns._ID + ") ON UPDATE CASCADE ON DELETE CASCADE"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,14 +133,7 @@ private void writeTrackPoints(List<Column> columns, Track track) throws Interrup
while (trackPointIterator.hasNext()) {
if (Thread.interrupted()) throw new InterruptedException();

TrackPoint trackPoint = trackPointIterator.next();

switch (trackPoint.getType()) {
case SEGMENT_START_MANUAL, SEGMENT_END_MANUAL, SEGMENT_START_AUTOMATIC, SENSORPOINT, TRACKPOINT ->
writeTrackPoint(columns, trackPoint);
default ->
throw new RuntimeException("Exporting this TrackPoint type is not implemented: " + trackPoint.getType());
}
writeTrackPoint(columns, trackPointIterator.next());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,15 +154,18 @@ private void writeTrackPoints(Track track) throws InterruptedException {
trackDistance = trackDistance.plus(writeTrackPoint(track.getZoneOffset(), trackPoint, sensorPoints, trackDistance));
sensorPoints.clear();
}
case SENSORPOINT -> sensorPoints.add(trackPoint);
case TRACKPOINT -> {
if (!wroteSegment) {
// Might happen for older data (pre v3.15.0)
writeOpenSegment();
wroteSegment = true;
}
trackDistance = trackDistance.plus(writeTrackPoint(track.getZoneOffset(), trackPoint, sensorPoints, trackDistance));
sensorPoints.clear();
if (trackPoint.hasLocation()) {
trackDistance = trackDistance.plus(writeTrackPoint(track.getZoneOffset(), trackPoint, sensorPoints, trackDistance));
sensorPoints.clear();
} else {
sensorPoints.add(trackPoint);
}
}
default ->
throw new RuntimeException("Exporting this TrackPoint type is not implemented: " + trackPoint.getType());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ private void writeLocations(Track track) throws InterruptedException {
writeCloseSegment();
wroteSegment = false;
}
case SENSORPOINT, TRACKPOINT -> {
case TRACKPOINT -> {
if (!wroteSegment) {
// Might happen for older data (pre v3.15.0)
writeOpenSegment();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -308,9 +308,8 @@ private void onTrackSegmentEnd() {
Instant time = whenList.get(i);
Location location = locationList.get(i);

TrackPoint trackPoint = new TrackPoint(TrackPoint.Type.SENSORPOINT, time);
TrackPoint trackPoint = new TrackPoint(TrackPoint.Type.TRACKPOINT, time);
if (location != null) {
trackPoint.setType(TrackPoint.Type.TRACKPOINT);
trackPoint.setLocation(location);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public synchronized void onChange(@NonNull Location location) {
* Got a new TrackPoint from Bluetooth only; contains no GPS location.
*/
public synchronized void onChange(@NonNull SensorDataSet unused) {
onNewTrackPoint(new TrackPoint(TrackPoint.Type.SENSORPOINT, createNow()));
onNewTrackPoint(new TrackPoint(TrackPoint.Type.TRACKPOINT, createNow()));
}

@VisibleForTesting
Expand Down

0 comments on commit 0e3cc98

Please sign in to comment.