diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 9f3cf73..fbe777c 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -35,7 +35,9 @@ android:name="com.example.hpostesting.presentation.hemocube.HemocubeActivity" android:exported="false" android:noHistory="true" - android:theme="@style/Theme.HPOS.NoActionBar" /> + android:parentActivityName="com.example.hpostesting.presentation.MainActivity" + android:theme="@style/Theme.HPOS.NoActionBar" + android:windowSoftInputMode="adjustPan" /> + hemoCubeViewModel.networkStatusLiveData.observe(viewLifecycleOwner) { isNetworkAvailable -> apply { kitSerial = DataHolder.SelectTestType?.kitSerial testTime = DataHolder.SelectTestType?.testTime @@ -63,6 +83,7 @@ class HemoCubeFragment : Fragment() { private fun setupButtonClickListeners() { binding.btnShowResult.setOnClickListener { +// readUsbData() fetchHemoCubeResult() } @@ -107,6 +128,56 @@ class HemoCubeFragment : Fragment() { } } + + private fun readUsbData() { + // Perform USB data reading and parsing here + val buffer = ByteArray(256) // Adjust the buffer size as per your data + val endpoint = usbDevice?.getInterface(0)?.getEndpoint(0) + + endpoint?.let { + val bytesRead = usbConnection?.bulkTransfer(it, buffer, buffer.size, TIMEOUT) + if (bytesRead != null && bytesRead > 0) { + val usbData = String(buffer, 0, bytesRead, Charsets.UTF_8) + val parsedData = parseUsbData(usbData) + displayValuesOnTextView(parsedData) + } else { + // Handle USB data read error + } + } + } + + private fun parseUsbData(data: String): Map { + val lines = data.split("\n") + val parsedData = mutableMapOf() + + for (line in lines) { + val labelAndValue = line.split(":") + if (labelAndValue.size == 2) { + val label = labelAndValue[0].trim() + val value = labelAndValue[1].trim() + parsedData[label] = value + } + } + + return parsedData + } + + + private fun displayValuesOnTextView(parsedData: Map) { + val fullReadOutput = StringBuilder() + val textView: TextView = requireView().findViewById(R.id.tv_subtitle4) + + val resultText = buildString { + for ((label, value) in parsedData) { + append("$label: $value\n") + } + } + + // Display the complete data accumulated so far + fullReadOutput.append(resultText) + textView.text = fullReadOutput.toString() + } + private fun fetchHemoCubeResult() { hemoCubeViewModel.progressBar.postValue(true) val fullReadOutput = StringBuilder() @@ -118,6 +189,8 @@ class HemoCubeFragment : Fragment() { fullReadOutput.append(stringData) binding.tvSubtitle4.text = fullReadOutput resultRatio = fullReadOutput.toString() + val data = parseUsbData(fullReadOutput.toString()) + Log.d("Result", data.toString()) hemoCubeViewModel.progressBar.postValue(false) } } diff --git a/app/src/main/java/com/example/hpostesting/presentation/testRight/TestRightActivity.kt b/app/src/main/java/com/example/hpostesting/presentation/testRight/TestRightActivity.kt index d59cf5f..2a75ac8 100644 --- a/app/src/main/java/com/example/hpostesting/presentation/testRight/TestRightActivity.kt +++ b/app/src/main/java/com/example/hpostesting/presentation/testRight/TestRightActivity.kt @@ -103,17 +103,6 @@ class TestRightActivity : AppCompatActivity() { } } -// private fun testing() { -// supportFragmentManager.beginTransaction() -// .replace(binding.flMain.id, TestRightExpSample()).commit() -// } - - // Todo: Comment this -// private fun connectUsb(permissionGranted: Boolean) { -// setupService() -// } - - // Todo: Uncomment this private fun connectUsb(permissionGranted: Boolean) { Log.d(TAG, "connectUsb() called, permission variable = $permissionGranted") val manager = getSystemService(Context.USB_SERVICE) as UsbManager @@ -124,13 +113,6 @@ class TestRightActivity : AppCompatActivity() { } else { mDriver = availableDrivers[0] -// if (mDriver.device.productId != Constants.DEVICE_PRODUCT_ID || mDriver.device.vendorId != Constants.DEVICE_VENDOR_ID -// || (mDriver.device.productId != Constants.HOMO_CUBE_ID || mDriver.device.vendorId != Constants.VENDOR_ID)){ -// Toast.makeText(this, "Device Connected is not supported", Toast.LENGTH_SHORT).show() -// onBackPressed() -// return -// } - viewModel.testDetails?.deviceId = mDriver.device.serialNumber.toString() DataHolder.deviceSerialNumber = mDriver.device.serialNumber.toString() mConnection = manager.openDevice(mDriver.device) @@ -143,12 +125,7 @@ class TestRightActivity : AppCompatActivity() { } } - /* - * Request user permission. The response will be received in the BroadcastReceiver - */ private fun requestUserPermission(manager: UsbManager, device: UsbDevice) { -// Log.d(TAG, "requestUserPermissions() called -> vendor id = ${device.vendorId} & product id = ${device.productId}") - val mPendingIntent: PendingIntent if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) { mPendingIntent = PendingIntent.getBroadcast( @@ -197,11 +174,6 @@ class TestRightActivity : AppCompatActivity() { fun onErrorReported(msg: String) { Toast.makeText(this, msg, Toast.LENGTH_SHORT).show() -// Snackbar.make(binding.clParent, msg, Snackbar.LENGTH_LONG) -// .setAction("CLOSE") { Toast.makeText(this, "Will soon", Toast.LENGTH_SHORT).show() } -// .setActionTextColor(resources.getColor(R.color.white)) -// .show() - if (!isFinishing) onBackPressed() } @@ -211,16 +183,6 @@ class TestRightActivity : AppCompatActivity() { myMenu = menu return true } - -// override fun onOptionsItemSelected(item: MenuItem) = when (item.itemId) { -// R.id.action_usb -> { -//// Toast.makeText(this, "USB Connected", Toast.LENGTH_SHORT).show() -//// myMenu?.get(0)?.icon = ContextCompat.getDrawable(this, R.drawable.ic_baseline_usb_off_24) -// true -// } -// else -> {super.onOptionsItemSelected(item)} -// } - override fun onDestroy() { super.onDestroy() if (viewModel.isServiceConnected) { @@ -229,10 +191,4 @@ class TestRightActivity : AppCompatActivity() { viewModel.isServiceConnected = false } } - -// fun View.setAllEnabled(enabled: Boolean) { -// isEnabled = enabled -// if (this is ViewGroup) children.forEach { child -> child.setAllEnabled(enabled) } -// } - } \ No newline at end of file diff --git a/app/src/main/java/com/example/hpostesting/presentation/testRight/TestRightExpReference.kt b/app/src/main/java/com/example/hpostesting/presentation/testRight/TestRightExpReference.kt index 36498be..3c3941a 100644 --- a/app/src/main/java/com/example/hpostesting/presentation/testRight/TestRightExpReference.kt +++ b/app/src/main/java/com/example/hpostesting/presentation/testRight/TestRightExpReference.kt @@ -24,16 +24,12 @@ import `in`.sminnovations.hpostesting.databinding.FragmentTestRightExpReferenceB @AndroidEntryPoint class TestRightExpReference : Fragment() { - private lateinit var binding: FragmentTestRightExpReferenceBinding private val viewModel: TestRightViewModel by activityViewModels() - private val TAG = "TestRightExpReference" - override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View { - // Inflate the layout for this fragment binding = FragmentTestRightExpReferenceBinding.inflate(inflater, container, false) return binding.root } @@ -41,12 +37,6 @@ class TestRightExpReference : Fragment() { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) setupListeners() - -// if (DataHolder.isReferenceTaken) -// moveToSamplePage() -// -// DataHolder.sampleReadCounter <= Constants.MAXIMUM_TEST_ALLOWED_BEFORE_REFERENCE - if (DataHolder.deviceConstant == null) { viewModel.progressBar.postValue(true) Handler(Looper.getMainLooper()).postDelayed( @@ -83,7 +73,6 @@ class TestRightExpReference : Fragment() { } viewModel.progressBar.observe(viewLifecycleOwner) { -// Log.d(TAG, "SURYA OBSERVER Activated outcome = $it") if (it) { binding.progressBar.visibility = View.VISIBLE binding.clParent.alpha = 0.5f @@ -107,9 +96,6 @@ class TestRightExpReference : Fragment() { private fun startTakingReference() { sendCmdToSetLed() } - - - // private fun sendCmdToFetchDeviceConstant(recursive: Boolean) { private fun sendCmdToFetchDeviceConstant() { Log.d(TAG, "sendCmdToFetchDeviceConstant() called") @@ -244,7 +230,6 @@ class TestRightExpReference : Fragment() { ) } } -// } } override fun onUsbError(e: Exception?) { @@ -266,10 +251,6 @@ class TestRightExpReference : Fragment() { data?.let { val stringData = String(it) fullReadOutput.append(stringData) - -// Log.d(TAG, stringData) -// if (stringData.contains("OK", true)) { -// if (fullReadOutput.substring(Math.max(fullReadOutput.length - 15, 0)).contains("OK", true)) { if (stringData.trim().isNotEmpty() && fullReadOutput.substring( Math.max( fullReadOutput.length - 15, 0 @@ -278,11 +259,7 @@ class TestRightExpReference : Fragment() { ) { Log.d(TAG, "onUsbRead() called in sendCmdToFetchLightIntensities()") -// Log.d(TAG, fullReadOutput.toString()) - viewModel.mapIntensityValues(fullReadOutput.toString(), true) -// viewModel.saveLogTest(requireContext().applicationContext, true, fullReadOutput.toString()) - DataHolder.isReferenceTaken = true Handler(Looper.getMainLooper()).postDelayed( diff --git a/app/src/main/java/com/example/hpostesting/presentation/testRight/TestRightExpSample.kt b/app/src/main/java/com/example/hpostesting/presentation/testRight/TestRightExpSample.kt index 000ec30..33f79ce 100644 --- a/app/src/main/java/com/example/hpostesting/presentation/testRight/TestRightExpSample.kt +++ b/app/src/main/java/com/example/hpostesting/presentation/testRight/TestRightExpSample.kt @@ -35,16 +35,7 @@ class TestRightExpSample : Fragment() { inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View { - // Inflate the layout for this fragment binding = FragmentTestRightExpSampleBinding.inflate(inflater, container, false) -// if (DataHolder.sampleReadCounter > Constants.MAXIMUM_TEST_ALLOWED_BEFORE_REFERENCE) { -// binding.btnUpdateReference.enable() -// binding.clInstructions.setAllEnabled(false) -// } else { -// binding.btnUpdateReference.disable() -// binding.clInstructions.setAllEnabled(false) -// } - return binding.root } @@ -75,10 +66,6 @@ class TestRightExpSample : Fragment() { } binding.btnSubmit.setOnClickListener { -// val details = validateAndReturnTestData() -// if (details != null) { -// viewModel.patientDetails = details - UIUtils.createAlertDialog( requireContext(), "WARNING", @@ -111,24 +98,13 @@ class TestRightExpSample : Fragment() { val i = Intent(requireContext(), MainActivity::class.java) startActivity(i) } - -// binding.etNameBlock.setOnClickListener { binding.etName.isErrorEnabled = false } -// binding.etAgeBlock.setOnClickListener { binding.etAge.isErrorEnabled = false } -// binding.etGenderBlock.setOnClickListener { binding.ddGender.isErrorEnabled = false } } - -// private fun validateAndReturnTestData(): TestDetails? { -// -// return PatientData(name.toString(), age.toString().toInt(), gender.toString(), TestRightResultType.UNDEFINED) -// } - /** * Executing the commands sequentially each after successfully executing one. * 1. command run * 2. command print */ private fun startAcquiring() { -// binding.progressBar.visibility = View.VISIBLE sendCmdToRun() } @@ -144,9 +120,6 @@ class TestRightExpSample : Fragment() { val stringData = String(it) fullReadOutput.append(stringData) Log.d(TAG, stringData) -// if (stringData.contains("OK", true)) { -// if (stringData.contains("OK", true) || fullReadOutput.substring(Math.max(fullReadOutput.length - 15, 0)).contains("OK", true)) { -// if (fullReadOutput.substring(Math.max(fullReadOutput.length - 15, 0)).contains("OK", true)) { if (fullReadOutput.substring(Math.max(fullReadOutput.length - 15, 0)).contains("OK [0]", true)) { Log.d(TAG, "onUsbRead() called in sendCmdToRun()") Handler(Looper.getMainLooper()).postDelayed( @@ -183,17 +156,10 @@ class TestRightExpSample : Fragment() { val stringData = String(it) fullReadOutput.append(stringData) Log.d(TAG, stringData) -// if (stringData.contains("OK", true)) { -// if (stringData.contains("OK", true) || stringData.contains("O", true) || stringData.contains("K", true)) { -// if (stringData.contains("OK", true) || fullReadOutput.substring(Math.max(fullReadOutput.length - 15, 0)).contains("OK", true)) { -// if (fullReadOutput.substring(Math.max(fullReadOutput.length - 15, 0)).contains("OK", true)) { if (stringData.trim().isNotEmpty() && fullReadOutput.substring(Math.max(fullReadOutput.length - 15, 0)).contains("OK [0]", true)) { Log.d(TAG, "onUsbRead() called in sendCmdToFetchLightIntensities()") viewModel.mapIntensityValues(fullReadOutput.toString(), false) -// viewModel.saveLogTest(requireContext().applicationContext, false, fullReadOutput.toString()) - -// showResultsAfterAcquiring() Handler(Looper.getMainLooper()).postDelayed( { checkIfToRunAgain() @@ -219,15 +185,11 @@ class TestRightExpSample : Fragment() { viewModel.mapWavelengthToAbsorbance() - // Todo: Save CSV + Test CSV - if (DataHolder.selectedTestType == TestType.SICKLECERT) { viewModel.calculateResults() } else { viewModel.calculateResultsForSickleFind() } - // Todo: Save Log - saveDataLocally() if (viewModel.numberOfSampleRun < Constants.NO_OF_TIMES_TO_RUN_SAMPLE) { @@ -244,33 +206,13 @@ class TestRightExpSample : Fragment() { } private fun showResultsAfterAcquiring() { -// viewModel.mapWavelengthToAbsorbance() -// viewModel.calculateResults() - viewModel.progressBar.postValue(false) -// binding.progressBar.visibility = View.GONE parentFragmentManager.beginTransaction().replace(R.id.fl_main, TestRightResults()) .commit() } private fun saveDataLocally() { - // OLD ------------------------------ -// val patientName = viewModel.testDetails?.patientName -// if (patientName.length > 5){ -// patientName = patientName.substring(0, 5) -// } -// val id = PreferenceUtility.generateId(requireContext()) - -// val prefixCsv: String = if (DataHolder.selectedTestType == TestType.SICKLECERT) -// "HPOSSC_${DataHolder.deviceSerialNumber}_${patientName}_" -// else -// "HPOSSF_${DataHolder.deviceSerialNumber}_${patientName}_" - -// val fileNameCsv = prefixCsv + id + fileExtensionCsv - - // NEW ------------------------------ viewModel.saveCsv(requireContext().applicationContext, viewModel.getCSVFileName()) -// viewModel.saveCsvForTesting(requireContext().applicationContext, viewModel.getDetailedCSVFileName()) viewModel.saveLogWithPatient(requireContext().applicationContext, viewModel.getLogFileName()) } diff --git a/app/src/main/java/com/example/hpostesting/presentation/testRight/TestRightResults.kt b/app/src/main/java/com/example/hpostesting/presentation/testRight/TestRightResults.kt index f29378b..a225428 100644 --- a/app/src/main/java/com/example/hpostesting/presentation/testRight/TestRightResults.kt +++ b/app/src/main/java/com/example/hpostesting/presentation/testRight/TestRightResults.kt @@ -38,7 +38,6 @@ class TestRightResults : Fragment() { override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View { - // Inflate the layout for this fragment binding = FragmentTestRightResultsBinding.inflate(inflater, container, false) sharedPreference = requireContext().getSharedPreferences("PREFERENCE_NAME", Context.MODE_PRIVATE) @@ -115,36 +114,6 @@ class TestRightResults : Fragment() { if (DataHolder.selectedTestType == TestType.SICKLECERT) { -// when (viewModel.testDetails?.result) { -// TestRightResultType.NORMAL -> { -// binding.resultNormal.visibility = View.VISIBLE -// } -// -// TestRightResultType.SICKLECELLDISEASE -> { -// binding.resultDisease.visibility = View.VISIBLE -// } -// -// TestRightResultType.SICKLECELLTRAIT -> { -// binding.resultTrait.visibility = View.VISIBLE -// } -// -// TestRightResultType.POSITIVEBORDERLINE -> { -// binding.resultPositiveBorderline.visibility = View.VISIBLE -// binding.tvRecommended.visibility = View.VISIBLE -// } -// -// TestRightResultType.NEGATIVEBORDERLINE -> { -// binding.resultNegativeBorderline.visibility = View.VISIBLE -// binding.tvRecommended.visibility = View.VISIBLE -// } -// else -> { -//// binding.resultUndefined.visibility = View.VISIBLE -// Toast.makeText( -// requireContext().applicationContext, "Please Test Again", Toast.LENGTH_LONG -// ).show() -// moveToSamplePage() -// } -// } binding.resultNormal.visibility = View.VISIBLE } else { when (viewModel.testDetails?.result) { @@ -194,6 +163,4 @@ class TestRightResults : Fragment() { .commit() } } - - } \ No newline at end of file diff --git a/app/src/main/java/com/example/hpostesting/presentation/testRight/TestRightViewModel.kt b/app/src/main/java/com/example/hpostesting/presentation/testRight/TestRightViewModel.kt index b6fcd03..ef254d1 100644 --- a/app/src/main/java/com/example/hpostesting/presentation/testRight/TestRightViewModel.kt +++ b/app/src/main/java/com/example/hpostesting/presentation/testRight/TestRightViewModel.kt @@ -64,8 +64,6 @@ class TestRightViewModel @Inject constructor( val allUserData = userDao.getAll() private lateinit var calculationData: TestRightCalculationData - - /* (For exp sample) Contains pixel no. -> Intensity of light falling on that pixel */ val intensitySampleArray = ArrayList() val wavelengthToAbsorbance = ArrayList>() @@ -73,8 +71,6 @@ class TestRightViewModel @Inject constructor( val calculationVariableList = ArrayList() fun mapDeviceConstants(string: String) { -// Log.d(TAG, "mapDeviceConstants() called") -// Log.d(TAG, "mapDeviceConstants() value -> $string") if (string.isNotEmpty()) { val listOfStrings = string.split(",") if (listOfStrings.size >= 4) { diff --git a/app/src/main/res/layout/fragment_hemo_cube_reference.xml b/app/src/main/res/layout/fragment_hemo_cube_reference.xml index 3df1249..39015c6 100644 --- a/app/src/main/res/layout/fragment_hemo_cube_reference.xml +++ b/app/src/main/res/layout/fragment_hemo_cube_reference.xml @@ -51,7 +51,7 @@ android:id="@+id/tv_subtitle4" style="@style/title2" android:layout_width="0dp" - android:layout_height="match_parent" + android:layout_height="wrap_content" android:layout_marginHorizontal="24dp" android:layout_marginTop="16dp" android:hint="Result" diff --git a/app/src/main/res/xml/device_filter.xml b/app/src/main/res/xml/device_filter.xml index 1d0d6e7..b07e602 100644 --- a/app/src/main/res/xml/device_filter.xml +++ b/app/src/main/res/xml/device_filter.xml @@ -10,6 +10,7 @@ +