NSCv3: process OfflineEvent

This commit is contained in:
Milos Kozak 2022-12-29 18:46:25 +01:00
parent 04689da0e6
commit 3df237f8e4
5 changed files with 108 additions and 9 deletions

View file

@ -4,17 +4,17 @@ import info.nightscout.sdk.localmodel.entry.NsUnits
data class NSOfflineEvent( data class NSOfflineEvent(
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

@ -39,6 +39,7 @@ import info.nightscout.plugins.sync.nsclientV3.extensions.toNSBolusWizard
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.toNSFood import info.nightscout.plugins.sync.nsclientV3.extensions.toNSFood
import info.nightscout.plugins.sync.nsclientV3.extensions.toNSOfflineEvent
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
@ -389,7 +390,7 @@ class NSClientV3Plugin @Inject constructor(
// 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)
// is DataSyncSelector.PairOfflineEvent -> dataPair.value.toJson(false, dateUtil) is DataSyncSelector.PairOfflineEvent -> dataPair.value.toNSOfflineEvent()
else -> null else -> null
}?.let { data -> }?.let { data ->
runBlocking { runBlocking {
@ -480,7 +481,15 @@ class NSClientV3Plugin @Inject constructor(
} }
dataSyncSelector.confirmLastEffectiveProfileSwitchIdIfGreater(dataPair.id) dataSyncSelector.confirmLastEffectiveProfileSwitchIdIfGreater(dataPair.id)
} }
// is DataSyncSelector.PairOfflineEvent -> dataPair.value.toJson(false, dateUtil)
is DataSyncSelector.PairOfflineEvent -> {
if (result.response == 201) { // created
dataPair.value.interfaceIDs.nightscoutId = result.identifier
storeDataForDb.nsIdOfflineEvents.add(dataPair.value)
storeDataForDb.scheduleNsIdUpdate()
}
dataSyncSelector.confirmLastOfflineEventIdIfGreater(dataPair.id)
}
} }
} }
} catch (e: Exception) { } catch (e: Exception) {

View file

@ -2,6 +2,7 @@ package info.nightscout.plugins.sync.nsclientV3.extensions
import info.nightscout.database.entities.OfflineEvent import info.nightscout.database.entities.OfflineEvent
import info.nightscout.database.entities.embedments.InterfaceIDs import info.nightscout.database.entities.embedments.InterfaceIDs
import info.nightscout.sdk.localmodel.treatment.EventType
import info.nightscout.sdk.localmodel.treatment.NSOfflineEvent import info.nightscout.sdk.localmodel.treatment.NSOfflineEvent
fun NSOfflineEvent.toOfflineEvent(): OfflineEvent = fun NSOfflineEvent.toOfflineEvent(): OfflineEvent =
@ -16,3 +17,21 @@ fun NSOfflineEvent.toOfflineEvent(): OfflineEvent =
fun NSOfflineEvent.Reason?.toReason(): OfflineEvent.Reason = fun NSOfflineEvent.Reason?.toReason(): OfflineEvent.Reason =
OfflineEvent.Reason.fromString(this?.name) OfflineEvent.Reason.fromString(this?.name)
fun OfflineEvent.toNSOfflineEvent(): NSOfflineEvent =
NSOfflineEvent(
eventType = EventType.APS_OFFLINE,
isValid = isValid,
date = timestamp,
utcOffset = utcOffset,
duration = duration,
reason = reason.toReason(),
identifier = interfaceIDs.nightscoutId,
pumpId = interfaceIDs.pumpId,
pumpType = interfaceIDs.pumpType?.name,
pumpSerial = interfaceIDs.pumpSerial,
endId = interfaceIDs.endId
)
fun OfflineEvent.Reason?.toReason(): NSOfflineEvent.Reason =
NSOfflineEvent.Reason.fromString(this?.name)

View file

@ -0,0 +1,66 @@
package info.nightscout.plugins.sync.nsclientV3.extensions
import info.nightscout.database.entities.OfflineEvent
import info.nightscout.database.entities.embedments.InterfaceIDs
import info.nightscout.sdk.localmodel.treatment.NSOfflineEvent
import info.nightscout.sdk.mapper.convertToRemoteAndBack
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
@Suppress("SpellCheckingInspection")
internal class OfflineEventKtTest {
@Test
fun toOfflineEvent() {
var offlineEvent = OfflineEvent(
timestamp = 10000,
isValid = true,
reason = OfflineEvent.Reason.DISCONNECT_PUMP,
duration = 30000,
interfaceIDs_backing = InterfaceIDs(
nightscoutId = "nightscoutId",
pumpId = 11000,
pumpType = InterfaceIDs.PumpType.DANA_I,
pumpSerial = "bbbb"
)
)
var offlineEvent2 = (offlineEvent.toNSOfflineEvent().convertToRemoteAndBack() as NSOfflineEvent).toOfflineEvent()
Assertions.assertTrue(offlineEvent.contentEqualsTo(offlineEvent2))
Assertions.assertTrue(offlineEvent.interfaceIdsEqualsTo(offlineEvent2))
offlineEvent = OfflineEvent(
timestamp = 10000,
isValid = true,
reason = OfflineEvent.Reason.SUSPEND,
duration = 3600000,
interfaceIDs_backing = InterfaceIDs(
nightscoutId = "nightscoutId",
pumpId = 11000,
pumpType = InterfaceIDs.PumpType.DANA_I,
pumpSerial = "bbbb"
)
)
offlineEvent2 = (offlineEvent.toNSOfflineEvent().convertToRemoteAndBack() as NSOfflineEvent).toOfflineEvent()
Assertions.assertTrue(offlineEvent.contentEqualsTo(offlineEvent2))
Assertions.assertTrue(offlineEvent.interfaceIdsEqualsTo(offlineEvent2))
offlineEvent = OfflineEvent(
timestamp = 10000,
isValid = true,
reason = OfflineEvent.Reason.DISABLE_LOOP,
duration = 0,
interfaceIDs_backing = InterfaceIDs(
nightscoutId = "nightscoutId",
pumpId = 11000,
pumpType = InterfaceIDs.PumpType.DANA_I,
pumpSerial = "bbbb"
)
)
offlineEvent2 = (offlineEvent.toNSOfflineEvent().convertToRemoteAndBack() as NSOfflineEvent).toOfflineEvent()
Assertions.assertTrue(offlineEvent.contentEqualsTo(offlineEvent2))
Assertions.assertTrue(offlineEvent.interfaceIdsEqualsTo(offlineEvent2))
}
}

View file

@ -1,5 +1,6 @@
package info.nightscout.androidaps.interaction.utils package info.nightscout.androidaps.interaction.utils
import info.nightscout.androidaps.R
import info.nightscout.androidaps.TestBase import info.nightscout.androidaps.TestBase
import info.nightscout.androidaps.data.RawDisplayData import info.nightscout.androidaps.data.RawDisplayData
import info.nightscout.androidaps.testing.mockers.RawDataMocker import info.nightscout.androidaps.testing.mockers.RawDataMocker
@ -24,7 +25,11 @@ class DisplayFormatTest : TestBase() {
displayFormat = DisplayFormat() displayFormat = DisplayFormat()
displayFormat.wearUtil = wearUtil displayFormat.wearUtil = wearUtil
displayFormat.sp = sp displayFormat.sp = sp
displayFormat.context = context
Mockito.`when`(sp.getBoolean("complication_unicode", true)).thenReturn(true) Mockito.`when`(sp.getBoolean("complication_unicode", true)).thenReturn(true)
Mockito.`when`(context.getString(R.string.hour_short)).thenReturn("h")
Mockito.`when`(context.getString(R.string.day_short)).thenReturn("d")
Mockito.`when`(context.getString(R.string.week_short)).thenReturn("w")
} }
@Test fun shortTimeSinceTest() { @Test fun shortTimeSinceTest() {