websockets stuff
This commit is contained in:
parent
2ea6cf1fc4
commit
be4df87c99
@ -71,4 +71,8 @@ dependencies {
|
|||||||
implementation 'com.github.squti:Android-Wave-Recorder:1.7.0'
|
implementation 'com.github.squti:Android-Wave-Recorder:1.7.0'
|
||||||
implementation("com.squareup.okhttp3:okhttp:4.9.3")
|
implementation("com.squareup.okhttp3:okhttp:4.9.3")
|
||||||
implementation 'com.google.code.gson:gson:2.8.9'
|
implementation 'com.google.code.gson:gson:2.8.9'
|
||||||
|
|
||||||
|
implementation ('io.socket:socket.io-client:2.0.0') {
|
||||||
|
exclude group: 'org.json', module: 'json'
|
||||||
|
}
|
||||||
}
|
}
|
@ -13,11 +13,23 @@ import androidx.compose.ui.tooling.preview.Preview
|
|||||||
import androidx.core.view.WindowCompat
|
import androidx.core.view.WindowCompat
|
||||||
import ch.broillet.jarvis.android.nav.Navigation
|
import ch.broillet.jarvis.android.nav.Navigation
|
||||||
import ch.broillet.jarvis.android.ui.theme.JarvisclientappTheme
|
import ch.broillet.jarvis.android.ui.theme.JarvisclientappTheme
|
||||||
|
import ch.broillet.jarvis.android.utils.SocketHandler
|
||||||
|
import java.util.*
|
||||||
|
|
||||||
|
|
||||||
class MainActivity : ComponentActivity() {
|
class MainActivity : ComponentActivity() {
|
||||||
|
var uniqueID = UUID.randomUUID().toString()
|
||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
|
|
||||||
|
// The following lines connects the Android app to the server.
|
||||||
|
SocketHandler.setSocket()
|
||||||
|
SocketHandler.establishConnection()
|
||||||
|
SocketHandler.joinRoom(uniqueID)
|
||||||
|
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
|
|
||||||
|
|
||||||
setContent {
|
setContent {
|
||||||
JarvisclientappTheme {
|
JarvisclientappTheme {
|
||||||
|
|
||||||
@ -30,8 +42,7 @@ class MainActivity : ComponentActivity() {
|
|||||||
|
|
||||||
// A surface container using the 'background' color from the theme
|
// A surface container using the 'background' color from the theme
|
||||||
Surface(
|
Surface(
|
||||||
modifier = Modifier.fillMaxSize(),
|
modifier = Modifier.fillMaxSize(), color = MaterialTheme.colorScheme.background
|
||||||
color = MaterialTheme.colorScheme.background
|
|
||||||
) {
|
) {
|
||||||
DefaultPreview()
|
DefaultPreview()
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package ch.broillet.jarvis.android.pages
|
package ch.broillet.jarvis.android.pages
|
||||||
|
|
||||||
|
import android.os.Looper
|
||||||
import androidx.compose.foundation.layout.*
|
import androidx.compose.foundation.layout.*
|
||||||
import androidx.compose.material.icons.Icons
|
import androidx.compose.material.icons.Icons
|
||||||
import androidx.compose.material.icons.outlined.Edit
|
import androidx.compose.material.icons.outlined.Edit
|
||||||
@ -17,6 +18,7 @@ import androidx.compose.ui.unit.dp
|
|||||||
import androidx.compose.ui.unit.sp
|
import androidx.compose.ui.unit.sp
|
||||||
import androidx.navigation.NavController
|
import androidx.navigation.NavController
|
||||||
import androidx.navigation.compose.rememberNavController
|
import androidx.navigation.compose.rememberNavController
|
||||||
|
import ch.broillet.jarvis.android.MainActivity
|
||||||
import ch.broillet.jarvis.android.R
|
import ch.broillet.jarvis.android.R
|
||||||
import ch.broillet.jarvis.android.audio.AudioRecorder
|
import ch.broillet.jarvis.android.audio.AudioRecorder
|
||||||
import ch.broillet.jarvis.android.chat.ConversationUiState
|
import ch.broillet.jarvis.android.chat.ConversationUiState
|
||||||
@ -25,10 +27,7 @@ import ch.broillet.jarvis.android.chat.Messages
|
|||||||
import ch.broillet.jarvis.android.nav.Screen
|
import ch.broillet.jarvis.android.nav.Screen
|
||||||
import ch.broillet.jarvis.android.ui.theme.JarvisclientappTheme
|
import ch.broillet.jarvis.android.ui.theme.JarvisclientappTheme
|
||||||
import ch.broillet.jarvis.android.ui.theme.productSansFont
|
import ch.broillet.jarvis.android.ui.theme.productSansFont
|
||||||
import ch.broillet.jarvis.android.utils.DefaultBox
|
import ch.broillet.jarvis.android.utils.*
|
||||||
import ch.broillet.jarvis.android.utils.DotsFlashing
|
|
||||||
import ch.broillet.jarvis.android.utils.DotsTyping
|
|
||||||
import ch.broillet.jarvis.android.utils.contactServerWithFileAudioRecording
|
|
||||||
import com.github.squti.androidwaverecorder.RecorderState
|
import com.github.squti.androidwaverecorder.RecorderState
|
||||||
import com.github.squti.androidwaverecorder.WaveRecorder
|
import com.github.squti.androidwaverecorder.WaveRecorder
|
||||||
import org.json.JSONObject
|
import org.json.JSONObject
|
||||||
@ -37,7 +36,7 @@ import kotlin.concurrent.thread
|
|||||||
|
|
||||||
//Draws the base of the main activity, that includes the 3-dots menu and the "hi text".
|
//Draws the base of the main activity, that includes the 3-dots menu and the "hi text".
|
||||||
@Composable
|
@Composable
|
||||||
fun Base(navController: NavController, uiState: ConversationUiState) {
|
fun Base(navController: NavController) {
|
||||||
|
|
||||||
Column(
|
Column(
|
||||||
Modifier
|
Modifier
|
||||||
@ -146,7 +145,7 @@ fun DisplayMainPage(
|
|||||||
// This column regroup the base and all the conversations (everything except the footer)
|
// This column regroup the base and all the conversations (everything except the footer)
|
||||||
Column(Modifier.padding(bottom = 80.dp)) {
|
Column(Modifier.padding(bottom = 80.dp)) {
|
||||||
|
|
||||||
Base(navController, uiState)
|
Base(navController)
|
||||||
|
|
||||||
Messages(
|
Messages(
|
||||||
messages = uiState.messages,
|
messages = uiState.messages,
|
||||||
@ -163,22 +162,34 @@ fun DisplayMainPage(
|
|||||||
var listening: Boolean by remember { mutableStateOf(false) }
|
var listening: Boolean by remember { mutableStateOf(false) }
|
||||||
var processing: Boolean by remember { mutableStateOf(false) }
|
var processing: Boolean by remember { mutableStateOf(false) }
|
||||||
|
|
||||||
|
SocketHandler.getSocket().on("message_from_jarvis") { args ->
|
||||||
|
if (args[0] != null) {
|
||||||
|
uiState.addMessage(Message(true, args.toString()))
|
||||||
|
}
|
||||||
|
}
|
||||||
audioRecorder.waveRecorder.onStateChangeListener = {
|
audioRecorder.waveRecorder.onStateChangeListener = {
|
||||||
when (it) {
|
when (it) {
|
||||||
RecorderState.RECORDING -> listening = true
|
RecorderState.RECORDING -> listening = true
|
||||||
RecorderState.STOP -> {
|
RecorderState.STOP -> {
|
||||||
listening = false
|
listening = false
|
||||||
processing = true
|
processing = true
|
||||||
|
|
||||||
|
SocketHandler.processMessage("test", MainActivity().uniqueID)
|
||||||
|
|
||||||
thread {
|
thread {
|
||||||
val requestOutput =
|
//val requestOutput = getTextFromAudio(audioRecorder.getOutputFile())
|
||||||
contactServerWithFileAudioRecording(audioRecorder.getOutputFile())
|
|
||||||
|
val temp = JSONObject()
|
||||||
|
temp.put("data", "salut je suis bob")
|
||||||
|
val requestOutput = temp.toString()
|
||||||
|
|
||||||
processing = false
|
processing = false
|
||||||
|
|
||||||
val json = JSONObject(requestOutput)
|
val json = JSONObject(requestOutput)
|
||||||
val sent = json.getString("transcription")
|
val sent = json.getString("data")
|
||||||
|
|
||||||
uiState.addMessage(Message(false, sent))
|
uiState.addMessage(Message(false, sent))
|
||||||
|
|
||||||
// Thread.sleep(1000)
|
// Thread.sleep(1000)
|
||||||
// uiState.addMessage(Message(true, json.getString("answer")))
|
// uiState.addMessage(Message(true, json.getString("answer")))
|
||||||
audioRecorder.getOutputFile().delete()
|
audioRecorder.getOutputFile().delete()
|
||||||
@ -191,7 +202,7 @@ fun DisplayMainPage(
|
|||||||
StartRecordingFAB(
|
StartRecordingFAB(
|
||||||
onClick = { if (listening) audioRecorder.stopRecording() else audioRecorder.startRecording() },
|
onClick = { if (listening) audioRecorder.stopRecording() else audioRecorder.startRecording() },
|
||||||
isRecording = listening,
|
isRecording = listening,
|
||||||
isProcessing = processing
|
isProcessing = processing
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,12 +6,14 @@ import okhttp3.Request
|
|||||||
import okhttp3.RequestBody.Companion.asRequestBody
|
import okhttp3.RequestBody.Companion.asRequestBody
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.io.IOException
|
import java.io.IOException
|
||||||
|
import java.net.Socket
|
||||||
|
|
||||||
fun contactServerWithFileAudioRecording(file: File): String {
|
|
||||||
|
fun getTextFromAudio(file: File): String {
|
||||||
val client = OkHttpClient()
|
val client = OkHttpClient()
|
||||||
|
|
||||||
val request = Request.Builder()
|
val request = Request.Builder()
|
||||||
.url("https://jarvis-server.broillet.ch/process_voice")
|
.url("https://jarvis-server.broillet.ch/get_text_from_audio")
|
||||||
.post(file.asRequestBody("audio/x-wav; charset=utf-8".toMediaType()))
|
.post(file.asRequestBody("audio/x-wav; charset=utf-8".toMediaType()))
|
||||||
.build()
|
.build()
|
||||||
|
|
||||||
@ -45,4 +47,4 @@ fun contactServerWithFileAudioRecording(file: File): String {
|
|||||||
if (!response.isSuccessful) throw IOException("Unexpected code $response")
|
if (!response.isSuccessful) throw IOException("Unexpected code $response")
|
||||||
return response.body!!.string()
|
return response.body!!.string()
|
||||||
}*/
|
}*/
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,47 @@
|
|||||||
|
package ch.broillet.jarvis.android.utils
|
||||||
|
|
||||||
|
import io.socket.client.IO
|
||||||
|
import io.socket.client.Socket
|
||||||
|
import org.json.JSONObject
|
||||||
|
import java.net.URISyntaxException
|
||||||
|
|
||||||
|
object SocketHandler {
|
||||||
|
|
||||||
|
lateinit var mSocket: Socket
|
||||||
|
|
||||||
|
@Synchronized
|
||||||
|
fun setSocket() {
|
||||||
|
try {
|
||||||
|
mSocket = IO.socket("https://jarvis-server.broillet.ch")
|
||||||
|
} catch (_: URISyntaxException) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Synchronized
|
||||||
|
fun getSocket(): Socket {
|
||||||
|
return mSocket
|
||||||
|
}
|
||||||
|
|
||||||
|
@Synchronized
|
||||||
|
fun establishConnection() {
|
||||||
|
mSocket.connect()
|
||||||
|
}
|
||||||
|
|
||||||
|
@Synchronized
|
||||||
|
fun closeConnection() {
|
||||||
|
mSocket.disconnect()
|
||||||
|
}
|
||||||
|
|
||||||
|
@Synchronized
|
||||||
|
fun processMessage(message: String, uuid: String) {
|
||||||
|
val body = JSONObject()
|
||||||
|
body.put("data", message)
|
||||||
|
body.put("uuid", uuid)
|
||||||
|
getSocket().emit("process_message", body)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Synchronized
|
||||||
|
fun joinRoom(uuid: String) {
|
||||||
|
getSocket().emit("join", uuid)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user