This commit is contained in:
smileomi18@gmail.com
2023-07-19 16:28:31 +05:30
parent ab9392cd59
commit 8151d6da61
5 changed files with 94 additions and 12 deletions

View File

@@ -21,6 +21,7 @@ object DataHolder {
var deviceConstant: TestRightDeviceConstants? = null
var deviceSerialNumber: String = "ABCD"
val deviceType = MutableLiveData<String>()
/* Contains wavelength -> pixel no.*/
val wavelengthToPixelArray = ArrayList<Double>()

View File

@@ -22,7 +22,11 @@ object Constants {
const val MAX_WAVELENGTH_RANGE_TO_RECORD = 750
const val DEVICE_PRODUCT_ID = 24597
const val DEVICE_TYPE_HOMOCUBE = "HOMOCUBE"
const val DEVICE_VENDOR_ID = 1027
const val HOMO_CUBE_ID = 29987
const val VENDOR_ID = 6790
const val ERROR_NORMAL = 400
const val ERROR_CRITICAL = 401

View File

@@ -11,6 +11,7 @@ import android.location.Location
import android.os.Bundle
import android.util.Log
import android.view.Menu
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
@@ -75,12 +76,38 @@ class MainActivity : AppCompatActivity() {
addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED)
}
registerReceiver(mUsbReceiver, filter)
// DataHolder.deviceType.observe(this) { deviceType ->
// when (deviceType) {
// Constants.DEVICE_TYPE_HOMOCUBE -> {
// binding.cvItem1.visibility = View.VISIBLE
// binding.cvItem3.visibility = View.VISIBLE
// binding.cvItem2.visibility = View.GONE
// binding.cvItem4.visibility = View.GONE
// Toast.makeText(this, "HomoCube Connected", Toast.LENGTH_SHORT).show()
// DataHolder.usbConnected.value = true
// DataHolder.deviceType.postValue(Constants.DEVICE_TYPE_HOMOCUBE)
// }
// "Test" -> {
//
// }
// }
// }
}
private fun checkAndUpdateUsbConnection() {
val availableDrivers = UsbSerialProber.getDefaultProber()
.findAllDrivers(getSystemService(Context.USB_SERVICE) as UsbManager)
if (availableDrivers.isNotEmpty() && availableDrivers[0].device.productId == Constants.DEVICE_PRODUCT_ID && availableDrivers[0].device.vendorId == Constants.DEVICE_VENDOR_ID) {
// Log.d("mariya", availableDrivers[0].device.toString())
if (availableDrivers.isNotEmpty() && availableDrivers[0].device.productId == Constants.HOMO_CUBE_ID && availableDrivers[0].device.vendorId == Constants.VENDOR_ID) {
Log.e("HomoCube", "product1")
binding.cvItem1.visibility = View.VISIBLE
binding.cvItem3.visibility = View.VISIBLE
binding.cvItem2.visibility = View.GONE
binding.cvItem4.visibility = View.GONE
Toast.makeText(this, "HomoCube Connected", Toast.LENGTH_SHORT).show()
DataHolder.usbConnected.value = true
DataHolder.deviceType.postValue(Constants.DEVICE_TYPE_HOMOCUBE)
}else if (availableDrivers.isNotEmpty() && availableDrivers[0].device.productId == Constants.DEVICE_PRODUCT_ID && availableDrivers[0].device.vendorId == Constants.DEVICE_VENDOR_ID) {
Log.d(TAG, "Device matched -> product = ${availableDrivers[0].device.productId}, vendor = ${availableDrivers[0].device.vendorId}")
DataHolder.usbConnected.value = true
} else if (availableDrivers.isNotEmpty()) {
@@ -89,6 +116,7 @@ class MainActivity : AppCompatActivity() {
DataHolder.usbConnected.value = false
} else {
Log.d(TAG, "Device NOT Connected")
Toast.makeText(this, "Device Not Connected", Toast.LENGTH_SHORT).show()
DataHolder.usbConnected.value = false
}
}

View File

@@ -124,11 +124,12 @@ class TestRightActivity : AppCompatActivity() {
} else {
mDriver = availableDrivers[0]
if (mDriver.device.productId != Constants.DEVICE_PRODUCT_ID || mDriver.device.vendorId != Constants.DEVICE_VENDOR_ID){
Toast.makeText(this, "Device Connected is not supported", Toast.LENGTH_SHORT).show()
onBackPressed()
return
}
// if (mDriver.device.productId != Constants.DEVICE_PRODUCT_ID || mDriver.device.vendorId != Constants.DEVICE_VENDOR_ID
// || (mDriver.device.productId != Constants.HOMO_CUBE_ID || mDriver.device.vendorId != Constants.VENDOR_ID)){
// Toast.makeText(this, "Device Connected is not supported", Toast.LENGTH_SHORT).show()
// onBackPressed()
// return
// }
viewModel.testDetails?.deviceId = mDriver.device.serialNumber.toString()
DataHolder.deviceSerialNumber = mDriver.device.serialNumber.toString()

View File

@@ -48,13 +48,18 @@ class TestRightExpReference : Fragment() {
//
// DataHolder.sampleReadCounter <= Constants.MAXIMUM_TEST_ALLOWED_BEFORE_REFERENCE
if (DataHolder.deviceConstant == null) {
viewModel.progressBar.postValue(true)
Handler(Looper.getMainLooper()).postDelayed(
{ sendCmdToFetchDeviceConstant() },
Constants.DELAY_BETWEEN_COMMANDS
)
Log.e("mariya", "onViewCreated 1")
if (DataHolder.deviceType.equals(Constants.DEVICE_TYPE_HOMOCUBE) ) {
Log.e("mariya", "onViewCreated if")
fetchHemoCubeResult()
}
// if (DataHolder.deviceConstant == null) {
// viewModel.progressBar.postValue(true)
// Handler(Looper.getMainLooper()).postDelayed(
// { sendCmdToFetchDeviceConstant() },
// Constants.DELAY_BETWEEN_COMMANDS
// )
// }
}
private fun setupListeners() {
@@ -112,6 +117,49 @@ class TestRightExpReference : Fragment() {
// }
}
// private fun sendCmdToFetchDeviceConstant(recursive: Boolean) {
private fun fetchHemoCubeResult() {
Log.d("mariya", "fetchHemoCubeResult() called")
viewModel.progressBar.postValue(true)
val fullReadOutput = StringBuilder()
(activity as TestRightActivity).mService.eventDrivenWrite(TestRightCommands.read,
object : UsbServiceListener {
override fun onUsbRead(data: ByteArray?) {
Log.d("mariya", data.toString())
data?.let {
val stringData = String(it)
fullReadOutput.append(stringData)
Log.d(TAG, "fullReadOutput = $fullReadOutput")
if (stringData.contains("OK", true)) {
Log.d(
TAG,
"onUsbRead() called in sendCmdToFetchDeviceConstant() found OK"
)
viewModel.mapDeviceConstants(fullReadOutput.toString())
viewModel.progressBar.postValue(false)
} else if (stringData.contains("FINE", true)) {
Log.d(TAG, "results = FINE")
viewModel.progressBar.postValue(false)
viewModel.errorTriggered.postValue(ErrorMessage("Please Try Again!!", Constants.ERROR_CRITICAL))
}
}
}
override fun onUsbError(e: Exception?) {
Log.e(TAG, "onUsbIoError() called in sendCmdToFetchDeviceConstant() -> $e")
viewModel.errorTriggered.postValue(ErrorMessage("Please Try Again this step", Constants.ERROR_NORMAL))
viewModel.progressBar.postValue(false)
}
})
}
// private fun sendCmdToFetchDeviceConstant(recursive: Boolean) {
private fun sendCmdToFetchDeviceConstant() {
Log.d(TAG, "sendCmdToFetchDeviceConstant() called")