1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-24 02:46:50 +01:00

Fix gpx hr parsing and unit tests

This commit is contained in:
José Rebelo 2024-09-23 22:13:01 +01:00
parent 8d24dfa7e4
commit 49750e31af
4 changed files with 26 additions and 11 deletions

View File

@ -18,6 +18,8 @@ package nodomain.freeyourgadget.gadgetbridge.model;
import android.location.Location;
import androidx.annotation.NonNull;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Comparator;
@ -111,6 +113,7 @@ public class GPSCoordinate {
return new BigDecimal(value).setScale(8, RoundingMode.HALF_UP).toPlainString();
}
@NonNull
@Override
public String toString() {
return "lon: " + formatLocation(longitude) + ", lat: " + formatLocation(latitude) + ", alt: " + formatLocation(altitude) + "m";

View File

@ -226,7 +226,7 @@ public class GpxParser {
while (eventType != XmlPullParser.END_TAG || !parser.getName().equals("extensions")) {
if (parser.getEventType() == XmlPullParser.START_TAG) {
switch (parser.getName()) {
case "gpxtpx:TrackPointExtension":
case "TrackPointExtension":
parseTrackPointExtensions(trackPointBuilder);
continue;
}
@ -237,11 +237,11 @@ public class GpxParser {
}
private void parseTrackPointExtensions(final GpxTrackPoint.Builder trackPointBuilder) throws Exception {
while (eventType != XmlPullParser.END_TAG || !parser.getName().equals("gpxtpx:TrackPointExtension")) {
while (eventType != XmlPullParser.END_TAG || !parser.getName().equals("TrackPointExtension")) {
if (parser.getEventType() == XmlPullParser.START_TAG) {
switch (parser.getName()) {
case "gpxtpx:hr":
trackPointBuilder.withHeartRate(Integer.parseInt(parseStringContent("gpxtpx:hr")));
case "hr":
trackPointBuilder.withHeartRate(Integer.parseInt(parseStringContent("hr")));
continue;
}
}

View File

@ -16,6 +16,8 @@
along with this program. If not, see <https://www.gnu.org/licenses/>. */
package nodomain.freeyourgadget.gadgetbridge.util.gpx.model;
import androidx.annotation.NonNull;
import java.util.Date;
import java.util.Objects;
@ -59,6 +61,12 @@ public class GpxTrackPoint extends GPSCoordinate {
return Objects.hash(super.hashCode(), time, heartRate);
}
@NonNull
@Override
public String toString() {
return "ts: " + time.getTime() + ", " + super.toString() + ", heartRate: " + heartRate;
}
public ActivityPoint toActivityPoint() {
final ActivityPoint activityPoint = new ActivityPoint();
activityPoint.setTime(time);
@ -73,7 +81,7 @@ public class GpxTrackPoint extends GPSCoordinate {
private double latitude;
private double altitude;
private Date time;
private int heartRate;
private int heartRate = -1;
public Builder withLongitude(final double longitude) {
this.longitude = longitude;

View File

@ -33,6 +33,10 @@ public class GPXParserTest extends TestBase {
Assert.assertEquals(df.format(tp.getLatitude()), "44.15");
Assert.assertThat(df.format(tp.getAltitude()), anyOf(is("40"), is("46")));
}
Assert.assertEquals(
new GpxTrackPoint(-68.200293, 44.152462, 40, new Date(1546300800000L)),
trackPoints.get(0)
);
}
}
@ -45,13 +49,13 @@ public class GPXParserTest extends TestBase {
Assert.assertEquals(2, gpxFile.getTracks().get(0).getTrackSegments().size());
final List<GpxTrackPoint> segment1 = new ArrayList<GpxTrackPoint>() {{
add(new GpxTrackPoint(-8.2695876, -70.6666343, 790.0, new Date(1680969788000L)));
add(new GpxTrackPoint(-8.2653274, -70.6670617, 296.0, new Date(1680970639000L)));
add(new GpxTrackPoint(-8.2695876, -70.6666343, 790.0, new Date(1680969788000L), 123));
add(new GpxTrackPoint(-8.2653274, -70.6670617, 296.0, new Date(1680970639000L), 56));
}};
final List<GpxTrackPoint> segment2 = new ArrayList<GpxTrackPoint>() {{
add(new GpxTrackPoint(-8.2653274, -70.6670617, 205.0, new Date(1680971684000L)));
add(new GpxTrackPoint(-8.2695876, -70.6666343, 209.0, new Date(1680973017000L)));
add(new GpxTrackPoint(-8.2653274, -70.6670617, 205.0, new Date(1680971684000L), 85));
add(new GpxTrackPoint(-8.2695876, -70.6666343, 209.0, new Date(1680973017000L), 150));
}};
Assert.assertEquals(gpxFile.getTracks().get(0).getTrackSegments().get(0).getTrackPoints(), segment1);
@ -68,8 +72,8 @@ public class GPXParserTest extends TestBase {
Assert.assertEquals(1, gpxFile.getTracks().get(0).getTrackSegments().size());
final List<GpxTrackPoint> segment1 = new ArrayList<GpxTrackPoint>() {{
add(new GpxTrackPoint(-8.2695876, -70.6666343, 790.0, new Date(1680969788000L)));
add(new GpxTrackPoint(-8.2653274, -70.6670617, 296.0, new Date(1680970639000L)));
add(new GpxTrackPoint(-8.2695876, -70.6666343, 790.0, new Date(1680969788000L), 123));
add(new GpxTrackPoint(-8.2653274, -70.6670617, 296.0, new Date(1680970639000L), 56));
}};
Assert.assertEquals(gpxFile.getTracks().get(0).getTrackSegments().get(0).getTrackPoints(), segment1);