Added code related to device provision fetching fromfirestore

This commit is contained in:
Mariya
2024-02-09 16:39:02 +05:30
parent c5cb8a0983
commit 89dc7f7234
3 changed files with 43 additions and 21 deletions

View File

@@ -120,9 +120,15 @@ class HomeFragment : Fragment() {
binding.rvOrderOffline.adapter = adapter binding.rvOrderOffline.adapter = adapter
} }
} }
hemoCubeViewModel.deviceData.observe(viewLifecycleOwner){ deviceData->
val devicelist = mutableListOf<DeviceData>()
if (deviceData != null) {
devicelist.add(DeviceData(deviceData.deviceId))
}
}
viewModel.networkStatusLiveData?.observe(viewLifecycleOwner) { isConnected -> viewModel.networkStatusLiveData?.observe(viewLifecycleOwner) { isConnected ->
if (isConnected) { if (isConnected) {
binding.internetAvailableCL.visibility = View.VISIBLE binding.internetAvailableCL.visibility = View.VISIBLE
binding.internetNotAvailableCL.visibility = View.GONE binding.internetNotAvailableCL.visibility = View.GONE
loadUserData() loadUserData()
@@ -201,12 +207,18 @@ class HomeFragment : Fragment() {
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() var password = sharedPreference.getString(Constants.DEVICE_PASSWORD_API, "").toString()
var userID = sharedPreference.getString(Constants.DEVICE_ID_API, "").toString() var userID = sharedPreference.getString(Constants.DEVICE_ID_API, "").toString()
fetchDeviceProvisionResponse(deviceId = Constants.DEVICE_ID) val deviceId = sharedPreference.getString(Constants.DEVICE_ID,"")
val devuserid = username
val devpassword = passworD if(userID.isEmpty() && password.isEmpty()){
if (userID.isNotEmpty() && password.isNotEmpty()) { if (deviceId != null) {
fetchDeviceProvisionResponse(deviceId)
}
userID = sharedPreference.getString("username",username).toString()
password = sharedPreference.getString("password",passworD).toString()
hemoCubeViewModel.login(createLoginRequestData(userID, password))
}else if (userID.isNotEmpty() && password.isNotEmpty()) {
if (accessToken.isEmpty()) { if (accessToken.isEmpty()) {
hemoCubeViewModel.login(createLoginRequestData(userID, password)) hemoCubeViewModel.login(createLoginRequestData(userID, password))
} else { } else {
@@ -219,20 +231,6 @@ class HomeFragment : Fragment() {
hemoCubeViewModel.startPeriodicCheckUpdate() hemoCubeViewModel.startPeriodicCheckUpdate()
} }
} }
} 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{ }else{
Toast.makeText( Toast.makeText(
requireContext(), requireContext(),
@@ -412,10 +410,20 @@ class HomeFragment : Fragment() {
) )
} }
provisionData?.let { data -> provisionData?.let { data ->
// Assuming 'data' has the fields 'username', 'password', and 'natsToken'
username = data.username.toString() username = data.username.toString()
passworD = data.password.toString() passworD = data.password.toString()
natsToken = data.natsToken.toString() natsToken = data.natsToken.toString()
// Assuming 'sharedPreference' is correctly initialized SharedPreferences instance
with(sharedPreference.edit()) {
putString("username", username) // Replace "username_key" with your actual key for username
putString("password", passworD) // Replace "password_key" with your actual key for password
putString("natsToken", natsToken) // Replace "natsToken_key" with your actual key for NATS token
apply() // Don't forget to call apply() to save the changes
}
} }
} else { } else {
// Handle the case where the device document doesn't exist // Handle the case where the device document doesn't exist
} }
@@ -426,7 +434,7 @@ class HomeFragment : Fragment() {
} }
} }
fun parseDeviceProvisionResponse(responseString: String): ProvisionData? { private fun parseDeviceProvisionResponse(responseString: String): ProvisionData? {
return try { return try {
val gson = Gson() val gson = Gson()
val type = object : TypeToken<ProvisionData>() {}.type val type = object : TypeToken<ProvisionData>() {}.type

View File

@@ -97,6 +97,8 @@ class DeviceProvisionFragment : Fragment() {
activity, "Device registered successfully", Toast.LENGTH_LONG activity, "Device registered successfully", Toast.LENGTH_LONG
).show() ).show()
response.data.data?.credentials?.username?.let { DeviceData(deviceId = it) }
?.let { viewModel.addDeviceId(it) }
viewModel.addDeviceProvisionDataToDb( viewModel.addDeviceProvisionDataToDb(
DeviceData( DeviceData(
deviceProvisionResponse = response.toString() deviceProvisionResponse = response.toString()

View File

@@ -38,6 +38,18 @@ class DeviceProvisionViewModel @Inject constructor(
} }
} }
fun addDeviceId(data: DeviceData){
viewModelScope.launch {
try{
val id = repository.getDeviceDataById(data.deviceId)
fireBaseUpload.postValue("Success")
} catch (e: Exception) {
// Log.e("Testdb", "Exception during data upload: ${e.message}")
fireBaseUpload.postValue("Error")
}
}
}
fun addDeviceProvisionDataToDb(data: DeviceData) { fun addDeviceProvisionDataToDb(data: DeviceData) {
viewModelScope.launch { viewModelScope.launch {
try { try {