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 {
data class PairTemporaryTarget(val value: TemporaryTarget, val updateRecordId: Long)
data class PairGlucoseValue(val value: GlucoseValue, val updateRecordId: Long)
data class PairTherapyEvent(val value: TherapyEvent, val updateRecordId: Long)
data class PairFood(val value: Food, val updateRecordId: Long)
data class PairBolus(val value: Bolus, val updateRecordId: Long)
data class PairCarbs(val value: Carbs, val updateRecordId: Long)
data class PairBolusCalculatorResult(val value: BolusCalculatorResult, val updateRecordId: Long)
data class PairTemporaryBasal(val value: TemporaryBasal, val updateRecordId: Long)
data class PairExtendedBolus(val value: ExtendedBolus, val updateRecordId: Long)
data class PairProfileSwitch(val value: ProfileSwitch, val updateRecordId: Long)
data class PairEffectiveProfileSwitch(val value: EffectiveProfileSwitch, val updateRecordId: Long)
data class PairOfflineEvent(val value: OfflineEvent, val updateRecordId: Long)
data class PairProfileStore(val value: JSONObject, val timestampSync: Long)
interface DataPair
data class PairTemporaryTarget(val value: TemporaryTarget, val updateRecordId: Long): DataPair
data class PairGlucoseValue(val value: GlucoseValue, val updateRecordId: Long): DataPair
data class PairTherapyEvent(val value: TherapyEvent, val updateRecordId: Long): DataPair
data class PairFood(val value: Food, val updateRecordId: Long): DataPair
data class PairBolus(val value: Bolus, val updateRecordId: Long): DataPair
data class PairCarbs(val value: Carbs, val updateRecordId: Long): DataPair
data class PairBolusCalculatorResult(val value: BolusCalculatorResult, val updateRecordId: Long): DataPair
data class PairTemporaryBasal(val value: TemporaryBasal, val updateRecordId: Long): DataPair
data class PairExtendedBolus(val value: ExtendedBolus, val updateRecordId: Long): DataPair
data class PairProfileSwitch(val value: ProfileSwitch, val updateRecordId: Long): DataPair
data class PairEffectiveProfileSwitch(val value: EffectiveProfileSwitch, val updateRecordId: Long): DataPair
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

View file

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

View file

@ -1,6 +1,5 @@
package info.nightscout.plugins.sync.nsclient
import info.nightscout.core.extensions.toJson
import info.nightscout.database.ValueWrapper
import info.nightscout.database.entities.Bolus
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.sync.DataSyncSelector
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.LTag
import info.nightscout.shared.sharedPreferences.SP
@ -161,7 +159,6 @@ class DataSyncSelectorImplementation @Inject constructor(
bolus.first.interfaceIDs.nightscoutId == null ->
activePlugin.activeNsClient?.dbAdd(
"treatments",
bolus.first.toJson(true, dateUtil),
DataSyncSelector.PairBolus(bolus.first, bolus.second.id),
" $startId/$lastDbId"
)
@ -169,8 +166,6 @@ class DataSyncSelectorImplementation @Inject constructor(
bolus.first.interfaceIDs.nightscoutId != null && bolus.first.id != bolus.second.id ->
activePlugin.activeNsClient?.dbUpdate(
"treatments",
bolus.first.interfaceIDs.nightscoutId,
bolus.first.toJson(false, dateUtil),
DataSyncSelector.PairBolus(bolus.first, bolus.second.id),
"$startId/$lastDbId"
)
@ -222,13 +217,11 @@ class DataSyncSelectorImplementation @Inject constructor(
}
// without nsId = create new
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
carb.first.interfaceIDs.nightscoutId != null && carb.first.id != carb.second.id ->
activePlugin.activeNsClient?.dbUpdate(
"treatments",
carb.first.interfaceIDs.nightscoutId,
carb.first.toJson(false, dateUtil),
DataSyncSelector.PairCarbs(carb.first, carb.second.id),
"$startId/$lastDbId"
)
@ -282,14 +275,13 @@ class DataSyncSelectorImplementation @Inject constructor(
bolusCalculatorResult.first.interfaceIDs.nightscoutId == null ->
activePlugin.activeNsClient?.dbAdd(
"treatments",
bolusCalculatorResult.first.toJson(true, dateUtil, profileFunction),
DataSyncSelector.PairBolusCalculatorResult(bolusCalculatorResult.first, bolusCalculatorResult.second.id),
"$startId/$lastDbId"
)
// with nsId = update if it's modified record
bolusCalculatorResult.first.interfaceIDs.nightscoutId != null && bolusCalculatorResult.first.id != bolusCalculatorResult.second.id ->
activePlugin.activeNsClient?.dbUpdate(
"treatments", bolusCalculatorResult.first.interfaceIDs.nightscoutId, bolusCalculatorResult.first.toJson(false, dateUtil, profileFunction),
"treatments",
DataSyncSelector.PairBolusCalculatorResult(bolusCalculatorResult.first, bolusCalculatorResult.second.id), "$startId/$lastDbId"
)
}
@ -342,7 +334,6 @@ class DataSyncSelectorImplementation @Inject constructor(
tt.first.interfaceIDs.nightscoutId == null ->
activePlugin.activeNsClient?.dbAdd(
"treatments",
tt.first.toJson(true, profileFunction.getUnits(), dateUtil),
DataSyncSelector.PairTemporaryTarget(tt.first, tt.second.id),
"$startId/$lastDbId"
)
@ -350,8 +341,6 @@ class DataSyncSelectorImplementation @Inject constructor(
tt.first.interfaceIDs.nightscoutId != null ->
activePlugin.activeNsClient?.dbUpdate(
"treatments",
tt.first.interfaceIDs.nightscoutId,
tt.first.toJson(false, profileFunction.getUnits(), dateUtil),
DataSyncSelector.PairTemporaryTarget(tt.first, tt.second.id),
"$startId/$lastDbId"
)
@ -403,13 +392,11 @@ class DataSyncSelectorImplementation @Inject constructor(
}
// without nsId = create new
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
food.first.interfaceIDs.nightscoutId != null ->
activePlugin.activeNsClient?.dbUpdate(
"food",
food.first.interfaceIDs.nightscoutId,
food.first.toJson(false),
DataSyncSelector.PairFood(food.first, food.second.id),
"$startId/$lastDbId"
)
@ -462,13 +449,11 @@ class DataSyncSelectorImplementation @Inject constructor(
}
// without nsId = create new
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
else -> // gv.first.interfaceIDs.nightscoutId != null
activePlugin.activeNsClient?.dbUpdate(
"entries",
gv.first.interfaceIDs.nightscoutId,
gv.first.toJson(false, dateUtil),
DataSyncSelector.PairGlucoseValue(gv.first, gv.second.id),
"$startId/$lastDbId"
)
@ -524,13 +509,11 @@ class DataSyncSelectorImplementation @Inject constructor(
}
// without nsId = create new
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
te.first.interfaceIDs.nightscoutId != null ->
activePlugin.activeNsClient?.dbUpdate(
"treatments",
te.first.interfaceIDs.nightscoutId,
te.first.toJson(false, dateUtil),
DataSyncSelector.PairTherapyEvent(te.first, te.second.id),
"$startId/$lastDbId"
)
@ -567,7 +550,7 @@ class DataSyncSelectorImplementation @Inject constructor(
when {
// without nsId = create new
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
deviceStatus.interfaceIDs.nightscoutId != null -> Any()
}
@ -601,48 +584,37 @@ class DataSyncSelectorImplementation @Inject constructor(
queueCounter.tbrsRemaining = lastDbId - startId
appRepository.getNextSyncElementTemporaryBasal(startId).blockingGet()?.let { tb ->
aapsLogger.info(LTag.NSCLIENT, "Loading TemporaryBasal data Start: $startId ID: ${tb.first.id} HistoryID: ${tb.second} ")
val profile = profileFunction.getProfile(tb.first.timestamp)
if (profile != null) {
when {
// new record with existing NS id => must be coming from NS => ignore
tb.first.id == tb.second.id && tb.first.interfaceIDs.nightscoutId != null -> {
aapsLogger.info(LTag.NSCLIENT, "Ignoring TemporaryBasal. Loaded from NS: ${tb.first.id} HistoryID: ${tb.second.id} ")
confirmLastTemporaryBasalIdIfGreater(tb.second.id)
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"
)
when {
// new record with existing NS id => must be coming from NS => ignore
tb.first.id == tb.second.id && tb.first.interfaceIDs.nightscoutId != null -> {
aapsLogger.info(LTag.NSCLIENT, "Ignoring TemporaryBasal. Loaded from NS: ${tb.first.id} HistoryID: ${tb.second.id} ")
confirmLastTemporaryBasalIdIfGreater(tb.second.id)
processChangedTemporaryBasalsCompat()
return
}
return
} else {
aapsLogger.info(LTag.NSCLIENT, "Ignoring TemporaryBasal. No profile: ${tb.first.id} HistoryID: ${tb.second.id} ")
confirmLastTemporaryBasalIdIfGreater(tb.second.id)
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",
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 ->
activePlugin.activeNsClient?.dbAdd(
"treatments",
eb.first.toJson(true, profile, dateUtil),
DataSyncSelector.PairExtendedBolus(eb.first, eb.second.id),
"$startId/$lastDbId"
)
@ -701,8 +672,6 @@ class DataSyncSelectorImplementation @Inject constructor(
eb.first.interfaceIDs.nightscoutId != null ->
activePlugin.activeNsClient?.dbUpdate(
"treatments",
eb.first.interfaceIDs.nightscoutId,
eb.first.toJson(false, profile, dateUtil),
DataSyncSelector.PairExtendedBolus(eb.first, eb.second.id),
"$startId/$lastDbId"
)
@ -759,13 +728,11 @@ class DataSyncSelectorImplementation @Inject constructor(
}
// without nsId = create new
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
ps.first.interfaceIDs.nightscoutId != null ->
activePlugin.activeNsClient?.dbUpdate(
"treatments",
ps.first.interfaceIDs.nightscoutId,
ps.first.toJson(false, dateUtil),
DataSyncSelector.PairProfileSwitch(ps.first, ps.second.id),
"$startId/$lastDbId"
)
@ -818,7 +785,6 @@ class DataSyncSelectorImplementation @Inject constructor(
ps.first.interfaceIDs.nightscoutId == null ->
activePlugin.activeNsClient?.dbAdd(
"treatments",
ps.first.toJson(true, dateUtil),
DataSyncSelector.PairEffectiveProfileSwitch(ps.first, ps.second.id),
"$startId/$lastDbId"
)
@ -826,8 +792,6 @@ class DataSyncSelectorImplementation @Inject constructor(
ps.first.interfaceIDs.nightscoutId != null ->
activePlugin.activeNsClient?.dbUpdate(
"treatments",
ps.first.interfaceIDs.nightscoutId,
ps.first.toJson(false, dateUtil),
DataSyncSelector.PairEffectiveProfileSwitch(ps.first, ps.second.id),
"$startId/$lastDbId"
)
@ -879,13 +843,11 @@ class DataSyncSelectorImplementation @Inject constructor(
}
// without nsId = create new
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
oe.first.interfaceIDs.nightscoutId != null ->
activePlugin.activeNsClient?.dbUpdate(
"treatments",
oe.first.interfaceIDs.nightscoutId,
oe.first.toJson(false, dateUtil),
DataSyncSelector.PairOfflineEvent(oe.first, oe.second.id),
"$startId/$lastDbId"
)
@ -905,7 +867,7 @@ class DataSyncSelectorImplementation @Inject constructor(
if (lastChange > lastSync) {
if (activePlugin.activeProfileSource.profile?.allProfilesValid != true) 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.SwitchPreference
import dagger.android.HasAndroidInjector
import info.nightscout.core.extensions.toJson
import info.nightscout.core.utils.fabric.FabricPrivacy
import info.nightscout.interfaces.Config
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.PluginDescription
import info.nightscout.interfaces.plugin.PluginType
import info.nightscout.interfaces.profile.ProfileFunction
import info.nightscout.interfaces.source.DoingOwnUploadSource
import info.nightscout.interfaces.sync.DataSyncSelector
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.EventNSClientUpdateGUI
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.rx.AapsSchedulers
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.shared.interfaces.ResourceHelper
import info.nightscout.shared.sharedPreferences.SP
import info.nightscout.shared.utils.DateUtil
import io.reactivex.rxjava3.disposables.CompositeDisposable
import io.reactivex.rxjava3.kotlin.plusAssign
import org.json.JSONObject
import javax.inject.Inject
import javax.inject.Singleton
@ -65,7 +68,9 @@ class NSClientPlugin @Inject constructor(
private val config: Config,
private val dataSyncSelector: DataSyncSelector,
private val uiInteraction: UiInteraction,
private val activePlugin: ActivePlugin
private val activePlugin: ActivePlugin,
private val dateUtil: DateUtil,
private val profileFunction: ProfileFunction
) : NsClient, Sync, PluginBase(
PluginDescription()
.mainType(PluginType.SYNC)
@ -237,11 +242,60 @@ class NSClientPlugin @Inject constructor(
dataSyncSelector.resetToNextFullSync()
}
override fun dbAdd(collection: String, data: JSONObject, originalObject: Any, progress: String) {
nsClientService?.dbAdd(collection, data, originalObject, progress)
override fun dbAdd(collection: String, originalObject: DataSyncSelector.DataPair, progress: String) {
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) {
nsClientService?.dbUpdate(collection, _id, data, originalObject, progress)
override fun dbUpdate(collection: String, originalObject: DataSyncSelector.DataPair, progress: String) {
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 org.json.JSONObject
fun ExtendedBolus.toJson(isAdd: Boolean, profile: Profile, dateUtil: DateUtil): JSONObject =
if (isEmulatingTempBasal)
toTemporaryBasal(profile)
.toJson(isAdd, profile, dateUtil)
.put("extendedEmulated", toRealJson(isAdd, dateUtil))
else toRealJson(isAdd, dateUtil)
fun ExtendedBolus.toJson(isAdd: Boolean, profile: Profile?, dateUtil: DateUtil): JSONObject? =
profile?.let {
if (isEmulatingTempBasal)
toTemporaryBasal(profile)
.toJson(isAdd, profile, dateUtil)
?.put("extendedEmulated", toRealJson(isAdd, dateUtil))
else toRealJson(isAdd, dateUtil)
}
fun ExtendedBolus.toRealJson(isAdd: Boolean, dateUtil: DateUtil): JSONObject =
JSONObject()

View file

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

View file

@ -6,7 +6,6 @@ import androidx.work.WorkerParameters
import androidx.work.workDataOf
import info.nightscout.core.utils.receivers.DataWorkerStorage
import info.nightscout.core.utils.worker.LoggingWorker
import info.nightscout.database.entities.DeviceStatus
import info.nightscout.database.impl.AppRepository
import info.nightscout.database.impl.transactions.UpdateNsIdBolusCalculatorResultTransaction
import info.nightscout.database.impl.transactions.UpdateNsIdBolusTransaction
@ -274,10 +273,10 @@ class NSClientAddAckWorker(
dataSyncSelector.processChangedEffectiveProfileSwitchesCompat()
}
is DeviceStatus -> {
is DataSyncSelector.PairDeviceStatus -> {
val deviceStatus = ack.originalObject
deviceStatus.interfaceIDs.nightscoutId = ack.id
repository.runTransactionForResult(UpdateNsIdDeviceStatusTransaction(deviceStatus))
deviceStatus.value.interfaceIDs.nightscoutId = ack.id
repository.runTransactionForResult(UpdateNsIdDeviceStatusTransaction(deviceStatus.value))
.doOnError { error ->
aapsLogger.error(LTag.DATABASE, "Updated ns id of DeviceStatus failed", error)
ret = Result.failure((workDataOf("Error" to error.toString())))
@ -285,10 +284,10 @@ class NSClientAddAckWorker(
.doOnSuccess {
ret = Result.success(workDataOf("ProcessedData" to deviceStatus.toString()))
aapsLogger.debug(LTag.DATABASE, "Updated ns id of DeviceStatus $deviceStatus")
dataSyncSelector.confirmLastDeviceStatusIdIfGreater(deviceStatus.id)
dataSyncSelector.confirmLastDeviceStatusIdIfGreater(deviceStatus.value.id)
}
.blockingGet()
rxBus.send(EventNSClientNewLog("DBADD", "Acked DeviceStatus " + deviceStatus.interfaceIDs.nightscoutId))
rxBus.send(EventNSClientNewLog("DBADD", "Acked DeviceStatus " + deviceStatus.value.interfaceIDs.nightscoutId))
// Send new if waiting
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.PluginDescription
import info.nightscout.interfaces.plugin.PluginType
import info.nightscout.interfaces.sync.DataSyncSelector
import info.nightscout.interfaces.sync.NsClient
import info.nightscout.interfaces.sync.Sync
import info.nightscout.interfaces.ui.UiInteraction
@ -301,11 +302,13 @@ class NSClientV3Plugin @Inject constructor(
storeLastFetched()
}
override fun dbAdd(collection: String, data: JSONObject, originalObject: Any, progress: String) {
TODO("Not yet implemented")
override fun dbAdd(collection: String, originalObject: DataSyncSelector.DataPair, progress: String) {
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")
}