NSCv3: process NSTemporaryBasal

This commit is contained in:
Milos Kozak 2022-12-26 14:04:27 +01:00
parent bb5d5a07ea
commit 94af8a7106
6 changed files with 116 additions and 14 deletions

View file

@ -4,17 +4,17 @@ import info.nightscout.sdk.localmodel.entry.NsUnits
data class NSTemporaryBasal( data class NSTemporaryBasal(
override val date: Long, override val date: Long,
override val device: String?, override val device: String? = null,
override val identifier: String?, override val identifier: String?,
override val units: NsUnits?, override val units: NsUnits? = null,
override val srvModified: Long?, override val srvModified: Long? = null,
override val srvCreated: Long?, override val srvCreated: Long? = null,
override val utcOffset: Long, override val utcOffset: Long,
override val subject: String?, override val subject: String? = null,
override var isReadOnly: Boolean, override var isReadOnly: Boolean = false,
override val isValid: Boolean, override val isValid: Boolean,
override val eventType: EventType, override val eventType: EventType,
override val notes: String?, override val notes: String? = null,
override val pumpId: Long?, override val pumpId: Long?,
override val endId: Long?, override val endId: Long?,
override val pumpType: String?, override val pumpType: String?,

View file

@ -21,8 +21,6 @@ import java.util.concurrent.TimeUnit
* Convert to [RemoteTreatment] and back to [NSTreatment] * Convert to [RemoteTreatment] and back to [NSTreatment]
* testing purpose only * testing purpose only
* *
* @param treatment original
*
* @return treatment after double conversion * @return treatment after double conversion
*/ */
fun NSTreatment.convertToRemoteAndBack(): NSTreatment? = fun NSTreatment.convertToRemoteAndBack(): NSTreatment? =
@ -461,7 +459,7 @@ internal fun NSTreatment.toRemoteTreatment(): RemoteTreatment? =
durationInMilliseconds = duration, durationInMilliseconds = duration,
absolute = absolute, absolute = absolute,
percent = percent, percent = percent,
rate = absolute, rate = rate,
type = type.name type = type.name
) )

View file

@ -47,7 +47,7 @@ data class TemporaryBasal(
require(duration > 0) require(duration > 0)
} }
private fun contentEqualsTo(other: TemporaryBasal): Boolean = fun contentEqualsTo(other: TemporaryBasal): Boolean =
isValid == other.isValid && isValid == other.isValid &&
timestamp == other.timestamp && timestamp == other.timestamp &&
utcOffset == other.utcOffset && utcOffset == other.utcOffset &&

View file

@ -23,6 +23,7 @@ import info.nightscout.interfaces.nsclient.StoreDataForDb
import info.nightscout.interfaces.plugin.PluginBase import info.nightscout.interfaces.plugin.PluginBase
import info.nightscout.interfaces.plugin.PluginDescription import info.nightscout.interfaces.plugin.PluginDescription
import info.nightscout.interfaces.plugin.PluginType import info.nightscout.interfaces.plugin.PluginType
import info.nightscout.interfaces.profile.ProfileFunction
import info.nightscout.interfaces.sync.DataSyncSelector import info.nightscout.interfaces.sync.DataSyncSelector
import info.nightscout.interfaces.sync.NsClient import info.nightscout.interfaces.sync.NsClient
import info.nightscout.interfaces.sync.Sync 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.toNSCarbs
import info.nightscout.plugins.sync.nsclientV3.extensions.toNSEffectiveProfileSwitch import info.nightscout.plugins.sync.nsclientV3.extensions.toNSEffectiveProfileSwitch
import info.nightscout.plugins.sync.nsclientV3.extensions.toNSProfileSwitch 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.LoadBgWorker
import info.nightscout.plugins.sync.nsclientV3.workers.LoadLastModificationWorker import info.nightscout.plugins.sync.nsclientV3.workers.LoadLastModificationWorker
import info.nightscout.plugins.sync.nsclientV3.workers.LoadStatusWorker import info.nightscout.plugins.sync.nsclientV3.workers.LoadStatusWorker
@ -85,7 +87,8 @@ class NSClientV3Plugin @Inject constructor(
private val dateUtil: DateUtil, private val dateUtil: DateUtil,
private val uiInteraction: UiInteraction, private val uiInteraction: UiInteraction,
private val storeDataForDb: StoreDataForDb, private val storeDataForDb: StoreDataForDb,
private val dataSyncSelector: DataSyncSelector private val dataSyncSelector: DataSyncSelector,
private val profileFunction: ProfileFunction
) : NsClient, Sync, PluginBase( ) : NsClient, Sync, PluginBase(
PluginDescription() PluginDescription()
.mainType(PluginType.SYNC) .mainType(PluginType.SYNC)
@ -327,7 +330,14 @@ class NSClientV3Plugin @Inject constructor(
// is DataSyncSelector.PairFood -> dataPair.value.toJson(false) // is DataSyncSelector.PairFood -> dataPair.value.toJson(false)
// is DataSyncSelector.PairGlucoseValue -> dataPair.value.toJson(false, dateUtil) // is DataSyncSelector.PairGlucoseValue -> dataPair.value.toJson(false, dateUtil)
// is DataSyncSelector.PairTherapyEvent -> 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.PairExtendedBolus -> dataPair.value.toJson(false, profileFunction.getProfile(dataPair.value.timestamp), dateUtil)
is DataSyncSelector.PairProfileSwitch -> dataPair.value.toNSProfileSwitch(dateUtil) is DataSyncSelector.PairProfileSwitch -> dataPair.value.toNSProfileSwitch(dateUtil)
is DataSyncSelector.PairEffectiveProfileSwitch -> dataPair.value.toNSEffectiveProfileSwitch(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.PairFood -> dataPair.value.toJson(false)
// is DataSyncSelector.PairGlucoseValue -> dataPair.value.toJson(false, dateUtil) // is DataSyncSelector.PairGlucoseValue -> dataPair.value.toJson(false, dateUtil)
// is DataSyncSelector.PairTherapyEvent -> 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.PairExtendedBolus -> dataPair.value.toJson(false, profileFunction.getProfile(dataPair.value.timestamp), dateUtil)
is DataSyncSelector.PairProfileSwitch -> { is DataSyncSelector.PairProfileSwitch -> {
if (result.response == 201) { // created if (result.response == 201) { // created

View file

@ -1,7 +1,11 @@
package info.nightscout.plugins.sync.nsclientV3.extensions package info.nightscout.plugins.sync.nsclientV3.extensions
import info.nightscout.core.extensions.convertedToAbsolute
import info.nightscout.database.entities.TemporaryBasal import info.nightscout.database.entities.TemporaryBasal
import info.nightscout.database.entities.TherapyEvent
import info.nightscout.database.entities.embedments.InterfaceIDs 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 import info.nightscout.sdk.localmodel.treatment.NSTemporaryBasal
fun NSTemporaryBasal.toTemporaryBasal(): TemporaryBasal = fun NSTemporaryBasal.toTemporaryBasal(): TemporaryBasal =
@ -24,4 +28,33 @@ fun NSTemporaryBasal.Type?.toType(): TemporaryBasal.Type =
NSTemporaryBasal.Type.SUPERBOLUS -> TemporaryBasal.Type.SUPERBOLUS NSTemporaryBasal.Type.SUPERBOLUS -> TemporaryBasal.Type.SUPERBOLUS
NSTemporaryBasal.Type.FAKE_EXTENDED -> TemporaryBasal.Type.FAKE_EXTENDED NSTemporaryBasal.Type.FAKE_EXTENDED -> TemporaryBasal.Type.FAKE_EXTENDED
null -> TemporaryBasal.Type.NORMAL 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
} }

View file

@ -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))
}
}