improve NS profile sync

This commit is contained in:
Milos Kozak 2021-05-25 20:59:41 +02:00
parent 5ef92462a7
commit 3ed45fdd11
12 changed files with 39 additions and 23 deletions

View file

@ -561,9 +561,9 @@ class DataSyncSelectorImplementation @Inject constructor(
val lastSync = sp.getLong(R.string.key_ns_profile_store_last_synced_timestamp, 0)
val lastChange = sp.getLong(R.string.key_local_profile_last_change, 0)
if (lastChange == 0L) return
localProfilePlugin.createProfileStore()
val profileJson = localProfilePlugin.profile?.data ?: return
if (lastChange > lastSync)
if (lastChange > lastSync) {
val profileJson = localProfilePlugin.profile?.data ?: return
nsClientPlugin.nsClientService?.dbAdd("profile", profileJson, DataSyncSelector.PairProfileStore(profileJson, dateUtil.now()), "")
}
}
}

View file

@ -34,7 +34,7 @@ class NSClientMbgWorker(
var ret = Result.success()
val acceptNSData = sp.getBoolean(R.string.key_ns_receive_therapy_events, false) || config.NSCLIENT
if (!acceptNSData) return ret
if (!acceptNSData) return Result.success(workDataOf("Result" to "Sync not enabled"))
val mbgArray = dataWorker.pickupJSONArray(inputData.getLong(DataWorker.STORE_KEY, -1))
?: return Result.failure(workDataOf("Error" to "missing input data"))

View file

@ -70,7 +70,7 @@ class LocalProfileFragment : DaggerFragment() {
}
private fun sumLabel(): String {
val profile = localProfilePlugin.createProfileStore().getDefaultProfile()
val profile = localProfilePlugin.profile?.getDefaultProfile()
val sum = profile?.let { ProfileSealed.Pure(profile).baseBasalSum() } ?: 0.0
return "" + DecimalFormatter.to2Decimal(sum) + resourceHelper.gs(R.string.insulin_unit_shortname)
}

View file

@ -139,6 +139,7 @@ class LocalProfilePlugin @Inject constructor(
}
sp.putInt(Constants.LOCAL_PROFILE + "_profiles", numOfProfiles)
sp.putLong(R.string.key_local_profile_last_change, dateUtil.now())
createAndStoreConvertedProfile()
isEdited = false
aapsLogger.debug(LTag.PROFILE, "Storing settings: " + rawProfile?.data.toString())
@ -148,12 +149,9 @@ class LocalProfilePlugin @Inject constructor(
val name = it.name ?: "."
if (name.contains(".")) namesOK = false
}
if (namesOK)
sp.putLong(R.string.key_local_profile_last_change, dateUtil.now())
else
activity?.let {
OKDialog.show(it, "", resourceHelper.gs(R.string.profilenamecontainsdot))
}
if (!namesOK) activity?.let {
OKDialog.show(it, "", resourceHelper.gs(R.string.profilenamecontainsdot))
}
}
@Synchronized
@ -372,7 +370,8 @@ class LocalProfilePlugin @Inject constructor(
}
}
if (numOfProfiles > 0) json.put("defaultProfile", currentProfile()?.name)
json.put("startDate", dateUtil.toISOAsUTC(dateUtil.now()))
val startDate = sp.getLong(R.string.key_local_profile_last_change, dateUtil.now())
json.put("startDate", dateUtil.toISOAsUTC(startDate))
json.put("store", store)
} catch (e: JSONException) {
aapsLogger.error("Unhandled exception", e)
@ -412,11 +411,19 @@ class LocalProfilePlugin @Inject constructor(
val profileJson = dataWorker.pickupJSONObject(inputData.getLong(DataWorker.STORE_KEY, -1))
?: return Result.failure(workDataOf("Error" to "missing input data"))
if (sp.getBoolean(R.string.key_ns_receive_profile_store, false) || config.NSCLIENT) {
localProfilePlugin.loadFromStore(ProfileStore(injector, profileJson, dateUtil))
aapsLogger.debug(LTag.PROFILE, "Received profileStore: $profileJson")
return Result.success(workDataOf("Data" to profileJson.toString().substring(0..min(5000, profileJson.length()))))
val store = ProfileStore(injector, profileJson, dateUtil)
val startDate = store.getStartDate()
val lastLocalChange = sp.getLong(R.string.key_local_profile_last_change, 0)
aapsLogger.debug(LTag.PROFILE, "Received profileStore: StartDate: $startDate Local last modification: $lastLocalChange")
@Suppress("LiftReturnOrAssignment")
if (startDate > lastLocalChange || startDate % 1000 == 0L) {// whole second means edited in NS
localProfilePlugin.loadFromStore(store)
aapsLogger.debug(LTag.PROFILE, "Received profileStore: $profileJson")
return Result.success(workDataOf("Data" to profileJson.toString().substring(0..min(5000, profileJson.length()))))
} else
return Result.success(workDataOf("Result" to "Unchanged. Ignoring"))
}
return Result.success()
return Result.success(workDataOf("Result" to "Sync not enabled"))
}
}

View file

@ -94,7 +94,7 @@ class DexcomPlugin @Inject constructor(
override fun doWork(): Result {
var ret = Result.success()
if (!dexcomPlugin.isEnabled(PluginType.BGSOURCE)) return Result.success()
if (!dexcomPlugin.isEnabled(PluginType.BGSOURCE)) return Result.success(workDataOf("Result" to "Plugin not enabled"))
val bundle = dataWorker.pickupBundle(inputData.getLong(DataWorker.STORE_KEY, -1))
?: return Result.failure(workDataOf("Error" to "missing input data"))
try {

View file

@ -69,7 +69,7 @@ class EversensePlugin @Inject constructor(
override fun doWork(): Result {
var ret = Result.success()
if (!eversensePlugin.isEnabled(PluginType.BGSOURCE)) return Result.success()
if (!eversensePlugin.isEnabled(PluginType.BGSOURCE)) return Result.success(workDataOf("Result" to "Plugin not enabled"))
val bundle = dataWorker.pickupBundle(inputData.getLong(DataWorker.STORE_KEY, -1))
?: return Result.failure(workDataOf("Error" to "missing input data"))
if (bundle.containsKey("currentCalibrationPhase")) aapsLogger.debug(LTag.BGSOURCE, "currentCalibrationPhase: " + bundle.getString("currentCalibrationPhase"))

View file

@ -56,7 +56,7 @@ class GlimpPlugin @Inject constructor(
override fun doWork(): Result {
var ret = Result.success()
if (!glimpPlugin.isEnabled(PluginType.BGSOURCE)) return Result.success()
if (!glimpPlugin.isEnabled(PluginType.BGSOURCE)) return Result.success(workDataOf("Result" to "Plugin not enabled"))
aapsLogger.debug(LTag.BGSOURCE, "Received Glimp Data: $inputData}")
val glucoseValues = mutableListOf<CgmSourceTransaction.TransactionGlucoseValue>()
glucoseValues += CgmSourceTransaction.TransactionGlucoseValue(

View file

@ -115,7 +115,7 @@ class NSClientSourcePlugin @Inject constructor(
override fun doWork(): Result {
var ret = Result.success()
if (!nsClientSourcePlugin.isEnabled() && !sp.getBoolean(R.string.key_ns_receive_cgm, true) && !dexcomPlugin.isEnabled()) return Result.success()
if (!nsClientSourcePlugin.isEnabled() && !sp.getBoolean(R.string.key_ns_receive_cgm, true) && !dexcomPlugin.isEnabled()) return Result.success(workDataOf("Result" to "Sync not enabled"))
val sgvs = dataWorker.pickupJSONArray(inputData.getLong(DataWorker.STORE_KEY, -1))
?: return Result.failure(workDataOf("Error" to "missing input data"))

View file

@ -60,7 +60,7 @@ class PoctechPlugin @Inject constructor(
override fun doWork(): Result {
var ret = Result.success()
if (!poctechPlugin.isEnabled(PluginType.BGSOURCE)) return Result.success()
if (!poctechPlugin.isEnabled(PluginType.BGSOURCE)) return Result.success(workDataOf("Result" to "Plugin not enabled"))
aapsLogger.debug(LTag.BGSOURCE, "Received Poctech Data $inputData")
try {
val glucoseValues = mutableListOf<CgmSourceTransaction.TransactionGlucoseValue>()

View file

@ -59,7 +59,7 @@ class TomatoPlugin @Inject constructor(
override fun doWork(): Result {
var ret = Result.success()
if (!tomatoPlugin.isEnabled(PluginType.BGSOURCE)) return Result.success()
if (!tomatoPlugin.isEnabled(PluginType.BGSOURCE)) return Result.success(workDataOf("Result" to "Plugin not enabled"))
val glucoseValues = mutableListOf<CgmSourceTransaction.TransactionGlucoseValue>()
glucoseValues += CgmSourceTransaction.TransactionGlucoseValue(
timestamp = inputData.getLong("com.fanqies.tomatofn.Extras.Time", 0),

View file

@ -73,7 +73,7 @@ class XdripPlugin @Inject constructor(
override fun doWork(): Result {
var ret = Result.success()
if (!xdripPlugin.isEnabled(PluginType.BGSOURCE)) return Result.success()
if (!xdripPlugin.isEnabled(PluginType.BGSOURCE)) return Result.success(workDataOf("Result" to "Plugin not enabled"))
val bundle = dataWorker.pickupBundle(inputData.getLong(DataWorker.STORE_KEY, -1))
?: return Result.failure(workDataOf("Error" to "missing input data"))

View file

@ -31,6 +31,15 @@ class ProfileStore(val injector: HasAndroidInjector, val data: JSONObject, val d
return null
}
fun getStartDate(): Long {
val iso = JsonHelper.safeGetString(data, "startDate") ?: return 0
return try {
dateUtil.fromISODateString(iso)
} catch (e: Exception) {
0
}
}
fun getDefaultProfile(): PureProfile? = getDefaultProfileName()?.let { getSpecificProfile(it) }
fun getDefaultProfileJson(): JSONObject? = getDefaultProfileName()?.let { getSpecificProfileJson(it) }