Compare commits

..
6 changed files with 54 additions and 25 deletions

View file

@ -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;
}
}

View file

@ -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,9 +119,16 @@ Window {
bottomMargin: 50
}
onConnect: {
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) {

View file

@ -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

View file

@ -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)

View file

@ -48,6 +48,7 @@ void RequestWorker::stop()
if(timer->isActive())
timer->stop();
stop_timer = true;
qDebug() << "Stopping" << timer->isActive();
}
void RequestWorker::makeRequest()
@ -70,13 +71,20 @@ void RequestWorker::requestFinished(QNetworkReply *rep)
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();
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;
int expected_to_fill = (water_level - min_height) / rate_of_fill;
if(reply_number == 0) {
rate_of_fill = 0;
expected_to_fill = 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;

View file

@ -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: