-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.cpp
More file actions
104 lines (80 loc) · 2.43 KB
/
server.cpp
File metadata and controls
104 lines (80 loc) · 2.43 KB
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
#include "server.h"
#include <QDebug>
QT_CHARTS_USE_NAMESPACE
Q_DECLARE_METATYPE(QAbstractSeries *)
Q_DECLARE_METATYPE(QDateTimeAxis *)
Server::Server(QObject *parent) : QObject(parent)
{
}
Server::~Server()
{
db.close();
}
QString Server::username() const
{
return m_username;
}
void Server::setDatabase(QString hn, QString db,int por)
{
hostname=hn;
database=db;
port=por;
}
void Server::setLineSeries(QLineSeries *lineSeries)
{
QPen pentemp;
pentemp.setColor(QColor(34, 102, 102));
pentemp.setWidth(5);
lineSeries->setPen(pentemp);
lineSeries->clear();
QSqlQuery query("SELECT * FROM "+database+".`usage`");
QSqlRecord rec = query.record();
qDebug() << "Number of columns: " << rec.count();
int timeCol = rec.indexOf("time"); // index of the field "name"
int energyCol = rec.indexOf("energy");
qDebug()<<timeCol;
while (query.next()){
qDebug() << query.value(timeCol); // output all names
lineSeries->append(QDateTime(query.value(timeCol).toDate(),QTime(0,0)).toMSecsSinceEpoch(), query.value(energyCol).toDouble());
}
query.clear();
}
float Server::queryLatest(QString qry)
{
QSqlQuery query("SELECT * FROM "+database+".`usage`");
QSqlRecord rec = query.record();
int Col = rec.indexOf(qry);
if(query.last()){
qDebug()<<"value ssad "<<qry<<" "<<query.value(Col).toFloat();
return query.value(Col).toFloat();
}
else
return 0.0;
}
QDateTimeAxis* Server::getAxisXTime()
{
xValue.setTickCount(4);
xValue.setFormat("MMM yyyy");
QSqlQuery query("SELECT * FROM "+database+".`usage`");
QSqlRecord rec = query.record();
qDebug() << "Number of columns: " << rec.count();
int timeCol = rec.indexOf("time");
int index=0;
if(query.first())
{xValue.setMin(QDateTime(query.value(timeCol).toDate(),QTime(0,0)));index++;}
if(query.last())
{xValue.setMax(QDateTime(query.value(timeCol).toDate(),QTime(0,0)));index++;}
return &xValue;
}
bool Server::authenticate(QString password)
{
db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName(hostname);
db.setDatabaseName(database);
db.setUserName(m_username);
db.setPassword(password);
db.setPort(port);
bool ret= db.open();
qDebug()<<db.hostName();
return ret;
}