-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsqlstorage.cpp
123 lines (93 loc) · 3.09 KB
/
sqlstorage.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
113
114
115
116
117
118
119
120
121
122
123
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlError>
#include <QSqlDriver>
#include <QString>
#include <QStringList>
#include <QTimer>
#include <QVariant>
#include <QDebug>
#include "sqlstorage.hpp"
#include "config.hpp"
SqlStorage::SqlStorage(QObject *parent) : QObject(parent)
{
sqldb = new QSqlDatabase(QSqlDatabase::addDatabase("QPSQL"));
queue = new QQueue<InverterData*>();
timer = new QTimer();
timer->setInterval(SMLGR_CONFIG_DEFAULT_SQL_INTERVAL);
connect(timer, SIGNAL(timeout()), this, SLOT(copyQueueToDatabase()));
}
SqlStorage::~SqlStorage()
{
timer->stop();
delete timer;
delete queue;
sqldb->close();
delete sqldb;
}
void SqlStorage::start()
{
timer->start();
}
void SqlStorage::stop()
{
timer->stop();
}
void SqlStorage::config(QString ip, int port, QString username, QString password, QString database)
{
if(ip.length() == 0)
return;
if(port < 1 || port > 65535)
return;
if(username.length() == 0)
return;
if(password.length() == 0)
return;
if(database.length() == 0)
return;
sqldb->setHostName(ip);
sqldb->setPort(port);
sqldb->setUserName(username);
sqldb->setPassword(password);
sqldb->setDatabaseName(database);
}
void SqlStorage::addDataIntoQueue(InverterData data)
{
InverterData* item = new InverterData(data);
queue->enqueue(item);
qDebug() << "DATABASE" << "COUNT" << queue->count();
}
void SqlStorage::copyQueueToDatabase()
{
if(!sqldb->open()) {
qDebug() << "DATABASE ERROR" << sqldb->lastError().driverText();
return;
}
qDebug() << "DATABASE START";
while(!queue->isEmpty()) {
InverterData* elem = queue->head();
QString sqlstring = "INSERT INTO history (whenquery, UDC, IDC, UL1, IL1, PAC, PRL, TKK, TNF, KDY) VALUES"
"( :whenquery , :UDC , :IDC , :UL1 , :IL1 , :PAC , :PRL , :TKK , :TNF , :KDY );";
QSqlQuery sqlquery(*sqldb);
sqlquery.prepare(sqlstring);
sqlquery.bindValue(":whenquery", QVariant(elem->getDatetime()));
sqlquery.bindValue(":UDC", QVariant((int) (elem->getUdc() * 10)));
sqlquery.bindValue(":IDC", QVariant((int) (elem->getIdc() * 100)));
sqlquery.bindValue(":UL1", QVariant((int) (elem->getUl1() * 10)));
sqlquery.bindValue(":IL1", QVariant((int) (elem->getIl1() * 100)));
sqlquery.bindValue(":PAC", QVariant((int) (elem->getPac() * 2)));
sqlquery.bindValue(":PRL", QVariant((int) (elem->getPrl())));
sqlquery.bindValue(":TKK", QVariant((int) (elem->getTkk())));
sqlquery.bindValue(":TNF", QVariant((int) (elem->getTnf() * 100)));
sqlquery.bindValue(":KDY", QVariant((int) (elem->getKdy() * 10)));
if(!sqlquery.exec()) {
qDebug() << "DATABASE ERROR" << sqlquery.lastError().databaseText();
qDebug() << "DATABASE ERROR" << sqlquery.lastError().driverText();
break;
}
qDebug() << "DATABASE" << "REMOVING" << elem->getRaw();
queue->dequeue();
delete elem;
}
sqldb->close();
}