NSCv3: process ProfileSwitch

This commit is contained in:
Milos Kozak 2022-12-16 11:06:57 +01:00
parent 10ca0773d9
commit 73f716ba87
5 changed files with 57 additions and 18 deletions

View file

@ -5,24 +5,24 @@ import org.json.JSONObject
data class NSProfileSwitch(
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 units: NsUnits? = null,
override val srvModified: Long? = null,
override val srvCreated: Long? = null,
override val utcOffset: Long,
override val subject: String?,
override var isReadOnly: Boolean,
override val subject: String? = null,
override var isReadOnly: Boolean = false,
override val isValid: Boolean,
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?,
override val pumpSerial: String?,
override var app: String? = null,
val profileJson: JSONObject?,
val profileName: String,
val profile: String,
val originalProfileName: String?,
val timeShift: Long?,
val percentage: Int?,

View file

@ -210,7 +210,7 @@ internal fun RemoteTreatment.toTreatment(): NSTreatment? {
pumpType = this.pumpType,
pumpSerial = this.pumpSerial,
profileJson = this.profileJson?.let { JSONObject(this.profileJson) },
profileName = this.profile,
profile = this.profile,
originalProfileName = this.originalProfileName,
originalDuration = this.originalDuration,
duration = this.duration,
@ -496,7 +496,7 @@ internal fun NSTreatment.toRemoteTreatment(): RemoteTreatment? =
pumpType = pumpType,
pumpSerial = pumpSerial,
profileJson = profileJson.toString(), // must be de-customized
profile = profileName,
profile = profile,
originalProfileName = originalProfileName,
originalDuration = originalDuration,
duration = duration,

View file

@ -54,7 +54,6 @@ import info.nightscout.interfaces.nsclient.StoreDataForDb
import info.nightscout.interfaces.pump.VirtualPump
import info.nightscout.interfaces.source.NSClientSource
import info.nightscout.interfaces.ui.UiInteraction
import info.nightscout.plugins.sync.nsclientV3.NSClientV3Plugin
import info.nightscout.rx.bus.RxBus
import info.nightscout.rx.events.EventNSClientNewLog
import info.nightscout.rx.logging.AAPSLogger
@ -766,7 +765,7 @@ class StoreDataForDbImpl @Inject constructor(
// cancel waiting task to prevent sending multiple posts
scheduledEventPost?.cancel(false)
val task: Runnable = PostRunnable()
scheduledEventPost = eventWorker.schedule(task, 30, TimeUnit.SECONDS)
scheduledEventPost = eventWorker.schedule(task, 10, TimeUnit.SECONDS)
}
private fun updateNsIds() {

View file

@ -36,6 +36,7 @@ import info.nightscout.plugins.sync.nsclient.NsClientReceiverDelegate
import info.nightscout.plugins.sync.nsclientV3.extensions.toNSBolus
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.workers.LoadBgWorker
import info.nightscout.plugins.sync.nsclientV3.workers.LoadLastModificationWorker
import info.nightscout.plugins.sync.nsclientV3.workers.LoadStatusWorker
@ -319,7 +320,7 @@ class NSClientV3Plugin @Inject constructor(
Operation.UPDATE -> nsAndroidClient?.let { return@let it::updateTreatment }
}
when (dataPair) {
is DataSyncSelector.PairBolus -> dataPair.value.toNSBolus()
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)
@ -328,10 +329,10 @@ class NSClientV3Plugin @Inject constructor(
// is DataSyncSelector.PairTherapyEvent -> dataPair.value.toJson(false, dateUtil)
// is DataSyncSelector.PairTemporaryBasal -> 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.toJson(false, dateUtil)
is DataSyncSelector.PairProfileSwitch -> dataPair.value.toNSProfileSwitch(dateUtil)
is DataSyncSelector.PairEffectiveProfileSwitch -> dataPair.value.toNSEffectiveProfileSwitch(dateUtil)
// is DataSyncSelector.PairOfflineEvent -> dataPair.value.toJson(false, dateUtil)
else -> null
else -> null
}?.let { data ->
runBlocking {
if (collection == "treatments") {
@ -374,7 +375,15 @@ class NSClientV3Plugin @Inject constructor(
// is DataSyncSelector.PairTherapyEvent -> dataPair.value.toJson(false, dateUtil)
// is DataSyncSelector.PairTemporaryBasal -> 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.toJson(false, dateUtil)
is DataSyncSelector.PairProfileSwitch -> {
if (result.response == 201) { // created
dataPair.value.interfaceIDs.nightscoutId = result.identifier
storeDataForDb.nsIdProfileSwitches.add(dataPair.value)
storeDataForDb.scheduleNsIdUpdate()
}
dataSyncSelector.confirmLastProfileSwitchIdIfGreater(dataPair.id)
}
is DataSyncSelector.PairEffectiveProfileSwitch -> {
if (result.response == 201) { // created
dataPair.value.interfaceIDs.nightscoutId = result.identifier

View file

@ -1,11 +1,14 @@
package info.nightscout.plugins.sync.nsclientV3.extensions
import info.nightscout.core.extensions.fromConstant
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
import info.nightscout.sdk.localmodel.treatment.NSProfileSwitch
import info.nightscout.shared.utils.DateUtil
import info.nightscout.shared.utils.T
@ -13,7 +16,7 @@ import info.nightscout.shared.utils.T
fun NSProfileSwitch.toProfileSwitch(activePlugin: ActivePlugin, dateUtil: DateUtil): ProfileSwitch? {
val pureProfile =
profileJson?.let { pureProfileFromJson(it, dateUtil) ?: return null }
?: activePlugin.activeProfileSource.profile?.getSpecificProfile(profileName) ?: return null
?: activePlugin.activeProfileSource.profile?.getSpecificProfile(profile) ?: return null
val profileSealed = ProfileSealed.Pure(pureProfile)
@ -26,7 +29,7 @@ fun NSProfileSwitch.toProfileSwitch(activePlugin: ActivePlugin, dateUtil: DateUt
icBlocks = profileSealed.icBlocks,
targetBlocks = profileSealed.targetBlocks,
glucoseUnit = ProfileSwitch.GlucoseUnit.fromConstant(profileSealed.units),
profileName = originalProfileName ?: profileName,
profileName = originalProfileName ?: profile,
timeshift = timeShift ?: 0,
percentage = percentage ?: 100,
duration = originalDuration ?: T.mins(duration ?: 0).msecs(),
@ -34,3 +37,31 @@ fun NSProfileSwitch.toProfileSwitch(activePlugin: ActivePlugin, dateUtil: DateUt
interfaceIDs_backing = InterfaceIDs(nightscoutId = identifier, pumpId = pumpId, pumpType = InterfaceIDs.PumpType.fromString(pumpType), pumpSerial = pumpSerial, endId = endId)
)
}
fun ProfileSwitch.toNSProfileSwitch(dateUtil: DateUtil): NSProfileSwitch {
val unmodifiedCustomizedName = getCustomizedName()
// ProfileSealed.PS doesn't provide unmodified json -> reset it
val unmodifiedTimeshift = timeshift
val unmodifiedPercentage = percentage
timeshift = 0
percentage = 100
return NSProfileSwitch(
eventType = EventType.fromString(TherapyEvent.Type.PROFILE_SWITCH.text),
isValid = isValid,
date = timestamp,
utcOffset = utcOffset,
timeShift = unmodifiedTimeshift,
percentage = unmodifiedPercentage,
duration = T.mins(duration).msecs(),
profile = unmodifiedCustomizedName,
originalProfileName = profileName,
originalDuration = duration,
profileJson = ProfileSealed.PS(this).toPureNsJson(dateUtil),
identifier = interfaceIDs.nightscoutId,
pumpId = interfaceIDs.pumpId,
pumpType = interfaceIDs.pumpType?.name,
pumpSerial = interfaceIDs.pumpSerial,
endId = interfaceIDs.endId
)
}