v0.0.7
This commit is contained in:
parent
26d3fce10a
commit
984c5acacc
1 changed files with 376 additions and 37 deletions
|
|
@ -19,11 +19,18 @@ import org.qtproject.qt.android.bindings.QtActivity;
|
|||
import android.app.Service;
|
||||
import java.time.Instant;
|
||||
import java.time.Duration;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.InputStreamReader;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
import org.json.JSONArray;
|
||||
|
||||
public class WaterLevelService extends Service {
|
||||
|
||||
private static float alert_thres_distance = 1000000;
|
||||
private boolean bits_flipped = false;
|
||||
private String mode = "";
|
||||
private String url = "";
|
||||
|
||||
public class UDPCommandClient implements Runnable {
|
||||
private DatagramSocket socket_cmd_recv;
|
||||
|
|
@ -60,7 +67,20 @@ public class WaterLevelService extends Service {
|
|||
bits_flipped = true;
|
||||
}
|
||||
}
|
||||
|
||||
else if(str.startsWith("<SET NAME=MODE VALUE=")) {
|
||||
str = str.replace("<SET NAME=MODE VALUE=", "");
|
||||
str = str.replace("=", "");
|
||||
str = str.replace(">", "");
|
||||
Log.d("UDP Service ", "Mode " + str);
|
||||
mode = str.toLowerCase();
|
||||
}
|
||||
else if(str.startsWith("<SET NAME=URL VALUE=")) {
|
||||
str = str.replace("<SET NAME=URL VALUE=", "");
|
||||
str = str.replace("=", "");
|
||||
str = str.replace(">", "");
|
||||
Log.d("UDP Service ", "Url " + str);
|
||||
url = str.toLowerCase();
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (IOException e) {
|
||||
|
|
@ -108,14 +128,59 @@ public class WaterLevelService extends Service {
|
|||
while (!Thread.currentThread().isInterrupted()) {
|
||||
count++;
|
||||
if(count == 3) {
|
||||
Log.i("UDP Service", "Thread is running");
|
||||
Log.i("UDP Service", "Thread is running with mode: " + mode);
|
||||
count = 0;
|
||||
}
|
||||
byte[] buffer = new byte[4];
|
||||
try {
|
||||
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
|
||||
socket_recv.receive(packet);
|
||||
} catch(java.net.SocketTimeoutException e) {
|
||||
byte[] buffer = null;
|
||||
if(mode.equals("udp")) {
|
||||
try {
|
||||
buffer = new byte[4];
|
||||
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
|
||||
socket_recv.receive(packet);
|
||||
} catch(java.net.SocketTimeoutException e) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
else if(mode.equals("http")) {
|
||||
try {
|
||||
Thread.sleep(2000);
|
||||
} catch (InterruptedException e) {
|
||||
continue;
|
||||
}
|
||||
Log.d("UDP Service", "Http String Url: " + url);
|
||||
URL t_url = new URL(url);
|
||||
HttpURLConnection con = (HttpURLConnection) t_url.openConnection();
|
||||
con.setRequestMethod("GET");
|
||||
con.setConnectTimeout(5000);
|
||||
con.setReadTimeout(5000);
|
||||
con.setInstanceFollowRedirects(true);
|
||||
int status = con.getResponseCode();
|
||||
Log.d("UDP Service", "Got status from http: " + status);
|
||||
if(status == 200) {
|
||||
BufferedReader streamReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
|
||||
String data = streamReader.readLine();
|
||||
try {
|
||||
JSONObject obj = new JSONObject(data);
|
||||
JSONArray arr = obj.getJSONArray("water_level");
|
||||
float f = (float ) arr.getDouble(0);
|
||||
buffer = ByteBuffer.allocate(4).putFloat(f).array();
|
||||
Log.d("UDP Service", "Got value from http: " + f);
|
||||
} catch (JSONException e) {
|
||||
Log.e("UDP Service", "Invalid Json");
|
||||
}
|
||||
}
|
||||
else {
|
||||
InputStreamReader streamReader = new InputStreamReader(con.getErrorStream());
|
||||
continue;
|
||||
}
|
||||
}
|
||||
else {
|
||||
try {
|
||||
Thread.sleep(3000);
|
||||
} catch (InterruptedException e) {
|
||||
continue;
|
||||
}
|
||||
Log.d("UDP Service", "Invalid mode: " + mode);
|
||||
continue;
|
||||
}
|
||||
float f;
|
||||
|
|
|
|||
Loading…
Reference in a new issue