mirror of
https://codeberg.org/Freeyourgadget/Gadgetbridge
synced 2025-01-13 11:17:33 +01:00
Simplify HR calculation
This commit is contained in:
parent
db7280b359
commit
447e9110db
@ -27,8 +27,8 @@ import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
|||||||
import nodomain.freeyourgadget.gadgetbridge.activities.HeartRateUtils;
|
import nodomain.freeyourgadget.gadgetbridge.activities.HeartRateUtils;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.model.ActivityKind;
|
import nodomain.freeyourgadget.gadgetbridge.model.ActivityKind;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.model.ActivitySample;
|
import nodomain.freeyourgadget.gadgetbridge.model.ActivitySample;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.model.ActivityUser;
|
|
||||||
import nodomain.freeyourgadget.gadgetbridge.model.ActivitySession;
|
import nodomain.freeyourgadget.gadgetbridge.model.ActivitySession;
|
||||||
|
import nodomain.freeyourgadget.gadgetbridge.model.ActivityUser;
|
||||||
|
|
||||||
public class StepAnalysis {
|
public class StepAnalysis {
|
||||||
protected static final Logger LOG = LoggerFactory.getLogger(StepAnalysis.class);
|
protected static final Logger LOG = LoggerFactory.getLogger(StepAnalysis.class);
|
||||||
@ -60,11 +60,8 @@ public class StepAnalysis {
|
|||||||
int durationSinceLastActiveStep = 0;
|
int durationSinceLastActiveStep = 0;
|
||||||
int activityKind;
|
int activityKind;
|
||||||
|
|
||||||
int heartRateForAverage = 0;
|
List<Integer> heartRateSum = new ArrayList<>();
|
||||||
int heartRateToAdd = 0;
|
List<Integer> heartRateBetweenActivePeriodsSum = new ArrayList<>();
|
||||||
int heartRateBetweenActivePeriods = 0;
|
|
||||||
int activeHrSamplesForAverage = 0;
|
|
||||||
int activeHrSamplesToAdd = 0;
|
|
||||||
|
|
||||||
float activeIntensity = 0;
|
float activeIntensity = 0;
|
||||||
float intensityBetweenActivePeriods = 0;
|
float intensityBetweenActivePeriods = 0;
|
||||||
@ -78,41 +75,41 @@ public class StepAnalysis {
|
|||||||
|
|
||||||
if (sample.getKind() != ActivityKind.TYPE_SLEEP //anything but sleep counts
|
if (sample.getKind() != ActivityKind.TYPE_SLEEP //anything but sleep counts
|
||||||
&& !(sample instanceof TrailingActivitySample)) { //trailing samples have wrong date and make trailing activity have 0 duration
|
&& !(sample instanceof TrailingActivitySample)) { //trailing samples have wrong date and make trailing activity have 0 duration
|
||||||
if (heartRateUtilsInstance.isValidHeartRateValue(sample.getHeartRate())) {
|
|
||||||
heartRateToAdd = sample.getHeartRate();
|
|
||||||
activeHrSamplesToAdd = 1;
|
|
||||||
} else {
|
|
||||||
heartRateToAdd = 0;
|
|
||||||
activeHrSamplesToAdd = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (sessionStart == null) {
|
if (sessionStart == null) {
|
||||||
sessionStart = getDateFromSample(sample);
|
sessionStart = getDateFromSample(sample);
|
||||||
activeSteps = sample.getSteps();
|
activeSteps = sample.getSteps();
|
||||||
activeIntensity = sample.getIntensity();
|
activeIntensity = sample.getIntensity();
|
||||||
heartRateForAverage = heartRateToAdd;
|
heartRateSum = new ArrayList<>();
|
||||||
activeHrSamplesForAverage = activeHrSamplesToAdd;
|
if (heartRateUtilsInstance.isValidHeartRateValue(sample.getHeartRate())) {
|
||||||
|
heartRateSum.add(sample.getHeartRate());
|
||||||
|
}
|
||||||
durationSinceLastActiveStep = 0;
|
durationSinceLastActiveStep = 0;
|
||||||
stepsBetweenActivePeriods = 0;
|
stepsBetweenActivePeriods = 0;
|
||||||
heartRateBetweenActivePeriods = 0;
|
heartRateBetweenActivePeriodsSum = new ArrayList<>();
|
||||||
previousSample = null;
|
previousSample = null;
|
||||||
}
|
}
|
||||||
if (previousSample != null) {
|
if (previousSample != null) {
|
||||||
int durationSinceLastSample = sample.getTimestamp() - previousSample.getTimestamp();
|
int durationSinceLastSample = sample.getTimestamp() - previousSample.getTimestamp();
|
||||||
activeHrSamplesForAverage += activeHrSamplesToAdd;
|
|
||||||
if (sample.getSteps() > MIN_STEPS_PER_MINUTE || //either some steps
|
if (sample.getSteps() > MIN_STEPS_PER_MINUTE || //either some steps
|
||||||
(sample.getIntensity() > MIN_SESSION_INTENSITY && sample.getSteps() > 0)) { //or some intensity plus at least one step
|
(sample.getIntensity() > MIN_SESSION_INTENSITY && sample.getSteps() > 0)) { //or some intensity plus at least one step
|
||||||
activeSteps += sample.getSteps() + stepsBetweenActivePeriods;
|
activeSteps += sample.getSteps() + stepsBetweenActivePeriods;
|
||||||
activeIntensity += sample.getIntensity() + intensityBetweenActivePeriods;
|
activeIntensity += sample.getIntensity() + intensityBetweenActivePeriods;
|
||||||
heartRateForAverage += heartRateToAdd + heartRateBetweenActivePeriods;
|
if (heartRateUtilsInstance.isValidHeartRateValue(sample.getHeartRate())) {
|
||||||
|
heartRateSum.add(sample.getHeartRate());
|
||||||
|
}
|
||||||
|
heartRateSum.addAll(heartRateBetweenActivePeriodsSum);
|
||||||
|
heartRateBetweenActivePeriodsSum = new ArrayList<>();
|
||||||
stepsBetweenActivePeriods = 0;
|
stepsBetweenActivePeriods = 0;
|
||||||
heartRateBetweenActivePeriods = 0;
|
|
||||||
intensityBetweenActivePeriods = 0;
|
intensityBetweenActivePeriods = 0;
|
||||||
durationSinceLastActiveStep = 0;
|
durationSinceLastActiveStep = 0;
|
||||||
|
|
||||||
} else { //short break data to remember, we will add it to the rest later, if break not too long
|
} else { //short break data to remember, we will add it to the rest later, if break not too long
|
||||||
stepsBetweenActivePeriods += sample.getSteps();
|
stepsBetweenActivePeriods += sample.getSteps();
|
||||||
heartRateBetweenActivePeriods += heartRateToAdd;
|
if (heartRateUtilsInstance.isValidHeartRateValue(sample.getHeartRate())) {
|
||||||
|
heartRateBetweenActivePeriodsSum.add(sample.getHeartRate());
|
||||||
|
}
|
||||||
durationSinceLastActiveStep += durationSinceLastSample;
|
durationSinceLastActiveStep += durationSinceLastSample;
|
||||||
intensityBetweenActivePeriods += sample.getIntensity();
|
intensityBetweenActivePeriods += sample.getIntensity();
|
||||||
}
|
}
|
||||||
@ -123,7 +120,7 @@ public class StepAnalysis {
|
|||||||
int session_length = current - starting - durationSinceLastActiveStep;
|
int session_length = current - starting - durationSinceLastActiveStep;
|
||||||
|
|
||||||
if (session_length >= MIN_SESSION_LENGTH) { //valid activity session
|
if (session_length >= MIN_SESSION_LENGTH) { //valid activity session
|
||||||
int heartRateAverage = activeHrSamplesForAverage > 0 ? heartRateForAverage / activeHrSamplesForAverage : 0;
|
int heartRateAverage = heartRateSum.toArray().length > 0 ? calculateSumOfInts(heartRateSum) / heartRateSum.toArray().length : 0;
|
||||||
float distance = (float) (activeSteps * STEP_LENGTH_M);
|
float distance = (float) (activeSteps * STEP_LENGTH_M);
|
||||||
sessionEnd = new Date((sample.getTimestamp() - durationSinceLastActiveStep) * 1000L);
|
sessionEnd = new Date((sample.getTimestamp() - durationSinceLastActiveStep) * 1000L);
|
||||||
activityKind = detect_activity_kind(session_length, activeSteps, heartRateAverage, activeIntensity);
|
activityKind = detect_activity_kind(session_length, activeSteps, heartRateAverage, activeIntensity);
|
||||||
@ -143,7 +140,7 @@ public class StepAnalysis {
|
|||||||
int session_length = current - starting - durationSinceLastActiveStep;
|
int session_length = current - starting - durationSinceLastActiveStep;
|
||||||
|
|
||||||
if (session_length >= MIN_SESSION_LENGTH) {
|
if (session_length >= MIN_SESSION_LENGTH) {
|
||||||
int heartRateAverage = activeHrSamplesForAverage > 0 ? heartRateForAverage / activeHrSamplesForAverage : 0;
|
int heartRateAverage = heartRateSum.toArray().length > 0 ? calculateSumOfInts(heartRateSum) / heartRateSum.toArray().length : 0;
|
||||||
float distance = (float) (activeSteps * STEP_LENGTH_M);
|
float distance = (float) (activeSteps * STEP_LENGTH_M);
|
||||||
sessionEnd = getDateFromSample(previousSample);
|
sessionEnd = getDateFromSample(previousSample);
|
||||||
activityKind = detect_activity_kind(session_length, activeSteps, heartRateAverage, activeIntensity);
|
activityKind = detect_activity_kind(session_length, activeSteps, heartRateAverage, activeIntensity);
|
||||||
@ -155,7 +152,6 @@ public class StepAnalysis {
|
|||||||
|
|
||||||
public List<ActivitySession> calculateSummary(List<ActivitySession> sessions, boolean empty) {
|
public List<ActivitySession> calculateSummary(List<ActivitySession> sessions, boolean empty) {
|
||||||
|
|
||||||
HeartRateUtils heartRateUtilsInstance = HeartRateUtils.getInstance();
|
|
||||||
Date startTime = null;
|
Date startTime = null;
|
||||||
Date endTime = null;
|
Date endTime = null;
|
||||||
int stepsSum = 0;
|
int stepsSum = 0;
|
||||||
@ -180,9 +176,6 @@ public class StepAnalysis {
|
|||||||
if (heartRateSum.toArray().length > 0) {
|
if (heartRateSum.toArray().length > 0) {
|
||||||
heartRateAverage = calculateSumOfInts(heartRateSum) / heartRateSum.toArray().length;
|
heartRateAverage = calculateSumOfInts(heartRateSum) / heartRateSum.toArray().length;
|
||||||
}
|
}
|
||||||
if (!heartRateUtilsInstance.isValidHeartRateValue(heartRateAverage)) {
|
|
||||||
heartRateAverage = 0;
|
|
||||||
}
|
|
||||||
startTime = new Date(0);
|
startTime = new Date(0);
|
||||||
endTime = new Date(durationSum);
|
endTime = new Date(durationSum);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user