feat: 连接状态

This commit is contained in:
2025-12-04 09:34:45 +08:00
parent 9c29fee24e
commit e1ddc14553
7 changed files with 42 additions and 14 deletions
@@ -18,6 +18,7 @@ import com.yuanxuan.rokid.databinding.ActivityMainBinding
import com.yuanxuan.rokid.extension.fadeIn
import com.yuanxuan.rokid.extension.fadeOut
import com.yuanxuan.rokid.keeplive.KeepLiveService
import com.yuanxuan.rokid.network.websocket.WebSocketConnectionState
import com.yuanxuan.rokid.network.websocket.WebSocketEvent
import com.yuanxuan.rokid.ui.NoticeFragment
import kotlinx.coroutines.delay
@@ -47,8 +48,6 @@ class MainActivity : AppCompatActivity() {
* 拦截返回键事件,防止返回到桌面
*/
onBackPressedDispatcher.addCallback {
viewModel.wakeUp()
navigationToast("TestMessage")
}
/**
* 这个服务保证心跳包准时发
@@ -119,6 +118,13 @@ class MainActivity : AppCompatActivity() {
binding.lottieVoiceInput.cancelAnimation()
binding.lottieVoiceInput.fadeOut(300)
}
if (uiState.connectStatus == WebSocketConnectionState.CONNECTED) {
binding.socketStatus.text =
resources.getString(R.string.socket_status_connected)
} else {
binding.socketStatus.text =
resources.getString(R.string.socket_status_connecting)
}
}
}
}
@@ -2,9 +2,10 @@ package com.yuanxuan.rokid
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.yuanxuan.rokid.dependencies.AppDependencies
import com.yuanxuan.rokid.dependencies.AppDependencies.webSocketManager
import com.yuanxuan.rokid.dependencies.AppDependencies.deviceServiceManager
import com.yuanxuan.rokid.device.DeviceServiceManager
import com.yuanxuan.rokid.network.websocket.WebSocketConnectionState
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.asSharedFlow
@@ -21,24 +22,27 @@ class MainViewModel : ViewModel() {
val uiState = combine(
deviceServiceManager.batteryPercentage,
deviceServiceManager.wifiState,
deviceServiceManager.instructState
) { battery, wifi, instruct ->
deviceServiceManager.instructState,
webSocketManager.connectFlow
) { battery, wifi, instruct, connect ->
MainUiState(
batteryLevel = battery,
wifiLevel = if (wifi is DeviceServiceManager.WifiState.Connected) wifi.level else 0,
isVoiceInputVisible = instruct == DeviceServiceManager.InstructState.WaitingInstructState
isVoiceInputVisible = instruct == DeviceServiceManager.InstructState.WaitingInstructState,
connectStatus = connect
)
}.stateIn(
scope = viewModelScope,
initialValue = MainUiState(
batteryLevel = 0,
wifiLevel = 0,
isVoiceInputVisible = false
isVoiceInputVisible = false,
connectStatus = WebSocketConnectionState.DISCONNECTED
),
started = SharingStarted.WhileSubscribed(5.seconds.inWholeMilliseconds),
)
val socketEvent = AppDependencies.webSocketManager.socketEventFlow
val socketEvent = webSocketManager.socketEventFlow
fun quitInstructReceived() {
deviceServiceManager.quitInstructReceived()
@@ -68,6 +72,7 @@ class MainViewModel : ViewModel() {
val batteryLevel: Int,
val wifiLevel: Int,
val isVoiceInputVisible: Boolean,
val connectStatus: WebSocketConnectionState,
)
}
@@ -6,8 +6,8 @@ import java.net.NetworkInterface
object NetUtils {
fun getBaseUrl() = "http://${getLocalIPV4address()}.70:7070"
// fun getBaseUrl() = "http://${getLocalIPV4address()}.52:8765"
// fun getBaseUrl() = "http://${getLocalIPV4address()}.70:7070"
fun getBaseUrl() = "http://${getLocalIPV4address()}.13:8765"
/**
@@ -11,6 +11,8 @@ class WebSocketManager(context: Context, scope: CoroutineScope) {
val socketEventFlow = webSocketConnection.eventFlow
val connectFlow = webSocketConnection.webSocketConnectionStateFlow
init {
scope.launch {
webSocketConnection.webSocketConnectionStateFlow.collectLatest { state ->