NSC: simplify db interface

This commit is contained in:
Milos Kozak 2022-12-12 10:54:55 +01:00
parent 63215c778a
commit 114710b292
8 changed files with 156 additions and 131 deletions

View file

@ -17,19 +17,21 @@ import org.json.JSONObject
interface DataSyncSelector { interface DataSyncSelector {
data class PairTemporaryTarget(val value: TemporaryTarget, val updateRecordId: Long) interface DataPair
data class PairGlucoseValue(val value: GlucoseValue, val updateRecordId: Long) data class PairTemporaryTarget(val value: TemporaryTarget, val updateRecordId: Long): DataPair
data class PairTherapyEvent(val value: TherapyEvent, val updateRecordId: Long) data class PairGlucoseValue(val value: GlucoseValue, val updateRecordId: Long): DataPair
data class PairFood(val value: Food, val updateRecordId: Long) data class PairTherapyEvent(val value: TherapyEvent, val updateRecordId: Long): DataPair
data class PairBolus(val value: Bolus, val updateRecordId: Long) data class PairFood(val value: Food, val updateRecordId: Long): DataPair
data class PairCarbs(val value: Carbs, val updateRecordId: Long) data class PairBolus(val value: Bolus, val updateRecordId: Long): DataPair
data class PairBolusCalculatorResult(val value: BolusCalculatorResult, val updateRecordId: Long) data class PairCarbs(val value: Carbs, val updateRecordId: Long): DataPair
data class PairTemporaryBasal(val value: TemporaryBasal, val updateRecordId: Long) data class PairBolusCalculatorResult(val value: BolusCalculatorResult, val updateRecordId: Long): DataPair
data class PairExtendedBolus(val value: ExtendedBolus, val updateRecordId: Long) data class PairTemporaryBasal(val value: TemporaryBasal, val updateRecordId: Long): DataPair
data class PairProfileSwitch(val value: ProfileSwitch, val updateRecordId: Long) data class PairExtendedBolus(val value: ExtendedBolus, val updateRecordId: Long): DataPair
data class PairEffectiveProfileSwitch(val value: EffectiveProfileSwitch, val updateRecordId: Long) data class PairProfileSwitch(val value: ProfileSwitch, val updateRecordId: Long): DataPair
data class PairOfflineEvent(val value: OfflineEvent, val updateRecordId: Long) data class PairEffectiveProfileSwitch(val value: EffectiveProfileSwitch, val updateRecordId: Long): DataPair
data class PairProfileStore(val value: JSONObject, val timestampSync: Long) data class PairOfflineEvent(val value: OfflineEvent, val updateRecordId: Long): DataPair
data class PairProfileStore(val value: JSONObject, val timestampSync: Long): DataPair
data class PairDeviceStatus(val value: DeviceStatus, val unused: Long?): DataPair
fun queueSize(): Long fun queueSize(): Long

View file

@ -23,6 +23,6 @@ interface NsClient : Sync {
fun resetToFullSync() fun resetToFullSync()
fun dbAdd(collection: String, data: JSONObject, originalObject: Any, progress: String) fun dbAdd(collection: String, originalObject: DataSyncSelector.DataPair, progress: String)
fun dbUpdate(collection: String, _id: String?, data: JSONObject?, originalObject: Any, progress: String) fun dbUpdate(collection: String, originalObject: DataSyncSelector.DataPair, progress: String)
} }

View file

@ -1,6 +1,5 @@
package info.nightscout.plugins.sync.nsclient package info.nightscout.plugins.sync.nsclient
import info.nightscout.core.extensions.toJson
import info.nightscout.database.ValueWrapper import info.nightscout.database.ValueWrapper
import info.nightscout.database.entities.Bolus import info.nightscout.database.entities.Bolus
import info.nightscout.database.entities.BolusCalculatorResult import info.nightscout.database.entities.BolusCalculatorResult
@ -20,7 +19,6 @@ import info.nightscout.interfaces.plugin.ActivePlugin
import info.nightscout.interfaces.profile.ProfileFunction import info.nightscout.interfaces.profile.ProfileFunction
import info.nightscout.interfaces.sync.DataSyncSelector import info.nightscout.interfaces.sync.DataSyncSelector
import info.nightscout.plugins.sync.R import info.nightscout.plugins.sync.R
import info.nightscout.plugins.sync.nsclient.extensions.toJson
import info.nightscout.rx.logging.AAPSLogger import info.nightscout.rx.logging.AAPSLogger
import info.nightscout.rx.logging.LTag import info.nightscout.rx.logging.LTag
import info.nightscout.shared.sharedPreferences.SP import info.nightscout.shared.sharedPreferences.SP
@ -161,7 +159,6 @@ class DataSyncSelectorImplementation @Inject constructor(
bolus.first.interfaceIDs.nightscoutId == null -> bolus.first.interfaceIDs.nightscoutId == null ->
activePlugin.activeNsClient?.dbAdd( activePlugin.activeNsClient?.dbAdd(
"treatments", "treatments",
bolus.first.toJson(true, dateUtil),
DataSyncSelector.PairBolus(bolus.first, bolus.second.id), DataSyncSelector.PairBolus(bolus.first, bolus.second.id),
" $startId/$lastDbId" " $startId/$lastDbId"
) )
@ -169,8 +166,6 @@ class DataSyncSelectorImplementation @Inject constructor(
bolus.first.interfaceIDs.nightscoutId != null && bolus.first.id != bolus.second.id -> bolus.first.interfaceIDs.nightscoutId != null && bolus.first.id != bolus.second.id ->
activePlugin.activeNsClient?.dbUpdate( activePlugin.activeNsClient?.dbUpdate(
"treatments", "treatments",
bolus.first.interfaceIDs.nightscoutId,
bolus.first.toJson(false, dateUtil),
DataSyncSelector.PairBolus(bolus.first, bolus.second.id), DataSyncSelector.PairBolus(bolus.first, bolus.second.id),
"$startId/$lastDbId" "$startId/$lastDbId"
) )
@ -222,13 +217,11 @@ class DataSyncSelectorImplementation @Inject constructor(
} }
// without nsId = create new // without nsId = create new
carb.first.interfaceIDs.nightscoutId == null -> carb.first.interfaceIDs.nightscoutId == null ->
activePlugin.activeNsClient?.dbAdd("treatments", carb.first.toJson(true, dateUtil), DataSyncSelector.PairCarbs(carb.first, carb.second.id), "$startId/$lastDbId") activePlugin.activeNsClient?.dbAdd("treatments", DataSyncSelector.PairCarbs(carb.first, carb.second.id), "$startId/$lastDbId")
// with nsId = update if it's modified record // with nsId = update if it's modified record
carb.first.interfaceIDs.nightscoutId != null && carb.first.id != carb.second.id -> carb.first.interfaceIDs.nightscoutId != null && carb.first.id != carb.second.id ->
activePlugin.activeNsClient?.dbUpdate( activePlugin.activeNsClient?.dbUpdate(
"treatments", "treatments",
carb.first.interfaceIDs.nightscoutId,
carb.first.toJson(false, dateUtil),
DataSyncSelector.PairCarbs(carb.first, carb.second.id), DataSyncSelector.PairCarbs(carb.first, carb.second.id),
"$startId/$lastDbId" "$startId/$lastDbId"
) )
@ -282,14 +275,13 @@ class DataSyncSelectorImplementation @Inject constructor(
bolusCalculatorResult.first.interfaceIDs.nightscoutId == null -> bolusCalculatorResult.first.interfaceIDs.nightscoutId == null ->
activePlugin.activeNsClient?.dbAdd( activePlugin.activeNsClient?.dbAdd(
"treatments", "treatments",
bolusCalculatorResult.first.toJson(true, dateUtil, profileFunction),
DataSyncSelector.PairBolusCalculatorResult(bolusCalculatorResult.first, bolusCalculatorResult.second.id), DataSyncSelector.PairBolusCalculatorResult(bolusCalculatorResult.first, bolusCalculatorResult.second.id),
"$startId/$lastDbId" "$startId/$lastDbId"
) )
// with nsId = update if it's modified record // with nsId = update if it's modified record
bolusCalculatorResult.first.interfaceIDs.nightscoutId != null && bolusCalculatorResult.first.id != bolusCalculatorResult.second.id -> bolusCalculatorResult.first.interfaceIDs.nightscoutId != null && bolusCalculatorResult.first.id != bolusCalculatorResult.second.id ->
activePlugin.activeNsClient?.dbUpdate( activePlugin.activeNsClient?.dbUpdate(
"treatments", bolusCalculatorResult.first.interfaceIDs.nightscoutId, bolusCalculatorResult.first.toJson(false, dateUtil, profileFunction), "treatments",
DataSyncSelector.PairBolusCalculatorResult(bolusCalculatorResult.first, bolusCalculatorResult.second.id), "$startId/$lastDbId" DataSyncSelector.PairBolusCalculatorResult(bolusCalculatorResult.first, bolusCalculatorResult.second.id), "$startId/$lastDbId"
) )
} }
@ -342,7 +334,6 @@ class DataSyncSelectorImplementation @Inject constructor(
tt.first.interfaceIDs.nightscoutId == null -> tt.first.interfaceIDs.nightscoutId == null ->
activePlugin.activeNsClient?.dbAdd( activePlugin.activeNsClient?.dbAdd(
"treatments", "treatments",
tt.first.toJson(true, profileFunction.getUnits(), dateUtil),
DataSyncSelector.PairTemporaryTarget(tt.first, tt.second.id), DataSyncSelector.PairTemporaryTarget(tt.first, tt.second.id),
"$startId/$lastDbId" "$startId/$lastDbId"
) )
@ -350,8 +341,6 @@ class DataSyncSelectorImplementation @Inject constructor(
tt.first.interfaceIDs.nightscoutId != null -> tt.first.interfaceIDs.nightscoutId != null ->
activePlugin.activeNsClient?.dbUpdate( activePlugin.activeNsClient?.dbUpdate(
"treatments", "treatments",
tt.first.interfaceIDs.nightscoutId,
tt.first.toJson(false, profileFunction.getUnits(), dateUtil),
DataSyncSelector.PairTemporaryTarget(tt.first, tt.second.id), DataSyncSelector.PairTemporaryTarget(tt.first, tt.second.id),
"$startId/$lastDbId" "$startId/$lastDbId"
) )
@ -403,13 +392,11 @@ class DataSyncSelectorImplementation @Inject constructor(
} }
// without nsId = create new // without nsId = create new
food.first.interfaceIDs.nightscoutId == null -> food.first.interfaceIDs.nightscoutId == null ->
activePlugin.activeNsClient?.dbAdd("food", food.first.toJson(true), DataSyncSelector.PairFood(food.first, food.second.id), "$startId/$lastDbId") activePlugin.activeNsClient?.dbAdd("food", DataSyncSelector.PairFood(food.first, food.second.id), "$startId/$lastDbId")
// with nsId = update // with nsId = update
food.first.interfaceIDs.nightscoutId != null -> food.first.interfaceIDs.nightscoutId != null ->
activePlugin.activeNsClient?.dbUpdate( activePlugin.activeNsClient?.dbUpdate(
"food", "food",
food.first.interfaceIDs.nightscoutId,
food.first.toJson(false),
DataSyncSelector.PairFood(food.first, food.second.id), DataSyncSelector.PairFood(food.first, food.second.id),
"$startId/$lastDbId" "$startId/$lastDbId"
) )
@ -462,13 +449,11 @@ class DataSyncSelectorImplementation @Inject constructor(
} }
// without nsId = create new // without nsId = create new
gv.first.interfaceIDs.nightscoutId == null -> gv.first.interfaceIDs.nightscoutId == null ->
activePlugin.activeNsClient?.dbAdd("entries", gv.first.toJson(true, dateUtil), DataSyncSelector.PairGlucoseValue(gv.first, gv.second.id), "$startId/$lastDbId") activePlugin.activeNsClient?.dbAdd("entries", DataSyncSelector.PairGlucoseValue(gv.first, gv.second.id), "$startId/$lastDbId")
// with nsId = update // with nsId = update
else -> // gv.first.interfaceIDs.nightscoutId != null else -> // gv.first.interfaceIDs.nightscoutId != null
activePlugin.activeNsClient?.dbUpdate( activePlugin.activeNsClient?.dbUpdate(
"entries", "entries",
gv.first.interfaceIDs.nightscoutId,
gv.first.toJson(false, dateUtil),
DataSyncSelector.PairGlucoseValue(gv.first, gv.second.id), DataSyncSelector.PairGlucoseValue(gv.first, gv.second.id),
"$startId/$lastDbId" "$startId/$lastDbId"
) )
@ -524,13 +509,11 @@ class DataSyncSelectorImplementation @Inject constructor(
} }
// without nsId = create new // without nsId = create new
te.first.interfaceIDs.nightscoutId == null -> te.first.interfaceIDs.nightscoutId == null ->
activePlugin.activeNsClient?.dbAdd("treatments", te.first.toJson(true, dateUtil), DataSyncSelector.PairTherapyEvent(te.first, te.second.id), "$startId/$lastDbId") activePlugin.activeNsClient?.dbAdd("treatments", DataSyncSelector.PairTherapyEvent(te.first, te.second.id), "$startId/$lastDbId")
// nsId = update // nsId = update
te.first.interfaceIDs.nightscoutId != null -> te.first.interfaceIDs.nightscoutId != null ->
activePlugin.activeNsClient?.dbUpdate( activePlugin.activeNsClient?.dbUpdate(
"treatments", "treatments",
te.first.interfaceIDs.nightscoutId,
te.first.toJson(false, dateUtil),
DataSyncSelector.PairTherapyEvent(te.first, te.second.id), DataSyncSelector.PairTherapyEvent(te.first, te.second.id),
"$startId/$lastDbId" "$startId/$lastDbId"
) )
@ -567,7 +550,7 @@ class DataSyncSelectorImplementation @Inject constructor(
when { when {
// without nsId = create new // without nsId = create new
deviceStatus.interfaceIDs.nightscoutId == null -> deviceStatus.interfaceIDs.nightscoutId == null ->
activePlugin.activeNsClient?.dbAdd("devicestatus", deviceStatus.toJson(dateUtil), deviceStatus, "$startId/$lastDbId") activePlugin.activeNsClient?.dbAdd("devicestatus", DataSyncSelector.PairDeviceStatus(deviceStatus, null), "$startId/$lastDbId")
// with nsId = ignore // with nsId = ignore
deviceStatus.interfaceIDs.nightscoutId != null -> Any() deviceStatus.interfaceIDs.nightscoutId != null -> Any()
} }
@ -601,48 +584,37 @@ class DataSyncSelectorImplementation @Inject constructor(
queueCounter.tbrsRemaining = lastDbId - startId queueCounter.tbrsRemaining = lastDbId - startId
appRepository.getNextSyncElementTemporaryBasal(startId).blockingGet()?.let { tb -> appRepository.getNextSyncElementTemporaryBasal(startId).blockingGet()?.let { tb ->
aapsLogger.info(LTag.NSCLIENT, "Loading TemporaryBasal data Start: $startId ID: ${tb.first.id} HistoryID: ${tb.second} ") aapsLogger.info(LTag.NSCLIENT, "Loading TemporaryBasal data Start: $startId ID: ${tb.first.id} HistoryID: ${tb.second} ")
val profile = profileFunction.getProfile(tb.first.timestamp) when {
if (profile != null) { // new record with existing NS id => must be coming from NS => ignore
when { tb.first.id == tb.second.id && tb.first.interfaceIDs.nightscoutId != null -> {
// new record with existing NS id => must be coming from NS => ignore aapsLogger.info(LTag.NSCLIENT, "Ignoring TemporaryBasal. Loaded from NS: ${tb.first.id} HistoryID: ${tb.second.id} ")
tb.first.id == tb.second.id && tb.first.interfaceIDs.nightscoutId != null -> { confirmLastTemporaryBasalIdIfGreater(tb.second.id)
aapsLogger.info(LTag.NSCLIENT, "Ignoring TemporaryBasal. Loaded from NS: ${tb.first.id} HistoryID: ${tb.second.id} ") processChangedTemporaryBasalsCompat()
confirmLastTemporaryBasalIdIfGreater(tb.second.id) return
processChangedTemporaryBasalsCompat()
return
}
// only NsId changed, no need to upload
tb.first.onlyNsIdAdded(tb.second) -> {
aapsLogger.info(LTag.NSCLIENT, "Ignoring TemporaryBasal. Only NS id changed ID: ${tb.first.id} HistoryID: ${tb.second.id} ")
confirmLastTemporaryBasalIdIfGreater(tb.second.id)
processChangedTemporaryBasalsCompat()
return
}
// without nsId = create new
tb.first.interfaceIDs.nightscoutId == null ->
activePlugin.activeNsClient?.dbAdd(
"treatments",
tb.first.toJson(true, profile, dateUtil),
DataSyncSelector.PairTemporaryBasal(tb.first, tb.second.id),
"$startId/$lastDbId"
)
// with nsId = update
tb.first.interfaceIDs.nightscoutId != null ->
activePlugin.activeNsClient?.dbUpdate(
"treatments",
tb.first.interfaceIDs.nightscoutId,
tb.first.toJson(false, profile, dateUtil),
DataSyncSelector.PairTemporaryBasal(tb.first, tb.second.id),
"$startId/$lastDbId"
)
} }
return // only NsId changed, no need to upload
} else { tb.first.onlyNsIdAdded(tb.second) -> {
aapsLogger.info(LTag.NSCLIENT, "Ignoring TemporaryBasal. No profile: ${tb.first.id} HistoryID: ${tb.second.id} ") aapsLogger.info(LTag.NSCLIENT, "Ignoring TemporaryBasal. Only NS id changed ID: ${tb.first.id} HistoryID: ${tb.second.id} ")
confirmLastTemporaryBasalIdIfGreater(tb.second.id) confirmLastTemporaryBasalIdIfGreater(tb.second.id)
processChangedTemporaryBasalsCompat() processChangedTemporaryBasalsCompat()
return return
}
// without nsId = create new
tb.first.interfaceIDs.nightscoutId == null ->
activePlugin.activeNsClient?.dbAdd(
"treatments",
DataSyncSelector.PairTemporaryBasal(tb.first, tb.second.id),
"$startId/$lastDbId"
)
// with nsId = update
tb.first.interfaceIDs.nightscoutId != null ->
activePlugin.activeNsClient?.dbUpdate(
"treatments",
DataSyncSelector.PairTemporaryBasal(tb.first, tb.second.id),
"$startId/$lastDbId"
)
} }
return
} }
} }
@ -693,7 +665,6 @@ class DataSyncSelectorImplementation @Inject constructor(
eb.first.interfaceIDs.nightscoutId == null -> eb.first.interfaceIDs.nightscoutId == null ->
activePlugin.activeNsClient?.dbAdd( activePlugin.activeNsClient?.dbAdd(
"treatments", "treatments",
eb.first.toJson(true, profile, dateUtil),
DataSyncSelector.PairExtendedBolus(eb.first, eb.second.id), DataSyncSelector.PairExtendedBolus(eb.first, eb.second.id),
"$startId/$lastDbId" "$startId/$lastDbId"
) )
@ -701,8 +672,6 @@ class DataSyncSelectorImplementation @Inject constructor(
eb.first.interfaceIDs.nightscoutId != null -> eb.first.interfaceIDs.nightscoutId != null ->
activePlugin.activeNsClient?.dbUpdate( activePlugin.activeNsClient?.dbUpdate(
"treatments", "treatments",
eb.first.interfaceIDs.nightscoutId,
eb.first.toJson(false, profile, dateUtil),
DataSyncSelector.PairExtendedBolus(eb.first, eb.second.id), DataSyncSelector.PairExtendedBolus(eb.first, eb.second.id),
"$startId/$lastDbId" "$startId/$lastDbId"
) )
@ -759,13 +728,11 @@ class DataSyncSelectorImplementation @Inject constructor(
} }
// without nsId = create new // without nsId = create new
ps.first.interfaceIDs.nightscoutId == null -> ps.first.interfaceIDs.nightscoutId == null ->
activePlugin.activeNsClient?.dbAdd("treatments", ps.first.toJson(true, dateUtil), DataSyncSelector.PairProfileSwitch(ps.first, ps.second.id), "$startId/$lastDbId") activePlugin.activeNsClient?.dbAdd("treatments", DataSyncSelector.PairProfileSwitch(ps.first, ps.second.id), "$startId/$lastDbId")
// with nsId = update // with nsId = update
ps.first.interfaceIDs.nightscoutId != null -> ps.first.interfaceIDs.nightscoutId != null ->
activePlugin.activeNsClient?.dbUpdate( activePlugin.activeNsClient?.dbUpdate(
"treatments", "treatments",
ps.first.interfaceIDs.nightscoutId,
ps.first.toJson(false, dateUtil),
DataSyncSelector.PairProfileSwitch(ps.first, ps.second.id), DataSyncSelector.PairProfileSwitch(ps.first, ps.second.id),
"$startId/$lastDbId" "$startId/$lastDbId"
) )
@ -818,7 +785,6 @@ class DataSyncSelectorImplementation @Inject constructor(
ps.first.interfaceIDs.nightscoutId == null -> ps.first.interfaceIDs.nightscoutId == null ->
activePlugin.activeNsClient?.dbAdd( activePlugin.activeNsClient?.dbAdd(
"treatments", "treatments",
ps.first.toJson(true, dateUtil),
DataSyncSelector.PairEffectiveProfileSwitch(ps.first, ps.second.id), DataSyncSelector.PairEffectiveProfileSwitch(ps.first, ps.second.id),
"$startId/$lastDbId" "$startId/$lastDbId"
) )
@ -826,8 +792,6 @@ class DataSyncSelectorImplementation @Inject constructor(
ps.first.interfaceIDs.nightscoutId != null -> ps.first.interfaceIDs.nightscoutId != null ->
activePlugin.activeNsClient?.dbUpdate( activePlugin.activeNsClient?.dbUpdate(
"treatments", "treatments",
ps.first.interfaceIDs.nightscoutId,
ps.first.toJson(false, dateUtil),
DataSyncSelector.PairEffectiveProfileSwitch(ps.first, ps.second.id), DataSyncSelector.PairEffectiveProfileSwitch(ps.first, ps.second.id),
"$startId/$lastDbId" "$startId/$lastDbId"
) )
@ -879,13 +843,11 @@ class DataSyncSelectorImplementation @Inject constructor(
} }
// without nsId = create new // without nsId = create new
oe.first.interfaceIDs.nightscoutId == null -> oe.first.interfaceIDs.nightscoutId == null ->
activePlugin.activeNsClient?.dbAdd("treatments", oe.first.toJson(true, dateUtil), DataSyncSelector.PairOfflineEvent(oe.first, oe.second.id), "$startId/$lastDbId") activePlugin.activeNsClient?.dbAdd("treatments", DataSyncSelector.PairOfflineEvent(oe.first, oe.second.id), "$startId/$lastDbId")
// existing with nsId = update // existing with nsId = update
oe.first.interfaceIDs.nightscoutId != null -> oe.first.interfaceIDs.nightscoutId != null ->
activePlugin.activeNsClient?.dbUpdate( activePlugin.activeNsClient?.dbUpdate(
"treatments", "treatments",
oe.first.interfaceIDs.nightscoutId,
oe.first.toJson(false, dateUtil),
DataSyncSelector.PairOfflineEvent(oe.first, oe.second.id), DataSyncSelector.PairOfflineEvent(oe.first, oe.second.id),
"$startId/$lastDbId" "$startId/$lastDbId"
) )
@ -905,7 +867,7 @@ class DataSyncSelectorImplementation @Inject constructor(
if (lastChange > lastSync) { if (lastChange > lastSync) {
if (activePlugin.activeProfileSource.profile?.allProfilesValid != true) return if (activePlugin.activeProfileSource.profile?.allProfilesValid != true) return
val profileJson = activePlugin.activeProfileSource.profile?.data ?: return val profileJson = activePlugin.activeProfileSource.profile?.data ?: return
activePlugin.activeNsClient?.dbAdd("profile", profileJson, DataSyncSelector.PairProfileStore(profileJson, dateUtil.now()), "") activePlugin.activeNsClient?.dbAdd("profile", DataSyncSelector.PairProfileStore(profileJson, dateUtil.now()), "")
} }
} }
} }

View file

@ -12,6 +12,7 @@ import androidx.preference.PreferenceFragmentCompat
import androidx.preference.PreferenceScreen import androidx.preference.PreferenceScreen
import androidx.preference.SwitchPreference import androidx.preference.SwitchPreference
import dagger.android.HasAndroidInjector import dagger.android.HasAndroidInjector
import info.nightscout.core.extensions.toJson
import info.nightscout.core.utils.fabric.FabricPrivacy import info.nightscout.core.utils.fabric.FabricPrivacy
import info.nightscout.interfaces.Config import info.nightscout.interfaces.Config
import info.nightscout.interfaces.Constants import info.nightscout.interfaces.Constants
@ -20,6 +21,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.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
import info.nightscout.interfaces.sync.NsClient import info.nightscout.interfaces.sync.NsClient
@ -32,6 +34,7 @@ import info.nightscout.plugins.sync.nsShared.events.EventNSClientResend
import info.nightscout.plugins.sync.nsShared.events.EventNSClientStatus import info.nightscout.plugins.sync.nsShared.events.EventNSClientStatus
import info.nightscout.plugins.sync.nsShared.events.EventNSClientUpdateGUI import info.nightscout.plugins.sync.nsShared.events.EventNSClientUpdateGUI
import info.nightscout.plugins.sync.nsclient.data.AlarmAck import info.nightscout.plugins.sync.nsclient.data.AlarmAck
import info.nightscout.plugins.sync.nsclient.extensions.toJson
import info.nightscout.plugins.sync.nsclient.services.NSClientService import info.nightscout.plugins.sync.nsclient.services.NSClientService
import info.nightscout.rx.AapsSchedulers import info.nightscout.rx.AapsSchedulers
import info.nightscout.rx.bus.RxBus import info.nightscout.rx.bus.RxBus
@ -45,9 +48,9 @@ import info.nightscout.rx.logging.AAPSLogger
import info.nightscout.rx.logging.LTag import info.nightscout.rx.logging.LTag
import info.nightscout.shared.interfaces.ResourceHelper import info.nightscout.shared.interfaces.ResourceHelper
import info.nightscout.shared.sharedPreferences.SP import info.nightscout.shared.sharedPreferences.SP
import info.nightscout.shared.utils.DateUtil
import io.reactivex.rxjava3.disposables.CompositeDisposable import io.reactivex.rxjava3.disposables.CompositeDisposable
import io.reactivex.rxjava3.kotlin.plusAssign import io.reactivex.rxjava3.kotlin.plusAssign
import org.json.JSONObject
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
@ -65,7 +68,9 @@ class NSClientPlugin @Inject constructor(
private val config: Config, private val config: Config,
private val dataSyncSelector: DataSyncSelector, private val dataSyncSelector: DataSyncSelector,
private val uiInteraction: UiInteraction, private val uiInteraction: UiInteraction,
private val activePlugin: ActivePlugin private val activePlugin: ActivePlugin,
private val dateUtil: DateUtil,
private val profileFunction: ProfileFunction
) : NsClient, Sync, PluginBase( ) : NsClient, Sync, PluginBase(
PluginDescription() PluginDescription()
.mainType(PluginType.SYNC) .mainType(PluginType.SYNC)
@ -237,11 +242,60 @@ class NSClientPlugin @Inject constructor(
dataSyncSelector.resetToNextFullSync() dataSyncSelector.resetToNextFullSync()
} }
override fun dbAdd(collection: String, data: JSONObject, originalObject: Any, progress: String) { override fun dbAdd(collection: String, originalObject: DataSyncSelector.DataPair, progress: String) {
nsClientService?.dbAdd(collection, data, originalObject, progress) when (originalObject) {
is DataSyncSelector.PairBolus -> originalObject.value.toJson(true, dateUtil)
is DataSyncSelector.PairCarbs -> originalObject.value.toJson(true, dateUtil)
is DataSyncSelector.PairBolusCalculatorResult -> originalObject.value.toJson(true, dateUtil, profileFunction)
is DataSyncSelector.PairTemporaryTarget -> originalObject.value.toJson(true, profileFunction.getUnits(), dateUtil)
is DataSyncSelector.PairFood -> originalObject.value.toJson(true)
is DataSyncSelector.PairGlucoseValue -> originalObject.value.toJson(true, dateUtil)
is DataSyncSelector.PairTherapyEvent -> originalObject.value.toJson(true, dateUtil)
is DataSyncSelector.PairDeviceStatus -> originalObject.value.toJson(dateUtil)
is DataSyncSelector.PairTemporaryBasal -> originalObject.value.toJson(true, profileFunction.getProfile(originalObject.value.timestamp), dateUtil)
is DataSyncSelector.PairExtendedBolus -> originalObject.value.toJson(true, profileFunction.getProfile(originalObject.value.timestamp), dateUtil)
is DataSyncSelector.PairProfileSwitch -> originalObject.value.toJson(true, dateUtil)
is DataSyncSelector.PairEffectiveProfileSwitch -> originalObject.value.toJson(true, dateUtil)
is DataSyncSelector.PairOfflineEvent -> originalObject.value.toJson(true, dateUtil)
is DataSyncSelector.PairProfileStore -> originalObject.value
else -> null
}?.let { data ->
nsClientService?.dbAdd(collection, data, originalObject, progress)
}
} }
override fun dbUpdate(collection: String, _id: String?, data: JSONObject?, originalObject: Any, progress: String) { override fun dbUpdate(collection: String, originalObject: DataSyncSelector.DataPair, progress: String) {
nsClientService?.dbUpdate(collection, _id, data, originalObject, progress) val id = when (originalObject) {
is DataSyncSelector.PairBolus -> originalObject.value.interfaceIDs.nightscoutId
is DataSyncSelector.PairCarbs -> originalObject.value.interfaceIDs.nightscoutId
is DataSyncSelector.PairBolusCalculatorResult -> originalObject.value.interfaceIDs.nightscoutId
is DataSyncSelector.PairTemporaryTarget -> originalObject.value.interfaceIDs.nightscoutId
is DataSyncSelector.PairFood -> originalObject.value.interfaceIDs.nightscoutId
is DataSyncSelector.PairGlucoseValue -> originalObject.value.interfaceIDs.nightscoutId
is DataSyncSelector.PairTherapyEvent -> originalObject.value.interfaceIDs.nightscoutId
is DataSyncSelector.PairTemporaryBasal -> originalObject.value.interfaceIDs.nightscoutId
is DataSyncSelector.PairExtendedBolus -> originalObject.value.interfaceIDs.nightscoutId
is DataSyncSelector.PairProfileSwitch -> originalObject.value.interfaceIDs.nightscoutId
is DataSyncSelector.PairEffectiveProfileSwitch -> originalObject.value.interfaceIDs.nightscoutId
is DataSyncSelector.PairOfflineEvent -> originalObject.value.interfaceIDs.nightscoutId
else -> throw IllegalStateException()
}
when (originalObject) {
is DataSyncSelector.PairBolus -> originalObject.value.toJson(false, dateUtil)
is DataSyncSelector.PairCarbs -> originalObject.value.toJson(false, dateUtil)
is DataSyncSelector.PairBolusCalculatorResult -> originalObject.value.toJson(false, dateUtil, profileFunction)
is DataSyncSelector.PairTemporaryTarget -> originalObject.value.toJson(false, profileFunction.getUnits(), dateUtil)
is DataSyncSelector.PairFood -> originalObject.value.toJson(false)
is DataSyncSelector.PairGlucoseValue -> originalObject.value.toJson(false, dateUtil)
is DataSyncSelector.PairTherapyEvent -> originalObject.value.toJson(false, dateUtil)
is DataSyncSelector.PairTemporaryBasal -> originalObject.value.toJson(false, profileFunction.getProfile(originalObject.value.timestamp), dateUtil)
is DataSyncSelector.PairExtendedBolus -> originalObject.value.toJson(false, profileFunction.getProfile(originalObject.value.timestamp), dateUtil)
is DataSyncSelector.PairProfileSwitch -> originalObject.value.toJson(false, dateUtil)
is DataSyncSelector.PairEffectiveProfileSwitch -> originalObject.value.toJson(false, dateUtil)
is DataSyncSelector.PairOfflineEvent -> originalObject.value.toJson(false, dateUtil)
else -> null
}?.let { data ->
nsClientService?.dbUpdate(collection, id, data, originalObject, progress)
}
} }
} }

View file

@ -9,12 +9,15 @@ import info.nightscout.shared.utils.DateUtil
import info.nightscout.shared.utils.T import info.nightscout.shared.utils.T
import org.json.JSONObject import org.json.JSONObject
fun ExtendedBolus.toJson(isAdd: Boolean, profile: Profile, dateUtil: DateUtil): JSONObject = fun ExtendedBolus.toJson(isAdd: Boolean, profile: Profile?, dateUtil: DateUtil): JSONObject? =
if (isEmulatingTempBasal) profile?.let {
toTemporaryBasal(profile) if (isEmulatingTempBasal)
.toJson(isAdd, profile, dateUtil) toTemporaryBasal(profile)
.put("extendedEmulated", toRealJson(isAdd, dateUtil)) .toJson(isAdd, profile, dateUtil)
else toRealJson(isAdd, dateUtil) ?.put("extendedEmulated", toRealJson(isAdd, dateUtil))
else toRealJson(isAdd, dateUtil)
}
fun ExtendedBolus.toRealJson(isAdd: Boolean, dateUtil: DateUtil): JSONObject = fun ExtendedBolus.toRealJson(isAdd: Boolean, dateUtil: DateUtil): JSONObject =
JSONObject() JSONObject()

View file

@ -10,25 +10,27 @@ import info.nightscout.shared.utils.DateUtil
import info.nightscout.shared.utils.T import info.nightscout.shared.utils.T
import org.json.JSONObject import org.json.JSONObject
fun TemporaryBasal.toJson(isAdd: Boolean, profile: Profile, dateUtil: DateUtil): JSONObject = fun TemporaryBasal.toJson(isAdd: Boolean, profile: Profile?, dateUtil: DateUtil): JSONObject? =
JSONObject() profile?.let {
.put("created_at", dateUtil.toISOString(timestamp)) JSONObject()
.put("enteredBy", "openaps://" + "AndroidAPS") .put("created_at", dateUtil.toISOString(timestamp))
.put("eventType", info.nightscout.database.entities.TherapyEvent.Type.TEMPORARY_BASAL.text) .put("enteredBy", "openaps://" + "AndroidAPS")
.put("isValid", isValid) .put("eventType", info.nightscout.database.entities.TherapyEvent.Type.TEMPORARY_BASAL.text)
.put("duration", T.msecs(duration).mins()) .put("isValid", isValid)
.put("durationInMilliseconds", duration) // rounded duration leads to different basal IOB .put("duration", T.msecs(duration).mins())
.put("type", type.name) .put("durationInMilliseconds", duration) // rounded duration leads to different basal IOB
.put("rate", convertedToAbsolute(timestamp, profile)) // generated by OpenAPS, for compatibility .put("type", type.name)
.also { .put("rate", convertedToAbsolute(timestamp, profile)) // generated by OpenAPS, for compatibility
if (isAbsolute) it.put("absolute", rate) .also {
else it.put("percent", rate - 100) if (isAbsolute) it.put("absolute", rate)
if (interfaceIDs.pumpId != null) it.put("pumpId", interfaceIDs.pumpId) else it.put("percent", rate - 100)
if (interfaceIDs.endId != null) it.put("endId", interfaceIDs.endId) if (interfaceIDs.pumpId != null) it.put("pumpId", interfaceIDs.pumpId)
if (interfaceIDs.pumpType != null) it.put("pumpType", interfaceIDs.pumpType!!.name) if (interfaceIDs.endId != null) it.put("endId", interfaceIDs.endId)
if (interfaceIDs.pumpSerial != null) it.put("pumpSerial", interfaceIDs.pumpSerial) if (interfaceIDs.pumpType != null) it.put("pumpType", interfaceIDs.pumpType!!.name)
if (isAdd && interfaceIDs.nightscoutId != null) it.put("_id", interfaceIDs.nightscoutId) if (interfaceIDs.pumpSerial != null) it.put("pumpSerial", interfaceIDs.pumpSerial)
} if (isAdd && interfaceIDs.nightscoutId != null) it.put("_id", interfaceIDs.nightscoutId)
}
}
fun temporaryBasalFromJson(jsonObject: JSONObject): TemporaryBasal? { fun temporaryBasalFromJson(jsonObject: JSONObject): TemporaryBasal? {
val timestamp = JsonHelper.safeGetLongAllowNull(jsonObject, "mills", null) ?: return null val timestamp = JsonHelper.safeGetLongAllowNull(jsonObject, "mills", null) ?: return null

View file

@ -6,7 +6,6 @@ import androidx.work.WorkerParameters
import androidx.work.workDataOf import androidx.work.workDataOf
import info.nightscout.core.utils.receivers.DataWorkerStorage import info.nightscout.core.utils.receivers.DataWorkerStorage
import info.nightscout.core.utils.worker.LoggingWorker import info.nightscout.core.utils.worker.LoggingWorker
import info.nightscout.database.entities.DeviceStatus
import info.nightscout.database.impl.AppRepository import info.nightscout.database.impl.AppRepository
import info.nightscout.database.impl.transactions.UpdateNsIdBolusCalculatorResultTransaction import info.nightscout.database.impl.transactions.UpdateNsIdBolusCalculatorResultTransaction
import info.nightscout.database.impl.transactions.UpdateNsIdBolusTransaction import info.nightscout.database.impl.transactions.UpdateNsIdBolusTransaction
@ -274,10 +273,10 @@ class NSClientAddAckWorker(
dataSyncSelector.processChangedEffectiveProfileSwitchesCompat() dataSyncSelector.processChangedEffectiveProfileSwitchesCompat()
} }
is DeviceStatus -> { is DataSyncSelector.PairDeviceStatus -> {
val deviceStatus = ack.originalObject val deviceStatus = ack.originalObject
deviceStatus.interfaceIDs.nightscoutId = ack.id deviceStatus.value.interfaceIDs.nightscoutId = ack.id
repository.runTransactionForResult(UpdateNsIdDeviceStatusTransaction(deviceStatus)) repository.runTransactionForResult(UpdateNsIdDeviceStatusTransaction(deviceStatus.value))
.doOnError { error -> .doOnError { error ->
aapsLogger.error(LTag.DATABASE, "Updated ns id of DeviceStatus failed", error) aapsLogger.error(LTag.DATABASE, "Updated ns id of DeviceStatus failed", error)
ret = Result.failure((workDataOf("Error" to error.toString()))) ret = Result.failure((workDataOf("Error" to error.toString())))
@ -285,10 +284,10 @@ class NSClientAddAckWorker(
.doOnSuccess { .doOnSuccess {
ret = Result.success(workDataOf("ProcessedData" to deviceStatus.toString())) ret = Result.success(workDataOf("ProcessedData" to deviceStatus.toString()))
aapsLogger.debug(LTag.DATABASE, "Updated ns id of DeviceStatus $deviceStatus") aapsLogger.debug(LTag.DATABASE, "Updated ns id of DeviceStatus $deviceStatus")
dataSyncSelector.confirmLastDeviceStatusIdIfGreater(deviceStatus.id) dataSyncSelector.confirmLastDeviceStatusIdIfGreater(deviceStatus.value.id)
} }
.blockingGet() .blockingGet()
rxBus.send(EventNSClientNewLog("DBADD", "Acked DeviceStatus " + deviceStatus.interfaceIDs.nightscoutId)) rxBus.send(EventNSClientNewLog("DBADD", "Acked DeviceStatus " + deviceStatus.value.interfaceIDs.nightscoutId))
// Send new if waiting // Send new if waiting
dataSyncSelector.processChangedDeviceStatusesCompat() dataSyncSelector.processChangedDeviceStatusesCompat()
} }

View file

@ -19,6 +19,7 @@ import info.nightscout.interfaces.nsclient.NSAlarm
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.sync.DataSyncSelector
import info.nightscout.interfaces.sync.NsClient import info.nightscout.interfaces.sync.NsClient
import info.nightscout.interfaces.sync.Sync import info.nightscout.interfaces.sync.Sync
import info.nightscout.interfaces.ui.UiInteraction import info.nightscout.interfaces.ui.UiInteraction
@ -301,11 +302,13 @@ class NSClientV3Plugin @Inject constructor(
storeLastFetched() storeLastFetched()
} }
override fun dbAdd(collection: String, data: JSONObject, originalObject: Any, progress: String) { override fun dbAdd(collection: String, originalObject: DataSyncSelector.DataPair, progress: String) {
TODO("Not yet implemented") if (collection == "treatments") {
//val result = nsAndroidClient.createTreatment()
}
} }
override fun dbUpdate(collection: String, _id: String?, data: JSONObject?, originalObject: Any, progress: String) { override fun dbUpdate(collection: String, originalObject: DataSyncSelector.DataPair, progress: String) {
TODO("Not yet implemented") TODO("Not yet implemented")
} }