Search in sources :

Example 1 with AggregateResponse

use of com.google.api.services.fitness.model.AggregateResponse in project BachelorPraktikum by lucasbuschlinger.

the class GoogleFitness method fetchActivitiesPerDay.

/**
 * Fetches all activities at a specific day.
 * @param day - a date as unix timestamp
 */
private void fetchActivitiesPerDay(final long day) {
    long[] startEnd = getStartEndDay(day);
    AggregateBy aggregate = new AggregateBy();
    aggregate.setDataTypeName("com.google.activity.segment");
    AggregateRequest agg = new AggregateRequest();
    agg.setStartTimeMillis(startEnd[0]);
    agg.setEndTimeMillis(startEnd[1]);
    agg.setAggregateBy(Arrays.asList(aggregate));
    try {
        Fitness.Users.Dataset.Aggregate request = fitnessService.users().dataset().aggregate("me", agg);
        AggregateResponse rep = request.execute();
        List<DataPoint> activitiesByDataSource = rep.getBucket().get(0).getDataset().get(0).getPoint();
        List<Activity> activities = new ArrayList<>();
        for (DataPoint dp : activitiesByDataSource) {
            activities.add(new Activity(dp.getStartTimeNanos() / NANO_TO_MILLISECONDS_DIVISOR, dp.getEndTimeNanos() / NANO_TO_MILLISECONDS_DIVISOR, dp.getValue().get(0).getIntVal()));
        }
        for (int i = 0; i < activities.size() - 1; i++) {
            Activity currentActivity = activities.get(i);
            Activity nextActivity = activities.get(i + 1);
            if (currentActivity.getActivityId() == nextActivity.getActivityId() && currentActivity.getEndTime() - nextActivity.getStartTime() <= UNIX_TIME_FIVE_MINUTES) {
                currentActivity.setEndTime(nextActivity.getEndTime());
                activities.remove(i + 1);
            }
        }
        LocationHistory.getInstance().addActivities(startEnd[0], activities);
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : AggregateBy(com.google.api.services.fitness.model.AggregateBy) AggregateRequest(com.google.api.services.fitness.model.AggregateRequest) AggregateResponse(com.google.api.services.fitness.model.AggregateResponse) ArrayList(java.util.ArrayList) Activity(de.opendiabetes.vault.plugin.importer.googlecrawler.models.Activity) IOException(java.io.IOException) DataPoint(com.google.api.services.fitness.model.DataPoint) DataPoint(com.google.api.services.fitness.model.DataPoint) Fitness(com.google.api.services.fitness.Fitness)

Example 2 with AggregateResponse

use of com.google.api.services.fitness.model.AggregateResponse in project BachelorPraktikum by lucasbuschlinger.

the class GoogleFitness method fetchHeartRatePerDay.

/**
 * Fetches the heart rate at a specific day.
 * @param day - a date as unix timestamp
 */
private void fetchHeartRatePerDay(final long day) {
    long[] startEnd = getStartEndDay(day);
    AggregateBy aggregate = new AggregateBy();
    aggregate.setDataTypeName("com.google.heart_rate.bpm");
    AggregateRequest aggregateRequest = new AggregateRequest();
    aggregateRequest.setStartTimeMillis(startEnd[0]);
    aggregateRequest.setEndTimeMillis(startEnd[1]);
    aggregateRequest.setAggregateBy(Arrays.asList(aggregate));
    try {
        Fitness.Users.Dataset.Aggregate request = fitnessService.users().dataset().aggregate("me", aggregateRequest);
        AggregateResponse rep = request.execute();
        if (rep.getBucket().get(0).getDataset().get(0).getPoint().size() != 0) {
            List<DataPoint> activitiesByDataSource = rep.getBucket().get(0).getDataset().get(0).getPoint();
            List<HeartRate> heartRates = new ArrayList<>();
            for (DataPoint dp : activitiesByDataSource) {
                heartRates.add(new HeartRate(dp.getStartTimeNanos() / NANO_TO_MILLISECONDS_DIVISOR, dp.getValue().get(0).getIntVal()));
            }
            LocationHistory.getInstance().addHeartRates(startEnd[0], heartRates);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : HeartRate(de.opendiabetes.vault.plugin.importer.googlecrawler.models.HeartRate) AggregateBy(com.google.api.services.fitness.model.AggregateBy) AggregateRequest(com.google.api.services.fitness.model.AggregateRequest) AggregateResponse(com.google.api.services.fitness.model.AggregateResponse) ArrayList(java.util.ArrayList) IOException(java.io.IOException) DataPoint(com.google.api.services.fitness.model.DataPoint) Fitness(com.google.api.services.fitness.Fitness)

Example 3 with AggregateResponse

use of com.google.api.services.fitness.model.AggregateResponse in project BachelorPraktikum by lucasbuschlinger.

the class GoogleFitness method fetchLocationsPerDay.

/**
 * Fetches all locations at a specific day.
 * @param day - a date as unix timestamp
 */
private void fetchLocationsPerDay(final long day) {
    long[] startEnd = getStartEndDay(day);
    AggregateBy aggregate = new AggregateBy();
    aggregate.setDataTypeName("com.google.location.sample");
    AggregateRequest agg = new AggregateRequest();
    agg.setStartTimeMillis(startEnd[0]);
    agg.setEndTimeMillis(startEnd[1]);
    agg.setAggregateBy(Arrays.asList(aggregate));
    try {
        Fitness.Users.Dataset.Aggregate request = fitnessService.users().dataset().aggregate("me", agg);
        AggregateResponse rep = request.execute();
        List<DataPoint> activitiesByDataSource = rep.getBucket().get(0).getDataset().get(0).getPoint();
        List<Coordinate> locations = new ArrayList<>();
        for (DataPoint dp : activitiesByDataSource) {
            Coordinate coord = new Coordinate(dp.getStartTimeNanos() / NANO_TO_MILLISECONDS_DIVISOR, dp.getValue().get(1).getFpVal(), dp.getValue().get(0).getFpVal(), (int) Math.ceil(dp.getValue().get(2).getFpVal()));
            if (dp.getValue().size() > 3) {
                coord.setAltitude(dp.getValue().get(3).getFpVal().intValue());
            }
            locations.add(coord);
        }
        LocationHistory.getInstance().addLocations(startEnd[0], locations);
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : AggregateBy(com.google.api.services.fitness.model.AggregateBy) AggregateRequest(com.google.api.services.fitness.model.AggregateRequest) AggregateResponse(com.google.api.services.fitness.model.AggregateResponse) ArrayList(java.util.ArrayList) IOException(java.io.IOException) DataPoint(com.google.api.services.fitness.model.DataPoint) Coordinate(de.opendiabetes.vault.plugin.importer.googlecrawler.models.Coordinate) Fitness(com.google.api.services.fitness.Fitness)

Aggregations

Fitness (com.google.api.services.fitness.Fitness)3 AggregateBy (com.google.api.services.fitness.model.AggregateBy)3 AggregateRequest (com.google.api.services.fitness.model.AggregateRequest)3 AggregateResponse (com.google.api.services.fitness.model.AggregateResponse)3 DataPoint (com.google.api.services.fitness.model.DataPoint)3 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 Activity (de.opendiabetes.vault.plugin.importer.googlecrawler.models.Activity)1 Coordinate (de.opendiabetes.vault.plugin.importer.googlecrawler.models.Coordinate)1 HeartRate (de.opendiabetes.vault.plugin.importer.googlecrawler.models.HeartRate)1