diff --git a/core/ns-sdk/src/main/java/info/nightscout/sdk/localmodel/treatment/NSBolus.kt b/core/ns-sdk/src/main/java/info/nightscout/sdk/localmodel/treatment/NSBolus.kt index c9f143f1ea..db67ab4420 100644 --- a/core/ns-sdk/src/main/java/info/nightscout/sdk/localmodel/treatment/NSBolus.kt +++ b/core/ns-sdk/src/main/java/info/nightscout/sdk/localmodel/treatment/NSBolus.kt @@ -21,7 +21,8 @@ data class NSBolus( override val pumpSerial: String?, override var app: String? = null, val insulin: Double, - val type: BolusType + val type: BolusType, + val isBasalInsulin: Boolean ) : NSTreatment { enum class BolusType { 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 bfef3e6720..e3104265b0 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 @@ -17,10 +17,21 @@ import info.nightscout.sdk.remotemodel.RemoteTreatment import org.json.JSONObject 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? = + toRemoteTreatment()?.toTreatment() + internal fun RemoteTreatment.toTreatment(): NSTreatment? { val treatmentTimestamp = timestamp() when { - insulin != null && insulin > 0 -> + insulin != null && insulin > 0 -> return NSBolus( date = treatmentTimestamp, device = this.device, @@ -40,6 +51,7 @@ internal fun RemoteTreatment.toTreatment(): NSTreatment? { pumpSerial = this.pumpSerial, insulin = this.insulin, type = NSBolus.BolusType.fromString(this.type), + isBasalInsulin = isBasalInsulin ?: false ) carbs != null && carbs > 0 -> @@ -355,7 +367,8 @@ internal fun NSTreatment.toRemoteTreatment(): RemoteTreatment? = pumpType = pumpType, pumpSerial = pumpSerial, insulin = insulin, - type = type.name + type = type.name, + isBasalInsulin = isBasalInsulin ) is NSCarbs -> RemoteTreatment( diff --git a/core/ns-sdk/src/main/java/info/nightscout/sdk/remotemodel/RemoteTreatment.kt b/core/ns-sdk/src/main/java/info/nightscout/sdk/remotemodel/RemoteTreatment.kt index df11416054..a605f5ab4c 100644 --- a/core/ns-sdk/src/main/java/info/nightscout/sdk/remotemodel/RemoteTreatment.kt +++ b/core/ns-sdk/src/main/java/info/nightscout/sdk/remotemodel/RemoteTreatment.kt @@ -76,7 +76,8 @@ internal data class RemoteTreatment( @SerializedName("rate") val rate: Double? = null, // Double "Temp Basal" absolute rate (could be calculated with percent and profile information...) @SerializedName("extendedEmulated") val extendedEmulated: RemoteTreatment? = null, // Gson of emulated EB @SerializedName("timeshift") val timeshift: Long? = null, // integer "Profile Switch" - @SerializedName("percentage") val percentage: Int? = null // integer "Profile Switch" + @SerializedName("percentage") val percentage: Int? = null, // integer "Profile Switch" + @SerializedName("isBasalInsulin") val isBasalInsulin: Boolean? = null // boolean "Bolus" ) { fun timestamp(): Long { diff --git a/database/entities/src/main/java/info/nightscout/database/entities/Bolus.kt b/database/entities/src/main/java/info/nightscout/database/entities/Bolus.kt index 93a0cf0d5e..f296700e84 100644 --- a/database/entities/src/main/java/info/nightscout/database/entities/Bolus.kt +++ b/database/entities/src/main/java/info/nightscout/database/entities/Bolus.kt @@ -49,7 +49,7 @@ data class Bolus( var insulinConfiguration: InsulinConfiguration? = null ) : TraceableDBEntry, DBEntryWithTime { - private fun contentEqualsTo(other: Bolus): Boolean = + fun contentEqualsTo(other: Bolus): Boolean = isValid == other.isValid && timestamp == other.timestamp && utcOffset == other.utcOffset && @@ -58,6 +58,16 @@ data class Bolus( notes == other.notes && isBasalInsulin == other.isBasalInsulin + fun interfaceIdsEqualsTo(other: Bolus): Boolean = + interfaceIDs.nightscoutId == interfaceIDs.nightscoutId && + interfaceIDs.nightscoutSystemId == interfaceIDs.nightscoutSystemId && + interfaceIDs.pumpType == interfaceIDs.pumpType && + interfaceIDs.pumpSerial == interfaceIDs.pumpSerial && + interfaceIDs.temporaryId == interfaceIDs.temporaryId && + interfaceIDs.pumpId == interfaceIDs.pumpId && + interfaceIDs.startId == interfaceIDs.startId && + interfaceIDs.endId == interfaceIDs.endId + fun onlyNsIdAdded(previous: Bolus): Boolean = previous.id != id && contentEqualsTo(previous) && 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 9b901ac7dd..2b5a059d24 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 @@ -14,6 +14,7 @@ fun NSBolus.toBolus(): Bolus = amount = insulin, type = type.toBolusType(), notes = notes, + isBasalInsulin = isBasalInsulin, interfaceIDs_backing = InterfaceIDs(nightscoutId = identifier, pumpId = pumpId, pumpType = InterfaceIDs.PumpType.fromString(pumpType), pumpSerial = pumpSerial, endId = endId) ) @@ -34,6 +35,7 @@ fun Bolus.toNSBolus(): NSBolus = insulin = amount, type = type.toBolusType(), notes = notes, + isBasalInsulin = isBasalInsulin, identifier = interfaceIDs.nightscoutId, pumpId = interfaceIDs.pumpId, pumpType = interfaceIDs.pumpType?.name, diff --git a/plugins/sync/src/test/java/info/nightscout/plugins/sync/nsclientV3/extensions/BolusExtensionKtTest.kt b/plugins/sync/src/test/java/info/nightscout/plugins/sync/nsclientV3/extensions/BolusExtensionKtTest.kt new file mode 100644 index 0000000000..3c8573bfc9 --- /dev/null +++ b/plugins/sync/src/test/java/info/nightscout/plugins/sync/nsclientV3/extensions/BolusExtensionKtTest.kt @@ -0,0 +1,54 @@ +package info.nightscout.plugins.sync.nsclientV3.extensions + +import info.nightscout.database.entities.Bolus +import info.nightscout.database.entities.embedments.InterfaceIDs +import info.nightscout.sdk.localmodel.treatment.NSBolus +import info.nightscout.sdk.mapper.convertToRemoteAndBack +import org.junit.jupiter.api.Assertions + +import org.junit.jupiter.api.Test + +@Suppress("SpellCheckingInspection") +internal class BolusExtensionKtTest { + + @Test + fun toBolus() { + var bolus = Bolus( + timestamp = 10000, + isValid = true, + amount = 1.0, + type = Bolus.Type.SMB, + notes = "aaaa", + isBasalInsulin = false, + interfaceIDs_backing = InterfaceIDs( + nightscoutId = "nightscoutId", + pumpId = 11000, + pumpType = InterfaceIDs.PumpType.DANA_I, + pumpSerial = "bbbb" + ) + ) + + var bolus2 = (bolus.toNSBolus().convertToRemoteAndBack() as NSBolus).toBolus() + Assertions.assertTrue(bolus.contentEqualsTo(bolus2)) + Assertions.assertTrue(bolus.interfaceIdsEqualsTo(bolus2)) + + bolus = Bolus( + timestamp = 10000, + isValid = false, + amount = 1.0, + type = Bolus.Type.NORMAL, + notes = "aaaa", + isBasalInsulin = true, + interfaceIDs_backing = InterfaceIDs( + nightscoutId = "nightscoutId", + pumpId = 11000, + pumpType = InterfaceIDs.PumpType.DANA_I, + pumpSerial = "bbbb" + ) + ) + + bolus2 = (bolus.toNSBolus().convertToRemoteAndBack() as NSBolus).toBolus() + Assertions.assertTrue(bolus.contentEqualsTo(bolus2)) + Assertions.assertTrue(bolus.interfaceIdsEqualsTo(bolus2)) + } +} \ No newline at end of file