add device data parsing and saving calcualtion data

This commit is contained in:
Pritimay Sarkar
2023-08-03 23:01:49 +05:30
parent 4c7c24449b
commit 547b5d8663
3 changed files with 82 additions and 76 deletions

View File

@@ -107,15 +107,18 @@ class HemoCubeFragment : Fragment() {
"Yes",
object : MyDialogListener {
override fun onClickNegativeButton() {
listenToHemoCube()
startBuffer()
}
override fun onClickPositiveButton() {
listenToHemoCube()
startSample()
}
}
)
} else {
listenToHemoCube()
startBuffer()
}
}
@@ -126,37 +129,13 @@ class HemoCubeFragment : Fragment() {
}
private fun setupButtonClickListeners() {
binding.btnSubmit.setOnClickListener {
binding.progressBar.visibility = View.VISIBLE
if (isOnline) {
getResult()
resultRatio?.let { ratio ->
Log.e("Testdb", "upload")
hemoCubeViewModel.uploadHemoCubeResultToDatabase(
requireContext(), isOnline, true, kitSerial, ratio, resultData, testTime
)
}
binding.progressBar.visibility = View.VISIBLE
} else {
getResult()
resultRatio?.let { ratio ->
hemoCubeViewModel.uploadHemoCubeResultToDatabase(
requireContext(), isOnline, true, kitSerial, ratio, resultData, testTime
)
}
Toast.makeText(
requireContext(),
"Internet not available, Test Data added to Local DB.",
Toast.LENGTH_SHORT
).show()
}
getResult()
}
binding.btnSubmit.isEnabled = false
binding.btnSubmit.isClickable = false
binding.btnSubmit.isEnabled = true
binding.btnSubmit.isClickable = true
}
private fun listenToHemoCube() {
@@ -172,16 +151,64 @@ class HemoCubeFragment : Fragment() {
val stringData = String(it)
fullReadOutput.append(stringData)
if (fullReadOutput.contains("#")) {
binding.tvSubtitle4.text = fullReadOutput
if (stringData.contains("#")) {
binding.tvSubtitle4.text = stringData
}
resultData += fullReadOutput.toString()
DataHolder.hemoCubeTestData?.resultData
if (fullReadOutput.contains("#Sample Completed")) {
binding.btnSubmit.isEnabled = false
binding.btnSubmit.isClickable = false
}
// if (stringData.contains("#Sample Completedok")) {
// binding.btnSubmit.isEnabled = true
// binding.btnSubmit.isClickable = true
// }
hemoCubeViewModel.progressBar.postValue(false)
if (resultData.contains("RESULT")) {
// val response: String = "RESULT SN 18291 1937.23 2828.11 28211.20 121829.12"
val results = resultData.split("\n")
val validStringFound = checkAndFindResults(results)
if (!validStringFound.isNullOrEmpty()) {
val response = validStringFound
val result = response.split(" ")
if (result.size == 8) {
val deviceSerialNo = result[2]
val led1Buffer = result[3].toDoubleOrNull()
val led2Buffer = result[4].toDoubleOrNull()
val led1Sample = result[5].toDoubleOrNull()
val led2Sample = result[6].toDoubleOrNull()
val led1Average = log10(led1Buffer?.div(led1Sample!!) ?: 0.0)
val led2Average = log10(led2Buffer?.div(led2Sample!!) ?: 0.0)
val deviceRatio = led1Average / led2Average
DataHolder.hemoCubeTestData?.deviceSerialNumber = deviceSerialNo
DataHolder.hemoCubeTestData?.led1Buffer = led1Buffer
DataHolder.hemoCubeTestData?.led2Buffer = led2Buffer
DataHolder.hemoCubeTestData?.led1Sample = led1Sample
DataHolder.hemoCubeTestData?.led2Sample = led2Sample
DataHolder.hemoCubeTestData?.led1Average = led1Average
DataHolder.hemoCubeTestData?.led2Average = led2Average
DataHolder.hemoCubeTestData?.deviceRatio = deviceRatio
DataHolder.hemoCubeTestData?.calculatedRatio =
calculateRatio(deviceRatio)
}
}
hemoCubeViewModel.progressBar.postValue(false)
if (isOnline) {
resultRatio?.let { ratio ->
Log.e("Testdb", "upload")
hemoCubeViewModel.uploadHemoCubeResultToDatabase(isOnline, true, kitSerial
)
}
binding.progressBar.visibility = View.VISIBLE
} else {
resultRatio?.let { ratio ->
hemoCubeViewModel.uploadHemoCubeResultToDatabase(isOnline, true, kitSerial)
}
Toast.makeText(
requireContext(),
"Internet not available, Test Data added to Local DB.",
Toast.LENGTH_SHORT
).show()
}
}
}
}
@@ -224,45 +251,11 @@ class HemoCubeFragment : Fragment() {
private fun getResult() {
hemoCubeViewModel.progressBar.postValue(true)
val fullReadOutput = StringBuilder()
(activity as HemocubeActivity).mService.sendAndListenToHemoCube(
HemoCubeCommands.getSample,
object : UsbServiceListener {
override fun onUsbRead(data: ByteArray?) {
data?.let {
val stringData = String(it)
fullReadOutput.append(stringData)
if (stringData.contains("RESULT", true)) {
// hemoCubeViewModel.mapDeviceConstants(fullReadOutput.toString())
// val response: String = "RESULT SN18291 1937.23 2828.11 28211.20 121829.12"
val response = stringData
val result = response.split(" ")
val deviceSerialNo = result[1]
val led1Buffer = result[2].toDoubleOrNull()
val led2Buffer = result[3].toDoubleOrNull()
val led1Sample = result[4].toDoubleOrNull()
val led2Sample = result[5].toDoubleOrNull()
val led1Average = log10(led1Buffer?.div(led1Sample!!) ?: 0.0)
val led2Average = log10(led2Buffer?.div(led2Sample!!) ?: 0.0)
val deviceRatio = led1Average / led2Average
DataHolder.hemoCubeTestData?.deviceSerialNumber = deviceSerialNo
DataHolder.hemoCubeTestData?.led1Buffer = led1Buffer
DataHolder.hemoCubeTestData?.led2Buffer = led2Buffer
DataHolder.hemoCubeTestData?.led1Sample = led1Sample
DataHolder.hemoCubeTestData?.led2Sample = led2Sample
DataHolder.hemoCubeTestData?.led1Average = led1Average
DataHolder.hemoCubeTestData?.led2Average = led2Average
DataHolder.hemoCubeTestData?.deviceRatio = deviceRatio
DataHolder.hemoCubeTestData?.calculatedRatio = calculateRatio(deviceRatio)
hemoCubeViewModel.progressBar.postValue(false)
}
}
}
override fun onUsbRead(data: ByteArray?) {}
override fun onUsbError(e: Exception?) {
hemoCubeViewModel.progressBar.postValue(false)
}
@@ -277,4 +270,20 @@ class HemoCubeFragment : Fragment() {
val c2 = deviceData?.coefficients?.get(1)!!
return c1 * ratio + c2
}
private fun checkAndFindResults(results: List<String>): String {
val reversedResults = results.reversed()
for (line in reversedResults) {
if (isValidFormat(line))
return line
}
return ""
}
private fun isValidFormat(line: String): Boolean {
if (line.contains("RESULT") && line.split(" ").size == 8) {
return true
}
return false
}
}

View File

@@ -39,16 +39,10 @@ class HemoCubeViewModel @Inject constructor(
val networkStatusLiveData: LiveData<Boolean>
get() = _networkStatusLiveData
val fireBaseUpload = MutableLiveData<String>()
fun uploadHemoCubeResultToDatabase(
context: Context, isOnline: Boolean, testStatus: Boolean, kitSerial: String?,
resultRatio: String,
resultData: String,
testTime: String?
) = viewModelScope.launch {
fun uploadHemoCubeResultToDatabase(isOnline: Boolean, testStatus: Boolean, kitSerial: String?) = viewModelScope.launch {
if (kitSerial != null) {
testDetails?.kitSerial = kitSerial
}
testDetails?.testTime = testTime
testDetails?.testStatus = testStatus
try {
@@ -87,6 +81,7 @@ class HemoCubeViewModel @Inject constructor(
testDetails?.led1Average = DataHolder.hemoCubeTestData?.led1Average
testDetails?.led2Average = DataHolder.hemoCubeTestData?.led2Average
testDetails?.deviceRatio = DataHolder.hemoCubeTestData?.deviceRatio
testDetails?.calculatedRatio = DataHolder.hemoCubeTestData?.calculatedRatio
viewModelScope.launch {
try {

View File

@@ -29,6 +29,8 @@ class UsbService : Service() {
}
var listener: UsbServiceListener? = null
var bus: UsbServiceListener? = null
fun connect(driver: UsbSerialDriver, connection: UsbDeviceConnection) {
mPort = driver.ports[0] // Most devices have just one port (port 0)
mPort.open(connection)
@@ -76,7 +78,7 @@ class UsbService : Service() {
fun sendAndListenToHemoCube(command: HemoCubeCommands, listener: UsbServiceListener) {
try {
this.listener = listener
this.bus = listener
mPort.write(command.command.toByteArray(), Constants.WRITE_TIMEOUT_MILLIS)
} catch (e: IOException) {
listener.onUsbError(e)