forked from hoffmannjoern/IoT-Shield-App
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApiManager.cpp
133 lines (102 loc) · 3.67 KB
/
ApiManager.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
124
125
126
127
128
129
130
131
132
#include "ApiManager.h"
#include <QNetworkRequest>
#include <QNetworkAccessManager>
#include <QEventLoop>
#include <QThread>
#include <future>
using namespace ThingSpeak;
const QUrl ApiManager::apiUrlDefault = QUrl("https://api.thingspeak.com");
ApiManager::ApiManager(const QUrl &apiUrl) : apiUrl(apiUrl)
{
}
qint64 ApiManager::getTimeSinceLastRequestSec()
{
QDateTime currentDateTime(QDateTime::currentDateTime());
return currentDateTime.toTime_t() - lastRequest.toTime_t();
}
quint64 ApiManager::getWaitTimeSec()
{
qint64 timeDiffSec = getTimeSinceLastRequestSec();
if (timeDiffSec < timeBetweenRequestsSec)
return timeBetweenRequestsSec - timeDiffSec + 2;
return 0;
}
void ApiManager::waitForTimeout()
{
quint64 waitTime = getWaitTimeSec();
if (waitTime == 0)
return;
QThread::sleep(waitTime);
}
QUrl ApiManager::buildRequestUrl(const QString &path)
{
QUrl url = apiUrl;
url.setPath(path);
return url;
}
QNetworkReply* ApiManager::sendPostRequestSync(const QString &path, const QUrlQuery &query)
{
// Build the url.
QUrl url = buildRequestUrl(path);
// Create request
QNetworkRequest request(url);
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
// Wait until we can issue the next request
waitForTimeout();
lastRequest = QDateTime(QDateTime::currentDateTime());
// Perform request and wait for completion
// Create an event loop to synchronize http request
QEventLoop syncEventLoop;
QObject::connect(&manager, SIGNAL(finished(QNetworkReply*)), &syncEventLoop, SLOT(quit()));
QNetworkReply *reply = manager.post(request, query.toString(QUrl::FullyEncoded).toUtf8());
// Wait for completion
syncEventLoop.exec();
return reply;
}
QNetworkReply* ApiManager::sendGetRequestSync(const QString &path, const QUrlQuery &query)
{
// Build the url.
QUrl url = buildRequestUrl(path);
url.setQuery(query);
// Create request
QNetworkRequest request(url);
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
// Wait until we can issue the next request
waitForTimeout();
lastRequest = QDateTime(QDateTime::currentDateTime());
// Perform request and wait for completion
// Connect the network manager's finish() signal in order to synchronize http requests
QEventLoop syncEventLoop;
QObject::connect(&manager, SIGNAL(finished(QNetworkReply*)), &syncEventLoop, SLOT(quit()));
QNetworkReply *reply = manager.get(request);
// Wait for completion
syncEventLoop.exec();
return reply;
}
void ApiManager::sendGetRequestAsync(const QString &path, const QUrlQuery &query, const std::function <void (QNetworkReply*)> &callback)
{
std::async(std::launch::async, [&](){
// Build the url.
QUrl url = buildRequestUrl(path);
url.setQuery(query);
// Create request
QNetworkRequest request(url);
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
// Wait until we can issue the next request
waitForTimeout();
lastRequest = QDateTime(QDateTime::currentDateTime());
qDebug() << "Here";
// Perform request and wait for completion
// Connect the network manager's finish() signal in order to synchronize http requests
QObject::connect(&manager, &QNetworkAccessManager::finished, callback);
manager.get(request);
});
}
unsigned int ApiManager::getTimeBetweenRequestsSec() const
{
return timeBetweenRequestsSec;
}
void ApiManager::setTimeBetweenRequestsSec(unsigned int value)
{
timeBetweenRequestsSec = value;
}