Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions data/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ kotlin {
implementation("io.ktor:ktor-client-content-negotiation:3.3.1")
implementation("io.ktor:ktor-serialization-kotlinx-json:3.3.1")
implementation("io.ktor:ktor-client-logging:3.3.1")
implementation("co.touchlab:kermit:2.0.4")

// Serialization
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.8.1")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package org.tidepool.sdk.di

import androidx.room.Room
import androidx.room.migration.Migration
import androidx.sqlite.SQLiteConnection
import androidx.sqlite.db.SupportSQLiteDatabase
import androidx.sqlite.driver.bundled.BundledSQLiteDriver
import org.koin.core.module.Module
import org.koin.dsl.module
Expand Down Expand Up @@ -35,6 +38,7 @@ actual val platformDataModule: Module
name = "loop-kit-database",
)
.setDriver(BundledSQLiteDriver())
.addMigrations(MIGRATION_1_2)
.build()
}
single<Logger> {
Expand All @@ -48,6 +52,22 @@ actual val platformDataModule: Module
}
}

private val MIGRATION_1_2 = object : Migration(1, 2) {
override fun migrate(db: SupportSQLiteDatabase) {
db.execSQL("ALTER TABLE bolus_data ADD COLUMN normal REAL")
}

override fun migrate(connection: SQLiteConnection) {
val statement = connection.prepare("ALTER TABLE bolus_data ADD COLUMN normal REAL")
try {
statement.step()
} finally {
statement.close()
}
}
}


actual fun provideAlertApi(ktorfit: Ktorfit) = ktorfit.createAlertApi()
actual fun provideAuthorizationApi(ktorfit: Ktorfit) = ktorfit.createAuthorizationApi()
actual fun provideBlobApi(ktorfit: Ktorfit) = ktorfit.createBlobApi()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import kotlinx.serialization.json.Json
import org.tidepool.sdk.dto.data.BasalAutomatedDataDto
import kotlinx.datetime.Instant
import java.util.TimeZone
import kotlin.math.roundToLong
import kotlin.time.Duration.Companion.milliseconds
import kotlin.time.Duration.Companion.minutes

@Entity(tableName = "basal_automated_data")
data class BasalAutomatedDataEntity(
Expand Down Expand Up @@ -41,9 +43,9 @@ data class BasalAutomatedDataEntity(
@ColumnInfo(name = "delivery_type")
var deliveryType: String,
@ColumnInfo(name = "duration")
var duration: Int,
var duration: Double,
@ColumnInfo(name = "expected_duration")
var expectedDuration: Int? = null,
var expectedDuration: Double? = null,
@ColumnInfo(name = "rate")
var rate: Double = -1.0,
@ColumnInfo(name = "schedule_name")
Expand Down Expand Up @@ -77,8 +79,8 @@ fun BasalAutomatedDataDto.toEntity() = BasalAutomatedDataEntity(
timeZone = timeZone?.id,
timeZoneOffset = timeZoneOffset,
deliveryType = Json.encodeToString(deliveryType),
duration = duration,
expectedDuration = expectedDuration,
duration = duration.toDouble().div(1.minutes.inWholeMilliseconds),
expectedDuration = expectedDuration?.toDouble()?.div(1.minutes.inWholeMilliseconds),
rate = rate,
scheduleName = scheduleName,
)
Expand All @@ -87,7 +89,9 @@ fun BasalAutomatedDataEntity.toDto() = BasalAutomatedDataDto(
id = id,
type = Json.decodeFromString(type),
time = time?.let { Instant.fromEpochSeconds(it) },
annotations = if (annotations.isNullOrBlank()) emptyList() else Json.decodeFromString(annotations!!),
annotations = annotations
?.takeUnless { it == "null" }
?.let { Json.decodeFromString<List<Map<String, String>>>(it) },
associations = if (associations.isNullOrBlank()) emptyList() else Json.decodeFromString(associations!!),
clockDriftOffset = clockDriftOffset?.milliseconds,
conversionOffset = conversionOffset?.milliseconds,
Expand All @@ -97,8 +101,8 @@ fun BasalAutomatedDataEntity.toDto() = BasalAutomatedDataDto(
timeZone = timeZone?.let { TimeZone.getTimeZone(it) },
timeZoneOffset = timeZoneOffset,
deliveryType = Json.decodeFromString(deliveryType),
duration = duration,
expectedDuration = expectedDuration,
duration = duration.times(1.minutes.inWholeMilliseconds).roundToLong(),
expectedDuration = expectedDuration?.times(1.minutes.inWholeMilliseconds)?.roundToLong(),
rate = rate,
scheduleName = scheduleName,
)
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package org.tidepool.sdk.database.entity.data

import androidx.room.Entity
import androidx.room.PrimaryKey
import co.touchlab.kermit.Logger
import org.tidepool.sdk.dto.data.BasalAutomatedDataDto
import org.tidepool.sdk.dto.data.BaseDataDto
import org.tidepool.sdk.dto.data.BolusDataDto
Expand Down Expand Up @@ -47,17 +48,20 @@ internal fun BaseDataEntity.toDomain(): BaseData = when (this) {
is PumpSettingsDataEntity -> toDomain()
}

internal fun BaseDataEntity.toDto(): BaseDataDto = when (this) {
is BasalAutomatedDataEntity -> toDto()
is BolusDataEntity -> toDto()
is ContinuousGlucoseDataEntity -> toDto()
is DosingDecisionDataEntity -> toDto()
is CgmSettingsDataEntity -> toDto()
is ControllerSettingsDataEntity -> toDto()
is FoodDataEntity -> toDto()
is InsulinDataEntity -> toDto()
is DeviceEventDataEntity -> toDto()
is PumpSettingsDataEntity -> toDto()
internal fun BaseDataEntity.toDto(): BaseDataDto {
Logger.d("BaseDataEntity") { "toDto(): ${javaClass.simpleName}, ${annotations?.let { "\"$it\"" }}"}
return when (this) {
is BasalAutomatedDataEntity -> toDto()
is BolusDataEntity -> toDto()
is ContinuousGlucoseDataEntity -> toDto()
is DosingDecisionDataEntity -> toDto()
is CgmSettingsDataEntity -> toDto()
is ControllerSettingsDataEntity -> toDto()
is FoodDataEntity -> toDto()
is InsulinDataEntity -> toDto()
is DeviceEventDataEntity -> toDto()
is PumpSettingsDataEntity -> toDto()
}
}

fun BaseDataDto.toEntity(): BaseDataEntity = when (this) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ data class BolusDataEntity(
var subType: String = "normal",
@ColumnInfo(name = "delivery_context")
var deliveryContext: String = "",
@ColumnInfo(name = "normal")
var normal: Double? = null,
) : BaseDataEntity(
id = id,
type = type,
Expand Down Expand Up @@ -72,15 +74,16 @@ fun BolusDataDto.toEntity() = BolusDataEntity(
timeZoneOffset = timeZoneOffset,
subType = Json.encodeToString(subType),
deliveryContext = Json.encodeToString(deliveryContext),
normal = normal,
)

fun BolusDataEntity.toDto() = BolusDataDto(
id = id,
type = Json.decodeFromString(type),
time = time?.let { Instant.fromEpochSeconds(it) },
annotations = annotations
?.let { Json.decodeFromString<List<Map<String, String>>>(it) }
.orEmpty(),
?.takeUnless { it == "null" }
?.let { Json.decodeFromString<List<Map<String, String>>>(it) },
associations = associations
?.let { Json.decodeFromString<List<AssociationDto>>(it) }
.orEmpty(),
Expand All @@ -95,4 +98,5 @@ fun BolusDataEntity.toDto() = BolusDataDto(
timeZoneOffset = timeZoneOffset,
subType = Json.decodeFromString(subType),
deliveryContext = Json.decodeFromString(deliveryContext),
normal = normal,
)
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ fun CgmSettingsDataDto.toEntity() = CgmSettingsDataEntity(
id = id,
type = Json.encodeToString(type),
time = time?.epochSeconds,
annotations = annotations.let { Json.encodeToString(it) },
annotations = annotations?.let { Json.encodeToString(it) },
associations = associations.let { Json.encodeToString(it) },
clockDriftOffset = clockDriftOffset?.inWholeMilliseconds,
conversionOffset = conversionOffset?.inWholeMilliseconds,
Expand All @@ -72,8 +72,8 @@ fun CgmSettingsDataEntity.toDto() = CgmSettingsDataDto(
type = Json.decodeFromString(type),
time = time?.let { Instant.fromEpochSeconds(it) },
annotations = annotations
?.let { Json.decodeFromString<List<Map<String, String>>>(it) }
.orEmpty(),
?.takeUnless { it == "null" }
?.let { Json.decodeFromString<List<Map<String, String>>>(it) },
associations = associations
?.let { Json.decodeFromString<List<AssociationDto>>(it) }
.orEmpty(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ fun ContinuousGlucoseDataDto.toEntity() = ContinuousGlucoseDataEntity(
id = id,
type = Json.encodeToString(type),
time = time?.epochSeconds,
annotations = annotations.let { Json.encodeToString(it) },
annotations = annotations?.let { Json.encodeToString(it) },
associations = associations.let { Json.encodeToString(it) },
clockDriftOffset = clockDriftOffset?.inWholeMilliseconds,
conversionOffset = conversionOffset?.inWholeMilliseconds,
Expand All @@ -85,8 +85,8 @@ fun ContinuousGlucoseDataEntity.toDto() = ContinuousGlucoseDataDto(
type = Json.decodeFromString(type),
time = time?.let { Instant.fromEpochSeconds(it) },
annotations = annotations
?.let { Json.decodeFromString<List<Map<String, String>>>(it) }
.orEmpty(),
?.takeUnless { it == "null" }
?.let { Json.decodeFromString<List<Map<String, String>>>(it) },
associations = associations
?.let { Json.decodeFromString<List<AssociationDto>>(it) }
.orEmpty(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ fun ControllerSettingsDataDto.toEntity() = ControllerSettingsDataEntity(
id = id,
type = Json.encodeToString(type),
time = time?.epochSeconds,
annotations = annotations.let { Json.encodeToString(it) },
annotations = annotations?.let { Json.encodeToString(it) },
associations = associations.let { Json.encodeToString(it) },
clockDriftOffset = clockDriftOffset?.inWholeMilliseconds,
conversionOffset = conversionOffset?.inWholeMilliseconds,
Expand All @@ -72,8 +72,8 @@ fun ControllerSettingsDataEntity.toDto() = ControllerSettingsDataDto(
type = Json.decodeFromString(type),
time = time?.let { Instant.fromEpochSeconds(it) },
annotations = annotations
?.let { Json.decodeFromString<List<Map<String, String>>>(it) }
.orEmpty(),
?.takeUnless { it == "null" }
?.let { Json.decodeFromString<List<Map<String, String>>>(it) },
associations = associations
?.let { Json.decodeFromString<List<AssociationDto>>(it) }
.orEmpty(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ fun DeviceEventDataDto.toEntity() = DeviceEventDataEntity(
id = id,
type = Json.encodeToString(type),
time = time?.epochSeconds,
annotations = annotations.let { Json.encodeToString(it) },
annotations = annotations?.let { Json.encodeToString(it) },
associations = associations.let { Json.encodeToString(it) },
clockDriftOffset = clockDriftOffset?.inWholeMilliseconds,
conversionOffset = conversionOffset?.inWholeMilliseconds,
Expand All @@ -88,8 +88,8 @@ fun DeviceEventDataEntity.toDto() = DeviceEventDataDto(
type = Json.decodeFromString(type),
time = time?.let { Instant.fromEpochSeconds(it) },
annotations = annotations
?.let { Json.decodeFromString<List<Map<String, String>>>(it) }
.orEmpty(),
?.takeUnless { it == "null" }
?.let { Json.decodeFromString<List<Map<String, String>>>(it) },
associations = associations
?.let { Json.decodeFromString<List<AssociationDto>>(it) }
.orEmpty(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ fun DosingDecisionDataEntity.toDto() = DosingDecisionDataDto(
type = Json.decodeFromString(type),
time = time?.let { Instant.fromEpochSeconds(it) },
annotations = annotations
?.let { Json.decodeFromString<List<Map<String, String>>>(it) }
.orEmpty(),
?.takeUnless { it == "null" }
?.let { Json.decodeFromString<List<Map<String, String>>>(it) },
associations = associations
?.let { Json.decodeFromString<List<AssociationDto>>(it) }
.orEmpty(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ fun FoodDataDto.toEntity() = FoodDataEntity(
id = id,
type = Json.encodeToString(type),
time = time?.epochSeconds,
annotations = annotations.let { Json.encodeToString(it) },
annotations = annotations?.let { Json.encodeToString(it) },
associations = associations.let { Json.encodeToString(it) },
clockDriftOffset = clockDriftOffset?.inWholeMilliseconds,
conversionOffset = conversionOffset?.inWholeMilliseconds,
Expand Down Expand Up @@ -96,8 +96,8 @@ fun FoodDataEntity.toDto() = FoodDataDto(
type = Json.decodeFromString(type),
time = time?.let { Instant.fromEpochSeconds(it) },
annotations = annotations
?.let { Json.decodeFromString<List<Map<String, String>>>(it) }
.orEmpty(),
?.takeUnless { it == "null" }
?.let { Json.decodeFromString<List<Map<String, String>>>(it) },
associations = associations
?.let { Json.decodeFromString<List<AssociationDto>>(it) }
.orEmpty(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ fun InsulinDataDto.toEntity() = InsulinDataEntity(
id = id,
type = Json.encodeToString(type),
time = time?.epochSeconds,
annotations = annotations.let { Json.encodeToString(it) },
annotations = annotations?.let { Json.encodeToString(it) },
associations = associations.let { Json.encodeToString(it) },
clockDriftOffset = clockDriftOffset?.inWholeMilliseconds,
conversionOffset = conversionOffset?.inWholeMilliseconds,
Expand All @@ -80,8 +80,8 @@ fun InsulinDataEntity.toDto() = InsulinDataDto(
type = Json.decodeFromString(type),
time = time?.let { Instant.fromEpochSeconds(it) },
annotations = annotations
?.let { Json.decodeFromString<List<Map<String, String>>>(it) }
.orEmpty(),
?.takeUnless { it == "null" }
?.let { Json.decodeFromString<List<Map<String, String>>>(it) },
associations = associations
?.let { Json.decodeFromString<List<AssociationDto>>(it) }
.orEmpty(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ fun PumpSettingsDataDto.toEntity() = PumpSettingsDataEntity(
id = id,
type = Json.encodeToString(type),
time = time?.epochSeconds,
annotations = annotations.let { Json.encodeToString(it) },
annotations = annotations?.let { Json.encodeToString(it) },
associations = associations.let { Json.encodeToString(it) },
clockDriftOffset = clockDriftOffset?.inWholeMilliseconds,
conversionOffset = conversionOffset?.inWholeMilliseconds,
Expand All @@ -73,8 +73,8 @@ fun PumpSettingsDataEntity.toDto() = PumpSettingsDataDto(
type = Json.decodeFromString(type),
time = time?.let { Instant.fromEpochSeconds(it) },
annotations = annotations
?.let { Json.decodeFromString<List<Map<String, String>>>(it) }
.orEmpty(),
?.takeUnless { it == "null" }
?.let { Json.decodeFromString<List<Map<String, String>>>(it) },
associations = associations
?.let { Json.decodeFromString<List<AssociationDto>>(it) }
.orEmpty(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ data class AssociationDto(

val id: String?,
@SerialName("url")
val url: String?,
val url: String? = null,
@SerialName("reason")
val reason: String?,
val reason: String? = null,
) {
@Serializable
enum class AssociationTypeDto {
Expand Down
Loading