summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorArseniy-Movshev <dodoradio@outlook.com>2023-03-12 00:33:58 +0000
committerArseniy-Movshev <dodoradio@outlook.com>2023-03-12 11:51:49 +0000
commit55339aacf03642657e1f9c7d087f9b9fd319de4a (patch)
treecabe8dcc069d7d63a4978e22b4baab0df861d41b
parent47ed16926ac86295000a51fe1360a0ff9e21d84a (diff)
Change to having one record file per day and rewrite file access infrastructure to serve this.
I believe this is a useful improvement. The main two advantages are that a) it is very cheap to check whether a record exists for a given day - just check for file presence and b) there is a reasonable limit to the size of these files (never more than a few hundred records per day) which means that they can just be loaded into ram for processing without any complex splitting operations Currently, the necessary `~/asteroid-healthloggerd/stepCounter` and `...loggerd/heartrateMonitor` directories are not automatically created and the code just vomits errors into log if it can't write to them.
-rw-r--r--logger.cpp26
-rw-r--r--logger.h9
-rw-r--r--sensorPlugins/heartrateSensor.cpp2
-rw-r--r--sensorPlugins/heartrateSensor.h2
-rw-r--r--sensorPlugins/stepCounter.cpp22
-rw-r--r--sensorPlugins/stepCounter.h2
6 files changed, 42 insertions, 21 deletions
diff --git a/logger.cpp b/logger.cpp
index 641987f..8fd5953 100644
--- a/logger.cpp
+++ b/logger.cpp
@@ -4,6 +4,7 @@
#include <QDateTime>
#include <QFile>
#include <QTextStream>
+#include <QStandardPaths>
#include <QTimer>
#include <QDebug>
@@ -52,33 +53,42 @@ void Logger::displayOn(QString displayState) {
}
}
-void writeReadingToFile(QString data, QString filename) {
- QFile file(filename);
+void fileAddRecord(QString sensorPrefix, QString logdata, QDateTime recordTime) { //adds a record to today's log file for the given sensor
+ qDebug() << fileNameForDate(recordTime.date(), sensorPrefix);
+ QFile file(fileNameForDate(recordTime.date(), sensorPrefix));
if (!file.open(QIODevice::ReadWrite | QIODevice::Text)) {
qDebug() << "failed to open file";
return;
}
file.seek(file.size());
QTextStream out(&file);
- out << data;
+ out << QString::number(recordTime.currentSecsSinceEpoch()) + ":" + logdata + "\n";
file.close();
}
+bool dayFileExists(QString sensorPrefix, QDateTime dateTime) {
+ return QFile::exists(fileNameForDate(dateTime.date(), sensorPrefix));
+}
-QString getLineFromFile(int lineNumber, QString filename) {
- QFile file(filename);
+QStringList fileGetPrevRecord(QString sensorPrefix, QDateTime recordTime) {
+ qDebug() << fileNameForDate(recordTime.date(), sensorPrefix);
+ QFile file(fileNameForDate(recordTime.date(), sensorPrefix));
if (!file.open(QIODevice::ReadWrite | QIODevice::Text)) {
qDebug() << "failed to open file";
- return "0 : 0";
+ return {0,0};
}
QTextStream inStream(&file);
QString line;
int i;
- while(!inStream.atEnd() & (i < lineNumber | i < 0))
+ while(!inStream.atEnd())
{
line = inStream.readLine();
qDebug() << line;
i++;
}
file.close();
- return line;
+ return line.split(":");
+}
+
+QString fileNameForDate(QDate date, QString prefix) {
+ return QStandardPaths::writableLocation(QStandardPaths::HomeLocation) + "/asteroid-healthloggerd/" + prefix + "/" + date.toString("yyyy-MM-dd.log");
}
diff --git a/logger.h b/logger.h
index 53d797e..e56a51a 100644
--- a/logger.h
+++ b/logger.h
@@ -3,6 +3,7 @@
#include <QObject>
#include <QDateTime>
+#include <QDate>
#include <QDBusInterface>
#include <QTimer>
#include <QString>
@@ -16,6 +17,7 @@ class Logger : public QObject
public:
explicit Logger(QObject *parent = 0);
virtual ~Logger() {};
+
private slots:
void displayOn(QString displayState);
@@ -25,8 +27,11 @@ private:
HeartrateSensorPlugin *m_heartrateSensor;
bool stepCounterEnabled = true;
StepCounterPlugin *m_stepCounter;
+
};
-void writeReadingToFile(QString data, QString filename);
-QString getLineFromFile(int lineNumber, QString filename);
+ void fileAddRecord(QString sensorPrefix, QString logdata, QDateTime recordTime = QDateTime::currentDateTime()); //adds a record to today's log file for the given sensor
+ bool dayFileExists(QString sensorPrefix, QDateTime date = QDateTime::currentDateTime()); //check if today has a log file for the given sensor
+ QStringList fileGetPrevRecord(QString sensorPrefix, QDateTime recordTime = QDateTime::currentDateTime()); //works backwards to find the last record in today's file before the given time - returns nothing if no file is found.
+ QString fileNameForDate(QDate date, QString prefix);
#endif // LOGGER_H
diff --git a/sensorPlugins/heartrateSensor.cpp b/sensorPlugins/heartrateSensor.cpp
index 3ae18ee..3477e9a 100644
--- a/sensorPlugins/heartrateSensor.cpp
+++ b/sensorPlugins/heartrateSensor.cpp
@@ -48,6 +48,6 @@ void HeartrateSensorPlugin::finishRecording() {
qDebug() << "hrm sensor accuracy insufficient. waiting.";
return;
}
- writeReadingToFile(QString::number(QDateTime::currentSecsSinceEpoch()) + " : " + QString::number(bpm) + "\n", "hrmOut.txt");
+ fileAddRecord(sensorPathPrefix,QString::number(bpm));
hrmSensor->stop();
}
diff --git a/sensorPlugins/heartrateSensor.h b/sensorPlugins/heartrateSensor.h
index f6f98e0..13c1b9d 100644
--- a/sensorPlugins/heartrateSensor.h
+++ b/sensorPlugins/heartrateSensor.h
@@ -28,6 +28,8 @@ private:
int interval;
QTimer *recordIntervalTimer;
QHrmSensor *hrmSensor;
+
+ const QString sensorPathPrefix = "heartrateMonitor";
};
#endif // HEARTRATESENSOR_H
diff --git a/sensorPlugins/stepCounter.cpp b/sensorPlugins/stepCounter.cpp
index b184238..1d1fb7a 100644
--- a/sensorPlugins/stepCounter.cpp
+++ b/sensorPlugins/stepCounter.cpp
@@ -20,17 +20,21 @@ StepCounterPlugin::StepCounterPlugin(QObject *parent, int initInterval) :
connect(recordIntervalTimer,SIGNAL(timeout()),this,SLOT(triggerRecording()));
recordIntervalTimer->setSingleShot(true);
recordIntervalTimer->start(interval);
- 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.
+
+ if (dayFileExists(sensorPathPrefix)) {
+ QStringList lastLineData = fileGetPrevRecord(sensorPathPrefix);
+ lastRecordTime = QDateTime::fromSecsSinceEpoch(lastLineData[0].toInt());
+ if (stepcounterSensor->reading()->steps() == 0) {
+ stepsOffset = -(lastLineData[1].toInt());
+ } else {
+
+ }
+ } 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();
- } 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() {
@@ -53,5 +57,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 - stepsOffset) + "\n", fileName);
+ fileAddRecord(sensorPathPrefix,QString::number(steps - stepsOffset));
}
diff --git a/sensorPlugins/stepCounter.h b/sensorPlugins/stepCounter.h
index b6f2807..a7cb580 100644
--- a/sensorPlugins/stepCounter.h
+++ b/sensorPlugins/stepCounter.h
@@ -27,7 +27,7 @@ private:
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";
+ const QString sensorPathPrefix = "stepCounter";
};
#endif // STEPCOUNTER_H