[U] Update usage in service
This commit is contained in:
@@ -12,11 +12,12 @@ import com.influxdb.client.kotlin.InfluxDBClientKotlin
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.SupervisorJob
|
||||
import kotlinx.coroutines.channels.Channel
|
||||
import kotlinx.coroutines.flow.consumeAsFlow
|
||||
import kotlinx.coroutines.launch
|
||||
import org.hydev.wearsync.BatteryInfo.Companion.batteryInfo
|
||||
import org.hydev.wearsync.bles.BluetoothHandler.Companion.ble
|
||||
import org.hydev.wearsync.bles.decoders.BatteryDecoder
|
||||
import org.hydev.wearsync.bles.decoders.HeartRateDecoder
|
||||
import org.hydev.wearsync.bles.decoders.IDecoder
|
||||
import timber.log.Timber
|
||||
|
||||
|
||||
@@ -41,13 +42,13 @@ class MyService : Service()
|
||||
|
||||
fun startCollect()
|
||||
{
|
||||
collect(ble.heartRateChannel)
|
||||
collect(ble.batteryChannel)
|
||||
observe<BatteryDecoder>()
|
||||
observe<HeartRateDecoder>()
|
||||
}
|
||||
|
||||
private fun <T : Any> collect(channel: Channel<T>) {
|
||||
scope.launch {
|
||||
channel.consumeAsFlow().collect {
|
||||
private inline fun <reified T : IDecoder<out Any>> observe() {
|
||||
ble.observeAny<T> {
|
||||
scope.launch {
|
||||
try {
|
||||
println("Adding ${it.javaClass.simpleName} to influxdb")
|
||||
influx add it
|
||||
|
||||
@@ -33,11 +33,15 @@ internal class BluetoothHandler private constructor(context: Context) {
|
||||
val listeners = HashMap<KClass<*>, MutableList<(Any) -> Unit>>()
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
inline fun <M : Any, reified D : IDecoder<M>> observe(crossinline cb: (M) -> Unit) {
|
||||
inline fun <M : Any, reified D : IDecoder<out M>> observe(crossinline cb: (M) -> Unit) {
|
||||
(listeners[D::class] ?: error("Cannot observe unknown decoder class ${D::class}"))
|
||||
.add { cb(it as M) }
|
||||
}
|
||||
|
||||
inline fun <reified D : IDecoder<out Any>> observeAny(noinline cb: (Any) -> Unit) {
|
||||
(listeners[D::class] ?: error("Cannot observe unknown decoder class ${D::class}")).add(cb)
|
||||
}
|
||||
|
||||
private suspend fun <T : Any> BluePeri.observe(dec: IDecoder<T>) {
|
||||
val cls = dec::class
|
||||
listeners[cls] = ArrayList()
|
||||
|
||||
Reference in New Issue
Block a user