Add interceptors for PodEvents and errors
This commit is contained in:
parent
8d629977ec
commit
634b9c20e7
4 changed files with 93 additions and 8 deletions
|
@ -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<PodEvent>
|
||||
get() = Observable.defer { bleManager.connect() }
|
||||
|
||||
override fun activatePodPart1(): Observable<PodEvent> {
|
||||
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<PodEvent> {
|
||||
|
||||
// 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<Throwable> {
|
||||
|
||||
override fun accept(throwable: Throwable) {
|
||||
logger.debug(LTag.PUMP, "Intercepted error in OmnipodDashManagerImpl: ${throwable.javaClass.simpleName}")
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -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()
|
||||
|
|
|
@ -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?
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue