diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml
index 9bc14ac..6885e3a 100644
--- a/app/src/main/AndroidManifest.xml
+++ b/app/src/main/AndroidManifest.xml
@@ -119,8 +119,13 @@
android:noHistory="true"
android:theme="@style/AppTheme.NoActionBar">
+<<<<<<< HEAD
+=======
+
+
+>>>>>>> origin/device-provision-response-store
= emptyList(),
@get:PropertyName("calibratedAt") @set:PropertyName("calibratedAt")
- var calibratedAt: String = ""
+ var calibratedAt: String = "" ,
+ @get:PropertyName("deviceProvisionResponse") @set:PropertyName("deviceProvisionResponse")
+ var deviceProvisionResponse: String = "" ,
+ @get:PropertyName("username") @set:PropertyName("username")
+ var username: String = "",
+ @get:PropertyName("password") @set:PropertyName("password")
+ var password: String = ""
+
)
\ No newline at end of file
diff --git a/app/src/main/java/com/example/hpostesting/data/repository/DatabaseRepository.kt b/app/src/main/java/com/example/hpostesting/data/repository/DatabaseRepository.kt
index d982014..d958c6c 100644
--- a/app/src/main/java/com/example/hpostesting/data/repository/DatabaseRepository.kt
+++ b/app/src/main/java/com/example/hpostesting/data/repository/DatabaseRepository.kt
@@ -206,7 +206,31 @@ class DatabaseRepository @Inject constructor(
return allDeviceDataList.find { it.deviceId == deviceId }
}
+ override suspend fun getDeviceResponse(data: DeviceData?): Response {
+ return try {
+ db.collection("devices").add(data!!).await()
+ Response.Success(data.deviceProvisionResponse)
+ } catch (e: Exception) {
+ Firebase.crashlytics.recordException(e)
+ Response.Error(e)
+ }
+ }
+
+
+
+
+ override suspend fun uploadDeviceId(data: DeviceData): Response? {
+ return try {
+ db.collection("devices").add(data!!).await()
+ Response.Success(data.deviceId)
+ } catch (e: Exception) {
+ Firebase.crashlytics.recordException(e)
+ Response.Error(e)
+ }
+ }
+
override fun addTestToDatabase(testDetails: UserData): Any {
TODO("Not yet implemented")
}
+
}
\ No newline at end of file
diff --git a/app/src/main/java/com/example/hpostesting/data/repository/Repository.kt b/app/src/main/java/com/example/hpostesting/data/repository/Repository.kt
index f595aeb..676b7b6 100644
--- a/app/src/main/java/com/example/hpostesting/data/repository/Repository.kt
+++ b/app/src/main/java/com/example/hpostesting/data/repository/Repository.kt
@@ -32,6 +32,8 @@ interface Repository {
suspend fun uploadFileToStorage(patientID: String, filePath: String): Response
suspend fun getDeviceDataById(deviceId: String): DeviceData?
+ suspend fun getDeviceResponse(data: DeviceData?): Response
+ suspend fun uploadDeviceId(data: DeviceData): Response?
abstract fun addTestToDatabase(testDetails: UserData): Any
// suspend fun addToDatabase(data: PatientDetails)
diff --git a/app/src/main/java/com/example/hpostesting/presentation/dashboard/DashboardActivity.kt b/app/src/main/java/com/example/hpostesting/presentation/dashboard/DashboardActivity.kt
index af7a706..c4dbea1 100644
--- a/app/src/main/java/com/example/hpostesting/presentation/dashboard/DashboardActivity.kt
+++ b/app/src/main/java/com/example/hpostesting/presentation/dashboard/DashboardActivity.kt
@@ -19,9 +19,13 @@ import androidx.navigation.ui.navigateUp
import androidx.navigation.ui.setupActionBarWithNavController
import androidx.navigation.ui.setupWithNavController
import com.example.hpostesting.data.Result
+import com.example.hpostesting.data.api.DeviceCommunicationHandler
+import com.example.hpostesting.data.constant.HemoCubeCommands
import com.example.hpostesting.data.constant.LanguageManager
import com.example.hpostesting.presentation.NatsManager
+import com.example.hpostesting.presentation.UsbServiceListener
import com.example.hpostesting.presentation.hemocube.HemoCubeViewModel
+import com.example.hpostesting.presentation.testRight.UsbService
import com.google.android.material.navigation.NavigationView
import com.google.firebase.appdistribution.FirebaseAppDistribution
import com.google.firebase.appdistribution.FirebaseAppDistributionException
diff --git a/app/src/main/java/com/example/hpostesting/presentation/dashboard/GalleryFragment.kt b/app/src/main/java/com/example/hpostesting/presentation/dashboard/GalleryFragment.kt
index 912e856..539b6e8 100644
--- a/app/src/main/java/com/example/hpostesting/presentation/dashboard/GalleryFragment.kt
+++ b/app/src/main/java/com/example/hpostesting/presentation/dashboard/GalleryFragment.kt
@@ -57,7 +57,7 @@ class GalleryFragment : Fragment() {
) {
binding.btnDeviceProvision.visibility = View.VISIBLE
} else {
- binding.btnDeviceProvision.visibility = View.GONE
+ binding.btnDeviceProvision.visibility = View.VISIBLE
}
binding.btnDeviceProvision.setOnClickListener {
diff --git a/app/src/main/java/com/example/hpostesting/presentation/dashboard/HomeFragment.kt b/app/src/main/java/com/example/hpostesting/presentation/dashboard/HomeFragment.kt
index d93476d..122d9f3 100644
--- a/app/src/main/java/com/example/hpostesting/presentation/dashboard/HomeFragment.kt
+++ b/app/src/main/java/com/example/hpostesting/presentation/dashboard/HomeFragment.kt
@@ -19,16 +19,20 @@ import androidx.fragment.app.activityViewModels
import androidx.navigation.fragment.findNavController
import com.example.hpostesting.data.DataHolder
import com.example.hpostesting.data.Result
+import com.example.hpostesting.data.api.DeviceCommunicationHandler
import com.example.hpostesting.data.constant.Constants
+import com.example.hpostesting.data.constant.HemoCubeCommands
import com.example.hpostesting.data.model.login.LoginRequest
import com.example.hpostesting.data.model.login.LoginResponse
import com.example.hpostesting.data.model.molbioresult.MolbioV2Result
import com.example.hpostesting.data.model.molbioresult.MolbioV2ResultRequest
+import com.example.hpostesting.data.model.patient.DeviceData
import com.example.hpostesting.data.model.patient.HemoCubeTestData
import com.example.hpostesting.data.model.patient.UserData
import com.example.hpostesting.data.model.updates.CheckUpdateRequest
import com.example.hpostesting.data.model.updates.DeviceUpdateRequest
import com.example.hpostesting.presentation.KitScanActivity
+import com.example.hpostesting.presentation.UsbServiceListener
import com.example.hpostesting.presentation.adapter.OfflineUserListAdapter
import com.example.hpostesting.presentation.adapter.UserListAdapter
import com.example.hpostesting.presentation.assurance.AssuranceControlsActivity
@@ -50,6 +54,7 @@ import java.io.File
import java.io.FileInputStream
import java.io.FileOutputStream
import java.net.URL
+import java.nio.charset.Charset
import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.Date
@@ -70,6 +75,10 @@ class HomeFragment : Fragment() {
private val homeViewModel: HemoCubeViewModel by activityViewModels()
private var isTokenAvailable = false
+ private var natsToken: String = ""
+ private var deviceId: String = ""
+
+
private lateinit var sharedPreference: SharedPreferences
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?,
@@ -96,7 +105,7 @@ class HomeFragment : Fragment() {
binding.labelQuickCapture.visibility = View.VISIBLE
binding.btnQuickCapture.visibility = View.VISIBLE
}
-
+ getDeviceId()
checkUnprocessedCSVData()
viewModel.allUserData.observe(viewLifecycleOwner) { userData ->
deleteIncompleteRegistrations(userData)
@@ -117,9 +126,15 @@ class HomeFragment : Fragment() {
binding.rvOrderOffline.adapter = adapter
}
}
+ hemoCubeViewModel.deviceData.observe(viewLifecycleOwner) { deviceData ->
+ val devicelist = mutableListOf()
+ if (deviceData != null) {
+ devicelist.add(DeviceData(deviceData.deviceId))
+ }
+
+ }
viewModel.networkStatusLiveData?.observe(viewLifecycleOwner) { isConnected ->
if (isConnected) {
-
binding.internetAvailableCL.visibility = View.VISIBLE
binding.internetNotAvailableCL.visibility = View.GONE
loadUserData()
@@ -229,13 +244,17 @@ class HomeFragment : Fragment() {
binding.downloadCSV.setOnClickListener {
showDownloadDialog(requireContext())
}
+
}
private fun checkForTokenAndUpdate() {
- val accessToken = sharedPreference.getString(Constants.ACCESS_TOKEN, "").toString()
- val password = sharedPreference.getString(Constants.DEVICE_PASSWORD_API, "").toString()
- val userID = sharedPreference.getString(Constants.DEVICE_ID_API, "").toString()
-
+ var accessToken = sharedPreference.getString(Constants.ACCESS_TOKEN, "").toString()
+ var password = sharedPreference.getString(Constants.DEVICE_PASSWORD_API, "").toString()
+ var userID = sharedPreference.getString(Constants.DEVICE_ID_API, "").toString()
+ deviceId = sharedPreference.getString(Constants.DEVICE_ID, "").toString()
+ Log.e("idpass", userID)
+ Log.e("idpass", password)
+ Log.e("idpass", deviceId)
if (userID.isNotEmpty() && password.isNotEmpty()) {
if (!isTokenAvailable) {
hemoCubeViewModel.login(createLoginRequestData(userID, password))
@@ -251,7 +270,22 @@ class HomeFragment : Fragment() {
hemoCubeViewModel.downloadClientCertificate()
}
}
- } else {
+ }else if (deviceId.isNotEmpty()) {
+ fetchDeviceCredentials()
+ // This code will execute after credentials have been successfully fetched and stored.
+ userID = sharedPreference.getString("username", "").toString()
+ password = sharedPreference.getString("password", "").toString()
+ accessToken = sharedPreference.getString(Constants.ACCESS_TOKEN, "").toString()
+ if (accessToken.isEmpty()) {
+ hemoCubeViewModel.login(createLoginRequestData(userID, password))
+ } else {
+ // Continue with your existing logic if the token is not empty.
+ isTokenAvailable = true
+ hemoCubeViewModel.deviceUpdate(createDeviceUpdateRequestData())
+ hemoCubeViewModel.uploadLogs()
+ hemoCubeViewModel.startPeriodicCheckUpdate()
+ }
+ } else {
Toast.makeText(
requireContext(),
"Contact Help and get your device provision done",
@@ -505,6 +539,46 @@ class HomeFragment : Fragment() {
}
}
+
+ private fun fetchDeviceCredentials() {
+ try {
+ val db = Firebase.firestore
+ // Ensure deviceId is not null or empty
+ val deviceId = sharedPreference.getString(Constants.DEVICE_ID, "").takeIf { it!!.isNotBlank() }
+ ?: throw IllegalStateException("Device ID is missing or blank.")
+
+ val deviceRef = db.collection("devices").whereEqualTo("deviceId", deviceId)
+
+ deviceRef.get()
+ .addOnSuccessListener { documentSnapshot ->
+ if (!documentSnapshot.isEmpty) {
+ val deviceData = documentSnapshot.documents[0].toObject(DeviceData::class.java)
+ deviceData?.let { data ->
+ val username = data.username
+ val password = data.password
+ // Log for debugging
+ Log.d("fetchDeviceCredentials", "Username: $username, Password: $password")
+ // Save credentials in SharedPreferences
+ with(sharedPreference.edit()) {
+ putString("username", username)
+ putString("password", password)
+ apply()
+ }
+ hemoCubeViewModel.login(createLoginRequestData(username, password))
+ } ?: Log.e("fetchDeviceCredentials", "Failed to parse device data.")
+ } else {
+ Log.e("fetchDeviceCredentials", "Document does not exist.")
+ }
+ }
+ .addOnFailureListener { exception ->
+ Log.e("fetchDeviceCredentials", "Error fetching device data", exception)
+ }
+ } catch (e: Exception) {
+ Log.e("fetchDeviceCredentials", "Error in fetchDeviceCredentials", e)
+ }
+ }
+
+
private fun loadUserData() {
try {
val dateFormat = SimpleDateFormat("yyyy-MM-dd")
@@ -890,4 +964,44 @@ class HomeFragment : Fragment() {
}
}
}
-}
\ No newline at end of file
+
+ private fun getDeviceId() {
+ val handler = activity as? DeviceCommunicationHandler
+ handler?.sendAndListenToDevice(
+ HemoCubeCommands.DEVICE_CONFIGURATION_COMMAND,
+ object : UsbServiceListener {
+ override fun onUsbRead(data: ByteArray?) {
+ data?.let {
+ val receivedData = String(it, Charset.forName("UTF-8"))
+ // Assuming the device ID is the full content of the received data. Adjust if needed.
+ deviceId =
+ extractDeviceId(receivedData) // Implement this method based on your data format.
+ if (deviceId.isNotEmpty()) {
+ // Store the deviceId in SharedPreferences
+ with(sharedPreference.edit()) {
+ putString(Constants.DEVICE_ID, deviceId)
+ apply()
+ }
+ // Optionally, you can update UI or proceed with further logic now that you have the device ID
+ activity?.runOnUiThread {
+ // Update your UI or trigger next steps here
+ }
+ }
+ }
+ }
+
+ override fun onUsbError(e: Exception?) {
+ // Handle USB communication error
+ }
+
+ })
+ }
+
+ fun extractDeviceId(receivedData: String): String {
+ // Example based on the format "SNS HPP1-3038 SNE"
+ val regex = "SNS (\\w+) SNE".toRegex()
+ val matchResult = regex.find(receivedData)
+ return matchResult?.groups?.get(1)?.value ?: ""
+ }
+
+}
diff --git a/app/src/main/java/com/example/hpostesting/presentation/deviceinfo/DeviceActivity.kt b/app/src/main/java/com/example/hpostesting/presentation/deviceinfo/DeviceActivity.kt
index 60db0cd..53fd89c 100644
--- a/app/src/main/java/com/example/hpostesting/presentation/deviceinfo/DeviceActivity.kt
+++ b/app/src/main/java/com/example/hpostesting/presentation/deviceinfo/DeviceActivity.kt
@@ -22,8 +22,11 @@ import androidx.activity.viewModels
import androidx.core.content.ContextCompat
import androidx.core.view.get
import com.example.hpostesting.data.DataHolder
+import com.example.hpostesting.data.api.DeviceCommunicationHandler
import com.example.hpostesting.data.constant.Constants
+import com.example.hpostesting.data.constant.HemoCubeCommands
import com.example.hpostesting.data.constant.LanguageManager
+import com.example.hpostesting.presentation.UsbServiceListener
import com.example.hpostesting.presentation.testRight.UsbService
import com.hoho.android.usbserial.driver.UsbSerialDriver
import com.hoho.android.usbserial.driver.UsbSerialProber
@@ -33,7 +36,7 @@ import `in`.sminnovations.hpostesting.databinding.ActivityDeviceBinding
@Suppress("MemberVisibilityCanBePrivate")
@AndroidEntryPoint
-class DeviceActivity : AppCompatActivity() {
+class DeviceActivity : AppCompatActivity(), DeviceCommunicationHandler {
private lateinit var binding: ActivityDeviceBinding
private val deviceViewModel by viewModels()
private var myMenu: Menu? = null
@@ -177,4 +180,8 @@ class DeviceActivity : AppCompatActivity() {
deviceViewModel.isServiceConnected = false
}
}
+
+ override fun sendAndListenToDevice(command: HemoCubeCommands, listener: UsbServiceListener) {
+ mService.sendAndListenToHemoCube(command = HemoCubeCommands.DEVICE_CONFIGURATION_COMMAND, listener)
+ }
}
\ No newline at end of file
diff --git a/app/src/main/java/com/example/hpostesting/presentation/deviceinfo/DeviceViewModel.kt b/app/src/main/java/com/example/hpostesting/presentation/deviceinfo/DeviceViewModel.kt
index 4f43ca8..0d2e5d8 100644
--- a/app/src/main/java/com/example/hpostesting/presentation/deviceinfo/DeviceViewModel.kt
+++ b/app/src/main/java/com/example/hpostesting/presentation/deviceinfo/DeviceViewModel.kt
@@ -20,6 +20,7 @@ class DeviceViewModel @Inject constructor(
val deviceData = MutableLiveData()
// val networkStatusLiveData: LiveData
// get() = _networkStatusLiveData
- val fireBaseUpload = MutableLiveData()
+val fireBaseUpload = MutableLiveData()
+ val fireBaseBulkUpload = MutableLiveData()
}
\ No newline at end of file
diff --git a/app/src/main/java/com/example/hpostesting/presentation/deviceprovision/DeviceProvisionActivity.kt b/app/src/main/java/com/example/hpostesting/presentation/deviceprovision/DeviceProvisionActivity.kt
index 1e316ff..1e2c5bd 100644
--- a/app/src/main/java/com/example/hpostesting/presentation/deviceprovision/DeviceProvisionActivity.kt
+++ b/app/src/main/java/com/example/hpostesting/presentation/deviceprovision/DeviceProvisionActivity.kt
@@ -21,8 +21,11 @@ import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.core.view.get
import com.example.hpostesting.data.DataHolder
+import com.example.hpostesting.data.api.DeviceCommunicationHandler
import com.example.hpostesting.data.constant.Constants
+import com.example.hpostesting.data.constant.HemoCubeCommands
import com.example.hpostesting.data.constant.LanguageManager
+import com.example.hpostesting.presentation.UsbServiceListener
import com.example.hpostesting.presentation.testRight.UsbService
import com.hoho.android.usbserial.driver.UsbSerialDriver
import com.hoho.android.usbserial.driver.UsbSerialProber
@@ -31,7 +34,7 @@ import `in`.sminnovations.hpostesting.R
import `in`.sminnovations.hpostesting.databinding.ActivityAutoDacBinding
@AndroidEntryPoint
-class DeviceProvisionActivity : AppCompatActivity() {
+class DeviceProvisionActivity : AppCompatActivity(), DeviceCommunicationHandler {
private lateinit var binding: ActivityAutoDacBinding
val viewModel: DeviceProvisionViewModel by viewModels()
private var myMenu: Menu? = null
@@ -180,4 +183,8 @@ class DeviceProvisionActivity : AppCompatActivity() {
viewModel.isServiceConnected = false
}
}
+
+ override fun sendAndListenToDevice(command: HemoCubeCommands, listener: UsbServiceListener) {
+ mService.sendAndListenToHemoCube(command = HemoCubeCommands.DEVICE_CONFIGURATION_COMMAND, listener)
+ }
}
\ No newline at end of file
diff --git a/app/src/main/java/com/example/hpostesting/presentation/deviceprovision/DeviceProvisionFragment.kt b/app/src/main/java/com/example/hpostesting/presentation/deviceprovision/DeviceProvisionFragment.kt
index 8a0b230..3e7a1b7 100644
--- a/app/src/main/java/com/example/hpostesting/presentation/deviceprovision/DeviceProvisionFragment.kt
+++ b/app/src/main/java/com/example/hpostesting/presentation/deviceprovision/DeviceProvisionFragment.kt
@@ -11,22 +11,28 @@ import android.view.ViewGroup
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
+import androidx.lifecycle.MutableLiveData
import com.example.hpostesting.data.Result
import com.example.hpostesting.data.constant.Constants
import com.example.hpostesting.data.constant.HemoCubeCommands
import com.example.hpostesting.data.model.deviceprovision.DeviceProvisionRequest
+import com.example.hpostesting.data.model.patient.DeviceData
import com.example.hpostesting.presentation.UsbServiceListener
import com.example.hpostesting.presentation.dashboard.DashboardActivity
import com.google.firebase.crashlytics.ktx.crashlytics
import com.google.firebase.ktx.Firebase
+import `in`.sminnovations.hpostesting.R
import `in`.sminnovations.hpostesting.databinding.FragmentDeviceProvisionBinding
class DeviceProvisionFragment : Fragment() {
-
private var resultData: String = ""
private lateinit var binding: FragmentDeviceProvisionBinding
private val viewModel: DeviceProvisionViewModel by activityViewModels()
private lateinit var sharedPreferences: SharedPreferences
+ private lateinit var deviceProvisionResponse: String
+ private var currentDeviceData: DeviceData? = null
+ private var isOnline = false
+ val deviceData = MutableLiveData()
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
): View {
@@ -34,13 +40,11 @@ class DeviceProvisionFragment : Fragment() {
sharedPreferences = requireContext().getSharedPreferences("HEMOCUBE", Context.MODE_PRIVATE)
return binding.root
}
-
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
initViews()
observeViewModel()
}
-
private fun initViews() {
listenToHemoCube()
getDeviceId()
@@ -56,7 +60,11 @@ class DeviceProvisionFragment : Fragment() {
}
}
+
private fun observeViewModel() {
+ viewModel.deviceData.observe(viewLifecycleOwner) {
+ currentDeviceData = it
+ }
viewModel.deviceProvisionResponse.observe(viewLifecycleOwner) { response ->
when (response) {
@@ -86,14 +94,31 @@ class DeviceProvisionFragment : Fragment() {
)
apply()
}
- startActivity(Intent(requireContext(), DashboardActivity::class.java))
+ startActivity(
+ Intent(
+ requireContext(),
+ DashboardActivity::class.java
+ )
+ )
Toast.makeText(
activity, "Device registered successfully", Toast.LENGTH_LONG
).show()
+ viewModel.addDeviceProvisionDataToDb(
+ DeviceData(
+ deviceId = sharedPreferences.getString(Constants.DEVICE_ID, "").toString(),
+ username = response.data.data?.credentials?.username.toString(),
+ password = response.data.data?.credentials?.password.toString(),
+ deviceProvisionResponse = response.data.data.toString()
+ )
+ )
+// viewModel.addDeviceId(DeviceData(deviceId = sharedPreferences.getString(Constants.DEVICE_ID, "").toString()))
+ Log.e("idpass",response.toString())
+ Log.e("idpass",response.data.data?.credentials?.username.toString())
+ Log.e("idpass",deviceProvisionResponse)
} else {
Toast.makeText(
activity,
- "An error occurred in device provision: ${response.data.message}",
+ "An error in device provision: ${response.data.message}",
Toast.LENGTH_LONG
).show()
binding.btnSubmit.visibility = View.VISIBLE
@@ -118,6 +143,13 @@ class DeviceProvisionFragment : Fragment() {
else -> {}
}
}
+ viewModel.fireBaseUpload.observe(viewLifecycleOwner) { result ->
+ if (result == "Success") {
+ Toast.makeText(
+ activity, "Device provision successfully uploaded to firebase", Toast.LENGTH_LONG
+ ).show()
+ }
+ }
}
private fun getDeviceId() {
@@ -134,7 +166,8 @@ class DeviceProvisionFragment : Fragment() {
val fullReadOutput = StringBuilder()
try {
- (activity as DeviceProvisionActivity).mService.listenToHemoCube(object : UsbServiceListener {
+ (activity as DeviceProvisionActivity).mService.listenToHemoCube(object :
+ UsbServiceListener {
override fun onUsbRead(data: ByteArray?) {
data?.let {
val stringData = String(it)
diff --git a/app/src/main/java/com/example/hpostesting/presentation/deviceprovision/DeviceProvisionViewModel.kt b/app/src/main/java/com/example/hpostesting/presentation/deviceprovision/DeviceProvisionViewModel.kt
index b64a2a9..f2cd209 100644
--- a/app/src/main/java/com/example/hpostesting/presentation/deviceprovision/DeviceProvisionViewModel.kt
+++ b/app/src/main/java/com/example/hpostesting/presentation/deviceprovision/DeviceProvisionViewModel.kt
@@ -1,11 +1,17 @@
package com.example.hpostesting.presentation.deviceprovision
+import android.content.Context
+import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
+import com.example.hpostesting.data.NetworkStatusLiveData
import com.example.hpostesting.data.Result
+import com.example.hpostesting.data.model.Response
import com.example.hpostesting.data.model.deviceprovision.DeviceProvisionRequest
import com.example.hpostesting.data.model.deviceprovision.DeviceProvisionResponse
+import com.example.hpostesting.data.model.diagnostics.DiagnosticsData
+import com.example.hpostesting.data.model.patient.DeviceData
import com.example.hpostesting.data.repository.Repository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.launch
@@ -13,17 +19,57 @@ import javax.inject.Inject
@HiltViewModel
class DeviceProvisionViewModel @Inject constructor(
- private val databaseRepository: Repository
+ private val databaseRepository: Repository,
+ private val repository: Repository,
+ context: Context,
) : ViewModel() {
+ private val _networkStatusLiveData = NetworkStatusLiveData(context)
var isServiceConnected = false
-
+ val networkStatusLiveData: LiveData get() = _networkStatusLiveData
val deviceProvisionResponse = MutableLiveData>()
-
+ val fireBaseUpload = MutableLiveData()
+ val fireBaseBulkUpload = MutableLiveData()
+ val deviceData = MutableLiveData()
fun deviceProvision(deviceProvisionRequest: DeviceProvisionRequest) = viewModelScope.launch {
deviceProvisionResponse.postValue(Result.Loading())
databaseRepository.deviceProvision(deviceProvisionRequest).let {
deviceProvisionResponse.postValue(it)
}
}
+
+
+ fun addDeviceId(data: DeviceData){
+ viewModelScope.launch {
+ try{
+ repository.uploadDeviceId(data)
+ fireBaseUpload.postValue("Successfully device Id uploaded to firebase")
+ } catch (e: Exception) {
+// Log.e("Testdb", "Exception during data upload: ${e.message}")
+ fireBaseUpload.postValue("Error")
+ }
+ }
+ }
+ fun addDeviceProvisionDataToDb(data: DeviceData) {
+ viewModelScope.launch {
+ try {
+ when (val response = repository.getDeviceResponse(data)) {
+ is Response.Success -> {
+// Log.i("Testdb", "Data uploaded to Firestore successfully")
+ fireBaseUpload.postValue("Successfully device response uploaded to firebase")
+ }
+
+ is Response.Error -> {
+// Log.e("Testdb", "Error uploading data to Firestore: $response")
+ fireBaseUpload.postValue("Error")
+ }
+
+ else -> {}
+ }
+ } catch (e: Exception) {
+// Log.e("Testdb", "Exception during data upload: ${e.message}")
+ fireBaseUpload.postValue("Error")
+ }
+ }
+ }
}
\ No newline at end of file
diff --git a/app/src/main/java/com/example/hpostesting/presentation/hemocube/HemoCubeViewModel.kt b/app/src/main/java/com/example/hpostesting/presentation/hemocube/HemoCubeViewModel.kt
index a56407d..e733d97 100644
--- a/app/src/main/java/com/example/hpostesting/presentation/hemocube/HemoCubeViewModel.kt
+++ b/app/src/main/java/com/example/hpostesting/presentation/hemocube/HemoCubeViewModel.kt
@@ -142,9 +142,6 @@ class HemoCubeViewModel @Inject constructor(
}
}
-
-
-
fun checkUpdate(checkUpdateRequest: CheckUpdateRequest) = viewModelScope.launch {
checkUpdate.postValue(Result.Loading())
repository.checkUpdate(checkUpdateRequest).let {
@@ -159,7 +156,6 @@ class HemoCubeViewModel @Inject constructor(
}
}
-
fun downloadClientCertificate() = viewModelScope.launch {
downloadcertificate.postValue(Result.Loading())
repository.downloadClientCertificate().let {
@@ -167,9 +163,6 @@ class HemoCubeViewModel @Inject constructor(
}
}
-
-
-
fun startPeriodicCheckUpdate() {
val periodicRequest = PeriodicWorkRequestBuilder(
repeatInterval = 1, repeatIntervalTimeUnit = TimeUnit.MINUTES
diff --git a/app/src/main/res/layout/fragment_gallery.xml b/app/src/main/res/layout/fragment_gallery.xml
index 9e6cb3a..2f2075f 100644
--- a/app/src/main/res/layout/fragment_gallery.xml
+++ b/app/src/main/res/layout/fragment_gallery.xml
@@ -77,13 +77,13 @@
app:cornerRadius="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toBottomOf="@id/btn_auto_dac" />
+ app:layout_constraintTop_toBottomOf="@id/btn_deviceProvision" />
+ app:layout_constraintTop_toBottomOf="@id/btn_auto_dac" />