Commit 94ec69b4 authored by Ishankha K.C's avatar Ishankha K.C

connect web socket to get emotions and view

parent bfd31212
...@@ -47,6 +47,7 @@ dependencies { ...@@ -47,6 +47,7 @@ dependencies {
implementation(libs.swiperefreshlayout) implementation(libs.swiperefreshlayout)
implementation(libs.converter.jackson) implementation(libs.converter.jackson)
implementation(libs.jackson.datatype.jsr310) implementation(libs.jackson.datatype.jsr310)
implementation(libs.okhttp)
testImplementation(libs.junit) testImplementation(libs.junit)
androidTestImplementation(libs.ext.junit) androidTestImplementation(libs.ext.junit)
androidTestImplementation(libs.espresso.core) androidTestImplementation(libs.espresso.core)
......
package com.kaluwa.enterprises.babycare.activities; package com.kaluwa.enterprises.babycare.activities;
import static com.kaluwa.enterprises.babycare.config.BabyEmotionWebSocketListener.NORMAL_CLOSURE_STATUS;
import static com.kaluwa.enterprises.babycare.config.TokenSaver.clearToken; import static com.kaluwa.enterprises.babycare.config.TokenSaver.clearToken;
import static com.kaluwa.enterprises.babycare.constants.Configs.EMOTIONAL_VIDEO_PROCESS_WS_URL;
import static com.kaluwa.enterprises.babycare.constants.Configs.LIVE_FEED_URL; import static com.kaluwa.enterprises.babycare.constants.Configs.LIVE_FEED_URL;
import static com.kaluwa.enterprises.babycare.constants.Configs.REFRESH_INTERVAL; import static com.kaluwa.enterprises.babycare.constants.Configs.REFRESH_INTERVAL;
import static com.kaluwa.enterprises.babycare.utils.Utils.animationChanger; import static com.kaluwa.enterprises.babycare.utils.Utils.animationChanger;
...@@ -28,12 +30,17 @@ import androidx.appcompat.widget.Toolbar; ...@@ -28,12 +30,17 @@ import androidx.appcompat.widget.Toolbar;
import com.kaluwa.enterprises.babycare.MainActivity; import com.kaluwa.enterprises.babycare.MainActivity;
import com.kaluwa.enterprises.babycare.R; import com.kaluwa.enterprises.babycare.R;
import com.kaluwa.enterprises.babycare.config.BabyEmotionWebSocketListener;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.URL; import java.net.URL;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.WebSocket;
public class LiveFeedActivity extends AppCompatActivity { public class LiveFeedActivity extends AppCompatActivity {
private static final String TAG = "LiveFeedActivity"; private static final String TAG = "LiveFeedActivity";
...@@ -44,6 +51,8 @@ public class LiveFeedActivity extends AppCompatActivity { ...@@ -44,6 +51,8 @@ public class LiveFeedActivity extends AppCompatActivity {
private Handler handler = new Handler(); private Handler handler = new Handler();
private Runnable updateRunnable; private Runnable updateRunnable;
private WebSocket webSocket;
private OkHttpClient client;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
...@@ -63,12 +72,21 @@ public class LiveFeedActivity extends AppCompatActivity { ...@@ -63,12 +72,21 @@ public class LiveFeedActivity extends AppCompatActivity {
handler.postDelayed(this, REFRESH_INTERVAL); handler.postDelayed(this, REFRESH_INTERVAL);
} }
}; };
// Start updating // Start updating
handler.post(updateRunnable); handler.post(updateRunnable);
// Toggle flashlight // Toggle flashlight
lfFlashBtn.setOnClickListener(v -> toggleFlashlight()); lfFlashBtn.setOnClickListener(v -> toggleFlashlight());
// Initialize the WebSocket connection
// client = new OkHttpClient();
// Request request = new Request.Builder()
// .url(EMOTIONAL_VIDEO_PROCESS_WS_URL)
// .build();
// BabyEmotionWebSocketListener listener = new BabyEmotionWebSocketListener(this, tvLlStatusValue);
// webSocket = client.newWebSocket(request, listener);
//
// // Clean up the WebSocket connection when done
// client.dispatcher().executorService().shutdown();
} }
private void toggleFlashlight() { private void toggleFlashlight() {
...@@ -99,10 +117,26 @@ public class LiveFeedActivity extends AppCompatActivity { ...@@ -99,10 +117,26 @@ public class LiveFeedActivity extends AppCompatActivity {
}).start(); }).start();
} }
@Override
protected void onStart() {
super.onStart();
connectWebSocket();
}
@Override
protected void onStop() {
super.onStop();
closeWebSocket();
}
@Override @Override
protected void onDestroy() { protected void onDestroy() {
super.onDestroy(); super.onDestroy();
handler.removeCallbacks(updateRunnable); // Stop updating when the activity is destroyed handler.removeCallbacks(updateRunnable); // Stop updating when the activity is destroyed
// Close the WebSocket connection
if (webSocket != null) {
webSocket.close(1000, "Activity destroyed");
}
} }
private void fetchAndDisplayImage() { private void fetchAndDisplayImage() {
...@@ -182,4 +216,18 @@ public class LiveFeedActivity extends AppCompatActivity { ...@@ -182,4 +216,18 @@ public class LiveFeedActivity extends AppCompatActivity {
super.onBackPressed(); super.onBackPressed();
animationChanger(this); animationChanger(this);
} }
private void connectWebSocket() {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url(EMOTIONAL_VIDEO_PROCESS_WS_URL).build();
webSocket = client.newWebSocket(request, new BabyEmotionWebSocketListener(this, tvLlStatusValue));
}
private void closeWebSocket() {
if (webSocket != null) {
webSocket.close(NORMAL_CLOSURE_STATUS, "Closing WebSocket");
webSocket = null; // Set to null after closing
}
}
} }
\ No newline at end of file
package com.kaluwa.enterprises.babycare.config;
import android.app.Activity;
import android.util.Log;
import android.widget.TextView;
import org.json.JSONException;
import org.json.JSONObject;
import java.nio.charset.StandardCharsets;
import okhttp3.Response;
import okhttp3.WebSocket;
import okhttp3.WebSocketListener;
import okio.ByteString;
public class BabyEmotionWebSocketListener extends WebSocketListener {
public static final int NORMAL_CLOSURE_STATUS = 1000;
private final TextView statusValueTextView;
private final Activity activity; // Reference to the Activity
public BabyEmotionWebSocketListener(Activity activity, TextView statusValueTextView) {
this.activity = activity;
this.statusValueTextView = statusValueTextView;
}
@Override
public void onOpen(WebSocket webSocket, Response response) {
// Connection opened
byte[] data = "Hello, WebSocket!".getBytes(StandardCharsets.UTF_8);
webSocket.send(ByteString.of(data));
}
@Override
public void onMessage(WebSocket webSocket, String text) {
// Parse the received message and update the UI
activity.runOnUiThread(() -> {
try {
JSONObject jsonObject = new JSONObject(text);
String emotion = jsonObject.getString("emotion");
// Capitalize the first letter
String capitalizedEmotion = emotion.substring(0, 1).toUpperCase() + emotion.substring(1);
if (capitalizedEmotion.equals("Null")) {
statusValueTextView.setText("No emotion detected!");
} else {
statusValueTextView.setText(capitalizedEmotion);
}
} catch (JSONException e) {
e.printStackTrace();
}
});
}
@Override
public void onMessage(WebSocket webSocket, ByteString bytes) {
onMessage(webSocket, bytes.utf8());
}
@Override
public void onClosing(WebSocket webSocket, int code, String reason) {
Log.e("WebSocket", "Connection closing: " + reason);
webSocket.close(NORMAL_CLOSURE_STATUS, null);
activity.runOnUiThread(() -> statusValueTextView.setText("Connection closed"));
}
@Override
public void onFailure(WebSocket webSocket, Throwable t, Response response) {
Log.e("WebSocket", "Error: " + t.getMessage());
activity.runOnUiThread(() -> statusValueTextView.setText("Connection error: " + t.getMessage()));
}
}
...@@ -2,8 +2,11 @@ package com.kaluwa.enterprises.babycare.constants; ...@@ -2,8 +2,11 @@ package com.kaluwa.enterprises.babycare.constants;
public class Configs { public class Configs {
public static final String BASE_URL = "http://192.168.1.2:8080/api/v1/baby-care/"; public static final String URL = "192.168.1.2:8080/api/v1/baby-care/";
public static final String BASE_URL = "http://"+URL;
public static final String LIVE_FEED_URL = "http://192.168.1.7"; public static final String LIVE_FEED_URL = "http://192.168.1.7";
public static final String EMOTIONAL_VIDEO_PROCESS_WS_URL = "ws://"+URL+"emotional/video-process";
public static final int REFRESH_INTERVAL = 100; // Refresh every 100 ms public static final int REFRESH_INTERVAL = 100; // Refresh every 100 ms
} }
...@@ -74,7 +74,7 @@ ...@@ -74,7 +74,7 @@
android:id="@+id/tvLlStatusLabel" android:id="@+id/tvLlStatusLabel"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="Baby's Status:" android:text="Last Baby's Status:"
android:textColor="@color/black" android:textColor="@color/black"
android:textSize="18sp" android:textSize="18sp"
android:layout_marginEnd="8dp"/> android:layout_marginEnd="8dp"/>
...@@ -83,7 +83,7 @@ ...@@ -83,7 +83,7 @@
android:id="@+id/tvLlStatusValue" android:id="@+id/tvLlStatusValue"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="Not Detected!" android:text="No emotion detected!"
android:textColor="@color/black" android:textColor="@color/black"
android:textStyle="bold" android:textStyle="bold"
android:textSize="18sp"/> android:textSize="18sp"/>
......
...@@ -16,6 +16,7 @@ materialVersion = "1.13.0-alpha02" ...@@ -16,6 +16,7 @@ materialVersion = "1.13.0-alpha02"
swiperefreshlayout = "1.2.0-alpha01" swiperefreshlayout = "1.2.0-alpha01"
converterJackson = "2.11.0" converterJackson = "2.11.0"
jacksonDatatypeJsr310 = "2.17.1" jacksonDatatypeJsr310 = "2.17.1"
okhttp = "5.0.0-alpha.14"
[libraries] [libraries]
junit = { group = "junit", name = "junit", version.ref = "junit" } junit = { group = "junit", name = "junit", version.ref = "junit" }
...@@ -36,6 +37,7 @@ ucrop = { group = "com.github.yalantis", name = "ucrop", version = "2.2.9" } ...@@ -36,6 +37,7 @@ ucrop = { group = "com.github.yalantis", name = "ucrop", version = "2.2.9" }
swiperefreshlayout = { group = "androidx.swiperefreshlayout", name = "swiperefreshlayout", version.ref = "swiperefreshlayout" } swiperefreshlayout = { group = "androidx.swiperefreshlayout", name = "swiperefreshlayout", version.ref = "swiperefreshlayout" }
converter-jackson = { group = "com.squareup.retrofit2", name = "converter-jackson", version.ref = "converterJackson" } converter-jackson = { group = "com.squareup.retrofit2", name = "converter-jackson", version.ref = "converterJackson" }
jackson-datatype-jsr310 = { group = "com.fasterxml.jackson.datatype", name = "jackson-datatype-jsr310", version.ref = "jacksonDatatypeJsr310" } jackson-datatype-jsr310 = { group = "com.fasterxml.jackson.datatype", name = "jackson-datatype-jsr310", version.ref = "jacksonDatatypeJsr310" }
okhttp = { group = "com.squareup.okhttp3", name = "okhttp", version.ref = "okhttp" }
[plugins] [plugins]
androidApplication = { id = "com.android.application", version.ref = "agp" } androidApplication = { id = "com.android.application", version.ref = "agp" }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment