summaryrefslogtreecommitdiff
path: root/sensors.cpp
blob: d670ad75a66b749269b3b4f822e9640f805704f6 (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
#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();
        qDebug() << "heartrate sensor is enabled. interval is (ms) " << hrmInterval;
        hrmTimer = new QTimer(this);
        connect(hrmTimer,SIGNAL(timeout()),this,SLOT(recordHeartrate()));
        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::recordHeartrate() {
    qDebug() << "heartrate interval recording";
    hrmTimer->start(hrmInterval);
    QHrmSensor* heartrateSensor = new QHrmSensor(this);
    QFile file("out.txt");
    if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
        qDebug() << "failed to open file";
        return;
    }
    heartrateSensor->start();
    QTextStream out(&file);
    while(heartrateSensor->reading()->bpm() == 0) {
        qDebug() << "waiting for reading, currently " << heartrateSensor->reading()->bpm() << heartrateSensor->status() << heartrateSensor->isActive();
    }
    qDebug() << QDateTime::currentDateTime().toString("hh:mm:ss") << " : " << heartrateSensor->reading()->bpm() << heartrateSensor->status() << heartrateSensor->isActive();
    out << QDateTime::currentSecsSinceEpoch() << " : " << heartrateSensor->reading(); //reading() like this doesn't do anything useful. We need to wait for sensor to initialise and feed us back a reading
    heartrateSensor->stop();
    file.close();
}

void Logger::recordGPS() {
    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::recordStepcounter() {
    qDebug() << "stepcounter interval recording";
    hrmTimer->start(hrmInterval);
    QStepCounterSensor* stepcounterSensor = new QStepCounterSensor(this);
    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();
}