summaryrefslogtreecommitdiff
path: root/sensors.cpp
blob: ac286c1405019d317e902cba5101829aaced5b10 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#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() {
}