diff --git a/core/ns-sdk/src/main/java/info/nightscout/sdk/localmodel/treatment/NSTemporaryTarget.kt b/core/ns-sdk/src/main/java/info/nightscout/sdk/localmodel/treatment/NSTemporaryTarget.kt index 4e151dab37..49a11cfe69 100644 --- a/core/ns-sdk/src/main/java/info/nightscout/sdk/localmodel/treatment/NSTemporaryTarget.kt +++ b/core/ns-sdk/src/main/java/info/nightscout/sdk/localmodel/treatment/NSTemporaryTarget.kt @@ -4,17 +4,17 @@ import info.nightscout.sdk.localmodel.entry.NsUnits data class NSTemporaryTarget( 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 srvModified: Long? = null, + override val srvCreated: Long? = null, override val utcOffset: Long, - override val subject: String?, - override var isReadOnly: Boolean, - override val isValid: Boolean, + override val subject: String? = null, + override var isReadOnly: Boolean = false, + override val isValid: Boolean = true, 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/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 b8fe1a0d64..6bdc650817 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 @@ -39,6 +39,7 @@ 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.extensions.toNSTemporaryTarget import info.nightscout.plugins.sync.nsclientV3.workers.LoadBgWorker import info.nightscout.plugins.sync.nsclientV3.workers.LoadLastModificationWorker import info.nightscout.plugins.sync.nsclientV3.workers.LoadStatusWorker @@ -326,7 +327,7 @@ class NSClientV3Plugin @Inject constructor( is DataSyncSelector.PairBolus -> dataPair.value.toNSBolus() is DataSyncSelector.PairCarbs -> dataPair.value.toNSCarbs() // is DataSyncSelector.PairBolusCalculatorResult -> dataPair.value.toJson(false, dateUtil, profileFunction) - // is DataSyncSelector.PairTemporaryTarget -> dataPair.value.toJson(false, profileFunction.getUnits(), dateUtil) + is DataSyncSelector.PairTemporaryTarget -> dataPair.value.toNSTemporaryTarget() // is DataSyncSelector.PairFood -> dataPair.value.toJson(false) // is DataSyncSelector.PairGlucoseValue -> dataPair.value.toJson(false, dateUtil) // is DataSyncSelector.PairTherapyEvent -> dataPair.value.toJson(false, dateUtil) @@ -370,6 +371,7 @@ class NSClientV3Plugin @Inject constructor( } dataSyncSelector.confirmLastBolusIdIfGreater(dataPair.id) } + is DataSyncSelector.PairCarbs -> { if (result.response == 201) { // created dataPair.value.interfaceIDs.nightscoutId = result.identifier @@ -379,11 +381,18 @@ class NSClientV3Plugin @Inject constructor( dataSyncSelector.confirmLastCarbsIdIfGreater(dataPair.id) } // is DataSyncSelector.PairBolusCalculatorResult -> dataPair.value.toJson(false, dateUtil, profileFunction) - // is DataSyncSelector.PairTemporaryTarget -> dataPair.value.toJson(false, profileFunction.getUnits(), dateUtil) + is DataSyncSelector.PairTemporaryTarget -> { + if (result.response == 201) { // created + dataPair.value.interfaceIDs.nightscoutId = result.identifier + storeDataForDb.nsIdTemporaryTargets.add(dataPair.value) + storeDataForDb.scheduleNsIdUpdate() + } + dataSyncSelector.confirmLastTempTargetsIdIfGreater(dataPair.id) + } // 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 -> { + is DataSyncSelector.PairTemporaryBasal -> { if (result.response == 201) { // created dataPair.value.interfaceIDs.nightscoutId = result.identifier storeDataForDb.nsIdTemporaryBasals.add(dataPair.value) diff --git a/plugins/sync/src/main/java/info/nightscout/plugins/sync/nsclientV3/extensions/BolusExtension.kt b/plugins/sync/src/main/java/info/nightscout/plugins/sync/nsclientV3/extensions/BolusExtension.kt index 2b5a059d24..c5eabb4f84 100644 --- a/plugins/sync/src/main/java/info/nightscout/plugins/sync/nsclientV3/extensions/BolusExtension.kt +++ b/plugins/sync/src/main/java/info/nightscout/plugins/sync/nsclientV3/extensions/BolusExtension.kt @@ -1,7 +1,6 @@ package info.nightscout.plugins.sync.nsclientV3.extensions import info.nightscout.database.entities.Bolus -import info.nightscout.database.entities.TherapyEvent import info.nightscout.database.entities.embedments.InterfaceIDs import info.nightscout.sdk.localmodel.treatment.EventType import info.nightscout.sdk.localmodel.treatment.NSBolus @@ -28,7 +27,7 @@ fun NSBolus.BolusType?.toBolusType(): Bolus.Type = fun Bolus.toNSBolus(): NSBolus = NSBolus( - eventType = EventType.fromString(if (type == Bolus.Type.SMB) TherapyEvent.Type.CORRECTION_BOLUS.text else TherapyEvent.Type.MEAL_BOLUS.text), + eventType = if (type == Bolus.Type.SMB) EventType.CORRECTION_BOLUS else EventType.MEAL_BOLUS, isValid = isValid, date = timestamp, utcOffset = utcOffset, diff --git a/plugins/sync/src/main/java/info/nightscout/plugins/sync/nsclientV3/extensions/CarbsExtension.kt b/plugins/sync/src/main/java/info/nightscout/plugins/sync/nsclientV3/extensions/CarbsExtension.kt index 6e9146c30f..732ed5da82 100644 --- a/plugins/sync/src/main/java/info/nightscout/plugins/sync/nsclientV3/extensions/CarbsExtension.kt +++ b/plugins/sync/src/main/java/info/nightscout/plugins/sync/nsclientV3/extensions/CarbsExtension.kt @@ -1,7 +1,6 @@ package info.nightscout.plugins.sync.nsclientV3.extensions import info.nightscout.database.entities.Carbs -import info.nightscout.database.entities.TherapyEvent import info.nightscout.database.entities.embedments.InterfaceIDs import info.nightscout.sdk.localmodel.treatment.EventType import info.nightscout.sdk.localmodel.treatment.NSCarbs @@ -19,7 +18,7 @@ fun NSCarbs.toCarbs(): Carbs = fun Carbs.toNSCarbs(): NSCarbs = NSCarbs( - eventType = EventType.fromString(if (amount < 12) TherapyEvent.Type.CARBS_CORRECTION.text else TherapyEvent.Type.MEAL_BOLUS.text), + eventType = if (amount < 12) EventType.CARBS_CORRECTION else EventType.MEAL_BOLUS, isValid = isValid, date = timestamp, utcOffset = utcOffset, diff --git a/plugins/sync/src/main/java/info/nightscout/plugins/sync/nsclientV3/extensions/ProfileSwitchExtension.kt b/plugins/sync/src/main/java/info/nightscout/plugins/sync/nsclientV3/extensions/ProfileSwitchExtension.kt index fad17e37c0..72edc7c22e 100644 --- a/plugins/sync/src/main/java/info/nightscout/plugins/sync/nsclientV3/extensions/ProfileSwitchExtension.kt +++ b/plugins/sync/src/main/java/info/nightscout/plugins/sync/nsclientV3/extensions/ProfileSwitchExtension.kt @@ -5,7 +5,6 @@ import info.nightscout.core.extensions.getCustomizedName import info.nightscout.core.extensions.pureProfileFromJson import info.nightscout.core.profile.ProfileSealed import info.nightscout.database.entities.ProfileSwitch -import info.nightscout.database.entities.TherapyEvent import info.nightscout.database.entities.embedments.InterfaceIDs import info.nightscout.interfaces.plugin.ActivePlugin import info.nightscout.sdk.localmodel.treatment.EventType @@ -46,7 +45,7 @@ fun ProfileSwitch.toNSProfileSwitch(dateUtil: DateUtil): NSProfileSwitch { notCustomized.percentage = 100 return NSProfileSwitch( - eventType = EventType.fromString(TherapyEvent.Type.PROFILE_SWITCH.text), + eventType = EventType.PROFILE_SWITCH, isValid = isValid, date = timestamp, utcOffset = utcOffset, 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 dc68a72cf9..141c00d347 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 @@ -2,7 +2,6 @@ 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 @@ -32,7 +31,7 @@ fun NSTemporaryBasal.Type?.toType(): TemporaryBasal.Type = fun TemporaryBasal.toNSTemporaryBasal(profile: Profile): NSTemporaryBasal = NSTemporaryBasal( - eventType = EventType.fromString(TherapyEvent.Type.TEMPORARY_BASAL.text), + eventType = EventType.TEMPORARY_BASAL, isValid = isValid, date = timestamp, utcOffset = utcOffset, diff --git a/plugins/sync/src/main/java/info/nightscout/plugins/sync/nsclientV3/extensions/TemporaryTargetExtension.kt b/plugins/sync/src/main/java/info/nightscout/plugins/sync/nsclientV3/extensions/TemporaryTargetExtension.kt index 066ea18e19..e0791865b2 100644 --- a/plugins/sync/src/main/java/info/nightscout/plugins/sync/nsclientV3/extensions/TemporaryTargetExtension.kt +++ b/plugins/sync/src/main/java/info/nightscout/plugins/sync/nsclientV3/extensions/TemporaryTargetExtension.kt @@ -2,6 +2,8 @@ package info.nightscout.plugins.sync.nsclientV3.extensions import info.nightscout.database.entities.TemporaryTarget import info.nightscout.database.entities.embedments.InterfaceIDs +import info.nightscout.sdk.localmodel.entry.NsUnits +import info.nightscout.sdk.localmodel.treatment.EventType import info.nightscout.sdk.localmodel.treatment.NSTemporaryTarget fun NSTemporaryTarget.toTemporaryTarget(): TemporaryTarget = @@ -25,4 +27,33 @@ fun NSTemporaryTarget.Reason?.toReason(): TemporaryTarget.Reason = NSTemporaryTarget.Reason.AUTOMATION -> TemporaryTarget.Reason.AUTOMATION NSTemporaryTarget.Reason.WEAR -> TemporaryTarget.Reason.WEAR null -> TemporaryTarget.Reason.CUSTOM + } + +fun TemporaryTarget.toNSTemporaryTarget(): NSTemporaryTarget = + NSTemporaryTarget( + eventType = EventType.TEMPORARY_TARGET, + isValid = isValid, + date = timestamp, + utcOffset = utcOffset, + reason = reason.toReason(), + targetTop = highTarget, + targetBottom = lowTarget, + units = NsUnits.MG_DL, + duration = duration, + identifier = interfaceIDs.nightscoutId, + pumpId = interfaceIDs.pumpId, + pumpType = interfaceIDs.pumpType?.name, + pumpSerial = interfaceIDs.pumpSerial, + endId = interfaceIDs.endId + ) + +fun TemporaryTarget.Reason?.toReason(): NSTemporaryTarget.Reason = + when (this) { + TemporaryTarget.Reason.CUSTOM -> NSTemporaryTarget.Reason.CUSTOM + TemporaryTarget.Reason.HYPOGLYCEMIA -> NSTemporaryTarget.Reason.HYPOGLYCEMIA + TemporaryTarget.Reason.ACTIVITY -> NSTemporaryTarget.Reason.ACTIVITY + TemporaryTarget.Reason.EATING_SOON -> NSTemporaryTarget.Reason.EATING_SOON + TemporaryTarget.Reason.AUTOMATION -> NSTemporaryTarget.Reason.AUTOMATION + TemporaryTarget.Reason.WEAR -> NSTemporaryTarget.Reason.WEAR + null -> NSTemporaryTarget.Reason.CUSTOM } \ No newline at end of file diff --git a/plugins/sync/src/test/java/info/nightscout/plugins/sync/nsclientV3/extensions/TemporaryTargetExtensionKtTest.kt b/plugins/sync/src/test/java/info/nightscout/plugins/sync/nsclientV3/extensions/TemporaryTargetExtensionKtTest.kt new file mode 100644 index 0000000000..a9b3f7be5d --- /dev/null +++ b/plugins/sync/src/test/java/info/nightscout/plugins/sync/nsclientV3/extensions/TemporaryTargetExtensionKtTest.kt @@ -0,0 +1,54 @@ +package info.nightscout.plugins.sync.nsclientV3.extensions + +import info.nightscout.androidaps.TestBaseWithProfile +import info.nightscout.database.entities.TemporaryTarget +import info.nightscout.database.entities.embedments.InterfaceIDs +import info.nightscout.sdk.localmodel.treatment.NSTemporaryTarget +import info.nightscout.sdk.mapper.convertToRemoteAndBack +import org.junit.jupiter.api.Assertions +import org.junit.jupiter.api.Test + +@Suppress("SpellCheckingInspection") +internal class TemporaryTargetExtensionKtTest : TestBaseWithProfile() { + + @Test + fun toTemporaryTarget() { + var temporaryTarget = TemporaryTarget( + timestamp = 10000, + isValid = true, + reason = TemporaryTarget.Reason.ACTIVITY, + highTarget = 100.0, + lowTarget = 99.0, + duration = 3600000, + interfaceIDs_backing = InterfaceIDs( + nightscoutId = "nightscoutId", + pumpId = 11000, + pumpType = InterfaceIDs.PumpType.DANA_I, + pumpSerial = "bbbb" + ) + ) + + var temporaryTarget2 = (temporaryTarget.toNSTemporaryTarget().convertToRemoteAndBack() as NSTemporaryTarget).toTemporaryTarget() + Assertions.assertTrue(temporaryTarget.contentEqualsTo(temporaryTarget2)) + Assertions.assertTrue(temporaryTarget.interfaceIdsEqualsTo(temporaryTarget2)) + + temporaryTarget = TemporaryTarget( + timestamp = 10000, + isValid = true, + reason = TemporaryTarget.Reason.CUSTOM, + highTarget = 150.0, + lowTarget = 150.0, + duration = 30000, + interfaceIDs_backing = InterfaceIDs( + nightscoutId = "nightscoutId", + pumpId = 11000, + pumpType = InterfaceIDs.PumpType.DANA_I, + pumpSerial = "bbbb" + ) + ) + + temporaryTarget2 = (temporaryTarget.toNSTemporaryTarget().convertToRemoteAndBack() as NSTemporaryTarget).toTemporaryTarget() + Assertions.assertTrue(temporaryTarget.contentEqualsTo(temporaryTarget2)) + Assertions.assertTrue(temporaryTarget.interfaceIdsEqualsTo(temporaryTarget2)) + } +} \ No newline at end of file