Added code for fetching response of device provision api from firebase
This commit is contained in:
@@ -0,0 +1,8 @@
|
|||||||
|
package com.example.hpostesting.data.model.deviceprovision
|
||||||
|
|
||||||
|
data class ProvisionData(
|
||||||
|
val username: String?,
|
||||||
|
val password: String?,
|
||||||
|
val natsToken: String?
|
||||||
|
)
|
||||||
|
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
package com.example.hpostesting.presentation.dashboard
|
package com.example.hpostesting.presentation.dashboard
|
||||||
|
|
||||||
import android.app.AlertDialog
|
import android.app.AlertDialog
|
||||||
|
import android.content.ContentValues.TAG
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.Context.BATTERY_SERVICE
|
import android.content.Context.BATTERY_SERVICE
|
||||||
import android.content.DialogInterface
|
import android.content.DialogInterface
|
||||||
@@ -20,10 +21,12 @@ import androidx.navigation.fragment.findNavController
|
|||||||
import com.example.hpostesting.data.DataHolder
|
import com.example.hpostesting.data.DataHolder
|
||||||
import com.example.hpostesting.data.Result
|
import com.example.hpostesting.data.Result
|
||||||
import com.example.hpostesting.data.constant.Constants
|
import com.example.hpostesting.data.constant.Constants
|
||||||
|
import com.example.hpostesting.data.model.deviceprovision.ProvisionData
|
||||||
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
|
||||||
import com.example.hpostesting.data.model.molbioresult.MolbioV2ResultRequest
|
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.HemoCubeTestData
|
||||||
import com.example.hpostesting.data.model.patient.UserData
|
import com.example.hpostesting.data.model.patient.UserData
|
||||||
import com.example.hpostesting.data.model.updates.CheckUpdateRequest
|
import com.example.hpostesting.data.model.updates.CheckUpdateRequest
|
||||||
@@ -35,11 +38,14 @@ import com.example.hpostesting.presentation.assurance.AssuranceControlsActivity
|
|||||||
import com.example.hpostesting.presentation.hemocube.HemoCubeViewModel
|
import com.example.hpostesting.presentation.hemocube.HemoCubeViewModel
|
||||||
import com.example.hpostesting.presentation.testRight.TestRightViewModel
|
import com.example.hpostesting.presentation.testRight.TestRightViewModel
|
||||||
import com.firebase.ui.firestore.FirestoreRecyclerOptions
|
import com.firebase.ui.firestore.FirestoreRecyclerOptions
|
||||||
|
import com.google.common.reflect.TypeToken
|
||||||
import com.google.firebase.crashlytics.ktx.crashlytics
|
import com.google.firebase.crashlytics.ktx.crashlytics
|
||||||
import com.google.firebase.firestore.Query
|
import com.google.firebase.firestore.Query
|
||||||
import com.google.firebase.firestore.ktx.firestore
|
import com.google.firebase.firestore.ktx.firestore
|
||||||
import com.google.firebase.ktx.Firebase
|
import com.google.firebase.ktx.Firebase
|
||||||
import com.google.firebase.perf.ktx.performance
|
import com.google.firebase.perf.ktx.performance
|
||||||
|
import com.google.gson.Gson
|
||||||
|
import com.google.gson.JsonSyntaxException
|
||||||
import dagger.hilt.android.AndroidEntryPoint
|
import dagger.hilt.android.AndroidEntryPoint
|
||||||
import `in`.sminnovations.hpostesting.R
|
import `in`.sminnovations.hpostesting.R
|
||||||
import `in`.sminnovations.hpostesting.databinding.FragmentHomeBinding
|
import `in`.sminnovations.hpostesting.databinding.FragmentHomeBinding
|
||||||
@@ -62,6 +68,10 @@ class HomeFragment : Fragment() {
|
|||||||
private val homeViewModel: HemoCubeViewModel by activityViewModels()
|
private val homeViewModel: HemoCubeViewModel by activityViewModels()
|
||||||
|
|
||||||
private var isTokenAvailable = false
|
private var isTokenAvailable = false
|
||||||
|
private var username:String =""
|
||||||
|
private var passworD:String =""
|
||||||
|
private var natsToken:String =""
|
||||||
|
|
||||||
|
|
||||||
private lateinit var sharedPreference: SharedPreferences
|
private lateinit var sharedPreference: SharedPreferences
|
||||||
override fun onCreateView(
|
override fun onCreateView(
|
||||||
@@ -186,12 +196,16 @@ class HomeFragment : Fragment() {
|
|||||||
binding.downloadCSV.setOnClickListener {
|
binding.downloadCSV.setOnClickListener {
|
||||||
showDownloadDialog(requireContext())
|
showDownloadDialog(requireContext())
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun checkForTokenAndUpdate() {
|
private fun checkForTokenAndUpdate() {
|
||||||
val accessToken = sharedPreference.getString(Constants.ACCESS_TOKEN, "").toString()
|
val accessToken = sharedPreference.getString(Constants.ACCESS_TOKEN, "").toString()
|
||||||
val password = sharedPreference.getString(Constants.DEVICE_PASSWORD_API, "").toString()
|
val password = sharedPreference.getString(Constants.DEVICE_PASSWORD_API, "").toString()
|
||||||
val userID = sharedPreference.getString(Constants.DEVICE_ID_API, "").toString()
|
var userID = sharedPreference.getString(Constants.DEVICE_ID_API, "").toString()
|
||||||
|
fetchDeviceProvisionResponse(deviceId = Constants.DEVICE_ID)
|
||||||
|
val devuserid = username
|
||||||
|
val devpassword = passworD
|
||||||
if (userID.isNotEmpty() && password.isNotEmpty()) {
|
if (userID.isNotEmpty() && password.isNotEmpty()) {
|
||||||
if (accessToken.isEmpty()) {
|
if (accessToken.isEmpty()) {
|
||||||
hemoCubeViewModel.login(createLoginRequestData(userID, password))
|
hemoCubeViewModel.login(createLoginRequestData(userID, password))
|
||||||
@@ -205,7 +219,21 @@ class HomeFragment : Fragment() {
|
|||||||
hemoCubeViewModel.startPeriodicCheckUpdate()
|
hemoCubeViewModel.startPeriodicCheckUpdate()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else if(devuserid.isNotEmpty() && devpassword.isNotEmpty()){
|
||||||
|
hemoCubeViewModel.login(createLoginRequestData(devuserid, devpassword))
|
||||||
|
if (accessToken.isEmpty()) {
|
||||||
|
hemoCubeViewModel.login(createLoginRequestData(devuserid, devpassword))
|
||||||
|
} else {
|
||||||
|
if (isTokenExpired(accessToken)) {
|
||||||
|
hemoCubeViewModel.login(createLoginRequestData(devuserid, devpassword))
|
||||||
|
} else {
|
||||||
|
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",
|
||||||
@@ -366,6 +394,50 @@ class HomeFragment : Fragment() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private fun fetchDeviceProvisionResponse(deviceId: String) {
|
||||||
|
val db = Firebase.firestore
|
||||||
|
val deviceRef = db.collection("devices").document(deviceId)
|
||||||
|
|
||||||
|
deviceRef.get()
|
||||||
|
.addOnSuccessListener { document ->
|
||||||
|
if (document.exists()) {
|
||||||
|
val deviceData = document.toObject(DeviceData::class.java)
|
||||||
|
val deviceProvisionResponse = deviceData?.deviceProvisionResponse
|
||||||
|
|
||||||
|
// Parse the deviceProvisionResponse to extract username, password, natsToken
|
||||||
|
val provisionData = deviceProvisionResponse?.let {
|
||||||
|
parseDeviceProvisionResponse(
|
||||||
|
it
|
||||||
|
)
|
||||||
|
}
|
||||||
|
provisionData?.let { data ->
|
||||||
|
username = data.username.toString()
|
||||||
|
passworD = data.password.toString()
|
||||||
|
natsToken = data.natsToken.toString()
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Handle the case where the device document doesn't exist
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.addOnFailureListener { exception ->
|
||||||
|
// Handle any errors
|
||||||
|
Log.e(TAG, "Error fetching device data: $exception")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun parseDeviceProvisionResponse(responseString: String): ProvisionData? {
|
||||||
|
return try {
|
||||||
|
val gson = Gson()
|
||||||
|
val type = object : TypeToken<ProvisionData>() {}.type
|
||||||
|
gson.fromJson(responseString, type)
|
||||||
|
} catch (e: JsonSyntaxException) {
|
||||||
|
null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private fun loadUserData() {
|
private fun loadUserData() {
|
||||||
try {
|
try {
|
||||||
val dateFormat = SimpleDateFormat("yyyy-MM-dd")
|
val dateFormat = SimpleDateFormat("yyyy-MM-dd")
|
||||||
|
|||||||
Reference in New Issue
Block a user