hemocube test uploading data
This commit is contained in:
@@ -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)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -36,42 +36,63 @@ class HemoCubeViewModel @Inject constructor(
|
|||||||
fun uploadHemocubeResultToDatabase(
|
fun uploadHemocubeResultToDatabase(
|
||||||
context: Context, isOnline: Boolean, testStatus: Boolean, kitSerial: String?,
|
context: Context, isOnline: Boolean, testStatus: Boolean, kitSerial: String?,
|
||||||
resultRatio: String,
|
resultRatio: String,
|
||||||
testTime: String?) = viewModelScope.launch {
|
testTime: String?
|
||||||
|
) = viewModelScope.launch {
|
||||||
if (kitSerial != null) {
|
if (kitSerial != null) {
|
||||||
testDetails?.kitSerial = kitSerial
|
testDetails?.kitSerial = kitSerial
|
||||||
}
|
}
|
||||||
testDetails?.testTime = testTime
|
testDetails?.testTime = testTime
|
||||||
testDetails?.testStatus = testStatus
|
testDetails?.testStatus = testStatus
|
||||||
if (isOnline) {
|
|
||||||
try {
|
try {
|
||||||
Log.e("Testdb", "upload")
|
if (isOnline) {
|
||||||
|
Log.e("Testdb", "Uploading data to Firestore...")
|
||||||
addResultTestToDb()
|
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 {
|
} catch (e: Exception) {
|
||||||
testDetails?.testTime = SimpleDateFormat(
|
Log.e("Testdb", "Upload failed: ${e.message}")
|
||||||
"yyyy-MM-dd HH:mm:ss", Locale.getDefault()
|
|
||||||
).format(Calendar.getInstance().time)
|
|
||||||
hemoCubeDao.insertAll(testDetails!!)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun addResultTestToDb() {
|
private fun addResultTestToDb() {
|
||||||
testDetails?.resultRatio = DataHolder.hemocubeResult
|
testDetails?.resultRatio = DataHolder.hemocubeResult
|
||||||
testDetailuser?.location = DataHolder.location
|
testDetailuser?.location = DataHolder.location
|
||||||
testDetails?.testTime = SimpleDateFormat(
|
testDetails?.testTime = SimpleDateFormat(
|
||||||
"yyyy-MM-dd HH:mm:ss", Locale.getDefault()
|
"yyyy-MM-dd HH:mm:ss", Locale.getDefault()
|
||||||
).format(Calendar.getInstance().time)
|
).format(Calendar.getInstance().time)
|
||||||
|
|
||||||
viewModelScope.launch {
|
viewModelScope.launch {
|
||||||
testDetails!!.reportUploadTime = SimpleDateFormat(
|
try {
|
||||||
"yyyy-MM-dd HH:mm:ss", Locale.getDefault()
|
testDetails!!.reportUploadTime = SimpleDateFormat(
|
||||||
).format(Calendar.getInstance().time)
|
"yyyy-MM-dd HH:mm:ss", Locale.getDefault()
|
||||||
when (repository.addTestToDatabase(testDetails)) {
|
).format(Calendar.getInstance().time)
|
||||||
is Response.Success -> {
|
|
||||||
Log.e("Testdb", "success")
|
when (val response = repository.addTestToDatabase(testDetails)) {
|
||||||
fireBaseUpload.postValue("Success")
|
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")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -51,7 +51,7 @@
|
|||||||
android:id="@+id/tv_subtitle4"
|
android:id="@+id/tv_subtitle4"
|
||||||
style="@style/title2"
|
style="@style/title2"
|
||||||
android:layout_width="0dp"
|
android:layout_width="0dp"
|
||||||
android:layout_height="70dp"
|
android:layout_height="wrap_content"
|
||||||
android:layout_marginHorizontal="24dp"
|
android:layout_marginHorizontal="24dp"
|
||||||
android:layout_marginTop="16dp"
|
android:layout_marginTop="16dp"
|
||||||
android:hint="Result"
|
android:hint="Result"
|
||||||
|
|||||||
Reference in New Issue
Block a user