Configured checking if folder is created from activity to view model while storing of files(csv, Logs). Issue resolved -> error reporting when app is opened directly to main activity from USB, resulting in not creation of app folder.

This commit is contained in:
vsuryakumar
2023-02-02 18:51:38 +05:30
parent 01676f9909
commit dc47352042
7 changed files with 123 additions and 177 deletions

View File

@@ -1,12 +1,16 @@
package com.example.hpos.domain
import android.util.Log
import com.example.hpos.data.model.CalculationVariableForTest
import com.opencsv.CSVWriter
import java.io.File
import java.io.FileOutputStream
import java.io.FileWriter
class SaveRawDataTest {
private val TAG = "SaveRawDataTest"
fun saveCsv(folderPath: String, fileName: String, calculationData: ArrayList<CalculationVariableForTest>) {
val fullPath = "$folderPath/$fileName"
@@ -28,6 +32,8 @@ class SaveRawDataTest {
fun saveLog(folderPath: String, fileName: String, isReference: Boolean, fullString: String) {
val fileObj = File(folderPath, fileName)
// Log.d(TAG, "$folderPath --- $fileName")
val writer = FileWriter(fileObj)
if (isReference)

View File

@@ -108,7 +108,7 @@ class SplashActivity : AppCompatActivity() {
}
private fun createAppFolder() {
val folderPath = MyUtils().createAppFolder(applicationContext)
val folderPath = MyUtils.createAppFolder(applicationContext)
if (folderPath != null){
DataHolder.isAppFolderCreated = true
DataHolder.appFolderPath = folderPath

View File

@@ -172,7 +172,7 @@ class TestRightExpReference : Fragment() {
}
override fun onUsbError(e: Exception?) {
Log.e(TAG, "onUsbIoError() called in writeUsbFetchDeviceConstant() -> $e")
Log.e(TAG, "onUsbIoError() called in sendCmdToSetLed() -> $e")
viewModel.progressBar.postValue(false)
}
})
@@ -255,11 +255,15 @@ class TestRightExpReference : Fragment() {
data?.let {
val stringData = String(it)
fullReadOutput.append(stringData)
// Log.d(TAG, stringData)
// Log.d(TAG, stringData)
if (stringData.contains("OK", true)) {
Log.d(TAG, "onUsbRead() called in sendCmdToFetchLightIntensities()")
// Log.d(TAG, fullReadOutput.toString())
viewModel.mapIntensityValues(fullReadOutput.toString(), true)
viewModel.saveLogTest(requireContext().applicationContext, true, fullReadOutput.toString())
DataHolder.isReferenceTaken = true
Handler(Looper.getMainLooper()).postDelayed(
@@ -286,88 +290,4 @@ class TestRightExpReference : Fragment() {
.commit()
}
// fun onErrorReported(msg: String) {
// Toast.makeText(requireContext(), msg, Toast.LENGTH_SHORT).show()
// }
// private fun testWriteConnection() {
// Log.d(TAG, "testWriteConnection() called")
// viewModel.progressBar.postValue(true)
//
// val fullReadOutput = StringBuilder()
// (activity as TestRightActivity).mService.eventDrivenWrite(TestRightCommands.testByEnter,
// object : UsbServiceListener {
// override fun onUsbRead(data: ByteArray?) {
// data?.let {
// val stringData = String(it)
// fullReadOutput.append(stringData)
// Log.d(TAG, "fullReadOutput = $fullReadOutput")
//
// Handler(Looper.getMainLooper()).postDelayed(
// Runnable {
// sendCmdToFetchDeviceConstant()
// },
// Constants.DELAY_BETWEEN_COMMANDS
// )
// }
// }
//
// override fun onUsbError(e: Exception?) {
// Log.e(TAG, "onUsbIoError() called in writeUsbFetchDeviceConstant() -> $e")
// viewModel.progressBar.postValue(false)
// }
// })
// }
// private fun testing(commands: TestRightCommands) {
// viewModel.progressBar.postValue(true)
// val fullReadOutput = StringBuilder()
// (activity as TestRightActivity).mService.eventDrivenWrite(TestRightCommands.printInRange,
// object : UsbServiceListener {
// override fun onUsbRead(data: ByteArray?) {
// data?.let {
// val stringData = String(it)
// fullReadOutput.append(stringData)
// Log.d(TAG, fullReadOutput.toString())
//
//
// if (stringData.contains("OK", true)) {
// Log.d(TAG, "onUsbRead() called in sendCmdToFetchLightIntensities()")
// viewModel.mapIntensityValues(fullReadOutput.toString(), true)
// DataHolder.isReferenceTaken = true
// viewModel.progressBar.postValue(false)
// moveToSamplePage()
// }
// }
// }
//
// override fun onUsbError(e: Exception?) {
// Log.e(TAG, "onUsbIoError() called in writeUsbFetchDeviceConstant() -> $e")
// viewModel.progressBar.postValue(false)
// }
// })
// }
// private fun startReadingReference() {
// val mService = (activity as TestRightActivity).mService
// mService.eventDrivenWrite(TestRightCommands.print_all)
// }
// private fun startTakingReference() {
// val mService = (activity as TestRightActivity).mService
// mService.write(TestRightCommands.print)
//
// val handler = Handler()
// handler.postDelayed(Runnable { // Do something after 5s = 5000ms
// mService.read()
// }, Constants.DELAY_BETWEEN_COMMANDS)
//
// }
// fun View.setAllEnabled(enabled: Boolean) {
// isEnabled = enabled
// if (this is ViewGroup) children.forEach { child -> child.setAllEnabled(enabled) }
// }
}

View File

@@ -171,7 +171,8 @@ class TestRightExpSample : Fragment() {
Log.d(TAG, "onUsbRead() called in sendCmdToFetchLightIntensities()")
viewModel.mapIntensityValues(fullReadOutput.toString(), false)
// viewModel.progressBar.postValue(false)
viewModel.saveLogTest(requireContext().applicationContext, false, fullReadOutput.toString())
showResultsAfterAcquiring()
}
}
@@ -199,14 +200,4 @@ class TestRightExpSample : Fragment() {
// if (this is ViewGroup) children.forEach { child -> child.setAllEnabled(enabled) }
// }
// private fun Button.disable() {
// backgroundTintList = ContextCompat.getColorStateList(requireActivity(), R.color.gray)
// isClickable = false
// }
//
// fun Button.enable() {
// backgroundTintList = ContextCompat.getColorStateList(requireActivity(), R.color.purple_500)
//// background.setColorFilter(Color.BLUE, PorterDuff.Mode.MULTIPLY)
// isClickable = true
// }
}

View File

@@ -67,44 +67,51 @@ class TestRightResults : Fragment() {
val sdfDate = SimpleDateFormat("ddMMyyyy HHmmss")
val fileName: String = "log_" + sdfDate.format(Date()) + ".txt"
if (!DataHolder.isAppFolderCreated) {
val folderPath = MyUtils().createAppFolder(requireContext().applicationContext)
if (folderPath != null) {
DataHolder.isAppFolderCreated = true
DataHolder.appFolderPath = folderPath
viewModel.saveLog(DataHolder.appFolderPath, fileName)
}
} else {
viewModel.saveLog(DataHolder.appFolderPath, fileName)
}
viewModel.saveLog(requireContext().applicationContext, fileName)
// if (!DataHolder.isAppFolderCreated) {
// val folderPath = MyUtils.createAppFolder(requireContext().applicationContext)
// if (folderPath != null) {
// DataHolder.isAppFolderCreated = true
// DataHolder.appFolderPath = folderPath
// viewModel.saveLog(DataHolder.appFolderPath, fileName)
// }
// } else {
// viewModel.saveLog(DataHolder.appFolderPath, fileName)
// }
}
// private fun saveCsv() {
// Log.d(TAG, "saveCsv() called")
//
// val sdfDate = SimpleDateFormat("ddMMyyyy HHmmss")
// val fileName: String = sdfDate.format(Date()) + ".csv"
//
// if (!DataHolder.isAppFolderCreated) {
// val folderPath = MyUtils.createAppFolder(requireContext().applicationContext)
// if (folderPath != null) {
// DataHolder.isAppFolderCreated = true
// DataHolder.appFolderPath = folderPath
// viewModel.saveCsv(DataHolder.appFolderPath, fileName)
//
// viewModel.saveCsvForTesting(DataHolder.appFolderPath, "testing$fileName")
// }
// } else {
// viewModel.saveCsv(DataHolder.appFolderPath, fileName)
//
// viewModel.saveCsvForTesting(DataHolder.appFolderPath, "testing$fileName")
// }
//
// }
private fun saveCsv() {
Log.d(TAG, "saveCsv() called")
// viewModel.mapWavelengthToAbsorbance()
// val fileName = "surya.csv"
val sdfDate = SimpleDateFormat("ddMMyyyy HHmmss")
val fileName: String = sdfDate.format(Date()) + ".csv"
if (!DataHolder.isAppFolderCreated) {
val folderPath = MyUtils().createAppFolder(requireContext().applicationContext)
if (folderPath != null) {
DataHolder.isAppFolderCreated = true
DataHolder.appFolderPath = folderPath
viewModel.saveCsv(DataHolder.appFolderPath, fileName)
// Todo: Remove
// viewModel.saveCsvForTesting(DataHolder.appFolderPath, "testing$fileName")
}
} else {
viewModel.saveCsv(DataHolder.appFolderPath, fileName)
// Todo: Remove
// viewModel.saveCsvForTesting(DataHolder.appFolderPath, "testing$fileName")
}
viewModel.saveCsv(requireContext().applicationContext, fileName)
viewModel.saveCsvForTesting(requireContext().applicationContext, "testing$fileName")
}
private fun updateResults() {

View File

@@ -1,10 +1,11 @@
package com.example.hpos.presentation.testRight
import android.content.Context
import android.util.Log
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.example.hpos.data.constant.Constants
import com.example.hpos.data.DataHolder
import com.example.hpos.data.constant.Constants
import com.example.hpos.data.model.CalculationVariableForTest
import com.example.hpos.data.model.PatientData
import com.example.hpos.data.model.TestRightCalculationData
@@ -13,9 +14,10 @@ import com.example.hpos.domain.ResultCalculationWithMaxImpl
import com.example.hpos.domain.SaveRawData
import com.example.hpos.domain.SaveRawDataTest
import com.example.hpos.domain.TestRightResultCalculation
import java.lang.Exception
import com.example.hpos.util.MyUtils
import java.text.SimpleDateFormat
import java.util.*
import java.util.Collections.sort
import kotlin.collections.ArrayList
import kotlin.math.log10
import kotlin.math.pow
@@ -96,23 +98,6 @@ class TestRightViewModel : ViewModel() {
if (isReference) DataHolder.intensityReferenceArray.clear()
else intensitySampleArray.clear()
// Todo: Remove
// var fileName: String =
// "test_log_" + SimpleDateFormat("ddMMyyyy HHmmss").format(Date()) + ".txt"
// fileName = if (isReference) {
// "reference_$fileName"
// } else {
// "sample_$fileName"
// }
// saveLogTest(DataHolder.appFolderPath, fileName, isReference, fullString)
// Log.d(TAG, "inside mapIntensityValues() -> isReference = $isReference")
// Log.d(TAG, "listOfString size = ${listOfString.size}")
// if(!isReference){
// Log.d(TAG, "fullString = $fullString")
// }
Log.d(TAG, fullString)
val listOfString = fullString.split("\n")
@@ -172,6 +157,7 @@ class TestRightViewModel : ViewModel() {
}
wavelengthToAbsorbance.clear()
calculationVariableList.clear()
// Index pointing to last of the array
// var indexInIntensityArrays = intensityReferenceArray.size - 1
@@ -207,7 +193,6 @@ class TestRightViewModel : ViewModel() {
temp.add(absorbance)
wavelengthToAbsorbance.add(temp)
// Todo: Remove
val calculationVariableForTest = CalculationVariableForTest()
calculationVariableForTest.pixelNo = index + 1
@@ -233,25 +218,77 @@ class TestRightViewModel : ViewModel() {
}
fun saveCsv(folderPath: String, filename: String) {
SaveRawData().saveCsv(folderPath, filename, wavelengthToAbsorbance)
fun saveCsv(appContext: Context, filename: String) {
var folderPath: String? = DataHolder.appFolderPath
if (DataHolder.isAppFolderCreated) {
SaveRawData().saveCsv(folderPath!!, filename, wavelengthToAbsorbance)
} else {
folderPath = MyUtils.createAppFolder(appContext)
if (folderPath != null){
DataHolder.isAppFolderCreated = true
DataHolder.appFolderPath = folderPath
SaveRawData().saveCsv(folderPath, filename, wavelengthToAbsorbance)
} else {
// errorTriggered.postValue("Unable to save CSV, Please try again")
}
}
}
fun saveCsvForTesting(folderPath: String, filename: String) {
SaveRawDataTest().saveCsv(folderPath, filename, calculationVariableList)
fun saveCsvForTesting(appContext: Context, filename: String) {
var folderPath: String? = DataHolder.appFolderPath
if (DataHolder.isAppFolderCreated) {
SaveRawDataTest().saveCsv(folderPath!!, filename, calculationVariableList)
} else {
folderPath = MyUtils.createAppFolder(appContext)
if (folderPath != null){
DataHolder.isAppFolderCreated = true
DataHolder.appFolderPath = folderPath
SaveRawDataTest().saveCsv(folderPath, filename, calculationVariableList)
} else {
// errorTriggered.postValue("Unable to save CSV, Please try again")
}
}
}
fun saveLog(appFolderPath: String, fileName: String) {
SaveRawData().saveLog(appFolderPath, fileName, calculationData)
fun saveLog(appContext: Context, fileName: String) {
var folderPath: String? = DataHolder.appFolderPath
if (DataHolder.isAppFolderCreated) {
SaveRawData().saveLog(folderPath!!, fileName, calculationData)
} else {
folderPath = MyUtils.createAppFolder(appContext)
if (folderPath != null){
DataHolder.isAppFolderCreated = true
DataHolder.appFolderPath = folderPath
SaveRawData().saveLog(folderPath, fileName, calculationData)
} else {
// errorTriggered.postValue("Unable to save Log, Please try again")
}
}
}
fun saveLogTest(
appFolderPath: String,
fileName: String,
isReference: Boolean,
fullString: String
) {
SaveRawDataTest().saveLog(appFolderPath, fileName, isReference, fullString)
fun saveLogTest(appContext: Context, isReference: Boolean, fullString: String) {
var fileName: String =
"test_log_" + SimpleDateFormat("ddMMyyyy_HHmmss").format(Date()) + ".txt"
fileName = if (isReference) {
"reference_$fileName"
} else {
"sample_$fileName"
}
var folderPath: String? = DataHolder.appFolderPath
if (DataHolder.isAppFolderCreated) {
SaveRawDataTest().saveLog(folderPath!!, fileName, isReference, fullString)
} else {
folderPath = MyUtils.createAppFolder(appContext)
if (folderPath != null){
DataHolder.isAppFolderCreated = true
DataHolder.appFolderPath = folderPath
SaveRawDataTest().saveLog(folderPath, fileName, isReference, fullString)
} else {
// errorTriggered.postValue("Unable to save Log, Please try again")
}
}
}
}

View File

@@ -5,34 +5,19 @@ import android.os.Environment
import com.example.hpos.R
import java.io.File
class MyUtils {
// fun getDeviceTypeFromString(deviceName: String) : DeviceType {
// return if (deviceName == "TestRight"){
// DeviceType.TestRight
// } else {
// DeviceType.Denovix
// }
// }
object MyUtils {
fun createAppFolder(context: Context) : String? {
val folderPath = Environment.getExternalStorageDirectory().absolutePath + "/" + context.resources.getString(R.string.app_name) + "/"
// DataHolder.appFolderPath = folderPath
val file = File(folderPath)
return if (!file.exists()) {
if (!file.mkdirs()) {
// DataHolder.isAppFolderCreated = false
// false
null
} else {
// DataHolder.isAppFolderCreated = true
// true
folderPath
}
} else {
// DataHolder.isAppFolderCreated = true
// true
folderPath
}
}