add device data parsing and saving calcualtion data

This commit is contained in:
Pritimay Sarkar
2023-08-03 23:01:49 +05:30
parent 4c7c24449b
commit 547b5d8663
3 changed files with 82 additions and 76 deletions

View File

@@ -107,15 +107,18 @@ class HemoCubeFragment : Fragment() {
"Yes", "Yes",
object : MyDialogListener { object : MyDialogListener {
override fun onClickNegativeButton() { override fun onClickNegativeButton() {
listenToHemoCube()
startBuffer() startBuffer()
} }
override fun onClickPositiveButton() { override fun onClickPositiveButton() {
listenToHemoCube()
startSample() startSample()
} }
} }
) )
} else { } else {
listenToHemoCube()
startBuffer() startBuffer()
} }
} }
@@ -126,37 +129,13 @@ class HemoCubeFragment : Fragment() {
} }
private fun setupButtonClickListeners() { private fun setupButtonClickListeners() {
binding.btnSubmit.setOnClickListener { binding.btnSubmit.setOnClickListener {
binding.progressBar.visibility = View.VISIBLE binding.progressBar.visibility = View.VISIBLE
getResult()
if (isOnline) {
getResult()
resultRatio?.let { ratio ->
Log.e("Testdb", "upload")
hemoCubeViewModel.uploadHemoCubeResultToDatabase(
requireContext(), isOnline, true, kitSerial, ratio, resultData, testTime
)
}
binding.progressBar.visibility = View.VISIBLE
} else {
getResult()
resultRatio?.let { ratio ->
hemoCubeViewModel.uploadHemoCubeResultToDatabase(
requireContext(), isOnline, true, kitSerial, ratio, resultData, testTime
)
}
Toast.makeText(
requireContext(),
"Internet not available, Test Data added to Local DB.",
Toast.LENGTH_SHORT
).show()
}
} }
binding.btnSubmit.isEnabled = false binding.btnSubmit.isEnabled = true
binding.btnSubmit.isClickable = false binding.btnSubmit.isClickable = true
} }
private fun listenToHemoCube() { private fun listenToHemoCube() {
@@ -172,16 +151,64 @@ class HemoCubeFragment : Fragment() {
val stringData = String(it) val stringData = String(it)
fullReadOutput.append(stringData) fullReadOutput.append(stringData)
if (fullReadOutput.contains("#")) { if (stringData.contains("#")) {
binding.tvSubtitle4.text = fullReadOutput binding.tvSubtitle4.text = stringData
} }
resultData += fullReadOutput.toString() resultData += fullReadOutput.toString()
DataHolder.hemoCubeTestData?.resultData DataHolder.hemoCubeTestData?.resultData
if (fullReadOutput.contains("#Sample Completed")) { // if (stringData.contains("#Sample Completedok")) {
binding.btnSubmit.isEnabled = false // binding.btnSubmit.isEnabled = true
binding.btnSubmit.isClickable = false // binding.btnSubmit.isClickable = true
} // }
hemoCubeViewModel.progressBar.postValue(false) hemoCubeViewModel.progressBar.postValue(false)
if (resultData.contains("RESULT")) {
// val response: String = "RESULT SN 18291 1937.23 2828.11 28211.20 121829.12"
val results = resultData.split("\n")
val validStringFound = checkAndFindResults(results)
if (!validStringFound.isNullOrEmpty()) {
val response = validStringFound
val result = response.split(" ")
if (result.size == 8) {
val deviceSerialNo = result[2]
val led1Buffer = result[3].toDoubleOrNull()
val led2Buffer = result[4].toDoubleOrNull()
val led1Sample = result[5].toDoubleOrNull()
val led2Sample = result[6].toDoubleOrNull()
val led1Average = log10(led1Buffer?.div(led1Sample!!) ?: 0.0)
val led2Average = log10(led2Buffer?.div(led2Sample!!) ?: 0.0)
val deviceRatio = led1Average / led2Average
DataHolder.hemoCubeTestData?.deviceSerialNumber = deviceSerialNo
DataHolder.hemoCubeTestData?.led1Buffer = led1Buffer
DataHolder.hemoCubeTestData?.led2Buffer = led2Buffer
DataHolder.hemoCubeTestData?.led1Sample = led1Sample
DataHolder.hemoCubeTestData?.led2Sample = led2Sample
DataHolder.hemoCubeTestData?.led1Average = led1Average
DataHolder.hemoCubeTestData?.led2Average = led2Average
DataHolder.hemoCubeTestData?.deviceRatio = deviceRatio
DataHolder.hemoCubeTestData?.calculatedRatio =
calculateRatio(deviceRatio)
}
}
hemoCubeViewModel.progressBar.postValue(false)
if (isOnline) {
resultRatio?.let { ratio ->
Log.e("Testdb", "upload")
hemoCubeViewModel.uploadHemoCubeResultToDatabase(isOnline, true, kitSerial
)
}
binding.progressBar.visibility = View.VISIBLE
} else {
resultRatio?.let { ratio ->
hemoCubeViewModel.uploadHemoCubeResultToDatabase(isOnline, true, kitSerial)
}
Toast.makeText(
requireContext(),
"Internet not available, Test Data added to Local DB.",
Toast.LENGTH_SHORT
).show()
}
}
} }
} }
@@ -224,45 +251,11 @@ class HemoCubeFragment : Fragment() {
private fun getResult() { private fun getResult() {
hemoCubeViewModel.progressBar.postValue(true) hemoCubeViewModel.progressBar.postValue(true)
val fullReadOutput = StringBuilder()
(activity as HemocubeActivity).mService.sendAndListenToHemoCube( (activity as HemocubeActivity).mService.sendAndListenToHemoCube(
HemoCubeCommands.getSample, HemoCubeCommands.getSample,
object : UsbServiceListener { object : UsbServiceListener {
override fun onUsbRead(data: ByteArray?) { override fun onUsbRead(data: ByteArray?) {}
data?.let {
val stringData = String(it)
fullReadOutput.append(stringData)
if (stringData.contains("RESULT", true)) {
// hemoCubeViewModel.mapDeviceConstants(fullReadOutput.toString())
// val response: String = "RESULT SN18291 1937.23 2828.11 28211.20 121829.12"
val response = stringData
val result = response.split(" ")
val deviceSerialNo = result[1]
val led1Buffer = result[2].toDoubleOrNull()
val led2Buffer = result[3].toDoubleOrNull()
val led1Sample = result[4].toDoubleOrNull()
val led2Sample = result[5].toDoubleOrNull()
val led1Average = log10(led1Buffer?.div(led1Sample!!) ?: 0.0)
val led2Average = log10(led2Buffer?.div(led2Sample!!) ?: 0.0)
val deviceRatio = led1Average / led2Average
DataHolder.hemoCubeTestData?.deviceSerialNumber = deviceSerialNo
DataHolder.hemoCubeTestData?.led1Buffer = led1Buffer
DataHolder.hemoCubeTestData?.led2Buffer = led2Buffer
DataHolder.hemoCubeTestData?.led1Sample = led1Sample
DataHolder.hemoCubeTestData?.led2Sample = led2Sample
DataHolder.hemoCubeTestData?.led1Average = led1Average
DataHolder.hemoCubeTestData?.led2Average = led2Average
DataHolder.hemoCubeTestData?.deviceRatio = deviceRatio
DataHolder.hemoCubeTestData?.calculatedRatio = calculateRatio(deviceRatio)
hemoCubeViewModel.progressBar.postValue(false)
}
}
}
override fun onUsbError(e: Exception?) { override fun onUsbError(e: Exception?) {
hemoCubeViewModel.progressBar.postValue(false) hemoCubeViewModel.progressBar.postValue(false)
} }
@@ -277,4 +270,20 @@ class HemoCubeFragment : Fragment() {
val c2 = deviceData?.coefficients?.get(1)!! val c2 = deviceData?.coefficients?.get(1)!!
return c1 * ratio + c2 return c1 * ratio + c2
} }
private fun checkAndFindResults(results: List<String>): String {
val reversedResults = results.reversed()
for (line in reversedResults) {
if (isValidFormat(line))
return line
}
return ""
}
private fun isValidFormat(line: String): Boolean {
if (line.contains("RESULT") && line.split(" ").size == 8) {
return true
}
return false
}
} }

View File

@@ -39,16 +39,10 @@ class HemoCubeViewModel @Inject constructor(
val networkStatusLiveData: LiveData<Boolean> val networkStatusLiveData: LiveData<Boolean>
get() = _networkStatusLiveData get() = _networkStatusLiveData
val fireBaseUpload = MutableLiveData<String>() val fireBaseUpload = MutableLiveData<String>()
fun uploadHemoCubeResultToDatabase( fun uploadHemoCubeResultToDatabase(isOnline: Boolean, testStatus: Boolean, kitSerial: String?) = viewModelScope.launch {
context: Context, isOnline: Boolean, testStatus: Boolean, kitSerial: String?,
resultRatio: String,
resultData: String,
testTime: String?
) = viewModelScope.launch {
if (kitSerial != null) { if (kitSerial != null) {
testDetails?.kitSerial = kitSerial testDetails?.kitSerial = kitSerial
} }
testDetails?.testTime = testTime
testDetails?.testStatus = testStatus testDetails?.testStatus = testStatus
try { try {
@@ -87,6 +81,7 @@ class HemoCubeViewModel @Inject constructor(
testDetails?.led1Average = DataHolder.hemoCubeTestData?.led1Average testDetails?.led1Average = DataHolder.hemoCubeTestData?.led1Average
testDetails?.led2Average = DataHolder.hemoCubeTestData?.led2Average testDetails?.led2Average = DataHolder.hemoCubeTestData?.led2Average
testDetails?.deviceRatio = DataHolder.hemoCubeTestData?.deviceRatio testDetails?.deviceRatio = DataHolder.hemoCubeTestData?.deviceRatio
testDetails?.calculatedRatio = DataHolder.hemoCubeTestData?.calculatedRatio
viewModelScope.launch { viewModelScope.launch {
try { try {

View File

@@ -29,6 +29,8 @@ class UsbService : Service() {
} }
var listener: UsbServiceListener? = null var listener: UsbServiceListener? = null
var bus: UsbServiceListener? = null
fun connect(driver: UsbSerialDriver, connection: UsbDeviceConnection) { fun connect(driver: UsbSerialDriver, connection: UsbDeviceConnection) {
mPort = driver.ports[0] // Most devices have just one port (port 0) mPort = driver.ports[0] // Most devices have just one port (port 0)
mPort.open(connection) mPort.open(connection)
@@ -76,7 +78,7 @@ class UsbService : Service() {
fun sendAndListenToHemoCube(command: HemoCubeCommands, listener: UsbServiceListener) { fun sendAndListenToHemoCube(command: HemoCubeCommands, listener: UsbServiceListener) {
try { try {
this.listener = listener this.bus = listener
mPort.write(command.command.toByteArray(), Constants.WRITE_TIMEOUT_MILLIS) mPort.write(command.command.toByteArray(), Constants.WRITE_TIMEOUT_MILLIS)
} catch (e: IOException) { } catch (e: IOException) {
listener.onUsbError(e) listener.onUsbError(e)