112 lines
3.2 KiB
C++
112 lines
3.2 KiB
C++
|
|
#include "requestworker.h"
|
||
|
|
#include <QJsonArray>
|
||
|
|
#include <QJsonValue>
|
||
|
|
#include <QJsonObject>
|
||
|
|
#include <QJsonDocument>
|
||
|
|
|
||
|
|
RequestWorker::RequestWorker(QObject *parent)
|
||
|
|
: QObject{parent}
|
||
|
|
{
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
void RequestWorker::setConfig(QJsonObject config)
|
||
|
|
{
|
||
|
|
this->url = config["url"].toString();
|
||
|
|
this->interval = config["interval"].toInt();
|
||
|
|
this->max_height = config["max"].toInt();
|
||
|
|
this->min_height = config["min"].toInt();
|
||
|
|
qDebug() << "In cpp config values are: ";
|
||
|
|
qDebug() << "Url: " << url;
|
||
|
|
qDebug() << "Interval: " << interval;
|
||
|
|
qDebug() << "Max Height: " << max_height;
|
||
|
|
qDebug() << "Min Height: " << min_height;
|
||
|
|
}
|
||
|
|
|
||
|
|
void RequestWorker::start()
|
||
|
|
{
|
||
|
|
if(manager) {
|
||
|
|
manager->deleteLater();
|
||
|
|
manager = nullptr;
|
||
|
|
}
|
||
|
|
manager = new QNetworkAccessManager();
|
||
|
|
if(timer) {
|
||
|
|
timer->deleteLater();
|
||
|
|
timer = nullptr;
|
||
|
|
}
|
||
|
|
timer = new QTimer(this);
|
||
|
|
timer->setInterval(interval);
|
||
|
|
timer->setSingleShot(true);
|
||
|
|
connect(timer, &QTimer::timeout, this, &RequestWorker::makeRequest);
|
||
|
|
timer->start();
|
||
|
|
stop_timer = false;
|
||
|
|
setStatus("Waiting to send.");
|
||
|
|
}
|
||
|
|
|
||
|
|
void RequestWorker::stop()
|
||
|
|
{
|
||
|
|
if(timer->isActive())
|
||
|
|
timer->stop();
|
||
|
|
stop_timer = true;
|
||
|
|
}
|
||
|
|
|
||
|
|
void RequestWorker::makeRequest()
|
||
|
|
{
|
||
|
|
if(stop_timer)
|
||
|
|
return;
|
||
|
|
QNetworkRequest req;
|
||
|
|
req.setUrl(this->url);
|
||
|
|
disconnect(manager, nullptr, nullptr, nullptr);
|
||
|
|
connect(manager, &QNetworkAccessManager::finished, this, &RequestWorker::requestFinished);
|
||
|
|
manager->setTransferTimeout(15000);
|
||
|
|
manager->get(req);
|
||
|
|
setStatus("Request sent; Waiting for Reply.");
|
||
|
|
}
|
||
|
|
|
||
|
|
void RequestWorker::requestFinished(QNetworkReply *rep)
|
||
|
|
{
|
||
|
|
QByteArray data = rep->readAll();
|
||
|
|
rep->deleteLater();
|
||
|
|
rep = nullptr;
|
||
|
|
auto now = std::chrono::high_resolution_clock::now();
|
||
|
|
auto time_diff = std::chrono::duration_cast<std::chrono::microseconds>(now - prev_time);
|
||
|
|
unsigned int time_in_microseconds = time_diff.count();
|
||
|
|
QJsonObject jdata = QJsonDocument::fromJson(data).object();
|
||
|
|
auto data_water_level = jdata["water_level"].toInt();
|
||
|
|
float water_level = (float) ((float) (data_water_level) / 10.0);
|
||
|
|
float rate_of_fill = ( float )(water_level - prev_distance) /(float) time_in_microseconds;
|
||
|
|
rate_of_fill = rate_of_fill * 1'000'000;
|
||
|
|
int expected_to_fill = (max_height - water_level) / rate_of_fill;
|
||
|
|
if(reply_number == 0) {
|
||
|
|
rate_of_fill = 0;
|
||
|
|
expected_to_fill = 0;
|
||
|
|
}
|
||
|
|
reply_number = 1;
|
||
|
|
qDebug() << "Got water level: " << water_level;
|
||
|
|
qDebug() << "Got rate of fill: " << rate_of_fill;
|
||
|
|
qDebug() << "Got expected to fill: " << expected_to_fill;
|
||
|
|
qDebug() << "Time difference: " << time_in_microseconds;
|
||
|
|
prev_distance = water_level;
|
||
|
|
emit networkReply(water_level, rate_of_fill, expected_to_fill);
|
||
|
|
if(stop_timer) {
|
||
|
|
setStatus("Timer stopped.");
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
prev_time = now;
|
||
|
|
timer->start();
|
||
|
|
QString status = "Got Reply on: " + QDateTime::currentDateTime().toString("hh:mm:ss") + "; Waiting to Send.";
|
||
|
|
setStatus(status);
|
||
|
|
}
|
||
|
|
|
||
|
|
QString RequestWorker::status() const
|
||
|
|
{
|
||
|
|
return m_status;
|
||
|
|
}
|
||
|
|
|
||
|
|
void RequestWorker::setStatus(const QString &newStatus)
|
||
|
|
{
|
||
|
|
if (m_status == newStatus)
|
||
|
|
return;
|
||
|
|
m_status = newStatus;
|
||
|
|
emit statusChanged();
|
||
|
|
}
|