Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
B
baby-monitoring-android-app
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
R24-145
baby-monitoring-android-app
Commits
94ec69b4
Commit
94ec69b4
authored
Oct 19, 2024
by
Ishankha K.C
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
connect web socket to get emotions and view
parent
bfd31212
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
130 additions
and
5 deletions
+130
-5
app/build.gradle.kts
app/build.gradle.kts
+1
-0
app/src/main/java/com/kaluwa/enterprises/babycare/activities/LiveFeedActivity.java
...uwa/enterprises/babycare/activities/LiveFeedActivity.java
+50
-2
app/src/main/java/com/kaluwa/enterprises/babycare/config/BabyEmotionWebSocketListener.java
...rprises/babycare/config/BabyEmotionWebSocketListener.java
+71
-0
app/src/main/java/com/kaluwa/enterprises/babycare/constants/Configs.java
...va/com/kaluwa/enterprises/babycare/constants/Configs.java
+4
-1
app/src/main/res/layout/activity_live_feed.xml
app/src/main/res/layout/activity_live_feed.xml
+2
-2
gradle/libs.versions.toml
gradle/libs.versions.toml
+2
-0
No files found.
app/build.gradle.kts
View file @
94ec69b4
...
...
@@ -47,6 +47,7 @@ dependencies {
implementation
(
libs
.
swiperefreshlayout
)
implementation
(
libs
.
converter
.
jackson
)
implementation
(
libs
.
jackson
.
datatype
.
jsr310
)
implementation
(
libs
.
okhttp
)
testImplementation
(
libs
.
junit
)
androidTestImplementation
(
libs
.
ext
.
junit
)
androidTestImplementation
(
libs
.
espresso
.
core
)
...
...
app/src/main/java/com/kaluwa/enterprises/babycare/activities/LiveFeedActivity.java
View file @
94ec69b4
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
.
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
.
REFRESH_INTERVAL
;
import
static
com
.
kaluwa
.
enterprises
.
babycare
.
utils
.
Utils
.
animationChanger
;
...
...
@@ -28,12 +30,17 @@ import androidx.appcompat.widget.Toolbar;
import
com.kaluwa.enterprises.babycare.MainActivity
;
import
com.kaluwa.enterprises.babycare.R
;
import
com.kaluwa.enterprises.babycare.config.BabyEmotionWebSocketListener
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.net.HttpURLConnection
;
import
java.net.URL
;
import
okhttp3.OkHttpClient
;
import
okhttp3.Request
;
import
okhttp3.WebSocket
;
public
class
LiveFeedActivity
extends
AppCompatActivity
{
private
static
final
String
TAG
=
"LiveFeedActivity"
;
...
...
@@ -44,6 +51,8 @@ public class LiveFeedActivity extends AppCompatActivity {
private
Handler
handler
=
new
Handler
();
private
Runnable
updateRunnable
;
private
WebSocket
webSocket
;
private
OkHttpClient
client
;
@Override
protected
void
onCreate
(
Bundle
savedInstanceState
)
{
...
...
@@ -63,12 +72,21 @@ public class LiveFeedActivity extends AppCompatActivity {
handler
.
postDelayed
(
this
,
REFRESH_INTERVAL
);
}
};
// Start updating
handler
.
post
(
updateRunnable
);
// Toggle flashlight
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
()
{
...
...
@@ -99,10 +117,26 @@ public class LiveFeedActivity extends AppCompatActivity {
}).
start
();
}
@Override
protected
void
onStart
()
{
super
.
onStart
();
connectWebSocket
();
}
@Override
protected
void
onStop
()
{
super
.
onStop
();
closeWebSocket
();
}
@Override
protected
void
onDestroy
()
{
super
.
onDestroy
();
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
()
{
...
...
@@ -182,4 +216,18 @@ public class LiveFeedActivity extends AppCompatActivity {
super
.
onBackPressed
();
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
app/src/main/java/com/kaluwa/enterprises/babycare/config/BabyEmotionWebSocketListener.java
0 → 100644
View file @
94ec69b4
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
()));
}
}
app/src/main/java/com/kaluwa/enterprises/babycare/constants/Configs.java
View file @
94ec69b4
...
...
@@ -2,8 +2,11 @@ package com.kaluwa.enterprises.babycare.constants;
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
EMOTIONAL_VIDEO_PROCESS_WS_URL
=
"ws://"
+
URL
+
"emotional/video-process"
;
public
static
final
int
REFRESH_INTERVAL
=
100
;
// Refresh every 100 ms
}
app/src/main/res/layout/activity_live_feed.xml
View file @
94ec69b4
...
...
@@ -74,7 +74,7 @@
android:id=
"@+id/tvLlStatusLabel"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:text=
"Baby's Status:"
android:text=
"
Last
Baby's Status:"
android:textColor=
"@color/black"
android:textSize=
"18sp"
android:layout_marginEnd=
"8dp"
/>
...
...
@@ -83,7 +83,7 @@
android:id=
"@+id/tvLlStatusValue"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:text=
"No
t D
etected!"
android:text=
"No
emotion d
etected!"
android:textColor=
"@color/black"
android:textStyle=
"bold"
android:textSize=
"18sp"
/>
...
...
gradle/libs.versions.toml
View file @
94ec69b4
...
...
@@ -16,6 +16,7 @@ materialVersion = "1.13.0-alpha02"
swiperefreshlayout
=
"1.2.0-alpha01"
converterJackson
=
"2.11.0"
jacksonDatatypeJsr310
=
"2.17.1"
okhttp
=
"5.0.0-alpha.14"
[libraries]
junit
=
{
group
=
"junit"
,
name
=
"junit"
,
version.ref
=
"junit"
}
...
...
@@ -36,6 +37,7 @@ ucrop = { group = "com.github.yalantis", name = "ucrop", version = "2.2.9" }
swiperefreshlayout
=
{
group
=
"androidx.swiperefreshlayout"
,
name
=
"swiperefreshlayout"
,
version.ref
=
"swiperefreshlayout"
}
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"
}
okhttp
=
{
group
=
"com.squareup.okhttp3"
,
name
=
"okhttp"
,
version.ref
=
"okhttp"
}
[plugins]
androidApplication
=
{
id
=
"com.android.application"
,
version.ref
=
"agp"
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment