Skip to content

Commit

Permalink
Add device status
Browse files Browse the repository at this point in the history
  • Loading branch information
pipe01 committed Mar 27, 2024
1 parent c12a80a commit 5ff80a0
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 18 deletions.
22 changes: 16 additions & 6 deletions app/src/main/java/net/pipe01/pinepartner/devices/Device.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ class Device private constructor(
private val client: ClientBleGatt,
private var services: ClientBleGattServices,
) {
enum class Status {
CONNECTED,
DISCONNECTED,
RECONNECTING,
}

private val TAG = "Device"

private val callMutex = Mutex()
Expand All @@ -52,8 +58,12 @@ class Device private constructor(
val mtu
get() = client.mtu.value

val isConnected
get() = client.isConnected
val status
get() = when {
client.isConnected -> Status.CONNECTED
reconnect -> Status.RECONNECTING
else -> Status.DISCONNECTED
}

companion object {
suspend fun connect(context: Context, coroutineScope: CoroutineScope, address: String): Device {
Expand All @@ -77,7 +87,7 @@ class Device private constructor(

init {
reconnectTimer.scheduleAtFixedRate(5000, 5000) {
if (!isConnected && reconnect) {
if (!client.isConnected && reconnect) {
Log.i(TAG, "Reconnecting to device")

try {
Expand Down Expand Up @@ -134,7 +144,7 @@ class Device private constructor(
}

private suspend fun <T> doCall(fn: suspend () -> T): T {
if (!isConnected) throw IllegalStateException("Device not connected")
if (!client.isConnected) throw IllegalStateException("Device not connected")

return callMutex.withLock { fn() }
}
Expand Down Expand Up @@ -191,7 +201,7 @@ class Device private constructor(
write(InfiniTime.CurrentTimeService.CURRENT_TIME, bytes)
}

suspend fun setCurrentWeather(weather: CurrentWeather) {
suspend fun setCurrentWeather(weather: CurrentWeather) = doCall {
val buffer = ByteBuffer.allocate(49).order(ByteOrder.LITTLE_ENDIAN)

// We want local timestamp, not UTC timestamp
Expand All @@ -213,7 +223,7 @@ class Device private constructor(
write(InfiniTime.WeatherService.WEATHER_DATA, buffer.array())
}

suspend fun flashDFU(dfuFile: InputStream, coroutineScope: CoroutineScope, onProgress: (DFUProgress) -> Unit) {
suspend fun flashDFU(dfuFile: InputStream, coroutineScope: CoroutineScope, onProgress: (DFUProgress) -> Unit) = doCall {
val files = dfuFile.unzip()

val manifestJson = files["manifest.json"]?.decodeToString() ?: throw IllegalArgumentException("No manifest.json found")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ import android.os.Parcelable
import kotlinx.parcelize.Parcelize

@Parcelize
class WatchState(val isConnected: Boolean, val firmwareVersion: String, val batteryLevel: Float) : Parcelable
class WatchState(val status: Device.Status, val firmwareVersion: String, val batteryLevel: Float) : Parcelable
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import net.pipe01.pinepartner.components.Header
import net.pipe01.pinepartner.components.LoadingStandIn
import net.pipe01.pinepartner.data.AppDatabase
import net.pipe01.pinepartner.data.Watch
import net.pipe01.pinepartner.devices.Device
import net.pipe01.pinepartner.devices.WatchState
import net.pipe01.pinepartner.service.BackgroundService
import net.pipe01.pinepartner.utils.composables.BoxWithFAB
Expand Down Expand Up @@ -199,14 +200,16 @@ private fun DeviceItem(
)

Text(
text = if (state?.isConnected == true)
"Connected, battery: %.0f%%".format(state!!.batteryLevel * 100)
else
"Not connected",
text = when (state?.status) {
Device.Status.CONNECTED -> "Connected, battery: %.0f%%".format(state!!.batteryLevel * 100)
Device.Status.RECONNECTING -> "Reconnecting..."
Device.Status.DISCONNECTED -> "Disconnected"
else -> "Unknown status"
}
)
}

if (state?.isConnected == true) {
if (state?.status == Device.Status.CONNECTED) {
Button(onClick = {
coroutineScope.launch {
backgroundService.disconnectWatch(watch.address).onFailure {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class WatchAdapter : ApiScriptableObject(WatchAdapter::class) {
fun getAddress() = device.address

@JSGetter
fun getIsConnected() = device.isConnected
fun getIsConnected() = device.status == Device.Status.CONNECTED

@JSFunction
fun sendNotification(title: String, body: String) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import net.pipe01.pinepartner.MainActivity
import net.pipe01.pinepartner.NotificationReceivedAction
import net.pipe01.pinepartner.R
import net.pipe01.pinepartner.data.AppDatabase
import net.pipe01.pinepartner.devices.Device
import net.pipe01.pinepartner.devices.WatchState
import net.pipe01.pinepartner.devices.blefs.createFolder
import net.pipe01.pinepartner.devices.blefs.deleteFile
Expand Down Expand Up @@ -248,10 +249,10 @@ class BackgroundService : Service() {

Log.d(TAG, "Get watch $address state")

if (device?.isConnected == true)
WatchState(true, device.getFirmwareRevision(), device.getBatteryLevel())
if (device?.status == Device.Status.CONNECTED)
WatchState(device.status, device.getFirmwareRevision(), device.getBatteryLevel())
else
WatchState(false, "", 0f)
WatchState(device?.status ?: Device.Status.DISCONNECTED, "", 0f)
}

suspend fun startWatchDFU(jobId: Int, address: String, uri: Uri) = Result.runCatching {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class DeviceManager(private val context: Context) {
val deviceDisconnected = _deviceDisconnected.toEvent()

val connectedDevices: List<Device>
get() = devicesMap.values.filter { it.isConnected }
get() = devicesMap.values.filter { it.status == Device.Status.CONNECTED }

fun get(address: String) = devicesMap[address]

Expand All @@ -27,7 +27,7 @@ class DeviceManager(private val context: Context) {

val device = devicesMap[address]
if (device != null) {
if (!device.isConnected) {
if (device.status != Device.Status.CONNECTED) {
Log.d(TAG, "Cleaning up old device")
devicesMap.remove(address)
} else {
Expand Down

0 comments on commit 5ff80a0

Please sign in to comment.