Address review findings

This commit is contained in:
Bart Sopers 2021-02-28 15:18:10 +01:00
parent 634b9c20e7
commit aa14843d33
16 changed files with 116 additions and 59 deletions

View file

@ -1,3 +1,9 @@
package info.nightscout.androidaps.plugins.pump.omnipod.common.ui.wizard.activation.viewmodel.action
abstract class InitializePodViewModel : PodActivationActionViewModelBase()
import dagger.android.HasAndroidInjector
import info.nightscout.androidaps.logging.AAPSLogger
abstract class InitializePodViewModel(
injector: HasAndroidInjector,
logger: AAPSLogger
) : PodActivationActionViewModelBase(injector, logger)

View file

@ -1,3 +1,9 @@
package info.nightscout.androidaps.plugins.pump.omnipod.common.ui.wizard.activation.viewmodel.action
abstract class InsertCannulaViewModel : PodActivationActionViewModelBase()
import dagger.android.HasAndroidInjector
import info.nightscout.androidaps.logging.AAPSLogger
abstract class InsertCannulaViewModel(
injector: HasAndroidInjector,
logger: AAPSLogger
) : PodActivationActionViewModelBase(injector, logger)

View file

@ -1,8 +1,13 @@
package info.nightscout.androidaps.plugins.pump.omnipod.common.ui.wizard.activation.viewmodel.action
import dagger.android.HasAndroidInjector
import info.nightscout.androidaps.logging.AAPSLogger
import info.nightscout.androidaps.plugins.pump.omnipod.common.ui.wizard.common.viewmodel.ActionViewModelBase
abstract class PodActivationActionViewModelBase : ActionViewModelBase() {
abstract class PodActivationActionViewModelBase(
injector: HasAndroidInjector,
logger: AAPSLogger
) : ActionViewModelBase(injector, logger) {
abstract fun isPodInAlarm(): Boolean

View file

@ -7,12 +7,11 @@ import info.nightscout.androidaps.data.PumpEnactResult
import info.nightscout.androidaps.logging.AAPSLogger
import info.nightscout.androidaps.logging.LTag
import io.reactivex.Single
import javax.inject.Inject
abstract class ActionViewModelBase : ViewModelBase() {
@Inject lateinit var injector: HasAndroidInjector
@Inject lateinit var logger: AAPSLogger
abstract class ActionViewModelBase(
protected val injector: HasAndroidInjector,
protected val logger: AAPSLogger
) : ViewModelBase() {
private val _isActionExecutingLiveData = MutableLiveData(false)
val isActionExecutingLiveData: LiveData<Boolean> = _isActionExecutingLiveData

View file

@ -1,8 +1,13 @@
package info.nightscout.androidaps.plugins.pump.omnipod.common.ui.wizard.deactivation.viewmodel.action
import dagger.android.HasAndroidInjector
import info.nightscout.androidaps.logging.AAPSLogger
import info.nightscout.androidaps.plugins.pump.omnipod.common.ui.wizard.common.viewmodel.ActionViewModelBase
abstract class DeactivatePodViewModel : ActionViewModelBase() {
abstract class DeactivatePodViewModel(
injector: HasAndroidInjector,
logger: AAPSLogger
) : ActionViewModelBase(injector, logger) {
abstract fun discardPod()
}

View file

@ -12,13 +12,13 @@ import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.definitio
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.Response
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 info.nightscout.androidaps.utils.rx.AapsSchedulers
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
import javax.inject.Singleton
@ -27,7 +27,8 @@ import javax.inject.Singleton
class OmnipodDashManagerImpl @Inject constructor(
private val logger: AAPSLogger,
private val podStateManager: OmnipodDashPodStateManager,
private val bleManager: OmnipodDashBleManager
private val bleManager: OmnipodDashBleManager,
private val aapsSchedulers: AapsSchedulers
) : OmnipodDashManager {
private val observePodReadyForActivationPart1: Observable<PodEvent>
@ -39,26 +40,28 @@ class OmnipodDashManagerImpl @Inject constructor(
}
}
private val connectToPod: Observable<PodEvent>
private val observeConnectToPod: Observable<PodEvent>
get() = Observable.defer { bleManager.connect() }
private val observeSendGetVersionCommand: Observable<PodEvent>
get() = Observable.defer {
bleManager.sendCommand(GetVersionCommand.Builder() //
.setSequenceNumber(podStateManager.messageSequenceNumber) //
.setUniqueId(DEFAULT_UNIQUE_ID) //
.build()) //
}
override fun activatePodPart1(): Observable<PodEvent> {
return Observable.concat(
observePodReadyForActivationPart1,
connectToPod,
Observable.defer {
bleManager.sendCommand(GetVersionCommand.Builder() //
.setSequenceNumber(podStateManager.messageSequenceNumber) //
.setUniqueId(DEFAULT_UNIQUE_ID) //
.build()) //
}
observeConnectToPod,
observeSendGetVersionCommand
// ... Send more commands
) //
// 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())
.subscribeOn(aapsSchedulers.io)
}
override fun activatePodPart2(): Observable<PodEvent> {
@ -128,7 +131,6 @@ class OmnipodDashManagerImpl @Inject constructor(
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) {
@ -147,23 +149,7 @@ class OmnipodDashManagerImpl @Inject constructor(
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)
}
}
handleResponse(event.response)
}
else -> {
@ -172,6 +158,26 @@ class OmnipodDashManagerImpl @Inject constructor(
}
}
private fun handleResponse(response: Response) {
when (response) {
is VersionResponse -> {
podStateManager.updateFromVersionResponse(response)
}
is SetUniqueIdResponse -> {
podStateManager.updateFromSetUniqueIdResponse(response)
}
is DefaultStatusResponse -> {
podStateManager.updateFromDefaultStatusResponse(response)
}
is AlarmStatusResponse -> {
podStateManager.updateFromAlarmStatusResponse(response)
}
}
}
}
inner class ErrorInterceptor : Consumer<Throwable> {

View file

@ -94,7 +94,7 @@ class OmnipodDashBleManagerImpl @Inject constructor(private val context: Context
val msgIO = MessageIO(aapsLogger, bleIO)
val ltkExchanger = LTKExchanger(aapsLogger, msgIO)
emitter.onNext(PodEvent.NegotiatingLtkAndNoncePrefix)
emitter.onNext(PodEvent.Pairing)
val ltk = ltkExchanger.negotiateLTKAndNonce()
aapsLogger.info(LTag.PUMPCOMM, "Got LTK and Nonce Prefix: ${ltk}")
@ -102,7 +102,7 @@ class OmnipodDashBleManagerImpl @Inject constructor(private val context: Context
emitter.onComplete()
} catch (ex: Exception) {
emitter.onError(ex) // TODO is this necessary?
emitter.tryOnError(ex) // TODO is this necessary?
}
}

View file

@ -42,7 +42,7 @@ class PodScanner(private val logger: AAPSLogger, private val bluetoothAdapter: B
companion object {
const val SCAN_FOR_SERVICE_UUID = "00004024-0000-1000-8000-00805F9B34FB"
const val POD_ID_NOT_ACTIVATED = 4294967294L
const val POD_ID_NOT_ACTIVATED = 0xFFFFFFFFL
private const val SCAN_DURATION_MS = 5000
}
}

View file

@ -10,7 +10,9 @@ sealed class PodEvent {
object Scanning : PodEvent()
object BluetoothConnecting : PodEvent()
class BluetoothConnected(val address: String) : PodEvent()
object NegotiatingLtkAndNoncePrefix : PodEvent()
object Pairing : PodEvent()
object Paired : PodEvent()
object EstablishingSession : PodEvent()
class Connected(val uniqueId: Long) : PodEvent()
/* Message exchange events */

View file

@ -34,8 +34,6 @@ class OmnipodDashPodStateManagerImpl @Inject constructor(
store()
}
// TODO: dynamic get() fun instead of assignment
override val isUniqueIdSet: Boolean
get() = activationProgress.isAtLeast(ActivationProgress.SET_UNIQUE_ID)

View file

@ -1,15 +1,22 @@
package info.nightscout.androidaps.plugins.pump.omnipod.dash.ui.wizard.activation.viewmodel.action
import androidx.annotation.StringRes
import dagger.android.HasAndroidInjector
import info.nightscout.androidaps.data.PumpEnactResult
import info.nightscout.androidaps.logging.AAPSLogger
import info.nightscout.androidaps.logging.LTag
import info.nightscout.androidaps.plugins.pump.omnipod.common.ui.wizard.activation.viewmodel.action.InitializePodViewModel
import info.nightscout.androidaps.plugins.pump.omnipod.dash.R
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.OmnipodDashManager
import io.reactivex.Single
import io.reactivex.rxkotlin.subscribeBy
import javax.inject.Inject
class DashInitializePodViewModel @Inject constructor(private val omnipodManager: OmnipodDashManager) : InitializePodViewModel() {
class DashInitializePodViewModel @Inject constructor(
private val omnipodManager: OmnipodDashManager,
injector: HasAndroidInjector,
logger: AAPSLogger
) : InitializePodViewModel(injector, logger) {
override fun isPodInAlarm(): Boolean = false // TODO
@ -19,13 +26,13 @@ class DashInitializePodViewModel @Inject constructor(private val omnipodManager:
override fun doExecuteAction(): Single<PumpEnactResult> =
Single.create { source ->
val disposable = omnipodManager.activatePodPart1().subscribe(
{ podEvent -> logger.debug(LTag.PUMP, "Received PodEvent in Pod activation part 1: $podEvent") },
{ throwable ->
val disposable = omnipodManager.activatePodPart1().subscribeBy(
onNext = { podEvent -> logger.debug(LTag.PUMP, "Received PodEvent in Pod activation part 1: $podEvent") },
onError = { throwable ->
logger.error(LTag.PUMP, "Error in Pod activation part 1", throwable)
source.onSuccess(PumpEnactResult(injector).success(false).comment(throwable.message))
},
{
onComplete = {
logger.debug("Pod activation part 1 completed")
source.onSuccess(PumpEnactResult(injector).success(true))
}

View file

@ -1,14 +1,20 @@
package info.nightscout.androidaps.plugins.pump.omnipod.dash.ui.wizard.activation.viewmodel.action
import androidx.annotation.StringRes
import dagger.android.HasAndroidInjector
import info.nightscout.androidaps.data.PumpEnactResult
import info.nightscout.androidaps.interfaces.ProfileFunction
import info.nightscout.androidaps.logging.AAPSLogger
import info.nightscout.androidaps.plugins.pump.omnipod.common.ui.wizard.activation.viewmodel.action.InsertCannulaViewModel
import info.nightscout.androidaps.plugins.pump.omnipod.dash.R
import io.reactivex.Single
import javax.inject.Inject
class DashInsertCannulaViewModel @Inject constructor(private val profileFunction: ProfileFunction) : InsertCannulaViewModel() {
class DashInsertCannulaViewModel @Inject constructor(
private val profileFunction: ProfileFunction,
injector: HasAndroidInjector,
logger: AAPSLogger
) : InsertCannulaViewModel(injector, logger) {
override fun isPodInAlarm(): Boolean = false // TODO

View file

@ -1,13 +1,18 @@
package info.nightscout.androidaps.plugins.pump.omnipod.dash.ui.wizard.deactivation.viewmodel.action
import androidx.annotation.StringRes
import dagger.android.HasAndroidInjector
import info.nightscout.androidaps.data.PumpEnactResult
import info.nightscout.androidaps.logging.AAPSLogger
import info.nightscout.androidaps.plugins.pump.omnipod.common.R
import info.nightscout.androidaps.plugins.pump.omnipod.common.ui.wizard.deactivation.viewmodel.action.DeactivatePodViewModel
import io.reactivex.Single
import javax.inject.Inject
class DashDeactivatePodViewModel @Inject constructor() : DeactivatePodViewModel() {
class DashDeactivatePodViewModel @Inject constructor(
injector: HasAndroidInjector,
logger: AAPSLogger
) : DeactivatePodViewModel(injector, logger) {
override fun doExecuteAction(): Single<PumpEnactResult> = Single.just(PumpEnactResult(injector).success(false).comment("TODO")) // TODO

View file

@ -1,7 +1,9 @@
package info.nightscout.androidaps.plugins.pump.omnipod.eros.ui.wizard.activation.viewmodel.action
import androidx.annotation.StringRes
import dagger.android.HasAndroidInjector
import info.nightscout.androidaps.data.PumpEnactResult
import info.nightscout.androidaps.logging.AAPSLogger
import info.nightscout.androidaps.plugins.pump.omnipod.common.ui.wizard.activation.viewmodel.action.InitializePodViewModel
import info.nightscout.androidaps.plugins.pump.omnipod.eros.R
import info.nightscout.androidaps.plugins.pump.omnipod.eros.driver.definition.ActivationProgress
@ -12,8 +14,10 @@ import javax.inject.Inject
class ErosInitializePodViewModel @Inject constructor(
private val aapsOmnipodManager: AapsOmnipodErosManager,
private val podStateManager: AapsErosPodStateManager
) : InitializePodViewModel() {
private val podStateManager: AapsErosPodStateManager,
injector: HasAndroidInjector,
logger: AAPSLogger
) : InitializePodViewModel(injector, logger) {
override fun isPodInAlarm(): Boolean = podStateManager.isPodFaulted

View file

@ -1,8 +1,10 @@
package info.nightscout.androidaps.plugins.pump.omnipod.eros.ui.wizard.activation.viewmodel.action
import androidx.annotation.StringRes
import dagger.android.HasAndroidInjector
import info.nightscout.androidaps.data.PumpEnactResult
import info.nightscout.androidaps.interfaces.ProfileFunction
import info.nightscout.androidaps.logging.AAPSLogger
import info.nightscout.androidaps.plugins.pump.omnipod.common.ui.wizard.activation.viewmodel.action.InsertCannulaViewModel
import info.nightscout.androidaps.plugins.pump.omnipod.eros.R
import info.nightscout.androidaps.plugins.pump.omnipod.eros.driver.definition.ActivationProgress
@ -14,8 +16,10 @@ import javax.inject.Inject
class ErosInsertCannulaViewModel @Inject constructor(
private val aapsOmnipodManager: AapsOmnipodErosManager,
private val podStateManager: AapsErosPodStateManager,
private val profileFunction: ProfileFunction
) : InsertCannulaViewModel() {
private val profileFunction: ProfileFunction,
injector: HasAndroidInjector,
logger: AAPSLogger
) : InsertCannulaViewModel(injector, logger) {
override fun isPodInAlarm(): Boolean = podStateManager.isPodFaulted

View file

@ -1,8 +1,10 @@
package info.nightscout.androidaps.plugins.pump.omnipod.eros.ui.wizard.deactivation.viewmodel.action
import androidx.annotation.StringRes
import dagger.android.HasAndroidInjector
import info.nightscout.androidaps.data.PumpEnactResult
import info.nightscout.androidaps.interfaces.CommandQueueProvider
import info.nightscout.androidaps.logging.AAPSLogger
import info.nightscout.androidaps.plugins.pump.omnipod.common.R
import info.nightscout.androidaps.plugins.pump.omnipod.common.queue.command.CommandDeactivatePod
import info.nightscout.androidaps.plugins.pump.omnipod.common.ui.wizard.deactivation.viewmodel.action.DeactivatePodViewModel
@ -13,8 +15,10 @@ import javax.inject.Inject
class ErosDeactivatePodViewModel @Inject constructor(
private val aapsOmnipodManager: AapsOmnipodErosManager,
private val commandQueueProvider: CommandQueueProvider
) : DeactivatePodViewModel() {
private val commandQueueProvider: CommandQueueProvider,
injector: HasAndroidInjector,
logger: AAPSLogger
) : DeactivatePodViewModel(injector, logger) {
override fun doExecuteAction(): Single<PumpEnactResult> =
Single.create { source ->