From f37cfb57e1c147f9968582d3547c8c6342790bcb Mon Sep 17 00:00:00 2001 From: Mariya Date: Tue, 7 Nov 2023 17:25:09 +0530 Subject: [PATCH] 1.baud rate is added, 2. existing buffer popup is added, 3. submit button make visible 4.handlevalidresult function changes, added code for fetch ledbuffer1 to ledbuffer4 values and sample1 to sample4 values --- .../presentation/hemocube/HemoCubeFragment.kt | 189 ++++++++++-------- 1 file changed, 108 insertions(+), 81 deletions(-) diff --git a/app/src/main/java/com/example/hpostesting/presentation/hemocube/HemoCubeFragment.kt b/app/src/main/java/com/example/hpostesting/presentation/hemocube/HemoCubeFragment.kt index 049d14f..448e6a5 100644 --- a/app/src/main/java/com/example/hpostesting/presentation/hemocube/HemoCubeFragment.kt +++ b/app/src/main/java/com/example/hpostesting/presentation/hemocube/HemoCubeFragment.kt @@ -79,9 +79,8 @@ class HemoCubeFragment : Fragment() { binding.nameEditText.visibility = View.GONE binding.tvTitle.visibility = View.GONE binding.btnGo.visibility = View.GONE - binding.btnSubmit.isEnabled = false - binding.btnSubmit.isClickable = false - + binding.btnSubmit.isEnabled = true + binding.btnSubmit.isClickable = true binding.tvName.text = "Name: ${testDetails?.name}\n ID: ${testDetails?._id}" Log.e("nametext", binding.tvName.text.toString()) @@ -92,8 +91,8 @@ class HemoCubeFragment : Fragment() { binding.tvSubtitle4.visibility = View.VISIBLE binding.tvSubtitle4.text = "Place Sample" } - startSampleProcess() - it.visibility = View.GONE + startSampleProcess() + it.visibility = View.GONE } binding.btnPlacebuffer.setOnClickListener { @@ -104,7 +103,6 @@ class HemoCubeFragment : Fragment() { binding.tvSubtitle4.visibility = View.VISIBLE binding.tvSubtitle4.text = "Place Buffer" } - checkAndStartProcess() it.visibility = View.GONE } @@ -258,13 +256,15 @@ class HemoCubeFragment : Fragment() { Log.e("nametext", stringData) when { - stringData.contains("#BC") -> { + stringData.contains("#BC") -> { activity?.runOnUiThread { binding.btnSamplestart.visibility = View.VISIBLE } } stringData.contains("#SC") -> { - binding.btnSubmit.visibility = View.VISIBLE + activity?.runOnUiThread { + binding.btnSubmit.visibility = View.VISIBLE + } fetchResult() testingTrace.stop() } @@ -351,43 +351,70 @@ class HemoCubeFragment : Fragment() { private fun handleValidResult(validString: String, fullReadOutput: String) { try { val resultLines = validString.split("\n") - if (resultLines.size == 9) { - val deviceLog = fullReadOutput - val led1BufferForDevice = resultLines.find { it.startsWith("LB1") }?.substring(4) - ?.toDoubleOrNull() - ?: 0.0 - val led2BufferForDevice = resultLines.find { it.startsWith("LB2") }?.substring(4) - ?.toDoubleOrNull() - ?: 0.0 - val led3BufferForDevice = resultLines.find { it.startsWith("LB3") }?.substring(4) - ?.toDoubleOrNull() - ?: 0.0 - val led4BufferForDevice = resultLines.find { it.startsWith("LB4") }?.substring(4) - ?.toDoubleOrNull() - ?: 0.0 +// if (resultLines.size == 9) { + val deviceLog = fullReadOutput +// val led1BufferForDevice = if (isUsingExistingBuffer) { +// sharedPreferences.getString(Constants.BUFFER_VALUE_1, "")?.toDoubleOrNull() +// } else { +// resultLines.find { it.startsWith("LB1") }?.substring(4) +// ?.toDoubleOrNull() +// ?: 0.0 +// } + val led1BufferForDevice = resultLines.find { it.startsWith("LB1") }?.substring(4)?.toDoubleOrNull() ?: 0.0 + val led2BufferForDevice = resultLines.find { it.startsWith("LB2") }?.substring(4)?.toDoubleOrNull() ?: 0.0 + val led3BufferForDevice = resultLines.find { it.startsWith("LB3") }?.substring(4)?.toDoubleOrNull() ?: 0.0 + val led4BufferForDevice = resultLines.find { it.startsWith("LB4") }?.substring(4)?.toDoubleOrNull() ?: 0.0 + val led1Sample = resultLines.find { it.startsWith("LS1") }?.substring(4)?.toDoubleOrNull() ?: 0.0 + val led2Sample = resultLines.find { it.startsWith("LS2") }?.substring(4)?.toDoubleOrNull() ?: 0.0 + val led3Sample = resultLines.find { it.startsWith("LS3") }?.substring(4)?.toDoubleOrNull() ?: 0.0 + val led4Sample = resultLines.find { it.startsWith("LS4") }?.substring(4)?.toDoubleOrNull() ?: 0.0 - val pInfo = requireActivity().packageManager.getPackageInfo( - requireActivity().packageName, 0 - ) - val version = pInfo.versionName +// val led1BufferForDevice = resultLines.find { it.startsWith("LB1") }?.let { +// try { +// it.substring(4).toDoubleOrNull() ?: 0.0 +// } catch (e: NumberFormatException) { +// // Handle invalid number format, log an error, or set a default value +// Log.e("Parsing Error", "Invalid format for LB1: $it") +// 0.0 +// } +// } ?: 0.0 +// +// val led2BufferForDevice = if (isUsingExistingBuffer) { +// sharedPreferences.getString(Constants.BUFFER_VALUE_2, "")?.toDoubleOrNull() +// } else { +// resultLines.find { it.startsWith("LB2") }?.substring(4) +// ?.toDoubleOrNull() +// ?: 0.0 +// } +// val led3BufferForDevice = resultLines.find { it.startsWith("LB3") }?.substring(4) +// ?.toDoubleOrNull() +// ?: 0.0 +// val led4BufferForDevice = resultLines.find { it.startsWith("LB4") }?.substring(4) +// ?.toDoubleOrNull() +// ?: 0.0 - val led1Sample = resultLines.find { it.startsWith("LS1") }?.substring(4) - ?.toDoubleOrNull() - ?: 0.0 - val led2Sample = resultLines.find { it.startsWith("LS2") }?.substring(4) - ?.toDoubleOrNull() - ?: 0.0 - val led3Sample = resultLines.find { it.startsWith("LS3") }?.substring(4) - ?.toDoubleOrNull() - ?: 0.0 - val led4Sample = resultLines.find { it.startsWith("LS4") }?.substring(4) - ?.toDoubleOrNull() - ?: 0.0 + val pInfo = requireActivity().packageManager.getPackageInfo( + requireActivity().packageName, 0 + ) + val version = pInfo.versionName - val led1Average = log10(led1BufferForDevice?.div(led1Sample!!) ?: 0.0) - val led2Average = log10(led2BufferForDevice?.div(led2Sample!!) ?: 0.0) - val led3Average = log10(led3BufferForDevice?.div(led3Sample!!) ?: 0.0) - val deviceRatio = led1Average / led2Average +// val led1Sample = resultLines.find { it.startsWith("LS1") }?.substring(4) +// ?.toDoubleOrNull() +// ?: 0.0 +// val led2Sample = resultLines.find { it.startsWith("LS2") }?.substring(4) +// ?.toDoubleOrNull() +// ?: 0.0 +// val led3Sample = resultLines.find { it.startsWith("LS3") }?.substring(4) +// ?.toDoubleOrNull() +// ?: 0.0 +// val led4Sample = resultLines.find { it.startsWith("LS4") }?.substring(4) +// ?.toDoubleOrNull() +// ?: 0.0 + + val led1Average = log10(led1BufferForDevice?.div(led1Sample!!) ?: 0.0) + val led2Average = log10(led2BufferForDevice?.div(led2Sample!!) ?: 0.0) + val led3Average = log10(led3BufferForDevice?.div(led3Sample!!) ?: 0.0) + val deviceRatio = led1Average / led2Average // if (led1BufferForDevice!! > 20000 && led1BufferForDevice < 24000 && led2BufferForDevice!! > 20000 && led2BufferForDevice < 24000) { // if (led1Sample!! < led1BufferForDevice && led1Sample < 24000 && led2Sample!! < led2BufferForDevice && led2Sample < 24000) { // if (deviceRatio < 0.08) { @@ -398,47 +425,47 @@ class HemoCubeFragment : Fragment() { // } // } - if (led1Average < 0 || led2Average < 0) { - activity?.runOnUiThread{ - binding.errorMessage.text = "Warning: Negative Abs. Retake Blank Reading" - binding.errorMessage.visibility = View.VISIBLE - } - } - - DataHolder.hemoCubeTestData?.apply { -// deviceId = deviceSerialNo - led1Buffer = led1BufferForDevice - led2Buffer = led2BufferForDevice - led3Buffer = led3BufferForDevice - led4Buffer = led4BufferForDevice - appVersion = version - this.led1Sample = led1Sample - this.led2Sample = led2Sample - this.led3Sample = led3Sample - this.led4Sample = led4Sample - this.led1Average = led1Average - this.led2Average = led2Average - this.deviceRatio = deviceRatio - this.calculatedRatio = calculateRatio(deviceRatio) - this.coefficients = currentDeviceData?.coefficients?.get(0).toString() + ", " + currentDeviceData?.coefficients?.get(1).toString() - this.classificationResult = findResult(calculatedRatio) - hemoCubeViewModel.messages.postValue(this.classificationResult) - this.resultData = deviceLog - if (!isUsingExistingBuffer) { - with(sharedPreferences.edit()) { - putString(Constants.BUFFER_VALUE_1, led1Buffer.toString()) - putString(Constants.BUFFER_VALUE_2, led2Buffer.toString()) - apply() - } - } - activity?.runOnUiThread { - binding.btnSubmit.visibility = View.VISIBLE - binding.btnSubmit.isEnabled = true - binding.btnSubmit.isClickable = true - binding.clParent.setBackgroundColor(Color.parseColor("#edfffd")) - } + if (led1Average < 0 || led2Average < 0) { + activity?.runOnUiThread{ + binding.errorMessage.text = "Warning: Negative Abs. Retake Blank Reading" + binding.errorMessage.visibility = View.VISIBLE } } + + DataHolder.hemoCubeTestData?.apply { +// deviceId = deviceSerialNo + led1Buffer = led1BufferForDevice + led2Buffer = led2BufferForDevice + led3Buffer = led3BufferForDevice + led4Buffer = led4BufferForDevice + appVersion = version + this.led1Sample = led1Sample + this.led2Sample = led2Sample + this.led3Sample = led3Sample + this.led4Sample = led4Sample + this.led1Average = led1Average + this.led2Average = led2Average + this.deviceRatio = deviceRatio + this.calculatedRatio = calculateRatio(deviceRatio) + this.coefficients = currentDeviceData?.coefficients?.get(0).toString() + ", " + currentDeviceData?.coefficients?.get(1).toString() + this.classificationResult = findResult(calculatedRatio) + hemoCubeViewModel.messages.postValue(this.classificationResult) + this.resultData = deviceLog + if (!isUsingExistingBuffer) { + with(sharedPreferences.edit()) { + putString(Constants.BUFFER_VALUE_1, led1Buffer.toString()) + putString(Constants.BUFFER_VALUE_2, led2Buffer.toString()) + apply() + } + } + activity?.runOnUiThread { + binding.btnSubmit.visibility = View.VISIBLE + binding.btnSubmit.isEnabled = true + binding.btnSubmit.isClickable = true + binding.clParent.setBackgroundColor(Color.parseColor("#edfffd")) + } + } + } catch (e: Exception) { Toast.makeText( requireContext(),