1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-09 03:37:03 +01:00

Test Device: Add HRV and body energy

This commit is contained in:
José Rebelo 2024-08-05 21:10:01 +01:00
parent 2ed6247658
commit 7efeb14ca4
5 changed files with 351 additions and 1 deletions

View File

@ -48,6 +48,9 @@ import nodomain.freeyourgadget.gadgetbridge.devices.SampleProvider;
import nodomain.freeyourgadget.gadgetbridge.devices.TimeSampleProvider;
import nodomain.freeyourgadget.gadgetbridge.devices.roidmi.RoidmiConst;
import nodomain.freeyourgadget.gadgetbridge.devices.test.activity.TestActivitySummaryParser;
import nodomain.freeyourgadget.gadgetbridge.devices.test.samples.TestBodyEnergySampleProvider;
import nodomain.freeyourgadget.gadgetbridge.devices.test.samples.TestHrvSummarySampleProvider;
import nodomain.freeyourgadget.gadgetbridge.devices.test.samples.TestHrvValueSampleProvider;
import nodomain.freeyourgadget.gadgetbridge.devices.test.samples.TestPaiSampleProvider;
import nodomain.freeyourgadget.gadgetbridge.devices.test.samples.TestSpo2SampleProvider;
import nodomain.freeyourgadget.gadgetbridge.devices.test.samples.TestStressSampleProvider;
@ -61,8 +64,11 @@ import nodomain.freeyourgadget.gadgetbridge.model.AbstractNotificationPattern;
import nodomain.freeyourgadget.gadgetbridge.model.ActivitySample;
import nodomain.freeyourgadget.gadgetbridge.model.ActivitySummaryParser;
import nodomain.freeyourgadget.gadgetbridge.model.BatteryConfig;
import nodomain.freeyourgadget.gadgetbridge.model.BodyEnergySample;
import nodomain.freeyourgadget.gadgetbridge.model.DeviceType;
import nodomain.freeyourgadget.gadgetbridge.model.HeartRateSample;
import nodomain.freeyourgadget.gadgetbridge.model.HrvSummarySample;
import nodomain.freeyourgadget.gadgetbridge.model.HrvValueSample;
import nodomain.freeyourgadget.gadgetbridge.model.PaiSample;
import nodomain.freeyourgadget.gadgetbridge.model.SleepRespiratoryRateSample;
import nodomain.freeyourgadget.gadgetbridge.model.Spo2Sample;
@ -113,6 +119,22 @@ public class TestDeviceCoordinator extends AbstractDeviceCoordinator {
return supportsStressMeasurement() ? new TestStressSampleProvider() : super.getStressSampleProvider(device, session);
}
@Override
public TimeSampleProvider<? extends BodyEnergySample> getBodyEnergySampleProvider(final GBDevice device, final DaoSession session) {
return supportsBodyEnergy() ? new TestBodyEnergySampleProvider() : super.getBodyEnergySampleProvider(device ,session);
}
@Override
public TimeSampleProvider<? extends HrvSummarySample> getHrvSummarySampleProvider(final GBDevice device, final DaoSession session) {
return supportsBodyEnergy() ? new TestHrvSummarySampleProvider() : super.getHrvSummarySampleProvider(device ,session);
}
@Override
public TimeSampleProvider<? extends HrvValueSample> getHrvValueSampleProvider(final GBDevice device, final DaoSession session) {
return supportsBodyEnergy() ? new TestHrvValueSampleProvider() : super.getHrvValueSampleProvider(device ,session);
}
@Override
public int[] getStressRanges() {
// TODO getStressRanges
@ -121,7 +143,7 @@ public class TestDeviceCoordinator extends AbstractDeviceCoordinator {
@Override
public TimeSampleProvider<? extends TemperatureSample> getTemperatureSampleProvider(final GBDevice device, final DaoSession session) {
return new TestTemperatureSampleProvider(); // TODO supportsTemperature
return supportsTemperatureMeasurement() ? new TestTemperatureSampleProvider() : super.getTemperatureSampleProvider(device, session);
}
@Override
@ -316,6 +338,16 @@ public class TestDeviceCoordinator extends AbstractDeviceCoordinator {
return supports(getTestDevice(), TestFeature.STRESS_MEASUREMENT);
}
@Override
public boolean supportsBodyEnergy() {
return supports(getTestDevice(), TestFeature.BODY_ENERGY);
}
@Override
public boolean supportsHrvMeasurement() {
return supports(getTestDevice(), TestFeature.HRV_MEASUREMENT);
}
@Override
public boolean supportsSpo2(GBDevice device) {
return supports(getTestDevice(), TestFeature.SPO2);

View File

@ -64,6 +64,8 @@ public enum TestFeature {
SPO2,
STEP_COUNTER,
STRESS_MEASUREMENT,
BODY_ENERGY,
HRV_MEASUREMENT,
TEMPERATURE_MEASUREMENT,
UNICODE_EMOJIS,
WATCHFACE_MANAGEMENT,

View File

@ -0,0 +1,87 @@
/* 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.test.samples;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
import nodomain.freeyourgadget.gadgetbridge.devices.TimeSampleProvider;
import nodomain.freeyourgadget.gadgetbridge.devices.test.TestDeviceRand;
import nodomain.freeyourgadget.gadgetbridge.model.BodyEnergySample;
public class TestBodyEnergySampleProvider implements TimeSampleProvider<BodyEnergySample> {
@NonNull
@Override
public List<BodyEnergySample> getAllSamples(final long timestampFrom, final long timestampTo) {
final List<BodyEnergySample> samples = new ArrayList<>();
for (long ts = timestampFrom; ts < timestampTo; ts += 15 * 60 * 1000L) {
samples.add(new TestBodyEnergySample(ts));
}
return samples;
}
@Override
public void addSample(final BodyEnergySample timeSample) {
throw new UnsupportedOperationException("read-only sample provider");
}
@Override
public void addSamples(final List<BodyEnergySample> timeSamples) {
throw new UnsupportedOperationException("read-only sample provider");
}
@Override
public BodyEnergySample createSample() {
throw new UnsupportedOperationException("read-only sample provider");
}
@Nullable
@Override
public BodyEnergySample getLatestSample() {
final long ts = System.currentTimeMillis();
return new TestBodyEnergySample(ts - TestDeviceRand.randLong(ts, 10 * 1000L, 2 * 60 * 60 * 1000L));
}
@Nullable
@Override
public BodyEnergySample getFirstSample() {
return new TestBodyEnergySample(TestDeviceRand.BASE_TIMESTAMP);
}
protected static class TestBodyEnergySample implements BodyEnergySample {
private final long timestamp;
public TestBodyEnergySample(final long timestamp) {
this.timestamp = timestamp;
}
@Override
public long getTimestamp() {
return timestamp;
}
@Override
public int getEnergy() {
return (int) Math.round(45 * Math.sin(timestamp / (86400000 / (2 * Math.PI))) + 55);
}
}
}

View File

@ -0,0 +1,131 @@
/* 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.test.samples;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
import nodomain.freeyourgadget.gadgetbridge.devices.TimeSampleProvider;
import nodomain.freeyourgadget.gadgetbridge.devices.test.TestDeviceRand;
import nodomain.freeyourgadget.gadgetbridge.model.HrvSummarySample;
public class TestHrvSummarySampleProvider implements TimeSampleProvider<HrvSummarySample> {
@NonNull
@Override
public List<HrvSummarySample> getAllSamples(final long timestampFrom, final long timestampTo) {
final List<HrvSummarySample> samples = new ArrayList<>();
for (long ts = timestampFrom; ts < timestampTo; ts += 24 * 60 * 60 * 1000L) {
samples.add(new TestHrvSummarySample(ts));
}
return samples;
}
@Override
public void addSample(final HrvSummarySample timeSample) {
throw new UnsupportedOperationException("read-only sample provider");
}
@Override
public void addSamples(final List<HrvSummarySample> timeSamples) {
throw new UnsupportedOperationException("read-only sample provider");
}
@Override
public HrvSummarySample createSample() {
throw new UnsupportedOperationException("read-only sample provider");
}
@Nullable
@Override
public HrvSummarySample getLatestSample() {
final long ts = System.currentTimeMillis();
return new TestHrvSummarySample(ts - TestDeviceRand.randLong(ts, 10 * 1000L, 2 * 60 * 60 * 1000L));
}
@Nullable
@Override
public HrvSummarySample getFirstSample() {
return new TestHrvSummarySample(TestDeviceRand.BASE_TIMESTAMP);
}
protected static class TestHrvSummarySample implements HrvSummarySample {
private final long timestamp;
private final int weeklyAverage;
public TestHrvSummarySample(final long timestamp) {
this(timestamp, TestDeviceRand.randInt(timestamp, 30, 95));
}
public TestHrvSummarySample(final long timestamp, final int weeklyAverage) {
this.timestamp = timestamp;
this.weeklyAverage = weeklyAverage;
}
@Override
public long getTimestamp() {
return timestamp;
}
@Override
public Integer getWeeklyAverage() {
return weeklyAverage;
}
@Override
public Integer getLastNightAverage() {
return TestDeviceRand.randInt(timestamp, 55, 90);
}
@Override
public Integer getLastNight5MinHigh() {
return TestDeviceRand.randInt(timestamp, 90, 110);
}
@Override
public Integer getBaselineLowUpper() {
return 50;
}
@Override
public Integer getBaselineBalancedLower() {
return 65;
}
@Override
public Integer getBaselineBalancedUpper() {
return 80;
}
@Override
public Status getStatus() {
if (getWeeklyAverage() > getBaselineBalancedUpper()) {
return Status.UNBALANCED;
} else if (getWeeklyAverage() < getBaselineLowUpper()) {
return Status.POOR;
} else if (getWeeklyAverage() < getBaselineBalancedLower()) {
return Status.LOW;
}
return Status.BALANCED;
}
}
}

View File

@ -0,0 +1,98 @@
/* 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.test.samples;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
import nodomain.freeyourgadget.gadgetbridge.devices.TimeSampleProvider;
import nodomain.freeyourgadget.gadgetbridge.devices.test.TestDeviceRand;
import nodomain.freeyourgadget.gadgetbridge.model.HrvValueSample;
public class TestHrvValueSampleProvider implements TimeSampleProvider<HrvValueSample> {
@NonNull
@Override
public List<HrvValueSample> getAllSamples(final long timestampFrom, final long timestampTo) {
final List<HrvValueSample> samples = new ArrayList<>();
int hrv = TestDeviceRand.randInt(timestampFrom, 50, 95);
for (long ts = timestampFrom; ts < timestampTo; ts += 5 * 60 * 1000L) {
samples.add(new TestHrvValueSample(ts, hrv));
hrv += TestDeviceRand.randInt(ts, (10 - hrv) / 10, (90 - hrv) / 10);
}
return samples;
}
@Override
public void addSample(final HrvValueSample timeSample) {
throw new UnsupportedOperationException("read-only sample provider");
}
@Override
public void addSamples(final List<HrvValueSample> timeSamples) {
throw new UnsupportedOperationException("read-only sample provider");
}
@Override
public HrvValueSample createSample() {
throw new UnsupportedOperationException("read-only sample provider");
}
@Nullable
@Override
public HrvValueSample getLatestSample() {
final long ts = System.currentTimeMillis();
return new TestHrvValueSample(
ts - TestDeviceRand.randLong(ts, 10 * 1000L, 2 * 60 * 60 * 1000L),
TestDeviceRand.randInt(ts, 50, 95)
);
}
@Nullable
@Override
public HrvValueSample getFirstSample() {
return new TestHrvValueSample(
TestDeviceRand.BASE_TIMESTAMP,
TestDeviceRand.randInt(TestDeviceRand.BASE_TIMESTAMP, 50, 95)
);
}
protected static class TestHrvValueSample implements HrvValueSample {
private final long timestamp;
private final int hrv;
public TestHrvValueSample(final long timestamp, final int hrv) {
this.timestamp = timestamp;
this.hrv = hrv;
}
@Override
public long getTimestamp() {
return timestamp;
}
@Override
public int getValue() {
return hrv;
}
}
}