Updated flow of commands to usb.

This commit is contained in:
vsuryakumar
2023-01-25 11:28:38 +05:30
parent 0b99ef9a18
commit dab3d09b3a
2 changed files with 210 additions and 83 deletions

View File

@@ -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
// }
}

View File

@@ -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() {