Molbio Data upload
This commit is contained in:
@@ -2,152 +2,38 @@ package com.example.hpostesting.domain
|
|||||||
|
|
||||||
import android.app.job.JobParameters
|
import android.app.job.JobParameters
|
||||||
import android.app.job.JobService
|
import android.app.job.JobService
|
||||||
import android.content.Context
|
|
||||||
import android.content.SharedPreferences
|
import android.content.SharedPreferences
|
||||||
import android.net.ConnectivityManager
|
|
||||||
import android.net.NetworkCapabilities
|
|
||||||
import android.net.NetworkInfo
|
|
||||||
import android.os.Build
|
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
import androidx.annotation.RequiresApi
|
import com.example.hpostesting.data.constant.Constants
|
||||||
import com.example.hpos.api.db.DatabaseHandler
|
import com.example.hpostesting.presentation.testRight.TestRightViewModel
|
||||||
import com.example.hpos.api.db.FileModel
|
import dagger.hilt.android.AndroidEntryPoint
|
||||||
import com.example.hpos.data.DataHolder
|
import javax.inject.Inject
|
||||||
import com.google.gson.JsonObject
|
|
||||||
import okhttp3.MediaType
|
|
||||||
import okhttp3.MultipartBody
|
|
||||||
import okhttp3.RequestBody
|
|
||||||
import retrofit2.Call
|
|
||||||
import retrofit2.Callback
|
|
||||||
import retrofit2.Response
|
|
||||||
import java.io.File
|
|
||||||
|
|
||||||
|
@AndroidEntryPoint
|
||||||
class MyJobScheduler : JobService(){
|
class MyJobScheduler : JobService(){
|
||||||
|
|
||||||
lateinit var sharedPreference: SharedPreferences
|
private lateinit var sharedPreference: SharedPreferences
|
||||||
var dummyService = ApiClient.getClient().create(apiService::class.java)
|
@Inject
|
||||||
var Flag=0
|
private lateinit var viewModel: TestRightViewModel
|
||||||
|
|
||||||
override fun onStartJob(p0: JobParameters?): Boolean {
|
override fun onStartJob(p0: JobParameters?): Boolean {
|
||||||
Log.d("jober","job started")
|
Log.d("jober","job started")
|
||||||
try{
|
viewModel.networkStatusLiveData.observeForever {
|
||||||
if(isOnline(this))
|
if (it) {
|
||||||
{
|
fetchInternalDataAndUpload()
|
||||||
val databaseHandler: DatabaseHandler = DatabaseHandler(this)
|
|
||||||
val normList:List<FileModel> = databaseHandler.getPendingNormFiles()
|
|
||||||
Log.d("jober_norm",normList.size.toString())
|
|
||||||
val testList:List<FileModel> = databaseHandler.getPendingTestFiles()
|
|
||||||
Log.d("jober_test",testList.size.toString())
|
|
||||||
val logList:List<FileModel> = databaseHandler.getPendingLogFiles()
|
|
||||||
Log.d("jober_test",logList.size.toString())
|
|
||||||
|
|
||||||
for(normDetails in normList)
|
|
||||||
{
|
|
||||||
Log.d("jober_norm",normDetails.normFilename)
|
|
||||||
var file = File(normDetails.normFilename)
|
|
||||||
if(file.exists())
|
|
||||||
{
|
|
||||||
uploadFile(applicationContext,normDetails.normFilename,normDetails.PatientId,0)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
for(testDetails in testList)
|
|
||||||
{
|
|
||||||
Log.d("jober_test",testDetails.testFilename)
|
|
||||||
var file = File(testDetails.testFilename)
|
|
||||||
if(file.exists())
|
|
||||||
{
|
|
||||||
uploadFile(this,testDetails.testFilename,testDetails.PatientId,1)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for(logDetails in logList)
|
|
||||||
{
|
|
||||||
Log.d("jober_log",logDetails.logFilename)
|
|
||||||
var file = File(logDetails.logFilename)
|
|
||||||
if(file.exists())
|
|
||||||
{
|
|
||||||
uploadFile(this,logDetails.logFilename,logDetails.PatientId,2)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
Log.d("jober","no net")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (e:Exception)
|
|
||||||
{
|
|
||||||
e.printStackTrace()
|
|
||||||
}
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
fun uploadFile(appContext: Context, filePath: String,pid:String, type:Int) {
|
private fun fetchInternalDataAndUpload() {
|
||||||
val logInfo="log_info"
|
val token = sharedPreference.getString(Constants.TOKEN,"")
|
||||||
|
viewModel.allUserData.observeForever {userList ->
|
||||||
val devicename= DataHolder.deviceSerialNumber
|
userList.forEach {user ->
|
||||||
sharedPreference = appContext.getSharedPreferences(logInfo,Context.MODE_PRIVATE)
|
if (user.uploadFlag == false) {
|
||||||
var path=filePath
|
viewModel.bulkUploadResultToDatabase(user, token)
|
||||||
var file = File(path)
|
|
||||||
if(file.exists())
|
|
||||||
{
|
|
||||||
Log.d("jober","getting in fileexist")
|
|
||||||
val token=sharedPreference.getString("token","")
|
|
||||||
val reqBody = RequestBody.create(MediaType.parse("text/csv"), file)
|
|
||||||
val reqString= RequestBody.create(MediaType.parse("text/plain"),devicename)
|
|
||||||
val formData = MultipartBody.Part.createFormData("raw_data_file", file.name, reqBody)
|
|
||||||
if (token != null) {
|
|
||||||
dummyService.uploadPdf(token,formData,reqString).enqueue(object :
|
|
||||||
Callback<JsonObject> {
|
|
||||||
override fun onResponse(call: Call<JsonObject>, response: Response<JsonObject>) {
|
|
||||||
if (response.isSuccessful) {
|
|
||||||
Log.d("jober","upload success_"+response.body().toString())
|
|
||||||
Flag=1
|
|
||||||
} else {
|
|
||||||
Log.d("jober","upload not success"+response.raw().toString()
|
|
||||||
)
|
|
||||||
Flag=0
|
|
||||||
}
|
|
||||||
updateFile(appContext,pid,type,Flag)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onFailure(call: Call<JsonObject>, t: Throwable) {
|
|
||||||
Log.d("jober","upload error"+t.toString())
|
|
||||||
Flag=0
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
|
||||||
Log.d("filecheck","given file doesn't exist")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun updateFile(appContext: Context,pid:String,type:Int,flag:Int)
|
|
||||||
{
|
|
||||||
val databaseHandler: DatabaseHandler = DatabaseHandler(appContext)
|
|
||||||
var fileData=databaseHandler.getFileFromId(pid.toString())
|
|
||||||
if(fileData!=null)
|
|
||||||
{
|
|
||||||
if(type==0) {
|
|
||||||
fileData.normFlag = flag
|
|
||||||
}
|
|
||||||
else if(type==1)
|
|
||||||
{
|
|
||||||
fileData.testFlag = flag
|
|
||||||
}
|
|
||||||
else if(type==2)
|
|
||||||
{
|
|
||||||
fileData.logFlag=flag
|
|
||||||
}
|
|
||||||
var vn=databaseHandler.updateFile(fileData)
|
|
||||||
Log.d("jober",vn.toString()+"updated as :"+flag+" \n$pid\n$type")
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
Log.d("jober","filedata is null")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -157,22 +43,4 @@ class MyJobScheduler : JobService(){
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fun isOnline(context: Context): Boolean {
|
|
||||||
val cm = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
|
|
||||||
|
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
|
||||||
val n = cm.activeNetwork
|
|
||||||
if (n != null) {
|
|
||||||
val nc = cm.getNetworkCapabilities(n)
|
|
||||||
//It will check for both wifi and cellular network
|
|
||||||
return nc!!.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) || nc.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
} else {
|
|
||||||
val netInfo = cm.activeNetworkInfo
|
|
||||||
return netInfo != null && netInfo.isConnectedOrConnecting
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -72,6 +72,15 @@ class HomeFragment : Fragment() {
|
|||||||
binding.uploadData.setOnClickListener {
|
binding.uploadData.setOnClickListener {
|
||||||
showUploadDialog()
|
showUploadDialog()
|
||||||
}
|
}
|
||||||
|
//
|
||||||
|
// val jobScheduler = getSystemService(Context.JOB_SCHEDULER_SERVICE) as JobScheduler
|
||||||
|
//
|
||||||
|
// val jobInfo = JobInfo.Builder(1, ComponentName(this, MyJobScheduler::class.java))
|
||||||
|
// .setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
|
||||||
|
// .setPersisted(true)
|
||||||
|
// .setPeriodic(30 * 60 * 1000)
|
||||||
|
// .build()
|
||||||
|
// jobScheduler.schedule(jobInfo)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun login()
|
private fun login()
|
||||||
@@ -239,12 +248,13 @@ class HomeFragment : Fragment() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun uploadLocalDBData(dialog: DialogInterface) {
|
private fun uploadLocalDBData(dialog: DialogInterface) {
|
||||||
|
val token = sharedPreference.getString(Constants.TOKEN,"")
|
||||||
viewModel.allUserData.observe(viewLifecycleOwner) { userDataList ->
|
viewModel.allUserData.observe(viewLifecycleOwner) { userDataList ->
|
||||||
if (userDataList.isEmpty()) {
|
if (userDataList.isEmpty()) {
|
||||||
dialog.dismiss()
|
dialog.dismiss()
|
||||||
} else {
|
} else {
|
||||||
userDataList.forEach { userData ->
|
userDataList.forEach { userData ->
|
||||||
viewModel.bulkUploadResultToDatabase(userData)
|
viewModel.bulkUploadResultToDatabase(userData, token!!)
|
||||||
}
|
}
|
||||||
dialog.dismiss()
|
dialog.dismiss()
|
||||||
Toast.makeText(
|
Toast.makeText(
|
||||||
|
|||||||
@@ -51,13 +51,21 @@ class TestRightResults : Fragment() {
|
|||||||
updateResults()
|
updateResults()
|
||||||
|
|
||||||
viewModel.fireBaseUpload.observe(viewLifecycleOwner) {
|
viewModel.fireBaseUpload.observe(viewLifecycleOwner) {
|
||||||
if (it == "Success") {
|
when (it) {
|
||||||
|
"Success" -> {
|
||||||
Toast.makeText(
|
Toast.makeText(
|
||||||
requireContext(), "Test Results Upload Successfully", Toast.LENGTH_SHORT
|
requireContext(), "Test Results Upload Successfully", Toast.LENGTH_SHORT
|
||||||
).show()
|
).show()
|
||||||
}
|
|
||||||
binding.progressBar.visibility = View.GONE
|
binding.progressBar.visibility = View.GONE
|
||||||
}
|
}
|
||||||
|
"Loading" -> {
|
||||||
|
binding.progressBar.visibility = View.VISIBLE
|
||||||
|
}
|
||||||
|
else -> {
|
||||||
|
Toast.makeText(requireContext(), "Test upload failed, please try again", Toast.LENGTH_SHORT).show()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package com.example.hpostesting.presentation.testRight
|
package com.example.hpostesting.presentation.testRight
|
||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.SharedPreferences
|
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
import androidx.lifecycle.LiveData
|
import androidx.lifecycle.LiveData
|
||||||
import androidx.lifecycle.MutableLiveData
|
import androidx.lifecycle.MutableLiveData
|
||||||
@@ -62,9 +61,7 @@ class TestRightViewModel @Inject constructor(
|
|||||||
val loginResponse = MutableLiveData<Result<MolbioLogin>>()
|
val loginResponse = MutableLiveData<Result<MolbioLogin>>()
|
||||||
|
|
||||||
val fireBaseUpload = MutableLiveData<String>()
|
val fireBaseUpload = MutableLiveData<String>()
|
||||||
lateinit var sharedPreference: SharedPreferences
|
|
||||||
val testDetails = DataHolder.selectedTest
|
val testDetails = DataHolder.selectedTest
|
||||||
var Flag=0
|
|
||||||
private val _networkStatusLiveData = NetworkStatusLiveData(context)
|
private val _networkStatusLiveData = NetworkStatusLiveData(context)
|
||||||
val networkStatusLiveData: LiveData<Boolean>
|
val networkStatusLiveData: LiveData<Boolean>
|
||||||
get() = _networkStatusLiveData
|
get() = _networkStatusLiveData
|
||||||
@@ -455,7 +452,6 @@ class TestRightViewModel @Inject constructor(
|
|||||||
is Response.Success -> {
|
is Response.Success -> {
|
||||||
uploadToMolbio(csvFilePath, logTxtFilePath, token)
|
uploadToMolbio(csvFilePath, logTxtFilePath, token)
|
||||||
}
|
}
|
||||||
|
|
||||||
else -> {}
|
else -> {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -485,6 +481,9 @@ class TestRightViewModel @Inject constructor(
|
|||||||
is Result.Success -> {
|
is Result.Success -> {
|
||||||
fireBaseUpload.postValue("Success")
|
fireBaseUpload.postValue("Success")
|
||||||
}
|
}
|
||||||
|
is Result.Error -> {
|
||||||
|
fireBaseUpload.postValue(upload.exception.message)
|
||||||
|
}
|
||||||
|
|
||||||
else -> {
|
else -> {
|
||||||
|
|
||||||
@@ -495,6 +494,7 @@ class TestRightViewModel @Inject constructor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun uploadResultToDatabase(context: Context, isOnline: Boolean, deviceSerialNumber: String, token: String) = viewModelScope.launch {
|
fun uploadResultToDatabase(context: Context, isOnline: Boolean, deviceSerialNumber: String, token: String) = viewModelScope.launch {
|
||||||
|
fireBaseUpload.postValue("Loading")
|
||||||
val csvFilePath = MyUtils.getAppFolderPath(context) + getCSVFileName()
|
val csvFilePath = MyUtils.getAppFolderPath(context) + getCSVFileName()
|
||||||
val logTxtFilePath = MyUtils.getAppFolderPath(context) + getLogFileName()
|
val logTxtFilePath = MyUtils.getAppFolderPath(context) + getLogFileName()
|
||||||
testDetails?.csvPath = csvFilePath
|
testDetails?.csvPath = csvFilePath
|
||||||
@@ -527,11 +527,14 @@ class TestRightViewModel @Inject constructor(
|
|||||||
testDetails?.testTime = SimpleDateFormat(
|
testDetails?.testTime = SimpleDateFormat(
|
||||||
"yyyy-MM-dd HH:mm:ss", Locale.getDefault()
|
"yyyy-MM-dd HH:mm:ss", Locale.getDefault()
|
||||||
).format(Calendar.getInstance().time)
|
).format(Calendar.getInstance().time)
|
||||||
userDao.insertAll(testDetails!!)
|
userDao.insertAll(testDetails!!).let {
|
||||||
|
fireBaseUpload.postValue("Success")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun bulkUploadResultToDatabase(userData: UserData) = viewModelScope.launch {
|
fun bulkUploadResultToDatabase(userData: UserData, token: String?) = viewModelScope.launch {
|
||||||
|
fireBaseUpload.postValue("Loading")
|
||||||
val storageRef = FirebaseStorage.getInstance().reference
|
val storageRef = FirebaseStorage.getInstance().reference
|
||||||
val storageTestDetailsRef = storageRef.child("${testDetails?._id}/")
|
val storageTestDetailsRef = storageRef.child("${testDetails?._id}/")
|
||||||
try {
|
try {
|
||||||
@@ -548,25 +551,31 @@ class TestRightViewModel @Inject constructor(
|
|||||||
val csvUri = csvUploadTask.storage.downloadUrl.await().toString()
|
val csvUri = csvUploadTask.storage.downloadUrl.await().toString()
|
||||||
val logUri = logUploadTask.storage.downloadUrl.await().toString()
|
val logUri = logUploadTask.storage.downloadUrl.await().toString()
|
||||||
|
|
||||||
|
val csvFilePath = userData.csvPath
|
||||||
|
val logTxtFilePath = userData.reportPath
|
||||||
|
|
||||||
userData.csvPath = csvUri
|
userData.csvPath = csvUri
|
||||||
userData.reportPath = logUri
|
userData.reportPath = logUri
|
||||||
|
|
||||||
|
bulkAddResultTestToDb(userData, csvFilePath, logTxtFilePath, token)
|
||||||
bulkAddResultTestToDb(userData)
|
|
||||||
} catch (exception: Exception) {
|
} catch (exception: Exception) {
|
||||||
// Handle the exception appropriately (e.g., log the error, display an error message)
|
// Handle the exception appropriately (e.g., log the error, display an error message)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun bulkAddResultTestToDb(userData: UserData) {
|
private fun bulkAddResultTestToDb(
|
||||||
|
userData: UserData,
|
||||||
|
csvFilePath: String,
|
||||||
|
logTxtFilePath: String,
|
||||||
|
token: String?
|
||||||
|
) {
|
||||||
viewModelScope.launch {
|
viewModelScope.launch {
|
||||||
userData.reportUploadTime = SimpleDateFormat(
|
userData.reportUploadTime = SimpleDateFormat(
|
||||||
"yyyy-MM-dd HH:mm:ss", Locale.getDefault()
|
"yyyy-MM-dd HH:mm:ss", Locale.getDefault()
|
||||||
).format(Calendar.getInstance().time)
|
).format(Calendar.getInstance().time)
|
||||||
when (repository.addTestToDatabase(userData)) {
|
when (repository.addTestToDatabase(userData)) {
|
||||||
is Response.Success -> {
|
is Response.Success -> {
|
||||||
fireBaseUpload.postValue("Success")
|
uploadToMolbio(csvFilePath, logTxtFilePath, token!!)
|
||||||
// repository.uploadPdf()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
else -> {}
|
else -> {}
|
||||||
|
|||||||
Reference in New Issue
Block a user