diff --git a/app/build.gradle b/app/build.gradle index 80d1253..fe980ad 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -12,7 +12,21 @@ 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\"" + } } } @@ -20,8 +34,8 @@ 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 diff --git a/app/src/main/java/com/smlnskgmail/jaman/remotetemperaturecontrol/MainActivity.kt b/app/src/main/java/com/smlnskgmail/jaman/remotetemperaturecontrol/MainActivity.kt index d2a6e20..03e2e04 100644 --- a/app/src/main/java/com/smlnskgmail/jaman/remotetemperaturecontrol/MainActivity.kt +++ b/app/src/main/java/com/smlnskgmail/jaman/remotetemperaturecontrol/MainActivity.kt @@ -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() } } diff --git a/app/src/main/java/com/smlnskgmail/jaman/remotetemperaturecontrol/components/dialogs/AppDialog.kt b/app/src/main/java/com/smlnskgmail/jaman/remotetemperaturecontrol/components/AppDialog.kt similarity index 98% rename from app/src/main/java/com/smlnskgmail/jaman/remotetemperaturecontrol/components/dialogs/AppDialog.kt rename to app/src/main/java/com/smlnskgmail/jaman/remotetemperaturecontrol/components/AppDialog.kt index e8d0302..c5f2b5f 100644 --- a/app/src/main/java/com/smlnskgmail/jaman/remotetemperaturecontrol/components/dialogs/AppDialog.kt +++ b/app/src/main/java/com/smlnskgmail/jaman/remotetemperaturecontrol/components/AppDialog.kt @@ -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 diff --git a/app/src/main/java/com/smlnskgmail/jaman/remotetemperaturecontrol/components/bottomsheets/BaseBottomSheet.kt b/app/src/main/java/com/smlnskgmail/jaman/remotetemperaturecontrol/components/BaseBottomSheet.kt similarity index 73% rename from app/src/main/java/com/smlnskgmail/jaman/remotetemperaturecontrol/components/bottomsheets/BaseBottomSheet.kt rename to app/src/main/java/com/smlnskgmail/jaman/remotetemperaturecontrol/components/BaseBottomSheet.kt index eee6597..d866015 100644 --- a/app/src/main/java/com/smlnskgmail/jaman/remotetemperaturecontrol/components/bottomsheets/BaseBottomSheet.kt +++ b/app/src/main/java/com/smlnskgmail/jaman/remotetemperaturecontrol/components/BaseBottomSheet.kt @@ -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 @@ -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() } @@ -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 diff --git a/app/src/main/java/com/smlnskgmail/jaman/remotetemperaturecontrol/components/fragments/BaseFragment.kt b/app/src/main/java/com/smlnskgmail/jaman/remotetemperaturecontrol/components/BaseFragment.kt similarity index 77% rename from app/src/main/java/com/smlnskgmail/jaman/remotetemperaturecontrol/components/fragments/BaseFragment.kt rename to app/src/main/java/com/smlnskgmail/jaman/remotetemperaturecontrol/components/BaseFragment.kt index 8491b43..7e5f47a 100644 --- a/app/src/main/java/com/smlnskgmail/jaman/remotetemperaturecontrol/components/fragments/BaseFragment.kt +++ b/app/src/main/java/com/smlnskgmail/jaman/remotetemperaturecontrol/components/BaseFragment.kt @@ -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 { @@ -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 @@ -25,6 +28,6 @@ abstract class BaseFragment : Fragment(), BtPauseTarget { ) } - override fun handleBtInOnPause() {} + override fun btOnPause() {} } diff --git a/app/src/main/java/com/smlnskgmail/jaman/remotetemperaturecontrol/logic/deviceselector/BtDevicesBottomSheet.kt b/app/src/main/java/com/smlnskgmail/jaman/remotetemperaturecontrol/logic/deviceselector/BtDevicesBottomSheet.kt index 4ae4bc8..81bd2f9 100644 --- a/app/src/main/java/com/smlnskgmail/jaman/remotetemperaturecontrol/logic/deviceselector/BtDevicesBottomSheet.kt +++ b/app/src/main/java/com/smlnskgmail/jaman/remotetemperaturecontrol/logic/deviceselector/BtDevicesBottomSheet.kt @@ -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 diff --git a/app/src/main/java/com/smlnskgmail/jaman/remotetemperaturecontrol/logic/monitor/MonitorFragment.kt b/app/src/main/java/com/smlnskgmail/jaman/remotetemperaturecontrol/logic/monitor/MonitorFragment.kt index 9aed384..1b2c460 100644 --- a/app/src/main/java/com/smlnskgmail/jaman/remotetemperaturecontrol/logic/monitor/MonitorFragment.kt +++ b/app/src/main/java/com/smlnskgmail/jaman/remotetemperaturecontrol/logic/monitor/MonitorFragment.kt @@ -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 @@ -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 @@ -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() } @@ -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 } @@ -186,7 +206,7 @@ class MonitorFragment : BaseFragment(), BtMonitorTarget { showBottomSheet(settingsBottomSheet) } - override fun handleBtInOnPause() { + override fun btOnPause() { monitorBtConnection!!.handleOnResume() } diff --git a/app/src/main/java/com/smlnskgmail/jaman/remotetemperaturecontrol/logic/monitor/api/BtConnection.kt b/app/src/main/java/com/smlnskgmail/jaman/remotetemperaturecontrol/logic/monitor/api/BtConnection.kt index c14bd49..d66988d 100644 --- a/app/src/main/java/com/smlnskgmail/jaman/remotetemperaturecontrol/logic/monitor/api/BtConnection.kt +++ b/app/src/main/java/com/smlnskgmail/jaman/remotetemperaturecontrol/logic/monitor/api/BtConnection.kt @@ -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) } diff --git a/app/src/main/java/com/smlnskgmail/jaman/remotetemperaturecontrol/logic/monitor/api/BtMonitor.kt b/app/src/main/java/com/smlnskgmail/jaman/remotetemperaturecontrol/logic/monitor/api/BtMonitor.kt index 1f2fc5a..683b227 100644 --- a/app/src/main/java/com/smlnskgmail/jaman/remotetemperaturecontrol/logic/monitor/api/BtMonitor.kt +++ b/app/src/main/java/com/smlnskgmail/jaman/remotetemperaturecontrol/logic/monitor/api/BtMonitor.kt @@ -3,7 +3,7 @@ package com.smlnskgmail.jaman.remotetemperaturecontrol.logic.monitor.api interface BtMonitor { fun onNewDataAvailable( - btMonitorSignalType: BtMonitorSignalType, + signalType: BtMonitorSignalType, rawData: String ) diff --git a/app/src/main/java/com/smlnskgmail/jaman/remotetemperaturecontrol/logic/monitor/api/BtMonitorSignalType.kt b/app/src/main/java/com/smlnskgmail/jaman/remotetemperaturecontrol/logic/monitor/api/BtMonitorSignalType.kt index f924715..6064b07 100644 --- a/app/src/main/java/com/smlnskgmail/jaman/remotetemperaturecontrol/logic/monitor/api/BtMonitorSignalType.kt +++ b/app/src/main/java/com/smlnskgmail/jaman/remotetemperaturecontrol/logic/monitor/api/BtMonitorSignalType.kt @@ -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 } diff --git a/app/src/main/java/com/smlnskgmail/jaman/remotetemperaturecontrol/logic/monitor/api/entities/targets/BtConnectTarget.kt b/app/src/main/java/com/smlnskgmail/jaman/remotetemperaturecontrol/logic/monitor/api/entities/targets/BtConnectTarget.kt index dcf9b41..29e2914 100644 --- a/app/src/main/java/com/smlnskgmail/jaman/remotetemperaturecontrol/logic/monitor/api/entities/targets/BtConnectTarget.kt +++ b/app/src/main/java/com/smlnskgmail/jaman/remotetemperaturecontrol/logic/monitor/api/entities/targets/BtConnectTarget.kt @@ -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 + ) } diff --git a/app/src/main/java/com/smlnskgmail/jaman/remotetemperaturecontrol/logic/monitor/api/entities/targets/BtPauseTarget.kt b/app/src/main/java/com/smlnskgmail/jaman/remotetemperaturecontrol/logic/monitor/api/entities/targets/BtPauseTarget.kt index 437f7dd..6e1de13 100644 --- a/app/src/main/java/com/smlnskgmail/jaman/remotetemperaturecontrol/logic/monitor/api/entities/targets/BtPauseTarget.kt +++ b/app/src/main/java/com/smlnskgmail/jaman/remotetemperaturecontrol/logic/monitor/api/entities/targets/BtPauseTarget.kt @@ -2,6 +2,6 @@ package com.smlnskgmail.jaman.remotetemperaturecontrol.logic.monitor.api.entitie interface BtPauseTarget { - fun handleBtInOnPause() + fun btOnPause() } diff --git a/app/src/main/java/com/smlnskgmail/jaman/remotetemperaturecontrol/logic/monitor/impl/debugbt/DebugBtConnection.kt b/app/src/main/java/com/smlnskgmail/jaman/remotetemperaturecontrol/logic/monitor/impl/debugbt/DebugBtConnection.kt index 931a90d..683d986 100644 --- a/app/src/main/java/com/smlnskgmail/jaman/remotetemperaturecontrol/logic/monitor/impl/debugbt/DebugBtConnection.kt +++ b/app/src/main/java/com/smlnskgmail/jaman/remotetemperaturecontrol/logic/monitor/impl/debugbt/DebugBtConnection.kt @@ -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 { @@ -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] @@ -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})" + ) + } + } diff --git a/app/src/main/java/com/smlnskgmail/jaman/remotetemperaturecontrol/logic/monitor/impl/debugbt/DebugBtMonitor.kt b/app/src/main/java/com/smlnskgmail/jaman/remotetemperaturecontrol/logic/monitor/impl/debugbt/DebugBtMonitor.kt index 1b42c80..e1349f3 100644 --- a/app/src/main/java/com/smlnskgmail/jaman/remotetemperaturecontrol/logic/monitor/impl/debugbt/DebugBtMonitor.kt +++ b/app/src/main/java/com/smlnskgmail/jaman/remotetemperaturecontrol/logic/monitor/impl/debugbt/DebugBtMonitor.kt @@ -9,11 +9,11 @@ class DebugBtMonitor( ) : BtMonitor { override fun onNewDataAvailable( - btMonitorSignalType: BtMonitorSignalType, + signalType: BtMonitorSignalType, rawData: String ) { @Suppress("UseCheckOrError") - when (btMonitorSignalType) { + when (signalType) { BtMonitorSignalType.Temperature -> { btMonitorTarget.temperatureAvailable( rawData @@ -45,7 +45,7 @@ class DebugBtMonitor( ) } else -> throw IllegalStateException( - "DebugMonitor cannot support $btMonitorSignalType!" + "DebugMonitor cannot support $signalType!" ) } } diff --git a/app/src/main/java/com/smlnskgmail/jaman/remotetemperaturecontrol/logic/monitor/impl/devicebt/DeviceBtConnection.kt b/app/src/main/java/com/smlnskgmail/jaman/remotetemperaturecontrol/logic/monitor/impl/devicebt/DeviceBtConnection.kt index 35648e0..9ef9158 100644 --- a/app/src/main/java/com/smlnskgmail/jaman/remotetemperaturecontrol/logic/monitor/impl/devicebt/DeviceBtConnection.kt +++ b/app/src/main/java/com/smlnskgmail/jaman/remotetemperaturecontrol/logic/monitor/impl/devicebt/DeviceBtConnection.kt @@ -13,9 +13,11 @@ class DeviceBtConnection( btAdapter: BluetoothAdapter, deviceMacAddress: String, private val btMonitor: BtMonitor -) : BtConnection() { +) : BtConnection { - private val btUuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB") + private val btUuid = UUID.fromString( + "00001101-0000-1000-8000-00805F9B34FB" + ) private var btSocket: BluetoothSocket? @@ -29,32 +31,14 @@ class DeviceBtConnection( btSocket = btDevice.createRfcommSocketToServiceRecord(btUuid) } - override fun run() { - runRead() - while (canRead()) { - read() - } - } - - private fun read() { - if (inputStreamIsOpen()) { - val rawData = inputStream!!.bufferedReader().readLine() - val signalType = - DeviceBtDataExtractor.signalType( - rawData - ) - val data = - DeviceBtDataExtractor.data( - rawData - ) - btMonitor.onNewDataAvailable(signalType, data) - } - } - private fun inputStreamIsOpen() = inputStream!!.bufferedReader().ready() override fun send(btMonitorSignalType: BtMonitorSignalType) { - outputStream!!.write(BtMonitorSignalType.signalOf(btMonitorSignalType).toByteArray()) + outputStream!!.write( + DeviceBtDataExtractor.signalOf( + btMonitorSignalType + ).toByteArray() + ) } override fun connect() { @@ -63,8 +47,25 @@ class DeviceBtConnection( inputStream = btSocket!!.inputStream outputStream = btSocket!!.outputStream } + runRead() + while (canRead()) { + read() + } + } + private fun read() { + if (inputStreamIsOpen()) { + val rawData = inputStream!!.bufferedReader().readLine() + val signalType = DeviceBtDataExtractor.signalType( + rawData + ) + val data = DeviceBtDataExtractor.data( + rawData + ) + btMonitor.onNewDataAvailable(signalType, data) + } } + override fun disconnect() { if (btSocket!!.isConnected) { isRunning = false diff --git a/app/src/main/java/com/smlnskgmail/jaman/remotetemperaturecontrol/logic/monitor/impl/devicebt/DeviceBtDataExtractor.kt b/app/src/main/java/com/smlnskgmail/jaman/remotetemperaturecontrol/logic/monitor/impl/devicebt/DeviceBtDataExtractor.kt index 21912f6..b4bd216 100644 --- a/app/src/main/java/com/smlnskgmail/jaman/remotetemperaturecontrol/logic/monitor/impl/devicebt/DeviceBtDataExtractor.kt +++ b/app/src/main/java/com/smlnskgmail/jaman/remotetemperaturecontrol/logic/monitor/impl/devicebt/DeviceBtDataExtractor.kt @@ -6,7 +6,7 @@ object DeviceBtDataExtractor { fun signalType(rawData: String): BtMonitorSignalType { if (rawData.isNotEmpty()) { - return BtMonitorSignalType.fromRawData(rawData) + return fromRawData(rawData) } return BtMonitorSignalType.Other } @@ -18,4 +18,32 @@ object DeviceBtDataExtractor { return rawData.substring(1) } + private fun fromRawData(rawData: String): BtMonitorSignalType { + return when (rawData[0].toString()) { + "t" -> BtMonitorSignalType.Temperature + "i" -> BtMonitorSignalType.TemperatureMinimum + "m" -> BtMonitorSignalType.TemperatureMaximum + "h" -> BtMonitorSignalType.Humidity + "q" -> BtMonitorSignalType.HumidityMinimum + "w" -> BtMonitorSignalType.HumidityMaximum + "r" -> BtMonitorSignalType.Reset + else -> BtMonitorSignalType.Other + } + } + + fun signalOf( + btMonitorSignalType: BtMonitorSignalType + ): String { + return when (btMonitorSignalType) { + BtMonitorSignalType.Temperature -> "t" + BtMonitorSignalType.TemperatureMinimum -> "i" + BtMonitorSignalType.TemperatureMaximum -> "m" + BtMonitorSignalType.Humidity -> "h" + BtMonitorSignalType.HumidityMinimum -> "q" + BtMonitorSignalType.HumidityMaximum -> "w" + BtMonitorSignalType.Reset -> "r" + else -> "" + } + } + } diff --git a/app/src/main/java/com/smlnskgmail/jaman/remotetemperaturecontrol/logic/monitor/impl/devicebt/DeviceBtMonitor.kt b/app/src/main/java/com/smlnskgmail/jaman/remotetemperaturecontrol/logic/monitor/impl/devicebt/DeviceBtMonitor.kt index 3fc921e..b7266e2 100644 --- a/app/src/main/java/com/smlnskgmail/jaman/remotetemperaturecontrol/logic/monitor/impl/devicebt/DeviceBtMonitor.kt +++ b/app/src/main/java/com/smlnskgmail/jaman/remotetemperaturecontrol/logic/monitor/impl/devicebt/DeviceBtMonitor.kt @@ -11,10 +11,10 @@ class DeviceBtMonitor( @Suppress("NON_EXHAUSTIVE_WHEN") override fun onNewDataAvailable( - btMonitorSignalType: BtMonitorSignalType, + signalType: BtMonitorSignalType, rawData: String ) { - when(btMonitorSignalType) { + when(signalType) { BtMonitorSignalType.Temperature -> { btMonitorTarget.temperatureAvailable( temperatureResult(rawData) diff --git a/app/src/main/java/com/smlnskgmail/jaman/remotetemperaturecontrol/logic/settings/SettingsBottomSheet.kt b/app/src/main/java/com/smlnskgmail/jaman/remotetemperaturecontrol/logic/settings/SettingsBottomSheet.kt index 3678430..8c3f90c 100644 --- a/app/src/main/java/com/smlnskgmail/jaman/remotetemperaturecontrol/logic/settings/SettingsBottomSheet.kt +++ b/app/src/main/java/com/smlnskgmail/jaman/remotetemperaturecontrol/logic/settings/SettingsBottomSheet.kt @@ -1,7 +1,7 @@ package com.smlnskgmail.jaman.remotetemperaturecontrol.logic.settings 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.monitor.api.entities.targets.BtDisconnectTarget import kotlinx.android.synthetic.main.bottom_sheet_settings.* diff --git a/app/src/test/java/com/smlnskgmail/jaman/remotetemperaturecontrol/api/MonitorDataExtractorTest.kt b/app/src/test/java/com/smlnskgmail/jaman/remotetemperaturecontrol/api/MonitorDataExtractorTest.kt index 0384604..6df2213 100644 --- a/app/src/test/java/com/smlnskgmail/jaman/remotetemperaturecontrol/api/MonitorDataExtractorTest.kt +++ b/app/src/test/java/com/smlnskgmail/jaman/remotetemperaturecontrol/api/MonitorDataExtractorTest.kt @@ -9,53 +9,82 @@ class MonitorDataExtractorTest { @Test fun validateTemperatureParse() { - checkValidation(BtMonitorSignalType.Temperature) + checkValidation( + BtMonitorSignalType.Temperature + ) } @Test fun validateTemperatureMinimumParse() { - checkValidation(BtMonitorSignalType.TemperatureMinimum) + checkValidation( + BtMonitorSignalType.TemperatureMinimum + ) } @Test fun validateTemperatureMaximumParse() { - checkValidation(BtMonitorSignalType.TemperatureMaximum) + checkValidation( + BtMonitorSignalType.TemperatureMaximum + ) } @Test fun validateHumidityParse() { - checkValidation(BtMonitorSignalType.Humidity) + checkValidation( + BtMonitorSignalType.Humidity + ) } @Test fun validateHumidityMinimumParse() { - checkValidation(BtMonitorSignalType.HumidityMinimum) + checkValidation( + BtMonitorSignalType.HumidityMinimum + ) } @Test fun validateHumidityMaximumParse() { - checkValidation(BtMonitorSignalType.HumidityMaximum) + checkValidation( + BtMonitorSignalType.HumidityMaximum + ) } @Test fun validateResetRequired() { - checkValidation(BtMonitorSignalType.Reset, "") + checkValidation( + BtMonitorSignalType.Reset, + "" + ) } private fun checkValidation( btMonitorSignalType: BtMonitorSignalType, cleanResult: String = DEFAULT_CLEAN_RESULT ) { - val rawData = rawData(BtMonitorSignalType.signalOf(btMonitorSignalType), cleanResult) + val rawData = rawData( + DeviceBtDataExtractor.signalOf( + btMonitorSignalType + ), + cleanResult + ) val parsedSignalType = DeviceBtDataExtractor.signalType(rawData) - assertEquals(parsedSignalType, btMonitorSignalType) + assertEquals( + parsedSignalType, + btMonitorSignalType + ) val parsedData = DeviceBtDataExtractor.data(rawData) - assertEquals(parsedData, cleanResult) + assertEquals( + parsedData, + cleanResult + ) } - private fun rawData(signalType: String, cleanResult: String): String { + private fun rawData( + signalType: String, + cleanResult: String + ): String { return String.format("%s%s", signalType, cleanResult) } diff --git a/app/src/test/java/com/smlnskgmail/jaman/remotetemperaturecontrol/api/MonitorDataSendTest.kt b/app/src/test/java/com/smlnskgmail/jaman/remotetemperaturecontrol/api/MonitorDataSendTest.kt index ea80590..2c55aed 100644 --- a/app/src/test/java/com/smlnskgmail/jaman/remotetemperaturecontrol/api/MonitorDataSendTest.kt +++ b/app/src/test/java/com/smlnskgmail/jaman/remotetemperaturecontrol/api/MonitorDataSendTest.kt @@ -11,25 +11,30 @@ import java.io.PrintStream class MonitorDataSendTest { - private val monitorTarget = - FakeMonitorHandleTarget() - private val signalTarget = - FakeMonitor( - monitorTarget - ) + private val monitorTarget = FakeMonitorHandleTarget() + private val signalTarget = FakeMonitor( + monitorTarget + ) private val outputStream = ByteArrayOutputStream() @Before fun initializeOutput() { - System.setOut(PrintStream(outputStream)) + System.setOut( + PrintStream( + outputStream + ) + ) } @Test fun validateTemperatureSend() { val validResult = "34.5" - sendNewData(BtMonitorSignalType.Temperature, validResult) + sendNewData( + BtMonitorSignalType.Temperature, + validResult + ) checkOutput(validResult) } @@ -37,7 +42,10 @@ class MonitorDataSendTest { fun validateTemperatureMinimumSend() { val validResult = "29.1" - sendNewData(BtMonitorSignalType.TemperatureMinimum, validResult) + sendNewData( + BtMonitorSignalType.TemperatureMinimum, + validResult + ) checkOutput(validResult) } @@ -45,7 +53,10 @@ class MonitorDataSendTest { fun validateTemperatureMaximumSend() { val validResult = "35.9" - sendNewData(BtMonitorSignalType.TemperatureMaximum, validResult) + sendNewData( + BtMonitorSignalType.TemperatureMaximum, + validResult + ) checkOutput(validResult) } @@ -53,7 +64,10 @@ class MonitorDataSendTest { fun validateHumiditySend() { val validResult = "74.2" - sendNewData(BtMonitorSignalType.Humidity, validResult) + sendNewData( + BtMonitorSignalType.Humidity, + validResult + ) checkOutput(validResult) } @@ -61,7 +75,10 @@ class MonitorDataSendTest { fun validateHumidityMinimumSend() { val validResult = "54.6" - sendNewData(BtMonitorSignalType.HumidityMinimum, validResult) + sendNewData( + BtMonitorSignalType.HumidityMinimum, + validResult + ) checkOutput(validResult) } @@ -69,7 +86,10 @@ class MonitorDataSendTest { fun validateHumidityMaximumSend() { val validResult = "89.3" - sendNewData(BtMonitorSignalType.HumidityMaximum, validResult) + sendNewData( + BtMonitorSignalType.HumidityMaximum, + validResult + ) checkOutput(validResult) } @@ -77,16 +97,28 @@ class MonitorDataSendTest { fun validateResetRequiredSend() { val validResult = "" - sendNewData(BtMonitorSignalType.Reset, validResult) + sendNewData( + BtMonitorSignalType.Reset, + validResult + ) checkOutput(validResult) } - private fun sendNewData(btMonitorSignalType: BtMonitorSignalType, data: String) { - signalTarget.onNewDataAvailable(btMonitorSignalType, data) + private fun sendNewData( + btMonitorSignalType: BtMonitorSignalType, + data: String + ) { + signalTarget.onNewDataAvailable( + btMonitorSignalType, + data + ) } private fun checkOutput(validOutput: String) { - assertEquals(validOutput, outputStream.toString()) + assertEquals( + validOutput, + outputStream.toString() + ) } } diff --git a/app/src/test/java/com/smlnskgmail/jaman/remotetemperaturecontrol/api/fakemonitor/FakeMonitor.kt b/app/src/test/java/com/smlnskgmail/jaman/remotetemperaturecontrol/api/fakemonitor/FakeMonitor.kt index ae05778..6b6b229 100644 --- a/app/src/test/java/com/smlnskgmail/jaman/remotetemperaturecontrol/api/fakemonitor/FakeMonitor.kt +++ b/app/src/test/java/com/smlnskgmail/jaman/remotetemperaturecontrol/api/fakemonitor/FakeMonitor.kt @@ -4,14 +4,16 @@ import com.smlnskgmail.jaman.remotetemperaturecontrol.logic.monitor.api.BtMonito import com.smlnskgmail.jaman.remotetemperaturecontrol.logic.monitor.api.BtMonitorSignalType import com.smlnskgmail.jaman.remotetemperaturecontrol.logic.monitor.api.BtMonitorTarget -class FakeMonitor(private val btMonitorTarget: BtMonitorTarget) : - BtMonitor { +class FakeMonitor( + private val btMonitorTarget: BtMonitorTarget +) : BtMonitor { override fun onNewDataAvailable( - btMonitorSignalType: BtMonitorSignalType, + signalType: BtMonitorSignalType, rawData: String ) { - when (btMonitorSignalType) { + @Suppress("NON_EXHAUSTIVE_WHEN") + when (signalType) { BtMonitorSignalType.Temperature -> { btMonitorTarget.temperatureAvailable(rawData) } diff --git a/app/src/test/java/com/smlnskgmail/jaman/remotetemperaturecontrol/api/fakemonitor/FakeMonitorHandleTarget.kt b/app/src/test/java/com/smlnskgmail/jaman/remotetemperaturecontrol/api/fakemonitor/FakeMonitorHandleTarget.kt index eb45840..874f204 100644 --- a/app/src/test/java/com/smlnskgmail/jaman/remotetemperaturecontrol/api/fakemonitor/FakeMonitorHandleTarget.kt +++ b/app/src/test/java/com/smlnskgmail/jaman/remotetemperaturecontrol/api/fakemonitor/FakeMonitorHandleTarget.kt @@ -2,8 +2,7 @@ package com.smlnskgmail.jaman.remotetemperaturecontrol.api.fakemonitor import com.smlnskgmail.jaman.remotetemperaturecontrol.logic.monitor.api.BtMonitorTarget -class FakeMonitorHandleTarget : - BtMonitorTarget { +class FakeMonitorHandleTarget : BtMonitorTarget { override fun temperatureAvailable(data: String) { printText(data)