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( data class NSProfileSwitch(
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?,
override val pumpSerial: String?, override val pumpSerial: String?,
override var app: String? = null, override var app: String? = null,
val profileJson: JSONObject?, val profileJson: JSONObject?,
val profileName: String, val profile: String,
val originalProfileName: String?, val originalProfileName: String?,
val timeShift: Long?, val timeShift: Long?,
val percentage: Int?, val percentage: Int?,

View file

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

View file

@ -54,7 +54,6 @@ import info.nightscout.interfaces.nsclient.StoreDataForDb
import info.nightscout.interfaces.pump.VirtualPump import info.nightscout.interfaces.pump.VirtualPump
import info.nightscout.interfaces.source.NSClientSource import info.nightscout.interfaces.source.NSClientSource
import info.nightscout.interfaces.ui.UiInteraction import info.nightscout.interfaces.ui.UiInteraction
import info.nightscout.plugins.sync.nsclientV3.NSClientV3Plugin
import info.nightscout.rx.bus.RxBus import info.nightscout.rx.bus.RxBus
import info.nightscout.rx.events.EventNSClientNewLog import info.nightscout.rx.events.EventNSClientNewLog
import info.nightscout.rx.logging.AAPSLogger import info.nightscout.rx.logging.AAPSLogger
@ -766,7 +765,7 @@ class StoreDataForDbImpl @Inject constructor(
// cancel waiting task to prevent sending multiple posts // cancel waiting task to prevent sending multiple posts
scheduledEventPost?.cancel(false) scheduledEventPost?.cancel(false)
val task: Runnable = PostRunnable() val task: Runnable = PostRunnable()
scheduledEventPost = eventWorker.schedule(task, 30, TimeUnit.SECONDS) scheduledEventPost = eventWorker.schedule(task, 10, TimeUnit.SECONDS)
} }
private fun updateNsIds() { 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.toNSBolus
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.toNSProfileSwitch
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
@ -328,7 +329,7 @@ class NSClientV3Plugin @Inject constructor(
// is DataSyncSelector.PairTherapyEvent -> dataPair.value.toJson(false, dateUtil) // is DataSyncSelector.PairTherapyEvent -> dataPair.value.toJson(false, dateUtil)
// is DataSyncSelector.PairTemporaryBasal -> dataPair.value.toJson(false, profileFunction.getProfile(dataPair.value.timestamp), 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.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.PairEffectiveProfileSwitch -> dataPair.value.toNSEffectiveProfileSwitch(dateUtil)
// is DataSyncSelector.PairOfflineEvent -> dataPair.value.toJson(false, dateUtil) // is DataSyncSelector.PairOfflineEvent -> dataPair.value.toJson(false, dateUtil)
else -> null else -> null
@ -374,7 +375,15 @@ class NSClientV3Plugin @Inject constructor(
// is DataSyncSelector.PairTherapyEvent -> dataPair.value.toJson(false, dateUtil) // is DataSyncSelector.PairTherapyEvent -> dataPair.value.toJson(false, dateUtil)
// is DataSyncSelector.PairTemporaryBasal -> dataPair.value.toJson(false, profileFunction.getProfile(dataPair.value.timestamp), 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.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 -> { is DataSyncSelector.PairEffectiveProfileSwitch -> {
if (result.response == 201) { // created if (result.response == 201) { // created
dataPair.value.interfaceIDs.nightscoutId = result.identifier dataPair.value.interfaceIDs.nightscoutId = result.identifier

View file

@ -1,11 +1,14 @@
package info.nightscout.plugins.sync.nsclientV3.extensions package info.nightscout.plugins.sync.nsclientV3.extensions
import info.nightscout.core.extensions.fromConstant import info.nightscout.core.extensions.fromConstant
import info.nightscout.core.extensions.getCustomizedName
import info.nightscout.core.extensions.pureProfileFromJson import info.nightscout.core.extensions.pureProfileFromJson
import info.nightscout.core.profile.ProfileSealed import info.nightscout.core.profile.ProfileSealed
import info.nightscout.database.entities.ProfileSwitch import info.nightscout.database.entities.ProfileSwitch
import info.nightscout.database.entities.TherapyEvent
import info.nightscout.database.entities.embedments.InterfaceIDs import info.nightscout.database.entities.embedments.InterfaceIDs
import info.nightscout.interfaces.plugin.ActivePlugin import info.nightscout.interfaces.plugin.ActivePlugin
import info.nightscout.sdk.localmodel.treatment.EventType
import info.nightscout.sdk.localmodel.treatment.NSProfileSwitch import info.nightscout.sdk.localmodel.treatment.NSProfileSwitch
import info.nightscout.shared.utils.DateUtil import info.nightscout.shared.utils.DateUtil
import info.nightscout.shared.utils.T import info.nightscout.shared.utils.T
@ -13,7 +16,7 @@ import info.nightscout.shared.utils.T
fun NSProfileSwitch.toProfileSwitch(activePlugin: ActivePlugin, dateUtil: DateUtil): ProfileSwitch? { fun NSProfileSwitch.toProfileSwitch(activePlugin: ActivePlugin, dateUtil: DateUtil): ProfileSwitch? {
val pureProfile = val pureProfile =
profileJson?.let { pureProfileFromJson(it, dateUtil) ?: return null } 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) val profileSealed = ProfileSealed.Pure(pureProfile)
@ -26,7 +29,7 @@ fun NSProfileSwitch.toProfileSwitch(activePlugin: ActivePlugin, dateUtil: DateUt
icBlocks = profileSealed.icBlocks, icBlocks = profileSealed.icBlocks,
targetBlocks = profileSealed.targetBlocks, targetBlocks = profileSealed.targetBlocks,
glucoseUnit = ProfileSwitch.GlucoseUnit.fromConstant(profileSealed.units), glucoseUnit = ProfileSwitch.GlucoseUnit.fromConstant(profileSealed.units),
profileName = originalProfileName ?: profileName, profileName = originalProfileName ?: profile,
timeshift = timeShift ?: 0, timeshift = timeShift ?: 0,
percentage = percentage ?: 100, percentage = percentage ?: 100,
duration = originalDuration ?: T.mins(duration ?: 0).msecs(), 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) 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
)
}