Added code to send response of device provision api into firebase
This commit is contained in:
@@ -10,27 +10,19 @@ import android.view.ViewGroup
|
|||||||
import android.widget.Toast
|
import android.widget.Toast
|
||||||
import androidx.fragment.app.Fragment
|
import androidx.fragment.app.Fragment
|
||||||
import androidx.fragment.app.activityViewModels
|
import androidx.fragment.app.activityViewModels
|
||||||
import androidx.lifecycle.LiveData
|
|
||||||
import androidx.lifecycle.MutableLiveData
|
import androidx.lifecycle.MutableLiveData
|
||||||
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.constant.HemoCubeCommands
|
import com.example.hpostesting.data.constant.HemoCubeCommands
|
||||||
import com.example.hpostesting.data.model.deviceprovision.DeviceProvisionRequest
|
import com.example.hpostesting.data.model.deviceprovision.DeviceProvisionRequest
|
||||||
import com.example.hpostesting.data.model.diagnostics.DiagnosticsData
|
|
||||||
import com.example.hpostesting.data.model.patient.DeviceData
|
import com.example.hpostesting.data.model.patient.DeviceData
|
||||||
import com.example.hpostesting.presentation.UsbServiceListener
|
import com.example.hpostesting.presentation.UsbServiceListener
|
||||||
import com.example.hpostesting.presentation.dashboard.DashboardActivity
|
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.R
|
|
||||||
import `in`.sminnovations.hpostesting.databinding.FragmentDeviceProvisionBinding
|
import `in`.sminnovations.hpostesting.databinding.FragmentDeviceProvisionBinding
|
||||||
import java.text.SimpleDateFormat
|
|
||||||
import java.util.Calendar
|
|
||||||
import java.util.Locale
|
|
||||||
|
|
||||||
class DeviceProvisionFragment : Fragment() {
|
class DeviceProvisionFragment : Fragment() {
|
||||||
|
|
||||||
private var resultData: String = ""
|
private var resultData: String = ""
|
||||||
private lateinit var binding: FragmentDeviceProvisionBinding
|
private lateinit var binding: FragmentDeviceProvisionBinding
|
||||||
private val viewModel: DeviceProvisionViewModel by activityViewModels()
|
private val viewModel: DeviceProvisionViewModel by activityViewModels()
|
||||||
@@ -46,13 +38,11 @@ class DeviceProvisionFragment : Fragment() {
|
|||||||
sharedPreferences = requireContext().getSharedPreferences("HEMOCUBE", Context.MODE_PRIVATE)
|
sharedPreferences = requireContext().getSharedPreferences("HEMOCUBE", Context.MODE_PRIVATE)
|
||||||
return binding.root
|
return binding.root
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||||
super.onViewCreated(view, savedInstanceState)
|
super.onViewCreated(view, savedInstanceState)
|
||||||
initViews()
|
initViews()
|
||||||
observeViewModel()
|
observeViewModel()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun initViews() {
|
private fun initViews() {
|
||||||
listenToHemoCube()
|
listenToHemoCube()
|
||||||
getDeviceId()
|
getDeviceId()
|
||||||
@@ -66,87 +56,81 @@ class DeviceProvisionFragment : Fragment() {
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
viewModel.deviceData.observe(viewLifecycleOwner) {
|
viewModel.deviceData.observe(viewLifecycleOwner) {
|
||||||
currentDeviceData = it
|
currentDeviceData = it
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
private fun observeViewModel() {
|
private fun observeViewModel() {
|
||||||
|
|
||||||
viewModel.deviceProvisionResponse.observe(viewLifecycleOwner) { response ->
|
viewModel.deviceProvisionResponse.observe(viewLifecycleOwner) { response ->
|
||||||
when (response) {
|
when (response) {
|
||||||
is Result.Success -> {
|
is Result.Success -> {
|
||||||
if (response.data.result == "Success") {
|
if (response.data.result == "Success") {
|
||||||
with(sharedPreferences.edit()) {
|
with(sharedPreferences.edit()) {
|
||||||
putString(
|
putString(
|
||||||
Constants.DEVICE_ID_API,
|
Constants.DEVICE_ID_API,
|
||||||
response.data.data?.credentials?.username
|
response.data.data?.credentials?.username
|
||||||
)
|
)
|
||||||
putString(
|
putString(
|
||||||
Constants.DEVICE_PASSWORD_API,
|
Constants.DEVICE_PASSWORD_API,
|
||||||
response.data.data?.credentials?.password
|
response.data.data?.credentials?.password
|
||||||
)
|
)
|
||||||
putString(
|
putString(
|
||||||
Constants.NATS_TOKEN,
|
Constants.NATS_TOKEN,
|
||||||
response.data.data?.device?.deviceUser?.natsToken
|
response.data.data?.device?.deviceUser?.natsToken
|
||||||
)
|
)
|
||||||
putString(
|
putString(
|
||||||
Constants.NATS_TOKEN_EXPIRE_DATE,
|
Constants.NATS_TOKEN_EXPIRE_DATE,
|
||||||
response.data.data?.device?.deviceUser?.natsTokenExpiry
|
response.data.data?.device?.deviceUser?.natsTokenExpiry
|
||||||
)
|
)
|
||||||
apply()
|
apply()
|
||||||
}
|
|
||||||
startActivity(
|
|
||||||
Intent(
|
|
||||||
requireContext(),
|
|
||||||
DashboardActivity::class.java
|
|
||||||
)
|
|
||||||
)
|
|
||||||
Toast.makeText(
|
|
||||||
activity, "Device registered successfully", Toast.LENGTH_LONG
|
|
||||||
).show()
|
|
||||||
|
|
||||||
viewModel.addDeviceProvisionDataToDb(
|
|
||||||
DeviceData(
|
|
||||||
deviceProvisionResponse = response.toString()
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
} else {
|
|
||||||
Toast.makeText(
|
|
||||||
activity,
|
|
||||||
"An error in device provision: ${response.data.message}",
|
|
||||||
Toast.LENGTH_LONG
|
|
||||||
).show()
|
|
||||||
binding.btnSubmit.visibility = View.VISIBLE
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
startActivity(
|
||||||
|
Intent(
|
||||||
|
requireContext(),
|
||||||
|
DashboardActivity::class.java
|
||||||
|
)
|
||||||
|
)
|
||||||
|
Toast.makeText(
|
||||||
|
activity, "Device registered successfully", Toast.LENGTH_LONG
|
||||||
|
).show()
|
||||||
|
|
||||||
is Result.Error -> {
|
viewModel.addDeviceProvisionDataToDb(
|
||||||
response.exception.let { message ->
|
DeviceData(
|
||||||
Toast.makeText(
|
deviceProvisionResponse = response.toString()
|
||||||
activity,
|
)
|
||||||
"An error occurred in device provision: $message",
|
)
|
||||||
Toast.LENGTH_LONG
|
} else {
|
||||||
).show()
|
Toast.makeText(
|
||||||
}
|
activity,
|
||||||
}
|
"An error in device provision: ${response.data.message}",
|
||||||
|
Toast.LENGTH_LONG
|
||||||
|
).show()
|
||||||
|
binding.btnSubmit.visibility = View.VISIBLE
|
||||||
|
}
|
||||||
|
|
||||||
is Result.Loading -> {
|
}
|
||||||
binding.btnSubmit.visibility = View.GONE
|
|
||||||
}
|
|
||||||
|
|
||||||
else -> {}
|
is Result.Error -> {
|
||||||
|
response.exception.let { message ->
|
||||||
|
Toast.makeText(
|
||||||
|
activity,
|
||||||
|
"An error occurred in device provision: $message",
|
||||||
|
Toast.LENGTH_LONG
|
||||||
|
).show()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
is Result.Loading -> {
|
||||||
|
binding.btnSubmit.visibility = View.GONE
|
||||||
|
}
|
||||||
|
|
||||||
|
else -> {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -164,7 +148,8 @@ class DeviceProvisionFragment : Fragment() {
|
|||||||
val fullReadOutput = StringBuilder()
|
val fullReadOutput = StringBuilder()
|
||||||
|
|
||||||
try {
|
try {
|
||||||
(activity as DeviceProvisionActivity).mService.listenToHemoCube(object : UsbServiceListener {
|
(activity as DeviceProvisionActivity).mService.listenToHemoCube(object :
|
||||||
|
UsbServiceListener {
|
||||||
override fun onUsbRead(data: ByteArray?) {
|
override fun onUsbRead(data: ByteArray?) {
|
||||||
data?.let {
|
data?.let {
|
||||||
val stringData = String(it)
|
val stringData = String(it)
|
||||||
@@ -180,7 +165,6 @@ class DeviceProvisionFragment : Fragment() {
|
|||||||
Firebase.crashlytics.recordException(e)
|
Firebase.crashlytics.recordException(e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun handleUsbData() {
|
private fun handleUsbData() {
|
||||||
when {
|
when {
|
||||||
resultData.contains("SNE") -> {
|
resultData.contains("SNE") -> {
|
||||||
|
|||||||
Reference in New Issue
Block a user