hemocube test uploading data

This commit is contained in:
smileomi18@gmail.com
2023-08-01 18:45:49 +05:30
parent f8f8f140a9
commit 9382708140
3 changed files with 85 additions and 20 deletions

View File

@@ -201,4 +201,48 @@ class HemoCubeFragment : Fragment() {
}
})
}
private fun fetchHemoCubeResultNew() {
hemoCubeViewModel.progressBar.postValue(true)
val fullReadOutput = StringBuilder()
(activity as HemocubeActivity).mService.eventDrivenWriteHemoCube(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)
}
})
}
}

View File

@@ -36,42 +36,63 @@ class HemoCubeViewModel @Inject constructor(
fun uploadHemocubeResultToDatabase(
context: Context, isOnline: Boolean, testStatus: Boolean, kitSerial: String?,
resultRatio: String,
testTime: String?) = viewModelScope.launch {
testTime: String?
) = viewModelScope.launch {
if (kitSerial != null) {
testDetails?.kitSerial = kitSerial
}
testDetails?.testTime = testTime
testDetails?.testStatus = testStatus
if (isOnline) {
try {
Log.e("Testdb", "upload")
try {
if (isOnline) {
Log.e("Testdb", "Uploading data to Firestore...")
addResultTestToDb()
} catch (_: Exception) {
Log.e("Testdb", "Upload successful!")
} else {
testDetails?.testTime = SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss", Locale.getDefault()
).format(Calendar.getInstance().time)
hemoCubeDao.insertAll(testDetails!!)
Log.e("Testdb", "Data saved locally.")
}
} else {
testDetails?.testTime = SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss", Locale.getDefault()
).format(Calendar.getInstance().time)
hemoCubeDao.insertAll(testDetails!!)
} catch (e: Exception) {
Log.e("Testdb", "Upload failed: ${e.message}")
}
}
private fun addResultTestToDb() {
testDetails?.resultRatio = DataHolder.hemocubeResult
testDetailuser?.location = DataHolder.location
testDetails?.testTime = SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss", Locale.getDefault()
).format(Calendar.getInstance().time)
viewModelScope.launch {
testDetails!!.reportUploadTime = SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss", Locale.getDefault()
).format(Calendar.getInstance().time)
when (repository.addTestToDatabase(testDetails)) {
is Response.Success -> {
Log.e("Testdb", "success")
fireBaseUpload.postValue("Success")
try {
testDetails!!.reportUploadTime = SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss", Locale.getDefault()
).format(Calendar.getInstance().time)
when (val response = repository.addTestToDatabase(testDetails)) {
is Response.Success -> {
Log.e("Testdb", "Data uploaded to Firestore successfully")
fireBaseUpload.postValue("Success")
}
is Response.Error -> {
Log.e("Testdb", "Error uploading data to Firestore: ${response}")
fireBaseUpload.postValue("Error")
}
else -> {
Log.e("Testdb", "Unknown response from repository: $response")
fireBaseUpload.postValue("Unknown")
}
}
else -> {}
} catch (e: Exception) {
Log.e("Testdb", "Exception during data upload: ${e.message}")
fireBaseUpload.postValue("Error")
}
}
}