NSCv3: process NSTherapyEvent

This commit is contained in:
Milos Kozak 2022-12-26 16:48:50 +01:00
parent 88c162e1a1
commit c2e314bd42
6 changed files with 140 additions and 48 deletions

View file

@ -5,14 +5,14 @@ import info.nightscout.sdk.localmodel.entry.NsUnits
data class NSTherapyEvent( data class NSTherapyEvent(
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?,
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?,
@ -35,7 +35,7 @@ data class NSTherapyEvent(
companion object { companion object {
fun fromString(text: String?) = values().firstOrNull { it.text == text } ?: MANUAL fun fromString(text: String?) = values().firstOrNull { it.text == text }
} }
} }
} }

View file

@ -46,7 +46,7 @@ data class TherapyEvent(
var glucoseUnit: GlucoseUnit, var glucoseUnit: GlucoseUnit,
) : TraceableDBEntry, DBEntryWithTimeAndDuration { ) : TraceableDBEntry, DBEntryWithTimeAndDuration {
private fun contentEqualsTo(other: TherapyEvent): Boolean = fun contentEqualsTo(other: TherapyEvent): Boolean =
isValid == other.isValid && isValid == other.isValid &&
timestamp == other.timestamp && timestamp == other.timestamp &&
utcOffset == other.utcOffset && utcOffset == other.utcOffset &&
@ -79,7 +79,7 @@ data class TherapyEvent(
companion object { companion object {
fun fromString(text: String?) = values().firstOrNull { it.text == text } ?: MANUAL fun fromString(text: String?) = values().firstOrNull { it.text == text }
} }
} }

View file

@ -86,7 +86,6 @@ class TddCalculatorImpl @Inject constructor(
} }
override fun calculate(startTime: Long, endTime: Long, allowMissingData: Boolean): TotalDailyDose? { override fun calculate(startTime: Long, endTime: Long, allowMissingData: Boolean): TotalDailyDose? {
val isWholeDay = (endTime - startTime) >= T.days(1).msecs()
val startTimeAligned = startTime - startTime % (5 * 60 * 1000) val startTimeAligned = startTime - startTime % (5 * 60 * 1000)
val endTimeAligned = endTime - endTime % (5 * 60 * 1000) val endTimeAligned = endTime - endTime % (5 * 60 * 1000)
val tdd = TotalDailyDose(timestamp = startTimeAligned) val tdd = TotalDailyDose(timestamp = startTimeAligned)

View file

@ -40,6 +40,7 @@ import info.nightscout.plugins.sync.nsclientV3.extensions.toNSEffectiveProfileSw
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.extensions.toNSTemporaryBasal
import info.nightscout.plugins.sync.nsclientV3.extensions.toNSTemporaryTarget import info.nightscout.plugins.sync.nsclientV3.extensions.toNSTemporaryTarget
import info.nightscout.plugins.sync.nsclientV3.extensions.toNSTherapyEvent
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
@ -330,7 +331,8 @@ class NSClientV3Plugin @Inject constructor(
is DataSyncSelector.PairTemporaryTarget -> dataPair.value.toNSTemporaryTarget() is DataSyncSelector.PairTemporaryTarget -> dataPair.value.toNSTemporaryTarget()
// 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.toNSTherapyEvent()
is DataSyncSelector.PairTemporaryBasal -> { is DataSyncSelector.PairTemporaryBasal -> {
val profile = profileFunction.getProfile(dataPair.value.timestamp) val profile = profileFunction.getProfile(dataPair.value.timestamp)
if (profile == null) { if (profile == null) {
@ -391,7 +393,15 @@ 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 -> {
if (result.response == 201) { // created
dataPair.value.interfaceIDs.nightscoutId = result.identifier
storeDataForDb.nsIdTherapyEvents.add(dataPair.value)
storeDataForDb.scheduleNsIdUpdate()
}
dataSyncSelector.confirmLastTherapyEventIdIfGreater(dataPair.id)
}
is DataSyncSelector.PairTemporaryBasal -> { is DataSyncSelector.PairTemporaryBasal -> {
if (result.response == 201) { // created if (result.response == 201) { // created
dataPair.value.interfaceIDs.nightscoutId = result.identifier dataPair.value.interfaceIDs.nightscoutId = result.identifier

View file

@ -22,43 +22,10 @@ fun NSTherapyEvent.toTherapyEvent(): TherapyEvent =
) )
fun EventType.toType(): TherapyEvent.Type = fun EventType.toType(): TherapyEvent.Type =
when (this) { TherapyEvent.Type.fromString(this.text)
EventType.CANNULA_CHANGE -> TherapyEvent.Type.CANNULA_CHANGE
EventType.INSULIN_CHANGE -> TherapyEvent.Type.INSULIN_CHANGE
EventType.PUMP_BATTERY_CHANGE -> TherapyEvent.Type.PUMP_BATTERY_CHANGE
EventType.SENSOR_CHANGE -> TherapyEvent.Type.SENSOR_CHANGE
EventType.SENSOR_STARTED -> TherapyEvent.Type.SENSOR_STARTED
EventType.SENSOR_STOPPED -> TherapyEvent.Type.SENSOR_STOPPED
EventType.FINGER_STICK_BG_VALUE -> TherapyEvent.Type.FINGER_STICK_BG_VALUE
EventType.EXERCISE -> TherapyEvent.Type.EXERCISE
EventType.ANNOUNCEMENT -> TherapyEvent.Type.ANNOUNCEMENT
EventType.QUESTION -> TherapyEvent.Type.QUESTION
EventType.NOTE -> TherapyEvent.Type.NOTE
EventType.APS_OFFLINE -> TherapyEvent.Type.APS_OFFLINE
EventType.DAD_ALERT -> TherapyEvent.Type.DAD_ALERT
EventType.NS_MBG -> TherapyEvent.Type.NS_MBG
EventType.CARBS_CORRECTION -> TherapyEvent.Type.CARBS_CORRECTION
EventType.BOLUS_WIZARD -> TherapyEvent.Type.BOLUS_WIZARD
EventType.CORRECTION_BOLUS -> TherapyEvent.Type.CORRECTION_BOLUS
EventType.MEAL_BOLUS -> TherapyEvent.Type.MEAL_BOLUS
EventType.COMBO_BOLUS -> TherapyEvent.Type.COMBO_BOLUS
EventType.TEMPORARY_TARGET -> TherapyEvent.Type.TEMPORARY_TARGET
EventType.TEMPORARY_TARGET_CANCEL -> TherapyEvent.Type.TEMPORARY_TARGET_CANCEL
EventType.PROFILE_SWITCH -> TherapyEvent.Type.PROFILE_SWITCH
EventType.SNACK_BOLUS -> TherapyEvent.Type.SNACK_BOLUS
EventType.TEMPORARY_BASAL -> TherapyEvent.Type.TEMPORARY_BASAL
EventType.TEMPORARY_BASAL_START -> TherapyEvent.Type.TEMPORARY_BASAL_START
EventType.TEMPORARY_BASAL_END -> TherapyEvent.Type.TEMPORARY_BASAL_END
EventType.NONE -> TherapyEvent.Type.NONE
}
fun NSTherapyEvent.MeterType?.toMeterType(): TherapyEvent.MeterType = fun NSTherapyEvent.MeterType?.toMeterType(): TherapyEvent.MeterType? =
when (this) { TherapyEvent.MeterType.fromString(this?.text)
NSTherapyEvent.MeterType.FINGER -> TherapyEvent.MeterType.FINGER
NSTherapyEvent.MeterType.SENSOR -> TherapyEvent.MeterType.SENSOR
NSTherapyEvent.MeterType.MANUAL -> TherapyEvent.MeterType.MANUAL
null -> TherapyEvent.MeterType.MANUAL
}
fun NsUnits?.toUnits(): TherapyEvent.GlucoseUnit = fun NsUnits?.toUnits(): TherapyEvent.GlucoseUnit =
when (this) { when (this) {
@ -66,3 +33,35 @@ fun NsUnits?.toUnits(): TherapyEvent.GlucoseUnit =
NsUnits.MMOL_L -> TherapyEvent.GlucoseUnit.MMOL NsUnits.MMOL_L -> TherapyEvent.GlucoseUnit.MMOL
null -> TherapyEvent.GlucoseUnit.MGDL null -> TherapyEvent.GlucoseUnit.MGDL
} }
fun TherapyEvent.toNSTherapyEvent(): NSTherapyEvent =
NSTherapyEvent(
isValid = isValid,
date = timestamp,
utcOffset = utcOffset,
units = glucoseUnit.toUnits(),
eventType = type.toType(),
notes = note,
enteredBy = enteredBy,
glucose = glucose,
glucoseType = glucoseType.toNSMeterType(),
duration = duration,
identifier = interfaceIDs.nightscoutId,
pumpId = interfaceIDs.pumpId,
pumpType = interfaceIDs.pumpType?.name,
pumpSerial = interfaceIDs.pumpSerial,
endId = interfaceIDs.endId
)
fun TherapyEvent.Type.toType(): EventType =
EventType.fromString(this.text)
fun TherapyEvent.MeterType?.toNSMeterType(): NSTherapyEvent.MeterType? =
NSTherapyEvent.MeterType.fromString(this?.text)
fun TherapyEvent.GlucoseUnit?.toUnits(): NsUnits =
when (this) {
TherapyEvent.GlucoseUnit.MGDL -> NsUnits.MG_DL
TherapyEvent.GlucoseUnit.MMOL -> NsUnits.MMOL_L
null -> NsUnits.MG_DL
}

View file

@ -0,0 +1,84 @@
package info.nightscout.plugins.sync.nsclientV3.extensions
import info.nightscout.androidaps.TestBaseWithProfile
import info.nightscout.database.entities.TemporaryTarget
import info.nightscout.database.entities.TherapyEvent
import info.nightscout.database.entities.embedments.InterfaceIDs
import info.nightscout.sdk.localmodel.treatment.NSTherapyEvent
import info.nightscout.sdk.mapper.convertToRemoteAndBack
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
@Suppress("SpellCheckingInspection")
internal class TherapyEventExtensionKtTest : TestBaseWithProfile() {
@Test
fun toTherapyEvent() {
var therapyEvent = TherapyEvent(
timestamp = 10000,
isValid = true,
type = TherapyEvent.Type.ANNOUNCEMENT,
note = "ccccc",
enteredBy = "dddd",
glucose = 101.0,
glucoseType = TherapyEvent.MeterType.FINGER,
glucoseUnit = TherapyEvent.GlucoseUnit.MGDL,
duration = 3600000,
interfaceIDs_backing = InterfaceIDs(
nightscoutId = "nightscoutId",
pumpId = 11000,
pumpType = InterfaceIDs.PumpType.DANA_I,
pumpSerial = "bbbb"
)
)
var therapyEvent2 = (therapyEvent.toNSTherapyEvent().convertToRemoteAndBack() as NSTherapyEvent).toTherapyEvent()
Assertions.assertTrue(therapyEvent.contentEqualsTo(therapyEvent2))
Assertions.assertTrue(therapyEvent.interfaceIdsEqualsTo(therapyEvent2))
therapyEvent = TherapyEvent(
timestamp = 10000,
isValid = true,
type = TherapyEvent.Type.QUESTION,
note = null,
enteredBy = null,
glucose = null,
glucoseType = null,
glucoseUnit = TherapyEvent.GlucoseUnit.MMOL,
duration = 30000,
interfaceIDs_backing = InterfaceIDs(
nightscoutId = "nightscoutId",
pumpId = 11000,
pumpType = InterfaceIDs.PumpType.DANA_I,
pumpSerial = "bbbb"
)
)
therapyEvent2 = (therapyEvent.toNSTherapyEvent().convertToRemoteAndBack() as NSTherapyEvent).toTherapyEvent()
Assertions.assertTrue(therapyEvent.contentEqualsTo(therapyEvent2))
Assertions.assertTrue(therapyEvent.interfaceIdsEqualsTo(therapyEvent2))
therapyEvent = TherapyEvent(
timestamp = 10000,
isValid = true,
type = TherapyEvent.Type.NOTE,
note = "qqqq",
enteredBy = null,
glucose = 10.0,
glucoseType = null,
glucoseUnit = TherapyEvent.GlucoseUnit.MGDL,
duration = 0,
interfaceIDs_backing = InterfaceIDs(
nightscoutId = "nightscoutId",
pumpId = 11000,
pumpType = InterfaceIDs.PumpType.DANA_I,
pumpSerial = "bbbb"
)
)
therapyEvent2 = (therapyEvent.toNSTherapyEvent().convertToRemoteAndBack() as NSTherapyEvent).toTherapyEvent()
Assertions.assertTrue(therapyEvent.contentEqualsTo(therapyEvent2))
Assertions.assertTrue(therapyEvent.interfaceIdsEqualsTo(therapyEvent2))
}
}