From 2a1363a223cd8a59f08609c18022d09bda5d66df Mon Sep 17 00:00:00 2001 From: Arseniy-Movshev Date: Wed, 7 Jun 2023 14:06:18 +0100 Subject: StepCounter: revert to an offsets model It seems that the step counter doesn't actually allow setting the steps value. While setSteps() will cause the reading to change, the step counter may reset back to the previous step count when more steps are taken. --- daemon/sensorPlugins/stepCounter.cpp | 15 +++++++++------ daemon/sensorPlugins/stepCounter.h | 8 +++++--- 2 files changed, 14 insertions(+), 9 deletions(-) (limited to 'daemon/sensorPlugins') diff --git a/daemon/sensorPlugins/stepCounter.cpp b/daemon/sensorPlugins/stepCounter.cpp index a01c0b9..00d7066 100644 --- a/daemon/sensorPlugins/stepCounter.cpp +++ b/daemon/sensorPlugins/stepCounter.cpp @@ -15,6 +15,7 @@ #include #include #include +#include #include #include "../logger.h" @@ -38,14 +39,15 @@ StepCounterPlugin::StepCounterPlugin(QObject* parent, int initInterval, bool dae setupFilePath(sensorPathPrefix); while (!stepcounterSensor->isActive()) { } + m_settings = new QSettings("asteroid", "sensorlogd"); if (dayFileExists(sensorPathPrefix) && daemonFresh) { QStringList lastLineData = fileGetPrevRecord(sensorPathPrefix); lastRecordTime = QDateTime::currentDateTime(); - 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. - + 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. } else if (dayFileExists) { lastRecordTime = QDateTime::currentDateTime(); + m_stepsOffset = m_settings->value("StepCounterPrivate/stepsOffset", 0).toInt(); } } @@ -65,12 +67,13 @@ void StepCounterPlugin::triggerRecording() { qDebug() << "stepcounter interval recording"; if (lastRecordTime.date() < QDate::currentDate()) { - int steps = stepcounterSensor->reading()->steps(); + 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 - stepcounterSensor->reading()->setSteps(0); // and then we reset the step counter - qDebug() << "date change detected; recorded " << steps << " to previous day and reset step counter hw. Step counter now holds " << stepcounterSensor->reading()->steps() << " steps"; + m_stepsOffset = stepcounterSensor->reading()->steps(); // 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."; } else { - int steps = stepcounterSensor->reading()->steps(); + int steps = stepcounterSensor->reading()->steps() - m_stepsOffset; qDebug() << QDateTime::currentDateTime().toString("hh:mm:ss") << " : " << steps << stepcounterSensor->isActive(); // we probably ought to do some error checking here fileAddRecord(sensorPathPrefix, QString::number(steps)); diff --git a/daemon/sensorPlugins/stepCounter.h b/daemon/sensorPlugins/stepCounter.h index a6475f5..cb6a7e4 100644 --- a/daemon/sensorPlugins/stepCounter.h +++ b/daemon/sensorPlugins/stepCounter.h @@ -15,14 +15,14 @@ #include #include #include +#include #include -class StepCounterPlugin : public QObject -{ +class StepCounterPlugin : public QObject { Q_OBJECT public: - explicit StepCounterPlugin(QObject *parent = 0, int initInterval = 600000, bool daemonFresh = false); + explicit StepCounterPlugin(QObject* parent = 0, int initInterval = 600000, bool daemonFresh = false); virtual ~StepCounterPlugin() {}; void timeUpdate(); @@ -35,6 +35,8 @@ private: int interval; QTimer *recordIntervalTimer; QStepCounterSensor *stepcounterSensor; + int m_stepsOffset; + QSettings *m_settings; const QString sensorPathPrefix = "stepCounter"; }; -- cgit v1.2.3-54-g00ecf