added methods to parse read string from device into java objects + added progress bar

This commit is contained in:
vsuryakumar
2023-01-24 17:13:05 +05:30
parent dc52b14335
commit c6599a9f41
11 changed files with 214 additions and 30 deletions

View File

@@ -6,4 +6,6 @@ object Constants {
const val WRITE_TIMEOUT_MILLIS = 60000 // 1 min
const val READ_TIMEOUT_MILLIS = 60000 // 1 min
// const val TEST_RIGHT_TOTAL_PIXEL = 3694
const val TEST_RIGHT_TOTAL_PIXEL = 12
}

View File

@@ -16,4 +16,5 @@ enum class TestRightCommands(val command: String) {
autoset("autoset\r"),
print("print 239 2719\r"),
print_all("print\r"),
}

View File

@@ -1,8 +1,8 @@
package com.example.refactoredapp.data.model
data class TestRightDeviceConstants(
val a: String,
val b: String,
var a: String,
var b: String,
val c: String,
val d: String
)

View File

@@ -66,13 +66,9 @@ class TestRightActivity : AppCompatActivity() {
setContentView(binding.root)
viewModel = ViewModelProvider(this, MyViewModelFactory(this.applicationContext))[TestRightViewModel::class.java]
connectUsb(false);
// if(connectUsb(false)) {
// moveToNext()
// } else {
// onBackPressed()
// }
connectUsb(false);
}

View File

@@ -1,7 +1,6 @@
package com.example.refactoredapp.presentation.testRight
import android.os.Bundle
import android.os.Handler
import android.util.Log
import android.view.LayoutInflater
import android.view.View
@@ -24,7 +23,8 @@ class TestRightExpReference : Fragment() {
private val TAG = "TestRightExpReference"
var fullString: String = ""
val fullReadOutput = StringBuilder()
private var readingDeviceConstantInProgress = false
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
@@ -40,9 +40,13 @@ class TestRightExpReference : Fragment() {
setupListeners()
if (DataHolder.isReferenceTaken)
parentFragmentManager.beginTransaction().replace(R.id.fl_main, TestRightExpSample())
.commit()
moveToSamplePage()
if (viewModel.deviceConstant == null) {
binding.progressBar.visibility = View.VISIBLE
readingDeviceConstantInProgress = true
writeAndReadFromDevice(TestRightCommands.read)
}
}
private fun setupListeners() {
@@ -55,7 +59,8 @@ class TestRightExpReference : Fragment() {
getString(R.string.no),
object : MyDialogListener {
override fun onClickBtnOne() {
startTakingReference()
binding.progressBar.visibility = View.VISIBLE
writeAndReadFromDevice(TestRightCommands.print_all)
}
override fun onClickBtnTwo() {}
@@ -75,11 +80,17 @@ class TestRightExpReference : Fragment() {
// Log.d(TAG, "onUsbRead(data) call back called")
data?.let {
val stringData = String(it)
// Log.d(TAG, stringData)
fullString += stringData
fullReadOutput.append(stringData)
if(stringData.contains("OK")){
viewModel.mapValues(fullString)
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!!)
@@ -99,16 +110,27 @@ class TestRightExpReference : Fragment() {
// Log.d(TAG, "clicked on text")
// val mService = (activity as TestRightActivity).mService
// mService.read()
Log.d(TAG, "FULL STRING IS -> $fullString")
Log.d(TAG, "FULL STRING IS -> $fullReadOutput")
}
}
private fun startTakingReference() {
private fun writeAndReadFromDevice(command: TestRightCommands) {
fullReadOutput.clear()
val mService = (activity as TestRightActivity).mService
mService.eventDrivenWrite(TestRightCommands.print_all)
mService.eventDrivenWrite(command)
}
private fun moveToSamplePage() {
parentFragmentManager.beginTransaction().replace(R.id.fl_main, TestRightExpSample())
.commit()
}
// private fun startReadingReference() {
// val mService = (activity as TestRightActivity).mService
// mService.eventDrivenWrite(TestRightCommands.print_all)
// }
// private fun startTakingReference() {
// val mService = (activity as TestRightActivity).mService
// mService.write(TestRightCommands.print)

View File

@@ -1,16 +1,107 @@
package com.example.refactoredapp.presentation.testRight
import android.util.Log
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.example.refactoredapp.data.Constants
import com.example.refactoredapp.data.model.TestRightDeviceConstants
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlin.math.pow
class TestRightViewModel : ViewModel() {
var isReferenceDone = false
var isServiceConnected = false
private val TAG = "TestRightViewModel"
val isUsbConnected = MutableLiveData(false)
fun mapValues(fullString: String) {
TODO("Not yet implemented")
var deviceConstant: TestRightDeviceConstants? = null
/* Contains wavelength -> pixel no.*/
val wavelengthToPixelArray = ArrayList<ArrayList<Double>>()
/* (For reference/baseline) Contains pixel no. -> Intensity of light falling on that pixel */
val intensityReferenceArray = ArrayList<ArrayList<Double>>()
/* (For exp sample) Contains pixel no. -> Intensity of light falling on that pixel */
val intensitySampleArray = ArrayList<ArrayList<Double>>()
fun mapDeviceConstants(string: String) {
// viewModelScope.launch {
if (string.isNotEmpty()) {
val listOfStrings = string.split(",")
if (listOfStrings.size >= 4) {
deviceConstant = TestRightDeviceConstants(
listOfStrings[0].trim(),
listOfStrings[1].trim(),
listOfStrings[2].trim(),
listOfStrings[3].trim()
)
} else {
// Todo: Throws error
}
} else {
// Todo: Throws error (showing error if empty by using a mutable error string)
}
// }
}
fun mapPixelNumberToWavelength() {
// viewModelScope.launch {
if (deviceConstant != null) {
for (x in 1..Constants.TEST_RIGHT_TOTAL_PIXEL) {
val wavelength: Double =
(deviceConstant!!.a.toDouble() * (x.toDouble().pow((3).toDouble()))) +
(deviceConstant!!.b.toDouble() * (x.toDouble().pow((2).toDouble()))) +
(deviceConstant!!.c.toDouble() * x) +
(deviceConstant!!.d.toDouble())
val temp = ArrayList<Double>(2)
temp.add(wavelength)
temp.add(x.toDouble())
wavelengthToPixelArray.add(temp)
}
} else {
// Todo: Throws error
}
// }
}
fun mapIntensityValues(fullString: String, isReference: Boolean) {
// viewModelScope.launch {
val listOfString = fullString.split("\n")
for (line in listOfString) {
if ("Buf" in line) {
/* Removing trailing spaces and "Buf" from the string then taking lhs & rhs of ':' */
val numbers = line.trim()
.substring(3).split(":")
if (numbers.size >= 2) {
val temp = ArrayList<Double>(2)
temp.add(numbers[0].toDouble())
temp.add(numbers[1].toDouble())
if (isReference) intensityReferenceArray.add(temp)
else intensitySampleArray.add(temp)
} else {
// Todo: Error handling
}
}
}
// }
}
// val dispatcher = TestCoroutineDispatcher()
//
// @Before
// fun setup() {
// Dispatchers.setMain(dispatcher)
// }
//
// @After
// fun tearDown() {
// Dispatchers.resetMain()
// }
}