Revert "Revert "remove duplication""

This reverts commit d49b87d124.
This commit is contained in:
Andrei Vereha 2021-04-21 20:44:26 +02:00
parent d49b87d124
commit 1e4cda01a3

View file

@ -14,6 +14,7 @@ import info.nightscout.androidaps.plugins.pump.common.defs.PumpType
import info.nightscout.androidaps.plugins.pump.omnipod.common.definition.OmnipodCommandType import info.nightscout.androidaps.plugins.pump.omnipod.common.definition.OmnipodCommandType
import info.nightscout.androidaps.plugins.pump.omnipod.common.queue.command.* import info.nightscout.androidaps.plugins.pump.omnipod.common.queue.command.*
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.OmnipodDashManager import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.OmnipodDashManager
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.event.PodEvent
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.definition.ActivationProgress import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.definition.ActivationProgress
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.definition.BeepType import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.definition.BeepType
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.response.ResponseType import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.response.ResponseType
@ -110,8 +111,11 @@ class OmnipodDashPumpPlugin @Inject constructor(
} }
override fun getPumpStatus(reason: String) { override fun getPumpStatus(reason: String) {
// TODO history Observable.concat(
omnipodManager.getStatus(ResponseType.StatusResponseType.DEFAULT_STATUS_RESPONSE).blockingSubscribeBy( omnipodManager.getStatus(ResponseType.StatusResponseType.DEFAULT_STATUS_RESPONSE),
history.updateFromState(podStateManager).toObservable(),
podStateManager.updateActiveCommand().toObservable(),
).blockingSubscribeBy(
onNext = { podEvent -> onNext = { podEvent ->
aapsLogger.debug( aapsLogger.debug(
LTag.PUMP, LTag.PUMP,
@ -128,26 +132,12 @@ class OmnipodDashPumpPlugin @Inject constructor(
} }
override fun setNewBasalProfile(profile: Profile): PumpEnactResult { override fun setNewBasalProfile(profile: Profile): PumpEnactResult {
// TODO history return executeProgrammingCommand(
history.createRecord(
return Single.create<PumpEnactResult> { source -> commandType = OmnipodCommandType.SET_BASAL_PROFILE
omnipodManager.setBasalProgram(mapProfileToBasalProgram(profile)).subscribeBy( ),
onNext = { podEvent -> omnipodManager.setBasalProgram(mapProfileToBasalProgram(profile))
aapsLogger.debug(
LTag.PUMP,
"Received PodEvent in setNewBasalProfile: $podEvent"
) )
},
onError = { throwable ->
aapsLogger.error(LTag.PUMP, "Error in setNewBasalProfile", throwable)
source.onSuccess(PumpEnactResult(injector).success(false).enacted(false).comment(throwable.message))
},
onComplete = {
aapsLogger.debug("setNewBasalProfile completed")
source.onSuccess(PumpEnactResult(injector).success(true).enacted(true))
}
)
}.blockingGet()
} }
override fun isThisProfileSet(profile: Profile): Boolean = podStateManager.basalProgram?.let { override fun isThisProfileSet(profile: Profile): Boolean = podStateManager.basalProgram?.let {
@ -155,7 +145,7 @@ class OmnipodDashPumpPlugin @Inject constructor(
} ?: true } ?: true
override fun lastDataTime(): Long { override fun lastDataTime(): Long {
return podStateManager.lastConnection return podStateManager.lastUpdatedSystem
} }
override val baseBasalRate: Double override val baseBasalRate: Double
@ -211,12 +201,15 @@ class OmnipodDashPumpPlugin @Inject constructor(
}, },
onError = { throwable -> onError = { throwable ->
aapsLogger.error(LTag.PUMP, "Error in deliverTreatment", throwable) aapsLogger.error(LTag.PUMP, "Error in deliverTreatment", throwable)
source.onSuccess(PumpEnactResult(injector).success(false).enacted(false).comment(throwable.message)) source.onSuccess(
PumpEnactResult(injector).success(false).enacted(false).comment(throwable.message)
)
}, },
onComplete = { onComplete = {
aapsLogger.debug("deliverTreatment completed") aapsLogger.debug("deliverTreatment completed")
source.onSuccess( source.onSuccess(
PumpEnactResult(injector).success(true).enacted(true).bolusDelivered(detailedBolusInfo.insulin) PumpEnactResult(injector).success(true).enacted(true)
.bolusDelivered(detailedBolusInfo.insulin)
.carbsDelivered(detailedBolusInfo.carbs) .carbsDelivered(detailedBolusInfo.carbs)
) )
} }
@ -226,29 +219,9 @@ class OmnipodDashPumpPlugin @Inject constructor(
override fun stopBolusDelivering() { override fun stopBolusDelivering() {
// TODO update Treatments (?) // TODO update Treatments (?)
executeProgrammingCommand(
Observable.concat( history.createRecord(OmnipodCommandType.CANCEL_BOLUS),
history.createRecord(
commandType = OmnipodCommandType.CANCEL_BOLUS,
).flatMapObservable { recordId ->
podStateManager.createActiveCommand(recordId).toObservable()
},
omnipodManager.stopBolus(), omnipodManager.stopBolus(),
history.updateFromState(podStateManager).toObservable(),
podStateManager.updateActiveCommand().toObservable(),
).blockingSubscribeBy(
onNext = { podEvent ->
aapsLogger.debug(
LTag.PUMP,
"Received PodEvent in stopBolusDelivering: $podEvent"
)
},
onError = { throwable ->
aapsLogger.error(LTag.PUMP, "Error in stopBolusDelivering", throwable)
},
onComplete = {
aapsLogger.debug("stopBolusDelivering completed")
}
) )
} }
@ -259,42 +232,18 @@ class OmnipodDashPumpPlugin @Inject constructor(
enforceNew: Boolean enforceNew: Boolean
): PumpEnactResult { ): PumpEnactResult {
// TODO update Treatments // TODO update Treatments
// TODO check for existing basal
return Single.create<PumpEnactResult> { source -> return executeProgrammingCommand(
Observable.concat(
history.createRecord( history.createRecord(
commandType = OmnipodCommandType.SET_TEMPORARY_BASAL, commandType = OmnipodCommandType.SET_TEMPORARY_BASAL,
tempBasalRecord = TempBasalRecord(duration = durationInMinutes, rate = absoluteRate) tempBasalRecord = TempBasalRecord(duration = durationInMinutes, rate = absoluteRate)
).flatMapObservable { recordId -> ),
podStateManager.createActiveCommand(recordId).toObservable()
},
omnipodManager.setTempBasal( omnipodManager.setTempBasal(
absoluteRate, absoluteRate,
durationInMinutes.toShort() durationInMinutes.toShort()
),
history.updateFromState(podStateManager).toObservable(),
podStateManager.updateActiveCommand().toObservable(),
).subscribeBy(
onNext = { podEvent ->
aapsLogger.debug(
LTag.PUMP,
"Received PodEvent in setTempBasalAbsolute: $podEvent"
) )
},
onError = { throwable ->
aapsLogger.error(LTag.PUMP, "Error in setTempBasalAbsolute", throwable)
source.onSuccess(PumpEnactResult(injector).success(false).enacted(false).comment(throwable.message))
},
onComplete = {
aapsLogger.debug("setTempBasalAbsolute completed")
source.onSuccess(
PumpEnactResult(injector).success(true).enacted(true).absolute(absoluteRate)
.duration(durationInMinutes)
) )
} }
)
}.blockingGet()
}
override fun setTempBasalPercent( override fun setTempBasalPercent(
percent: Int, percent: Int,
@ -315,37 +264,10 @@ class OmnipodDashPumpPlugin @Inject constructor(
override fun cancelTempBasal(enforceNew: Boolean): PumpEnactResult { override fun cancelTempBasal(enforceNew: Boolean): PumpEnactResult {
// TODO update Treatments // TODO update Treatments
return executeProgrammingCommand(
return Single.create<PumpEnactResult> { source -> history.createRecord(OmnipodCommandType.CANCEL_TEMPORARY_BASAL),
omnipodManager.stopTempBasal()
Observable.concat(
history.createRecord(
commandType = OmnipodCommandType.CANCEL_TEMPORARY_BASAL
).flatMapObservable { recordId ->
podStateManager.createActiveCommand(recordId).toObservable()
},
omnipodManager.stopTempBasal(),
history.updateFromState(podStateManager).toObservable(),
podStateManager.updateActiveCommand().toObservable(),
).subscribeBy(
onNext = { podEvent ->
aapsLogger.debug(
LTag.PUMP,
"Received PodEvent in cancelTempBasal: $podEvent"
) )
},
onError = { throwable ->
aapsLogger.error(LTag.PUMP, "Error in cancelTempBasal", throwable)
source.onSuccess(PumpEnactResult(injector).success(false).enacted(false).comment(throwable.message))
},
onComplete = {
aapsLogger.debug("cancelTempBasal completed")
source.onSuccess(
PumpEnactResult(injector).success(true).enacted(true)
)
}
)
}.blockingGet()
} }
override fun cancelExtendedBolus(): PumpEnactResult { override fun cancelExtendedBolus(): PumpEnactResult {
@ -370,11 +292,8 @@ class OmnipodDashPumpPlugin @Inject constructor(
} }
override fun serialNumber(): String { override fun serialNumber(): String {
return if (podStateManager.uniqueId == null) { return podStateManager.uniqueId?.toString()
"n/a" // TODO i18n ?: "n/a" // TODO i18n
} else {
podStateManager.uniqueId.toString()
}
} }
override fun shortStatus(veryShort: Boolean): String { override fun shortStatus(veryShort: Boolean): String {
@ -433,12 +352,15 @@ class OmnipodDashPumpPlugin @Inject constructor(
} }
private fun silenceAlerts(): PumpEnactResult { private fun silenceAlerts(): PumpEnactResult {
// TODO history
// TODO filter alert types // TODO filter alert types
return podStateManager.activeAlerts?.let { return podStateManager.activeAlerts?.let {
Single.create<PumpEnactResult> { source -> Single.create<PumpEnactResult> { source ->
omnipodManager.silenceAlerts(it).subscribeBy( Observable.concat(
// TODO: is this a programming command? if yes, save to history
omnipodManager.silenceAlerts(it),
history.updateFromState(podStateManager).toObservable(),
podStateManager.updateActiveCommand().toObservable(),
).subscribeBy(
onNext = { podEvent -> onNext = { podEvent ->
aapsLogger.debug( aapsLogger.debug(
LTag.PUMP, LTag.PUMP,
@ -459,75 +381,26 @@ class OmnipodDashPumpPlugin @Inject constructor(
} }
private fun suspendDelivery(): PumpEnactResult { private fun suspendDelivery(): PumpEnactResult {
// TODO history return executeProgrammingCommand(
history.createRecord(OmnipodCommandType.RESUME_DELIVERY),
return Single.create<PumpEnactResult> { source -> omnipodManager.suspendDelivery()
omnipodManager.suspendDelivery().subscribeBy(
onNext = { podEvent ->
aapsLogger.debug(
LTag.PUMP,
"Received PodEvent in suspendDelivery: $podEvent"
) )
},
onError = { throwable ->
aapsLogger.error(LTag.PUMP, "Error in suspendDelivery", throwable)
source.onSuccess(PumpEnactResult(injector).success(false).comment(throwable.message))
},
onComplete = {
aapsLogger.debug("suspendDelivery completed")
source.onSuccess(PumpEnactResult(injector).success(true))
}
)
}.blockingGet()
} }
private fun resumeDelivery(): PumpEnactResult { private fun resumeDelivery(): PumpEnactResult {
// TODO history
return profileFunction.getProfile()?.let { return profileFunction.getProfile()?.let {
executeProgrammingCommand(
Single.create<PumpEnactResult> { source -> history.createRecord(OmnipodCommandType.RESUME_DELIVERY),
omnipodManager.setBasalProgram(mapProfileToBasalProgram(it)).subscribeBy( omnipodManager.setBasalProgram(mapProfileToBasalProgram(it))
onNext = { podEvent ->
aapsLogger.debug(
LTag.PUMP,
"Received PodEvent in resumeDelivery: $podEvent"
) )
},
onError = { throwable ->
aapsLogger.error(LTag.PUMP, "Error in resumeDelivery", throwable)
source.onSuccess(PumpEnactResult(injector).success(false).comment(throwable.message))
},
onComplete = {
aapsLogger.debug("resumeDelivery completed")
source.onSuccess(PumpEnactResult(injector).success(true))
}
)
}.blockingGet()
} ?: PumpEnactResult(injector).success(false).enacted(false).comment("No profile active") // TODO i18n } ?: PumpEnactResult(injector).success(false).enacted(false).comment("No profile active") // TODO i18n
} }
private fun deactivatePod(): PumpEnactResult { private fun deactivatePod(): PumpEnactResult {
// TODO history return executeProgrammingCommand(
history.createRecord(OmnipodCommandType.DEACTIVATE_POD),
return Single.create<PumpEnactResult> { source -> omnipodManager.deactivatePod()
omnipodManager.deactivatePod().subscribeBy(
onNext = { podEvent ->
aapsLogger.debug(
LTag.PUMP,
"Received PodEvent in deactivatePod: $podEvent"
) )
},
onError = { throwable ->
aapsLogger.error(LTag.PUMP, "Error in deactivatePod", throwable)
source.onSuccess(PumpEnactResult(injector).success(false).comment(throwable.message))
},
onComplete = {
aapsLogger.debug("deactivatePod completed")
source.onSuccess(PumpEnactResult(injector).success(true))
}
)
}.blockingGet()
} }
private fun handleTimeChange(): PumpEnactResult { private fun handleTimeChange(): PumpEnactResult {
@ -541,10 +414,13 @@ class OmnipodDashPumpPlugin @Inject constructor(
} }
private fun playTestBeep(): PumpEnactResult { private fun playTestBeep(): PumpEnactResult {
// TODO history
return Single.create<PumpEnactResult> { source -> return Single.create<PumpEnactResult> { source ->
omnipodManager.playBeep(BeepType.LONG_SINGLE_BEEP).subscribeBy( Observable.concat(
omnipodManager.playBeep(BeepType.LONG_SINGLE_BEEP),
history.updateFromState(podStateManager).toObservable(),
podStateManager.updateActiveCommand().toObservable(),
).subscribeBy(
onNext = { podEvent -> onNext = { podEvent ->
aapsLogger.debug( aapsLogger.debug(
LTag.PUMP, LTag.PUMP,
@ -553,7 +429,9 @@ class OmnipodDashPumpPlugin @Inject constructor(
}, },
onError = { throwable -> onError = { throwable ->
aapsLogger.error(LTag.PUMP, "Error in playTestBeep", throwable) aapsLogger.error(LTag.PUMP, "Error in playTestBeep", throwable)
source.onSuccess(PumpEnactResult(injector).success(false).enacted(false).comment(throwable.message)) source.onSuccess(
PumpEnactResult(injector).success(false).enacted(false).comment(throwable.message)
)
}, },
onComplete = { onComplete = {
aapsLogger.debug("playTestBeep completed") aapsLogger.debug("playTestBeep completed")
@ -585,4 +463,39 @@ class OmnipodDashPumpPlugin @Inject constructor(
commandQueue.customCommand(CommandHandleTimeChange(false), null) commandQueue.customCommand(CommandHandleTimeChange(false), null)
} }
private fun executeProgrammingCommand(
historyEntry: Single<String>,
command: Observable<PodEvent>
): PumpEnactResult {
return Single.create<PumpEnactResult> { source ->
Observable.concat(
historyEntry.flatMapObservable { recordId ->
podStateManager.createActiveCommand(recordId).toObservable()
},
command,
history.updateFromState(podStateManager).toObservable(),
podStateManager.updateActiveCommand().toObservable(),
).subscribeBy(
onNext = { podEvent ->
aapsLogger.debug(
LTag.PUMP,
"Received PodEvent: $podEvent"
)
},
onError = { throwable ->
aapsLogger.error(LTag.PUMP, "Error executing command", throwable)
source.onSuccess(
PumpEnactResult(injector).success(false).enacted(false).comment(throwable.message)
)
},
onComplete = {
aapsLogger.debug("Command completed")
source.onSuccess(
PumpEnactResult(injector).success(true).enacted(true)
)
}
)
}.blockingGet()
}
} }