NSC: improve profile sync

This commit is contained in:
Milos Kozak 2023-09-01 10:56:30 +02:00
parent a652f51651
commit 4c4f1ec990
10 changed files with 92 additions and 17 deletions

View file

@ -5,6 +5,9 @@ import org.json.JSONArray
interface ProfileSource {
/**
* SingleProfile stores a name of a profile in addition to PureProfile
*/
class SingleProfile(
var name: String,
var mgdl: Boolean,
@ -31,11 +34,41 @@ interface ProfileSource {
val profile: ProfileStore?
val profileName: String?
fun addProfile(p: SingleProfile)
/**
* Convert [PureProfile] to [SingleProfile]
*
* @param pureProfile PureProfile
* @param newName Name
* @return SingleProfile
*/
fun copyFrom(pureProfile: PureProfile, newName: String): SingleProfile
/**
* Currently edited profile in store as index
*/
var currentProfileIndex: Int
/**
* Get currently edited profile from store as [SingleProfile]
*
* @return currently selected profile
*/
fun currentProfile(): SingleProfile?
fun storeSettings(activity: FragmentActivity? = null, emptyCreated: Boolean = false)
/**
* Store active [ProfileStore] to SharedPreferences
*
* @param activity context for error dialog
* @param timestamp timestamp of latest change
*/
fun storeSettings(activity: FragmentActivity? = null, timestamp: Long)
/**
* Import [ProfileStore] to memory and and save to SharedPreferences
*
* @param store ProfileStore to import
*/
fun loadFromStore(store: ProfileStore)
}

View file

@ -44,4 +44,13 @@ interface DataSyncSelector {
fun resetToNextFullSync()
suspend fun doUpload()
/**
* This function called when new profile is received from NS
* Plugin should update internal timestamp to not send Profile back as a new/updated
*
* @param timestamp received timestamp of profile
*
*/
fun profileReceived(timestamp: Long)
}

View file

@ -370,7 +370,7 @@ class AutotunePlugin @Inject constructor(
profilePlugin.currentProfile()?.basal = newProfile.basal()
profilePlugin.currentProfile()?.ic = newProfile.ic(circadian)
profilePlugin.currentProfile()?.isf = newProfile.isf(circadian)
profilePlugin.storeSettings()
profilePlugin.storeSettings(timestamp = dateUtil.now())
}
fun saveLastRun() {

View file

@ -335,7 +335,7 @@ class ProfileFragment : DaggerFragment() {
?: ""
)
)
profilePlugin.storeSettings(activity)
profilePlugin.storeSettings(activity, dateUtil.now())
build()
}
updateGUI()

View file

@ -168,7 +168,7 @@ class ProfilePlugin @Inject constructor(
}
@Synchronized
override fun storeSettings(activity: FragmentActivity?, emptyCreated: Boolean) {
override fun storeSettings(activity: FragmentActivity?, timestamp: Long) {
for (i in 0 until numOfProfiles) {
profiles[i].run {
val localProfileNumbered = Constants.LOCAL_PROFILE + "_" + i + "_"
@ -184,7 +184,7 @@ class ProfilePlugin @Inject constructor(
}
sp.putInt(Constants.LOCAL_PROFILE + "_profiles", numOfProfiles)
sp.putLong(info.nightscout.core.utils.R.string.key_local_profile_last_change, if (emptyCreated) 0 else dateUtil.now())
sp.putLong(info.nightscout.core.utils.R.string.key_local_profile_last_change, timestamp)
createAndStoreConvertedProfile()
isEdited = false
aapsLogger.debug(LTag.PROFILE, "Storing settings: " + rawProfile?.data.toString())
@ -249,13 +249,12 @@ class ProfilePlugin @Inject constructor(
)
}
}
if (newProfiles.size > 0) {
if (newProfiles.isNotEmpty()) {
profiles = newProfiles
currentProfileIndex = 0
isEdited = false
createAndStoreConvertedProfile()
aapsLogger.debug(LTag.PROFILE, "Accepted ${profiles.size} profiles")
storeSettings()
storeSettings(timestamp = store.getStartDate())
rxBus.send(EventLocalProfileChanged())
} else
aapsLogger.debug(LTag.PROFILE, "ProfileStore not accepted")
@ -354,7 +353,7 @@ class ProfilePlugin @Inject constructor(
)
currentProfileIndex = profiles.size - 1
createAndStoreConvertedProfile()
storeSettings(emptyCreated = true)
storeSettings(timestamp = 0)
}
fun cloneProfile() {
@ -363,7 +362,7 @@ class ProfilePlugin @Inject constructor(
profiles.add(p)
currentProfileIndex = profiles.size - 1
createAndStoreConvertedProfile()
storeSettings()
storeSettings(timestamp = dateUtil.now())
isEdited = false
}
@ -371,16 +370,16 @@ class ProfilePlugin @Inject constructor(
profiles.add(p)
currentProfileIndex = profiles.size - 1
createAndStoreConvertedProfile()
storeSettings()
storeSettings(timestamp = dateUtil.now())
isEdited = false
}
fun removeCurrentProfile() {
profiles.removeAt(currentProfileIndex)
if (profiles.size == 0) addNewProfile()
if (profiles.isEmpty()) addNewProfile()
currentProfileIndex = 0
createAndStoreConvertedProfile()
storeSettings()
storeSettings(timestamp = dateUtil.now())
isEdited = false
}

View file

@ -16,7 +16,18 @@ import info.nightscout.interfaces.profile.ProfileSource
import info.nightscout.interfaces.source.NSClientSource
import info.nightscout.interfaces.utils.JsonHelper
import info.nightscout.plugins.sync.R
import info.nightscout.plugins.sync.nsclientV3.extensions.*
import info.nightscout.plugins.sync.nsclientV3.extensions.toBolus
import info.nightscout.plugins.sync.nsclientV3.extensions.toBolusCalculatorResult
import info.nightscout.plugins.sync.nsclientV3.extensions.toCarbs
import info.nightscout.plugins.sync.nsclientV3.extensions.toEffectiveProfileSwitch
import info.nightscout.plugins.sync.nsclientV3.extensions.toExtendedBolus
import info.nightscout.plugins.sync.nsclientV3.extensions.toFood
import info.nightscout.plugins.sync.nsclientV3.extensions.toOfflineEvent
import info.nightscout.plugins.sync.nsclientV3.extensions.toProfileSwitch
import info.nightscout.plugins.sync.nsclientV3.extensions.toTemporaryBasal
import info.nightscout.plugins.sync.nsclientV3.extensions.toTemporaryTarget
import info.nightscout.plugins.sync.nsclientV3.extensions.toTherapyEvent
import info.nightscout.plugins.sync.nsclientV3.extensions.toTransactionGlucoseValue
import info.nightscout.rx.bus.RxBus
import info.nightscout.rx.events.EventDismissNotification
import info.nightscout.rx.events.EventNSClientNewLog
@ -24,7 +35,17 @@ import info.nightscout.rx.logging.AAPSLogger
import info.nightscout.rx.logging.LTag
import info.nightscout.sdk.localmodel.entry.NSSgvV3
import info.nightscout.sdk.localmodel.food.NSFood
import info.nightscout.sdk.localmodel.treatment.*
import info.nightscout.sdk.localmodel.treatment.NSBolus
import info.nightscout.sdk.localmodel.treatment.NSBolusWizard
import info.nightscout.sdk.localmodel.treatment.NSCarbs
import info.nightscout.sdk.localmodel.treatment.NSEffectiveProfileSwitch
import info.nightscout.sdk.localmodel.treatment.NSExtendedBolus
import info.nightscout.sdk.localmodel.treatment.NSOfflineEvent
import info.nightscout.sdk.localmodel.treatment.NSProfileSwitch
import info.nightscout.sdk.localmodel.treatment.NSTemporaryBasal
import info.nightscout.sdk.localmodel.treatment.NSTemporaryTarget
import info.nightscout.sdk.localmodel.treatment.NSTherapyEvent
import info.nightscout.sdk.localmodel.treatment.NSTreatment
import info.nightscout.shared.sharedPreferences.SP
import info.nightscout.shared.utils.DateUtil
import info.nightscout.shared.utils.T
@ -223,8 +244,9 @@ class NsIncomingDataProcessor @Inject constructor(
val lastLocalChange = sp.getLong(info.nightscout.core.utils.R.string.key_local_profile_last_change, 0)
aapsLogger.debug(LTag.PROFILE, "Received profileStore: createdAt: $createdAt Local last modification: $lastLocalChange")
@Suppress("LiftReturnOrAssignment")
if (createdAt > lastLocalChange || createdAt % 1000 == 0L) {// whole second means edited in NS
if (createdAt > lastLocalChange || createdAt % 1000 == 0L) { // whole second means edited in NS
profileSource.loadFromStore(store)
activePlugin.activeNsClient?.dataSyncSelector?.profileReceived(store.getStartDate())
aapsLogger.debug(LTag.PROFILE, "Received profileStore: $profileJson")
}
}

View file

@ -775,6 +775,10 @@ class DataSyncSelectorV1 @Inject constructor(
sp.putLong(R.string.key_ns_profile_store_last_synced_timestamp, lastSynced)
}
override fun profileReceived(timestamp: Long) {
sp.putLong(R.string.key_ns_profile_store_last_synced_timestamp, timestamp)
}
private suspend fun processChangedProfileStore() {
if (isPaused) return
val lastSync = sp.getLong(R.string.key_ns_profile_store_last_synced_timestamp, 0)

View file

@ -668,6 +668,10 @@ class DataSyncSelectorV3 @Inject constructor(
sp.putLong(R.string.key_ns_profile_store_last_synced_timestamp, lastSynced)
}
override fun profileReceived(timestamp: Long) {
sp.putLong(R.string.key_ns_profile_store_last_synced_timestamp, timestamp)
}
private suspend fun processChangedProfileStore() {
if (isPaused) return
val lastSync = sp.getLong(R.string.key_ns_profile_store_last_synced_timestamp, 0)

View file

@ -531,6 +531,10 @@ class DataSyncSelectorXdripImpl @Inject constructor(
sp.putLong(R.string.key_xdrip_profile_store_last_synced_timestamp, lastSynced)
}
override fun profileReceived(timestamp: Long) {
sp.putLong(R.string.key_xdrip_profile_store_last_synced_timestamp, timestamp)
}
private fun processChangedProfileStore() {
if (!isEnabled) return
val lastSync = sp.getLong(R.string.key_xdrip_profile_store_last_synced_timestamp, 0)

View file

@ -24,7 +24,7 @@ import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
@SuppressWarnings({"unused", "rawtypes", "SuspiciousMethodCalls", "unchecked"})
@SuppressWarnings({"unused", "rawtypes", "SuspiciousMethodCalls", "unchecked", "deprecation"})
public final class BundleMock {
public static Bundle mock() {