better sync TBR

This commit is contained in:
Milos Kozak 2021-11-07 16:55:34 +01:00
parent f09d967e59
commit e14fc44957
8 changed files with 27 additions and 21 deletions

View file

@ -621,7 +621,6 @@ class DataSyncSelectorImplementation @Inject constructor(
//private var lastTbrId = -1L
//private var lastTbrTime = -1L
override fun processChangedTemporaryBasalsCompat(): Boolean {
val useAbsolute = sp.getBoolean(R.string.key_ns_sync_use_absolute, false)
val lastDbIdWrapped = appRepository.getLastTemporaryBasalIdWrapped().blockingGet()
val lastDbId = if (lastDbIdWrapped is ValueWrapper.Existing) lastDbIdWrapped.value else 0L
var startId = sp.getLong(R.string.key_ns_temporary_basal_last_synced_id, 0)
@ -658,7 +657,7 @@ class DataSyncSelectorImplementation @Inject constructor(
tb.first.interfaceIDs.nightscoutId == null ->
nsClientPlugin.nsClientService?.dbAdd(
"treatments",
tb.first.toJson(true, profile, dateUtil, useAbsolute),
tb.first.toJson(true, profile, dateUtil),
DataSyncSelector.PairTemporaryBasal(tb.first, tb.second.id),
"$startId/$lastDbId"
)
@ -667,7 +666,7 @@ class DataSyncSelectorImplementation @Inject constructor(
nsClientPlugin.nsClientService?.dbUpdate(
"treatments",
tb.first.interfaceIDs.nightscoutId,
tb.first.toJson(false, profile, dateUtil, useAbsolute),
tb.first.toJson(false, profile, dateUtil),
DataSyncSelector.PairTemporaryBasal(tb.first, tb.second.id),
"$startId/$lastDbId"
)
@ -700,7 +699,6 @@ class DataSyncSelectorImplementation @Inject constructor(
//private var lastEbId = -1L
//private var lastEbTime = -1L
override fun processChangedExtendedBolusesCompat(): Boolean {
val useAbsolute = sp.getBoolean(R.string.key_ns_sync_use_absolute, false)
val lastDbIdWrapped = appRepository.getLastExtendedBolusIdWrapped().blockingGet()
val lastDbId = if (lastDbIdWrapped is ValueWrapper.Existing) lastDbIdWrapped.value else 0L
var startId = sp.getLong(R.string.key_ns_extended_bolus_last_synced_id, 0)
@ -737,7 +735,7 @@ class DataSyncSelectorImplementation @Inject constructor(
eb.first.interfaceIDs.nightscoutId == null ->
nsClientPlugin.nsClientService?.dbAdd(
"treatments",
eb.first.toJson(true, profile, dateUtil, useAbsolute),
eb.first.toJson(true, profile, dateUtil),
DataSyncSelector.PairExtendedBolus(eb.first, eb.second.id),
"$startId/$lastDbId"
)
@ -746,7 +744,7 @@ class DataSyncSelectorImplementation @Inject constructor(
nsClientPlugin.nsClientService?.dbUpdate(
"treatments",
eb.first.interfaceIDs.nightscoutId,
eb.first.toJson(false, profile, dateUtil, useAbsolute),
eb.first.toJson(false, profile, dateUtil),
DataSyncSelector.PairExtendedBolus(eb.first, eb.second.id),
"$startId/$lastDbId"
)

View file

@ -152,7 +152,7 @@ class NSClientPlugin @Inject constructor(
preferenceFragment.findPreference<SwitchPreference>(rh.gs(R.string.key_ns_create_announcements_from_errors))?.isVisible = false
preferenceFragment.findPreference<SwitchPreference>(rh.gs(R.string.key_ns_create_announcements_from_carbs_req))?.isVisible = false
preferenceFragment.findPreference<SwitchPreference>(rh.gs(R.string.key_ns_sync_use_absolute))?.isVisible = false
// preferenceFragment.findPreference<SwitchPreference>(rh.gs(R.string.key_ns_sync_use_absolute))?.isVisible = false
} else {
// APS or pumpControl mode
preferenceFragment.findPreference<SwitchPreference>(rh.gs(R.string.key_ns_receive_profile_switch))?.isVisible = buildHelper.isEngineeringMode()

View file

@ -66,7 +66,6 @@
<string name="nav_resetdb">Reset Databases</string>
<string name="reset_db_confirm">Do you really want to reset the databases?</string>
<string name="nav_exit">Exit</string>
<string name="ns_sync_use_absolute_title">Always use basal absolute values</string>
<string name="alert_dialog_permission_battery_optimization_failed">This device does not appear to support battery optimization whitelisting - you may experience performance issues.</string>
<string name="description_actions">Some buttons to quickly access common features</string>

View file

@ -184,11 +184,6 @@
android:summary="@string/ns_localbroadcasts"
android:title="@string/ns_localbroadcasts_title" />
<SwitchPreference
android:defaultValue="false"
android:key="@string/key_ns_sync_use_absolute"
android:title="@string/ns_sync_use_absolute_title" />
<SwitchPreference
android:defaultValue="false"
android:key="@string/key_ns_sync_slow"

View file

@ -50,10 +50,10 @@ fun ExtendedBolus.toTemporaryBasal(profile: Profile): TemporaryBasal =
type = TemporaryBasal.Type.FAKE_EXTENDED
)
fun ExtendedBolus.toJson(isAdd: Boolean, profile: Profile, dateUtil: DateUtil, useAbsolute: Boolean): JSONObject =
fun ExtendedBolus.toJson(isAdd: Boolean, profile: Profile, dateUtil: DateUtil): JSONObject =
if (isEmulatingTempBasal)
toTemporaryBasal(profile)
.toJson(isAdd, profile, dateUtil, useAbsolute)
.toJson(isAdd, profile, dateUtil)
.put("extendedEmulated", toRealJson(isAdd, dateUtil))
else toRealJson(isAdd, dateUtil)

View file

@ -62,7 +62,7 @@ fun TemporaryBasal.toStringFull(profile: Profile, dateUtil: DateUtil): String {
}
}
fun TemporaryBasal.toJson(isAdd: Boolean, profile: Profile, dateUtil: DateUtil, useAbsolute: Boolean): JSONObject =
fun TemporaryBasal.toJson(isAdd: Boolean, profile: Profile, dateUtil: DateUtil): JSONObject =
JSONObject()
.put("created_at", dateUtil.toISOString(timestamp))
.put("enteredBy", "openaps://" + "AndroidAPS")
@ -72,9 +72,10 @@ fun TemporaryBasal.toJson(isAdd: Boolean, profile: Profile, dateUtil: DateUtil,
.put("durationInMilliseconds", duration) // rounded duration leads to different basal IOB
.put("rate", rate)
.put("type", type.name)
.put("isAbsolute", isAbsolute)
.put("absolute", convertedToAbsolute(timestamp, profile))
.put("percent", convertedToPercent(timestamp, profile) - 100)
.also {
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)
if (interfaceIDs.pumpType != null) it.put("pumpType", interfaceIDs.pumpType!!.name)
@ -97,6 +98,8 @@ fun temporaryBasalFromNsIdForInvalidating(nsId: String): TemporaryBasal =
fun temporaryBasalFromJson(jsonObject: JSONObject): TemporaryBasal? {
val timestamp = JsonHelper.safeGetLongAllowNull(jsonObject, "mills", null) ?: return null
var rate = JsonHelper.safeGetDoubleAllowNull(jsonObject, "rate")
var isAbsolute = JsonHelper.safeGetBooleanAllowNull(jsonObject, "isAbsolute")
val percent = JsonHelper.safeGetDoubleAllowNull(jsonObject, "percent")
val absolute = JsonHelper.safeGetDoubleAllowNull(jsonObject, "absolute")
val duration = JsonHelper.safeGetLongAllowNull(jsonObject, "duration") ?: return null
@ -109,7 +112,8 @@ fun temporaryBasalFromJson(jsonObject: JSONObject): TemporaryBasal? {
val pumpType = InterfaceIDs.PumpType.fromString(JsonHelper.safeGetStringAllowNull(jsonObject, "pumpType", null))
val pumpSerial = JsonHelper.safeGetStringAllowNull(jsonObject, "pumpSerial", null)
val rate = if (percent != null) percent + 100 else absolute ?: return null
rate = rate ?: if (percent != null) percent + 100 else absolute ?: return null
isAbsolute = isAbsolute ?: percent == null
if (duration == 0L) return null
if (timestamp == 0L) return null
@ -118,7 +122,7 @@ fun temporaryBasalFromJson(jsonObject: JSONObject): TemporaryBasal? {
rate = rate,
duration = durationInMilliseconds ?: T.mins(duration).msecs(),
type = type,
isAbsolute = percent == null,
isAbsolute = isAbsolute,
isValid = isValid
).also {
it.interfaceIDs.nightscoutId = id

View file

@ -150,4 +150,15 @@ object JsonHelper {
}
return result
}
fun safeGetBooleanAllowNull(json: JSONObject?, fieldName: String, defaultValue: Boolean? = null): Boolean? {
var result = defaultValue
if (json != null && json.has(fieldName)) {
try {
result = json.getBoolean(fieldName)
} catch (ignored: JSONException) {
}
}
return result
}
}

View file

@ -24,7 +24,6 @@
<string name="key_pump_unreachable_threshold_minutes" translatable="false">pump_unreachable_threshold</string>
<string name="key_missed_bg_readings_threshold_minutes" translatable="false">missed_bg_readings_threshold</string>
<string name="key_gradually_increase_notification_volume" translatable="false">gradually_increase_notification_volume</string>
<string name="key_ns_sync_use_absolute" translatable="false">ns_sync_use_absolute</string>
<string name="key_virtualpump_type" translatable="false">virtualpump_type</string>
<string name="key_quickwizard" translatable="false">QuickWizard</string>
<string name="key_wear_control" translatable="false">wearcontrol</string>