Catch exceptions

This commit is contained in:
Sylwester Zielinski
2023-05-03 16:11:25 +02:00
parent 736c753c5e
commit 8283d210cc
7 changed files with 41 additions and 6 deletions

View File

@@ -47,6 +47,7 @@ import no.nordicsemi.android.cgms.data.CGMServiceCommand
import no.nordicsemi.android.common.logger.NordicBlekLogger
import no.nordicsemi.android.kotlin.ble.client.main.callback.BleGattClient
import no.nordicsemi.android.kotlin.ble.client.main.connect
import no.nordicsemi.android.kotlin.ble.client.main.errors.GattOperationException
import no.nordicsemi.android.kotlin.ble.client.main.service.BleGattCharacteristic
import no.nordicsemi.android.kotlin.ble.client.main.service.BleGattServices
import no.nordicsemi.android.kotlin.ble.core.ServerDevice
@@ -173,6 +174,7 @@ internal class CGMService : NotificationService() {
batteryLevelCharacteristic.getNotifications()
.mapNotNull { BatteryLevelParser.parse(it) }
.onEach { repository.onBatteryLevelChanged(it) }
.catch { it.printStackTrace() }
.launchIn(lifecycleScope)
measurementCharacteristic.getNotifications()
@@ -190,6 +192,7 @@ internal class CGMService : NotificationService() {
repository.onDataReceived(result)
}
.catch { it.printStackTrace() }
.launchIn(lifecycleScope)
opsControlPointCharacteristic.getNotifications()
@@ -209,11 +212,13 @@ internal class CGMService : NotificationService() {
}
}
}
.catch { it.printStackTrace() }
.launchIn(lifecycleScope)
recordAccessControlPointCharacteristic.getNotifications()
.mapNotNull { RecordAccessControlPointParser.parse(it) }
.onEach { onAccessControlPointDataReceived(it) }
.catch { it.printStackTrace() }
.launchIn(lifecycleScope)
lifecycleScope.launchWithCatch {
@@ -292,19 +297,34 @@ internal class CGMService : NotificationService() {
private suspend fun requestLastRecord() {
clear()
repository.onNewRequestStatus(RequestStatus.PENDING)
recordAccessControlPointCharacteristic.write(RecordAccessControlPointInputParser.reportLastStoredRecord().value)
try {
recordAccessControlPointCharacteristic.write(RecordAccessControlPointInputParser.reportLastStoredRecord().value)
} catch (e: GattOperationException) {
e.printStackTrace()
repository.onNewRequestStatus(RequestStatus.FAILED)
}
}
private suspend fun requestFirstRecord() {
clear()
repository.onNewRequestStatus(RequestStatus.PENDING)
recordAccessControlPointCharacteristic.write(RecordAccessControlPointInputParser.reportFirstStoredRecord().value)
try {
recordAccessControlPointCharacteristic.write(RecordAccessControlPointInputParser.reportFirstStoredRecord().value)
} catch (e: GattOperationException) {
e.printStackTrace()
repository.onNewRequestStatus(RequestStatus.FAILED)
}
}
private suspend fun requestAllRecords() {
clear()
repository.onNewRequestStatus(RequestStatus.PENDING)
recordAccessControlPointCharacteristic.write(RecordAccessControlPointInputParser.reportNumberOfAllStoredRecords().value)
try {
recordAccessControlPointCharacteristic.write(RecordAccessControlPointInputParser.reportNumberOfAllStoredRecords().value)
} catch (e: GattOperationException) {
e.printStackTrace()
repository.onNewRequestStatus(RequestStatus.FAILED)
}
}
private fun stopIfDisconnected(connectionState: GattConnectionStateWithStatus) {

View File

@@ -125,12 +125,14 @@ internal class CSCService : NotificationService() {
batteryLevelCharacteristic.getNotifications()
.mapNotNull { BatteryLevelParser.parse(it) }
.onEach { repository.onBatteryLevelChanged(it) }
.catch { it.printStackTrace() }
.launchIn(lifecycleScope)
val cscDataParser = CSCDataParser()
cscMeasurementCharacteristic.getNotifications()
.mapNotNull { cscDataParser.parse(it, repository.wheelSize.value) }
.onEach { repository.onCSCDataChanged(it) }
.catch { it.printStackTrace() }
.launchIn(lifecycleScope)
}

View File

@@ -133,11 +133,13 @@ internal class HRSService : NotificationService() {
batteryLevelCharacteristic.getNotifications()
.mapNotNull { BatteryLevelParser.parse(it) }
.onEach { repository.onBatteryLevelChanged(it) }
.catch { it.printStackTrace() }
.launchIn(lifecycleScope)
hrsMeasurementCharacteristic.getNotifications()
.mapNotNull { HRSDataParser.parse(it) }
.onEach { repository.onHRSDataChanged(it) }
.catch { it.printStackTrace() }
.launchIn(lifecycleScope)
}

View File

@@ -127,11 +127,13 @@ internal class HTSService : NotificationService() {
batteryLevelCharacteristic.getNotifications()
.mapNotNull { BatteryLevelParser.parse(it) }
.onEach { repository.onBatteryLevelChanged(it) }
.catch { it.printStackTrace() }
.launchIn(lifecycleScope)
htsMeasurementCharacteristic.getNotifications()
.mapNotNull { HTSDataParser.parse(it) }
.onEach { repository.onHTSDataChanged(it) }
.catch { it.printStackTrace() }
.launchIn(lifecycleScope)
}

View File

@@ -44,6 +44,7 @@ import kotlinx.coroutines.launch
import no.nordicsemi.android.common.logger.NordicBlekLogger
import no.nordicsemi.android.kotlin.ble.client.main.callback.BleGattClient
import no.nordicsemi.android.kotlin.ble.client.main.connect
import no.nordicsemi.android.kotlin.ble.client.main.errors.GattOperationException
import no.nordicsemi.android.kotlin.ble.client.main.service.BleGattCharacteristic
import no.nordicsemi.android.kotlin.ble.client.main.service.BleGattServices
import no.nordicsemi.android.kotlin.ble.core.ServerDevice
@@ -174,7 +175,6 @@ internal class PRXService : NotificationService() {
client.connectionStateWithStatus
.filterNotNull()
.onEach { repository.onConnectionStateChanged(it) }
.filterNotNull()
.onEach { stopIfDisconnected(it.state, it.status) }
.launchIn(lifecycleScope)
@@ -204,14 +204,19 @@ internal class PRXService : NotificationService() {
batteryLevelCharacteristic.getNotifications()
.mapNotNull { BatteryLevelParser.parse(it) }
.onEach { repository.onBatteryLevelChanged(it) }
.catch { it.printStackTrace() }
.launchIn(lifecycleScope)
linkLossCharacteristic.write(AlertLevelInputParser.parse(AlarmLevel.HIGH))
}
private suspend fun writeAlertLevel(alarmLevel: AlarmLevel) {
alertLevelCharacteristic.write(AlertLevelInputParser.parse(alarmLevel), BleWriteType.NO_RESPONSE)
repository.onRemoteAlarmLevelSet(alarmLevel)
try {
alertLevelCharacteristic.write(AlertLevelInputParser.parse(alarmLevel), BleWriteType.NO_RESPONSE)
repository.onRemoteAlarmLevelSet(alarmLevel)
} catch (e: GattOperationException) {
e.printStackTrace()
}
}
private fun stopIfDisconnected(connectionState: GattConnectionState, connectionStatus: BleGattConnectionStatus) {

View File

@@ -125,11 +125,13 @@ internal class RSCSService : NotificationService() {
batteryLevelCharacteristic.getNotifications()
.mapNotNull { BatteryLevelParser.parse(it) }
.onEach { repository.onBatteryLevelChanged(it) }
.catch { it.printStackTrace() }
.launchIn(lifecycleScope)
rscsMeasurementCharacteristic.getNotifications()
.mapNotNull { RSCSDataParser.parse(it) }
.onEach { repository.onRSCSDataChanged(it) }
.catch { it.printStackTrace() }
.launchIn(lifecycleScope)
}

View File

@@ -132,11 +132,13 @@ internal class UARTService : NotificationService() {
batteryService?.findCharacteristic(BATTERY_LEVEL_CHARACTERISTIC_UUID)?.getNotifications()
?.mapNotNull { BatteryLevelParser.parse(it) }
?.onEach { repository.onBatteryLevelChanged(it) }
?.catch { it.printStackTrace() }
?.launchIn(lifecycleScope)
txCharacteristic.getNotifications()
.onEach { repository.onNewMessageReceived(String(it)) }
.onEach { logger.log(10, "Received: ${String(it)}") }
.catch { it.printStackTrace() }
.launchIn(lifecycleScope)
repository.command