From 4af2e8a736f02a9309a80ef7ab8229e4d5b163c7 Mon Sep 17 00:00:00 2001 From: Arseniy-Movshev Date: Wed, 31 May 2023 01:00:14 +0100 Subject: Move to resetting the sensor in hardware The step counter is now set in hardware to the previous reading + any changes when the daemon starts This means that the step counter now just records the raw step counter value, and adjusts it correctly when needed This introduces a bug where the step counter will show bad readings if the daemon is restarted without a system reboot: the step counter's current value will be added to the last recorded value, which will cause the number of steps to effectively double in most situations. This might be fixed with some code to detect if a system reboot has passed since the last time the counter started. --- daemon/sensorPlugins/stepCounter.cpp | 17 +++++------------ daemon/sensorPlugins/stepCounter.h | 1 - 2 files changed, 5 insertions(+), 13 deletions(-) diff --git a/daemon/sensorPlugins/stepCounter.cpp b/daemon/sensorPlugins/stepCounter.cpp index 7d4a86f..3df23c7 100644 --- a/daemon/sensorPlugins/stepCounter.cpp +++ b/daemon/sensorPlugins/stepCounter.cpp @@ -15,6 +15,7 @@ #include #include #include +#include #include "../logger.h" @@ -36,18 +37,11 @@ StepCounterPlugin::StepCounterPlugin(QObject *parent, int initInterval) : QDateTime currDateTime = QDateTime::currentDateTime(); setupFilePath(sensorPathPrefix); + while (!stepcounterSensor->isActive()) {} if (dayFileExists(sensorPathPrefix)) { QStringList lastLineData = fileGetPrevRecord(sensorPathPrefix); - lastRecordTime = QDateTime::fromSecsSinceEpoch(lastLineData[0].toInt()); - if (stepcounterSensor->reading()->steps() == 0) { - stepsOffset = -(lastLineData[1].toInt()); - } else { - stepsOffset = - stepcounterSensor->reading()->steps() + (lastLineData[1].toInt()); - } - } else { - //if it's a new day, we 'reset' the counter. this is crude - we should really check for a boot here, since certain machines have capability of counting steps when powered down. - stepsOffset = stepcounterSensor->reading()->steps(); + stepcounterSensor->reading()->setSteps(lastLineData[1].toInt() + stepcounterSensor->reading()->steps()); // 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. } } @@ -66,15 +60,14 @@ void StepCounterPlugin::triggerRecording() { qDebug() << "stepcounter interval recording"; if (lastRecordTime.date() < QDate::currentDate()) { int steps = stepcounterSensor->reading()->steps(); - fileAddRecord(sensorPathPrefix,QString::number(steps - stepsOffset),QDateTime(QDate::currentDate().addDays(-1),QTime(23,59,59))); // this writes the current step count to the last second of the previous day + 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 stepcounterSensor->reading()->setSteps(0); // and then we reset the step counter - stepsOffset = 0; // and, since the step counter has been reset, there's no need for an offset any more qDebug() << "date change detected; recorded " << steps << " to previous day and reset step counter hw"; } else { int steps = stepcounterSensor->reading()->steps(); qDebug() << QDateTime::currentDateTime().toString("hh:mm:ss") << " : " << steps << stepcounterSensor->isActive(); //we probably ought to do some error checking here - fileAddRecord(sensorPathPrefix,QString::number(steps - stepsOffset)); + fileAddRecord(sensorPathPrefix,QString::number(steps)); } lastRecordTime = QDateTime::currentDateTime(); } diff --git a/daemon/sensorPlugins/stepCounter.h b/daemon/sensorPlugins/stepCounter.h index a221561..27dc01e 100644 --- a/daemon/sensorPlugins/stepCounter.h +++ b/daemon/sensorPlugins/stepCounter.h @@ -35,7 +35,6 @@ private: int interval; QTimer *recordIntervalTimer; QStepCounterSensor *stepcounterSensor; - int stepsOffset; //this is subtracted from the raw sensor value to compensate for daily step resets and boot offsets. const QString sensorPathPrefix = "stepCounter"; }; -- cgit v1.2.3-54-g00ecf