HemoCube new Changes
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
package com.example.hpostesting.data
|
package com.example.hpostesting.data
|
||||||
|
|
||||||
import androidx.lifecycle.MutableLiveData
|
import androidx.lifecycle.MutableLiveData
|
||||||
|
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.test.TestRightDeviceConstants
|
import com.example.hpostesting.data.model.test.TestRightDeviceConstants
|
||||||
import com.example.hpostesting.data.model.test.TestType
|
import com.example.hpostesting.data.model.test.TestType
|
||||||
@@ -8,12 +9,8 @@ import com.example.hpostesting.data.model.test.TestType
|
|||||||
object DataHolder {
|
object DataHolder {
|
||||||
|
|
||||||
var selectedTestType: TestType = TestType.SICKLECERT
|
var selectedTestType: TestType = TestType.SICKLECERT
|
||||||
|
|
||||||
val usbConnected = MutableLiveData(true)
|
val usbConnected = MutableLiveData(true)
|
||||||
|
|
||||||
var mobileUniqueId: String? = null
|
var mobileUniqueId: String? = null
|
||||||
|
|
||||||
var isStoragePermissionGranted = false
|
|
||||||
var isAppFolderCreated = false
|
var isAppFolderCreated = false
|
||||||
var appFolderPath = ""
|
var appFolderPath = ""
|
||||||
var isReferenceTaken = false
|
var isReferenceTaken = false
|
||||||
@@ -22,16 +19,12 @@ object DataHolder {
|
|||||||
var deviceConstant: TestRightDeviceConstants? = null
|
var deviceConstant: TestRightDeviceConstants? = null
|
||||||
var deviceSerialNumber: String = "ABCD"
|
var deviceSerialNumber: String = "ABCD"
|
||||||
val deviceType = MutableLiveData<String>()
|
val deviceType = MutableLiveData<String>()
|
||||||
|
|
||||||
/* Contains wavelength -> pixel no.*/
|
|
||||||
val wavelengthToPixelArray = ArrayList<Double>()
|
val wavelengthToPixelArray = ArrayList<Double>()
|
||||||
|
|
||||||
/* (For reference/baseline) Contains pixel no. -> Intensity of light falling on that pixel */
|
|
||||||
val intensityReferenceArray = ArrayList<Double>()
|
val intensityReferenceArray = ArrayList<Double>()
|
||||||
|
|
||||||
var selectedTest: UserData? = null
|
var selectedTest: UserData? = null
|
||||||
|
var SelectTestType: HemoCubeTestData? = null
|
||||||
var location: UserData.Location? = null
|
var location: UserData.Location? = null
|
||||||
|
var locationhemocube: HemoCubeTestData.Location? = null
|
||||||
var testExp: Boolean = true
|
var testExp: Boolean = true
|
||||||
|
var hemocubeResult: Double? = null
|
||||||
}
|
}
|
||||||
@@ -9,4 +9,5 @@ import com.example.hpostesting.data.model.patient.UserData
|
|||||||
@TypeConverters(Converters::class)
|
@TypeConverters(Converters::class)
|
||||||
abstract class MyDatabase : RoomDatabase() {
|
abstract class MyDatabase : RoomDatabase() {
|
||||||
abstract fun userDao(): UserDao
|
abstract fun userDao(): UserDao
|
||||||
|
abstract fun hemoCubeDb(): HemoCubeDb
|
||||||
}
|
}
|
||||||
@@ -13,7 +13,7 @@ interface UserDao {
|
|||||||
fun getAll(): LiveData<List<UserData>>
|
fun getAll(): LiveData<List<UserData>>
|
||||||
|
|
||||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||||
suspend fun insertAll(userData: UserData)
|
suspend fun insertAll(userData: com.example.hpostesting.data.model.patient.HemoCubeTestData)
|
||||||
|
|
||||||
@Query("SELECT * FROM user_table WHERE _id = :id")
|
@Query("SELECT * FROM user_table WHERE _id = :id")
|
||||||
suspend fun getUserByID(id: String): UserData
|
suspend fun getUserByID(id: String): UserData
|
||||||
|
|||||||
@@ -45,12 +45,6 @@ data class PatientDetails(
|
|||||||
OTHER
|
OTHER
|
||||||
}
|
}
|
||||||
|
|
||||||
// enum class BloodGroup {
|
|
||||||
// O_POSITIVE, O_NEGATIVE,
|
|
||||||
// A_POSITIVE, A_NEGATIVE,
|
|
||||||
// B_POSITIVE, B_NEGATIVE,
|
|
||||||
// AB_POSITIVE, AB_NEGATIVE,
|
|
||||||
// }
|
|
||||||
|
|
||||||
data class Address(
|
data class Address(
|
||||||
var house: String?,
|
var house: String?,
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ package com.example.hpostesting.data.repository
|
|||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
import com.example.hpostesting.data.model.PendingUploads
|
import com.example.hpostesting.data.model.PendingUploads
|
||||||
import com.example.hpostesting.data.model.Response
|
import com.example.hpostesting.data.model.Response
|
||||||
import com.example.hpostesting.data.model.patient.UserData
|
import com.example.hpostesting.data.model.patient.HemoCubeTestData
|
||||||
import com.google.firebase.firestore.FirebaseFirestore
|
import com.google.firebase.firestore.FirebaseFirestore
|
||||||
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
|
||||||
@@ -16,7 +16,7 @@ class DatabaseRepository : Repository {
|
|||||||
private val db: FirebaseFirestore = Firebase.firestore
|
private val db: FirebaseFirestore = Firebase.firestore
|
||||||
private val storage = Firebase.storage
|
private val storage = Firebase.storage
|
||||||
|
|
||||||
suspend fun addTestToDatabase(data: UserData): Response<String> {
|
suspend fun addTestToDatabase(data: HemoCubeTestData?): Response<String> {
|
||||||
return try {
|
return try {
|
||||||
val userdata = db.collection("patientData").whereEqualTo("_id", data._id).get().await()
|
val userdata = db.collection("patientData").whereEqualTo("_id", data._id).get().await()
|
||||||
if (userdata.documents.isNotEmpty()) {
|
if (userdata.documents.isNotEmpty()) {
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package com.example.hpostesting.domain.di
|
|||||||
import android.app.Application
|
import android.app.Application
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import androidx.room.Room
|
import androidx.room.Room
|
||||||
|
import com.example.hpostesting.data.dao.HemoCubeDb
|
||||||
import com.example.hpostesting.data.dao.MyDatabase
|
import com.example.hpostesting.data.dao.MyDatabase
|
||||||
import com.example.hpostesting.data.dao.UserDao
|
import com.example.hpostesting.data.dao.UserDao
|
||||||
import com.example.hpostesting.data.datasource.LocalFileDataSource
|
import com.example.hpostesting.data.datasource.LocalFileDataSource
|
||||||
@@ -35,6 +36,12 @@ object AppModule {
|
|||||||
return myDatabase.userDao()
|
return myDatabase.userDao()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Provides
|
||||||
|
@Singleton
|
||||||
|
fun provideMyHemo(myDatabase: MyDatabase): HemoCubeDb {
|
||||||
|
return myDatabase.hemoCubeDb()
|
||||||
|
}
|
||||||
|
|
||||||
@Provides
|
@Provides
|
||||||
@Singleton
|
@Singleton
|
||||||
fun provideContext(application: Application): Context {
|
fun provideContext(application: Application): Context {
|
||||||
|
|||||||
@@ -28,7 +28,6 @@ import java.util.Locale
|
|||||||
|
|
||||||
@AndroidEntryPoint
|
@AndroidEntryPoint
|
||||||
class HomeFragment : Fragment() {
|
class HomeFragment : Fragment() {
|
||||||
|
|
||||||
private var _binding: FragmentHomeBinding? = null
|
private var _binding: FragmentHomeBinding? = null
|
||||||
private val binding get() = _binding!!
|
private val binding get() = _binding!!
|
||||||
private val viewModel: TestRightViewModel by activityViewModels()
|
private val viewModel: TestRightViewModel by activityViewModels()
|
||||||
@@ -43,7 +42,6 @@ class HomeFragment : Fragment() {
|
|||||||
|
|
||||||
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)
|
||||||
viewModel.allUserData.observe(viewLifecycleOwner) { userData ->
|
viewModel.allUserData.observe(viewLifecycleOwner) { userData ->
|
||||||
@@ -69,7 +67,6 @@ class HomeFragment : Fragment() {
|
|||||||
showUploadDialog(requireContext())
|
showUploadDialog(requireContext())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun setUserId() {
|
private fun setUserId() {
|
||||||
binding.btnSubmit.setOnClickListener {
|
binding.btnSubmit.setOnClickListener {
|
||||||
val userId = binding.userId.text.toString()
|
val userId = binding.userId.text.toString()
|
||||||
@@ -84,7 +81,6 @@ class HomeFragment : Fragment() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun loadUserData() {
|
private fun loadUserData() {
|
||||||
val query = Firebase.firestore.collection("patientData")
|
val query = Firebase.firestore.collection("patientData")
|
||||||
val recyclerViewOptions =
|
val recyclerViewOptions =
|
||||||
@@ -94,7 +90,6 @@ class HomeFragment : Fragment() {
|
|||||||
binding.rvOrder.adapter = rvAdapter
|
binding.rvOrder.adapter = rvAdapter
|
||||||
rvAdapter.startListening()
|
rvAdapter.startListening()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun saveUserId(userId: String) {
|
private fun saveUserId(userId: String) {
|
||||||
with(sharedPreference.edit()) {
|
with(sharedPreference.edit()) {
|
||||||
putString(Constants.USER_ID, userId)
|
putString(Constants.USER_ID, userId)
|
||||||
@@ -105,15 +100,11 @@ class HomeFragment : Fragment() {
|
|||||||
val builder = AlertDialog.Builder(context)
|
val builder = AlertDialog.Builder(context)
|
||||||
builder.setTitle("Log Out")
|
builder.setTitle("Log Out")
|
||||||
builder.setMessage("Do you want to LogOut the Application?")
|
builder.setMessage("Do you want to LogOut the Application?")
|
||||||
|
|
||||||
// Positive button
|
|
||||||
builder.setPositiveButton("Yes") { dialog, _ ->
|
builder.setPositiveButton("Yes") { dialog, _ ->
|
||||||
saveUserId("")
|
saveUserId("")
|
||||||
findNavController().navigate(R.id.action_nav_home_to_loginFragment)
|
findNavController().navigate(R.id.action_nav_home_to_loginFragment)
|
||||||
dialog.dismiss()
|
dialog.dismiss()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Negative button
|
|
||||||
builder.setNegativeButton("No") { dialog, _ ->
|
builder.setNegativeButton("No") { dialog, _ ->
|
||||||
dialog.dismiss()
|
dialog.dismiss()
|
||||||
}
|
}
|
||||||
@@ -121,9 +112,6 @@ class HomeFragment : Fragment() {
|
|||||||
val dialog = builder.create()
|
val dialog = builder.create()
|
||||||
dialog.show()
|
dialog.show()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private fun getData(search: String?, field: String) {
|
private fun getData(search: String?, field: String) {
|
||||||
val capitalizedSearch = search?.replaceFirstChar {
|
val capitalizedSearch = search?.replaceFirstChar {
|
||||||
if (search.lowercase()
|
if (search.lowercase()
|
||||||
@@ -131,19 +119,15 @@ class HomeFragment : Fragment() {
|
|||||||
) it.titlecase(Locale.getDefault()) else it.toString()
|
) it.titlecase(Locale.getDefault()) else it.toString()
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
val query = Firebase.firestore.collection("patientData").orderBy(field).startAt(search)
|
val query = Firebase.firestore.collection("patientData").orderBy(field).startAt(search)
|
||||||
.endAt(search + "\uf8ff")
|
.endAt(search + "\uf8ff")
|
||||||
|
|
||||||
val recyclerViewOptions =
|
val recyclerViewOptions =
|
||||||
FirestoreRecyclerOptions.Builder<UserData>().setQuery(query, UserData::class.java)
|
FirestoreRecyclerOptions.Builder<UserData>().setQuery(query, UserData::class.java)
|
||||||
.build()
|
.build()
|
||||||
|
|
||||||
rvAdapter = UserListAdapter(recyclerViewOptions, binding.root)
|
rvAdapter = UserListAdapter(recyclerViewOptions, binding.root)
|
||||||
binding.rvOrder.adapter = rvAdapter
|
binding.rvOrder.adapter = rvAdapter
|
||||||
rvAdapter.startListening()
|
rvAdapter.startListening()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun setSearch() {
|
private fun setSearch() {
|
||||||
binding.searchProduct.setOnQueryTextListener(object :
|
binding.searchProduct.setOnQueryTextListener(object :
|
||||||
androidx.appcompat.widget.SearchView.OnQueryTextListener {
|
androidx.appcompat.widget.SearchView.OnQueryTextListener {
|
||||||
@@ -162,7 +146,6 @@ class HomeFragment : Fragment() {
|
|||||||
false
|
false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onQueryTextChange(query: String?): Boolean {
|
override fun onQueryTextChange(query: String?): Boolean {
|
||||||
return if (!query.isNullOrEmpty()) {
|
return if (!query.isNullOrEmpty()) {
|
||||||
val field = when {
|
val field = when {
|
||||||
@@ -180,14 +163,12 @@ class HomeFragment : Fragment() {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun checkForLocalDBData() {
|
private fun checkForLocalDBData() {
|
||||||
viewModel.allUserData.observe(viewLifecycleOwner) { userDataList ->
|
viewModel.allUserData.observe(viewLifecycleOwner) { userDataList ->
|
||||||
val uploadDataVisibility = if (userDataList.isEmpty()) View.GONE else View.VISIBLE
|
val uploadDataVisibility = if (userDataList.isEmpty()) View.GONE else View.VISIBLE
|
||||||
binding.uploadData.visibility = uploadDataVisibility
|
binding.uploadData.visibility = uploadDataVisibility
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun showUploadDialog(context: Context) {
|
private fun showUploadDialog(context: Context) {
|
||||||
val builder = AlertDialog.Builder(context)
|
val builder = AlertDialog.Builder(context)
|
||||||
builder.setTitle(R.string.upload_db_registration_title)
|
builder.setTitle(R.string.upload_db_registration_title)
|
||||||
|
|||||||
@@ -1,59 +1,83 @@
|
|||||||
package com.example.hpostesting.presentation.hemocube
|
package com.example.hpostesting.presentation.hemocube
|
||||||
|
|
||||||
import android.content.Intent
|
import android.content.Context
|
||||||
|
import android.content.SharedPreferences
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
import androidx.fragment.app.Fragment
|
import androidx.fragment.app.Fragment
|
||||||
import android.view.LayoutInflater
|
import android.view.LayoutInflater
|
||||||
import android.view.View
|
import android.view.View
|
||||||
import android.view.ViewGroup
|
import android.view.ViewGroup
|
||||||
|
import android.widget.Toast
|
||||||
import androidx.fragment.app.activityViewModels
|
import androidx.fragment.app.activityViewModels
|
||||||
import com.example.hpostesting.data.DataHolder
|
import com.example.hpostesting.data.DataHolder
|
||||||
import com.example.hpostesting.data.constant.Constants
|
|
||||||
import com.example.hpostesting.data.constant.TestRightCommands
|
|
||||||
import com.example.hpostesting.data.model.ErrorMessage
|
|
||||||
import com.example.hpostesting.presentation.UsbServiceListener
|
import com.example.hpostesting.presentation.UsbServiceListener
|
||||||
import com.example.hpostesting.presentation.testRight.TestRightActivity
|
|
||||||
import `in`.sminnovations.hpostesting.databinding.FragmentHemoCubeReferenceBinding
|
import `in`.sminnovations.hpostesting.databinding.FragmentHemoCubeReferenceBinding
|
||||||
|
|
||||||
|
|
||||||
// TODO: Rename parameter arguments, choose names that match
|
|
||||||
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
|
|
||||||
private const val ARG_PARAM1 = "param1"
|
private const val ARG_PARAM1 = "param1"
|
||||||
private const val ARG_PARAM2 = "param2"
|
private const val ARG_PARAM2 = "param2"
|
||||||
|
|
||||||
/**
|
|
||||||
* A simple [Fragment] subclass.
|
|
||||||
* Use the [HemoCubeFragment.newInstance] factory method to
|
|
||||||
* create an instance of this fragment.
|
|
||||||
*/
|
|
||||||
class HemoCubeFragment : Fragment() {
|
class HemoCubeFragment : Fragment() {
|
||||||
|
private var testTime: String? = ""
|
||||||
|
private var resultRatio: String? = ""
|
||||||
|
private var kitSerial: String? = ""
|
||||||
private lateinit var binding: FragmentHemoCubeReferenceBinding
|
private lateinit var binding: FragmentHemoCubeReferenceBinding
|
||||||
private val viewModel: HemoCubeViewModel by activityViewModels()
|
private val viewModel: HemoCubeViewModel by activityViewModels()
|
||||||
|
private lateinit var sharedPreference: SharedPreferences
|
||||||
private val TAG = "TestRightExpReference"
|
private val TAG = "HemocubeReference"
|
||||||
|
val testDetails = DataHolder.SelectTestType
|
||||||
|
var isOnline = false
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onCreateView(
|
override fun onCreateView(
|
||||||
inflater: LayoutInflater, container: ViewGroup?,
|
inflater: LayoutInflater, container: ViewGroup?,
|
||||||
savedInstanceState: Bundle?
|
savedInstanceState: Bundle?
|
||||||
): View {
|
): View {
|
||||||
// Inflate the layout for this fragment
|
|
||||||
binding = FragmentHemoCubeReferenceBinding.inflate(inflater, container, false)
|
binding = FragmentHemoCubeReferenceBinding.inflate(inflater, container, false)
|
||||||
|
|
||||||
binding.btnShowResult.setOnClickListener {
|
binding.btnShowResult.setOnClickListener {
|
||||||
fetchHemoCubeResult()
|
fetchHemoCubeResult()
|
||||||
}
|
}
|
||||||
|
sharedPreference =
|
||||||
|
requireContext().getSharedPreferences("PREFERENCE_NAME", Context.MODE_PRIVATE)
|
||||||
|
|
||||||
|
viewModel.fireBaseUpload.observe(viewLifecycleOwner) {
|
||||||
|
if (it == "Success") {
|
||||||
|
Toast.makeText(
|
||||||
|
requireContext(), "Test Results Upload Successfully", Toast.LENGTH_SHORT
|
||||||
|
).show()
|
||||||
|
}
|
||||||
|
binding.progressBar.visibility = View.GONE
|
||||||
|
}
|
||||||
|
binding.btnSubmit.setOnClickListener(View.OnClickListener {
|
||||||
|
Log.e("upload","db")
|
||||||
|
if (isOnline) {
|
||||||
|
if (resultRatio != null) {
|
||||||
|
Log.e("uploadData","resultdb")
|
||||||
|
viewModel.uploadHemocubeResultToDatabase(requireContext(), true,true, kitSerial, resultRatio!!,testTime)
|
||||||
|
}
|
||||||
|
binding.progressBar.visibility = View.VISIBLE
|
||||||
|
} else {
|
||||||
|
if (resultRatio != null) {
|
||||||
|
viewModel.uploadHemocubeResultToDatabase(requireContext(), true,true, kitSerial, resultRatio!!,testTime)
|
||||||
|
}
|
||||||
|
Toast.makeText(
|
||||||
|
requireContext(),
|
||||||
|
"Internet not available, Test Data added to Local DB",
|
||||||
|
Toast.LENGTH_SHORT
|
||||||
|
).show()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
viewModel.networkStatusLiveData.observe(viewLifecycleOwner) { isNetworkAvailable ->
|
||||||
|
kitSerial = testDetails?.kitSerial
|
||||||
|
testTime = testDetails?.testTime
|
||||||
|
isOnline = isNetworkAvailable
|
||||||
|
}
|
||||||
|
|
||||||
return binding.root
|
return binding.root
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun fetchHemoCubeResult() {
|
private fun fetchHemoCubeResult() {
|
||||||
viewModel.progressBar.postValue(true)
|
viewModel.progressBar.postValue(true)
|
||||||
val fullReadOutput = StringBuilder()
|
val fullReadOutput = StringBuilder()
|
||||||
|
|
||||||
(activity as HemocubeActivity).mService.eventDrivenWriteHemoCube(
|
(activity as HemocubeActivity).mService.eventDrivenWriteHemoCube(
|
||||||
object : UsbServiceListener {
|
object : UsbServiceListener {
|
||||||
override fun onUsbRead(data: ByteArray?) {
|
override fun onUsbRead(data: ByteArray?) {
|
||||||
@@ -61,41 +85,23 @@ class HemoCubeFragment : Fragment() {
|
|||||||
val stringData = String(it)
|
val stringData = String(it)
|
||||||
fullReadOutput.append(stringData)
|
fullReadOutput.append(stringData)
|
||||||
binding.tvSubtitle4.text = fullReadOutput
|
binding.tvSubtitle4.text = fullReadOutput
|
||||||
|
resultRatio = fullReadOutput.toString()
|
||||||
if (stringData.contains("OK", true)) {
|
Log.d(
|
||||||
Log.d(
|
|
||||||
TAG,
|
TAG,
|
||||||
"onUsbRead() called in sendCmdToFetchDeviceConstant() found OK"
|
"onUsbRead() called in sendCmdToFetchDeviceConstant() found OK"
|
||||||
)
|
)
|
||||||
|
|
||||||
viewModel.progressBar.postValue(false)
|
viewModel.progressBar.postValue(false)
|
||||||
} else if (stringData.contains("FINE", true)) {
|
|
||||||
Log.d(TAG, "results = FINE")
|
|
||||||
viewModel.progressBar.postValue(false)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onUsbError(e: Exception?) {
|
override fun onUsbError(e: Exception?) {
|
||||||
Log.e(TAG, "onUsbIoError() called in sendCmdToFetchDeviceConstant() -> $e")
|
Log.e(TAG, "onUsbIoError() called in sendCmdToFetchDeviceConstant() -> $e")
|
||||||
// viewModel.errorTriggered.postValue(ErrorMessage("Please Try Again this step", Constants.ERROR_NORMAL))
|
|
||||||
|
|
||||||
viewModel.progressBar.postValue(false)
|
viewModel.progressBar.postValue(false)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
/**
|
|
||||||
* Use this factory method to create a new instance of
|
|
||||||
* this fragment using the provided parameters.
|
|
||||||
*
|
|
||||||
* @param param1 Parameter 1.
|
|
||||||
* @param param2 Parameter 2.
|
|
||||||
* @return A new instance of fragment HemoCubeReference.
|
|
||||||
*/
|
|
||||||
// TODO: Rename and change types and number of parameters
|
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun newInstance(param1: String, param2: String) =
|
fun newInstance(param1: String, param2: String) =
|
||||||
HemoCubeFragment().apply {
|
HemoCubeFragment().apply {
|
||||||
|
|||||||
@@ -1,19 +1,72 @@
|
|||||||
package com.example.hpostesting.presentation.hemocube
|
package com.example.hpostesting.presentation.hemocube
|
||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
|
import androidx.lifecycle.LiveData
|
||||||
import androidx.lifecycle.MutableLiveData
|
import androidx.lifecycle.MutableLiveData
|
||||||
import androidx.lifecycle.ViewModel
|
import androidx.lifecycle.ViewModel
|
||||||
|
import androidx.lifecycle.viewModelScope
|
||||||
import com.example.hpostesting.data.DataHolder
|
import com.example.hpostesting.data.DataHolder
|
||||||
import com.example.hpostesting.data.dao.UserDao
|
import com.example.hpostesting.data.NetworkStatusLiveData
|
||||||
|
import com.example.hpostesting.data.dao.HemoCubeDb
|
||||||
|
import com.example.hpostesting.data.model.Response
|
||||||
|
import com.example.hpostesting.data.repository.DatabaseRepository
|
||||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
|
import java.text.SimpleDateFormat
|
||||||
|
import java.util.Calendar
|
||||||
|
import java.util.Locale
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
@HiltViewModel
|
@HiltViewModel
|
||||||
class HemoCubeViewModel@Inject constructor(
|
class HemoCubeViewModel@Inject constructor(
|
||||||
private val userDao: UserDao,
|
private val hemoCubeDb: HemoCubeDb,
|
||||||
|
private val repository: DatabaseRepository,
|
||||||
context: Context
|
context: Context
|
||||||
) : ViewModel() {
|
) : ViewModel() {
|
||||||
var isServiceConnected = false
|
var isServiceConnected = false
|
||||||
val progressBar = MutableLiveData(false)
|
val progressBar = MutableLiveData(false)
|
||||||
val testDetails = DataHolder.selectedTest
|
val testDetails = DataHolder.SelectTestType
|
||||||
|
private val _networkStatusLiveData = NetworkStatusLiveData(context)
|
||||||
|
val networkStatusLiveData: LiveData<Boolean>
|
||||||
|
get() = _networkStatusLiveData
|
||||||
|
val fireBaseUpload = MutableLiveData<String>()
|
||||||
|
fun uploadHemocubeResultToDatabase(
|
||||||
|
context: Context, isOnline: Boolean, testStatus: Boolean, kitSerial: String?,
|
||||||
|
resultRatio: String,
|
||||||
|
testTime: String?) = viewModelScope.launch {
|
||||||
|
|
||||||
|
if (kitSerial != null) {
|
||||||
|
testDetails?.kitSerial = kitSerial
|
||||||
|
}
|
||||||
|
testDetails?.testTime = testTime
|
||||||
|
testDetails?.testStatus = testStatus
|
||||||
|
if (isOnline) {
|
||||||
|
try {
|
||||||
|
addResultTestToDb()
|
||||||
|
} catch (exception: Exception) {
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
testDetails?.testTime = SimpleDateFormat(
|
||||||
|
"yyyy-MM-dd HH:mm:ss", Locale.getDefault()
|
||||||
|
).format(Calendar.getInstance().time)
|
||||||
|
hemoCubeDb.insertAll(testDetails!!)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
private fun addResultTestToDb() {
|
||||||
|
testDetails?.resultRatio = DataHolder.hemocubeResult
|
||||||
|
testDetails?.locationhemo = DataHolder.locationhemocube
|
||||||
|
testDetails?.testTime = SimpleDateFormat(
|
||||||
|
"yyyy-MM-dd HH:mm:ss", Locale.getDefault()
|
||||||
|
).format(Calendar.getInstance().time)
|
||||||
|
viewModelScope.launch {
|
||||||
|
testDetails!!.reportUploadTime = SimpleDateFormat(
|
||||||
|
"yyyy-MM-dd HH:mm:ss", Locale.getDefault()
|
||||||
|
).format(Calendar.getInstance().time)
|
||||||
|
when (repository.addTestToDatabase(testDetails)) {
|
||||||
|
is Response.Success -> {
|
||||||
|
fireBaseUpload.postValue("Success")
|
||||||
|
}
|
||||||
|
else -> {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -64,14 +64,11 @@ class HemocubeActivity : AppCompatActivity() {
|
|||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private val connection = object : ServiceConnection {
|
private val connection = object : ServiceConnection {
|
||||||
override fun onServiceConnected(className: ComponentName, service: IBinder) {
|
override fun onServiceConnected(className: ComponentName, service: IBinder) {
|
||||||
val binder = service as UsbService.UsbServiceBinder
|
val binder = service as UsbService.UsbServiceBinder
|
||||||
mService = binder.getService()
|
mService = binder.getService()
|
||||||
viewModel.isServiceConnected = true
|
viewModel.isServiceConnected = true
|
||||||
|
|
||||||
// Todo: Uncomment this
|
|
||||||
mConnection.let { mService.connect(mDriver, mConnection!!) }
|
mConnection.let { mService.connect(mDriver, mConnection!!) }
|
||||||
moveToNext()
|
moveToNext()
|
||||||
}
|
}
|
||||||
@@ -87,9 +84,7 @@ class HemocubeActivity : AppCompatActivity() {
|
|||||||
setContentView(binding.root)
|
setContentView(binding.root)
|
||||||
setSupportActionBar(binding.myToolbar)
|
setSupportActionBar(binding.myToolbar)
|
||||||
supportActionBar?.setDisplayHomeAsUpEnabled(true)
|
supportActionBar?.setDisplayHomeAsUpEnabled(true)
|
||||||
|
|
||||||
setupListener()
|
setupListener()
|
||||||
|
|
||||||
connectUsb(false)
|
connectUsb(false)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -98,12 +93,9 @@ class HemocubeActivity : AppCompatActivity() {
|
|||||||
Log.d("USB OBSERVE", "HemoCube called -> $it")
|
Log.d("USB OBSERVE", "HemoCube called -> $it")
|
||||||
if (it){
|
if (it){
|
||||||
myMenu?.get(0)?.icon = ContextCompat.getDrawable(this, R.drawable.ic_baseline_usb_24)
|
myMenu?.get(0)?.icon = ContextCompat.getDrawable(this, R.drawable.ic_baseline_usb_24)
|
||||||
// binding.clParent.setAllEnabled(true)
|
|
||||||
} else {
|
} else {
|
||||||
myMenu?.get(0)?.icon = ContextCompat.getDrawable(this, R.drawable.ic_baseline_usb_off_24)
|
myMenu?.get(0)?.icon = ContextCompat.getDrawable(this, R.drawable.ic_baseline_usb_off_24)
|
||||||
// val myToast = Toast.makeText(this, "USB is Detached", Toast.LENGTH_SHORT)
|
|
||||||
Toast.makeText(this, "Device is Disconnected", Toast.LENGTH_SHORT).show()
|
Toast.makeText(this, "Device is Disconnected", Toast.LENGTH_SHORT).show()
|
||||||
// binding.clParent.setAllEnabled(false)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -117,9 +109,6 @@ class HemocubeActivity : AppCompatActivity() {
|
|||||||
onErrorReported("No Device is Connected")
|
onErrorReported("No Device is Connected")
|
||||||
} else {
|
} else {
|
||||||
mDriver = availableDrivers[0]
|
mDriver = availableDrivers[0]
|
||||||
|
|
||||||
// viewModel.testDetails?.deviceId = mDriver.device.serialNumber.toString()
|
|
||||||
// DataHolder.deviceSerialNumber = mDriver.device.serialNumber.toString()
|
|
||||||
mConnection = manager.openDevice(mDriver.device)
|
mConnection = manager.openDevice(mDriver.device)
|
||||||
|
|
||||||
if (mConnection == null) {
|
if (mConnection == null) {
|
||||||
@@ -132,8 +121,6 @@ class HemocubeActivity : AppCompatActivity() {
|
|||||||
|
|
||||||
|
|
||||||
private fun requestUserPermission(manager: UsbManager, device: UsbDevice) {
|
private fun requestUserPermission(manager: UsbManager, device: UsbDevice) {
|
||||||
// Log.d(TAG, "requestUserPermissions() called -> vendor id = ${device.vendorId} & product id = ${device.productId}")
|
|
||||||
|
|
||||||
val mPendingIntent: PendingIntent
|
val mPendingIntent: PendingIntent
|
||||||
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) {
|
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) {
|
||||||
mPendingIntent = PendingIntent.getBroadcast(
|
mPendingIntent = PendingIntent.getBroadcast(
|
||||||
@@ -172,30 +159,14 @@ class HemocubeActivity : AppCompatActivity() {
|
|||||||
|
|
||||||
fun onErrorReported(msg: String) {
|
fun onErrorReported(msg: String) {
|
||||||
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show()
|
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show()
|
||||||
// Snackbar.make(binding.clParent, msg, Snackbar.LENGTH_LONG)
|
|
||||||
// .setAction("CLOSE") { Toast.makeText(this, "Will soon", Toast.LENGTH_SHORT).show() }
|
|
||||||
// .setActionTextColor(resources.getColor(R.color.white))
|
|
||||||
// .show()
|
|
||||||
|
|
||||||
if (!isFinishing)
|
if (!isFinishing)
|
||||||
onBackPressed()
|
onBackPressed()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
|
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
|
||||||
menuInflater.inflate(R.menu.my_menu, menu)
|
menuInflater.inflate(R.menu.my_menu, menu)
|
||||||
// myMenu = menu
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// override fun onOptionsItemSelected(item: MenuItem) = when (item.itemId) {
|
|
||||||
// R.id.action_usb -> {
|
|
||||||
//// Toast.makeText(this, "USB Connected", Toast.LENGTH_SHORT).show()
|
|
||||||
//// myMenu?.get(0)?.icon = ContextCompat.getDrawable(this, R.drawable.ic_baseline_usb_off_24)
|
|
||||||
// true
|
|
||||||
// }
|
|
||||||
// else -> {super.onOptionsItemSelected(item)}
|
|
||||||
// }
|
|
||||||
|
|
||||||
override fun onDestroy() {
|
override fun onDestroy() {
|
||||||
super.onDestroy()
|
super.onDestroy()
|
||||||
if (viewModel.isServiceConnected) {
|
if (viewModel.isServiceConnected) {
|
||||||
|
|||||||
@@ -13,18 +13,13 @@ import com.hoho.android.usbserial.driver.UsbSerialDriver
|
|||||||
import com.hoho.android.usbserial.driver.UsbSerialPort
|
import com.hoho.android.usbserial.driver.UsbSerialPort
|
||||||
import com.hoho.android.usbserial.util.SerialInputOutputManager
|
import com.hoho.android.usbserial.util.SerialInputOutputManager
|
||||||
import java.io.IOException
|
import java.io.IOException
|
||||||
|
|
||||||
|
|
||||||
class UsbService : Service() {
|
class UsbService : Service() {
|
||||||
|
|
||||||
// Binder to be given to clients
|
|
||||||
private val binder = UsbServiceBinder()
|
private val binder = UsbServiceBinder()
|
||||||
private lateinit var mPort: UsbSerialPort
|
private lateinit var mPort: UsbSerialPort
|
||||||
private var isUsbConnected = false
|
private var isUsbConnected = false
|
||||||
private val TAG = "UsbService"
|
private val TAG = "UsbService"
|
||||||
|
|
||||||
inner class UsbServiceBinder : Binder() {
|
inner class UsbServiceBinder : Binder() {
|
||||||
// Return this instance of UsbService so clients can call public methods
|
|
||||||
fun getService(): UsbService = this@UsbService
|
fun getService(): UsbService = this@UsbService
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -34,19 +29,15 @@ class UsbService : Service() {
|
|||||||
|
|
||||||
var listener: UsbServiceListener? = null
|
var listener: UsbServiceListener? = null
|
||||||
fun connect(driver: UsbSerialDriver, connection: UsbDeviceConnection) {
|
fun connect(driver: UsbSerialDriver, connection: UsbDeviceConnection) {
|
||||||
// Todo: Uncomment this
|
|
||||||
mPort = driver.ports[0] // Most devices have just one port (port 0)
|
mPort = driver.ports[0] // Most devices have just one port (port 0)
|
||||||
mPort.open(connection)
|
mPort.open(connection)
|
||||||
mPort.setParameters(115200, 8, UsbSerialPort.STOPBITS_1, UsbSerialPort.PARITY_NONE)
|
mPort.setParameters(115200, 8, UsbSerialPort.STOPBITS_1, UsbSerialPort.PARITY_NONE)
|
||||||
// mPort.setParameters(9600, 8, UsbSerialPort.STOPBITS_1, UsbSerialPort.PARITY_NONE)
|
|
||||||
//
|
|
||||||
isUsbConnected = true
|
isUsbConnected = true
|
||||||
Log.d(TAG, "My Usb Connected ${mPort.driver}")
|
Log.d(TAG, "My Usb Connected ${mPort.driver}")
|
||||||
|
|
||||||
val usbIoManager = SerialInputOutputManager(mPort,
|
val usbIoManager = SerialInputOutputManager(mPort,
|
||||||
object : SerialInputOutputManager.Listener{
|
object : SerialInputOutputManager.Listener{
|
||||||
override fun onNewData(data: ByteArray?) {
|
override fun onNewData(data: ByteArray?) {
|
||||||
// Log.e(TAG, "onNewData() called inside eventDrivenWrite()")
|
|
||||||
listener?.onUsbRead(data)
|
listener?.onUsbRead(data)
|
||||||
}
|
}
|
||||||
override fun onRunError(e: Exception?) {
|
override fun onRunError(e: Exception?) {
|
||||||
@@ -57,7 +48,6 @@ class UsbService : Service() {
|
|||||||
})
|
})
|
||||||
usbIoManager.start();
|
usbIoManager.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
fun disconnect() {
|
fun disconnect() {
|
||||||
if(isUsbConnected){
|
if(isUsbConnected){
|
||||||
mPort.close()
|
mPort.close()
|
||||||
@@ -65,45 +55,6 @@ class UsbService : Service() {
|
|||||||
Log.d(TAG, "My Usb disconnected:: ${mPort.driver}")
|
Log.d(TAG, "My Usb disconnected:: ${mPort.driver}")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Todo: comment this
|
|
||||||
// fun eventDrivenWrite(command: TestRightCommands, listener: UsbServiceListener) {
|
|
||||||
// this.listener = listener
|
|
||||||
// try {
|
|
||||||
// when (command) {
|
|
||||||
// TestRightCommands.autoset -> {
|
|
||||||
// this.listener!!.onUsbRead("OK".toByteArray())
|
|
||||||
// }
|
|
||||||
// TestRightCommands.led2Set50 -> {
|
|
||||||
// this.listener!!.onUsbRead("OK".toByteArray())
|
|
||||||
// }
|
|
||||||
// TestRightCommands.run -> {
|
|
||||||
// this.listener!!.onUsbRead("OK [0]".toByteArray())
|
|
||||||
// }
|
|
||||||
// TestRightCommands.read -> {
|
|
||||||
// this.listener!!.onUsbRead(InputData().inputRead.toByteArray())
|
|
||||||
// }
|
|
||||||
// TestRightCommands.printAll -> {
|
|
||||||
// if (DataHolder.testExp) {
|
|
||||||
// for (each in InputData().printForReference.lines()) {
|
|
||||||
// this.listener!!.onUsbRead((each + "\n").toByteArray())
|
|
||||||
// }
|
|
||||||
// } else {
|
|
||||||
// for (each in InputData().printForSample.lines()) {
|
|
||||||
// this.listener!!.onUsbRead((each + "\n").toByteArray())
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// else -> {
|
|
||||||
// this.listener!!.onUsbRead("OK".toByteArray())
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// } catch (e: IOException) {
|
|
||||||
// listener.onUsbError(e)
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// Todo: Uncomment this
|
|
||||||
fun eventDrivenWrite(command: TestRightCommands, listener: UsbServiceListener) {
|
fun eventDrivenWrite(command: TestRightCommands, listener: UsbServiceListener) {
|
||||||
this.listener = listener
|
this.listener = listener
|
||||||
try {
|
try {
|
||||||
@@ -121,20 +72,4 @@ class UsbService : Service() {
|
|||||||
listener.onUsbError(e)
|
listener.onUsbError(e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// fun write(command: TestRightCommands){
|
|
||||||
// mPort.write(command.command.toByteArray(), Constants.WRITE_TIMEOUT_MILLIS)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// fun read(){
|
|
||||||
// val byteArray = ByteArray(3700)
|
|
||||||
// val len = mPort.read(byteArray, Constants.READ_TIMEOUT_MILLIS)
|
|
||||||
//
|
|
||||||
// Log.d(TAG, "length is $len")
|
|
||||||
// val byteArrayCropped = ByteArray(len)
|
|
||||||
// System.arraycopy(byteArray, 0, byteArrayCropped, 0, len)
|
|
||||||
//
|
|
||||||
// listener?.onUsbRead(byteArrayCropped)
|
|
||||||
// }
|
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user