Search in sources :

Example 6 with ActivityPoint

use of nodomain.freeyourgadget.gadgetbridge.model.ActivityPoint in project Gadgetbridge by Freeyourgadget.

the class GPXExporterTest method readActivityPoints.

private List<ActivityPoint> readActivityPoints(String resourcePath) throws IOException, ParseException {
    final List<ActivityPoint> points = new ArrayList<>();
    try (final InputStream inputStream = getClass().getResourceAsStream(resourcePath)) {
        try (final BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
            String nextLine = reader.readLine();
            while (nextLine != null) {
                final String[] pieces = nextLine.split("\\s+");
                final ActivityPoint point = new ActivityPoint();
                point.setLocation(new GPSCoordinate(Double.parseDouble(pieces[0]), Double.parseDouble(pieces[1]), Double.parseDouble(pieces[2])));
                final int dateIndex;
                if (pieces.length == 5) {
                    point.setHeartRate(Integer.parseInt(pieces[3]));
                    dateIndex = 4;
                } else {
                    dateIndex = 3;
                }
                // Not sure about this parser but seemed safe to use
                point.setTime(ISO8601Utils.parse(pieces[dateIndex], new ParsePosition(0)));
                points.add(point);
                nextLine = reader.readLine();
            }
        }
    }
    return points;
}
Also used : InputStreamReader(java.io.InputStreamReader) ActivityPoint(nodomain.freeyourgadget.gadgetbridge.model.ActivityPoint) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) BufferedReader(java.io.BufferedReader) ActivityPoint(nodomain.freeyourgadget.gadgetbridge.model.ActivityPoint) GPSCoordinate(nodomain.freeyourgadget.gadgetbridge.model.GPSCoordinate) ParsePosition(java.text.ParsePosition)

Example 7 with ActivityPoint

use of nodomain.freeyourgadget.gadgetbridge.model.ActivityPoint in project Gadgetbridge by Freeyourgadget.

the class HuamiActivityDetailsParser method consumeHeartRate.

private int consumeHeartRate(byte[] bytes, int offset, long timeOffsetSeconds) {
    int v1 = BLETypeConversions.toUint16(bytes[offset]);
    int v2 = BLETypeConversions.toUint16(bytes[offset + 1]);
    int v3 = BLETypeConversions.toUint16(bytes[offset + 2]);
    int v4 = BLETypeConversions.toUint16(bytes[offset + 3]);
    int v5 = BLETypeConversions.toUint16(bytes[offset + 4]);
    int v6 = BLETypeConversions.toUint16(bytes[offset + 5]);
    if (v2 == 0 && v3 == 0 && v4 == 0 && v5 == 0 && v6 == 0) {
        // new version
        LOG.info("detected heart rate in 'new' version format");
        ActivityPoint ap = getActivityPointFor(timeOffsetSeconds);
        ap.setHeartRate(v1);
        add(ap);
    } else {
        ActivityPoint ap = getActivityPointFor(v1);
        ap.setHeartRate(v2);
        add(ap);
        ap = getActivityPointFor(v3);
        ap.setHeartRate(v4);
        add(ap);
        ap = getActivityPointFor(v5);
        ap.setHeartRate(v6);
        add(ap);
    }
    return 6;
}
Also used : ActivityPoint(nodomain.freeyourgadget.gadgetbridge.model.ActivityPoint) ActivityPoint(nodomain.freeyourgadget.gadgetbridge.model.ActivityPoint)

Example 8 with ActivityPoint

use of nodomain.freeyourgadget.gadgetbridge.model.ActivityPoint in project Gadgetbridge by Freeyourgadget.

the class HuamiActivityDetailsParser method fixupMissingTimestamps.

private void fixupMissingTimestamps(ActivityTrack activityTrack) {
    try {
        int pointer = 0;
        List<ActivityPoint> activityPointList = activityTrack.getTrackPoints();
        Date gpsStartTime = null;
        List<ActivityPoint> entriesToFixUp = new ArrayList<>();
        while (pointer < activityPointList.size() - 1) {
            ActivityPoint activityPoint = activityPointList.get(pointer);
            if (activityPoint.getLocation() == null) {
                pointer++;
                continue;
            }
            if (activityPoint.getTime().equals(activityPointList.get(pointer + 1).getTime())) {
                entriesToFixUp.add(activityPoint);
            } else {
                // found the first activity point with a proper timestamp
                entriesToFixUp.add(activityPoint);
                gpsStartTime = activityPointList.get(pointer + 1).getTime();
                break;
            }
            pointer++;
        }
        if (gpsStartTime != null) {
            // now adjust those entries without a timestamp
            long differenceInSec = TimeUnit.SECONDS.convert(Math.abs(gpsStartTime.getTime() - baseDate.getTime()), TimeUnit.MILLISECONDS);
            double multiplier = (double) differenceInSec / (double) (entriesToFixUp.size());
            for (int j = 0; j < entriesToFixUp.size(); j++) {
                long timeOffsetSeconds = Math.round(j * multiplier);
                entriesToFixUp.get(j).setTime(makeAbsolute(timeOffsetSeconds));
            }
        }
    } catch (Exception ex) {
        LOG.warn("Error cleaning activity details", ex);
    }
}
Also used : ActivityPoint(nodomain.freeyourgadget.gadgetbridge.model.ActivityPoint) ArrayList(java.util.ArrayList) ActivityPoint(nodomain.freeyourgadget.gadgetbridge.model.ActivityPoint) Date(java.util.Date) GBException(nodomain.freeyourgadget.gadgetbridge.GBException)

Example 9 with ActivityPoint

use of nodomain.freeyourgadget.gadgetbridge.model.ActivityPoint in project Gadgetbridge by Freeyourgadget.

the class GPXExporterTest method createTestTrack.

private ActivityTrack createTestTrack(List<ActivityPoint> points) {
    final User user = new User();
    user.setName("Test User");
    Device device = new Device();
    device.setName("Test Device");
    final ActivityTrack track = new ActivityTrack();
    track.setName("Test Track");
    track.setBaseTime(new Date());
    track.setUser(user);
    track.setDevice(device);
    for (final ActivityPoint point : points) {
        track.addTrackPoint(point);
    }
    return track;
}
Also used : User(nodomain.freeyourgadget.gadgetbridge.entities.User) Device(nodomain.freeyourgadget.gadgetbridge.entities.Device) ActivityPoint(nodomain.freeyourgadget.gadgetbridge.model.ActivityPoint) ActivityTrack(nodomain.freeyourgadget.gadgetbridge.model.ActivityTrack) Date(java.util.Date)

Example 10 with ActivityPoint

use of nodomain.freeyourgadget.gadgetbridge.model.ActivityPoint in project Gadgetbridge by Freeyourgadget.

the class GPXExporterTest method shouldCreateValidGpxFromSimulatedDataWithHeartrate.

@Test
public void shouldCreateValidGpxFromSimulatedDataWithHeartrate() throws IOException, ParseException, GPXTrackEmptyException, ParserConfigurationException, SAXException {
    final List<ActivityPoint> points = readActivityPoints("/GPXExporterTest-SampleTracksHR.csv");
    final GPXExporter gpxExporter = new GPXExporter();
    gpxExporter.setCreator("Gadgetbridge Test");
    final ActivityTrack track = createTestTrack(points);
    final File tempFile = File.createTempFile("gpx-exporter-test-track", ".gpx");
    tempFile.deleteOnExit();
    gpxExporter.performExport(track, tempFile);
    validateGpxFile(tempFile);
}
Also used : ActivityPoint(nodomain.freeyourgadget.gadgetbridge.model.ActivityPoint) ActivityTrack(nodomain.freeyourgadget.gadgetbridge.model.ActivityTrack) File(java.io.File) Test(org.junit.Test)

Aggregations

ActivityPoint (nodomain.freeyourgadget.gadgetbridge.model.ActivityPoint)12 ActivityTrack (nodomain.freeyourgadget.gadgetbridge.model.ActivityTrack)5 Test (org.junit.Test)4 File (java.io.File)3 InputStream (java.io.InputStream)3 Date (java.util.Date)3 ArrayList (java.util.ArrayList)2 BipActivitySummary (nodomain.freeyourgadget.gadgetbridge.devices.amazfitbip.BipActivitySummary)2 GPSCoordinate (nodomain.freeyourgadget.gadgetbridge.model.GPSCoordinate)2 HuamiActivityDetailsParser (nodomain.freeyourgadget.gadgetbridge.service.devices.huami.HuamiActivityDetailsParser)2 Nullable (androidx.annotation.Nullable)1 BufferedReader (java.io.BufferedReader)1 InputStreamReader (java.io.InputStreamReader)1 ParsePosition (java.text.ParsePosition)1 GBException (nodomain.freeyourgadget.gadgetbridge.GBException)1 HeartRateUtils (nodomain.freeyourgadget.gadgetbridge.activities.HeartRateUtils)1 Device (nodomain.freeyourgadget.gadgetbridge.entities.Device)1 User (nodomain.freeyourgadget.gadgetbridge.entities.User)1 GPXExporter (nodomain.freeyourgadget.gadgetbridge.export.GPXExporter)1