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

Commit

Permalink
2020-02-05 Version 1.1.0: Added Debug Monitor implementation for test…
Browse files Browse the repository at this point in the history
…s and run the app without Arduino
  • Loading branch information
fartem committed Feb 5, 2020
1 parent c10dfbe commit f715178
Show file tree
Hide file tree
Showing 28 changed files with 342 additions and 189 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ android {
minSdkVersion 21
targetSdkVersion 29
versionCode 1
versionName "1.0.1"
versionName "1.1.0"
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package com.smlnskgmail.jaman.remotetemperaturecontrol
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import com.smlnskgmail.jaman.remotetemperaturecontrol.logic.monitor.btmonitor.connection.targets.BtPauseTarget
import com.smlnskgmail.jaman.remotetemperaturecontrol.logic.monitor.ui.MonitorHandleFragment
import com.smlnskgmail.jaman.remotetemperaturecontrol.logic.monitor.MonitorFragment
import com.smlnskgmail.jaman.remotetemperaturecontrol.logic.monitor.api.entities.targets.BtPauseTarget

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
showFragment(MonitorHandleFragment())
showFragment(MonitorFragment())
}

private fun showFragment(fragment: Fragment) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,10 @@ 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.btmonitor.connection.targets.BtPauseTarget
import com.smlnskgmail.jaman.remotetemperaturecontrol.logic.monitor.api.entities.targets.BtPauseTarget

abstract class BaseFragment : Fragment(), BtPauseTarget {

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

abstract fun initializeFragment(view: View)

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ 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.logic.deviceselector.recycler.BtDevicesAdapter
import com.smlnskgmail.jaman.remotetemperaturecontrol.logic.monitor.btmonitor.connection.entities.BtDevice
import com.smlnskgmail.jaman.remotetemperaturecontrol.logic.monitor.btmonitor.connection.targets.BtConnectTarget
import com.smlnskgmail.jaman.remotetemperaturecontrol.logic.monitor.api.entities.BtDevice
import com.smlnskgmail.jaman.remotetemperaturecontrol.logic.monitor.api.entities.targets.BtConnectTarget
import kotlinx.android.synthetic.main.bottom_sheet_bt_devices.*

class BtDevicesBottomSheet : BaseBottomSheet() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.smlnskgmail.jaman.remotetemperaturecontrol.logic.deviceselector.recy

import android.view.View
import androidx.recyclerview.widget.RecyclerView
import com.smlnskgmail.jaman.remotetemperaturecontrol.logic.monitor.btmonitor.connection.entities.BtDevice
import com.smlnskgmail.jaman.remotetemperaturecontrol.logic.monitor.api.entities.BtDevice
import kotlinx.android.synthetic.main.item_bt_device.view.*

class BtDeviceHolder(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.smlnskgmail.jaman.remotetemperaturecontrol.R
import com.smlnskgmail.jaman.remotetemperaturecontrol.logic.monitor.btmonitor.connection.entities.BtDevice
import com.smlnskgmail.jaman.remotetemperaturecontrol.logic.monitor.api.entities.BtDevice

class BtDevicesAdapter(
private val btDevices: List<BtDevice>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,41 +1,43 @@
package com.smlnskgmail.jaman.remotetemperaturecontrol.logic.monitor.ui
package com.smlnskgmail.jaman.remotetemperaturecontrol.logic.monitor

import android.annotation.SuppressLint
import android.bluetooth.BluetoothAdapter
import android.content.DialogInterface
import android.os.Bundle
import android.view.View
import android.widget.TextView
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.logic.deviceselector.BtDevicesBottomSheet
import com.smlnskgmail.jaman.remotetemperaturecontrol.logic.monitor.MonitorHandleTarget
import com.smlnskgmail.jaman.remotetemperaturecontrol.logic.monitor.MonitorSignalType
import com.smlnskgmail.jaman.remotetemperaturecontrol.logic.monitor.btmonitor.BtMonitor
import com.smlnskgmail.jaman.remotetemperaturecontrol.logic.monitor.btmonitor.connection.MonitorBtConnection
import com.smlnskgmail.jaman.remotetemperaturecontrol.logic.monitor.btmonitor.connection.entities.BtDevice
import com.smlnskgmail.jaman.remotetemperaturecontrol.logic.monitor.btmonitor.connection.targets.BtConnectTarget
import com.smlnskgmail.jaman.remotetemperaturecontrol.logic.monitor.btmonitor.connection.targets.BtDisconnectTarget
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
import com.smlnskgmail.jaman.remotetemperaturecontrol.logic.monitor.api.BtMonitorTarget
import com.smlnskgmail.jaman.remotetemperaturecontrol.logic.monitor.api.entities.BtDevice
import com.smlnskgmail.jaman.remotetemperaturecontrol.logic.monitor.api.entities.targets.BtConnectTarget
import com.smlnskgmail.jaman.remotetemperaturecontrol.logic.monitor.api.entities.targets.BtDisconnectTarget
import com.smlnskgmail.jaman.remotetemperaturecontrol.logic.monitor.impl.debugbt.DebugBtConnection
import com.smlnskgmail.jaman.remotetemperaturecontrol.logic.monitor.impl.debugbt.DebugBtMonitor
import com.smlnskgmail.jaman.remotetemperaturecontrol.logic.monitor.impl.devicebt.DeviceBtConnection
import com.smlnskgmail.jaman.remotetemperaturecontrol.logic.monitor.impl.devicebt.DeviceBtMonitor
import com.smlnskgmail.jaman.remotetemperaturecontrol.logic.settings.SettingsBottomSheet
import kotlinx.android.synthetic.main.fragment_monitor.*

@SuppressWarnings("TooManyFunctions")
class MonitorHandleFragment : BaseFragment(), MonitorHandleTarget {
class MonitorFragment : BaseFragment(), BtMonitorTarget {

private var monitorBtConnection: MonitorBtConnection? = null
private var monitorBtConnection: BtConnection? = null
private var btAdapter: BluetoothAdapter? = null

private var btMonitor: BtMonitor? = null

override fun initializeFragment(view: View) {
enableMonitor()
}

private fun enableMonitor() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
if (btIsEnabled()) {
btAdapter = BluetoothAdapter.getDefaultAdapter()
val btDevices = getBtDevices()
if (btDevices.isNotEmpty()) {
btMonitor = BtMonitor(this)
btMonitor = DeviceBtMonitor(this)
showDevicesList(btDevices)
} else {
showBtDevicesNotFoundWarning()
Expand All @@ -45,14 +47,26 @@ class MonitorHandleFragment : BaseFragment(), MonitorHandleTarget {
}
}

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

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

private fun btIsEnabled() = true

private fun showDevicesList(btDevices: List<BtDevice>) {
val devicesBottomSheet = BtDevicesBottomSheet()
devicesBottomSheet.setBtDevices(btDevices)
devicesBottomSheet.setBtDeviceSelectCallback(object : BtConnectTarget {
override fun onBtDeviceSelected(name: String, address: String) {
monitorBtConnection = MonitorBtConnection(
monitorBtConnection = DeviceBtConnection(
btAdapter!!,
address,
btMonitor!!
Expand All @@ -70,7 +84,10 @@ class MonitorHandleFragment : BaseFragment(), MonitorHandleTarget {
private fun getBtDevices(): List<BtDevice> {
val bondedDevices= btAdapter!!.bondedDevices
return bondedDevices.mapTo(ArrayList(bondedDevices.size)) {
BtDevice(it.name, it.address)
BtDevice(
it.name,
it.address
)
}
}

Expand Down Expand Up @@ -151,21 +168,19 @@ class MonitorHandleFragment : BaseFragment(), MonitorHandleTarget {

private fun initializeButtons() {
btn_reset_monitor.setOnClickListener {
monitorBtConnection!!.send(MonitorSignalType.Reset)
monitorBtConnection!!.send(BtMonitorSignalType.Reset)
}
btn_main_options.setOnClickListener {
showSettings()
}
}

private fun showSettings() {
val settingsBottomSheet =
SettingsBottomSheet()
val settingsBottomSheet = SettingsBottomSheet()
settingsBottomSheet.setBtDisconnectListener(object : BtDisconnectTarget {
override fun btDisconnect() {
monitorBtConnection!!.disconnect()
resetRequired()
enableMonitor()
}
})
showBottomSheet(settingsBottomSheet)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.smlnskgmail.jaman.remotetemperaturecontrol.logic.monitor.api

abstract class BtConnection : Thread() {

open fun connect() {

}

open fun disconnect() {

}

open fun handleOnResume() {

}

open fun send(btMonitorSignalType: BtMonitorSignalType) {

}

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.smlnskgmail.jaman.remotetemperaturecontrol.logic.monitor
package com.smlnskgmail.jaman.remotetemperaturecontrol.logic.monitor.api

interface Monitor {
interface BtMonitor {

fun onNewDataAvailable(
monitorSignalType: MonitorSignalType,
btMonitorSignalType: BtMonitorSignalType,
rawData: String
)

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

enum class MonitorSignalType {
enum class BtMonitorSignalType {

Temperature,
TemperatureMinimum,
Expand All @@ -13,7 +13,7 @@ enum class MonitorSignalType {

companion object {

fun fromRawData(rawData: String): MonitorSignalType {
fun fromRawData(rawData: String): BtMonitorSignalType {
return when (rawData[0].toString()) {
"t" -> Temperature
"i" -> TemperatureMinimum
Expand All @@ -26,8 +26,8 @@ enum class MonitorSignalType {
}
}

fun signalOf(monitorSignalType: MonitorSignalType): String {
return when (monitorSignalType) {
fun signalOf(btMonitorSignalType: BtMonitorSignalType): String {
return when (btMonitorSignalType) {
Temperature -> "t"
TemperatureMinimum -> "i"
TemperatureMaximum -> "m"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.smlnskgmail.jaman.remotetemperaturecontrol.logic.monitor
package com.smlnskgmail.jaman.remotetemperaturecontrol.logic.monitor.api

interface MonitorHandleTarget {
interface BtMonitorTarget {

fun temperatureAvailable(data: String)
fun temperatureMaximumAvailable(data: String)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.smlnskgmail.jaman.remotetemperaturecontrol.logic.monitor.btmonitor.connection.entities
package com.smlnskgmail.jaman.remotetemperaturecontrol.logic.monitor.api.entities

data class BtDevice(
val name: String,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.smlnskgmail.jaman.remotetemperaturecontrol.logic.monitor.btmonitor.connection.targets
package com.smlnskgmail.jaman.remotetemperaturecontrol.logic.monitor.api.entities.targets

interface BtConnectTarget {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.smlnskgmail.jaman.remotetemperaturecontrol.logic.monitor.btmonitor.connection.targets
package com.smlnskgmail.jaman.remotetemperaturecontrol.logic.monitor.api.entities.targets

interface BtDisconnectTarget {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.smlnskgmail.jaman.remotetemperaturecontrol.logic.monitor.btmonitor.connection.targets
package com.smlnskgmail.jaman.remotetemperaturecontrol.logic.monitor.api.entities.targets

interface BtPauseTarget {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package com.smlnskgmail.jaman.remotetemperaturecontrol.logic.monitor.impl.debugbt

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() {

companion object {

private val temperature = arrayListOf(
"25.3 C",
"24.6 C",
"23.5 C",
"21.2 C",
"22.4 C",
"23.3 C",
"24.7 C",
"25.1 C",
"25.6 C",
"25.7 C"
)

private val humidity = arrayListOf(
"78.1 %",
"79.4 %",
"77.3 %",
"77.7 %",
"77.8 %",
"78.4 %",
"79.5 %",
"81.6 %",
"79.4 %",
"77.5 %"
)

}

private var loopCounter = 0

override fun run() {
super.run()
while (true) {
@Suppress("MagicNumber")
sleep(5_000)
btMonitor.onNewDataAvailable(
BtMonitorSignalType.Temperature,
temperature[loopCounter]
)
btMonitor.onNewDataAvailable(
BtMonitorSignalType.TemperatureMaximum,
"25.7 C"
)
btMonitor.onNewDataAvailable(
BtMonitorSignalType.TemperatureMinimum,
"21.2 C"
)
btMonitor.onNewDataAvailable(
BtMonitorSignalType.Humidity,
humidity[loopCounter]
)
btMonitor.onNewDataAvailable(
BtMonitorSignalType.HumidityMaximum,
"81.6 %"
)
btMonitor.onNewDataAvailable(
BtMonitorSignalType.HumidityMinimum,
"77.3 %"
)
if (loopCounter < temperature.size - 1) {
loopCounter++
} else {
loopCounter = 0
}
}
}

}
Loading

0 comments on commit f715178

Please sign in to comment.