summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorArseniy-Movshev <dodoradio@outlook.com>2023-06-10 00:29:19 +0100
committerArseniy-Movshev <dodoradio@outlook.com>2023-06-20 00:05:28 +0100
commit451388685a13669561b778e5719379db39164d47 (patch)
tree2b8e36bece5d78e2c528c6f2dcb9cdd47aa3736b
parent79184000729be9c1615d3a07449d1244dcacd887 (diff)
Compensate for steps when daemon starts up and when date changes
This is a radical new approach where we just scrap all steps that have happened outside of the current day. Theoretically, this should be more robust. Remains to be seen whether it actually does work any better.
-rw-r--r--daemon/sensorPlugins/stepCounter.cpp26
-rw-r--r--daemon/sensorPlugins/stepCounter.h2
2 files changed, 20 insertions, 8 deletions
diff --git a/daemon/sensorPlugins/stepCounter.cpp b/daemon/sensorPlugins/stepCounter.cpp
index 00d7066..fd09e81 100644
--- a/daemon/sensorPlugins/stepCounter.cpp
+++ b/daemon/sensorPlugins/stepCounter.cpp
@@ -44,11 +44,12 @@ StepCounterPlugin::StepCounterPlugin(QObject* parent, int initInterval, bool dae
if (dayFileExists(sensorPathPrefix) && daemonFresh) {
QStringList lastLineData = fileGetPrevRecord(sensorPathPrefix);
lastRecordTime = QDateTime::currentDateTime();
- m_stepsOffset = -lastLineData[1].toInt(); // we add the last recorded value from today to the current value. This 'recovers' the steps from between reboots. I'm not sure how this will work on catfish or medaka.
+ m_stepsOffset = stepcounterSensor->reading()->steps() - lastLineData[1].toInt(); // we reset the step count, ignoring all steps that may have been taken while the daemon isn't running. Devices which count steps independently will need custom code.
} else if (dayFileExists) {
lastRecordTime = QDateTime::currentDateTime();
m_stepsOffset = m_settings->value("StepCounterPrivate/stepsOffset", 0).toInt();
}
+ m_settings->setValue("StepCounterPrivate/stepsOffset", m_stepsOffset);
}
void StepCounterPlugin::timeUpdate()
@@ -66,16 +67,27 @@ void StepCounterPlugin::timeUpdate()
void StepCounterPlugin::triggerRecording()
{
qDebug() << "stepcounter interval recording";
- if (lastRecordTime.date() < QDate::currentDate()) {
- int steps = stepcounterSensor->reading()->steps() - m_stepsOffset;
- fileAddRecord(sensorPathPrefix, QString::number(steps), QDateTime(QDate::currentDate().addDays(-1), QTime(23, 59, 59))); // this writes the current step count to the last second of the previous day
- m_stepsOffset = stepcounterSensor->reading()->steps(); // and then we reset the step counter
+ int rawSteps = stepcounterSensor->reading()->steps();
+ if (lastRecordTime.date() != QDate::currentDate()) { // ok, so the date has changed.
+ if (dayFileExists(sensorPathPrefix)) { // this likely means we have just had a system time change
+ QStringList lastLineData = fileGetPrevRecord(sensorPathPrefix);
+ m_stepsOffset = rawSteps - lastLineData[1].toInt(); // we scrap the current steps value, recover the last reading from today's file and count from there
+ // The reset is necessary because asteroid doesn't have robust time when it boots up, it takes a while for that to happen. So, instead of dealing with that, we just reset the counter every time there's a date change
+ } else if (lastRecordTime.date() == QDate::currentDate().addDays(-1)) { // this means that a midnight just passed
+ int steps = rawSteps - m_stepsOffset;
+ fileAddRecord(sensorPathPrefix, QString::number(steps), QDateTime(QDate::currentDate().addDays(-1), QTime(23, 59, 59))); // this writes the current step count to the last second of the previous day
+ m_stepsOffset = rawSteps; // and then we 'reset' the step counter
+ }
m_settings->setValue("StepCounterPrivate/stepsOffset", m_stepsOffset);
- qDebug() << "date change detected; recorded " << steps << " to previous day and reset step counter hw.";
+ qDebug() << "date change detected. Step counter value is " << rawSteps << " and offset is " << m_stepsOffset;
} else {
- int steps = stepcounterSensor->reading()->steps() - m_stepsOffset;
+ int steps = rawSteps - m_stepsOffset;
qDebug() << QDateTime::currentDateTime().toString("hh:mm:ss") << " : " << steps << stepcounterSensor->isActive();
// we probably ought to do some error checking here
+ if (steps < fileGetPrevRecord(sensorPathPrefix)[1].toInt()) {
+ qDebug() << "error: offset value is less than previous value - not recording"; // this is an error condition which is either caused by a bad offset value or by the sensor being slow and returning a bad value
+ return;
+ }
fileAddRecord(sensorPathPrefix, QString::number(steps));
}
lastRecordTime = QDateTime::currentDateTime();
diff --git a/daemon/sensorPlugins/stepCounter.h b/daemon/sensorPlugins/stepCounter.h
index cb6a7e4..ad8d8d6 100644
--- a/daemon/sensorPlugins/stepCounter.h
+++ b/daemon/sensorPlugins/stepCounter.h
@@ -35,7 +35,7 @@ private:
int interval;
QTimer *recordIntervalTimer;
QStepCounterSensor *stepcounterSensor;
- int m_stepsOffset;
+ int m_stepsOffset = 0;
QSettings *m_settings;
const QString sensorPathPrefix = "stepCounter";