From f0d0740cd739ac25d27132281c471bb14b456aa0 Mon Sep 17 00:00:00 2001 From: Pritimay Sarkar Date: Tue, 7 Nov 2023 05:18:27 +0530 Subject: [PATCH 1/2] firmware changes for 4 leds --- .../hpostesting/data/constant/Constants.kt | 2 + .../presentation/hemocube/HemoCubeFragment.kt | 297 +++++++++++------- 2 files changed, 178 insertions(+), 121 deletions(-) diff --git a/app/src/main/java/com/example/hpostesting/data/constant/Constants.kt b/app/src/main/java/com/example/hpostesting/data/constant/Constants.kt index b79ea1a..56aff13 100644 --- a/app/src/main/java/com/example/hpostesting/data/constant/Constants.kt +++ b/app/src/main/java/com/example/hpostesting/data/constant/Constants.kt @@ -69,5 +69,7 @@ object Constants { const val KIT_COUNT = "KitCount" const val BUFFER_VALUE_1 = "BufferValue1" const val BUFFER_VALUE_2 = "BufferValue2" + const val BUFFER_VALUE_3 = "BufferValue3" + const val BUFFER_VALUE_4 = "BufferValue4" const val DEVICE_ID = "DEVICE_ID" } \ No newline at end of file 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..ea1cfc4 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 @@ -20,6 +20,7 @@ import com.example.hpostesting.data.model.patient.DeviceData import com.example.hpostesting.data.model.patient.toHemoCubeTestData import com.example.hpostesting.presentation.UsbServiceListener import com.example.hpostesting.presentation.dashboard.DashboardActivity +import com.example.hpostesting.presentation.diagnostics.DiagnosticsActivity import com.example.hpostesting.presentation.utils.MyDialogListener import com.example.hpostesting.presentation.utils.UIUtils import com.google.firebase.crashlytics.ktx.crashlytics @@ -42,6 +43,14 @@ class HemoCubeFragment : Fragment() { private var isTestOngoing = false private var startListening = MutableLiveData(false) val testingTrace = Firebase.performance.newTrace("testing_trace") + private var led1BufferForDevice = 0.0 + private var led2BufferForDevice = 0.0 + private var led3BufferForDevice = 0.0 + private var led4BufferForDevice = 0.0 + private var led1SampleForDevice = 0.0 + private var led2SampleForDevice = 0.0 + private var led3SampleForDevice = 0.0 + private var led4SampleForDevice = 0.0 override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?, @@ -55,6 +64,8 @@ class HemoCubeFragment : Fragment() { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) initViews() + listenToHemoCube() + getDeviceInfo() observeViewModel() // checkAndStartProcess() } @@ -81,6 +92,7 @@ class HemoCubeFragment : Fragment() { binding.btnGo.visibility = View.GONE binding.btnSubmit.isEnabled = false binding.btnSubmit.isClickable = false + binding.btnPlacebuffer.visibility = View.GONE binding.tvName.text = "Name: ${testDetails?.name}\n ID: ${testDetails?._id}" @@ -90,19 +102,19 @@ class HemoCubeFragment : Fragment() { binding.btnSamplestart.setOnClickListener { activity?.runOnUiThread { binding.tvSubtitle4.visibility = View.VISIBLE - binding.tvSubtitle4.text = "Place Sample" + binding.tvSubtitle4.text = "Sample Started" } startSampleProcess() it.visibility = View.GONE } binding.btnPlacebuffer.setOnClickListener { - if (isBufferValueAvailable()){ + if (isBufferValueAvailable()) { showBufferAlertDialog() - }else{ + } else { activity?.runOnUiThread { binding.tvSubtitle4.visibility = View.VISIBLE - binding.tvSubtitle4.text = "Place Buffer" +// binding.tvSubtitle4.text = "Buffer Started" } checkAndStartProcess() @@ -155,7 +167,7 @@ class HemoCubeFragment : Fragment() { } hemoCubeViewModel.messages.observe(viewLifecycleOwner) { - binding.tvSubtitle4.text = it +// binding.tvSubtitle4.text = it } // startListening.observe(viewLifecycleOwner) { // if (it) { @@ -176,19 +188,37 @@ class HemoCubeFragment : Fragment() { } private fun checkAndStartProcess() { - listenToHemoCube() +// listenToHemoCube() startBufferProcess() } private fun isBufferValueAvailable(): Boolean { - return if (sharedPreferences.getString( - Constants.BUFFER_VALUE_1, "" - ) != "" && sharedPreferences.getString(Constants.BUFFER_VALUE_2, "") != "" - ) { - sharedPreferences.getString(Constants.BUFFER_VALUE_1, "") - ?.toDouble()!! > 0.0 && sharedPreferences.getString(Constants.BUFFER_VALUE_2, "") - ?.toDouble()!! > 0.0 - } else { + return try { + if (sharedPreferences.getString( + Constants.BUFFER_VALUE_1, "" + ) != "" && sharedPreferences.getString(Constants.BUFFER_VALUE_2, "") != "" + && sharedPreferences.getString(Constants.BUFFER_VALUE_3, "") != "" + && sharedPreferences.getString(Constants.BUFFER_VALUE_4, "") != "" + ) { + sharedPreferences.getString(Constants.BUFFER_VALUE_1, "") + ?.toDouble()!! > 0.0 && sharedPreferences.getString( + Constants.BUFFER_VALUE_2, + "" + ) + ?.toDouble()!! > 0.0 && sharedPreferences.getString( + Constants.BUFFER_VALUE_3, + "" + ) + ?.toDouble()!! > 0.0 && sharedPreferences.getString( + Constants.BUFFER_VALUE_4, + "" + ) + ?.toDouble()!! > 0.0 + } else { + false + } + } catch (e: Exception) { + showToast("error while getting existing buffer") false } } @@ -206,13 +236,17 @@ class HemoCubeFragment : Fragment() { positiveText, object : MyDialogListener { override fun onClickNegativeButton() { - listenToHemoCube() +// listenToHemoCube() startBufferProcess() } override fun onClickPositiveButton() { - listenToHemoCube() - startSampleProcess() +// listenToHemoCube() +// startSampleProcess() + activity?.runOnUiThread { + binding.btnPlacebuffer.visibility = View.GONE + binding.btnSamplestart.visibility = View.VISIBLE + } isUsingExistingBuffer = true } }) @@ -248,6 +282,25 @@ class HemoCubeFragment : Fragment() { } } + private fun getDeviceInfo() { + hemoCubeViewModel.progressBar.postValue(true) + (activity as HemocubeActivity).mService.sendAndListenToHemoCube( + HemoCubeCommands.getDeviceId, + object : UsbServiceListener { + override fun onUsbRead(data: ByteArray?) { + data?.let { + val stringData = String(it) + hemoCubeViewModel.messages.postValue(stringData) + binding.tvSubtitle4.text = stringData + } + } + + override fun onUsbError(e: Exception?) { + hemoCubeViewModel.progressBar.postValue(false) + } + }) + } + private fun handleUsbData(stringData: String, fullReadOutput: StringBuilder) { if (stringData.contains("#")) { hemoCubeViewModel.messages.postValue(stringData) @@ -255,44 +308,75 @@ class HemoCubeFragment : Fragment() { } resultData += stringData - Log.e("nametext", stringData) when { - stringData.contains("#BC") -> { + stringData.contains("SN") -> { + val slData = stringData.split(" ") + if (slData.size > 1) { + val hardwareId = slData[1].trim() + with(sharedPreferences.edit()) { + putString(Constants.DEVICE_ID, hardwareId) + apply() + } + } activity?.runOnUiThread { + binding.btnPlacebuffer.visibility = View.VISIBLE + } + } + + stringData.contains("#BC") -> { + activity?.runOnUiThread { + binding.tvSubtitle4.text = "Buffer Completed" binding.btnSamplestart.visibility = View.VISIBLE } } + stringData.contains("#SC") -> { - binding.btnSubmit.visibility = View.VISIBLE + binding.tvSubtitle4.text = "Sample Completed" fetchResult() testingTrace.stop() } - stringData.contains("RESULT") || resultData.contains("REND") -> { - var validString: String - val results: List - if (stringData.contains("RESULT")) { - validString = isValidResult(stringData) - if (validString.isEmpty()) { - results = resultData.split("\n") - validString = parseResult(results) - } + resultData.contains("REND") -> { + val resultLines = resultData.split("\\s+(?=LB|LS)".toRegex()) + var bufferIntensity = resultLines[1].split(' ')[1].trim() + led1BufferForDevice = if (isUsingExistingBuffer) { + sharedPreferences.getString(Constants.BUFFER_VALUE_1, "")?.toDoubleOrNull()!! } else { - results = resultData.split("\n") - validString = parseResult(results) + bufferIntensity.toDoubleOrNull()!! } - if (validString.isNotEmpty()) { - handleValidResult(validString, resultData) + bufferIntensity = resultLines[2].split(' ')[1].trim() + led2BufferForDevice = if (isUsingExistingBuffer) { + sharedPreferences.getString(Constants.BUFFER_VALUE_2, "")?.toDoubleOrNull()!! + } else { + bufferIntensity.toDoubleOrNull()!! } + bufferIntensity = resultLines[3].split(' ')[1].trim() + led3BufferForDevice = if (isUsingExistingBuffer) { + sharedPreferences.getString(Constants.BUFFER_VALUE_3, "")?.toDoubleOrNull()!! + } else { + bufferIntensity.toDoubleOrNull()!! + } + bufferIntensity = resultLines[4].split(' ')[1].trim() + led4BufferForDevice = if (isUsingExistingBuffer) { + sharedPreferences.getString(Constants.BUFFER_VALUE_4, "")?.toDoubleOrNull()!! + } else { + bufferIntensity.toDoubleOrNull()!! + } + + var sampleIntensity = resultLines[5].split(' ')[1].trim() + led1SampleForDevice = resultLines[5].split(' ')[1].trim().toDoubleOrNull()!! + sampleIntensity = resultLines[6].split(' ')[1].trim() + led2SampleForDevice = resultLines[6].split(' ')[1].trim().toDoubleOrNull()!! + sampleIntensity = resultLines[7].split(' ')[1].trim() + led3SampleForDevice = resultLines[7].split(' ')[1].trim().toDoubleOrNull()!! + sampleIntensity = resultLines[8].split(' ')[1].split('\r')[0].trim() + led4SampleForDevice = resultLines[8].split(' ')[1].split('\r')[0].trim().toDoubleOrNull()!! + processResult() } } } - - - - private fun isValidResult(line: String): String { return if (line.contains("RESULT") && line.split(" ").size == 8) { line @@ -348,95 +432,64 @@ class HemoCubeFragment : Fragment() { } } - private fun handleValidResult(validString: String, fullReadOutput: String) { + private fun processResult() { 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 + val deviceLog = resultData - val pInfo = requireActivity().packageManager.getPackageInfo( - requireActivity().packageName, 0 - ) - val version = pInfo.versionName + val pInfo = requireActivity().packageManager.getPackageInfo( + requireActivity().packageName, 0 + ) + val version = pInfo.versionName - 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(led1SampleForDevice!!) ?: 0.0) + val led2Average = log10(led2BufferForDevice?.div(led2SampleForDevice!!) ?: 0.0) + val led3Average = log10(led3BufferForDevice?.div(led3SampleForDevice!!) ?: 0.0) + val led4Average = log10(led4BufferForDevice?.div(led4SampleForDevice!!) ?: 0.0) + val deviceRatio = led1Average / led3Average - 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) { -// if (deviceRatio > 1.07) { -// -// } -// } -// } -// } + if (led1Average < 0 || led2Average < 0) { + activity?.runOnUiThread { + binding.errorMessage.text = "Warning: Negative Abs. Retake Blank Reading" + binding.errorMessage.visibility = View.VISIBLE + } + } - 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 = sharedPreferences.getString(Constants.DEVICE_ID, "").toString() + led1Buffer = led1BufferForDevice + led2Buffer = led2BufferForDevice + led3Buffer = led3BufferForDevice + led4Buffer = led4BufferForDevice + appVersion = version + this.led1Sample = led1SampleForDevice + this.led2Sample = led2SampleForDevice + this.led3Sample = led3SampleForDevice + this.led4Sample = led4SampleForDevice + this.led1Average = led1Average + this.led2Average = led2Average + this.led3Average = led3Average + this.led4Average = led4Average + 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, led1BufferForDevice.toString()) + putString(Constants.BUFFER_VALUE_2, led2BufferForDevice.toString()) + putString(Constants.BUFFER_VALUE_3, led3BufferForDevice.toString()) + putString(Constants.BUFFER_VALUE_4, led4BufferForDevice.toString()) + apply() } } - - 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")) - } + activity?.runOnUiThread { + binding.btnSubmit.visibility = View.VISIBLE + binding.btnSubmit.isEnabled = true + binding.btnSubmit.isClickable = true + binding.clParent.setBackgroundColor(Color.parseColor("#edfffd")) } } } catch (e: Exception) { @@ -484,6 +537,10 @@ class HemoCubeFragment : Fragment() { private fun startBufferProcess() { hemoCubeViewModel.progressBar.postValue(true) + activity?.runOnUiThread { + binding.btnPlacebuffer.visibility = View.GONE + } + (activity as HemocubeActivity).mService.sendAndListenToHemoCube(HemoCubeCommands.startBuffer, object : UsbServiceListener { override fun onUsbRead(data: ByteArray?) {} @@ -542,8 +599,6 @@ class HemoCubeFragment : Fragment() { return "" } - - private fun checkDeviceIdMismatch(deviceId: String, loginId: String): Boolean { if (!deviceId.isNullOrEmpty() && !loginId.isNullOrEmpty() && deviceId.last() != loginId.last()) return true From 9d86d874af6e540a672c70d4fa10c14d7afc678e Mon Sep 17 00:00:00 2001 From: Pritimay Sarkar Date: Tue, 7 Nov 2023 07:38:10 +0530 Subject: [PATCH 2/2] fix testing steps --- .../presentation/hemocube/HemoCubeFragment.kt | 91 ++++--------------- 1 file changed, 18 insertions(+), 73 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 ea1cfc4..c899460 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 @@ -42,7 +42,7 @@ class HemoCubeFragment : Fragment() { private var isUsingExistingBuffer = false private var isTestOngoing = false private var startListening = MutableLiveData(false) - val testingTrace = Firebase.performance.newTrace("testing_trace") + private val testingTrace = Firebase.performance.newTrace("testing_trace") private var led1BufferForDevice = 0.0 private var led2BufferForDevice = 0.0 private var led3BufferForDevice = 0.0 @@ -67,7 +67,6 @@ class HemoCubeFragment : Fragment() { listenToHemoCube() getDeviceInfo() observeViewModel() -// checkAndStartProcess() } private fun initViews() { @@ -79,11 +78,6 @@ class HemoCubeFragment : Fragment() { isOnline, true, sharedPreferences.getString(Constants.KIT_NUMBER, "") ) } - if (isTestOngoing) { -// binding.btnKit.visibility = View.GONE -// binding.btnKit.isEnabled = false -// binding.btnKit.isClickable = false - } binding.tvTitle2.visibility = View.GONE binding.etAbhaId.visibility = View.GONE @@ -94,10 +88,8 @@ class HemoCubeFragment : Fragment() { binding.btnSubmit.isClickable = false binding.btnPlacebuffer.visibility = View.GONE - binding.tvName.text = "Name: ${testDetails?.name}\n ID: ${testDetails?._id}" - Log.e("nametext", binding.tvName.text.toString()) - + binding.tvSubtitle4.text = "Config" binding.btnSamplestart.setOnClickListener { activity?.runOnUiThread { @@ -167,28 +159,11 @@ class HemoCubeFragment : Fragment() { } hemoCubeViewModel.messages.observe(viewLifecycleOwner) { -// binding.tvSubtitle4.text = it + binding.tvSubtitle4.text = it } -// startListening.observe(viewLifecycleOwner) { -// if (it) { -// lifecycleScope.launch { -// delay(1000) -// if (binding.tvSubtitle4.text.toString().isEmpty()) { -// (activity as HemocubeActivity).reconnectDevice() -// if (isBufferValueAvailable()) { -// showBufferAlertDialog() -// } else { -// listenToHemoCube() -// startBufferProcess() -// } -// } -// } -// } -// } } private fun checkAndStartProcess() { -// listenToHemoCube() startBufferProcess() } @@ -236,16 +211,14 @@ class HemoCubeFragment : Fragment() { positiveText, object : MyDialogListener { override fun onClickNegativeButton() { -// listenToHemoCube() startBufferProcess() } override fun onClickPositiveButton() { -// listenToHemoCube() -// startSampleProcess() activity?.runOnUiThread { binding.btnPlacebuffer.visibility = View.GONE binding.btnSamplestart.visibility = View.VISIBLE + binding.tvSubtitle4.text = "Place Sample" } isUsingExistingBuffer = true } @@ -303,7 +276,6 @@ class HemoCubeFragment : Fragment() { private fun handleUsbData(stringData: String, fullReadOutput: StringBuilder) { if (stringData.contains("#")) { - hemoCubeViewModel.messages.postValue(stringData) isTestOngoing = true } @@ -322,6 +294,13 @@ class HemoCubeFragment : Fragment() { activity?.runOnUiThread { binding.btnPlacebuffer.visibility = View.VISIBLE } + hemoCubeViewModel.messages.postValue("Start Buffer") + } + + stringData.contains("#BS") -> { + activity?.runOnUiThread { + binding.tvSubtitle4.text = "Buffer Started" + } } stringData.contains("#BC") -> { @@ -331,6 +310,13 @@ class HemoCubeFragment : Fragment() { } } + stringData.contains("#SS") -> { + activity?.runOnUiThread { + binding.tvSubtitle4.text = "Sample Started" + binding.btnSamplestart.visibility = View.GONE + } + } + stringData.contains("#SC") -> { binding.tvSubtitle4.text = "Sample Completed" fetchResult() @@ -377,14 +363,6 @@ class HemoCubeFragment : Fragment() { } } - private fun isValidResult(line: String): String { - return if (line.contains("RESULT") && line.split(" ").size == 8) { - line - } else { - "" - } - } - private fun showError() { activity?.runOnUiThread { UIUtils.createAlertDialog(requireContext(), @@ -414,24 +392,6 @@ class HemoCubeFragment : Fragment() { } } - private fun showStartSampleDialog() { - activity?.runOnUiThread { - UIUtils.createAlertDialog(requireContext(), - "Start Sample", - "Do you want to start sample reading?", - getString(R.string.no), - "Yes", - object : MyDialogListener { - override fun onClickNegativeButton() {} - - override fun onClickPositiveButton() { - listenToHemoCube() - startSampleProcess() - } - }) - } - } - private fun processResult() { try { val deviceLog = resultData @@ -584,21 +544,6 @@ class HemoCubeFragment : Fragment() { return coefficient1 * ratio + coefficient2 } - private fun parseResult(frames: List): String { - val lines = mutableListOf() - for (frame in frames.reversed()) { - if (frame.contains("REND") || frame.contains("RESULT")) lines += frame - if (frame.contains("RESULT")) { - break - } - } - val line = lines.reversed().joinToString("").trim() - if (line.contains("RESULT") && line.split(" ").size == 8) { - return line - } - return "" - } - private fun checkDeviceIdMismatch(deviceId: String, loginId: String): Boolean { if (!deviceId.isNullOrEmpty() && !loginId.isNullOrEmpty() && deviceId.last() != loginId.last()) return true