Compare commits
3 Commits
review_rel
...
usb_releas
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
91e2032af7 | ||
|
|
0179173860 | ||
|
|
36dc7e68a2 |
105
.gitlab-ci.yml
105
.gitlab-ci.yml
@@ -1,63 +1,31 @@
|
||||
# This file is a template, and might need editing before it works on your project.
|
||||
# To contribute improvements to CI/CD templates, please follow the Development guide at:
|
||||
# https://docs.gitlab.com/ee/development/cicd/templates.html
|
||||
# This specific template is located at:
|
||||
# https://gitlab.com/gitlab-org/gitlab/-/blob/master/lib/gitlab/ci/templates/Android.gitlab-ci.yml
|
||||
|
||||
# Read more about this script on this blog post https://about.gitlab.com/2018/10/24/setting-up-gitlab-ci-for-android-projects/, by Jason Lenny
|
||||
# If you are interested in using Android with FastLane for publishing take a look at the Android-Fastlane template.
|
||||
|
||||
image: eclipse-temurin:17-jdk-jammy
|
||||
|
||||
variables:
|
||||
|
||||
# ANDROID_COMPILE_SDK is the version of Android you're compiling with.
|
||||
# It should match compileSdkVersion.
|
||||
ANDROID_COMPILE_SDK: "34"
|
||||
|
||||
# ANDROID_BUILD_TOOLS is the version of the Android build tools you are using.
|
||||
# It should match buildToolsVersion.
|
||||
ANDROID_BUILD_TOOLS: "33.0.2"
|
||||
|
||||
# It's what version of the command line tools we're going to download from the official site.
|
||||
# Official Site-> https://developer.android.com/studio/index.html
|
||||
# There, look down below at the cli tools only, sdk tools package is of format:
|
||||
# commandlinetools-os_type-ANDROID_SDK_TOOLS_latest.zip
|
||||
# when the script was last modified for latest compileSdkVersion, it was which is written down below
|
||||
ANDROID_SDK_TOOLS: "9477386"
|
||||
|
||||
# Keystore credentials stored as GitLab CI/CD variables
|
||||
KEYSTORE_PASSWORD: $KS_PASSWORD
|
||||
KEY_ALIAS: $KS_ALIAS
|
||||
KEY_PASSWORD: $KS_KEY_PASSWORD
|
||||
|
||||
# Packages installation before running script
|
||||
before_script:
|
||||
- apt-get --quiet update --yes
|
||||
- apt-get --quiet install --yes wget unzip
|
||||
|
||||
# Setup path as android_home for moving/exporting the downloaded sdk into it
|
||||
- export ANDROID_HOME="${PWD}/android-sdk-root"
|
||||
# Create a new directory at specified location
|
||||
- install -d $ANDROID_HOME
|
||||
# Here we are installing androidSDK tools from official source,
|
||||
# (the key thing here is the url from where you are downloading these sdk tool for command line, so please do note this url pattern there and here as well)
|
||||
# after that unzipping those tools and
|
||||
# then running a series of SDK manager commands to install necessary android SDK packages that'll allow the app to build
|
||||
- wget --no-verbose --output-document=$ANDROID_HOME/cmdline-tools.zip https://dl.google.com/android/repository/commandlinetools-linux-${ANDROID_SDK_TOOLS}_latest.zip
|
||||
- unzip -q -d "$ANDROID_HOME/cmdline-tools" "$ANDROID_HOME/cmdline-tools.zip"
|
||||
- mv -T "$ANDROID_HOME/cmdline-tools/cmdline-tools" "$ANDROID_HOME/cmdline-tools/tools"
|
||||
- export PATH=$PATH:$ANDROID_HOME/cmdline-tools/latest/bin:$ANDROID_HOME/cmdline-tools/tools/bin
|
||||
|
||||
# Nothing fancy here, just checking sdkManager version
|
||||
- sdkmanager --version
|
||||
|
||||
# use yes to accept all licenses
|
||||
- yes | sdkmanager --licenses > /dev/null || true
|
||||
- sdkmanager "platforms;android-${ANDROID_COMPILE_SDK}"
|
||||
- sdkmanager "platform-tools"
|
||||
- sdkmanager "build-tools;${ANDROID_BUILD_TOOLS}"
|
||||
|
||||
# Not necessary, but just for surity
|
||||
- chmod +x ./gradlew
|
||||
|
||||
# Basic android and gradle stuff
|
||||
# Check linting
|
||||
lintDebug:
|
||||
interruptible: true
|
||||
stage: build
|
||||
@@ -69,7 +37,6 @@ lintDebug:
|
||||
expose_as: "lint-report"
|
||||
when: always
|
||||
|
||||
# Make Project
|
||||
assembleDebug:
|
||||
interruptible: true
|
||||
stage: build
|
||||
@@ -78,8 +45,68 @@ assembleDebug:
|
||||
artifacts:
|
||||
paths:
|
||||
- app/build/outputs/
|
||||
|
||||
# Job for building signed release APK for tags containing "release" on any branch
|
||||
assembleRelease:
|
||||
stage: build
|
||||
script:
|
||||
- |
|
||||
if [[ "$CI_COMMIT_TAG" =~ release ]]; then
|
||||
echo "Decoding keystore file from Base64"
|
||||
|
||||
# Debug: Print the first few characters of BASE64_KEYSTORE
|
||||
echo "First 20 characters of BASE64_KEYSTORE: ${BASE64_KEYSTORE:0:20}..."
|
||||
|
||||
# Check if BASE64_KEYSTORE is a file path
|
||||
if [[ "$BASE64_KEYSTORE" == /* ]] && [[ -f "$BASE64_KEYSTORE" ]]; then
|
||||
echo "BASE64_KEYSTORE appears to be a file path. Reading content..."
|
||||
BASE64_CONTENT=$(cat "$BASE64_KEYSTORE")
|
||||
else
|
||||
echo "BASE64_KEYSTORE is not a file path. Using as-is."
|
||||
BASE64_CONTENT="$BASE64_KEYSTORE"
|
||||
fi
|
||||
|
||||
# Remove any potential whitespace or newline characters
|
||||
CLEANED_KEYSTORE=$(echo "$BASE64_CONTENT" | tr -d '[:space:]')
|
||||
|
||||
# Attempt to decode and save to a file
|
||||
if echo "$CLEANED_KEYSTORE" | base64 -d > "$CI_PROJECT_DIR/app/keystore.jks" 2>/tmp/base64_error; then
|
||||
echo "Keystore file decoded successfully"
|
||||
else
|
||||
echo "Error decoding keystore file:"
|
||||
cat /tmp/base64_error
|
||||
echo "First 20 characters of cleaned content: ${CLEANED_KEYSTORE:0:20}..."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Check if the keystore file was created and has content
|
||||
if [ -s "$CI_PROJECT_DIR/app/keystore.jks" ]; then
|
||||
echo "Keystore file created successfully"
|
||||
# Print file size for verification
|
||||
ls -l "$CI_PROJECT_DIR/app/keystore.jks"
|
||||
else
|
||||
echo "Error: Keystore file is empty or not created"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "Building signed release APK"
|
||||
./gradlew assembleRelease \
|
||||
-Pandroid.injected.signing.store.file="$CI_PROJECT_DIR/app/keystore.jks" \
|
||||
-Pandroid.injected.signing.store.password="$KEYSTORE_PASSWORD" \
|
||||
-Pandroid.injected.signing.key.alias="$KEY_ALIAS" \
|
||||
-Pandroid.injected.signing.key.password="$KEY_PASSWORD"
|
||||
else
|
||||
echo "Tag '$CI_COMMIT_TAG' does not contain 'release'. Skipping release build."
|
||||
fi
|
||||
artifacts:
|
||||
paths:
|
||||
- app/build/outputs/
|
||||
expire_in: never
|
||||
rules:
|
||||
- if: $CI_COMMIT_TAG =~ /release/
|
||||
when: always
|
||||
- when: never
|
||||
|
||||
# Run all tests, if any fails, interrupt the pipeline(fail it)
|
||||
debugTests:
|
||||
needs: [lintDebug, assembleDebug]
|
||||
interruptible: true
|
||||
|
||||
@@ -63,6 +63,7 @@ android {
|
||||
dependencies {
|
||||
implementation "com.google.dagger:hilt-android:2.46"
|
||||
implementation 'androidx.activity:activity:1.8.0'
|
||||
implementation 'androidx.compose.ui:ui-android:1.7.6'
|
||||
kapt "com.google.dagger:hilt-android-compiler:2.46"
|
||||
|
||||
implementation 'androidx.core:core-ktx:1.12.0'
|
||||
|
||||
@@ -68,6 +68,7 @@
|
||||
<activity
|
||||
android:name="com.example.hpostesting.presentation.usb_teminal.UsbTerminalActivity"
|
||||
android:exported="false"
|
||||
android:launchMode="singleInstance"
|
||||
android:screenOrientation="portrait" />
|
||||
<activity
|
||||
android:name="com.example.hpostesting.presentation.deviceinfo.DeviceActivity"
|
||||
@@ -198,6 +199,7 @@
|
||||
android:exported="true"
|
||||
android:permission=""
|
||||
android:screenOrientation="portrait"
|
||||
android:launchMode="singleInstance"
|
||||
android:theme="@style/AppTheme.NoActionBar">
|
||||
<intent-filter>
|
||||
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
|
||||
|
||||
@@ -73,8 +73,15 @@ class MainActivity : AppCompatActivity() {
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
sharedPreference = getSharedPreferences("HEMOCUBE", Context.MODE_PRIVATE)
|
||||
|
||||
sharedPreference = getSharedPreferences("HEMOCUBE", Context.MODE_PRIVATE)
|
||||
val action = intent?.action
|
||||
if (action == UsbManager.ACTION_USB_DEVICE_ATTACHED || action == UsbManager.ACTION_USB_DEVICE_DETACHED) {
|
||||
// If the activity is triggered by USB events, simply finish it
|
||||
checkAndUpdateUsbConnection()
|
||||
finish()
|
||||
return
|
||||
}
|
||||
binding = ActivityMainBinding.inflate(layoutInflater)
|
||||
setContentView(binding.root)
|
||||
binding.myToolbar.title = "Test Type"
|
||||
@@ -150,7 +157,7 @@ class MainActivity : AppCompatActivity() {
|
||||
}
|
||||
|
||||
(device.productId == 24577 && device.vendorId == 1027) || (device.productId == 8963 && device.vendorId == 1659) || (device.productId == 4614 && device.vendorId == 7111) -> {
|
||||
changeUI()
|
||||
// changeUI()
|
||||
DataHolder.deviceType.postValue(Constants.DEVICE_TYPE_TRUEHEME)
|
||||
Constants.DEVICE_TYPE_TRUEHEME
|
||||
}
|
||||
|
||||
@@ -116,6 +116,7 @@ class GalleryFragment : Fragment() {
|
||||
binding.btnUsbTerminal.setOnClickListener {
|
||||
startActivity(Intent(requireContext(), UsbTerminalActivity::class.java))
|
||||
}
|
||||
|
||||
binding.btnSubmit.setOnClickListener {
|
||||
startActivity(Intent(requireContext(), HemocubeBufferCheckActivity::class.java))
|
||||
}
|
||||
|
||||
@@ -36,11 +36,7 @@ import androidx.core.content.ContextCompat
|
||||
import androidx.core.view.get
|
||||
import com.example.hpostesting.data.constant.Constants
|
||||
import com.example.hpostesting.data.constant.DataHolder
|
||||
import com.example.hpostesting.data.constant.HemoCubeCommands
|
||||
import com.example.hpostesting.data.constant.LanguageManager
|
||||
import com.example.hpostesting.presentation.deviceinfo.DeviceFragment
|
||||
import com.example.hpostesting.presentation.utils.DeviceCommunicationHandler
|
||||
import com.example.hpostesting.presentation.utils.UsbServiceListener
|
||||
import com.example.hpostesting.util.UsbService
|
||||
import com.hoho.android.usbserial.driver.UsbSerialDriver
|
||||
import com.hoho.android.usbserial.driver.UsbSerialProber
|
||||
@@ -59,25 +55,37 @@ class UsbTerminalActivity : AppCompatActivity() {
|
||||
private var mConnection: UsbDeviceConnection? = null
|
||||
lateinit var mService: UsbService
|
||||
private var deviceId = ""
|
||||
private var executedCommands: MutableList<String> = mutableListOf()
|
||||
private var isResuming = false
|
||||
private val TAG = "Calibration"
|
||||
|
||||
private val broadcastReceiver = object : BroadcastReceiver() {
|
||||
override fun onReceive(context: Context, intent: Intent) {
|
||||
|
||||
synchronized(this) {
|
||||
val device: UsbDevice? = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE)
|
||||
|
||||
if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
|
||||
device?.apply {
|
||||
if (UsbManager.ACTION_USB_DEVICE_DETACHED == intent.action) {
|
||||
// Handle USB disconnection
|
||||
Log.d(TAG, "USB device detached")
|
||||
cleanupUsb()
|
||||
DataHolder.usbConnected.postValue(false)
|
||||
}
|
||||
|
||||
if (UsbManager.ACTION_USB_DEVICE_ATTACHED == intent.action) {
|
||||
// Handle USB reconnection
|
||||
Log.d(TAG, "USB device attached")
|
||||
connectUsb(false) // Attempt reconnection
|
||||
} else if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
|
||||
// Handle USB permission granted
|
||||
device?.let {
|
||||
connectUsb(true)
|
||||
DataHolder.usbConnected.postValue(true)
|
||||
}
|
||||
} else {
|
||||
onErrorReported("permission denied for device")
|
||||
DataHolder.usbConnected.postValue(true)
|
||||
Log.e(TAG, "USB permission denied")
|
||||
DataHolder.usbConnected.postValue(false)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -96,6 +104,15 @@ class UsbTerminalActivity : AppCompatActivity() {
|
||||
}
|
||||
}
|
||||
|
||||
fun getExecutedCommands(): List<String> {
|
||||
return executedCommands
|
||||
}
|
||||
|
||||
fun addExecutedCommand(command: String) {
|
||||
if (!executedCommands.contains(command)) {
|
||||
executedCommands.add(command)
|
||||
}
|
||||
}
|
||||
override fun attachBaseContext(newBase: Context?) {
|
||||
val languageCode = LanguageManager.getSavedLanguage(newBase!!)
|
||||
LanguageManager.setLocale(newBase, languageCode)
|
||||
@@ -106,18 +123,28 @@ class UsbTerminalActivity : AppCompatActivity() {
|
||||
super.onCreate(savedInstanceState)
|
||||
binding = ActivityUsbTerminalBinding.inflate(layoutInflater)
|
||||
setContentView(binding.root)
|
||||
// setSupportActionBar(binding.myToolbar)
|
||||
supportActionBar?.setDisplayHomeAsUpEnabled(true)
|
||||
|
||||
val filter = IntentFilter()
|
||||
filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED)
|
||||
filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED)
|
||||
registerReceiver(broadcastReceiver, filter)
|
||||
|
||||
doBoth()
|
||||
}
|
||||
|
||||
|
||||
fun doBoth(){
|
||||
setupListener()
|
||||
connectUsb(false)
|
||||
}
|
||||
|
||||
private fun setupListener() {
|
||||
|
||||
DataHolder.usbConnected.observe(this) {
|
||||
Log.d("USB OBSERVE", "HemoCube called -> $it")
|
||||
if (it) {
|
||||
myMenu?.get(0)?.icon =
|
||||
ContextCompat.getDrawable(this, R.drawable.ic_baseline_usb_24)
|
||||
|
||||
} else {
|
||||
myMenu?.get(0)?.icon =
|
||||
ContextCompat.getDrawable(this, R.drawable.ic_baseline_usb_off_24)
|
||||
@@ -128,11 +155,14 @@ class UsbTerminalActivity : AppCompatActivity() {
|
||||
|
||||
open fun connectUsb(permissionGranted: Boolean) {
|
||||
Log.d(TAG, "connectUsb() called, permission variable = $permissionGranted")
|
||||
cleanupUsb() // Clean up before reinitializing
|
||||
|
||||
val manager = getSystemService(Context.USB_SERVICE) as UsbManager
|
||||
val availableDrivers = UsbSerialProber.getDefaultProber().findAllDrivers(manager)
|
||||
|
||||
if (availableDrivers.isEmpty()) {
|
||||
onErrorReported("No Device is Connected")
|
||||
onErrorReported("No Device Connected")
|
||||
DataHolder.usbConnected.postValue(false)
|
||||
} else {
|
||||
mDriver = availableDrivers[0]
|
||||
mConnection = manager.openDevice(mDriver.device)
|
||||
@@ -140,13 +170,13 @@ class UsbTerminalActivity : AppCompatActivity() {
|
||||
requestUserPermission(manager, mDriver.device)
|
||||
} else {
|
||||
setupService()
|
||||
DataHolder.usbConnected.postValue(true)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun onErrorReported(msg: String) {
|
||||
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show()
|
||||
if (!isFinishing) onBackPressed()
|
||||
}
|
||||
|
||||
private fun moveToNext() {
|
||||
@@ -156,6 +186,9 @@ class UsbTerminalActivity : AppCompatActivity() {
|
||||
.commit()
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@SuppressLint("MutableImplicitPendingIntent")
|
||||
private fun requestUserPermission(manager: UsbManager, device: UsbDevice) {
|
||||
val mPendingIntent: PendingIntent
|
||||
@@ -187,12 +220,29 @@ class UsbTerminalActivity : AppCompatActivity() {
|
||||
return true
|
||||
}
|
||||
|
||||
|
||||
override fun onDestroy() {
|
||||
super.onDestroy()
|
||||
unregisterReceiver(broadcastReceiver)
|
||||
if (usbTerminalViewModel.isServiceConnected) {
|
||||
mService.disconnect()
|
||||
unbindService(connection)
|
||||
usbTerminalViewModel.isServiceConnected = false
|
||||
}
|
||||
cleanupUsb()
|
||||
}
|
||||
|
||||
|
||||
|
||||
private fun cleanupUsb() {
|
||||
try {
|
||||
mService.disconnect()
|
||||
unbindService(connection)
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "Error during USB cleanup: ${e.message}")
|
||||
}
|
||||
mConnection?.close()
|
||||
mConnection = null
|
||||
}
|
||||
|
||||
}
|
||||
@@ -13,6 +13,8 @@
|
||||
|
||||
package com.example.hpostesting.presentation.usb_teminal
|
||||
|
||||
// Fragment Class
|
||||
|
||||
import android.content.Context
|
||||
import android.content.SharedPreferences
|
||||
import android.os.Bundle
|
||||
@@ -23,63 +25,145 @@ import android.view.ViewGroup
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.fragment.app.activityViewModels
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import com.example.hpostesting.data.constant.HemoCubeCommands
|
||||
import com.example.hpostesting.presentation.utils.UsbServiceListener
|
||||
import com.google.firebase.crashlytics.ktx.crashlytics
|
||||
import com.google.firebase.ktx.Firebase
|
||||
import `in`.sminnovations.hpostesting.databinding.FragmentUsbTerminalBinding
|
||||
|
||||
|
||||
class UsbTerminalFragment : Fragment() {
|
||||
private lateinit var binding: FragmentUsbTerminalBinding
|
||||
private val usbTerminalViewModel: UsbTerminalViewModel by activityViewModels()
|
||||
private lateinit var sharedPreferences: SharedPreferences
|
||||
private var startListening = MutableLiveData(false)
|
||||
private var resultData: String = ""
|
||||
private var resultData = StringBuilder()
|
||||
private var commandQueue: MutableList<String> = mutableListOf()
|
||||
private var currentCommand: String? = null
|
||||
var executedCommands: MutableList<String> = mutableListOf()
|
||||
|
||||
override fun onCreateView(
|
||||
inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?,
|
||||
): View {
|
||||
binding = FragmentUsbTerminalBinding.inflate(inflater, container, false)
|
||||
sharedPreferences =
|
||||
requireContext().getSharedPreferences("HEMOCUBE", Context.MODE_PRIVATE)
|
||||
sharedPreferences = requireContext().getSharedPreferences("HEMOCUBE", Context.MODE_PRIVATE)
|
||||
return binding.root
|
||||
}
|
||||
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
super.onViewCreated(view, savedInstanceState)
|
||||
|
||||
val pastCommands = (activity as? UsbTerminalActivity)?.getExecutedCommands() ?: emptyList()
|
||||
if (pastCommands.isNotEmpty()) {
|
||||
resumeCommands(pastCommands)
|
||||
}
|
||||
|
||||
initViews()
|
||||
}
|
||||
|
||||
|
||||
private fun initViews() {
|
||||
binding.messageCommand.movementMethod = ScrollingMovementMethod()
|
||||
usbTerminalViewModel.messages.observe(viewLifecycleOwner) {
|
||||
binding.messageCommand.text = it
|
||||
|
||||
// Observe messages with proper formatting
|
||||
usbTerminalViewModel.messages.observe(viewLifecycleOwner) { message ->
|
||||
binding.messageCommand.text = message
|
||||
scrollToBottom()
|
||||
}
|
||||
|
||||
binding.sendCommand.setOnClickListener {
|
||||
callCommand(binding.etCommands.text.trim().toString())
|
||||
val commands = binding.etCommands.text.toString().trim()
|
||||
if (commands.isNotEmpty()) {
|
||||
// Process entire command string
|
||||
addToQueue(listOf(commands))
|
||||
binding.etCommands.text.clear()
|
||||
}
|
||||
}
|
||||
binding.button.setOnClickListener{
|
||||
|
||||
}
|
||||
|
||||
listenToHemoCube()
|
||||
callCommand("I")
|
||||
}
|
||||
|
||||
private fun callCommand(command: String) {
|
||||
usbTerminalViewModel.progressBar.postValue(true)
|
||||
(activity as UsbTerminalActivity).mService.sendAndListenToHemoCubeTxt(
|
||||
command,
|
||||
object : UsbServiceListener {
|
||||
override fun onUsbRead(data: ByteArray?) {
|
||||
//nothing
|
||||
}
|
||||
private fun scrollToBottom() {
|
||||
binding.messageCommand.post {
|
||||
val scrollAmount = binding.messageCommand.layout.getLineTop(binding.messageCommand.lineCount) -
|
||||
binding.messageCommand.height
|
||||
if (scrollAmount > 0) {
|
||||
binding.messageCommand.scrollTo(0, scrollAmount)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onUsbError(e: Exception?) {
|
||||
usbTerminalViewModel.progressBar.postValue(false)
|
||||
private fun addToQueue(commands: List<String>) {
|
||||
commandQueue.clear()
|
||||
currentCommand = null
|
||||
|
||||
commandQueue.addAll(commands)
|
||||
|
||||
// Track commands in the activity
|
||||
(activity as? UsbTerminalActivity)?.let { activity ->
|
||||
commands.forEach { command ->
|
||||
activity.addExecutedCommand(command)
|
||||
}
|
||||
}
|
||||
|
||||
appendFormattedMessage("\nNew priority commands: ${commands.joinToString(" ")}")
|
||||
processNextCommand()
|
||||
}
|
||||
|
||||
|
||||
|
||||
private fun appendFormattedMessage(message: String) {
|
||||
resultData.append("\n").append(message)
|
||||
usbTerminalViewModel.messages.postValue(resultData.toString())
|
||||
scrollToBottom()
|
||||
}
|
||||
|
||||
private fun processNextCommand() {
|
||||
if (currentCommand == null && commandQueue.isNotEmpty()) {
|
||||
currentCommand = commandQueue.removeAt(0)
|
||||
callCommand(currentCommand!!)
|
||||
}
|
||||
}
|
||||
|
||||
private fun callCommand(command: String) {
|
||||
// Don't process if command was cleared
|
||||
if (currentCommand == null) {
|
||||
return
|
||||
}
|
||||
|
||||
usbTerminalViewModel.progressBar.postValue(true)
|
||||
appendFormattedMessage("Sending command: $command \n")
|
||||
|
||||
try {
|
||||
(activity as UsbTerminalActivity).mService.sendAndListenToHemoCubeTxt(
|
||||
command,
|
||||
object : UsbServiceListener {
|
||||
override fun onUsbRead(data: ByteArray?) {
|
||||
// Keep empty as per original code
|
||||
}
|
||||
|
||||
override fun onUsbError(e: Exception?) {
|
||||
activity?.runOnUiThread {
|
||||
|
||||
appendFormattedMessage("Error sending command $command: ${e?.message ?: "Unknown error"}")
|
||||
usbTerminalViewModel.progressBar.postValue(false)
|
||||
currentCommand = null
|
||||
|
||||
|
||||
processNextCommand()
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
)
|
||||
} catch (e: Exception) {
|
||||
appendFormattedMessage("Error: ${e.localizedMessage}")
|
||||
usbTerminalViewModel.progressBar.postValue(false)
|
||||
currentCommand = null
|
||||
processNextCommand()
|
||||
}
|
||||
}
|
||||
|
||||
private fun listenToHemoCube() {
|
||||
|
||||
val fullReadOutput = StringBuilder()
|
||||
startListening.postValue(true)
|
||||
|
||||
try {
|
||||
@@ -87,20 +171,58 @@ class UsbTerminalFragment : Fragment() {
|
||||
override fun onUsbRead(data: ByteArray?) {
|
||||
data?.let {
|
||||
val stringData = String(it)
|
||||
fullReadOutput.append(stringData)
|
||||
resultData += stringData
|
||||
usbTerminalViewModel.messages.postValue(resultData)
|
||||
resultData.append(stringData)
|
||||
|
||||
activity?.runOnUiThread {
|
||||
usbTerminalViewModel.messages.postValue(resultData.toString())
|
||||
scrollToBottom()
|
||||
|
||||
// Check for command completion
|
||||
if (currentCommand != null && stringData.contains("${currentCommand}C")) {
|
||||
currentCommand = null
|
||||
usbTerminalViewModel.progressBar.postValue(false)
|
||||
|
||||
// If there are no more commands in queue, clear completion flags
|
||||
if (commandQueue.isEmpty()) {
|
||||
appendFormattedMessage("\nAll commands completed")
|
||||
} else {
|
||||
processNextCommand()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onUsbError(e: Exception?) {
|
||||
usbTerminalViewModel.messages.postValue(e!!.localizedMessage)
|
||||
usbTerminalViewModel.progressBar.postValue(false)
|
||||
activity?.runOnUiThread {
|
||||
appendFormattedMessage("Error: ${e?.localizedMessage ?: "Unknown error"}")
|
||||
usbTerminalViewModel.progressBar.postValue(false)
|
||||
currentCommand = null
|
||||
processNextCommand()
|
||||
}
|
||||
}
|
||||
})
|
||||
} catch (e: Exception) {
|
||||
usbTerminalViewModel.messages.postValue(e.localizedMessage)
|
||||
Firebase.crashlytics.recordException(e)
|
||||
appendFormattedMessage("Error: ${e.localizedMessage}")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun resumeCommands(pastCommands: List<String>) {
|
||||
appendFormattedMessage("Resuming execution...")
|
||||
pastCommands.forEach { command ->
|
||||
if (!commandQueue.contains(command)) {
|
||||
commandQueue.add(command)
|
||||
}
|
||||
}
|
||||
processNextCommand()
|
||||
}
|
||||
|
||||
|
||||
override fun onDestroyView() {
|
||||
super.onDestroyView()
|
||||
startListening.value = false
|
||||
resultData.clear()
|
||||
commandQueue.clear()
|
||||
currentCommand = null
|
||||
}
|
||||
}
|
||||
@@ -23,8 +23,6 @@ import javax.inject.Inject
|
||||
|
||||
@HiltViewModel
|
||||
class UsbTerminalViewModel @Inject constructor(
|
||||
private val hemoCubeDao: HemoCubeDao,
|
||||
private val repository: Repository,
|
||||
) : ViewModel() {
|
||||
var isServiceConnected = false
|
||||
val progressBar = MutableLiveData(false)
|
||||
|
||||
5
app/src/main/res/drawable/baseline_send_24.xml
Normal file
5
app/src/main/res/drawable/baseline_send_24.xml
Normal file
@@ -0,0 +1,5 @@
|
||||
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:autoMirrored="true" android:height="24dp" android:tint="#000000" android:viewportHeight="24" android:viewportWidth="24" android:width="24dp">
|
||||
|
||||
<path android:fillColor="@android:color/white" android:pathData="M2.01,21L23,12 2.01,3 2,10l15,2 -15,2z"/>
|
||||
|
||||
</vector>
|
||||
@@ -11,59 +11,51 @@
|
||||
~ // from ShanMukha Innovations Pvt. Ltd.
|
||||
-->
|
||||
|
||||
<RelativeLayout
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_marginStart="8dp"
|
||||
android:layout_marginEnd="8dp"
|
||||
>
|
||||
android:orientation="vertical"
|
||||
android:padding="16dp">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/message_command"
|
||||
style="@style/title1_1"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_marginTop="10dp"
|
||||
android:gravity="start|bottom"
|
||||
android:text="Start"
|
||||
android:scrollbars = "vertical"
|
||||
android:layout_alignParentStart="true"
|
||||
android:layout_alignParentEnd="true"
|
||||
android:layout_above="@+id/ll_commands"
|
||||
android:visibility="visible"
|
||||
android:textColor="@color/black"
|
||||
android:textSize="18sp"
|
||||
/>
|
||||
<RelativeLayout
|
||||
android:id="@+id/ll_commands"
|
||||
<Button
|
||||
android:id="@+id/button"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_alignParentEnd="true"
|
||||
android:layout_alignParentStart="true"
|
||||
android:layout_alignParentBottom="true"
|
||||
android:text="Clar All Commands" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/messageCommand"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="0dp"
|
||||
android:layout_weight="1"
|
||||
android:background="@android:color/darker_gray"
|
||||
android:padding="8dp"
|
||||
android:scrollbars="vertical"
|
||||
android:textColor="@android:color/white" />
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="16dp"
|
||||
android:orientation="horizontal">
|
||||
|
||||
<EditText
|
||||
android:id="@+id/et_commands"
|
||||
android:layout_width="match_parent"
|
||||
android:id="@+id/etCommands"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="10dp"
|
||||
android:hint="@string/commands"
|
||||
android:layout_alignParentStart="true"
|
||||
android:imeOptions="actionDone"
|
||||
android:textSize="18sp"
|
||||
android:layout_marginBottom="10dp"
|
||||
/>
|
||||
<ImageView
|
||||
android:id="@+id/send_command"
|
||||
android:layout_width="32dp"
|
||||
android:layout_height="26dp"
|
||||
android:layout_alignParentEnd="true"
|
||||
android:layout_centerInParent="true"
|
||||
android:src="@drawable/ic_baseline_send_24"/>
|
||||
</RelativeLayout>
|
||||
android:layout_weight="1"
|
||||
android:hint="Enter command"
|
||||
android:inputType="text"
|
||||
android:maxLines="1"
|
||||
android:minHeight="48dp" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/sendCommand"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginStart="8dp"
|
||||
android:text="Send" />
|
||||
|
||||
</RelativeLayout>
|
||||
</LinearLayout>
|
||||
|
||||
</LinearLayout>
|
||||
@@ -12,7 +12,7 @@
|
||||
-->
|
||||
|
||||
<resources>
|
||||
<string name="app_name">HPOS Testing</string>
|
||||
<string name="app_name">HPOS ALPHA Testing</string>
|
||||
|
||||
<string-array name="instrument">
|
||||
<item>TestRight</item>
|
||||
|
||||
Reference in New Issue
Block a user