This commit is contained in:
TheUnrealDragon 2026-04-09 14:26:21 +05:30
commit 26d3fce10a
3 changed files with 39 additions and 7 deletions

View file

@ -554,7 +554,9 @@ Window {
else {
water_level.color = root.water_color;
}
if(root.distance_count > 1000) {
root.distance_count = 5;
}
console.log("Current reading is: " + current_reading)
console.log("Normalized height is: " + normalized_height)
water_level.current_level = normalized_height;

View file

@ -23,6 +23,7 @@ import java.time.Duration;
public class WaterLevelService extends Service {
private static float alert_thres_distance = 1000000;
private boolean bits_flipped = false;
public class UDPCommandClient implements Runnable {
private DatagramSocket socket_cmd_recv;
@ -40,13 +41,25 @@ public class WaterLevelService extends Service {
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
socket_cmd_recv.receive(packet);
String str = new String(buffer, StandardCharsets.UTF_8);
Log.d("UDP Service ", "Received command packet " + str);
if(str.startsWith("<SET NAME=ALERT VALUE=")) {
str = str.replace("<SET NAME=ALERT VALUE=", "");
str = str.replace("=", "");
str = str.replace(">", "");
alert_thres_distance = Float.parseFloat(str);
}
Log.d("UDP Service ", "Received command packet " + str);
else if(str.startsWith("<SET NAME=BITS_FLIPPED VALUE=")) {
str = str.replace("<SET NAME=BITS_FLIPPED VALUE=", "");
str = str.replace("=", "");
str = str.replace(">", "");
Log.d("UDP Service ", "Flipping " + str);
if(str.toLowerCase() == "false") {
bits_flipped = false;
}
else if(str.toLowerCase() == "true") {
bits_flipped = true;
}
}
}
}
@ -105,7 +118,13 @@ public class WaterLevelService extends Service {
} catch(java.net.SocketTimeoutException e) {
continue;
}
float f = ByteBuffer.wrap(buffer).order(ByteOrder.LITTLE_ENDIAN).getFloat();
float f;
if(bits_flipped) {
f = ByteBuffer.wrap(buffer).order(ByteOrder.LITTLE_ENDIAN).getFloat();
}
else {
f = ByteBuffer.wrap(buffer).order(ByteOrder.BIG_ENDIAN).getFloat();
}
Log.d("UDP Service ", "Received packet: " + f);
Log.d("UDP Service ", "Threshold is: " + alert_thres_distance);
boolean duration_passed = false;

View file

@ -61,16 +61,27 @@ void UDPListener::setConfig(float distance, bool flip_bits)
command_socket = new QUdpSocket(this);
bool bind = command_socket->bind(8181);
if(bind) {
QNetworkDatagram data;
data.setDestination(QHostAddress::LocalHost, 8178);
QNetworkDatagram data1, data2;
data1.setDestination(QHostAddress::LocalHost, 8178);
data2.setDestination(QHostAddress::LocalHost, 8178);
QString s_data = "<SET NAME=ALERT VALUE=" + QString::number(distance) + ">";
while(s_data.toUtf8().size() != 1024) {
s_data += "=";
}
data.setData(s_data.toUtf8());
command_socket->writeDatagram(data);
data1.setData(s_data.toUtf8());
command_socket->writeDatagram(data1);
command_socket->flush();
QString b_data = "<SET NAME=BITS_FLIPPED VALUE=" + QString((flip_bits ? "TRUE" : "FALSE")) + ">";
while(b_data.toUtf8().size() != 1024) {
b_data += "=";
}
data2.setData(b_data.toUtf8());
command_socket->writeDatagram(data2);
command_socket->flush();
command_socket->close();
command_socket->deleteLater();
command_socket = nullptr;
}
}
averages.clear();