Data tranfering improvement
This commit is contained in:
@@ -1,18 +1,18 @@
|
||||
package com.helible.pilot.viewmodels
|
||||
|
||||
import android.util.Log
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.helible.pilot.controllers.BluetoothController
|
||||
import com.helible.pilot.controllers.ConnectionResult
|
||||
import com.helible.pilot.dataclasses.AlarmStateMessage
|
||||
import com.helible.pilot.dataclasses.BluetoothDevice
|
||||
import com.helible.pilot.dataclasses.BluetoothUiState
|
||||
import com.helible.pilot.dataclasses.EmergStopMessage
|
||||
import com.helible.pilot.dataclasses.RotorsSpeedMessage
|
||||
import com.helible.pilot.dataclasses.ChangedDeviceStatus
|
||||
import com.helible.pilot.dataclasses.DeviceStatus
|
||||
import com.helible.pilot.dataclasses.DeviceStatusJsonAdapter
|
||||
import com.squareup.moshi.Moshi
|
||||
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.SharingStarted
|
||||
@@ -25,6 +25,7 @@ import kotlinx.coroutines.flow.onEach
|
||||
import kotlinx.coroutines.flow.stateIn
|
||||
import kotlinx.coroutines.flow.update
|
||||
import kotlinx.coroutines.launch
|
||||
import java.time.LocalTime
|
||||
|
||||
class BluetoothViewModel(
|
||||
private val bluetoothController: BluetoothController,
|
||||
@@ -43,10 +44,9 @@ class BluetoothViewModel(
|
||||
pairedBluetoothDevices = pairedDevices
|
||||
)
|
||||
}.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000), _state.value)
|
||||
private val moshi: Moshi = Moshi.Builder().addLast(KotlinJsonAdapterFactory()).build()
|
||||
private val rotorsStateMessegeAdapter = moshi.adapter(RotorsSpeedMessage::class.java)
|
||||
private val alarmStateMessageAdapter = moshi.adapter(AlarmStateMessage::class.java)
|
||||
private val emergStopMessageAdapter = moshi.adapter(EmergStopMessage::class.java)
|
||||
private var deviceConnectionJob: Job? = null
|
||||
private val moshi = Moshi.Builder().add(KotlinJsonAdapterFactory()).add(DeviceStatusJsonAdapter()).build()
|
||||
private val newStatusMessageAdapter = moshi.adapter(ChangedDeviceStatus::class.java)
|
||||
|
||||
init {
|
||||
bluetoothController.isConnected.onEach { isConnected ->
|
||||
@@ -94,9 +94,11 @@ class BluetoothViewModel(
|
||||
}
|
||||
|
||||
is ConnectionResult.TransferSucceded -> {
|
||||
_state.update { it.copy(
|
||||
|
||||
) }
|
||||
_state.update {
|
||||
it.copy(
|
||||
deviceState = result.message
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
is ConnectionResult.Error -> {
|
||||
@@ -111,19 +113,19 @@ class BluetoothViewModel(
|
||||
}
|
||||
}
|
||||
.catch { throwable ->
|
||||
Log.e("BluetoothController", "Error occured while data transfer: ${throwable.message}")
|
||||
bluetoothController.closeConnection()
|
||||
_state.update { it.copy(
|
||||
isConnected = false,
|
||||
isConnecting = false
|
||||
isConnecting = false,
|
||||
deviceState = null
|
||||
) }
|
||||
}
|
||||
.launchIn(viewModelScope)
|
||||
}
|
||||
|
||||
private var deviceConnectionJob: Job? = null
|
||||
|
||||
fun connectToDevice(device: String) {
|
||||
if (_state.value.isConnected and _state.value.isConnecting) {
|
||||
if (_state.value.isConnected or _state.value.isConnecting) {
|
||||
return
|
||||
}
|
||||
_state.update { it.copy(isConnecting = true) }
|
||||
@@ -138,7 +140,8 @@ class BluetoothViewModel(
|
||||
_state.update {
|
||||
it.copy(
|
||||
isConnecting = false,
|
||||
isConnected = false
|
||||
isConnected = false,
|
||||
deviceState = null
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -170,4 +173,18 @@ class BluetoothViewModel(
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fun startImuCalibration() {
|
||||
viewModelScope.launch {
|
||||
val message = newStatusMessageAdapter.toJson(
|
||||
ChangedDeviceStatus(DeviceStatus.IsImuCalibration)
|
||||
) + "\n\r"
|
||||
val success = bluetoothController.trySendMessage(
|
||||
message.toByteArray()
|
||||
)
|
||||
if(!success) {
|
||||
Log.e("BluetoothVM", "Failed to start IMU calibration: $message")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user