NSCv1: fix TBR upload

This commit is contained in:
Milos Kozak 2023-07-21 18:08:26 +02:00
parent a25da53e5a
commit 18f01d7b59
4 changed files with 52 additions and 43 deletions

View file

@ -1,6 +1,7 @@
package info.nightscout.interfaces.sync package info.nightscout.interfaces.sync
import info.nightscout.interfaces.nsclient.NSAlarm import info.nightscout.interfaces.nsclient.NSAlarm
import info.nightscout.interfaces.profile.Profile
import info.nightscout.rx.events.EventNSClientNewLog import info.nightscout.rx.events.EventNSClientNewLog
/** /**
@ -91,7 +92,7 @@ interface NsClient : Sync {
* @param progress progress of sync in format "number/number". Only for display in fragment * @param progress progress of sync in format "number/number". Only for display in fragment
* @return true for successful upload * @return true for successful upload
*/ */
suspend fun nsAdd(collection: String, dataPair: DataSyncSelector.DataPair, progress: String): Boolean suspend fun nsAdd(collection: String, dataPair: DataSyncSelector.DataPair, progress: String, profile: Profile? = null): Boolean
/** /**
* Upload updated record to NS * Upload updated record to NS
@ -101,5 +102,5 @@ interface NsClient : Sync {
* @param progress progress of sync in format "number/number". Only for display in fragment * @param progress progress of sync in format "number/number". Only for display in fragment
* @return true for successful upload * @return true for successful upload
*/ */
suspend fun nsUpdate(collection: String, dataPair: DataSyncSelector.DataPair, progress: String): Boolean suspend fun nsUpdate(collection: String, dataPair: DataSyncSelector.DataPair, progress: String, profile: Profile? = null): Boolean
} }

View file

@ -493,6 +493,8 @@ class DataSyncSelectorV1Impl @Inject constructor(
rxBus.send(EventNSClientUpdateGuiQueue()) rxBus.send(EventNSClientUpdateGuiQueue())
appRepository.getNextSyncElementTemporaryBasal(startId).blockingGet()?.let { tb -> appRepository.getNextSyncElementTemporaryBasal(startId).blockingGet()?.let { tb ->
aapsLogger.info(LTag.NSCLIENT, "Loading TemporaryBasal data Start: $startId ${tb.first} forID: ${tb.second.id} ") aapsLogger.info(LTag.NSCLIENT, "Loading TemporaryBasal data Start: $startId ${tb.first} forID: ${tb.second.id} ")
val profile = profileFunction.getProfile(tb.first.timestamp)
if (profile != null) {
when { when {
// new record with existing NS id => must be coming from NS => ignore // new record with existing NS id => must be coming from NS => ignore
tb.first.id == tb.second.id && tb.first.interfaceIDs.nightscoutId != null -> { tb.first.id == tb.second.id && tb.first.interfaceIDs.nightscoutId != null -> {
@ -510,12 +512,18 @@ class DataSyncSelectorV1Impl @Inject constructor(
} }
// without nsId = create new // without nsId = create new
tb.first.interfaceIDs.nightscoutId == null -> tb.first.interfaceIDs.nightscoutId == null ->
activePlugin.activeNsClient?.nsAdd("treatments", DataSyncSelector.PairTemporaryBasal(tb.first, tb.second.id), "$startId/$lastDbId") activePlugin.activeNsClient?.nsAdd("treatments", DataSyncSelector.PairTemporaryBasal(tb.first, tb.second.id), "$startId/$lastDbId", profile)
// with nsId = update // with nsId = update
tb.first.interfaceIDs.nightscoutId != null -> tb.first.interfaceIDs.nightscoutId != null ->
activePlugin.activeNsClient?.nsUpdate("treatments", DataSyncSelector.PairTemporaryBasal(tb.first, tb.second.id), "$startId/$lastDbId") activePlugin.activeNsClient?.nsUpdate("treatments", DataSyncSelector.PairTemporaryBasal(tb.first, tb.second.id), "$startId/$lastDbId", profile)
} }
return return
} else {
aapsLogger.info(LTag.NSCLIENT, "Ignoring TemporaryBasal. No profile: ${tb.second.id} ")
confirmLastTemporaryBasalIdIfGreater(tb.second.id)
processChangedTemporaryBasals()
return
}
} }
} }
@ -557,10 +565,10 @@ class DataSyncSelectorV1Impl @Inject constructor(
} }
// without nsId = create new // without nsId = create new
eb.first.interfaceIDs.nightscoutId == null -> eb.first.interfaceIDs.nightscoutId == null ->
activePlugin.activeNsClient?.nsAdd("treatments", DataSyncSelector.PairExtendedBolus(eb.first, eb.second.id), "$startId/$lastDbId") activePlugin.activeNsClient?.nsAdd("treatments", DataSyncSelector.PairExtendedBolus(eb.first, eb.second.id), "$startId/$lastDbId", profile)
// with nsId = update // with nsId = update
eb.first.interfaceIDs.nightscoutId != null -> eb.first.interfaceIDs.nightscoutId != null ->
activePlugin.activeNsClient?.nsUpdate("treatments", DataSyncSelector.PairExtendedBolus(eb.first, eb.second.id), "$startId/$lastDbId") activePlugin.activeNsClient?.nsUpdate("treatments", DataSyncSelector.PairExtendedBolus(eb.first, eb.second.id), "$startId/$lastDbId", profile)
} }
return return
} else { } else {

View file

@ -5,6 +5,7 @@ import android.content.Context
import android.content.Intent import android.content.Intent
import android.content.ServiceConnection import android.content.ServiceConnection
import android.os.IBinder import android.os.IBinder
import android.provider.ContactsContract
import androidx.preference.PreferenceFragmentCompat import androidx.preference.PreferenceFragmentCompat
import androidx.preference.PreferenceScreen import androidx.preference.PreferenceScreen
import androidx.preference.SwitchPreference import androidx.preference.SwitchPreference
@ -19,6 +20,7 @@ import info.nightscout.interfaces.plugin.ActivePlugin
import info.nightscout.interfaces.plugin.PluginBase import info.nightscout.interfaces.plugin.PluginBase
import info.nightscout.interfaces.plugin.PluginDescription import info.nightscout.interfaces.plugin.PluginDescription
import info.nightscout.interfaces.plugin.PluginType import info.nightscout.interfaces.plugin.PluginType
import info.nightscout.interfaces.profile.Profile
import info.nightscout.interfaces.profile.ProfileFunction import info.nightscout.interfaces.profile.ProfileFunction
import info.nightscout.interfaces.source.DoingOwnUploadSource import info.nightscout.interfaces.source.DoingOwnUploadSource
import info.nightscout.interfaces.sync.DataSyncSelector import info.nightscout.interfaces.sync.DataSyncSelector
@ -200,7 +202,7 @@ class NSClientPlugin @Inject constructor(
dataSyncSelector.resetToNextFullSync() dataSyncSelector.resetToNextFullSync()
} }
override suspend fun nsAdd(collection: String, dataPair: DataSyncSelector.DataPair, progress: String): Boolean { override suspend fun nsAdd(collection: String, dataPair: DataSyncSelector.DataPair, progress: String, profile: Profile?): Boolean {
when (dataPair) { when (dataPair) {
is DataSyncSelector.PairBolus -> dataPair.value.toJson(true, dateUtil) is DataSyncSelector.PairBolus -> dataPair.value.toJson(true, dateUtil)
is DataSyncSelector.PairCarbs -> dataPair.value.toJson(true, dateUtil) is DataSyncSelector.PairCarbs -> dataPair.value.toJson(true, dateUtil)
@ -210,8 +212,8 @@ class NSClientPlugin @Inject constructor(
is DataSyncSelector.PairGlucoseValue -> dataPair.value.toJson(true, dateUtil) is DataSyncSelector.PairGlucoseValue -> dataPair.value.toJson(true, dateUtil)
is DataSyncSelector.PairTherapyEvent -> dataPair.value.toJson(true, dateUtil) is DataSyncSelector.PairTherapyEvent -> dataPair.value.toJson(true, dateUtil)
is DataSyncSelector.PairDeviceStatus -> dataPair.value.toJson(dateUtil) is DataSyncSelector.PairDeviceStatus -> dataPair.value.toJson(dateUtil)
is DataSyncSelector.PairTemporaryBasal -> dataPair.value.toJson(true, profileFunction.getProfile(dataPair.value.timestamp), dateUtil) is DataSyncSelector.PairTemporaryBasal -> dataPair.value.toJson(true, profile, dateUtil)
is DataSyncSelector.PairExtendedBolus -> dataPair.value.toJson(true, profileFunction.getProfile(dataPair.value.timestamp), dateUtil) is DataSyncSelector.PairExtendedBolus -> dataPair.value.toJson(true, profile, dateUtil)
is DataSyncSelector.PairProfileSwitch -> dataPair.value.toJson(true, dateUtil) is DataSyncSelector.PairProfileSwitch -> dataPair.value.toJson(true, dateUtil)
is DataSyncSelector.PairEffectiveProfileSwitch -> dataPair.value.toJson(true, dateUtil) is DataSyncSelector.PairEffectiveProfileSwitch -> dataPair.value.toJson(true, dateUtil)
is DataSyncSelector.PairOfflineEvent -> dataPair.value.toJson(true, dateUtil) is DataSyncSelector.PairOfflineEvent -> dataPair.value.toJson(true, dateUtil)
@ -223,7 +225,7 @@ class NSClientPlugin @Inject constructor(
return true return true
} }
override suspend fun nsUpdate(collection: String, dataPair: DataSyncSelector.DataPair, progress: String): Boolean { override suspend fun nsUpdate(collection: String, dataPair: DataSyncSelector.DataPair, progress: String, profile: Profile?): Boolean {
val id = when (dataPair) { val id = when (dataPair) {
is DataSyncSelector.PairBolus -> dataPair.value.interfaceIDs.nightscoutId is DataSyncSelector.PairBolus -> dataPair.value.interfaceIDs.nightscoutId
is DataSyncSelector.PairCarbs -> dataPair.value.interfaceIDs.nightscoutId is DataSyncSelector.PairCarbs -> dataPair.value.interfaceIDs.nightscoutId
@ -247,8 +249,8 @@ class NSClientPlugin @Inject constructor(
is DataSyncSelector.PairFood -> dataPair.value.toJson(false) is DataSyncSelector.PairFood -> dataPair.value.toJson(false)
is DataSyncSelector.PairGlucoseValue -> dataPair.value.toJson(false, dateUtil) is DataSyncSelector.PairGlucoseValue -> dataPair.value.toJson(false, dateUtil)
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, profile, dateUtil)
is DataSyncSelector.PairExtendedBolus -> dataPair.value.toJson(false, profileFunction.getProfile(dataPair.value.timestamp), dateUtil) is DataSyncSelector.PairExtendedBolus -> dataPair.value.toJson(false, profile, dateUtil)
is DataSyncSelector.PairProfileSwitch -> dataPair.value.toJson(false, dateUtil) is DataSyncSelector.PairProfileSwitch -> dataPair.value.toJson(false, dateUtil)
is DataSyncSelector.PairEffectiveProfileSwitch -> dataPair.value.toJson(false, dateUtil) is DataSyncSelector.PairEffectiveProfileSwitch -> dataPair.value.toJson(false, dateUtil)
is DataSyncSelector.PairOfflineEvent -> dataPair.value.toJson(false, dateUtil) is DataSyncSelector.PairOfflineEvent -> dataPair.value.toJson(false, dateUtil)

View file

@ -27,7 +27,7 @@ import info.nightscout.interfaces.nsclient.StoreDataForDb
import info.nightscout.interfaces.plugin.PluginBase import info.nightscout.interfaces.plugin.PluginBase
import info.nightscout.interfaces.plugin.PluginDescription import info.nightscout.interfaces.plugin.PluginDescription
import info.nightscout.interfaces.plugin.PluginType import info.nightscout.interfaces.plugin.PluginType
import info.nightscout.interfaces.profile.ProfileFunction import info.nightscout.interfaces.profile.Profile
import info.nightscout.interfaces.source.NSClientSource import info.nightscout.interfaces.source.NSClientSource
import info.nightscout.interfaces.sync.DataSyncSelector import info.nightscout.interfaces.sync.DataSyncSelector
import info.nightscout.interfaces.sync.DataSyncSelectorV3 import info.nightscout.interfaces.sync.DataSyncSelectorV3
@ -90,7 +90,6 @@ import io.socket.client.Ack
import io.socket.client.IO import io.socket.client.IO
import io.socket.client.Socket import io.socket.client.Socket
import io.socket.emitter.Emitter import io.socket.emitter.Emitter
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.Json import kotlinx.serialization.json.Json
import org.json.JSONArray import org.json.JSONArray
import org.json.JSONObject import org.json.JSONObject
@ -116,7 +115,6 @@ class NSClientV3Plugin @Inject constructor(
private val dateUtil: DateUtil, private val dateUtil: DateUtil,
private val uiInteraction: UiInteraction, private val uiInteraction: UiInteraction,
private val dataSyncSelectorV3: DataSyncSelectorV3, private val dataSyncSelectorV3: DataSyncSelectorV3,
private val profileFunction: ProfileFunction,
private val repository: AppRepository, private val repository: AppRepository,
private val nsDeviceStatusHandler: NSDeviceStatusHandler, private val nsDeviceStatusHandler: NSDeviceStatusHandler,
private val nsClientSource: NSClientSource, private val nsClientSource: NSClientSource,
@ -605,11 +603,11 @@ class NSClientV3Plugin @Inject constructor(
dataSyncSelectorV3.resetToNextFullSync() dataSyncSelectorV3.resetToNextFullSync()
} }
override suspend fun nsAdd(collection: String, dataPair: DataSyncSelector.DataPair, progress: String): Boolean = override suspend fun nsAdd(collection: String, dataPair: DataSyncSelector.DataPair, progress: String, profile: Profile?): Boolean =
dbOperation(collection, dataPair, progress, Operation.CREATE) dbOperation(collection, dataPair, progress, Operation.CREATE, profile)
override suspend fun nsUpdate(collection: String, dataPair: DataSyncSelector.DataPair, progress: String): Boolean = override suspend fun nsUpdate(collection: String, dataPair: DataSyncSelector.DataPair, progress: String, profile: Profile?): Boolean =
dbOperation(collection, dataPair, progress, Operation.UPDATE) dbOperation(collection, dataPair, progress, Operation.UPDATE, profile)
enum class Operation { CREATE, UPDATE } enum class Operation { CREATE, UPDATE }
@ -761,7 +759,7 @@ class NSClientV3Plugin @Inject constructor(
return true return true
} }
private suspend fun dbOperationTreatments(collection: String = "treatments", dataPair: DataSyncSelector.DataPair, progress: String, operation: Operation): Boolean { private suspend fun dbOperationTreatments(collection: String = "treatments", dataPair: DataSyncSelector.DataPair, progress: String, operation: Operation, profile: Profile?): Boolean {
val call = when (operation) { val call = when (operation) {
Operation.CREATE -> nsAndroidClient?.let { return@let it::createTreatment } Operation.CREATE -> nsAndroidClient?.let { return@let it::createTreatment }
Operation.UPDATE -> nsAndroidClient?.let { return@let it::updateTreatment } Operation.UPDATE -> nsAndroidClient?.let { return@let it::updateTreatment }
@ -774,12 +772,12 @@ class NSClientV3Plugin @Inject constructor(
is DataSyncSelector.PairTherapyEvent -> dataPair.value.toNSTherapyEvent() is DataSyncSelector.PairTherapyEvent -> dataPair.value.toNSTherapyEvent()
is DataSyncSelector.PairTemporaryBasal -> { is DataSyncSelector.PairTemporaryBasal -> {
val profile = profileFunction.getProfile(dataPair.value.timestamp) ?: return true profile ?: return true
dataPair.value.toNSTemporaryBasal(profile) dataPair.value.toNSTemporaryBasal(profile)
} }
is DataSyncSelector.PairExtendedBolus -> { is DataSyncSelector.PairExtendedBolus -> {
val profile = profileFunction.getProfile(dataPair.value.timestamp) ?: return true profile ?: return true
dataPair.value.toNSExtendedBolus(profile) dataPair.value.toNSExtendedBolus(profile)
} }
@ -882,13 +880,13 @@ class NSClientV3Plugin @Inject constructor(
return true return true
} }
private suspend fun dbOperation(collection: String, dataPair: DataSyncSelector.DataPair, progress: String, operation: Operation): Boolean = private suspend fun dbOperation(collection: String, dataPair: DataSyncSelector.DataPair, progress: String, operation: Operation, profile: Profile?): Boolean =
when (collection) { when (collection) {
"profile" -> dbOperationProfileStore(dataPair = dataPair, progress = progress) "profile" -> dbOperationProfileStore(dataPair = dataPair, progress = progress)
"devicestatus" -> dbOperationDeviceStatus(dataPair = dataPair as DataSyncSelector.PairDeviceStatus, progress = progress) "devicestatus" -> dbOperationDeviceStatus(dataPair = dataPair as DataSyncSelector.PairDeviceStatus, progress = progress)
"entries" -> dbOperationEntries(dataPair = dataPair as DataSyncSelector.PairGlucoseValue, progress = progress, operation = operation) "entries" -> dbOperationEntries(dataPair = dataPair as DataSyncSelector.PairGlucoseValue, progress = progress, operation = operation)
"food" -> dbOperationFood(dataPair = dataPair as DataSyncSelector.PairFood, progress = progress, operation = operation) "food" -> dbOperationFood(dataPair = dataPair as DataSyncSelector.PairFood, progress = progress, operation = operation)
"treatments" -> dbOperationTreatments(dataPair = dataPair, progress = progress, operation = operation) "treatments" -> dbOperationTreatments(dataPair = dataPair, progress = progress, operation = operation, profile = profile)
else -> false else -> false
} }