QoL Fixes and Changes
This commit is contained in:
parent
b8201398e1
commit
93995959d0
1 changed files with 905 additions and 229 deletions
|
|
@ -4,6 +4,7 @@
|
|||
#include <QJsonObject>
|
||||
#include <QJsonDocument>
|
||||
#include <QFile>
|
||||
#include <QStandardPaths>
|
||||
|
||||
RequestWorker::RequestWorker(QObject *parent)
|
||||
: QObject{parent}
|
||||
|
|
@ -63,6 +64,30 @@ void RequestWorker::stop()
|
|||
qDebug() << "Stopping" << timer->isActive();
|
||||
}
|
||||
|
||||
QString RequestWorker::getRequestResponseString(QNetworkReply *rep, QString rep_data)
|
||||
{
|
||||
if(!rep) {
|
||||
return "No Reply";
|
||||
}
|
||||
QNetworkRequest req = rep->request();
|
||||
QString errorString = rep->errorString();
|
||||
QString resp = "GET " + req.url().toString() + "\n";
|
||||
for(auto &key: req.rawHeaderList()) {
|
||||
QByteArray value = req.rawHeader(key);
|
||||
resp += key + ": " + value + "\n";
|
||||
}
|
||||
qDebug() << "Raw header list size: " << req.rawHeaderList().size();
|
||||
QString ret;
|
||||
ret = "Request:\n";
|
||||
ret += resp;
|
||||
ret += "Response:\n";
|
||||
if(rep_data == "")
|
||||
ret += errorString;
|
||||
else
|
||||
ret += rep_data;
|
||||
return ret;
|
||||
}
|
||||
|
||||
void RequestWorker::pollWiFi()
|
||||
{
|
||||
#ifdef Q_OS_ANDROID
|
||||
|
|
@ -90,8 +115,53 @@ void RequestWorker::gotWiFiSSID(QString ssid)
|
|||
|
||||
void RequestWorker::playAlarm()
|
||||
{
|
||||
if(player_started)
|
||||
if (player_started)
|
||||
return;
|
||||
|
||||
// Step 1: Copy the qrc resource to a local cache file so Android can read it
|
||||
#ifdef Q_OS_ANDROID
|
||||
QString qrcPath = ":/sounds/echo.mp3";
|
||||
QString cachePath = QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + "/echo.mp3";
|
||||
|
||||
if (QFile::exists(cachePath)) {
|
||||
QFile::remove(cachePath);
|
||||
}
|
||||
|
||||
if (QFile::copy(qrcPath, cachePath)) {
|
||||
qDebug() << "Successfully copied resource to cache:" << cachePath;
|
||||
} else {
|
||||
qDebug() << "Failed to copy resource to cache path!";
|
||||
return;
|
||||
}
|
||||
|
||||
// Step 2: Use Android JNI to initialize and play via android.media.MediaPlayer
|
||||
QJniEnvironment env;
|
||||
|
||||
// Create new android.media.MediaPlayer()
|
||||
QJniObject mediaPlayer("android/media/MediaPlayer");
|
||||
if (!mediaPlayer.isValid()) {
|
||||
qDebug() << "Failed to create native Android MediaPlayer!";
|
||||
return;
|
||||
}
|
||||
|
||||
// Set data source using the local cache file path
|
||||
QJniObject jPath = QJniObject::fromString(cachePath);
|
||||
mediaPlayer.callMethod<void>("setDataSource", "(Ljava/lang/String;)V", jPath.object<jstring>());
|
||||
|
||||
// Enable looping
|
||||
mediaPlayer.callMethod<void>("setLooping", "(Z)V", true);
|
||||
|
||||
// Prepare and start playback
|
||||
mediaPlayer.callMethod<void>("prepare");
|
||||
mediaPlayer.callMethod<void>("start");
|
||||
|
||||
// Store a global reference so it doesn't get garbage collected immediately
|
||||
m_androidMediaPlayer = new QJniObject(mediaPlayer.object());
|
||||
|
||||
player_started = true;
|
||||
qDebug() << "Native Android alarm started successfully.";
|
||||
return;
|
||||
#endif
|
||||
if(player) {
|
||||
player->deleteLater();
|
||||
player = nullptr;
|
||||
|
|
@ -122,6 +192,7 @@ void RequestWorker::makeRequest()
|
|||
if(stop_timer)
|
||||
return;
|
||||
QNetworkRequest req;
|
||||
req.setRawHeader("Testing", "Something");
|
||||
req.setUrl(this->url);
|
||||
disconnect(manager, nullptr, nullptr, nullptr);
|
||||
connect(manager, &QNetworkAccessManager::finished, this, &RequestWorker::requestFinished);
|
||||
|
|
@ -133,15 +204,16 @@ void RequestWorker::makeRequest()
|
|||
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);
|
||||
if(data.size() == 0) {
|
||||
prev_time = now;
|
||||
timer->start();
|
||||
emit requestFailed();
|
||||
QString request_response_string = getRequestResponseString(rep);
|
||||
emit requestFailed(request_response_string);
|
||||
emit setStatus("Invalid Response; Resending Request");
|
||||
rep->deleteLater();
|
||||
rep = nullptr;
|
||||
return;
|
||||
}
|
||||
unsigned int time_in_microseconds = time_diff.count();
|
||||
|
|
@ -151,10 +223,10 @@ 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;
|
||||
int percentage_filled = (float (max_height - water_level) / float (max_height)) * 100;
|
||||
if(enable_alarm) {
|
||||
qDebug() << "Alarm enabled";
|
||||
if(percentage_filled >= alarm_thres) {
|
||||
if(water_level <= alarm_thres) {
|
||||
playAlarm();
|
||||
}
|
||||
}
|
||||
|
|
@ -169,14 +241,19 @@ void RequestWorker::requestFinished(QNetworkReply *rep)
|
|||
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);
|
||||
QString timestamp = QDateTime::currentDateTime().toString("hh:mm:ss");
|
||||
QString request_response_string = getRequestResponseString(rep, data);
|
||||
request_response_string += "Timestamp:\n" + timestamp;
|
||||
rep->deleteLater();
|
||||
rep = nullptr;
|
||||
emit networkReply(data_water_level, rate_of_fill, expected_to_fill, request_response_string);
|
||||
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.";
|
||||
QString status = "Got Reply on: " + timestamp + "; Waiting to Send.";
|
||||
setStatus(status);
|
||||
}
|
||||
|
||||
|
|
@ -211,4 +288,10 @@ void RequestWorker::stopAlarm()
|
|||
audioOutput->deleteLater();
|
||||
audioOutput = nullptr;
|
||||
}
|
||||
#ifdef Q_OS_ANDROID
|
||||
if(m_androidMediaPlayer) {
|
||||
delete m_androidMediaPlayer;
|
||||
m_androidMediaPlayer = nullptr;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue