From 634b9c20e76b267efc8948a562cf52fc08dde277 Mon Sep 17 00:00:00 2001 From: Bart Sopers Date: Sun, 28 Feb 2021 00:42:32 +0100 Subject: [PATCH] Add interceptors for PodEvents and errors --- .../dash/driver/OmnipodDashManagerImpl.kt | 75 ++++++++++++++++++- .../omnipod/dash/driver/event/PodEvent.kt | 2 +- .../pod/state/OmnipodDashPodStateManager.kt | 4 +- .../state/OmnipodDashPodStateManagerImpl.kt | 20 ++++- 4 files changed, 93 insertions(+), 8 deletions(-) diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/OmnipodDashManagerImpl.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/OmnipodDashManagerImpl.kt index 8497b92a87..e4c18b1d13 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/OmnipodDashManagerImpl.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/OmnipodDashManagerImpl.kt @@ -1,6 +1,7 @@ package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver import info.nightscout.androidaps.logging.AAPSLogger +import info.nightscout.androidaps.logging.LTag import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.OmnipodDashBleManager import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.event.PodEvent import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.command.GetVersionCommand @@ -9,9 +10,14 @@ import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.definitio import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.definition.AlertConfiguration import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.definition.AlertSlot import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.definition.BasalProgram +import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.response.AlarmStatusResponse +import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.response.DefaultStatusResponse +import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.response.SetUniqueIdResponse +import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.response.VersionResponse import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.state.OmnipodDashPodStateManager import io.reactivex.Observable import io.reactivex.android.schedulers.AndroidSchedulers +import io.reactivex.functions.Consumer import io.reactivex.schedulers.Schedulers import java.util.* import javax.inject.Inject @@ -19,7 +25,7 @@ import javax.inject.Singleton @Singleton class OmnipodDashManagerImpl @Inject constructor( - private val aapsLogger: AAPSLogger, + private val logger: AAPSLogger, private val podStateManager: OmnipodDashPodStateManager, private val bleManager: OmnipodDashBleManager ) : OmnipodDashManager { @@ -33,10 +39,13 @@ class OmnipodDashManagerImpl @Inject constructor( } } + private val connectToPod: Observable + get() = Observable.defer { bleManager.connect() } + override fun activatePodPart1(): Observable { return Observable.concat( observePodReadyForActivationPart1, - bleManager.connect(), + connectToPod, Observable.defer { bleManager.sendCommand(GetVersionCommand.Builder() // .setSequenceNumber(podStateManager.messageSequenceNumber) // @@ -44,7 +53,11 @@ class OmnipodDashManagerImpl @Inject constructor( .build()) // } // ... Send more commands - ).subscribeOn(Schedulers.io()) // + ) // + // TODO these would be common for any observable returned in a public function in this class + .doOnNext(PodEventInterceptor()) // + .doOnError(ErrorInterceptor()) + .subscribeOn(Schedulers.io()) // .observeOn(AndroidSchedulers.mainThread()) } @@ -112,4 +125,60 @@ class OmnipodDashManagerImpl @Inject constructor( // TODO return Observable.empty() } + + inner class PodEventInterceptor : Consumer { + + // TODO split into separate methods + override fun accept(event: PodEvent) { + logger.debug(LTag.PUMP, "Intercepted PodEvent in OmnipodDashManagerImpl: ${event.javaClass.simpleName}") + when (event) { + is PodEvent.AlreadyConnected -> { + podStateManager.bluetoothAddress = event.bluetoothAddress + podStateManager.uniqueId = event.uniqueId + } + + is PodEvent.BluetoothConnected -> { + podStateManager.bluetoothAddress = event.address + } + + is PodEvent.Connected -> { + podStateManager.uniqueId = event.uniqueId + } + + is PodEvent.ResponseReceived -> { + podStateManager.increaseMessageSequenceNumber() + when (event.response) { + is VersionResponse -> { + podStateManager.updateFromVersionResponse(event.response) + } + + is SetUniqueIdResponse -> { + podStateManager.updateFromSetUniqueIdResponse(event.response) + } + + is DefaultStatusResponse -> { + podStateManager.updateFromDefaultStatusResponse(event.response) + } + + is AlarmStatusResponse -> { + podStateManager.updateFromAlarmStatusResponse(event.response) + } + } + } + + else -> { + // Do nothing + } + } + } + + } + + inner class ErrorInterceptor : Consumer { + + override fun accept(throwable: Throwable) { + logger.debug(LTag.PUMP, "Intercepted error in OmnipodDashManagerImpl: ${throwable.javaClass.simpleName}") + } + + } } \ No newline at end of file diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/event/PodEvent.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/event/PodEvent.kt index 2f68033ea5..25386ca32a 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/event/PodEvent.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/event/PodEvent.kt @@ -6,7 +6,7 @@ import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.response. sealed class PodEvent { /* BT connection events */ - class AlreadyConnected(val uniqueId: Long) : PodEvent() + class AlreadyConnected(val bluetoothAddress: String, val uniqueId: Long) : PodEvent() object Scanning : PodEvent() object BluetoothConnecting : PodEvent() class BluetoothConnected(val address: String) : PodEvent() diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/state/OmnipodDashPodStateManager.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/state/OmnipodDashPodStateManager.kt index 85e959d5ec..bcfe894f5e 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/state/OmnipodDashPodStateManager.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/state/OmnipodDashPodStateManager.kt @@ -21,8 +21,8 @@ interface OmnipodDashPodStateManager { val messageSequenceNumber: Short val sequenceNumberOfLastProgrammingCommand: Short? val activationTime: Long? - val uniqueId: Long? - val bluetoothAddress: String? + var uniqueId: Long? + var bluetoothAddress: String? val bluetoothVersion: SoftwareVersion? val firmwareVersion: SoftwareVersion? diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/state/OmnipodDashPodStateManagerImpl.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/state/OmnipodDashPodStateManagerImpl.kt index 5fd48d82b4..bfc345801f 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/state/OmnipodDashPodStateManagerImpl.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/state/OmnipodDashPodStateManagerImpl.kt @@ -68,11 +68,27 @@ class OmnipodDashPodStateManagerImpl @Inject constructor( override val activationTime: Long? get() = podState.activationTime - override val uniqueId: Long? + override var uniqueId: Long? get() = podState.uniqueId + set(uniqueId) { + if (podState.uniqueId == null) { + podState.uniqueId = uniqueId + store() + } else if (uniqueId != podState.uniqueId) { + throw IllegalStateException("Trying to set Unique ID to $uniqueId, but it is already set to ${podState.uniqueId}") + } + } - override val bluetoothAddress: String? + override var bluetoothAddress: String? get() = podState.bluetoothAddress + set(bluetoothAddress) { + if (podState.bluetoothAddress == null) { + podState.bluetoothAddress = bluetoothAddress + store() + } else if (bluetoothAddress != podState.bluetoothAddress) { + throw IllegalStateException("Trying to set Bluetooth Address to $bluetoothAddress, but it is already set to ${podState.bluetoothAddress}") + } + } override val bluetoothVersion: SoftwareVersion? get() = podState.bleVersion