Skip to content

Commit

Permalink
refactor all the rest of the handlers + services into PebbleDevice 😓
Browse files Browse the repository at this point in the history
  • Loading branch information
crc-32 committed Sep 23, 2024
1 parent 0c7bf7f commit 0095991
Show file tree
Hide file tree
Showing 56 changed files with 459 additions and 833 deletions.
2 changes: 1 addition & 1 deletion android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@
</provider>

<provider
android:name=".providers.PebbleKitProvider"
android:name=".shared.providers.PebbleKitProvider"
android:authorities="${pebbleKitProviderAuthority}"
android:exported="true"
tools:ignore="ExportedContentProvider" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import io.rebble.cobble.di.AppComponent
import io.rebble.cobble.di.DaggerAppComponent
import io.rebble.cobble.log.AppTaggedDebugTree
import io.rebble.cobble.log.FileLoggingTree
import io.rebble.cobble.shared.database.closeDatabase
import io.rebble.cobble.shared.di.initKoin
import timber.log.Timber
import kotlin.system.exitProcess
Expand All @@ -27,7 +26,6 @@ class CobbleApplication : FlutterApplication() {
initKoin(applicationContext)

component.initNotificationChannels()
component.initLibPebbleCommonServices()

beginConnectingToDefaultWatch()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class ConnectionLooper @Inject constructor(
val connectionScope = CoroutineScope(SupervisorJob() + errorHandler + Dispatchers.IO) + CoroutineName("ConnectionScope-$macAddress")
try {
blueCommon.startSingleWatchConnection(macAddress).collect {
if (it is SingleConnectionStatus.Connected && connectionState.value !is ConnectionState.Connected && connectionState.value !is ConnectionState.RecoveryMode) {
if (it is SingleConnectionStatus.Connected /*&& connectionState.value !is ConnectionState.Connected && connectionState.value !is ConnectionState.RecoveryMode*/) {
// initial connection, wait on negotiation
_connectionState.value = ConnectionState.Negotiating(it.watch)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,14 @@ import io.rebble.cobble.bluetooth.classic.BlueSerialDriver
import io.rebble.cobble.bluetooth.classic.SocketSerialDriver
import io.rebble.cobble.bluetooth.scan.BleScanner
import io.rebble.cobble.bluetooth.scan.ClassicScanner
import io.rebble.cobble.datasources.FlutterPreferences
import io.rebble.cobble.shared.datastore.FlutterPreferences
import io.rebble.cobble.datasources.IncomingPacketsListener
import io.rebble.cobble.shared.domain.common.PebbleDevice
import io.rebble.libpebblecommon.ProtocolHandler
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.FlowPreview
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.onCompletion
import org.koin.mp.KoinPlatformTools
import timber.log.Timber
import javax.inject.Inject
import javax.inject.Singleton
Expand All @@ -31,7 +30,6 @@ class DeviceTransport @Inject constructor(
private val context: Context,
private val bleScanner: BleScanner,
private val classicScanner: ClassicScanner,
private val protocolHandler: ProtocolHandler,
private val flutterPreferences: FlutterPreferences,
private val incomingPacketsListener: IncomingPacketsListener
) {
Expand Down Expand Up @@ -59,23 +57,25 @@ class DeviceTransport @Inject constructor(
lastMacAddress = macAddress

val bluetoothDevice = if (BuildConfig.DEBUG && !macAddress.contains(":")) {
EmulatedPebbleDevice(macAddress, protocolHandler)
EmulatedPebbleDevice(macAddress)
} else {
val bluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
BluetoothPebbleDevice(bluetoothAdapter.getRemoteDevice(macAddress), protocolHandler, macAddress)
BluetoothPebbleDevice(bluetoothAdapter.getRemoteDevice(macAddress), macAddress)
}

val driver = getTargetTransport(bluetoothDevice)
this@DeviceTransport.driver = driver
return driver.startSingleWatchConnection(bluetoothDevice)
return driver.startSingleWatchConnection(bluetoothDevice).onCompletion {
bluetoothDevice.close()
}
}

@Throws(SecurityException::class)
private fun getTargetTransport(pebbleDevice: PebbleDevice): BlueIO {
return when (pebbleDevice) {
is EmulatedPebbleDevice -> {
SocketSerialDriver(
protocolHandler,
pebbleDevice,
incomingPacketsListener.receivedPackets
)
}
Expand All @@ -90,7 +90,7 @@ class DeviceTransport @Inject constructor(
}
BlueLEDriver(
context = context,
protocolHandler = protocolHandler,
pebbleDevice = pebbleDevice,
gattServerManager = gattServerManager,
incomingPacketsListener = incomingPacketsListener.receivedPackets,
) {
Expand All @@ -100,7 +100,7 @@ class DeviceTransport @Inject constructor(

BluetoothDevice.DEVICE_TYPE_CLASSIC, BluetoothDevice.DEVICE_TYPE_DUAL -> { // Serial only device or serial/LE
BlueSerialDriver(
protocolHandler,
pebbleDevice,
incomingPacketsListener.receivedPackets
)
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@ package io.rebble.cobble.bridges.common

import io.rebble.cobble.bridges.FlutterBridge
import io.rebble.cobble.bridges.ui.BridgeLifecycleController
import io.rebble.cobble.middleware.AppLogController
import io.rebble.cobble.shared.middleware.AppLogController
import io.rebble.cobble.pigeons.Pigeons
import io.rebble.cobble.shared.domain.state.ConnectionStateManager
import io.rebble.cobble.shared.domain.state.watchOrNull
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.filterNotNull
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.launch
import timber.log.Timber
import javax.inject.Inject

class AppLogFlutterBridge @Inject constructor(
bridgeLifecycleController: BridgeLifecycleController,
private val coroutineScope: CoroutineScope,
private val appLogController: AppLogController
bridgeLifecycleController: BridgeLifecycleController
) : FlutterBridge, Pigeons.AppLogControl {
private val callbacks = bridgeLifecycleController.createCallbacks(Pigeons::AppLogCallbacks)

Expand All @@ -25,21 +27,29 @@ class AppLogFlutterBridge @Inject constructor(

override fun startSendingLogs() {
stopSendingLogs()

logsJob = coroutineScope.launch {
appLogController.logs.collect {
Timber.d("Received in pigeon '%s'", it.message.get())
callbacks.onLogReceived(
Pigeons.AppLogEntry.Builder()
.setUuid(it.uuid.get().toString())
.setTimestamp(it.timestamp.get().toLong())
.setLevel(it.level.get().toLong())
.setLineNumber(it.lineNumber.get().toLong())
.setFilename(it.filename.get())
.setMessage(it.message.get())
.build()

) {}
val pebbleDevice = ConnectionStateManager.connectionState.value.watchOrNull
?: run {
Timber.e("No app log service available")
return
}
pebbleDevice.negotiationScope.launch {
val connectionScope = pebbleDevice.connectionScope.filterNotNull().first()
logsJob = connectionScope.launch {
val appLogController = AppLogController(pebbleDevice)
appLogController.logs.collect {
Timber.d("Received in pigeon '%s'", it.message.get())
callbacks.onLogReceived(
Pigeons.AppLogEntry.Builder()
.setUuid(it.uuid.get().toString())
.setTimestamp(it.timestamp.get().toLong())
.setLevel(it.level.get().toLong())
.setLineNumber(it.lineNumber.get().toLong())
.setFilename(it.filename.get())
.setMessage(it.message.get())
.build()

) {}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ import io.rebble.cobble.shared.domain.state.watchOrNull
import io.rebble.libpebblecommon.ProtocolHandler
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onEach
import javax.inject.Inject

class ConnectionFlutterBridge @Inject constructor(
bridgeLifecycleController: BridgeLifecycleController,
private val connectionLooper: ConnectionLooper,
private val coroutineScope: CoroutineScope,
private val protocolHandler: ProtocolHandler
private val coroutineScope: CoroutineScope
) : FlutterBridge, Pigeons.ConnectionControl {
private val connectionCallbacks = bridgeLifecycleController
.createCallbacks(Pigeons::ConnectionCallbacks)
Expand All @@ -41,19 +42,14 @@ class ConnectionFlutterBridge @Inject constructor(

@Suppress("UNCHECKED_CAST")
override fun sendRawPacket(arg: Pigeons.ListWrapper) {
coroutineScope.launch {
val byteArray = (arg.value as List<Number>).map { it.toByte().toUByte() }.toUByteArray()
protocolHandler.send(byteArray)
}
error("Deprecated")
}

override fun observeConnectionChanges() {
statusObservingJob = coroutineScope.launch(Dispatchers.Main) {
combine(
connectionLooper.connectionState,
ConnectionStateManager.connectedWatchMetadata,
) { connectionState, watchMetadata ->
ConnectionStateManager.connectionState.map { connectionState ->
val bluetoothDevice = connectionState.watchOrNull
val watchMetadata = connectionState.watchOrNull?.metadata?.value
val model = watchMetadata?.running?.hardwarePlatform?.get()?.toInt()
Pigeons.WatchConnectionStatePigeon.Builder()
.setIsConnected(connectionState is ConnectionState.Connected ||
Expand Down
Loading

0 comments on commit 0095991

Please sign in to comment.