-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.cpp
112 lines (96 loc) · 3.03 KB
/
logger.cpp
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
#include "logger.h"
Logger::Logger(QObject *parent) : QObject(parent)
{
}
void Logger::init(QDir path)
{
logTimer = new QTimer();
logTimer->setInterval(1000); //Log every second
QObject::connect(logTimer, &QTimer::timeout, this, &Logger::log_to_file);
this->path = path;
voltage = 0.0f;
current = 0.0f;
energy = 0.0;
}
void Logger::start()
{
qDebug() << "Starting logger: " << path;
setup_file();
write_header();
if (logTimer) {
logTimer->start();
}
}
void Logger::stop()
{
if (logTimer) {
logTimer->stop();
}
file->close();
}
void Logger::update_data(float voltage, float current, double energy)
{
this->voltage = voltage;
this->current = current;
this->energy = energy;
}
void Logger::set_path(QDir path)
{
this->path = path;
}
void Logger::log_to_file()
{
if (!file->isOpen()) {
qDebug() << "Logger: Cannot write file! File not opened.";
logTimer->stop();
}
//concatenate all values to one long QString
QString line =
QString::number(QDateTime::currentDateTime().date().year()) + "-" +
QString::number(QDateTime::currentDateTime().date().month()) + "-" +
QString::number(QDateTime::currentDateTime().date().day()) + " " +
QDateTime::currentDateTime().time().toString() + ";" +
QString::number(voltage) + ";" +
QString::number(current) + ";" +
QString::number(energy);
QTextStream out(file);
out << line << "\r\n";
file->flush();
emit file_written();
}
void Logger::setup_file()
{
QString filename =
/* File name has format: yyyymmdd_hhmm_logfile.csv */
/* Year */
QString::number(QDateTime::currentDateTime().date().year()) +
/* Month with leading 0 */
QString("%1").arg(QDateTime::currentDateTime().date().month(), 2, 10, QChar('0')) +
/* Day with leading 0 */
QString("%1").arg(QDateTime::currentDateTime().date().day(), 2, 10, QChar('0')) +
/* Underscore and hour with leading 0 */
"_" + QString("%1").arg(QDateTime::currentDateTime().time().hour(), 2, 10, QChar('0')) +
/* Minute with leading 0 */
QString("%1").arg(QDateTime::currentDateTime().time().minute(), 2, 10, QChar('0')) +
/* Underscore and filename */
"_" + "logfile.csv";
file = new QFile(path.absolutePath() + "/" + filename);
if (!file->open(QIODevice::WriteOnly | QIODevice::Append)) {
qDebug() << "Cannot create logfile!";
}
}
void Logger::write_header()
{
if (!file->isOpen()) {
qDebug() << "Logger: Cannot write file! File not opened.";
logTimer->stop();
}
//Value headings
QString line =
QStringLiteral("Timestamp") + ";" +
QStringLiteral("Voltage [V]") + ";" +
QStringLiteral("Current [A]") + ";" +
QStringLiteral("Energy [Ah]");
QTextStream out(file);
out << line << "\r\n";
}