create history records on pod activation. save basal profiles to history

This commit is contained in:
Andrei Vereha 2021-10-25 23:25:38 +02:00
parent 75f51b1408
commit a51c49f81c
9 changed files with 62 additions and 34 deletions

View file

@ -36,6 +36,7 @@ import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.response.
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.state.CommandConfirmed import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.state.CommandConfirmed
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.state.OmnipodDashPodStateManager import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.state.OmnipodDashPodStateManager
import info.nightscout.androidaps.plugins.pump.omnipod.dash.history.DashHistory import info.nightscout.androidaps.plugins.pump.omnipod.dash.history.DashHistory
import info.nightscout.androidaps.plugins.pump.omnipod.dash.history.data.BasalValuesRecord
import info.nightscout.androidaps.plugins.pump.omnipod.dash.history.data.BolusRecord import info.nightscout.androidaps.plugins.pump.omnipod.dash.history.data.BolusRecord
import info.nightscout.androidaps.plugins.pump.omnipod.dash.history.data.BolusType import info.nightscout.androidaps.plugins.pump.omnipod.dash.history.data.BolusType
import info.nightscout.androidaps.plugins.pump.omnipod.dash.history.data.TempBasalRecord import info.nightscout.androidaps.plugins.pump.omnipod.dash.history.data.TempBasalRecord
@ -372,7 +373,10 @@ class OmnipodDashPumpPlugin @Inject constructor(
deliverySuspended = true deliverySuspended = true
} }
}, },
historyEntry = history.createRecord(commandType = OmnipodCommandType.SET_BASAL_PROFILE), historyEntry = history.createRecord(
commandType = OmnipodCommandType.SET_BASAL_PROFILE,
basalProfileRecord = BasalValuesRecord(profile.getBasalValues().toList())
),
activeCommandEntry = { historyId -> activeCommandEntry = { historyId ->
podStateManager.createActiveCommand(historyId, basalProgram = basalProgram) podStateManager.createActiveCommand(historyId, basalProgram = basalProgram)
}, },
@ -1139,7 +1143,7 @@ class OmnipodDashPumpPlugin @Inject constructor(
return profileFunction.getProfile()?.let { return profileFunction.getProfile()?.let {
executeProgrammingCommand( executeProgrammingCommand(
pre = observeDeliverySuspended(), pre = observeDeliverySuspended(),
historyEntry = history.createRecord(OmnipodCommandType.RESUME_DELIVERY), historyEntry = history.createRecord(OmnipodCommandType.RESUME_DELIVERY, basalProfileRecord = BasalValuesRecord(it.getBasalValues().toList())),
command = omnipodManager.setBasalProgram(mapProfileToBasalProgram(it), hasBasalBeepEnabled()) command = omnipodManager.setBasalProgram(mapProfileToBasalProgram(it), hasBasalBeepEnabled())
.ignoreElements() .ignoreElements()
).doFinally { ).doFinally {

View file

@ -7,11 +7,7 @@ import info.nightscout.androidaps.plugins.pump.omnipod.common.definition.Omnipod
import info.nightscout.androidaps.plugins.pump.omnipod.common.definition.OmnipodCommandType.SET_BOLUS import info.nightscout.androidaps.plugins.pump.omnipod.common.definition.OmnipodCommandType.SET_BOLUS
import info.nightscout.androidaps.plugins.pump.omnipod.common.definition.OmnipodCommandType.SET_TEMPORARY_BASAL import info.nightscout.androidaps.plugins.pump.omnipod.common.definition.OmnipodCommandType.SET_TEMPORARY_BASAL
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.state.* import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.state.*
import info.nightscout.androidaps.plugins.pump.omnipod.dash.history.data.BolusRecord import info.nightscout.androidaps.plugins.pump.omnipod.dash.history.data.*
import info.nightscout.androidaps.plugins.pump.omnipod.dash.history.data.HistoryRecord
import info.nightscout.androidaps.plugins.pump.omnipod.dash.history.data.InitialResult
import info.nightscout.androidaps.plugins.pump.omnipod.dash.history.data.ResolvedResult
import info.nightscout.androidaps.plugins.pump.omnipod.dash.history.data.TempBasalRecord
import info.nightscout.androidaps.plugins.pump.omnipod.dash.history.database.HistoryRecordDao import info.nightscout.androidaps.plugins.pump.omnipod.dash.history.database.HistoryRecordDao
import info.nightscout.androidaps.plugins.pump.omnipod.dash.history.database.HistoryRecordEntity import info.nightscout.androidaps.plugins.pump.omnipod.dash.history.database.HistoryRecordEntity
import info.nightscout.androidaps.plugins.pump.omnipod.dash.history.mapper.HistoryMapper import info.nightscout.androidaps.plugins.pump.omnipod.dash.history.mapper.HistoryMapper
@ -53,6 +49,7 @@ class DashHistory @Inject constructor(
initialResult: InitialResult = InitialResult.NOT_SENT, initialResult: InitialResult = InitialResult.NOT_SENT,
tempBasalRecord: TempBasalRecord? = null, tempBasalRecord: TempBasalRecord? = null,
bolusRecord: BolusRecord? = null, bolusRecord: BolusRecord? = null,
basalProfileRecord: BasalValuesRecord? = null,
resolveResult: ResolvedResult? = null, resolveResult: ResolvedResult? = null,
resolvedAt: Long? = null resolvedAt: Long? = null
): Single<String> = Single.defer { ): Single<String> = Single.defer {
@ -72,6 +69,7 @@ class DashHistory @Inject constructor(
commandType = commandType, commandType = commandType,
tempBasalRecord = tempBasalRecord, tempBasalRecord = tempBasalRecord,
bolusRecord = bolusRecord, bolusRecord = bolusRecord,
basalProfileRecord = basalProfileRecord,
initialResult = initialResult, initialResult = initialResult,
resolvedResult = resolveResult, resolvedResult = resolveResult,
resolvedAt = resolvedAt resolvedAt = resolvedAt

View file

@ -1,6 +1,7 @@
package info.nightscout.androidaps.plugins.pump.omnipod.dash.history.data package info.nightscout.androidaps.plugins.pump.omnipod.dash.history.data
import info.nightscout.androidaps.data.DetailedBolusInfo import info.nightscout.androidaps.data.DetailedBolusInfo
import info.nightscout.androidaps.interfaces.Profile
sealed class Record sealed class Record
@ -8,6 +9,8 @@ data class BolusRecord(val amout: Double, val bolusType: BolusType) : Record()
data class TempBasalRecord(val duration: Int, val rate: Double) : Record() data class TempBasalRecord(val duration: Int, val rate: Double) : Record()
data class BasalValuesRecord(val segments: List<Profile.ProfileValue>) : Record()
enum class BolusType { enum class BolusType {
DEFAULT, SMB; DEFAULT, SMB;

View file

@ -1,6 +1,8 @@
package info.nightscout.androidaps.plugins.pump.omnipod.dash.history.database package info.nightscout.androidaps.plugins.pump.omnipod.dash.history.database
import androidx.room.TypeConverter import androidx.room.TypeConverter
import com.google.gson.GsonBuilder
import info.nightscout.androidaps.interfaces.Profile
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.dash.history.data.BolusType import info.nightscout.androidaps.plugins.pump.omnipod.dash.history.data.BolusType
import info.nightscout.androidaps.plugins.pump.omnipod.dash.history.data.InitialResult import info.nightscout.androidaps.plugins.pump.omnipod.dash.history.data.InitialResult
@ -31,4 +33,17 @@ class Converters {
@TypeConverter @TypeConverter
fun fromOmnipodCommandType(omnipodCommandType: OmnipodCommandType) = omnipodCommandType.name fun fromOmnipodCommandType(omnipodCommandType: OmnipodCommandType) = omnipodCommandType.name
@TypeConverter
fun toSegments(s: String?): List<Profile.ProfileValue> {
s ?: return emptyList()
val gson = GsonBuilder().create()
return gson.fromJson(s, Array<Profile.ProfileValue>::class.java).toList()
}
@TypeConverter
fun fromBasalValues(segments: List<Profile.ProfileValue>): String {
val gson = GsonBuilder().create()
return gson.toJson(segments)
}
} }

View file

@ -18,13 +18,13 @@ abstract class DashHistoryDatabase : RoomDatabase() {
companion object { companion object {
const val VERSION = 1 const val VERSION = 2
fun build(context: Context) = fun build(context: Context) =
Room.databaseBuilder( Room.databaseBuilder(
context.applicationContext, context.applicationContext,
DashHistoryDatabase::class.java, DashHistoryDatabase::class.java,
"omnipod_dash_history_database.db" "omnipod_dash_history_database.db",
) )
.fallbackToDestructiveMigration() .fallbackToDestructiveMigration()
.build() .build()

View file

@ -4,6 +4,7 @@ import androidx.room.Embedded
import androidx.room.Entity import androidx.room.Entity
import androidx.room.PrimaryKey import androidx.room.PrimaryKey
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.dash.history.data.BasalValuesRecord
import info.nightscout.androidaps.plugins.pump.omnipod.dash.history.data.BolusRecord import info.nightscout.androidaps.plugins.pump.omnipod.dash.history.data.BolusRecord
import info.nightscout.androidaps.plugins.pump.omnipod.dash.history.data.InitialResult import info.nightscout.androidaps.plugins.pump.omnipod.dash.history.data.InitialResult
import info.nightscout.androidaps.plugins.pump.omnipod.dash.history.data.ResolvedResult import info.nightscout.androidaps.plugins.pump.omnipod.dash.history.data.ResolvedResult
@ -18,6 +19,7 @@ data class HistoryRecordEntity(
val initialResult: InitialResult, val initialResult: InitialResult,
@Embedded(prefix = "tempBasalRecord_") val tempBasalRecord: TempBasalRecord?, @Embedded(prefix = "tempBasalRecord_") val tempBasalRecord: TempBasalRecord?,
@Embedded(prefix = "bolusRecord_") val bolusRecord: BolusRecord?, @Embedded(prefix = "bolusRecord_") val bolusRecord: BolusRecord?,
@Embedded(prefix = "basalprofile_") val basalProfileRecord: BasalValuesRecord?,
val resolvedResult: ResolvedResult?, val resolvedResult: ResolvedResult?,
val resolvedAt: Long? val resolvedAt: Long?
) { ) {

View file

@ -1,5 +1,6 @@
package info.nightscout.androidaps.plugins.pump.omnipod.dash.history.mapper package info.nightscout.androidaps.plugins.pump.omnipod.dash.history.mapper
import info.nightscout.androidaps.plugins.pump.omnipod.dash.history.data.BasalValuesRecord
import info.nightscout.androidaps.plugins.pump.omnipod.dash.history.data.BolusRecord import info.nightscout.androidaps.plugins.pump.omnipod.dash.history.data.BolusRecord
import info.nightscout.androidaps.plugins.pump.omnipod.dash.history.data.HistoryRecord import info.nightscout.androidaps.plugins.pump.omnipod.dash.history.data.HistoryRecord
import info.nightscout.androidaps.plugins.pump.omnipod.dash.history.data.TempBasalRecord import info.nightscout.androidaps.plugins.pump.omnipod.dash.history.data.TempBasalRecord
@ -17,7 +18,8 @@ class HistoryMapper {
tempBasalRecord = historyRecord.record as? TempBasalRecord, tempBasalRecord = historyRecord.record as? TempBasalRecord,
bolusRecord = historyRecord.record as? BolusRecord, bolusRecord = historyRecord.record as? BolusRecord,
resolvedResult = historyRecord.resolvedResult, resolvedResult = historyRecord.resolvedResult,
resolvedAt = historyRecord.resolvedAt resolvedAt = historyRecord.resolvedAt,
basalProfileRecord = historyRecord.record as? BasalValuesRecord
) )
fun entityToDomain(entity: HistoryRecordEntity): HistoryRecord = fun entityToDomain(entity: HistoryRecordEntity): HistoryRecord =
@ -27,7 +29,7 @@ class HistoryMapper {
date = entity.date, date = entity.date,
initialResult = entity.initialResult, initialResult = entity.initialResult,
commandType = entity.commandType, commandType = entity.commandType,
record = entity.bolusRecord ?: entity.tempBasalRecord, record = entity.bolusRecord ?: entity.tempBasalRecord ?: entity.basalProfileRecord,
resolvedResult = entity.resolvedResult, resolvedResult = entity.resolvedResult,
resolvedAt = entity.resolvedAt resolvedAt = entity.resolvedAt
) )

View file

@ -5,11 +5,13 @@ import dagger.android.HasAndroidInjector
import info.nightscout.androidaps.data.PumpEnactResult import info.nightscout.androidaps.data.PumpEnactResult
import info.nightscout.androidaps.logging.AAPSLogger import info.nightscout.androidaps.logging.AAPSLogger
import info.nightscout.androidaps.logging.LTag import info.nightscout.androidaps.logging.LTag
import info.nightscout.androidaps.plugins.pump.omnipod.common.definition.OmnipodCommandType
import info.nightscout.androidaps.plugins.pump.omnipod.common.ui.wizard.activation.viewmodel.action.InitializePodViewModel 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.R
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.pod.definition.AlertTrigger import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.definition.AlertTrigger
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.state.OmnipodDashPodStateManager import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.state.OmnipodDashPodStateManager
import info.nightscout.androidaps.plugins.pump.omnipod.dash.history.DashHistory
import info.nightscout.androidaps.plugins.pump.omnipod.dash.util.I8n import info.nightscout.androidaps.plugins.pump.omnipod.dash.util.I8n
import info.nightscout.androidaps.utils.resources.ResourceHelper import info.nightscout.androidaps.utils.resources.ResourceHelper
import info.nightscout.androidaps.utils.sharedPreferences.SP import info.nightscout.androidaps.utils.sharedPreferences.SP
@ -24,7 +26,9 @@ class DashInitializePodViewModel @Inject constructor(
logger: AAPSLogger, logger: AAPSLogger,
private val sp: SP, private val sp: SP,
private val podStateManager: OmnipodDashPodStateManager, private val podStateManager: OmnipodDashPodStateManager,
private val resourceHelper: ResourceHelper private val resourceHelper: ResourceHelper,
private val history: DashHistory,
) : InitializePodViewModel(injector, logger) { ) : InitializePodViewModel(injector, logger) {
override fun isPodInAlarm(): Boolean = false // TODO override fun isPodInAlarm(): Boolean = false // TODO
@ -41,27 +45,24 @@ class DashInitializePodViewModel @Inject constructor(
} else } else
null null
super.disposable += omnipodManager.activatePodPart1(lowReservoirAlertTrigger).subscribeBy( super.disposable += omnipodManager.activatePodPart1(lowReservoirAlertTrigger)
onNext = { podEvent -> .ignoreElements()
logger.debug( .andThen(podStateManager.updateLowReservoirAlertSettings(lowReservoirAlertEnabled, lowReservoirAlertUnits))
LTag.PUMP, .andThen(history.createRecord(OmnipodCommandType.INITIALIZE_POD).ignoreElement())
"Received PodEvent in Pod activation part 1: $podEvent" .subscribeBy(
) onError = { throwable ->
}, logger.error(LTag.PUMP, "Error in Pod activation part 1", throwable)
onError = { throwable -> source.onSuccess(
logger.error(LTag.PUMP, "Error in Pod activation part 1", throwable) PumpEnactResult(injector)
source.onSuccess( .success(false)
PumpEnactResult(injector) .comment(I8n.textFromException(throwable, resourceHelper))
.success(false) )
.comment(I8n.textFromException(throwable, resourceHelper)) },
) onComplete = {
}, logger.debug("Pod activation part 1 completed")
onComplete = { source.onSuccess(PumpEnactResult(injector).success(true))
logger.debug("Pod activation part 1 completed") }
podStateManager.updateLowReservoirAlertSettings(lowReservoirAlertEnabled, lowReservoirAlertUnits) )
source.onSuccess(PumpEnactResult(injector).success(true))
}
)
} }
@StringRes @StringRes

View file

@ -12,10 +12,12 @@ import info.nightscout.androidaps.plugins.bus.RxBus
import info.nightscout.androidaps.plugins.general.overview.events.EventDismissNotification import info.nightscout.androidaps.plugins.general.overview.events.EventDismissNotification
import info.nightscout.androidaps.plugins.general.overview.notifications.Notification import info.nightscout.androidaps.plugins.general.overview.notifications.Notification
import info.nightscout.androidaps.plugins.pump.common.defs.PumpType 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.ui.wizard.activation.viewmodel.action.InsertCannulaViewModel import info.nightscout.androidaps.plugins.pump.omnipod.common.ui.wizard.activation.viewmodel.action.InsertCannulaViewModel
import info.nightscout.androidaps.plugins.pump.omnipod.dash.R import info.nightscout.androidaps.plugins.pump.omnipod.dash.R
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.pod.state.OmnipodDashPodStateManager import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.state.OmnipodDashPodStateManager
import info.nightscout.androidaps.plugins.pump.omnipod.dash.history.DashHistory
import info.nightscout.androidaps.plugins.pump.omnipod.dash.util.Constants import info.nightscout.androidaps.plugins.pump.omnipod.dash.util.Constants
import info.nightscout.androidaps.plugins.pump.omnipod.dash.util.I8n import info.nightscout.androidaps.plugins.pump.omnipod.dash.util.I8n
import info.nightscout.androidaps.plugins.pump.omnipod.dash.util.mapProfileToBasalProgram import info.nightscout.androidaps.plugins.pump.omnipod.dash.util.mapProfileToBasalProgram
@ -34,6 +36,7 @@ class DashInsertCannulaViewModel @Inject constructor(
private val rxBus: RxBus, private val rxBus: RxBus,
private val sp: SP, private val sp: SP,
private val resourceHelper: ResourceHelper, private val resourceHelper: ResourceHelper,
private val history: DashHistory,
injector: HasAndroidInjector, injector: HasAndroidInjector,
logger: AAPSLogger logger: AAPSLogger
@ -67,6 +70,7 @@ class DashInsertCannulaViewModel @Inject constructor(
super.disposable += omnipodManager.activatePodPart2(basalProgram, expirationHoursBeforeShutdown) super.disposable += omnipodManager.activatePodPart2(basalProgram, expirationHoursBeforeShutdown)
.ignoreElements() .ignoreElements()
.andThen(podStateManager.updateExpirationAlertSettings(expirationReminderEnabled, expirationHours)) .andThen(podStateManager.updateExpirationAlertSettings(expirationReminderEnabled, expirationHours))
.andThen(history.createRecord(OmnipodCommandType.INSERT_CANNULA).ignoreElement())
.subscribeBy( .subscribeBy(
onError = { throwable -> onError = { throwable ->
logger.error(LTag.PUMP, "Error in Pod activation part 2", throwable) logger.error(LTag.PUMP, "Error in Pod activation part 2", throwable)
@ -98,7 +102,6 @@ class DashInsertCannulaViewModel @Inject constructor(
pumpType = PumpType.OMNIPOD_DASH, pumpType = PumpType.OMNIPOD_DASH,
pumpSerial = podStateManager.uniqueId?.toString() ?: "n/a" pumpSerial = podStateManager.uniqueId?.toString() ?: "n/a"
) )
rxBus.send(EventDismissNotification(Notification.OMNIPOD_POD_NOT_ATTACHED)) rxBus.send(EventDismissNotification(Notification.OMNIPOD_POD_NOT_ATTACHED))
source.onSuccess(PumpEnactResult(injector).success(true)) source.onSuccess(PumpEnactResult(injector).success(true))
} }