Http Version - Added Alarm Button.

This commit is contained in:
unrealdragon 2026-08-12 18:22:15 +05:30
commit 07bd73bb33

View file

@ -3,6 +3,7 @@
#include <QJsonValue>
#include <QJsonObject>
#include <QJsonDocument>
#include <QFile>
RequestWorker::RequestWorker(QObject *parent)
: QObject{parent}
@ -16,11 +17,13 @@ void RequestWorker::setConfig(QJsonObject config)
this->interval = config["interval"].toInt();
this->max_height = config["max"].toInt();
this->min_height = config["min"].toInt();
this->alarm_thres = config["alert"].toInt();
qDebug() << "In cpp config values are: ";
qDebug() << "Url: " << url;
qDebug() << "Interval: " << interval;
qDebug() << "Max Height: " << max_height;
qDebug() << "Min Height: " << min_height;
qDebug() << "Alert: " << alarm_thres;
}
void RequestWorker::start()
@ -51,6 +54,34 @@ void RequestWorker::stop()
qDebug() << "Stopping" << timer->isActive();
}
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();
}
void RequestWorker::makeRequest()
{
if(stop_timer)
@ -85,6 +116,13 @@ void RequestWorker::requestFinished(QNetworkReply *rep)
float rate_of_fill = ( float )(prev_distance - water_level) /(float) time_in_microseconds;
rate_of_fill = rate_of_fill * 1'000'000;
int expected_to_fill = (water_level - min_height) / rate_of_fill;
int percentage_filled = (float (max_height - water_level) / float (max_height)) *100;
if(enable_alarm) {
qDebug() << "Alarm enabled";
if(percentage_filled >= alarm_thres) {
playAlarm();
}
}
if(reply_number == 0) {
rate_of_fill = 0;
expected_to_fill = 0;
@ -93,6 +131,7 @@ void RequestWorker::requestFinished(QNetworkReply *rep)
qDebug() << "Got water level: " << water_level;
qDebug() << "Got rate of fill: " << rate_of_fill;
qDebug() << "Got expected to fill: " << expected_to_fill;
qDebug() << "Percentage filled: " << percentage_filled;
qDebug() << "Time difference: " << time_in_microseconds;
prev_distance = water_level;
emit networkReply(data_water_level, rate_of_fill, expected_to_fill);
@ -118,3 +157,23 @@ void RequestWorker::setStatus(const QString &newStatus)
m_status = newStatus;
emit statusChanged();
}
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;
}
}