Experimenting with Uploads
This commit is contained in:
@@ -22,6 +22,7 @@ import com.example.hpostesting.data.Result
|
|||||||
import com.example.hpostesting.data.api.DeviceCommunicationHandler
|
import com.example.hpostesting.data.api.DeviceCommunicationHandler
|
||||||
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.HemoCubeCommands
|
||||||
|
import com.example.hpostesting.data.encryption.AESCrypt.decrypt
|
||||||
import com.example.hpostesting.data.model.login.LoginRequest
|
import com.example.hpostesting.data.model.login.LoginRequest
|
||||||
import com.example.hpostesting.data.model.login.LoginResponse
|
import com.example.hpostesting.data.model.login.LoginResponse
|
||||||
import com.example.hpostesting.data.model.molbioresult.MolbioV2Result
|
import com.example.hpostesting.data.model.molbioresult.MolbioV2Result
|
||||||
@@ -61,6 +62,7 @@ import java.util.Locale
|
|||||||
import java.util.zip.ZipEntry
|
import java.util.zip.ZipEntry
|
||||||
import java.util.zip.ZipInputStream
|
import java.util.zip.ZipInputStream
|
||||||
|
|
||||||
|
|
||||||
@AndroidEntryPoint
|
@AndroidEntryPoint
|
||||||
class HomeFragment : Fragment() {
|
class HomeFragment : Fragment() {
|
||||||
private var _binding: FragmentHomeBinding? = null
|
private var _binding: FragmentHomeBinding? = null
|
||||||
@@ -146,27 +148,37 @@ class HomeFragment : Fragment() {
|
|||||||
|
|
||||||
hemoCubeViewModel.allUserData.observe(viewLifecycleOwner) { originalUserDataList ->
|
hemoCubeViewModel.allUserData.observe(viewLifecycleOwner) { originalUserDataList ->
|
||||||
// Ensure the list does not contain duplicates based on _id
|
// Ensure the list does not contain duplicates based on _id
|
||||||
val userDataList = originalUserDataList.distinctBy { it._id }
|
//val userDataList = originalUserDataList.distinctBy { it.testTime }
|
||||||
|
val userDataList = originalUserDataList
|
||||||
|
|
||||||
// Initialize the resultList outside of the loop to avoid duplicates
|
// Initialize the resultList outside of the loop to avoid duplicates
|
||||||
val resultList = MolbioV2ResultRequest(mutableListOf())
|
val resultList = MolbioV2ResultRequest(mutableListOf())
|
||||||
val processedUserIds = mutableSetOf<String>() // To track processed users based on _id
|
|
||||||
|
|
||||||
userDataList.forEach { userData ->
|
userDataList.forEach { userData ->
|
||||||
// Ensure we process each user only once
|
// Ensure we process each user only once
|
||||||
if (!processedUserIds.contains(userData._id)) {
|
// if (!processedUserIds.contains(userData._id)) {
|
||||||
if (!userData.molbioFlag && isTokenAvailable && Constants.MOLBIO_INTEGRATION) {
|
Log.d(": USER DATA", userDataList.count().toString() + " : " + userData._id)
|
||||||
val currentTimeFormatted = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZZZZZ", Locale.getDefault()).format(Calendar.getInstance().time)
|
|
||||||
val bufferIntensityThreshold = Constants.BUFFER_INTENSITY_THRESHOLDS[userData.deviceId]?.toString() ?: "defaultThreshold" // Handle possible nulls safely
|
|
||||||
|
|
||||||
|
|
||||||
|
if (!userData.molbioFlag && isTokenAvailable && Constants.MOLBIO_INTEGRATION) {
|
||||||
|
val currentTimeFormatted = SimpleDateFormat(
|
||||||
|
"yyyy-MM-dd'T'HH:mm:ssZZZZZ",
|
||||||
|
Locale.getDefault()
|
||||||
|
).format(Calendar.getInstance().time)
|
||||||
|
val bufferIntensityThreshold =
|
||||||
|
Constants.BUFFER_INTENSITY_THRESHOLDS[userData.deviceId]?.toString()
|
||||||
|
?: "defaultThreshold" // Handle possible nulls safely
|
||||||
resultList.results?.add(
|
resultList.results?.add(
|
||||||
MolbioV2Result(
|
MolbioV2Result(
|
||||||
rawData = userData,
|
rawData = userData,
|
||||||
analysisId = userData._id,
|
analysisId = userData._id,
|
||||||
analysisDate = currentTimeFormatted,
|
analysisDate = currentTimeFormatted,
|
||||||
analysisStatus = userData.classificationResult ?: "defaultStatus", // Handle possible nulls
|
analysisStatus = userData.classificationResult
|
||||||
|
?: "defaultStatus", // Handle possible nulls
|
||||||
thresholds = bufferIntensityThreshold,
|
thresholds = bufferIntensityThreshold,
|
||||||
interpretation = userData.classificationResult ?: "defaultInterpretation", // Handle possible nulls
|
interpretation = userData.classificationResult
|
||||||
|
?: "defaultInterpretation", // Handle possible nulls
|
||||||
testId = userData._id,
|
testId = userData._id,
|
||||||
testTime = currentTimeFormatted,
|
testTime = currentTimeFormatted,
|
||||||
collectionTime = currentTimeFormatted,
|
collectionTime = currentTimeFormatted,
|
||||||
@@ -174,20 +186,32 @@ class HomeFragment : Fragment() {
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
// Mark this userData as processed to avoid re-processing
|
// Mark this userData as processed to avoid re-processing
|
||||||
processedUserIds.add(userData._id)
|
// processedUserIds.add(userData.testTime.toString())
|
||||||
|
// if (!userData.localFlag) {
|
||||||
|
// hemoCubeViewModel.bulkAddResultTestToDb(userData)
|
||||||
|
// userData.localFlag = true
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
// Mark the userData for localFlag update, this will be handled in bulk after the loop
|
// Mark the userData for localFlag update, this will be handled in bulk after the loop
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
Log.d("USER DATA LIST SIZE", resultList.results?.count().toString())
|
||||||
|
|
||||||
|
|
||||||
|
// Perform bulk operations after the loop
|
||||||
|
// Set localFlag to true for all usersToUpdate and perform bulk database upd
|
||||||
|
|
||||||
|
resultList.results?.forEach { result ->
|
||||||
|
val userData = result.rawData
|
||||||
|
Log.d("UserData", userData.toString())
|
||||||
|
if (userData != null) {
|
||||||
if (!userData.localFlag) {
|
if (!userData.localFlag) {
|
||||||
hemoCubeViewModel.bulkAddResultTestToDb(userData)
|
hemoCubeViewModel.bulkAddResultTestToDb(userData)
|
||||||
userData.localFlag = true
|
userData.localFlag = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Perform bulk operations after the loop
|
|
||||||
// Set localFlag to true for all usersToUpdate and perform bulk database upd
|
|
||||||
|
|
||||||
// Upload results after processing all userData to avoid duplicates and ensure all modifications are done
|
// Upload results after processing all userData to avoid duplicates and ensure all modifications are done
|
||||||
if (resultList.results?.isNotEmpty() == true) {
|
if (resultList.results?.isNotEmpty() == true) {
|
||||||
hemoCubeViewModel.uploadResult(resultList)
|
hemoCubeViewModel.uploadResult(resultList)
|
||||||
@@ -304,7 +328,31 @@ class HomeFragment : Fragment() {
|
|||||||
hemoCubeViewModel.uploadLogs()
|
hemoCubeViewModel.uploadLogs()
|
||||||
hemoCubeViewModel.startPeriodicCheckUpdate()
|
hemoCubeViewModel.startPeriodicCheckUpdate()
|
||||||
}
|
}
|
||||||
} else {
|
}
|
||||||
|
// } else if (fis.available()>0){
|
||||||
|
// val buffer = ByteArray(fis.available())
|
||||||
|
// fis.read(buffer)
|
||||||
|
// fis.close()
|
||||||
|
// val encryptedData = String(buffer)
|
||||||
|
// val parts = encryptedData.split(",".toRegex()).dropLastWhile { it.isEmpty() }
|
||||||
|
// .toTypedArray()
|
||||||
|
// val deviceID = parts[0]
|
||||||
|
// val decryptedUsername = decrypt(parts[1])
|
||||||
|
// val decryptedPassword = decrypt(parts[2])
|
||||||
|
// if (accessToken.isEmpty()) {
|
||||||
|
// hemoCubeViewModel.login(createLoginRequestData(decryptedUsername, decryptedPassword))
|
||||||
|
//}
|
||||||
|
// else {
|
||||||
|
// // Continue with your existing logic if the token is not empty.
|
||||||
|
// isTokenAvailable = true
|
||||||
|
// hemoCubeViewModel.deviceUpdate(createDeviceUpdateRequestData())
|
||||||
|
// hemoCubeViewModel.uploadLogs()
|
||||||
|
// hemoCubeViewModel.startPeriodicCheckUpdate()
|
||||||
|
// }
|
||||||
|
|
||||||
|
|
||||||
|
//}
|
||||||
|
else {
|
||||||
Toast.makeText(
|
Toast.makeText(
|
||||||
requireContext(),
|
requireContext(),
|
||||||
"Contact Help and get your device provision done",
|
"Contact Help and get your device provision done",
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import android.content.Context
|
|||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.content.SharedPreferences
|
import android.content.SharedPreferences
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.util.Base64
|
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
import android.view.LayoutInflater
|
import android.view.LayoutInflater
|
||||||
import android.view.View
|
import android.view.View
|
||||||
@@ -24,8 +23,6 @@ import com.example.hpostesting.presentation.dashboard.DashboardActivity
|
|||||||
import com.google.firebase.crashlytics.ktx.crashlytics
|
import com.google.firebase.crashlytics.ktx.crashlytics
|
||||||
import com.google.firebase.ktx.Firebase
|
import com.google.firebase.ktx.Firebase
|
||||||
import `in`.sminnovations.hpostesting.databinding.FragmentDeviceProvisionBinding
|
import `in`.sminnovations.hpostesting.databinding.FragmentDeviceProvisionBinding
|
||||||
import javax.crypto.Cipher
|
|
||||||
import javax.crypto.spec.SecretKeySpec
|
|
||||||
|
|
||||||
|
|
||||||
class DeviceProvisionFragment : Fragment() {
|
class DeviceProvisionFragment : Fragment() {
|
||||||
@@ -230,13 +227,14 @@ class DeviceProvisionFragment : Fragment() {
|
|||||||
|
|
||||||
//saves username and password to a local file after encrypting it.
|
//saves username and password to a local file after encrypting it.
|
||||||
private fun saveDataToLocalFile( username : String, password : String){
|
private fun saveDataToLocalFile( username : String, password : String){
|
||||||
|
val deviceID = sharedPreferences.getString(Constants.DEVICE_ID, "").toString();
|
||||||
val encryptedUsername = AESCrypt.encrypt(username)
|
val encryptedUsername = AESCrypt.encrypt(username)
|
||||||
val encryptedPassword = AESCrypt.encrypt(password)
|
val encryptedPassword = AESCrypt.encrypt(password)
|
||||||
val encryptedData = encryptedUsername + "\n" + encryptedPassword
|
val encryptedData = deviceID + "\n" + encryptedUsername + "\n" + encryptedPassword
|
||||||
|
|
||||||
val fos = requireContext().openFileOutput("credentials.txt", Context.MODE_PRIVATE)
|
val fileOutputStream = requireContext().openFileOutput("credentials.txt", Context.MODE_PRIVATE)
|
||||||
fos.write(encryptedData.toByteArray())
|
fileOutputStream.write(encryptedData.toByteArray())
|
||||||
fos.close()
|
fileOutputStream.close()
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user