From 94af8a7106d50b6bb8f4f7605a592fcd3de2e6c1 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Mon, 26 Dec 2022 14:04:27 +0100 Subject: [PATCH] NSCv3: process NSTemporaryBasal --- .../localmodel/treatment/NSTemporaryBasal.kt | 14 ++--- .../nightscout/sdk/mapper/TreatmentMapper.kt | 4 +- .../database/entities/TemporaryBasal.kt | 2 +- .../sync/nsclientV3/NSClientV3Plugin.kt | 23 ++++++-- .../extensions/TemporaryBasalExtension.kt | 33 ++++++++++++ .../TemporaryBasalExtensionKtTest.kt | 54 +++++++++++++++++++ 6 files changed, 116 insertions(+), 14 deletions(-) create mode 100644 plugins/sync/src/test/java/info/nightscout/plugins/sync/nsclientV3/extensions/TemporaryBasalExtensionKtTest.kt diff --git a/core/ns-sdk/src/main/java/info/nightscout/sdk/localmodel/treatment/NSTemporaryBasal.kt b/core/ns-sdk/src/main/java/info/nightscout/sdk/localmodel/treatment/NSTemporaryBasal.kt index 88d588aca2..b55b8b8e04 100644 --- a/core/ns-sdk/src/main/java/info/nightscout/sdk/localmodel/treatment/NSTemporaryBasal.kt +++ b/core/ns-sdk/src/main/java/info/nightscout/sdk/localmodel/treatment/NSTemporaryBasal.kt @@ -4,17 +4,17 @@ import info.nightscout.sdk.localmodel.entry.NsUnits data class NSTemporaryBasal( override val date: Long, - override val device: String?, + override val device: String? = null, override val identifier: String?, - override val units: NsUnits?, - override val srvModified: Long?, - override val srvCreated: Long?, + override val units: NsUnits? = null, + override val srvModified: Long? = null, + override val srvCreated: Long? = null, override val utcOffset: Long, - override val subject: String?, - override var isReadOnly: Boolean, + override val subject: String? = null, + override var isReadOnly: Boolean = false, override val isValid: Boolean, override val eventType: EventType, - override val notes: String?, + override val notes: String? = null, override val pumpId: Long?, override val endId: Long?, override val pumpType: String?, diff --git a/core/ns-sdk/src/main/java/info/nightscout/sdk/mapper/TreatmentMapper.kt b/core/ns-sdk/src/main/java/info/nightscout/sdk/mapper/TreatmentMapper.kt index e3104265b0..2a1eadf39e 100644 --- a/core/ns-sdk/src/main/java/info/nightscout/sdk/mapper/TreatmentMapper.kt +++ b/core/ns-sdk/src/main/java/info/nightscout/sdk/mapper/TreatmentMapper.kt @@ -21,8 +21,6 @@ import java.util.concurrent.TimeUnit * Convert to [RemoteTreatment] and back to [NSTreatment] * testing purpose only * - * @param treatment original - * * @return treatment after double conversion */ fun NSTreatment.convertToRemoteAndBack(): NSTreatment? = @@ -461,7 +459,7 @@ internal fun NSTreatment.toRemoteTreatment(): RemoteTreatment? = durationInMilliseconds = duration, absolute = absolute, percent = percent, - rate = absolute, + rate = rate, type = type.name ) diff --git a/database/entities/src/main/java/info/nightscout/database/entities/TemporaryBasal.kt b/database/entities/src/main/java/info/nightscout/database/entities/TemporaryBasal.kt index dd6c2a9c1b..981950978c 100644 --- a/database/entities/src/main/java/info/nightscout/database/entities/TemporaryBasal.kt +++ b/database/entities/src/main/java/info/nightscout/database/entities/TemporaryBasal.kt @@ -47,7 +47,7 @@ data class TemporaryBasal( require(duration > 0) } - private fun contentEqualsTo(other: TemporaryBasal): Boolean = + fun contentEqualsTo(other: TemporaryBasal): Boolean = isValid == other.isValid && timestamp == other.timestamp && utcOffset == other.utcOffset && diff --git a/plugins/sync/src/main/java/info/nightscout/plugins/sync/nsclientV3/NSClientV3Plugin.kt b/plugins/sync/src/main/java/info/nightscout/plugins/sync/nsclientV3/NSClientV3Plugin.kt index 09e12f36dd..b8fe1a0d64 100644 --- a/plugins/sync/src/main/java/info/nightscout/plugins/sync/nsclientV3/NSClientV3Plugin.kt +++ b/plugins/sync/src/main/java/info/nightscout/plugins/sync/nsclientV3/NSClientV3Plugin.kt @@ -23,6 +23,7 @@ import info.nightscout.interfaces.nsclient.StoreDataForDb import info.nightscout.interfaces.plugin.PluginBase import info.nightscout.interfaces.plugin.PluginDescription import info.nightscout.interfaces.plugin.PluginType +import info.nightscout.interfaces.profile.ProfileFunction import info.nightscout.interfaces.sync.DataSyncSelector import info.nightscout.interfaces.sync.NsClient import info.nightscout.interfaces.sync.Sync @@ -37,6 +38,7 @@ import info.nightscout.plugins.sync.nsclientV3.extensions.toNSBolus import info.nightscout.plugins.sync.nsclientV3.extensions.toNSCarbs import info.nightscout.plugins.sync.nsclientV3.extensions.toNSEffectiveProfileSwitch import info.nightscout.plugins.sync.nsclientV3.extensions.toNSProfileSwitch +import info.nightscout.plugins.sync.nsclientV3.extensions.toNSTemporaryBasal import info.nightscout.plugins.sync.nsclientV3.workers.LoadBgWorker import info.nightscout.plugins.sync.nsclientV3.workers.LoadLastModificationWorker import info.nightscout.plugins.sync.nsclientV3.workers.LoadStatusWorker @@ -85,7 +87,8 @@ class NSClientV3Plugin @Inject constructor( private val dateUtil: DateUtil, private val uiInteraction: UiInteraction, private val storeDataForDb: StoreDataForDb, - private val dataSyncSelector: DataSyncSelector + private val dataSyncSelector: DataSyncSelector, + private val profileFunction: ProfileFunction ) : NsClient, Sync, PluginBase( PluginDescription() .mainType(PluginType.SYNC) @@ -327,7 +330,14 @@ class NSClientV3Plugin @Inject constructor( // is DataSyncSelector.PairFood -> dataPair.value.toJson(false) // is DataSyncSelector.PairGlucoseValue -> dataPair.value.toJson(false, dateUtil) // is DataSyncSelector.PairTherapyEvent -> dataPair.value.toJson(false, dateUtil) - // is DataSyncSelector.PairTemporaryBasal -> dataPair.value.toJson(false, profileFunction.getProfile(dataPair.value.timestamp), dateUtil) + is DataSyncSelector.PairTemporaryBasal -> { + val profile = profileFunction.getProfile(dataPair.value.timestamp) + if (profile == null) { + dataSyncSelector.confirmLastTemporaryBasalIdIfGreater(dataPair.id) + return + } + dataPair.value.toNSTemporaryBasal(profile) + } // is DataSyncSelector.PairExtendedBolus -> dataPair.value.toJson(false, profileFunction.getProfile(dataPair.value.timestamp), dateUtil) is DataSyncSelector.PairProfileSwitch -> dataPair.value.toNSProfileSwitch(dateUtil) is DataSyncSelector.PairEffectiveProfileSwitch -> dataPair.value.toNSEffectiveProfileSwitch(dateUtil) @@ -373,7 +383,14 @@ class NSClientV3Plugin @Inject constructor( // is DataSyncSelector.PairFood -> dataPair.value.toJson(false) // is DataSyncSelector.PairGlucoseValue -> dataPair.value.toJson(false, dateUtil) // is DataSyncSelector.PairTherapyEvent -> dataPair.value.toJson(false, dateUtil) - // is DataSyncSelector.PairTemporaryBasal -> dataPair.value.toJson(false, profileFunction.getProfile(dataPair.value.timestamp), dateUtil) + is DataSyncSelector.PairTemporaryBasal -> { + if (result.response == 201) { // created + dataPair.value.interfaceIDs.nightscoutId = result.identifier + storeDataForDb.nsIdTemporaryBasals.add(dataPair.value) + storeDataForDb.scheduleNsIdUpdate() + } + dataSyncSelector.confirmLastTemporaryBasalIdIfGreater(dataPair.id) + } // is DataSyncSelector.PairExtendedBolus -> dataPair.value.toJson(false, profileFunction.getProfile(dataPair.value.timestamp), dateUtil) is DataSyncSelector.PairProfileSwitch -> { if (result.response == 201) { // created diff --git a/plugins/sync/src/main/java/info/nightscout/plugins/sync/nsclientV3/extensions/TemporaryBasalExtension.kt b/plugins/sync/src/main/java/info/nightscout/plugins/sync/nsclientV3/extensions/TemporaryBasalExtension.kt index 78463fd7a2..dc68a72cf9 100644 --- a/plugins/sync/src/main/java/info/nightscout/plugins/sync/nsclientV3/extensions/TemporaryBasalExtension.kt +++ b/plugins/sync/src/main/java/info/nightscout/plugins/sync/nsclientV3/extensions/TemporaryBasalExtension.kt @@ -1,7 +1,11 @@ package info.nightscout.plugins.sync.nsclientV3.extensions +import info.nightscout.core.extensions.convertedToAbsolute import info.nightscout.database.entities.TemporaryBasal +import info.nightscout.database.entities.TherapyEvent import info.nightscout.database.entities.embedments.InterfaceIDs +import info.nightscout.interfaces.profile.Profile +import info.nightscout.sdk.localmodel.treatment.EventType import info.nightscout.sdk.localmodel.treatment.NSTemporaryBasal fun NSTemporaryBasal.toTemporaryBasal(): TemporaryBasal = @@ -24,4 +28,33 @@ fun NSTemporaryBasal.Type?.toType(): TemporaryBasal.Type = NSTemporaryBasal.Type.SUPERBOLUS -> TemporaryBasal.Type.SUPERBOLUS NSTemporaryBasal.Type.FAKE_EXTENDED -> TemporaryBasal.Type.FAKE_EXTENDED null -> TemporaryBasal.Type.NORMAL + } + +fun TemporaryBasal.toNSTemporaryBasal(profile: Profile): NSTemporaryBasal = + NSTemporaryBasal( + eventType = EventType.fromString(TherapyEvent.Type.TEMPORARY_BASAL.text), + isValid = isValid, + date = timestamp, + utcOffset = utcOffset, + type = type.toType(), + rate = convertedToAbsolute(timestamp, profile), + isAbsolute = isAbsolute, + absolute = if (isAbsolute) rate else null, + percent = if (!isAbsolute) rate - 100 else null, + duration = duration, + identifier = interfaceIDs.nightscoutId, + pumpId = interfaceIDs.pumpId, + pumpType = interfaceIDs.pumpType?.name, + pumpSerial = interfaceIDs.pumpSerial, + endId = interfaceIDs.endId + ) + +fun TemporaryBasal.Type?.toType(): NSTemporaryBasal.Type = + when (this) { + TemporaryBasal.Type.NORMAL -> NSTemporaryBasal.Type.NORMAL + TemporaryBasal.Type.EMULATED_PUMP_SUSPEND -> NSTemporaryBasal.Type.EMULATED_PUMP_SUSPEND + TemporaryBasal.Type.PUMP_SUSPEND -> NSTemporaryBasal.Type.PUMP_SUSPEND + TemporaryBasal.Type.SUPERBOLUS -> NSTemporaryBasal.Type.SUPERBOLUS + TemporaryBasal.Type.FAKE_EXTENDED -> NSTemporaryBasal.Type.FAKE_EXTENDED + null -> NSTemporaryBasal.Type.NORMAL } \ No newline at end of file diff --git a/plugins/sync/src/test/java/info/nightscout/plugins/sync/nsclientV3/extensions/TemporaryBasalExtensionKtTest.kt b/plugins/sync/src/test/java/info/nightscout/plugins/sync/nsclientV3/extensions/TemporaryBasalExtensionKtTest.kt new file mode 100644 index 0000000000..9b4ed52fff --- /dev/null +++ b/plugins/sync/src/test/java/info/nightscout/plugins/sync/nsclientV3/extensions/TemporaryBasalExtensionKtTest.kt @@ -0,0 +1,54 @@ +package info.nightscout.plugins.sync.nsclientV3.extensions + +import info.nightscout.androidaps.TestBaseWithProfile +import info.nightscout.database.entities.TemporaryBasal +import info.nightscout.database.entities.embedments.InterfaceIDs +import info.nightscout.sdk.localmodel.treatment.NSTemporaryBasal +import info.nightscout.sdk.mapper.convertToRemoteAndBack +import org.junit.jupiter.api.Assertions +import org.junit.jupiter.api.Test + +@Suppress("SpellCheckingInspection") +internal class TemporaryBasalExtensionKtTest : TestBaseWithProfile() { + + @Test + fun toTemporaryBasal() { + var temporaryBasal = TemporaryBasal( + timestamp = 10000, + isValid = true, + type = TemporaryBasal.Type.NORMAL, + rate = 2.0, + isAbsolute = true, + duration = 3600000, + interfaceIDs_backing = InterfaceIDs( + nightscoutId = "nightscoutId", + pumpId = 11000, + pumpType = InterfaceIDs.PumpType.DANA_I, + pumpSerial = "bbbb" + ) + ) + + var temporaryBasal2 = (temporaryBasal.toNSTemporaryBasal(validProfile).convertToRemoteAndBack() as NSTemporaryBasal).toTemporaryBasal() + Assertions.assertTrue(temporaryBasal.contentEqualsTo(temporaryBasal2)) + Assertions.assertTrue(temporaryBasal.interfaceIdsEqualsTo(temporaryBasal2)) + + temporaryBasal = TemporaryBasal( + timestamp = 10000, + isValid = true, + type = TemporaryBasal.Type.PUMP_SUSPEND, + rate = 120.0, + isAbsolute = false, + duration = 30000, + interfaceIDs_backing = InterfaceIDs( + nightscoutId = "nightscoutId", + pumpId = 11000, + pumpType = InterfaceIDs.PumpType.DANA_I, + pumpSerial = "bbbb" + ) + ) + + temporaryBasal2 = (temporaryBasal.toNSTemporaryBasal(validProfile).convertToRemoteAndBack() as NSTemporaryBasal).toTemporaryBasal() + Assertions.assertTrue(temporaryBasal.contentEqualsTo(temporaryBasal2)) + Assertions.assertTrue(temporaryBasal.interfaceIdsEqualsTo(temporaryBasal2)) + } +} \ No newline at end of file