v0.0.8
This commit is contained in:
parent
ab69505b80
commit
f59704545b
1 changed files with 98 additions and 16 deletions
|
|
@ -24,6 +24,7 @@ import java.io.InputStreamReader;
|
|||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
import org.json.JSONArray;
|
||||
import android.widget.Toast;
|
||||
|
||||
public class WaterLevelService extends Service {
|
||||
|
||||
|
|
@ -84,7 +85,8 @@ public class WaterLevelService extends Service {
|
|||
}
|
||||
}
|
||||
catch (IOException e) {
|
||||
Log.e("UDP Service", "Socket Error", e);
|
||||
// showToast("IOException: " + e);
|
||||
Log.e("UDP Service", "IOException Error" + e);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -132,12 +134,14 @@ public class WaterLevelService extends Service {
|
|||
count = 0;
|
||||
}
|
||||
byte[] buffer = null;
|
||||
String updated = "";
|
||||
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) {
|
||||
// showToast("Socket Timeout: " + e);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
|
@ -163,24 +167,30 @@ public class WaterLevelService extends Service {
|
|||
JSONObject obj = new JSONObject(data);
|
||||
JSONArray arr = obj.getJSONArray("water_level");
|
||||
float f = (float ) arr.getDouble(0);
|
||||
updated = obj.getString("updated_on");
|
||||
buffer = ByteBuffer.allocate(4).putFloat(f).array();
|
||||
Log.d("UDP Service", "Got value from http: " + f);
|
||||
Log.d("UDP Service", "Last updated on: " + updated);
|
||||
} catch (JSONException e) {
|
||||
Log.e("UDP Service", "Invalid Json");
|
||||
// showToast("Invalid Json" + e);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
else {
|
||||
// showToast("Bad Response From Server: " + status);
|
||||
InputStreamReader streamReader = new InputStreamReader(con.getErrorStream());
|
||||
continue;
|
||||
}
|
||||
}
|
||||
else {
|
||||
Log.d("UDP Service", "Invalid mode: " + mode);
|
||||
// showToast("Invalid mode: " + mode);
|
||||
try {
|
||||
Thread.sleep(3000);
|
||||
} catch (InterruptedException e) {
|
||||
continue;
|
||||
}
|
||||
Log.d("UDP Service", "Invalid mode: " + mode);
|
||||
continue;
|
||||
}
|
||||
float f;
|
||||
|
|
@ -220,10 +230,17 @@ public class WaterLevelService extends Service {
|
|||
DatagramPacket send_packet = new DatagramPacket(buffer, buffer.length, InetAddress.getByName("localhost"), 8180);
|
||||
socket_send.send(send_packet);
|
||||
Log.d("UDP Service ", "Sent packet");
|
||||
}
|
||||
if(mode.equals("http")) {
|
||||
byte[] buff2 = updated.getBytes(StandardCharsets.UTF_8);
|
||||
DatagramPacket send_packet_2 = new DatagramPacket(buff2, buff2.length, InetAddress.getByName("localhost"), 8185);
|
||||
socket_send.send(send_packet_2);
|
||||
Log.d("UDP Service ", "Sent last updated packet");
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (IOException e) {
|
||||
Log.e("UDP Service", "Socket Error", e);
|
||||
// showToast("IOException: " + e);
|
||||
Log.e("UDP Service", "IOException Error" + e);
|
||||
} finally {
|
||||
if (wifiLock.isHeld())
|
||||
wifiLock.release();
|
||||
|
|
@ -282,6 +299,11 @@ public class WaterLevelService extends Service {
|
|||
return START_STICKY;
|
||||
}
|
||||
|
||||
public void showToast(String str) {
|
||||
int duration = Toast.LENGTH_SHORT;
|
||||
Toast toast = Toast.makeText(this, str, duration);
|
||||
toast.show();
|
||||
}
|
||||
|
||||
private Notification createNotification() {
|
||||
String channelId = "water_level_channel";
|
||||
|
|
|
|||
Loading…
Reference in a new issue