Data transfer service rewritten

This commit is contained in:
2024-03-01 22:11:06 +07:00
parent 5e0f2f1bb7
commit 3ac39136c6

View File

@@ -2,28 +2,19 @@ package com.helible.pilot.viewmodels
import android.bluetooth.BluetoothSocket import android.bluetooth.BluetoothSocket
import android.util.Log import android.util.Log
import com.helible.pilot.dataclasses.DeviceState
import com.helible.pilot.dataclasses.DeviceStatusJsonAdapter
import com.helible.pilot.dataclasses.GeneralMessage import com.helible.pilot.dataclasses.GeneralMessage
import com.helible.pilot.dataclasses.MessageType import com.helible.pilot.dataclasses.MessageType
import com.helible.pilot.dataclasses.PidSettings
import com.helible.pilot.exceptions.TransferFailedException import com.helible.pilot.exceptions.TransferFailedException
import com.squareup.moshi.JsonDataException
import com.squareup.moshi.JsonEncodingException
import com.squareup.moshi.Moshi
import com.squareup.moshi.adapter
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOn import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import java.io.BufferedInputStream
import java.io.IOException import java.io.IOException
const val maxPackageSize = 512; // bytes const val maxPackageSize = 512 // [bytes]
@ExperimentalStdlibApi
class BluetoothDataTransferService( class BluetoothDataTransferService(
private val socket: BluetoothSocket, private val socket: BluetoothSocket,
) { ) {
@@ -32,25 +23,23 @@ class BluetoothDataTransferService(
if (!socket.isConnected) if (!socket.isConnected)
return@flow return@flow
val buffer = ByteArray(maxPackageSize) val buffer = BufferedInputStream(socket.inputStream, maxPackageSize)
while (true) { while (true) {
val byteCount: Int = try { val message: String = try {
socket.inputStream.read(buffer) buffer.bufferedReader().readLine()
} catch (e: IOException) { } catch (e: IOException) {
Log.e("BluetoothController", "Failed to receive incoming data") Log.e("BluetoothController", "Failed to receive incoming data")
throw TransferFailedException() throw TransferFailedException()
} }
var messageData: String = buffer.decodeToString(endIndex = byteCount) try {
val messageType: MessageType? = MessageType.values() val messageType =
.elementAtOrNull(messageData.split(";")[0].toInt()) MessageType.values().elementAtOrNull(message.split(";").first().toInt())
val messageData = message.split(";").last()
if (messageData.endsWith("\n\r") && messageType != null) { if (messageType != null) {
messageData = messageData.dropLast(2).split(";")[1]
emit(GeneralMessage(messageType, messageData)) emit(GeneralMessage(messageType, messageData))
Log.d("BluetoothController", "Received: $messageData") }
} else { } catch (e: NoSuchElementException) {
Log.i("BluetoothController", "Package end isn't valid.") Log.e("BluetoothController", "Message type is invalid: $message")
Log.i("BluetoothController", messageData)
} }
} }
}.flowOn(Dispatchers.IO) }.flowOn(Dispatchers.IO)