Added code related to device provision fetching fromfirestore
This commit is contained in:
@@ -120,9 +120,15 @@ class HomeFragment : Fragment() {
|
||||
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 ->
|
||||
if (isConnected) {
|
||||
|
||||
binding.internetAvailableCL.visibility = View.VISIBLE
|
||||
binding.internetNotAvailableCL.visibility = View.GONE
|
||||
loadUserData()
|
||||
@@ -201,12 +207,18 @@ class HomeFragment : Fragment() {
|
||||
|
||||
private fun checkForTokenAndUpdate() {
|
||||
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()
|
||||
fetchDeviceProvisionResponse(deviceId = Constants.DEVICE_ID)
|
||||
val devuserid = username
|
||||
val devpassword = passworD
|
||||
if (userID.isNotEmpty() && password.isNotEmpty()) {
|
||||
val deviceId = sharedPreference.getString(Constants.DEVICE_ID,"")
|
||||
|
||||
if(userID.isEmpty() && password.isEmpty()){
|
||||
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()) {
|
||||
hemoCubeViewModel.login(createLoginRequestData(userID, password))
|
||||
} else {
|
||||
@@ -219,20 +231,6 @@ class HomeFragment : Fragment() {
|
||||
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{
|
||||
Toast.makeText(
|
||||
requireContext(),
|
||||
@@ -412,10 +410,20 @@ class HomeFragment : Fragment() {
|
||||
)
|
||||
}
|
||||
provisionData?.let { data ->
|
||||
// Assuming 'data' has the fields 'username', 'password', and 'natsToken'
|
||||
username = data.username.toString()
|
||||
passworD = data.password.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 {
|
||||
// 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 {
|
||||
val gson = Gson()
|
||||
val type = object : TypeToken<ProvisionData>() {}.type
|
||||
|
||||
@@ -97,6 +97,8 @@ class DeviceProvisionFragment : Fragment() {
|
||||
activity, "Device registered successfully", Toast.LENGTH_LONG
|
||||
).show()
|
||||
|
||||
response.data.data?.credentials?.username?.let { DeviceData(deviceId = it) }
|
||||
?.let { viewModel.addDeviceId(it) }
|
||||
viewModel.addDeviceProvisionDataToDb(
|
||||
DeviceData(
|
||||
deviceProvisionResponse = response.toString()
|
||||
|
||||
@@ -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) {
|
||||
viewModelScope.launch {
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user