WaterLevelSensor/requestworker.h

69 lines
1.7 KiB
C
Raw Normal View History

2026-08-04 21:31:40 +05:30
#ifndef REQUESTWORKER_H
#define REQUESTWORKER_H
#include <QObject>
#include <QQmlEngine>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
2026-08-12 18:22:15 +05:30
#include <QMediaPlayer>
#include <QAudioOutput>
2026-08-04 21:31:40 +05:30
#include <QTimer>
#include <QThread>
#include <chrono>
2026-09-14 15:31:21 +05:30
#ifdef Q_OS_ANDROID
#include "wifihelper.h"
#endif
2026-08-04 21:31:40 +05:30
class RequestWorker : public QObject
{
Q_OBJECT
QML_ELEMENT
Q_PROPERTY(QString status READ status WRITE setStatus NOTIFY statusChanged FINAL)
QNetworkAccessManager *manager = nullptr;
QTimer *timer = nullptr;
QString url;
int interval;
float prev_distance = 0;
int max_height = 0;
int min_height = 0;
bool stop_timer = false;
int reply_number = 0;
2026-08-12 18:22:15 +05:30
int alarm_thres = 0;
bool enable_alarm = false;
bool player_started = false;
QMediaPlayer *player = nullptr;
QAudioOutput *audioOutput = nullptr;
2026-09-14 15:31:21 +05:30
#ifdef Q_OS_ANDROID
WiFiHelper *wifi = nullptr;
QTimer *poll_wifi = nullptr;
#endif
2026-08-04 21:31:40 +05:30
std::chrono::time_point<std::chrono::high_resolution_clock> prev_time;
public:
explicit RequestWorker(QObject *parent = nullptr);
QString status() const;
void setStatus(const QString &newStatus);
signals:
void requestFailed();
void networkReply(int distance, float rate_of_fill, int expected_to_fill);
2026-08-04 21:31:40 +05:30
void statusChanged();
2026-08-12 18:22:15 +05:30
void playMedia();
2026-09-14 15:31:21 +05:30
void connectionText(QString text, bool errored = false);
2026-08-04 21:31:40 +05:30
public slots:
2026-08-12 18:22:15 +05:30
void startAlarm();
void stopAlarm();
2026-08-04 21:31:40 +05:30
void setConfig(QJsonObject config);
void start();
void stop();
private slots:
2026-09-14 15:31:21 +05:30
void pollWiFi();
void gotWiFiSSID(QString ssid);
2026-08-12 18:22:15 +05:30
void playAlarm();
2026-08-04 21:31:40 +05:30
void makeRequest();
void requestFinished(QNetworkReply *rep);
private:
QString m_status;
};
#endif // REQUESTWORKER_H