From e28271fac611f0ca4b2b2eca15d622b403070359 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Wed, 14 Dec 2022 22:26:40 +0100 Subject: [PATCH] NSCv3: upload EffectiveProfileSwitch --- .../treatment/NSEffectiveProfileSwitch.kt | 14 +++---- .../configBuilder/RunningConfigurationImpl.kt | 2 +- .../sync/nsclientV3/NSClientV3Plugin.kt | 41 ++++++++++++++++++- .../EffectiveProfileSwitchExtension.kt | 24 ++++++++++- 4 files changed, 70 insertions(+), 11 deletions(-) diff --git a/core/ns-sdk/src/main/java/info/nightscout/sdk/localmodel/treatment/NSEffectiveProfileSwitch.kt b/core/ns-sdk/src/main/java/info/nightscout/sdk/localmodel/treatment/NSEffectiveProfileSwitch.kt index 65662e5058..61b9aa716d 100644 --- a/core/ns-sdk/src/main/java/info/nightscout/sdk/localmodel/treatment/NSEffectiveProfileSwitch.kt +++ b/core/ns-sdk/src/main/java/info/nightscout/sdk/localmodel/treatment/NSEffectiveProfileSwitch.kt @@ -5,14 +5,14 @@ import org.json.JSONObject data class NSEffectiveProfileSwitch( override val date: Long, - override val device: String?, - override val identifier: String?, - override val units: NsUnits?, - override val srvModified: Long?, - override val srvCreated: Long?, + override val device: String? = null, + override val identifier: String? = null, + 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?, diff --git a/plugins/configuration/src/main/java/info/nightscout/configuration/configBuilder/RunningConfigurationImpl.kt b/plugins/configuration/src/main/java/info/nightscout/configuration/configBuilder/RunningConfigurationImpl.kt index e1f68e6410..6ee49e4e96 100644 --- a/plugins/configuration/src/main/java/info/nightscout/configuration/configBuilder/RunningConfigurationImpl.kt +++ b/plugins/configuration/src/main/java/info/nightscout/configuration/configBuilder/RunningConfigurationImpl.kt @@ -72,7 +72,7 @@ class RunningConfigurationImpl @Inject constructor( assert(config.NSCLIENT) configuration.version?.let { - rxBus.send(EventNSClientNewLog("VERSION", "Received AndroidAPS version $it")) + rxBus.send(EventNSClientNewLog("VERSION", "Received AAPS version $it")) if (config.VERSION_NAME.startsWith(it).not()) uiInteraction.addNotification(Notification.NSCLIENT_VERSION_DOES_NOT_MATCH, rh.gs(R.string.nsclient_version_does_not_match), Notification.NORMAL) } diff --git a/plugins/sync/src/main/java/info/nightscout/plugins/sync/nsclientV3/NSClientV3Plugin.kt b/plugins/sync/src/main/java/info/nightscout/plugins/sync/nsclientV3/NSClientV3Plugin.kt index df68228f2e..d21159e7c1 100644 --- a/plugins/sync/src/main/java/info/nightscout/plugins/sync/nsclientV3/NSClientV3Plugin.kt +++ b/plugins/sync/src/main/java/info/nightscout/plugins/sync/nsclientV3/NSClientV3Plugin.kt @@ -34,6 +34,7 @@ import info.nightscout.plugins.sync.nsShared.events.EventNSClientResend import info.nightscout.plugins.sync.nsShared.events.EventNSClientUpdateGUI import info.nightscout.plugins.sync.nsclient.NsClientReceiverDelegate import info.nightscout.plugins.sync.nsclientV3.extensions.toNSBolus +import info.nightscout.plugins.sync.nsclientV3.extensions.toNSEffectiveProfileSwitch import info.nightscout.plugins.sync.nsclientV3.workers.LoadBgWorker import info.nightscout.plugins.sync.nsclientV3.workers.LoadLastModificationWorker import info.nightscout.plugins.sync.nsclientV3.workers.LoadStatusWorker @@ -43,6 +44,8 @@ import info.nightscout.rx.events.EventAppExit import info.nightscout.rx.events.EventChargingState import info.nightscout.rx.events.EventNSClientNewLog import info.nightscout.rx.events.EventNetworkChange +import info.nightscout.rx.events.EventNewBG +import info.nightscout.rx.events.EventNewHistoryData import info.nightscout.rx.events.EventPreferenceChange import info.nightscout.rx.events.EventSWSyncStatus import info.nightscout.rx.logging.AAPSLogger @@ -59,6 +62,9 @@ import io.reactivex.rxjava3.kotlin.plusAssign import kotlinx.coroutines.runBlocking import kotlinx.serialization.decodeFromString import kotlinx.serialization.json.Json +import java.util.concurrent.Executors +import java.util.concurrent.ScheduledFuture +import java.util.concurrent.TimeUnit import javax.inject.Inject import javax.inject.Singleton @@ -170,6 +176,14 @@ class NSClientV3Plugin @Inject constructor( .toObservable(EventNSClientResend::class.java) .observeOn(aapsSchedulers.io) .subscribe({ event -> resend(event.reason) }, fabricPrivacy::logException) + disposable += rxBus + .toObservable(EventNewBG::class.java) + .observeOn(aapsSchedulers.io) + .subscribe({ scheduleExecution() }, fabricPrivacy::logException) + disposable += rxBus + .toObservable(EventNewHistoryData::class.java) + .observeOn(aapsSchedulers.io) + .subscribe({ scheduleExecution() }, fabricPrivacy::logException) runLoop = Runnable { handler.postDelayed(runLoop, REFRESH_INTERVAL) @@ -314,7 +328,7 @@ class NSClientV3Plugin @Inject constructor( // 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.PairEffectiveProfileSwitch -> dataPair.value.toJson(false, dateUtil) + is DataSyncSelector.PairEffectiveProfileSwitch -> dataPair.value.toNSEffectiveProfileSwitch(dateUtil) // is DataSyncSelector.PairOfflineEvent -> dataPair.value.toJson(false, dateUtil) else -> null }?.let { data -> @@ -353,7 +367,14 @@ class NSClientV3Plugin @Inject constructor( // 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.PairEffectiveProfileSwitch -> dataPair.value.toJson(false, dateUtil) + is DataSyncSelector.PairEffectiveProfileSwitch -> { + if (result.response == 201) { // created + dataPair.value.interfaceIDs.nightscoutId = result.identifier + storeDataForDb.nsIdEffectiveProfileSwitches.add(dataPair.value) + storeDataForDb.scheduleNsIdUpdate() + } + dataSyncSelector.confirmLastEffectiveProfileSwitchIdIfGreater(dataPair.id) + } // is DataSyncSelector.PairOfflineEvent -> dataPair.value.toJson(false, dateUtil) } } catch (e: Exception) { @@ -415,4 +436,20 @@ class NSClientV3Plugin @Inject constructor( return true return false } + + private val eventWorker = Executors.newSingleThreadScheduledExecutor() + private var scheduledEventPost: ScheduledFuture<*>? = null + fun scheduleExecution() { + class PostRunnable : Runnable { + + override fun run() { + scheduledEventPost = null + executeLoop() + } + } + // cancel waiting task to prevent sending multiple posts + scheduledEventPost?.cancel(false) + val task: Runnable = PostRunnable() + scheduledEventPost = eventWorker.schedule(task, 10, TimeUnit.SECONDS) + } } \ No newline at end of file diff --git a/plugins/sync/src/main/java/info/nightscout/plugins/sync/nsclientV3/extensions/EffectiveProfileSwitchExtension.kt b/plugins/sync/src/main/java/info/nightscout/plugins/sync/nsclientV3/extensions/EffectiveProfileSwitchExtension.kt index 9a38c4013f..949eb42c4a 100644 --- a/plugins/sync/src/main/java/info/nightscout/plugins/sync/nsclientV3/extensions/EffectiveProfileSwitchExtension.kt +++ b/plugins/sync/src/main/java/info/nightscout/plugins/sync/nsclientV3/extensions/EffectiveProfileSwitchExtension.kt @@ -5,6 +5,7 @@ import info.nightscout.core.profile.ProfileSealed import info.nightscout.database.entities.EffectiveProfileSwitch import info.nightscout.database.entities.embedments.InterfaceIDs import info.nightscout.plugins.sync.nsclient.extensions.fromConstant +import info.nightscout.sdk.localmodel.treatment.EventType import info.nightscout.sdk.localmodel.treatment.NSEffectiveProfileSwitch import info.nightscout.shared.utils.DateUtil @@ -30,4 +31,25 @@ fun NSEffectiveProfileSwitch.toEffectiveProfileSwitch(dateUtil: DateUtil): Effec insulinConfiguration = profileSealed.insulinConfiguration, interfaceIDs_backing = InterfaceIDs(nightscoutId = identifier, pumpId = pumpId, pumpType = InterfaceIDs.PumpType.fromString(pumpType), pumpSerial = pumpSerial, endId = endId) ) -} \ No newline at end of file +} + +fun EffectiveProfileSwitch.toNSEffectiveProfileSwitch(dateUtil: DateUtil) : NSEffectiveProfileSwitch = + NSEffectiveProfileSwitch( + eventType = EventType.NOTE, + isValid = isValid, + date = timestamp, + utcOffset = utcOffset, + profileJson = ProfileSealed.EPS(this).toPureNsJson(dateUtil), + originalProfileName = originalProfileName, + originalCustomizedName = originalCustomizedName, + originalTimeshift = originalTimeshift, + originalPercentage = originalPercentage, + originalDuration = originalDuration, + originalEnd = originalEnd, + notes = originalCustomizedName, + identifier = interfaceIDs.nightscoutId, + pumpId = interfaceIDs.pumpId, + pumpType = interfaceIDs.pumpType?.name, + pumpSerial = interfaceIDs.pumpSerial, + endId = interfaceIDs.endId + ) \ No newline at end of file