From e1d57eae3288f7d980041724e13538d4e0718c12 Mon Sep 17 00:00:00 2001 From: unrealdragon Date: Wed, 12 Aug 2026 14:32:54 +0530 Subject: [PATCH 1/2] Http Version - Fix to show error when requests fail, and changed display type of distance in UI from float to Int --- ConnectButton.qml | 12 ++++++++++-- Main.qml | 27 +++++++++++++++++++-------- SettingsPopup.qml | 4 ++-- WaterTank.qml | 19 ++++++++++--------- requestworker.cpp | 12 ++++++++++-- requestworker.h | 3 ++- 6 files changed, 53 insertions(+), 24 deletions(-) diff --git a/ConnectButton.qml b/ConnectButton.qml index 9225e44..dd058bc 100644 --- a/ConnectButton.qml +++ b/ConnectButton.qml @@ -10,6 +10,8 @@ Rectangle { property int font_enlarged_text_size: 20 property int font_normal_text_size: 16 property string text: "Connect" + property string text2: "Stop" + property int mode: 0 signal connect; layer.enabled: true layer.effect: MultiEffect { @@ -20,7 +22,7 @@ Rectangle { anchors.centerIn: parent font.pointSize: root.font_enlarged_text_size color: root.font_color - text: root.text + text: mode ? root.text2 : root.text horizontalAlignment: Text.AlignHCenter } @@ -46,10 +48,16 @@ Rectangle { settings_rect_pressed_prev = root.color root.color = "#5A1F4F1F" root.connect(); + switchMode(); } onReleased: { root.color = settings_rect_pressed_prev } } - + function switchMode() { + if(root.mode === 0) + root.mode = 1 + else + root.mode = 0; + } } diff --git a/Main.qml b/Main.qml index db445e4..950a2b9 100644 --- a/Main.qml +++ b/Main.qml @@ -39,10 +39,10 @@ Window { color: "#0A0F0F0F" SettingsBarIcon { height: top_bar.height - 20 - width: top_bar.width / 12 + width: height anchors { top: parent.top - left: parent.left + right: parent.right bottomMargin: 5 topMargin: 5 leftMargin: 5 @@ -77,10 +77,10 @@ Window { anchors { top: top_bar.bottom horizontalCenter: parent.horizontalCenter - topMargin: 0 + topMargin: 100 } - height: (parent.height - top_bar.height) / 1.2 - width: parent.width / 1.2 + height: (parent.height - top_bar.height) / 2 + width: parent.width / 2.2 base_color: root.background_color distance: 0 tank_lid_color: "#F0121212" @@ -119,8 +119,15 @@ Window { bottomMargin: 50 } onConnect: { - requester.start(); - root.status = "Waiting to Send."; + if(connect_button.mode === 0) + { + requester.start(); + root.status = "Waiting to Send."; + } + else { + root.status = "Stopped."; + requester.stop(); + } } } } @@ -134,6 +141,10 @@ Window { tank.setDistance(water_level); tank.rate_of_fill = fill_rate; tank.expected_to_fill = expected; + status.color = root.accent_color; + } + onRequestFailed: () => { + status.color = root.danger_color; } } @@ -145,7 +156,7 @@ Window { console.log("Loading initial config"); setConfig(config); if(root.status === "Initializing") { - root.status = "Waiting to connect."; + root.status = "Press Connect."; } } function setConfig(config) { diff --git a/SettingsPopup.qml b/SettingsPopup.qml index 051c306..41f5cc7 100644 --- a/SettingsPopup.qml +++ b/SettingsPopup.qml @@ -23,8 +23,8 @@ Popup { border.color: settings_popup.accent_color border.width: 1 } - height: Math.max(parent.height / 1.2, contentHeight) - width: parent.width / 1.1 + height: Math.max(parent.height / 1.6, contentHeight) + width: parent.width / 1.2 anchors.centerIn: Overlay.overlay Rectangle { id: settings_base diff --git a/WaterTank.qml b/WaterTank.qml index 5c1b5af..dc264f1 100644 --- a/WaterTank.qml +++ b/WaterTank.qml @@ -14,7 +14,7 @@ Item { property double height_max: tank_base.height - 10 property double current_level: 0 property double percentage_filled: 0 - property double distance: 0 + property int distance: 0 property double min_thres_input: 0 property double max_thres_input: 0 property double rate_of_fill: 0 @@ -33,16 +33,16 @@ Item { bottomMargin: -5 } radius: 10 - width: parent.width / 4 - height: parent.height / 15 + width: tank_base.width / 3 + height: tank_base.height / 15 } Rectangle { id: tank_base color: root.tank_base_color anchors.centerIn: parent - width: parent.width / 2 - height: parent.height / 2 + width: parent.width + height: parent.height border.color: root.tank_base_border_color border.width: 5 radius: 4 @@ -72,7 +72,7 @@ Item { Text { id: water_level_reading - text: "Current Reading: " + root.distance + " cm" + text: "Current Reading: " + (root.distance / 10)+ " cm" font.pointSize: 16 color: root.text_color anchors { @@ -125,7 +125,7 @@ Item { text: "Expected to fill in: " + root.expected_to_fill + " s" font.pointSize: 16 color: root.text_color - visible: (root.rate_of_fill > 1) ? true : false + visible: (root.rate_of_fill > 0) ? true : false anchors { margins: 5 top: rate_of_fill_label.bottom @@ -141,10 +141,11 @@ Item { function setDistance(distance) { root.distance = distance; - console.log("Distance changed to: ", distance); + let distance_f = distance / 10; + console.log("Distance changed to: ", distance_f); let current_min_reading = Math.max(0, min_thres_input); let effective_max_reading = Math.max(0, max_thres_input) - current_min_reading; - let current_reading = Math.max(0, root.distance - current_min_reading); + let current_reading = Math.max(0, distance_f - current_min_reading); console.log("Current reading is: " + current_reading) console.log("Current min reading is: " + current_min_reading) console.log("Effective max reading is: " + effective_max_reading) diff --git a/requestworker.cpp b/requestworker.cpp index eff8079..be64b2f 100644 --- a/requestworker.cpp +++ b/requestworker.cpp @@ -48,6 +48,7 @@ void RequestWorker::stop() if(timer->isActive()) timer->stop(); stop_timer = true; + qDebug() << "Stopping" << timer->isActive(); } void RequestWorker::makeRequest() @@ -70,11 +71,18 @@ void RequestWorker::requestFinished(QNetworkReply *rep) rep = nullptr; auto now = std::chrono::high_resolution_clock::now(); auto time_diff = std::chrono::duration_cast(now - prev_time); + if(data.size() == 0) { + prev_time = now; + timer->start(); + emit requestFailed(); + emit setStatus("Invalid Response; Resending Request"); + return; + } 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; + 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 = (max_height - water_level) / rate_of_fill; if(reply_number == 0) { @@ -87,7 +95,7 @@ void RequestWorker::requestFinished(QNetworkReply *rep) 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); + emit networkReply(data_water_level, rate_of_fill, expected_to_fill); if(stop_timer) { setStatus("Timer stopped."); return; diff --git a/requestworker.h b/requestworker.h index 05fbafa..37d0888 100644 --- a/requestworker.h +++ b/requestworker.h @@ -32,7 +32,8 @@ public: void setStatus(const QString &newStatus); signals: - void networkReply(float distance, float rate_of_fill, int expected_to_fill); + void requestFailed(); + void networkReply(int distance, float rate_of_fill, int expected_to_fill); void statusChanged(); public slots: From 99bc9209af266aab2af1652a5360e1ada3d0b0af Mon Sep 17 00:00:00 2001 From: unrealdragon Date: Wed, 12 Aug 2026 14:58:48 +0530 Subject: [PATCH 2/2] Http Version - Fix to calculate the fill rate. --- requestworker.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requestworker.cpp b/requestworker.cpp index be64b2f..649d515 100644 --- a/requestworker.cpp +++ b/requestworker.cpp @@ -84,7 +84,7 @@ void RequestWorker::requestFinished(QNetworkReply *rep) float water_level = (float) ((float) (data_water_level) / 10.0); 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 = (max_height - water_level) / rate_of_fill; + int expected_to_fill = (water_level - min_height) / rate_of_fill; if(reply_number == 0) { rate_of_fill = 0; expected_to_fill = 0;