1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-12-25 01:55:50 +01:00

Garmin: Persist distance and calories

This commit is contained in:
José Rebelo 2024-09-21 19:14:35 +01:00
parent c700d49bf0
commit 9c67a30835
3 changed files with 84 additions and 3 deletions

View File

@ -46,7 +46,7 @@ public class GBDaoGenerator {
public static void main(String[] args) throws Exception {
final Schema schema = new Schema(79, MAIN_PACKAGE + ".entities");
final Schema schema = new Schema(80, MAIN_PACKAGE + ".entities");
Entity userAttributes = addUserAttributes(schema);
Entity user = addUserInfo(schema, userAttributes);
@ -753,6 +753,8 @@ public class GBDaoGenerator {
activitySample.addIntProperty(SAMPLE_STEPS).notNull().codeBeforeGetterAndSetter(OVERRIDE);
activitySample.addIntProperty(SAMPLE_RAW_KIND).notNull().codeBeforeGetterAndSetter(OVERRIDE);
addHeartRateProperties(activitySample);
activitySample.addIntProperty("distanceCm").notNull().codeBeforeGetterAndSetter(OVERRIDE);
activitySample.addIntProperty("activeCalories").notNull().codeBeforeGetterAndSetter(OVERRIDE);
return activitySample;
}

View File

@ -0,0 +1,44 @@
/* Copyright (C) 2024 José Rebelo
This file is part of Gadgetbridge.
Gadgetbridge is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Gadgetbridge is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. */
package nodomain.freeyourgadget.gadgetbridge.database.schema;
import android.database.sqlite.SQLiteDatabase;
import nodomain.freeyourgadget.gadgetbridge.database.DBHelper;
import nodomain.freeyourgadget.gadgetbridge.database.DBUpdateScript;
import nodomain.freeyourgadget.gadgetbridge.entities.GarminActivitySampleDao;
public class GadgetbridgeUpdate_80 implements DBUpdateScript {
@Override
public void upgradeSchema(final SQLiteDatabase db) {
if (!DBHelper.existsColumn(GarminActivitySampleDao.TABLENAME, GarminActivitySampleDao.Properties.DistanceCm.columnName, db)) {
final String statement = "ALTER TABLE " + GarminActivitySampleDao.TABLENAME + " ADD COLUMN \""
+ GarminActivitySampleDao.Properties.DistanceCm.columnName + "\" INTEGER NOT NULL DEFAULT -1";
db.execSQL(statement);
}
if (!DBHelper.existsColumn(GarminActivitySampleDao.TABLENAME, GarminActivitySampleDao.Properties.ActiveCalories.columnName, db)) {
final String statement = "ALTER TABLE " + GarminActivitySampleDao.TABLENAME + " ADD COLUMN \""
+ GarminActivitySampleDao.Properties.ActiveCalories.columnName + "\" INTEGER NOT NULL DEFAULT -1";
db.execSQL(statement);
}
}
@Override
public void downgradeSchema(final SQLiteDatabase db) {
}
}

View File

@ -356,9 +356,11 @@ public class FitImporter {
final List<GarminActivitySample> activitySamples = new ArrayList<>(activitySamplesPerTimestamp.size());
// Garmin reports the cumulative steps per activity, but not always, so we need to keep
// track of the number of steps for each activity, and set the sum of all on the sample
// Garmin reports the cumulative data per activity, but not always, so we need to keep
// track of the amounts for each activity, and set the sum of all on the sample
final Map<Integer, Long> stepsPerActivity = new HashMap<>();
final Map<Integer, Long> distancePerActivity = new HashMap<>();
final Map<Integer, Integer> caloriesPerActivity = new HashMap<>();
final int THRESHOLD_NOT_WORN = 10 * 60; // 10 min gap between samples = not-worn
int prevActivityKind = ActivityKind.UNKNOWN.getCode();
@ -374,6 +376,9 @@ public class FitImporter {
sample.setRawKind(ts - prevTs > THRESHOLD_NOT_WORN ? ActivityKind.NOT_WORN.getCode() : prevActivityKind);
sample.setRawIntensity(ActivitySample.NOT_MEASURED);
sample.setSteps(ActivitySample.NOT_MEASURED);
sample.setHeartRate(ActivitySample.NOT_MEASURED);
sample.setDistanceCm(ActivitySample.NOT_MEASURED);
sample.setActiveCalories(ActivitySample.NOT_MEASURED);
activitySamples.add(sample);
}
}
@ -386,8 +391,12 @@ public class FitImporter {
sample.setRawIntensity(ActivitySample.NOT_MEASURED);
sample.setSteps(ActivitySample.NOT_MEASURED);
sample.setHeartRate(ActivitySample.NOT_MEASURED);
sample.setDistanceCm(ActivitySample.NOT_MEASURED);
sample.setActiveCalories(ActivitySample.NOT_MEASURED);
boolean hasSteps = false;
boolean hasDistance = false;
boolean hasCalories = false;
for (final FitMonitoring record : Objects.requireNonNull(records)) {
final Integer activityType = record.getComputedActivityType().orElse(ActivitySample.NOT_MEASURED);
@ -402,6 +411,18 @@ public class FitImporter {
hasSteps = true;
}
final Long distance = record.getDistance();
if (distance != null) {
distancePerActivity.put(activityType, distance);
hasDistance = true;
}
final Integer calories = record.getActiveCalories();
if (calories != null) {
caloriesPerActivity.put(activityType, calories);
hasCalories = true;
}
final Integer intensity = record.getComputedIntensity();
if (intensity != null) {
sample.setRawIntensity(intensity);
@ -414,6 +435,20 @@ public class FitImporter {
}
sample.setSteps(sumSteps);
}
if (hasDistance) {
int sumDistance = 0;
for (final Long distance : distancePerActivity.values()) {
sumDistance += distance;
}
sample.setDistanceCm(sumDistance);
}
if (hasCalories) {
int sumCalories = 0;
for (final Integer calories : caloriesPerActivity.values()) {
sumCalories += calories;
}
sample.setActiveCalories(sumCalories);
}
activitySamples.add(sample);