From dab3d09b3aeb0c5648bc3672589b78b5d46d2b78 Mon Sep 17 00:00:00 2001 From: vsuryakumar Date: Wed, 25 Jan 2023 11:28:38 +0530 Subject: [PATCH] Updated flow of commands to usb. --- .../testRight/TestRightExpReference.kt | 202 ++++++++++++------ .../testRight/TestRightExpSample.kt | 91 ++++++-- 2 files changed, 210 insertions(+), 83 deletions(-) diff --git a/app/src/main/java/com/example/refactoredapp/presentation/testRight/TestRightExpReference.kt b/app/src/main/java/com/example/refactoredapp/presentation/testRight/TestRightExpReference.kt index 414cff1..f10264c 100644 --- a/app/src/main/java/com/example/refactoredapp/presentation/testRight/TestRightExpReference.kt +++ b/app/src/main/java/com/example/refactoredapp/presentation/testRight/TestRightExpReference.kt @@ -8,8 +8,8 @@ import android.view.ViewGroup import androidx.fragment.app.Fragment import androidx.fragment.app.activityViewModels import com.example.refactoredapp.R -import com.example.refactoredapp.data.DataHolder -import com.example.refactoredapp.data.model.TestRightCommands +import com.example.refactoredapp.data.Constants +import com.example.refactoredapp.data.constant.TestRightCommands import com.example.refactoredapp.databinding.FragmentTestRightExpReferenceBinding import com.example.refactoredapp.presentation.UsbServiceListener import com.example.refactoredapp.presentation.utils.MyDialogListener @@ -24,7 +24,6 @@ class TestRightExpReference : Fragment() { private val TAG = "TestRightExpReference" val fullReadOutput = StringBuilder() - private var readingDeviceConstantInProgress = false override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, @@ -39,13 +38,11 @@ class TestRightExpReference : Fragment() { super.onViewCreated(view, savedInstanceState) setupListeners() - if (DataHolder.isReferenceTaken) + if (viewModel.isReferenceDone && viewModel.sampleReadCounter <= Constants.MAXIMUM_TEST_ALLOWED_BEFORE_REFERENCE) moveToSamplePage() if (viewModel.deviceConstant == null) { - binding.progressBar.visibility = View.VISIBLE - readingDeviceConstantInProgress = true - writeAndReadFromDevice(TestRightCommands.read) + sendCmdToFetchDeviceConstant() } } @@ -59,53 +56,13 @@ class TestRightExpReference : Fragment() { getString(R.string.no), object : MyDialogListener { override fun onClickBtnOne() { - binding.progressBar.visibility = View.VISIBLE - writeAndReadFromDevice(TestRightCommands.print_all) + startTakingReference() } override fun onClickBtnTwo() {} }) } - (activity as TestRightActivity).mService.addListener( - object : UsbServiceListener { - override fun onUsbConnect() { - Log.d(TAG, "onUsbConnect() called") - } - - override fun onUsbConnectError(e: Exception?) { - } - - override fun onUsbRead(data: ByteArray?) { -// Log.d(TAG, "onUsbRead(data) call back called") - data?.let { - val stringData = String(it) - fullReadOutput.append(stringData) - - if(stringData.contains("OK", true)){ - if (readingDeviceConstantInProgress) { - readingDeviceConstantInProgress = false - viewModel.mapDeviceConstants(fullReadOutput.toString()) - } else { - viewModel.mapIntensityValues(fullReadOutput.toString(), true) - moveToSamplePage() - } - binding.progressBar.visibility = View.GONE - } - } -// val stringData = String(data!!) -// Log.d(TAG, stringData) - } - - override fun onUsbRead() { - Log.d(TAG, "onSerialRead() call back called") - } - override fun onUsbIoError(e: Exception?) { - Log.e(TAG, "onUsbIoError() called -> $e") - } - } - ) - binding.btnRead.setOnClickListener { // Log.d(TAG, "clicked on text") // val mService = (activity as TestRightActivity).mService @@ -114,11 +71,137 @@ class TestRightExpReference : Fragment() { } } + /** + * Executing the commands sequentially each after successfully executing one. + * 1. command to set LED + * 2. command autoset + * 3. command run + * 4. command print + */ + private fun startTakingReference() { + sendCmdToSetLed() + } - private fun writeAndReadFromDevice(command: TestRightCommands) { - fullReadOutput.clear() - val mService = (activity as TestRightActivity).mService - mService.eventDrivenWrite(command) + private fun sendCmdToFetchDeviceConstant() { + binding.progressBar.visibility = View.VISIBLE + val fullReadOutput = StringBuilder() + (activity as TestRightActivity).mService.eventDrivenWrite(TestRightCommands.read, + object : UsbServiceListener { + override fun onUsbRead(data: ByteArray?) { + data?.let { + val stringData = String(it) + fullReadOutput.append(stringData) + + if (stringData.contains("OK", true)) { + viewModel.mapDeviceConstants(fullReadOutput.toString()) + binding.progressBar.visibility = View.GONE + } + } + } + + override fun onUsbError(e: Exception?) { + Log.e(TAG, "onUsbIoError() called in writeUsbFetchDeviceConstant() -> $e") + binding.progressBar.visibility = View.GONE + } + }) + } + + private fun sendCmdToSetLed() { + binding.progressBar.visibility = View.VISIBLE + val fullReadOutput = StringBuilder() + (activity as TestRightActivity).mService.eventDrivenWrite(TestRightCommands.led2Set50, + object : UsbServiceListener { + override fun onUsbRead(data: ByteArray?) { + data?.let { + val stringData = String(it) + fullReadOutput.append(stringData) + + if (stringData.contains("OK", true)) { + binding.progressBar.visibility = View.GONE + sendCmdToAutoset() + } + } + } + + override fun onUsbError(e: Exception?) { + Log.e(TAG, "onUsbIoError() called in writeUsbFetchDeviceConstant() -> $e") + binding.progressBar.visibility = View.GONE + } + }) + } + + private fun sendCmdToAutoset() { + binding.progressBar.visibility = View.VISIBLE + val fullReadOutput = StringBuilder() + (activity as TestRightActivity).mService.eventDrivenWrite(TestRightCommands.autoset, + object : UsbServiceListener { + override fun onUsbRead(data: ByteArray?) { + data?.let { + val stringData = String(it) + fullReadOutput.append(stringData) + + if (stringData.contains("OK", true)) { + binding.progressBar.visibility = View.GONE + sendCmdToRun() + } + } + } + + override fun onUsbError(e: Exception?) { + Log.e(TAG, "onUsbIoError() called in writeUsbFetchDeviceConstant() -> $e") + binding.progressBar.visibility = View.GONE + } + }) + } + + private fun sendCmdToRun() { + binding.progressBar.visibility = View.VISIBLE + val fullReadOutput = StringBuilder() + (activity as TestRightActivity).mService.eventDrivenWrite(TestRightCommands.run, + object : UsbServiceListener { + override fun onUsbRead(data: ByteArray?) { + data?.let { + val stringData = String(it) + fullReadOutput.append(stringData) + + if (stringData.contains("OK", true)) { + binding.progressBar.visibility = View.GONE + sendCmdToFetchLightIntensities() + } + } + } + + override fun onUsbError(e: Exception?) { + Log.e(TAG, "onUsbIoError() called in writeUsbFetchDeviceConstant() -> $e") + binding.progressBar.visibility = View.GONE + } + }) + } + + private fun sendCmdToFetchLightIntensities() { + binding.progressBar.visibility = View.VISIBLE + val fullReadOutput = StringBuilder() + (activity as TestRightActivity).mService.eventDrivenWrite(TestRightCommands.printAll, + object : UsbServiceListener { + override fun onUsbRead(data: ByteArray?) { + data?.let { + val stringData = String(it) + fullReadOutput.append(stringData) + + if (stringData.contains("OK", true)) { + viewModel.mapIntensityValues(fullReadOutput.toString(), true) + binding.progressBar.visibility = View.GONE + viewModel.isReferenceDone = true + moveToSamplePage() + } + } + } + + override fun onUsbError(e: Exception?) { + Log.e(TAG, "onUsbIoError() called in writeUsbFetchDeviceConstant() -> $e") + binding.progressBar.visibility = View.GONE + } + }) } private fun moveToSamplePage() { @@ -140,20 +223,5 @@ class TestRightExpReference : Fragment() { // mService.read() // }, 10000) // -// } - - override fun onDestroy() { - super.onDestroy() - (activity as TestRightActivity).mService.removeListener() - } - -// fun Button.disable() { -// background.setColorFilter(Color.GRAY, PorterDuff.Mode.MULTIPLY) -// isClickable = false -// } -// -// fun Button.enable() { -// background.setColorFilter(Color.BLUE, PorterDuff.Mode.MULTIPLY) -// isClickable = true // } } \ No newline at end of file diff --git a/app/src/main/java/com/example/refactoredapp/presentation/testRight/TestRightExpSample.kt b/app/src/main/java/com/example/refactoredapp/presentation/testRight/TestRightExpSample.kt index ee72f03..c17967e 100644 --- a/app/src/main/java/com/example/refactoredapp/presentation/testRight/TestRightExpSample.kt +++ b/app/src/main/java/com/example/refactoredapp/presentation/testRight/TestRightExpSample.kt @@ -1,12 +1,7 @@ package com.example.refactoredapp.presentation.testRight -import android.R.attr.button -import android.content.res.ColorStateList -import android.content.res.Resources -import android.graphics.Color -import android.graphics.LightingColorFilter -import android.graphics.PorterDuff import android.os.Bundle +import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -15,14 +10,18 @@ import androidx.core.content.ContextCompat import androidx.fragment.app.Fragment import androidx.fragment.app.activityViewModels import com.example.refactoredapp.R -import com.example.refactoredapp.data.model.PatientDetails +import com.example.refactoredapp.data.constant.TestRightCommands +import com.example.refactoredapp.data.model.PatientData import com.example.refactoredapp.databinding.FragmentTestRightExpSampleBinding +import com.example.refactoredapp.presentation.UsbServiceListener class TestRightExpSample : Fragment() { private lateinit var binding: FragmentTestRightExpSampleBinding private val viewModel: TestRightViewModel by activityViewModels() + private val TAG = "TestRightExpSample" + override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? @@ -45,16 +44,12 @@ class TestRightExpSample : Fragment() { startAcquiring() } } - binding.etNameBlock.setOnClickListener { binding.etName.error = "" } - binding.etAgeBlock.setOnClickListener { binding.etAge.error = "" } - binding.etGenderBlock.setOnClickListener { binding.ddGender.error = "" } + binding.etNameBlock.setOnClickListener { binding.etName.isErrorEnabled = false } + binding.etAgeBlock.setOnClickListener { binding.etAge.isErrorEnabled = false } + binding.etGenderBlock.setOnClickListener { binding.ddGender.isErrorEnabled = false } } - private fun startAcquiring() { - - } - - private fun isValidInput() : PatientDetails? { + private fun isValidInput() : PatientData? { val name = binding.etName.editText?.text?.trim() if (name.isNullOrEmpty()){ binding.etName.error = getString(R.string.name_error) @@ -73,7 +68,71 @@ class TestRightExpSample : Fragment() { binding.ddGender.error = getString(R.string.gender_error) return null } - return PatientDetails(name.toString(), age.toString().toInt(), gender.toString()) + return PatientData(name.toString(), age.toString().toInt(), gender.toString(), null) + } + + /** + * Executing the commands sequentially each after successfully executing one. + * 1. command run + * 2. command print + */ + private fun startAcquiring() { + sendCmdToRun() + } + + private fun sendCmdToRun() { + binding.progressBar.visibility = View.VISIBLE + val fullReadOutput = StringBuilder() + (activity as TestRightActivity).mService.eventDrivenWrite( + TestRightCommands.run, + object : UsbServiceListener { + override fun onUsbRead(data: ByteArray?) { + data?.let { + val stringData = String(it) + fullReadOutput.append(stringData) + + if (stringData.contains("OK", true)) { + binding.progressBar.visibility = View.GONE + sendCmdToFetchLightIntensities() + } + } + } + + override fun onUsbError(e: Exception?) { + Log.e(TAG, "onUsbIoError() called in writeUsbFetchDeviceConstant() -> $e") + binding.progressBar.visibility = View.GONE + } + }) + } + + private fun sendCmdToFetchLightIntensities() { + binding.progressBar.visibility = View.VISIBLE + val fullReadOutput = StringBuilder() + (activity as TestRightActivity).mService.eventDrivenWrite(TestRightCommands.printAll, + object : UsbServiceListener { + override fun onUsbRead(data: ByteArray?) { + data?.let { + val stringData = String(it) + fullReadOutput.append(stringData) + + if (stringData.contains("OK", true)) { + viewModel.mapIntensityValues(fullReadOutput.toString(), false) + binding.progressBar.visibility = View.GONE + showResultsAfterAcquiring() + } + } + } + + override fun onUsbError(e: Exception?) { + Log.e(TAG, "onUsbIoError() called in writeUsbFetchDeviceConstant() -> $e") + binding.progressBar.visibility = View.GONE + } + }) + } + + private fun showResultsAfterAcquiring() { + viewModel.calculateResults() + } private fun Button.disable() {