This commit is contained in:
Pritimay Sarkar
2023-08-02 23:14:57 +05:30
parent 0b44ce47b9
commit 3292a0e382
3 changed files with 78 additions and 4 deletions

View File

@@ -14,6 +14,7 @@ import android.widget.Toast
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels import androidx.fragment.app.activityViewModels
import com.example.hpostesting.data.DataHolder import com.example.hpostesting.data.DataHolder
import com.example.hpostesting.data.constant.HemoCubeCommands
import com.example.hpostesting.data.model.patient.toHemoCubeTestData import com.example.hpostesting.data.model.patient.toHemoCubeTestData
import com.example.hpostesting.presentation.UsbServiceListener import com.example.hpostesting.presentation.UsbServiceListener
import com.example.hpostesting.presentation.utils.MyDialogListener import com.example.hpostesting.presentation.utils.MyDialogListener
@@ -96,11 +97,11 @@ class HemoCubeFragment : Fragment() {
override fun onClickNegativeButton() {} override fun onClickNegativeButton() {}
override fun onClickPositiveButton() { override fun onClickPositiveButton() {
fetchHemoCubeResult() fetchHemoCubeResult()
sendCommandToHemoCube()
} }
}) })
} }
}) })
} }
binding.btnSubmit.setOnClickListener { binding.btnSubmit.setOnClickListener {
@@ -194,7 +195,7 @@ class HemoCubeFragment : Fragment() {
hemoCubeViewModel.progressBar.postValue(true) hemoCubeViewModel.progressBar.postValue(true)
val fullReadOutput = StringBuilder() val fullReadOutput = StringBuilder()
(activity as HemocubeActivity).mService.eventDrivenWriteHemoCube(object : (activity as HemocubeActivity).mService.listenToHemoCube(object :
UsbServiceListener { UsbServiceListener {
override fun onUsbRead(data: ByteArray?) { override fun onUsbRead(data: ByteArray?) {
data?.let { data?.let {
@@ -216,4 +217,67 @@ class HemoCubeFragment : Fragment() {
} }
}) })
} }
private fun fetchHemoCubeResultNew() {
hemoCubeViewModel.progressBar.postValue(true)
val fullReadOutput = StringBuilder()
(activity as HemocubeActivity).mService.listenToHemoCube(object :
UsbServiceListener {
override fun onUsbRead(data: ByteArray?) {
data?.let {
val stringData = String(it)
fullReadOutput.append(stringData)
// Split the data into lines
val lines = fullReadOutput.toString().split("\n")
// Filter and get only the lines starting with "#"
val bufferLines = lines.filter { it.startsWith("#") }
// Create a new text with the filtered lines and join them with line breaks
val newText = bufferLines.joinToString("\n")
// Update the TextView with the new text
binding.tvSubtitle4.text = newText
// Get the result data
val data = parseUsbData(fullReadOutput.toString())
Log.d("Result", data.toString())
// Update the view model with progress bar state
hemoCubeViewModel.progressBar.postValue(false)
}
}
override fun onUsbError(e: Exception?) {
hemoCubeViewModel.progressBar.postValue(false)
}
})
}
private fun sendCommandToHemoCube() {
hemoCubeViewModel.progressBar.postValue(true)
val fullReadOutput = StringBuilder()
(activity as HemocubeActivity).mService.eventDrivenWriteHemoCube(HemoCubeCommands.run,
object : UsbServiceListener {
override fun onUsbRead(data: ByteArray?) {
data?.let {
val stringData = String(it)
fullReadOutput.append(stringData)
if (stringData.contains("RESULT", true)) {
// hemoCubeViewModel.mapDeviceConstants(fullReadOutput.toString())
hemoCubeViewModel.progressBar.postValue(false)
} else if (stringData.contains("FINE", true)) {
hemoCubeViewModel.progressBar.postValue(false)
}
}
}
override fun onUsbError(e: Exception?) {
hemoCubeViewModel.progressBar.postValue(false)
}
})
}
} }

View File

@@ -83,7 +83,6 @@ class TestRightExpReference : Fragment() {
binding.btnSetReference.isEnabled = true binding.btnSetReference.isEnabled = true
} }
} }
} }
/** /**
@@ -96,6 +95,7 @@ class TestRightExpReference : Fragment() {
private fun startTakingReference() { private fun startTakingReference() {
sendCmdToSetLed() sendCmdToSetLed()
} }
private fun sendCmdToFetchDeviceConstant() { private fun sendCmdToFetchDeviceConstant() {
Log.d(TAG, "sendCmdToFetchDeviceConstant() called") Log.d(TAG, "sendCmdToFetchDeviceConstant() called")

View File

@@ -7,6 +7,7 @@ import android.os.Binder
import android.os.IBinder import android.os.IBinder
import android.util.Log import android.util.Log
import com.example.hpostesting.data.constant.Constants import com.example.hpostesting.data.constant.Constants
import com.example.hpostesting.data.constant.HemoCubeCommands
import com.example.hpostesting.data.constant.TestRightCommands import com.example.hpostesting.data.constant.TestRightCommands
import com.example.hpostesting.presentation.UsbServiceListener import com.example.hpostesting.presentation.UsbServiceListener
import com.hoho.android.usbserial.driver.UsbSerialDriver import com.hoho.android.usbserial.driver.UsbSerialDriver
@@ -64,7 +65,7 @@ class UsbService : Service() {
} }
} }
fun eventDrivenWriteHemoCube(listener: UsbServiceListener) { fun listenToHemoCube(listener: UsbServiceListener) {
this.listener = listener this.listener = listener
try { try {
@@ -72,4 +73,13 @@ class UsbService : Service() {
listener.onUsbError(e) listener.onUsbError(e)
} }
} }
fun eventDrivenWriteHemoCube(command: HemoCubeCommands, listener: UsbServiceListener) {
try {
this.listener = listener
mPort.write(command.command.toByteArray(), Constants.WRITE_TIMEOUT_MILLIS)
} catch (e: IOException) {
listener.onUsbError(e)
}
}
} }