Search in sources :

Example 1 with HeartRate

use of de.opendiabetes.vault.plugin.importer.googlecrawler.models.HeartRate 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)

Aggregations

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