From 47ed16926ac86295000a51fe1360a0ff9e21d84a Mon Sep 17 00:00:00 2001 From: Arseniy-Movshev Date: Sat, 11 Mar 2023 00:45:44 +0000 Subject: Step counter: reset every 24 hours, and load offsets from the log file when initialising. - This is quite crude - Firstly, we assume that the last record in the file is chronologically the last. This needs more consideration. - we do check if we're on a fresh boot, but we just give up completely if it's not a fresh boot, which may work, but needs testing to figure out how it actually works in practice --- logger.cpp | 19 +++++++++++++++++++ logger.h | 1 + sensorPlugins/stepCounter.cpp | 20 +++++++++++++++++--- sensorPlugins/stepCounter.h | 3 +++ 4 files changed, 40 insertions(+), 3 deletions(-) diff --git a/logger.cpp b/logger.cpp index 74b3d59..641987f 100644 --- a/logger.cpp +++ b/logger.cpp @@ -63,3 +63,22 @@ void writeReadingToFile(QString data, QString filename) { out << data; file.close(); } + +QString getLineFromFile(int lineNumber, QString filename) { + QFile file(filename); + if (!file.open(QIODevice::ReadWrite | QIODevice::Text)) { + qDebug() << "failed to open file"; + return "0 : 0"; + } + QTextStream inStream(&file); + QString line; + int i; + while(!inStream.atEnd() & (i < lineNumber | i < 0)) + { + line = inStream.readLine(); + qDebug() << line; + i++; + } + file.close(); + return line; +} diff --git a/logger.h b/logger.h index d92b52c..53d797e 100644 --- a/logger.h +++ b/logger.h @@ -27,5 +27,6 @@ private: StepCounterPlugin *m_stepCounter; }; void writeReadingToFile(QString data, QString filename); +QString getLineFromFile(int lineNumber, QString filename); #endif // LOGGER_H diff --git a/sensorPlugins/stepCounter.cpp b/sensorPlugins/stepCounter.cpp index 6afc408..b184238 100644 --- a/sensorPlugins/stepCounter.cpp +++ b/sensorPlugins/stepCounter.cpp @@ -20,11 +20,25 @@ StepCounterPlugin::StepCounterPlugin(QObject *parent, int initInterval) : connect(recordIntervalTimer,SIGNAL(timeout()),this,SLOT(triggerRecording())); recordIntervalTimer->setSingleShot(true); recordIntervalTimer->start(interval); - lastRecordTime = QDateTime::currentDateTime(); + QStringList lastLineData = getLineFromFile(-1,fileName).split(" : "); + lastRecordTime = QDateTime::fromSecsSinceEpoch(lastLineData[0].toInt()); + QDateTime currDateTime = QDateTime::currentDateTime(); + qDebug() << "last record time: " << lastLineData[0] << " last record steps: " << lastLineData[1]; + if (lastRecordTime.date() < currDateTime.date()) { //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(); + } else if (stepcounterSensor->reading()->steps() == 0) { //otherwise, keep counting from the last point. though we also need to check if we're running on the same boot as the previous recording, otherwise we may go into negatives. + stepsOffset = -(lastLineData[1].toInt()); + } + //and here we need to load in an appropriate offset. but this is a difficult topic. it would be nice really useful to figure out whether the daemon is running the first time for a given boot, in order to figure out whether we need to offset the sensor or take the first value as an actual data point. this gets even more messed up by catfish and other watches with their own rogue sensor management systems... maybe we could do the 'freshness' detection by creating a file in /tmp + //alternatively, we just assume that steps should only be counted when the daemon is running. hence, we set the offset to [current sensor reading] - [last reading in today's log] and } void StepCounterPlugin::timeUpdate() { - uint elapsed = QDateTime::currentMSecsSinceEpoch() - lastRecordTime.toMSecsSinceEpoch(); + QDateTime currDateTime = QDateTime::currentDateTime(); + if (lastRecordTime.date() < currDateTime.date()) { + stepsOffset = stepcounterSensor->reading()->steps(); //this 'resets' the reading whenever the screen is first turned on after midnight. this means that, in the morning, the step count will always be zero, but steps taken just before midnight are still counted and not discarded. + } + uint elapsed = currDateTime.toMSecsSinceEpoch() - lastRecordTime.toMSecsSinceEpoch(); qDebug() << "time until next steps recording" << recordIntervalTimer->remainingTime() << " elapsed = " << elapsed << " lastRecordTime " << lastRecordTime.toMSecsSinceEpoch(); if (elapsed > interval) { //if too much time has passed, reset the timer and record triggerRecording(); @@ -39,5 +53,5 @@ void StepCounterPlugin::triggerRecording() { int steps = stepcounterSensor->reading()->steps(); qDebug() << QDateTime::currentDateTime().toString("hh:mm:ss") << " : " << steps << stepcounterSensor->isActive(); //we probably ought to do some error checking here - writeReadingToFile(QString::number(QDateTime::currentSecsSinceEpoch()) + " : " + QString::number(steps) + "\n", "stepsOut.txt"); + writeReadingToFile(QString::number(QDateTime::currentSecsSinceEpoch()) + " : " + QString::number(steps - stepsOffset) + "\n", fileName); } diff --git a/sensorPlugins/stepCounter.h b/sensorPlugins/stepCounter.h index aff0917..b6f2807 100644 --- a/sensorPlugins/stepCounter.h +++ b/sensorPlugins/stepCounter.h @@ -25,6 +25,9 @@ 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 fileName = "stepsOut.txt"; }; #endif // STEPCOUNTER_H -- cgit v1.2.3-54-g00ecf