summaryrefslogtreecommitdiff
path: root/daemon/logger.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'daemon/logger.cpp')
-rw-r--r--daemon/logger.cpp105
1 files changed, 105 insertions, 0 deletions
diff --git a/daemon/logger.cpp b/daemon/logger.cpp
new file mode 100644
index 0000000..93f764f
--- /dev/null
+++ b/daemon/logger.cpp
@@ -0,0 +1,105 @@
+/*
+ * Copyright (C) 2023 Arseniy Movshev <dodoradio@outlook.com>
+ * This file is part of sensorlogd, a sensor logger for the AsteroidOS smartwatch OS.
+ *
+ * sensorlogd is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
+ * sensorlogd is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+
+#include <QSettings>
+#include <QDBusInterface>
+#include <QDBusConnection>
+#include <QDateTime>
+#include <QFile>
+#include <QTextStream>
+#include <QStandardPaths>
+#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 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 << QString::number(recordTime.currentSecsSinceEpoch()) + ":" + logdata + "\n";
+ file.close();
+}
+bool dayFileExists(QString sensorPrefix, QDateTime dateTime) {
+ return QFile::exists(fileNameForDate(dateTime.date(), sensorPrefix));
+}
+
+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};
+ }
+ QTextStream inStream(&file);
+ QString line;
+ int i;
+ while(!inStream.atEnd())
+ {
+ line = inStream.readLine();
+ qDebug() << line;
+ i++;
+ }
+ file.close();
+ return line.split(":");
+}
+
+QString fileNameForDate(QDate date, QString prefix) {
+ return QStandardPaths::writableLocation(QStandardPaths::HomeLocation) + "/asteroid-healthloggerd/" + prefix + "/" + date.toString("yyyy-MM-dd.log");
+}