Http Version - Fix to show error when requests fail, and changed display type of distance in UI from float to Int

This commit is contained in:
unrealdragon 2026-08-12 14:32:54 +05:30
commit e1d57eae32
6 changed files with 53 additions and 24 deletions

View file

@ -10,6 +10,8 @@ Rectangle {
property int font_enlarged_text_size: 20 property int font_enlarged_text_size: 20
property int font_normal_text_size: 16 property int font_normal_text_size: 16
property string text: "Connect" property string text: "Connect"
property string text2: "Stop"
property int mode: 0
signal connect; signal connect;
layer.enabled: true layer.enabled: true
layer.effect: MultiEffect { layer.effect: MultiEffect {
@ -20,7 +22,7 @@ Rectangle {
anchors.centerIn: parent anchors.centerIn: parent
font.pointSize: root.font_enlarged_text_size font.pointSize: root.font_enlarged_text_size
color: root.font_color color: root.font_color
text: root.text text: mode ? root.text2 : root.text
horizontalAlignment: Text.AlignHCenter horizontalAlignment: Text.AlignHCenter
} }
@ -46,10 +48,16 @@ Rectangle {
settings_rect_pressed_prev = root.color settings_rect_pressed_prev = root.color
root.color = "#5A1F4F1F" root.color = "#5A1F4F1F"
root.connect(); root.connect();
switchMode();
} }
onReleased: { onReleased: {
root.color = settings_rect_pressed_prev root.color = settings_rect_pressed_prev
} }
} }
function switchMode() {
if(root.mode === 0)
root.mode = 1
else
root.mode = 0;
}
} }

View file

@ -39,10 +39,10 @@ Window {
color: "#0A0F0F0F" color: "#0A0F0F0F"
SettingsBarIcon { SettingsBarIcon {
height: top_bar.height - 20 height: top_bar.height - 20
width: top_bar.width / 12 width: height
anchors { anchors {
top: parent.top top: parent.top
left: parent.left right: parent.right
bottomMargin: 5 bottomMargin: 5
topMargin: 5 topMargin: 5
leftMargin: 5 leftMargin: 5
@ -77,10 +77,10 @@ Window {
anchors { anchors {
top: top_bar.bottom top: top_bar.bottom
horizontalCenter: parent.horizontalCenter horizontalCenter: parent.horizontalCenter
topMargin: 0 topMargin: 100
} }
height: (parent.height - top_bar.height) / 1.2 height: (parent.height - top_bar.height) / 2
width: parent.width / 1.2 width: parent.width / 2.2
base_color: root.background_color base_color: root.background_color
distance: 0 distance: 0
tank_lid_color: "#F0121212" tank_lid_color: "#F0121212"
@ -119,9 +119,16 @@ Window {
bottomMargin: 50 bottomMargin: 50
} }
onConnect: { onConnect: {
if(connect_button.mode === 0)
{
requester.start(); requester.start();
root.status = "Waiting to Send."; root.status = "Waiting to Send.";
} }
else {
root.status = "Stopped.";
requester.stop();
}
}
} }
} }
@ -134,6 +141,10 @@ Window {
tank.setDistance(water_level); tank.setDistance(water_level);
tank.rate_of_fill = fill_rate; tank.rate_of_fill = fill_rate;
tank.expected_to_fill = expected; 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"); console.log("Loading initial config");
setConfig(config); setConfig(config);
if(root.status === "Initializing") { if(root.status === "Initializing") {
root.status = "Waiting to connect."; root.status = "Press Connect.";
} }
} }
function setConfig(config) { function setConfig(config) {

View file

@ -23,8 +23,8 @@ Popup {
border.color: settings_popup.accent_color border.color: settings_popup.accent_color
border.width: 1 border.width: 1
} }
height: Math.max(parent.height / 1.2, contentHeight) height: Math.max(parent.height / 1.6, contentHeight)
width: parent.width / 1.1 width: parent.width / 1.2
anchors.centerIn: Overlay.overlay anchors.centerIn: Overlay.overlay
Rectangle { Rectangle {
id: settings_base id: settings_base

View file

@ -14,7 +14,7 @@ Item {
property double height_max: tank_base.height - 10 property double height_max: tank_base.height - 10
property double current_level: 0 property double current_level: 0
property double percentage_filled: 0 property double percentage_filled: 0
property double distance: 0 property int distance: 0
property double min_thres_input: 0 property double min_thres_input: 0
property double max_thres_input: 0 property double max_thres_input: 0
property double rate_of_fill: 0 property double rate_of_fill: 0
@ -33,16 +33,16 @@ Item {
bottomMargin: -5 bottomMargin: -5
} }
radius: 10 radius: 10
width: parent.width / 4 width: tank_base.width / 3
height: parent.height / 15 height: tank_base.height / 15
} }
Rectangle { Rectangle {
id: tank_base id: tank_base
color: root.tank_base_color color: root.tank_base_color
anchors.centerIn: parent anchors.centerIn: parent
width: parent.width / 2 width: parent.width
height: parent.height / 2 height: parent.height
border.color: root.tank_base_border_color border.color: root.tank_base_border_color
border.width: 5 border.width: 5
radius: 4 radius: 4
@ -72,7 +72,7 @@ Item {
Text { Text {
id: water_level_reading id: water_level_reading
text: "Current Reading: " + root.distance + " cm" text: "Current Reading: " + (root.distance / 10)+ " cm"
font.pointSize: 16 font.pointSize: 16
color: root.text_color color: root.text_color
anchors { anchors {
@ -125,7 +125,7 @@ Item {
text: "Expected to fill in: " + root.expected_to_fill + " s" text: "Expected to fill in: " + root.expected_to_fill + " s"
font.pointSize: 16 font.pointSize: 16
color: root.text_color color: root.text_color
visible: (root.rate_of_fill > 1) ? true : false visible: (root.rate_of_fill > 0) ? true : false
anchors { anchors {
margins: 5 margins: 5
top: rate_of_fill_label.bottom top: rate_of_fill_label.bottom
@ -141,10 +141,11 @@ Item {
function setDistance(distance) { function setDistance(distance) {
root.distance = 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 current_min_reading = Math.max(0, min_thres_input);
let effective_max_reading = Math.max(0, max_thres_input) - current_min_reading; 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 reading is: " + current_reading)
console.log("Current min reading is: " + current_min_reading) console.log("Current min reading is: " + current_min_reading)
console.log("Effective max reading is: " + effective_max_reading) console.log("Effective max reading is: " + effective_max_reading)

View file

@ -48,6 +48,7 @@ void RequestWorker::stop()
if(timer->isActive()) if(timer->isActive())
timer->stop(); timer->stop();
stop_timer = true; stop_timer = true;
qDebug() << "Stopping" << timer->isActive();
} }
void RequestWorker::makeRequest() void RequestWorker::makeRequest()
@ -70,11 +71,18 @@ void RequestWorker::requestFinished(QNetworkReply *rep)
rep = nullptr; rep = nullptr;
auto now = std::chrono::high_resolution_clock::now(); auto now = std::chrono::high_resolution_clock::now();
auto time_diff = std::chrono::duration_cast<std::chrono::microseconds>(now - prev_time); auto time_diff = std::chrono::duration_cast<std::chrono::microseconds>(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(); unsigned int time_in_microseconds = time_diff.count();
QJsonObject jdata = QJsonDocument::fromJson(data).object(); QJsonObject jdata = QJsonDocument::fromJson(data).object();
auto data_water_level = jdata["water_level"].toInt(); auto data_water_level = jdata["water_level"].toInt();
float water_level = (float) ((float) (data_water_level) / 10.0); 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; rate_of_fill = rate_of_fill * 1'000'000;
int expected_to_fill = (max_height - water_level) / rate_of_fill; int expected_to_fill = (max_height - water_level) / rate_of_fill;
if(reply_number == 0) { if(reply_number == 0) {
@ -87,7 +95,7 @@ void RequestWorker::requestFinished(QNetworkReply *rep)
qDebug() << "Got expected to fill: " << expected_to_fill; qDebug() << "Got expected to fill: " << expected_to_fill;
qDebug() << "Time difference: " << time_in_microseconds; qDebug() << "Time difference: " << time_in_microseconds;
prev_distance = water_level; 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) { if(stop_timer) {
setStatus("Timer stopped."); setStatus("Timer stopped.");
return; return;

View file

@ -32,7 +32,8 @@ public:
void setStatus(const QString &newStatus); void setStatus(const QString &newStatus);
signals: 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(); void statusChanged();
public slots: public slots: