2026-08-04 21:31:40 +05:30
|
|
|
#include "requestworker.h"
|
|
|
|
|
#include <QJsonArray>
|
|
|
|
|
#include <QJsonValue>
|
|
|
|
|
#include <QJsonObject>
|
|
|
|
|
#include <QJsonDocument>
|
2026-08-12 18:22:15 +05:30
|
|
|
#include <QFile>
|
2026-08-04 21:31:40 +05:30
|
|
|
|
|
|
|
|
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();
|
2026-08-12 18:22:15 +05:30
|
|
|
this->alarm_thres = config["alert"].toInt();
|
2026-08-04 21:31:40 +05:30
|
|
|
qDebug() << "In cpp config values are: ";
|
|
|
|
|
qDebug() << "Url: " << url;
|
|
|
|
|
qDebug() << "Interval: " << interval;
|
|
|
|
|
qDebug() << "Max Height: " << max_height;
|
|
|
|
|
qDebug() << "Min Height: " << min_height;
|
2026-08-12 18:22:15 +05:30
|
|
|
qDebug() << "Alert: " << alarm_thres;
|
2026-08-04 21:31:40 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
2026-08-12 14:32:54 +05:30
|
|
|
qDebug() << "Stopping" << timer->isActive();
|
2026-08-04 21:31:40 +05:30
|
|
|
}
|
|
|
|
|
|
2026-08-12 18:22:15 +05:30
|
|
|
void RequestWorker::playAlarm()
|
|
|
|
|
{
|
|
|
|
|
if(player_started)
|
|
|
|
|
return;
|
|
|
|
|
if(player) {
|
|
|
|
|
player->deleteLater();
|
|
|
|
|
player = nullptr;
|
|
|
|
|
}
|
|
|
|
|
if(audioOutput) {
|
|
|
|
|
audioOutput->deleteLater();
|
|
|
|
|
audioOutput->deleteLater();
|
|
|
|
|
}
|
|
|
|
|
qDebug() << "Loading player";
|
|
|
|
|
player = new QMediaPlayer(this);
|
|
|
|
|
audioOutput = new QAudioOutput(this);
|
|
|
|
|
player->setAudioOutput(audioOutput);
|
|
|
|
|
if (QFile::exists(":/sounds/echo.mp3")) {
|
|
|
|
|
qDebug() << "Resource found successfully!";
|
|
|
|
|
} else {
|
|
|
|
|
qDebug() << "Resource not found. Check CMakeLists.txt or .pro file!";
|
|
|
|
|
}
|
|
|
|
|
player->setSource(QUrl("qrc:/sounds/echo.mp3"));
|
|
|
|
|
audioOutput->setVolume(0.5);
|
|
|
|
|
connect(this, &RequestWorker::playMedia, player, &QMediaPlayer::play);
|
|
|
|
|
player_started = true;
|
|
|
|
|
emit playMedia();
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-04 21:31:40 +05:30
|
|
|
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);
|
2026-08-12 14:32:54 +05:30
|
|
|
if(data.size() == 0) {
|
|
|
|
|
prev_time = now;
|
|
|
|
|
timer->start();
|
|
|
|
|
emit requestFailed();
|
|
|
|
|
emit setStatus("Invalid Response; Resending Request");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2026-08-04 21:31:40 +05:30
|
|
|
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);
|
2026-08-12 14:32:54 +05:30
|
|
|
float rate_of_fill = ( float )(prev_distance - water_level) /(float) time_in_microseconds;
|
2026-08-04 21:31:40 +05:30
|
|
|
rate_of_fill = rate_of_fill * 1'000'000;
|
2026-08-12 14:58:48 +05:30
|
|
|
int expected_to_fill = (water_level - min_height) / rate_of_fill;
|
2026-08-12 18:22:15 +05:30
|
|
|
int percentage_filled = (float (max_height - water_level) / float (max_height)) *100;
|
|
|
|
|
if(enable_alarm) {
|
|
|
|
|
qDebug() << "Alarm enabled";
|
|
|
|
|
if(percentage_filled >= alarm_thres) {
|
|
|
|
|
playAlarm();
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-08-04 21:31:40 +05:30
|
|
|
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;
|
2026-08-12 18:22:15 +05:30
|
|
|
qDebug() << "Percentage filled: " << percentage_filled;
|
2026-08-04 21:31:40 +05:30
|
|
|
qDebug() << "Time difference: " << time_in_microseconds;
|
|
|
|
|
prev_distance = water_level;
|
2026-08-12 14:32:54 +05:30
|
|
|
emit networkReply(data_water_level, rate_of_fill, expected_to_fill);
|
2026-08-04 21:31:40 +05:30
|
|
|
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();
|
|
|
|
|
}
|
2026-08-12 18:22:15 +05:30
|
|
|
|
|
|
|
|
void RequestWorker::startAlarm()
|
|
|
|
|
{
|
|
|
|
|
enable_alarm = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RequestWorker::stopAlarm()
|
|
|
|
|
{
|
|
|
|
|
enable_alarm = false;
|
|
|
|
|
player_started = false;
|
|
|
|
|
if(player) {
|
|
|
|
|
player->stop();
|
|
|
|
|
player->deleteLater();
|
|
|
|
|
player = nullptr;
|
|
|
|
|
}
|
|
|
|
|
if(audioOutput) {
|
|
|
|
|
audioOutput->deleteLater();
|
|
|
|
|
audioOutput = nullptr;
|
|
|
|
|
}
|
|
|
|
|
}
|