From 222f776d9f9d1f362038d26f835c96918a7af5fe Mon Sep 17 00:00:00 2001 From: Arseniy-Movshev Date: Mon, 6 Mar 2023 20:59:36 +0000 Subject: 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. --- sensorPlugins/heartrateSensor.cpp | 53 +++++++++++++++++++++++++++++++++++++++ sensorPlugins/heartrateSensor.h | 33 ++++++++++++++++++++++++ sensorPlugins/stepCounter.cpp | 43 +++++++++++++++++++++++++++++++ sensorPlugins/stepCounter.h | 30 ++++++++++++++++++++++ 4 files changed, 159 insertions(+) create mode 100644 sensorPlugins/heartrateSensor.cpp create mode 100644 sensorPlugins/heartrateSensor.h create mode 100644 sensorPlugins/stepCounter.cpp create mode 100644 sensorPlugins/stepCounter.h (limited to 'sensorPlugins') 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 +#include +#include +#include +#include + +#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 +#include +#include +#include + +#include + +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 +#include +#include +#include +#include + +#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 +#include +#include +#include + +#include + +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 -- cgit v1.2.3-54-g00ecf