From 9d87dec574072d74ee749fc505af7594923cb3c6 Mon Sep 17 00:00:00 2001 From: Philoul Date: Wed, 12 May 2021 18:28:52 +0200 Subject: [PATCH] NSClient Fix Use Absolute --- .../general/nsclient/DataSyncSelectorImplementation.kt | 10 ++++++---- .../androidaps/extensions/ExtendedBolusExtension.kt | 4 ++-- .../androidaps/extensions/TemporaryBasalExtension.kt | 4 ++-- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/DataSyncSelectorImplementation.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/DataSyncSelectorImplementation.kt index 9e124bfcca..3e27bb8c4e 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/DataSyncSelectorImplementation.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/DataSyncSelectorImplementation.kt @@ -368,6 +368,7 @@ class DataSyncSelectorImplementation @Inject constructor( override fun processChangedTemporaryBasalsCompat(): Boolean { val startId = sp.getLong(R.string.key_ns_temporary_basal_last_synced_id, 0) + val useAbsolute = sp.getBoolean(R.string.key_ns_sync_use_absolute, false) appRepository.getNextSyncElementTemporaryBasal(startId).blockingGet()?.let { tb -> aapsLogger.info(LTag.DATABASE, "Loading TemporaryBasal data Start: $startId ID: ${tb.first.id} HistoryID: ${tb.second} ") profileFunction.getProfile(tb.first.timestamp)?.let { profile -> @@ -379,10 +380,10 @@ class DataSyncSelectorImplementation @Inject constructor( nsClientPlugin.nsClientService?.dbRemove("treatments", tb.first.interfaceIDs.nightscoutId, DataSyncSelector.PairTemporaryBasal(tb.first, tb.second)) // existing without nsId = create new tb.first.isValid && tb.first.interfaceIDs.nightscoutId == null -> - nsClientPlugin.nsClientService?.dbAdd("treatments", tb.first.toJson(profile, dateUtil), DataSyncSelector.PairTemporaryBasal(tb.first, tb.second)) + nsClientPlugin.nsClientService?.dbAdd("treatments", tb.first.toJson(profile, dateUtil, useAbsolute), DataSyncSelector.PairTemporaryBasal(tb.first, tb.second)) // existing with nsId = update tb.first.isValid && tb.first.interfaceIDs.nightscoutId != null -> - nsClientPlugin.nsClientService?.dbUpdate("treatments", tb.first.interfaceIDs.nightscoutId, tb.first.toJson(profile, dateUtil), DataSyncSelector.PairTemporaryBasal(tb.first, tb.second)) + nsClientPlugin.nsClientService?.dbUpdate("treatments", tb.first.interfaceIDs.nightscoutId, tb.first.toJson(profile, dateUtil, useAbsolute), DataSyncSelector.PairTemporaryBasal(tb.first, tb.second)) } return true } ?: confirmLastTemporaryBasalIdIfGreater(tb.second) @@ -407,6 +408,7 @@ class DataSyncSelectorImplementation @Inject constructor( override fun processChangedExtendedBolusesCompat(): Boolean { val startId = sp.getLong(R.string.key_ns_extended_bolus_last_synced_id, 0) + val useAbsolute = sp.getBoolean(R.string.key_ns_sync_use_absolute, false) appRepository.getNextSyncElementExtendedBolus(startId).blockingGet()?.let { eb -> aapsLogger.info(LTag.DATABASE, "Loading ExtendedBolus data Start: $startId ID: ${eb.first.id} HistoryID: ${eb.second} ") profileFunction.getProfile(eb.first.timestamp)?.let { profile -> @@ -418,10 +420,10 @@ class DataSyncSelectorImplementation @Inject constructor( nsClientPlugin.nsClientService?.dbRemove("treatments", eb.first.interfaceIDs.nightscoutId, DataSyncSelector.PairExtendedBolus(eb.first, eb.second)) // existing without nsId = create new eb.first.isValid && eb.first.interfaceIDs.nightscoutId == null -> - nsClientPlugin.nsClientService?.dbAdd("treatments", eb.first.toJson(profile, dateUtil), DataSyncSelector.PairExtendedBolus(eb.first, eb.second)) + nsClientPlugin.nsClientService?.dbAdd("treatments", eb.first.toJson(profile, dateUtil, useAbsolute), DataSyncSelector.PairExtendedBolus(eb.first, eb.second)) // existing with nsId = update eb.first.isValid && eb.first.interfaceIDs.nightscoutId != null -> - nsClientPlugin.nsClientService?.dbUpdate("treatments", eb.first.interfaceIDs.nightscoutId, eb.first.toJson(profile, dateUtil), DataSyncSelector.PairExtendedBolus(eb.first, eb.second)) + nsClientPlugin.nsClientService?.dbUpdate("treatments", eb.first.interfaceIDs.nightscoutId, eb.first.toJson(profile, dateUtil, useAbsolute), DataSyncSelector.PairExtendedBolus(eb.first, eb.second)) } return true } ?: confirmLastExtendedBolusIdIfGreater(eb.second) diff --git a/core/src/main/java/info/nightscout/androidaps/extensions/ExtendedBolusExtension.kt b/core/src/main/java/info/nightscout/androidaps/extensions/ExtendedBolusExtension.kt index df0802ed19..09f5c9d9a5 100644 --- a/core/src/main/java/info/nightscout/androidaps/extensions/ExtendedBolusExtension.kt +++ b/core/src/main/java/info/nightscout/androidaps/extensions/ExtendedBolusExtension.kt @@ -50,10 +50,10 @@ fun ExtendedBolus.toTemporaryBasal(profile: Profile): TemporaryBasal = type = TemporaryBasal.Type.FAKE_EXTENDED ) -fun ExtendedBolus.toJson(profile: Profile, dateUtil: DateUtil): JSONObject = +fun ExtendedBolus.toJson(profile: Profile, dateUtil: DateUtil, useAbsolute: Boolean): JSONObject = if (isEmulatingTempBasal) toTemporaryBasal(profile) - .toJson(profile, dateUtil) + .toJson(profile, dateUtil, useAbsolute) .put("extendedEmulated", toRealJson(dateUtil)) else toRealJson(dateUtil) diff --git a/core/src/main/java/info/nightscout/androidaps/extensions/TemporaryBasalExtension.kt b/core/src/main/java/info/nightscout/androidaps/extensions/TemporaryBasalExtension.kt index e25a6284d1..2522f930b4 100644 --- a/core/src/main/java/info/nightscout/androidaps/extensions/TemporaryBasalExtension.kt +++ b/core/src/main/java/info/nightscout/androidaps/extensions/TemporaryBasalExtension.kt @@ -62,7 +62,7 @@ fun TemporaryBasal.toStringFull(profile: Profile, dateUtil: DateUtil): String { } } -fun TemporaryBasal.toJson(profile: Profile, dateUtil: DateUtil): JSONObject = +fun TemporaryBasal.toJson(profile: Profile, dateUtil: DateUtil, useAbsolute: Boolean): JSONObject = JSONObject() .put("created_at", dateUtil.toISOString(timestamp)) .put("enteredBy", "openaps://" + "AndroidAPS") @@ -71,7 +71,7 @@ fun TemporaryBasal.toJson(profile: Profile, dateUtil: DateUtil): JSONObject = .put("rate", rate) .put("type", type.name) .also { - if (isAbsolute) it.put("absolute", rate) + if (useAbsolute) it.put("absolute", convertedToAbsolute(timestamp, profile)) else it.put("percent", convertedToPercent(timestamp, profile) - 100) if (interfaceIDs.pumpId != null) it.put("pumpId", interfaceIDs.pumpId) if (interfaceIDs.endId != null) it.put("endId", interfaceIDs.endId)