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

Add DefaultRestingMetabolicRateProvider

This commit is contained in:
José Rebelo 2024-11-16 23:47:05 +00:00
parent 37ea9bc571
commit df138e9c79
2 changed files with 146 additions and 1 deletions

View File

@ -283,7 +283,7 @@ public abstract class AbstractDeviceCoordinator implements DeviceCoordinator {
@Override
public TimeSampleProvider<? extends RestingMetabolicRateSample> getRestingMetabolicRateProvider(final GBDevice device, final DaoSession session) {
return null; // FIXME return new DefaultRestingMetabolicRateProvider(device, session);
return new DefaultRestingMetabolicRateProvider(device, session);
}
@Override

View File

@ -0,0 +1,145 @@
/* 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.devices;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.util.Collections;
import java.util.List;
import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.model.ActivityUser;
import nodomain.freeyourgadget.gadgetbridge.model.RestingMetabolicRateSample;
/**
* Provides a default resting metabolic rate, for devices that do not provide it. Currently it uses the
* Mifflin St Jeor equation. TODO: use the user data at that timestamp, and make the algorithm configurable.
*/
public class DefaultRestingMetabolicRateProvider implements TimeSampleProvider<RestingMetabolicRateSample> {
private final DaoSession mSession;
private final GBDevice mDevice;
protected DefaultRestingMetabolicRateProvider(final GBDevice device, final DaoSession session) {
mDevice = device;
mSession = session;
}
public GBDevice getDevice() {
return mDevice;
}
public DaoSession getSession() {
return mSession;
}
@NonNull
@Override
public List<RestingMetabolicRateSample> getAllSamples(final long timestampFrom, final long timestampTo) {
return Collections.singletonList(getLatestSample());
}
@Override
public void addSample(final RestingMetabolicRateSample timeSample) {
throw new UnsupportedOperationException("addSample not supported");
}
@Override
public void addSamples(final List<RestingMetabolicRateSample> timeSamples) {
throw new UnsupportedOperationException("addSamples not supported");
}
@Override
public RestingMetabolicRateSample createSample() {
throw new UnsupportedOperationException("createSample not supported");
}
@Nullable
@Override
public RestingMetabolicRateSample getLatestSample() {
return new DefaultRestingMetabolicRateSample();
}
@Nullable
@Override
public RestingMetabolicRateSample getFirstSample() {
return getLatestSample();
}
private static class DefaultRestingMetabolicRateSample extends RestingMetabolicRateSample {
private long timestamp;
private final int rate;
private DefaultRestingMetabolicRateSample() {
this.timestamp = System.currentTimeMillis();
ActivityUser activityUser = new ActivityUser();
final int weightKg = activityUser.getWeightKg();
final int heightCm = activityUser.getHeightCm();
final int age = activityUser.getAge();
final int s;
switch (activityUser.getGender()) {
case ActivityUser.GENDER_FEMALE:
s = -161;
break;
case ActivityUser.GENDER_MALE:
s = 5;
break;
default:
s = (5 - 161) / 2; // average it?
break;
}
// Mifflin St Jeor equation
this.rate = (int) Math.round(10.0d * weightKg + 6.25 * heightCm - 5d * age + s);
}
@Override
public int getRestingMetabolicRate() {
return rate;
}
@Override
public void setTimestamp(final long timestamp) {
this.timestamp = timestamp;
}
@Override
public long getUserId() {
return 0;
}
@Override
public void setUserId(final long userId) {
}
@Override
public long getDeviceId() {
return 0;
}
@Override
public void setDeviceId(final long deviceId) {
}
@Override
public long getTimestamp() {
return timestamp;
}
}
}