summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorArseniy-Movshev <dodoradio@outlook.com>2023-03-06 20:59:36 +0000
committerArseniy-Movshev <dodoradio@outlook.com>2023-03-07 21:44:42 +0000
commit222f776d9f9d1f362038d26f835c96918a7af5fe (patch)
treee1c5b01753d52603bc9d8e5cb91c1cf3287a8ba9
parent31a0592f40ccbe0ed31abcd8bf39c2c00dab699a (diff)
Refactor sensors into separate files
- this should make things a lot easier to maintain. instead of having everything in the same file, each sensor provides a constructor that sets it up correctly etc. - this should also get the step counter running when the service starts, which is necessary, since the sensor won't count on most devices unless the service is running. - this also temporarily removes the gps module from the service. it's not implemented right now anyway.
-rw-r--r--CMakeLists.txt8
-rw-r--r--healthd.cpp2
-rw-r--r--logger.cpp65
-rw-r--r--logger.h31
-rw-r--r--sensorPlugins/heartrateSensor.cpp53
-rw-r--r--sensorPlugins/heartrateSensor.h33
-rw-r--r--sensorPlugins/stepCounter.cpp43
-rw-r--r--sensorPlugins/stepCounter.h30
-rw-r--r--sensors.cpp144
-rw-r--r--sensors.h41
10 files changed, 262 insertions, 188 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index f03e831..44db130 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -13,8 +13,12 @@ find_package(Qt5 COMPONENTS Core DBus Qml Positioning Sensors REQUIRED)
add_executable(healthd
healthd.cpp
- sensors.h
- sensors.cpp
+ logger.cpp
+ logger.h
+ sensorPlugins/stepCounter.cpp
+ sensorPlugins/stepCounter.h
+ sensorPlugins/heartrateSensor.cpp
+ sensorPlugins/heartrateSensor.h
)
set_target_properties(healthd PROPERTIES AUTOMOC ON)
#add_compile_definitions(Q_DECLARE_PRIVATE_SUPPORTS_UNIQUE_PTR=${Q_DECLARE_PRIVATE_SUPPORTS_UNIQUE_PTR})
diff --git a/healthd.cpp b/healthd.cpp
index c7a0e85..884b55c 100644
--- a/healthd.cpp
+++ b/healthd.cpp
@@ -1,7 +1,7 @@
#include <QCoreApplication>
#include <QDBusConnection>
#include <QDebug>
-#include "sensors.h"
+#include "logger.h"
int main(int argc, char **argv)
{
diff --git a/logger.cpp b/logger.cpp
new file mode 100644
index 0000000..74b3d59
--- /dev/null
+++ b/logger.cpp
@@ -0,0 +1,65 @@
+#include <QSettings>
+#include <QDBusInterface>
+#include <QDBusConnection>
+#include <QDateTime>
+#include <QFile>
+#include <QTextStream>
+#include <QTimer>
+#include <QDebug>
+
+#include "logger.h"
+
+#include "sensorPlugins/stepCounter.h"
+#include "sensorPlugins/heartrateSensor.h"
+
+Logger::Logger(QObject *parent) :
+ QObject(parent){
+ m_iface = new QDBusInterface("com.nokia.mce","/com/nokia/mce/signal", "com.nokia.mce.signal", QDBusConnection::systemBus());
+ QSettings settings;
+
+//intialise HRM
+ if (heartrateSensorEnabled) { //add check for HRM
+ m_heartrateSensor = new HeartrateSensorPlugin(this,settings.value("stepsInterval",600000).toInt());
+ }
+
+//initialise step counter
+ if (stepCounterEnabled) { //add check for step sensor
+ m_stepCounter = new StepCounterPlugin(this,settings.value("stepsInterval",600000).toInt());
+ }
+
+ if(!m_iface->isValid()) {
+ qDebug() << "interface is not valid";
+ qDebug() << m_iface->lastError();
+ }
+ if(connect(m_iface, SIGNAL(display_status_ind(QString)), this, SLOT(displayOn(QString)))) { //this fires when the display turns on
+ qDebug() << "healthd connected display_status signal to slot";
+ }
+ qDebug() << "healthd sensors logger initialised";
+}
+
+void Logger::displayOn(QString displayState) {
+ if (displayState == "off")
+ return;
+ qDebug() << "display on detected";
+ uint currTime = QDateTime::currentMSecsSinceEpoch();
+
+ if (heartrateSensorEnabled) {
+ m_heartrateSensor->timeUpdate();
+ }
+
+ if (stepCounterEnabled) {
+ m_stepCounter->timeUpdate();
+ }
+}
+
+void writeReadingToFile(QString data, QString filename) {
+ QFile file(filename);
+ if (!file.open(QIODevice::ReadWrite | QIODevice::Text)) {
+ qDebug() << "failed to open file";
+ return;
+ }
+ file.seek(file.size());
+ QTextStream out(&file);
+ out << data;
+ file.close();
+}
diff --git a/logger.h b/logger.h
new file mode 100644
index 0000000..d92b52c
--- /dev/null
+++ b/logger.h
@@ -0,0 +1,31 @@
+#ifndef LOGGER_H
+#define LOGGER_H
+
+#include <QObject>
+#include <QDateTime>
+#include <QDBusInterface>
+#include <QTimer>
+#include <QString>
+
+#include "sensorPlugins/stepCounter.h"
+#include "sensorPlugins/heartrateSensor.h"
+
+class Logger : public QObject
+{
+ Q_OBJECT
+public:
+ explicit Logger(QObject *parent = 0);
+ virtual ~Logger() {};
+private slots:
+ void displayOn(QString displayState);
+
+private:
+ QDBusInterface *m_iface;
+ bool heartrateSensorEnabled = true;
+ HeartrateSensorPlugin *m_heartrateSensor;
+ bool stepCounterEnabled = true;
+ StepCounterPlugin *m_stepCounter;
+};
+void writeReadingToFile(QString data, QString filename);
+
+#endif // LOGGER_H
diff --git a/sensorPlugins/heartrateSensor.cpp b/sensorPlugins/heartrateSensor.cpp
new file mode 100644
index 0000000..3ae18ee
--- /dev/null
+++ b/sensorPlugins/heartrateSensor.cpp
@@ -0,0 +1,53 @@
+#include <QDateTime>
+#include <QTimer>
+#include <QtSensors/QHrmSensor>
+#include <QDebug>
+#include <QString>
+
+#include "../logger.h"
+
+#include "heartrateSensor.h"
+
+HeartrateSensorPlugin::HeartrateSensorPlugin(QObject *parent, int initInterval) :
+ QObject(parent){
+ interval = initInterval;
+
+ hrmSensor = new QHrmSensor(this);
+ connect(hrmSensor,SIGNAL(readingChanged()),this,SLOT(finishRecording()));
+
+ qDebug() << "heartrate sensor is enabled. interval is (ms) " << interval;
+ recordIntervalTimer = new QTimer(this);
+ connect(recordIntervalTimer,SIGNAL(timeout()),this,SLOT(triggerRecording()));
+ recordIntervalTimer->setSingleShot(true);
+ recordIntervalTimer->start(interval);
+ lastRecordTime = QDateTime::currentDateTime();
+}
+
+void HeartrateSensorPlugin::timeUpdate() {
+ uint elapsed = QDateTime::currentMSecsSinceEpoch() - 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();
+ lastRecordTime = QDateTime::currentDateTime();
+ } else { //otherwise, restart the timer and compensate for time spent in suspend
+ recordIntervalTimer->start(interval - elapsed);
+ }
+}
+
+void HeartrateSensorPlugin::triggerRecording() {
+ qDebug() << "heartrate interval recording";
+ recordIntervalTimer->start(interval);
+ hrmSensor->start();
+}
+
+void HeartrateSensorPlugin::finishRecording() {
+ qDebug() << "bpm update received";
+ int bpm = hrmSensor->reading()->bpm();
+ qDebug() << QDateTime::currentDateTime().toString("hh:mm:ss") << " : " << bpm << hrmSensor->status() << hrmSensor->isActive();
+ if ((bpm == 0) || (hrmSensor->status() < 3)) {
+ qDebug() << "hrm sensor accuracy insufficient. waiting.";
+ return;
+ }
+ writeReadingToFile(QString::number(QDateTime::currentSecsSinceEpoch()) + " : " + QString::number(bpm) + "\n", "hrmOut.txt");
+ hrmSensor->stop();
+}
diff --git a/sensorPlugins/heartrateSensor.h b/sensorPlugins/heartrateSensor.h
new file mode 100644
index 0000000..f6f98e0
--- /dev/null
+++ b/sensorPlugins/heartrateSensor.h
@@ -0,0 +1,33 @@
+#ifndef HEARTRATESENSOR_H
+#define HEARTRATESENSOR_H
+
+#include <QObject>
+#include <QDateTime>
+#include <QDBusInterface>
+#include <QTimer>
+
+#include <QtSensors/QHrmSensor>
+
+class HeartrateSensorPlugin : public QObject
+{
+ Q_OBJECT
+public:
+ explicit HeartrateSensorPlugin(QObject *parent = 0, int initInterval = 600000);
+ virtual ~HeartrateSensorPlugin() {};
+
+ void timeUpdate();
+
+public slots:
+ void triggerRecording();
+
+private slots:
+ void finishRecording();
+
+private:
+ QDateTime lastRecordTime;
+ int interval;
+ QTimer *recordIntervalTimer;
+ QHrmSensor *hrmSensor;
+};
+
+#endif // HEARTRATESENSOR_H
diff --git a/sensorPlugins/stepCounter.cpp b/sensorPlugins/stepCounter.cpp
new file mode 100644
index 0000000..6afc408
--- /dev/null
+++ b/sensorPlugins/stepCounter.cpp
@@ -0,0 +1,43 @@
+#include <QDateTime>
+#include <QTimer>
+#include <QtSensors/QStepCounterSensor>
+#include <QDebug>
+#include <QString>
+
+#include "../logger.h"
+
+#include "stepCounter.h"
+
+StepCounterPlugin::StepCounterPlugin(QObject *parent, int initInterval) :
+ QObject(parent){
+ interval = initInterval;
+
+ stepcounterSensor = new QStepCounterSensor(this);
+ stepcounterSensor->start();
+
+ qDebug() << "step counter sensor is enabled. interval is (ms) " << interval;
+ recordIntervalTimer = new QTimer(this);
+ connect(recordIntervalTimer,SIGNAL(timeout()),this,SLOT(triggerRecording()));
+ recordIntervalTimer->setSingleShot(true);
+ recordIntervalTimer->start(interval);
+ lastRecordTime = QDateTime::currentDateTime();
+}
+
+void StepCounterPlugin::timeUpdate() {
+ uint elapsed = QDateTime::currentMSecsSinceEpoch() - 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();
+ lastRecordTime = QDateTime::currentDateTime();
+ } else { //otherwise, restart the timer and compensate for time spent in suspend
+ recordIntervalTimer->start(interval - elapsed);
+ }
+}
+
+void StepCounterPlugin::triggerRecording() {
+ qDebug() << "stepcounter interval recording";
+ 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");
+}
diff --git a/sensorPlugins/stepCounter.h b/sensorPlugins/stepCounter.h
new file mode 100644
index 0000000..aff0917
--- /dev/null
+++ b/sensorPlugins/stepCounter.h
@@ -0,0 +1,30 @@
+#ifndef STEPCOUNTER_H
+#define STEPCOUNTER_H
+
+#include <QObject>
+#include <QDateTime>
+#include <QDBusInterface>
+#include <QTimer>
+
+#include <QtSensors/QStepCounterSensor>
+
+class StepCounterPlugin : public QObject
+{
+ Q_OBJECT
+public:
+ explicit StepCounterPlugin(QObject *parent = 0, int initInterval = 600000);
+ virtual ~StepCounterPlugin() {};
+
+ void timeUpdate();
+
+public slots:
+ void triggerRecording();
+
+private:
+ QDateTime lastRecordTime;
+ int interval;
+ QTimer *recordIntervalTimer;
+ QStepCounterSensor *stepcounterSensor;
+};
+
+#endif // STEPCOUNTER_H
diff --git a/sensors.cpp b/sensors.cpp
deleted file mode 100644
index ac286c1..0000000
--- a/sensors.cpp
+++ /dev/null
@@ -1,144 +0,0 @@
-#include <QSettings>
-#include <QDBusInterface>
-#include <QDBusConnection>
-#include <QDateTime>
-#include <QFile>
-#include <QTextStream>
-#include <QTimer>
-#include <QtSensors/QStepCounterSensor>
-#include <QtSensors/QHrmSensor>
-#include <QDebug>
-
-#include "sensors.h"
-
-Logger::Logger(QObject *parent) :
- QObject(parent){
- m_iface = new QDBusInterface("com.nokia.mce","/com/nokia/mce/signal", "com.nokia.mce.signal", QDBusConnection::systemBus());
- QSettings settings;
- if (true) { //add check for HRM
- hrmInterval = settings.value("hrmInterval",60000).toInt();
-
- heartrateSensor = new QHrmSensor(this);
- connect(heartrateSensor,SIGNAL(readingChanged()),this,SLOT(recordHeartrate()));
-
- qDebug() << "heartrate sensor is enabled. interval is (ms) " << hrmInterval;
- hrmTimer = new QTimer(this);
- connect(hrmTimer,SIGNAL(timeout()),this,SLOT(setupRecordHeartrate()));
- hrmTimer->setSingleShot(true);
- hrmTimer->start(hrmInterval);
- hrmLastTime = QDateTime::currentDateTime();
- }
- if (true) { //add check for HRM
- stepsInterval = settings.value("stepsInterval",60000).toInt();
- qDebug() << "step counter sensor is enabled. interval is (ms) " << stepsInterval;
- stepsTimer = new QTimer(this);
- connect(stepsTimer,SIGNAL(timeout()),this,SLOT(recordStepcounter()));
- stepsTimer->setSingleShot(true);
- stepsTimer->start(stepsInterval);
- stepsLastTime = QDateTime::currentDateTime();
- }
- if(!m_iface->isValid()) {
- qDebug() << "interface is not valid";
- qDebug() << m_iface->lastError();
- }
- if(connect(m_iface, SIGNAL(display_status_ind(QString)), this, SLOT(displayOn(QString)))) { //this fires when the display turns on
- qDebug() << "healthd connected display_status signal to slot";
- }
- qDebug() << "healthd sensors logger initialised";
-}
-
-void Logger::displayOn(QString displayState) {
- if (displayState == "off")
- return;
- qDebug() << "display on detected";
- uint currTime = QDateTime::currentMSecsSinceEpoch();
- if (true) { //check for hrm
- uint elapsed = currTime - hrmLastTime.toMSecsSinceEpoch();
- qDebug() << "time until next hrm recording" << hrmTimer->remainingTime() << " elapsed = " << elapsed << " currTime " << currTime << " hrmLastTime" << hrmLastTime.toMSecsSinceEpoch();
- if (elapsed > hrmInterval) { //if too much time has passed, reset the timer and record heartrate
- recordHeartrate();
- hrmLastTime = QDateTime::currentDateTime();
- } else { //otherwise, restart the timer and compensate for time spent in suspend
- hrmTimer->start(hrmInterval - elapsed);
- }
- }
- if (true) { //check for stepcounter
- uint elapsed = currTime - stepsLastTime.toMSecsSinceEpoch();
- qDebug() << "time until next steps recording" << stepsTimer->remainingTime() << " elapsed = " << elapsed << " currTime " << currTime << " stepsLastTime" << stepsLastTime.toMSecsSinceEpoch();
- if (elapsed > stepsInterval) { //if too much time has passed, reset the timer and record heartrate
- recordStepcounter();
- stepsLastTime = QDateTime::currentDateTime();
- } else { //otherwise, restart the timer and compensate for time spent in suspend
- stepsTimer->start(stepsInterval - elapsed);
- }
- }
-}
-
-void Logger::setupRecordHeartrate() {
- qDebug() << "heartrate interval recording";
- hrmTimer->start(hrmInterval);
- heartrateSensor->start();
-}
-
-void Logger::recordHeartrate() {
- qDebug() << "bpm update received";
- int bpm = heartrateSensor->reading()->bpm();
- qDebug() << QDateTime::currentDateTime().toString("hh:mm:ss") << " : " << bpm << heartrateSensor->status() << heartrateSensor->isActive();
- if ((bpm == 0) || (heartrateSensor->status() < 3)) {
- return;
- }
- QFile file("out.txt");
- if (!file.open(QIODevice::ReadWrite | QIODevice::Text)) {
- qDebug() << "failed to open file";
- return;
- }
- file.seek(file.size());
- QTextStream out(&file);
- out << QDateTime::currentSecsSinceEpoch() << " : " << bpm << "\n";
- qDebug() << "wrote to file";
- heartrateSensor->stop();
- file.close();
-}
-
-void Logger::setupRecordGPS() {
- QTimer::singleShot(hrmInterval, this, SLOT(recordHeartrate()));
- QHrmSensor* heartrateSensor = new QHrmSensor(this); //ok GPS doesn't work like this, this gonna need a custom solution. QtLocation to the rescue, or something :/
- QFile file("out.txt");
- if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
- return;
-
- QTextStream out(&file);
- out << QDateTime::currentSecsSinceEpoch() << " : " << heartrateSensor->reading();
- file.close();
- //create the GPS location
- //open the file we're recording into
- //wait for the sensor to finish setting up
- //read location
- //format
- //save to file
-}
-
-void Logger::recordGPS() {
-}
-
-void Logger::setupRecordStepcounter() {
- qDebug() << "stepcounter interval recording";
- hrmTimer->start(hrmInterval);
- QFile file("out.txt");
- if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
- qDebug() << "failed to open file";
- return;
- }
- stepcounterSensor->start();
- QTextStream out(&file);
- while(/*stepcounterSensor->reading()->steps() == 0*/false) {
- qDebug() << "waiting for reading, currently " << stepcounterSensor->reading()->steps() << stepcounterSensor->isActive();
- }
- qDebug() << QDateTime::currentDateTime().toString("hh:mm:ss") << " : " << stepcounterSensor->reading()->steps() << stepcounterSensor->isActive();
- out << QDateTime::currentSecsSinceEpoch() << " : " << stepcounterSensor->reading(); //reading() like this doesn't do anything useful. We need to wait for sensor to initialise and feed us back a reading
- stepcounterSensor->stop();
- file.close();
-}
-
-void Logger::recordStepcounter() {
-}
diff --git a/sensors.h b/sensors.h
deleted file mode 100644
index bd44672..0000000
--- a/sensors.h
+++ /dev/null
@@ -1,41 +0,0 @@
-#ifndef LOGGER_H
-#define LOGGER_H
-
-#include <QObject>
-#include <QDateTime>
-#include <QDBusInterface>
-#include <QTimer>
-
-#include <QtSensors/QStepCounterSensor>
-#include <QtSensors/QHrmSensor>
-
-class Logger : public QObject
-{
- Q_OBJECT
-public:
- explicit Logger(QObject *parent = 0);
- virtual ~Logger() {};
-private slots:
- void displayOn(QString displayState);
- void recordHeartrate();
- void recordGPS();
- void recordStepcounter();
-
- void setupRecordHeartrate();
- void setupRecordGPS();
- void setupRecordStepcounter();
-private:
- QDateTime hrmLastTime;
- QDateTime stepsLastTime;
- QDBusInterface *m_iface;
- int hrmInterval;
- int gpsInterval;
- int stepsInterval;
- QTimer *hrmTimer;
- QTimer *gpsTimer;
- QTimer *stepsTimer;
- QHrmSensor *heartrateSensor ;
- QStepCounterSensor *stepcounterSensor;
-};
-
-#endif // LOGGER_H