Skip to content
This repository has been archived by the owner on Aug 15, 2021. It is now read-only.

Commit

Permalink
2020-02-17 Version 1.2.0: Refactored BtMonitor realizations
Browse files Browse the repository at this point in the history
  • Loading branch information
fartem committed Feb 17, 2020
1 parent df6544d commit ba554d8
Show file tree
Hide file tree
Showing 22 changed files with 278 additions and 152 deletions.
20 changes: 17 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,30 @@ android {
minSdkVersion 21
targetSdkVersion 29
versionCode 1
versionName "1.1.0"
versionName "1.2.0"

buildConfigField "String", "API_IMPL", "\"\""
}
productFlavors {
devicebt {
flavorDimensions "build"

buildConfigField "String", "API_IMPL", "\"DEVICE_BT\""
}
debugbt {
flavorDimensions "build"

buildConfigField "String", "API_IMPL", "\"DEBUG_BT\""
}
}
}

dependencies {
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"

implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'com.google.android.material:material:1.0.0'
implementation 'androidx.core:core-ktx:1.1.0'
implementation 'com.google.android.material:material:1.1.0'
implementation 'androidx.core:core-ktx:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'

// Unit tests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class MainActivity : AppCompatActivity() {
private fun handleOnPause() {
val currentFragment = supportFragmentManager.findFragmentByTag(CURRENT_FRAGMENT_TAG)
if (currentFragment != null && currentFragment is BtPauseTarget) {
currentFragment.handleBtInOnPause()
currentFragment.btOnPause()
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.smlnskgmail.jaman.remotetemperaturecontrol.components.dialogs
package com.smlnskgmail.jaman.remotetemperaturecontrol.components

import android.app.AlertDialog
import android.content.Context
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.smlnskgmail.jaman.remotetemperaturecontrol.components.bottomsheets
package com.smlnskgmail.jaman.remotetemperaturecontrol.components

import android.os.Bundle
import android.view.LayoutInflater
Expand All @@ -8,7 +8,10 @@ import com.google.android.material.bottomsheet.BottomSheetDialogFragment

abstract class BaseBottomSheet : BottomSheetDialogFragment() {

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
override fun onViewCreated(
view: View,
savedInstanceState: Bundle?
) {
initialize()
}

Expand All @@ -18,7 +21,11 @@ abstract class BaseBottomSheet : BottomSheetDialogFragment() {
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
) : View? = inflater.inflate(getLayoutResId(), container, false)
) : View? = inflater.inflate(
getLayoutResId(),
container,
false
)

abstract fun getLayoutResId(): Int

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package com.smlnskgmail.jaman.remotetemperaturecontrol.components.fragments
package com.smlnskgmail.jaman.remotetemperaturecontrol.components

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.smlnskgmail.jaman.remotetemperaturecontrol.components.bottomsheets.BaseBottomSheet
import com.smlnskgmail.jaman.remotetemperaturecontrol.logic.monitor.api.entities.targets.BtPauseTarget

abstract class BaseFragment : Fragment(), BtPauseTarget {
Expand All @@ -14,7 +13,11 @@ abstract class BaseFragment : Fragment(), BtPauseTarget {
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
) : View = inflater.inflate(getLayoutResId(), container, false)
) : View = inflater.inflate(
getLayoutResId(),
container,
false
)

abstract fun getLayoutResId(): Int

Expand All @@ -25,6 +28,6 @@ abstract class BaseFragment : Fragment(), BtPauseTarget {
)
}

override fun handleBtInOnPause() {}
override fun btOnPause() {}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.smlnskgmail.jaman.remotetemperaturecontrol.logic.deviceselector

import com.smlnskgmail.jaman.remotetemperaturecontrol.R
import com.smlnskgmail.jaman.remotetemperaturecontrol.components.bottomsheets.BaseBottomSheet
import com.smlnskgmail.jaman.remotetemperaturecontrol.components.BaseBottomSheet
import com.smlnskgmail.jaman.remotetemperaturecontrol.logic.deviceselector.recycler.BtDevicesAdapter
import com.smlnskgmail.jaman.remotetemperaturecontrol.logic.monitor.api.entities.BtDevice
import com.smlnskgmail.jaman.remotetemperaturecontrol.logic.monitor.api.entities.targets.BtConnectTarget
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import android.content.DialogInterface
import android.os.Bundle
import android.view.View
import android.widget.TextView
import com.smlnskgmail.jaman.remotetemperaturecontrol.BuildConfig
import com.smlnskgmail.jaman.remotetemperaturecontrol.R
import com.smlnskgmail.jaman.remotetemperaturecontrol.components.dialogs.AppDialog
import com.smlnskgmail.jaman.remotetemperaturecontrol.components.fragments.BaseFragment
import com.smlnskgmail.jaman.remotetemperaturecontrol.components.AppDialog
import com.smlnskgmail.jaman.remotetemperaturecontrol.components.BaseFragment
import com.smlnskgmail.jaman.remotetemperaturecontrol.logic.deviceselector.BtDevicesBottomSheet
import com.smlnskgmail.jaman.remotetemperaturecontrol.logic.monitor.api.BtConnection
import com.smlnskgmail.jaman.remotetemperaturecontrol.logic.monitor.api.BtMonitor
Expand All @@ -32,31 +33,45 @@ class MonitorFragment : BaseFragment(), BtMonitorTarget {

private var btMonitor: BtMonitor? = null

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
if (btIsEnabled()) {
btAdapter = BluetoothAdapter.getDefaultAdapter()
val btDevices = getBtDevices()
if (btDevices.isNotEmpty()) {
btMonitor = DeviceBtMonitor(this)
showDevicesList(btDevices)
override fun onViewCreated(
view: View,
savedInstanceState: Bundle?
) {
if (BuildConfig.API_IMPL == "DEVICE_BT") {
if (btIsEnabled()) {
btAdapter = BluetoothAdapter.getDefaultAdapter()
val btDevices = getBtDevices()
if (btDevices.isNotEmpty()) {
btMonitor = DeviceBtMonitor(this)
showDevicesList(btDevices)
} else {
showBtDevicesNotFoundWarning()
}
} else {
showBtDevicesNotFoundWarning()
showBtDisabledWarning()
}
} else {
showBtDisabledWarning()
startInDebugMode()
initializeButtons()
}
}

@SuppressLint("SetTextI18n")
@Suppress("unused")
private fun startInDebugMode() {
btMonitor = DebugBtMonitor(this)
monitorBtConnection = DebugBtConnection(btMonitor!!)
monitorBtConnection!!.start()
startMonitorThread()

tv_connected_device_info.text = "DEBUG"
btn_main_options.isEnabled = false
btn_reset_monitor.isEnabled = false
}

private fun startMonitorThread() {
object : Thread() {
override fun run() {
super.run()
monitorBtConnection!!.connect()
}
}.start()
}

private fun btIsEnabled() = true
Expand All @@ -65,14 +80,16 @@ class MonitorFragment : BaseFragment(), BtMonitorTarget {
val devicesBottomSheet = BtDevicesBottomSheet()
devicesBottomSheet.setBtDevices(btDevices)
devicesBottomSheet.setBtDeviceSelectCallback(object : BtConnectTarget {
override fun onBtDeviceSelected(name: String, address: String) {
override fun onBtDeviceSelected(
name: String,
address: String
) {
monitorBtConnection = DeviceBtConnection(
btAdapter!!,
address,
btMonitor!!
)
monitorBtConnection!!.connect()
monitorBtConnection!!.start()
startMonitorThread()
setDeviceName(name)
initializeButtons()
}
Expand Down Expand Up @@ -132,7 +149,10 @@ class MonitorFragment : BaseFragment(), BtMonitorTarget {
}
}

private fun setTextOnUIThread(textView: TextView, text: String) {
private fun setTextOnUIThread(
textView: TextView,
text: String
) {
activity!!.runOnUiThread {
textView.text = text
}
Expand Down Expand Up @@ -186,7 +206,7 @@ class MonitorFragment : BaseFragment(), BtMonitorTarget {
showBottomSheet(settingsBottomSheet)
}

override fun handleBtInOnPause() {
override fun btOnPause() {
monitorBtConnection!!.handleOnResume()
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,11 @@
package com.smlnskgmail.jaman.remotetemperaturecontrol.logic.monitor.api

abstract class BtConnection : Thread() {
interface BtConnection {

open fun connect() {
fun connect()
fun disconnect()
fun handleOnResume()

}

open fun disconnect() {

}

open fun handleOnResume() {

}

open fun send(btMonitorSignalType: BtMonitorSignalType) {

}
fun send(btMonitorSignalType: BtMonitorSignalType)

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.smlnskgmail.jaman.remotetemperaturecontrol.logic.monitor.api
interface BtMonitor {

fun onNewDataAvailable(
btMonitorSignalType: BtMonitorSignalType,
signalType: BtMonitorSignalType,
rawData: String
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,6 @@ enum class BtMonitorSignalType {
HumidityMinimum,
HumidityMaximum,
Reset,
Other;

companion object {

fun fromRawData(rawData: String): BtMonitorSignalType {
return when (rawData[0].toString()) {
"t" -> Temperature
"i" -> TemperatureMinimum
"m" -> TemperatureMaximum
"h" -> Humidity
"q" -> HumidityMinimum
"w" -> HumidityMaximum
"r" -> Reset
else -> Other
}
}

fun signalOf(btMonitorSignalType: BtMonitorSignalType): String {
return when (btMonitorSignalType) {
Temperature -> "t"
TemperatureMinimum -> "i"
TemperatureMaximum -> "m"
Humidity -> "h"
HumidityMinimum -> "q"
HumidityMaximum -> "w"
Reset -> "r"
else -> ""
}
}

}
Other

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package com.smlnskgmail.jaman.remotetemperaturecontrol.logic.monitor.api.entitie

interface BtConnectTarget {

fun onBtDeviceSelected(name: String, address: String)
fun onBtDeviceSelected(
name: String,
address: String
)

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ package com.smlnskgmail.jaman.remotetemperaturecontrol.logic.monitor.api.entitie

interface BtPauseTarget {

fun handleBtInOnPause()
fun btOnPause()

}
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package com.smlnskgmail.jaman.remotetemperaturecontrol.logic.monitor.impl.debugbt

import android.util.Log
import com.smlnskgmail.jaman.remotetemperaturecontrol.logic.monitor.api.BtConnection
import com.smlnskgmail.jaman.remotetemperaturecontrol.logic.monitor.api.BtMonitor
import com.smlnskgmail.jaman.remotetemperaturecontrol.logic.monitor.api.BtMonitorSignalType

class DebugBtConnection(
private val btMonitor: BtMonitor
) : BtConnection() {
) : BtConnection {

companion object {

Expand Down Expand Up @@ -40,11 +41,13 @@ class DebugBtConnection(

private var loopCounter = 0

override fun run() {
super.run()
override fun connect() {
writeLog(
"Debug Monitor: connect()"
)
while (true) {
@Suppress("MagicNumber")
sleep(5_000)
Thread.sleep(5_000)
btMonitor.onNewDataAvailable(
BtMonitorSignalType.Temperature,
temperature[loopCounter]
Expand Down Expand Up @@ -77,4 +80,29 @@ class DebugBtConnection(
}
}

private fun writeLog(message: String) {
Log.i(
"MONITOR",
message
)
}

override fun disconnect() {
writeLog(
"Debug Monitor: disconnect()"
)
}

override fun handleOnResume() {
writeLog(
"Debug Monitor: handleOnResume()"
)
}

override fun send(btMonitorSignalType: BtMonitorSignalType) {
writeLog(
"Debug Monitor: send(${btMonitorSignalType})"
)
}

}
Loading

0 comments on commit ba554d8

Please sign in to comment.