diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/DataSyncSelectorImplementation.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/DataSyncSelectorImplementation.kt index 697192f485..debcb920a7 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/DataSyncSelectorImplementation.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/DataSyncSelectorImplementation.kt @@ -2,6 +2,7 @@ package info.nightscout.androidaps.plugins.general.nsclient import info.nightscout.androidaps.R import info.nightscout.androidaps.database.AppRepository +import info.nightscout.androidaps.database.ValueWrapper import info.nightscout.androidaps.database.entities.* import info.nightscout.androidaps.extensions.toJson import info.nightscout.androidaps.interfaces.ActivePlugin @@ -78,7 +79,13 @@ class DataSyncSelectorImplementation @Inject constructor( private var lastBolusId = -1L private var lastBolusTime = -1L override fun processChangedBolusesCompat(): Boolean { - val startId = sp.getLong(R.string.key_ns_bolus_last_synced_id, 0) + val lastDbIdWrapped = appRepository.getLastBolusIdWrapped().blockingGet() + val lastDbId = if (lastDbIdWrapped is ValueWrapper.Existing) lastDbIdWrapped.value else 0L + var startId = sp.getLong(R.string.key_ns_bolus_last_synced_id, 0) + if (startId > lastDbId) { + sp.putLong(R.string.key_ns_bolus_last_synced_id, 0) + startId = 0 + } if (startId == lastBolusId && dateUtil.now() - lastBolusTime < 5000) return false lastBolusId = startId lastBolusTime = dateUtil.now() @@ -87,10 +94,10 @@ class DataSyncSelectorImplementation @Inject constructor( when { // without nsId = create new bolus.first.interfaceIDs.nightscoutId == null -> - nsClientPlugin.nsClientService?.dbAdd("treatments", bolus.first.toJson(dateUtil), DataSyncSelector.PairBolus(bolus.first, bolus.second)) + nsClientPlugin.nsClientService?.dbAdd("treatments", bolus.first.toJson(dateUtil), DataSyncSelector.PairBolus(bolus.first, bolus.second), "$startId/$lastDbId") // with nsId = update bolus.first.interfaceIDs.nightscoutId != null -> - nsClientPlugin.nsClientService?.dbUpdate("treatments", bolus.first.interfaceIDs.nightscoutId, bolus.first.toJson(dateUtil), DataSyncSelector.PairBolus(bolus.first, bolus.second)) + nsClientPlugin.nsClientService?.dbUpdate("treatments", bolus.first.interfaceIDs.nightscoutId, bolus.first.toJson(dateUtil), DataSyncSelector.PairBolus(bolus.first, bolus.second), "$startId/$lastDbId") } return true } @@ -115,7 +122,13 @@ class DataSyncSelectorImplementation @Inject constructor( private var lastCarbsId = -1L private var lastCarbsTime = -1L override fun processChangedCarbsCompat(): Boolean { - val startId = sp.getLong(R.string.key_ns_carbs_last_synced_id, 0) + val lastDbIdWrapped = appRepository.getLastCarbsIdWrapped().blockingGet() + val lastDbId = if (lastDbIdWrapped is ValueWrapper.Existing) lastDbIdWrapped.value else 0L + var startId = sp.getLong(R.string.key_ns_carbs_last_synced_id, 0) + if (startId > lastDbId) { + sp.putLong(R.string.key_ns_carbs_last_synced_id, 0) + startId = 0 + } if (startId == lastCarbsId && dateUtil.now() - lastCarbsTime < 5000) return false lastCarbsId = startId lastCarbsTime = dateUtil.now() @@ -124,10 +137,10 @@ class DataSyncSelectorImplementation @Inject constructor( when { // without nsId = create new carb.first.interfaceIDs.nightscoutId == null -> - nsClientPlugin.nsClientService?.dbAdd("treatments", carb.first.toJson(dateUtil), DataSyncSelector.PairCarbs(carb.first, carb.second)) + nsClientPlugin.nsClientService?.dbAdd("treatments", carb.first.toJson(dateUtil), DataSyncSelector.PairCarbs(carb.first, carb.second), "$startId/$lastDbId") // with nsId = update carb.first.interfaceIDs.nightscoutId != null -> - nsClientPlugin.nsClientService?.dbUpdate("treatments", carb.first.interfaceIDs.nightscoutId, carb.first.toJson(dateUtil), DataSyncSelector.PairCarbs(carb.first, carb.second)) + nsClientPlugin.nsClientService?.dbUpdate("treatments", carb.first.interfaceIDs.nightscoutId, carb.first.toJson(dateUtil), DataSyncSelector.PairCarbs(carb.first, carb.second), "$startId/$lastDbId") } return true } @@ -152,7 +165,13 @@ class DataSyncSelectorImplementation @Inject constructor( private var lastBcrId = -1L private var lastBcrTime = -1L override fun processChangedBolusCalculatorResultsCompat(): Boolean { - val startId = sp.getLong(R.string.key_ns_bolus_calculator_result_last_synced_id, 0) + val lastDbIdWrapped = appRepository.getLastBolusCalculatorResultIdWrapped().blockingGet() + val lastDbId = if (lastDbIdWrapped is ValueWrapper.Existing) lastDbIdWrapped.value else 0L + var startId = sp.getLong(R.string.key_ns_bolus_calculator_result_last_synced_id, 0) + if (startId > lastDbId) { + sp.putLong(R.string.key_ns_bolus_calculator_result_last_synced_id, 0) + startId = 0 + } if (startId == lastBcrId && dateUtil.now() - lastBcrTime < 5000) return false lastBcrId = startId lastBcrTime = dateUtil.now() @@ -161,10 +180,10 @@ class DataSyncSelectorImplementation @Inject constructor( when { // without nsId = create new bolusCalculatorResult.first.interfaceIDs.nightscoutId == null -> - nsClientPlugin.nsClientService?.dbAdd("treatments", bolusCalculatorResult.first.toJson(dateUtil), DataSyncSelector.PairBolusCalculatorResult(bolusCalculatorResult.first, bolusCalculatorResult.second)) + nsClientPlugin.nsClientService?.dbAdd("treatments", bolusCalculatorResult.first.toJson(dateUtil), DataSyncSelector.PairBolusCalculatorResult(bolusCalculatorResult.first, bolusCalculatorResult.second), "$startId/$lastDbId") // with nsId = update bolusCalculatorResult.first.interfaceIDs.nightscoutId != null -> - nsClientPlugin.nsClientService?.dbUpdate("treatments", bolusCalculatorResult.first.interfaceIDs.nightscoutId, bolusCalculatorResult.first.toJson(dateUtil), DataSyncSelector.PairBolusCalculatorResult(bolusCalculatorResult.first, bolusCalculatorResult.second)) + nsClientPlugin.nsClientService?.dbUpdate("treatments", bolusCalculatorResult.first.interfaceIDs.nightscoutId, bolusCalculatorResult.first.toJson(dateUtil), DataSyncSelector.PairBolusCalculatorResult(bolusCalculatorResult.first, bolusCalculatorResult.second), "$startId/$lastDbId") } return true } @@ -189,7 +208,13 @@ class DataSyncSelectorImplementation @Inject constructor( private var lastTtId = -1L private var lastTtTime = -1L override fun processChangedTempTargetsCompat(): Boolean { - val startId = sp.getLong(R.string.key_ns_temporary_target_last_synced_id, 0) + val lastDbIdWrapped = appRepository.getLastTempTargetIdWrapped().blockingGet() + val lastDbId = if (lastDbIdWrapped is ValueWrapper.Existing) lastDbIdWrapped.value else 0L + var startId = sp.getLong(R.string.key_ns_temporary_target_last_synced_id, 0) + if (startId > lastDbId) { + sp.putLong(R.string.key_ns_temporary_target_last_synced_id, 0) + startId = 0 + } if (startId == lastTtId && dateUtil.now() - lastTtTime < 5000) return false lastTtId = startId lastTtTime = dateUtil.now() @@ -198,10 +223,10 @@ class DataSyncSelectorImplementation @Inject constructor( when { // without nsId = create new tt.first.interfaceIDs.nightscoutId == null -> - nsClientPlugin.nsClientService?.dbAdd("treatments", tt.first.toJson(profileFunction.getUnits(), dateUtil), DataSyncSelector.PairTemporaryTarget(tt.first, tt.second)) + nsClientPlugin.nsClientService?.dbAdd("treatments", tt.first.toJson(profileFunction.getUnits(), dateUtil), DataSyncSelector.PairTemporaryTarget(tt.first, tt.second), "$startId/$lastDbId") // existing with nsId = update tt.first.interfaceIDs.nightscoutId != null -> - nsClientPlugin.nsClientService?.dbUpdate("treatments", tt.first.interfaceIDs.nightscoutId, tt.first.toJson(profileFunction.getUnits(), dateUtil), DataSyncSelector.PairTemporaryTarget(tt.first, tt.second)) + nsClientPlugin.nsClientService?.dbUpdate("treatments", tt.first.interfaceIDs.nightscoutId, tt.first.toJson(profileFunction.getUnits(), dateUtil), DataSyncSelector.PairTemporaryTarget(tt.first, tt.second), "$startId/$lastDbId") } return true } @@ -226,7 +251,13 @@ class DataSyncSelectorImplementation @Inject constructor( private var lastFoodId = -1L private var lastFoodTime = -1L override fun processChangedFoodsCompat(): Boolean { - val startId = sp.getLong(R.string.key_ns_food_last_synced_id, 0) + val lastDbIdWrapped = appRepository.getLastFoodIdWrapped().blockingGet() + val lastDbId = if (lastDbIdWrapped is ValueWrapper.Existing) lastDbIdWrapped.value else 0L + var startId = sp.getLong(R.string.key_ns_food_last_synced_id, 0) + if (startId > lastDbId) { + sp.putLong(R.string.key_ns_food_last_synced_id, 0) + startId = 0 + } if (startId == lastFoodId && dateUtil.now() - lastFoodTime < 5000) return false lastFoodId = startId lastFoodTime = dateUtil.now() @@ -235,10 +266,10 @@ class DataSyncSelectorImplementation @Inject constructor( when { // without nsId = create new food.first.interfaceIDs.nightscoutId == null -> - nsClientPlugin.nsClientService?.dbAdd("food", food.first.toJson(), DataSyncSelector.PairFood(food.first, food.second)) + nsClientPlugin.nsClientService?.dbAdd("food", food.first.toJson(), DataSyncSelector.PairFood(food.first, food.second), "$startId/$lastDbId") // with nsId = update food.first.interfaceIDs.nightscoutId != null -> - nsClientPlugin.nsClientService?.dbUpdate("food", food.first.interfaceIDs.nightscoutId, food.first.toJson(), DataSyncSelector.PairFood(food.first, food.second)) + nsClientPlugin.nsClientService?.dbUpdate("food", food.first.interfaceIDs.nightscoutId, food.first.toJson(), DataSyncSelector.PairFood(food.first, food.second), "$startId/$lastDbId") } return true } @@ -263,7 +294,13 @@ class DataSyncSelectorImplementation @Inject constructor( private var lastGvId = -1L private var lastGvTime = -1L override fun processChangedGlucoseValuesCompat(): Boolean { - val startId = sp.getLong(R.string.key_ns_glucose_value_last_synced_id, 0) + val lastDbIdWrapped = appRepository.getLastGlucoseValueIdWrapped().blockingGet() + val lastDbId = if (lastDbIdWrapped is ValueWrapper.Existing) lastDbIdWrapped.value else 0L + var startId = sp.getLong(R.string.key_ns_glucose_value_last_synced_id, 0) + if (startId > lastDbId) { + sp.putLong(R.string.key_ns_glucose_value_last_synced_id, 0) + startId = 0 + } if (startId == lastGvId && dateUtil.now() - lastGvTime < 5000) return false lastGvId = startId lastGvTime = dateUtil.now() @@ -273,13 +310,17 @@ class DataSyncSelectorImplementation @Inject constructor( when { // without nsId = create new gv.first.interfaceIDs.nightscoutId == null -> - nsClientPlugin.nsClientService?.dbAdd("entries", gv.first.toJson(dateUtil), DataSyncSelector.PairGlucoseValue(gv.first, gv.second)) + nsClientPlugin.nsClientService?.dbAdd("entries", gv.first.toJson(dateUtil), DataSyncSelector.PairGlucoseValue(gv.first, gv.second), "$startId/$lastDbId") // with nsId = update gv.first.interfaceIDs.nightscoutId != null -> - nsClientPlugin.nsClientService?.dbUpdate("entries", gv.first.interfaceIDs.nightscoutId, gv.first.toJson(dateUtil), DataSyncSelector.PairGlucoseValue(gv.first, gv.second)) + nsClientPlugin.nsClientService?.dbUpdate("entries", gv.first.interfaceIDs.nightscoutId, gv.first.toJson(dateUtil), DataSyncSelector.PairGlucoseValue(gv.first, gv.second), "$startId/$lastDbId") } return true - } else confirmLastGlucoseValueIdIfGreater(gv.second) + } else { + confirmLastGlucoseValueIdIfGreater(gv.second) + lastGvId = -1 + processChangedGlucoseValuesCompat() + } } return false } @@ -302,7 +343,13 @@ class DataSyncSelectorImplementation @Inject constructor( private var lastTeId = -1L private var lastTeTime = -1L override fun processChangedTherapyEventsCompat(): Boolean { - val startId = sp.getLong(R.string.key_ns_therapy_event_last_synced_id, 0) + val lastDbIdWrapped = appRepository.getLastTherapyEventIdWrapped().blockingGet() + val lastDbId = if (lastDbIdWrapped is ValueWrapper.Existing) lastDbIdWrapped.value else 0L + var startId = sp.getLong(R.string.key_ns_therapy_event_last_synced_id, 0) + if (startId > lastDbId) { + sp.putLong(R.string.key_ns_therapy_event_last_synced_id, 0) + startId = 0 + } if (startId == lastTeId && dateUtil.now() - lastTeTime < 5000) return false lastTeId = startId lastTeTime = dateUtil.now() @@ -311,10 +358,10 @@ class DataSyncSelectorImplementation @Inject constructor( when { // without nsId = create new te.first.interfaceIDs.nightscoutId == null -> - nsClientPlugin.nsClientService?.dbAdd("treatments", te.first.toJson(), DataSyncSelector.PairTherapyEvent(te.first, te.second)) + nsClientPlugin.nsClientService?.dbAdd("treatments", te.first.toJson(), DataSyncSelector.PairTherapyEvent(te.first, te.second), "$startId/$lastDbId") // nsId = update te.first.interfaceIDs.nightscoutId != null -> - nsClientPlugin.nsClientService?.dbUpdate("treatments", te.first.interfaceIDs.nightscoutId, te.first.toJson(), DataSyncSelector.PairTherapyEvent(te.first, te.second)) + nsClientPlugin.nsClientService?.dbUpdate("treatments", te.first.interfaceIDs.nightscoutId, te.first.toJson(), DataSyncSelector.PairTherapyEvent(te.first, te.second), "$startId/$lastDbId") } return true } @@ -338,7 +385,13 @@ class DataSyncSelectorImplementation @Inject constructor( private var lastDsId = -1L private var lastDsTime = -1L override fun processChangedDeviceStatusesCompat(): Boolean { - val startId = sp.getLong(R.string.key_ns_device_status_last_synced_id, 0) + val lastDbIdWrapped = appRepository.getLastDeviceStatusIdWrapped().blockingGet() + val lastDbId = if (lastDbIdWrapped is ValueWrapper.Existing) lastDbIdWrapped.value else 0L + var startId = sp.getLong(R.string.key_ns_device_status_last_synced_id, 0) + if (startId > lastDbId) { + sp.putLong(R.string.key_ns_device_status_last_synced_id, 0) + startId = 0 + } if (startId == lastDsId && dateUtil.now() - lastDsTime < 5000) return false lastDsId = startId lastDsTime = dateUtil.now() @@ -347,7 +400,7 @@ class DataSyncSelectorImplementation @Inject constructor( when { // without nsId = create new deviceStatus.interfaceIDs.nightscoutId == null -> - nsClientPlugin.nsClientService?.dbAdd("devicestatus", deviceStatus.toJson(dateUtil), deviceStatus) + nsClientPlugin.nsClientService?.dbAdd("devicestatus", deviceStatus.toJson(dateUtil), deviceStatus, "$startId/$lastDbId") // with nsId = ignore deviceStatus.interfaceIDs.nightscoutId != null -> Any() } @@ -375,23 +428,34 @@ class DataSyncSelectorImplementation @Inject constructor( private var lastTbrTime = -1L override fun processChangedTemporaryBasalsCompat(): Boolean { val useAbsolute = sp.getBoolean(R.string.key_ns_sync_use_absolute, false) - val startId = sp.getLong(R.string.key_ns_temporary_basal_last_synced_id, 0) + 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) + if (startId > lastDbId) { + sp.putLong(R.string.key_ns_temporary_basal_last_synced_id, 0) + startId = 0 + } if (startId == lastTbrId && dateUtil.now() - lastTbrTime < 5000) return false lastTbrId = startId lastTbrTime = dateUtil.now() appRepository.getNextSyncElementTemporaryBasal(startId).blockingGet()?.let { tb -> aapsLogger.info(LTag.DATABASE, "Loading TemporaryBasal data Start: $startId ID: ${tb.first.id} HistoryID: ${tb.second} ") - profileFunction.getProfile(tb.first.timestamp)?.let { profile -> + val profile = profileFunction.getProfile(tb.first.timestamp) + if (profile != null) { when { // without nsId = create new tb.first.interfaceIDs.nightscoutId == null -> - nsClientPlugin.nsClientService?.dbAdd("treatments", tb.first.toJson(profile, dateUtil, useAbsolute), DataSyncSelector.PairTemporaryBasal(tb.first, tb.second)) + nsClientPlugin.nsClientService?.dbAdd("treatments", tb.first.toJson(profile, dateUtil, useAbsolute), DataSyncSelector.PairTemporaryBasal(tb.first, tb.second), "$startId/$lastDbId") // with nsId = update tb.first.interfaceIDs.nightscoutId != null -> - nsClientPlugin.nsClientService?.dbUpdate("treatments", tb.first.interfaceIDs.nightscoutId, tb.first.toJson(profile, dateUtil, useAbsolute), DataSyncSelector.PairTemporaryBasal(tb.first, tb.second)) + nsClientPlugin.nsClientService?.dbUpdate("treatments", tb.first.interfaceIDs.nightscoutId, tb.first.toJson(profile, dateUtil, useAbsolute), DataSyncSelector.PairTemporaryBasal(tb.first, tb.second), "$startId/$lastDbId") } return true - } ?: confirmLastTemporaryBasalIdIfGreater(tb.second) + } else { + confirmLastTemporaryBasalIdIfGreater(tb.second) + lastTbrId = -1 + processChangedTemporaryBasalsCompat() + } } return false } @@ -415,23 +479,34 @@ class DataSyncSelectorImplementation @Inject constructor( private var lastEbTime = -1L override fun processChangedExtendedBolusesCompat(): Boolean { val useAbsolute = sp.getBoolean(R.string.key_ns_sync_use_absolute, false) - val startId = sp.getLong(R.string.key_ns_extended_bolus_last_synced_id, 0) + 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) + if (startId > lastDbId) { + sp.putLong(R.string.key_ns_extended_bolus_last_synced_id, 0) + startId = 0 + } if (startId == lastEbId && dateUtil.now() - lastEbTime < 5000) return false lastEbId = startId lastEbTime = dateUtil.now() appRepository.getNextSyncElementExtendedBolus(startId).blockingGet()?.let { eb -> aapsLogger.info(LTag.DATABASE, "Loading ExtendedBolus data Start: $startId ID: ${eb.first.id} HistoryID: ${eb.second} ") - profileFunction.getProfile(eb.first.timestamp)?.let { profile -> + val profile = profileFunction.getProfile(eb.first.timestamp) + if (profile != null) { when { // without nsId = create new eb.first.interfaceIDs.nightscoutId == null -> - nsClientPlugin.nsClientService?.dbAdd("treatments", eb.first.toJson(profile, dateUtil, useAbsolute), DataSyncSelector.PairExtendedBolus(eb.first, eb.second)) + nsClientPlugin.nsClientService?.dbAdd("treatments", eb.first.toJson(profile, dateUtil, useAbsolute), DataSyncSelector.PairExtendedBolus(eb.first, eb.second), "$startId/$lastDbId") // with nsId = update eb.first.interfaceIDs.nightscoutId != null -> - nsClientPlugin.nsClientService?.dbUpdate("treatments", eb.first.interfaceIDs.nightscoutId, eb.first.toJson(profile, dateUtil, useAbsolute), DataSyncSelector.PairExtendedBolus(eb.first, eb.second)) + nsClientPlugin.nsClientService?.dbUpdate("treatments", eb.first.interfaceIDs.nightscoutId, eb.first.toJson(profile, dateUtil, useAbsolute), DataSyncSelector.PairExtendedBolus(eb.first, eb.second), "$startId/$lastDbId") } return true - } ?: confirmLastExtendedBolusIdIfGreater(eb.second) + } else { + confirmLastExtendedBolusIdIfGreater(eb.second) + lastEbId = -1 + processChangedExtendedBolusesCompat() + } } return false } @@ -453,7 +528,13 @@ class DataSyncSelectorImplementation @Inject constructor( private var lastPsId = -1L private var lastPsTime = -1L override fun processChangedProfileSwitchesCompat(): Boolean { - val startId = sp.getLong(R.string.key_ns_profile_switch_last_synced_id, 0) + val lastDbIdWrapped = appRepository.getLastProfileSwitchIdWrapped().blockingGet() + val lastDbId = if (lastDbIdWrapped is ValueWrapper.Existing) lastDbIdWrapped.value else 0L + var startId = sp.getLong(R.string.key_ns_profile_switch_last_synced_id, 0) + if (startId > lastDbId) { + sp.putLong(R.string.key_ns_profile_switch_last_synced_id, 0) + startId = 0 + } if (startId == lastPsId && dateUtil.now() - lastPsTime < 5000) return false lastPsId = startId lastPsTime = dateUtil.now() @@ -462,10 +543,10 @@ class DataSyncSelectorImplementation @Inject constructor( when { // without nsId = create new ps.first.interfaceIDs.nightscoutId == null -> - nsClientPlugin.nsClientService?.dbAdd("treatments", ps.first.toJson(dateUtil), DataSyncSelector.PairProfileSwitch(ps.first, ps.second)) + nsClientPlugin.nsClientService?.dbAdd("treatments", ps.first.toJson(dateUtil), DataSyncSelector.PairProfileSwitch(ps.first, ps.second), "$startId/$lastDbId") // with nsId = update ps.first.interfaceIDs.nightscoutId != null -> - nsClientPlugin.nsClientService?.dbUpdate("treatments", ps.first.interfaceIDs.nightscoutId, ps.first.toJson(dateUtil), DataSyncSelector.PairProfileSwitch(ps.first, ps.second)) + nsClientPlugin.nsClientService?.dbUpdate("treatments", ps.first.interfaceIDs.nightscoutId, ps.first.toJson(dateUtil), DataSyncSelector.PairProfileSwitch(ps.first, ps.second), "$startId/$lastDbId") } return true } @@ -483,6 +564,6 @@ class DataSyncSelectorImplementation @Inject constructor( localProfilePlugin.createProfileStore() val profileJson = localProfilePlugin.profile?.data ?: return if (lastChange > lastSync) - nsClientPlugin.nsClientService?.dbAdd("profile", profileJson, DataSyncSelector.PairProfileStore(profileJson, dateUtil.now())) + nsClientPlugin.nsClientService?.dbAdd("profile", profileJson, DataSyncSelector.PairProfileStore(profileJson, dateUtil.now()), "") } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/NSClientAddAckWorker.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/NSClientAddAckWorker.kt index 3404d2008f..cc3c6b6aea 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/NSClientAddAckWorker.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/NSClientAddAckWorker.kt @@ -206,7 +206,7 @@ class NSClientAddAckWorker( .blockingGet() rxBus.send(EventNSClientNewLog("DBADD", "Acked ExtendedBolus " + pair.value.interfaceIDs.nightscoutId)) // Send new if waiting - dataSyncSelector.processChangedTemporaryBasalsCompat() + dataSyncSelector.processChangedExtendedBolusesCompat() } is PairProfileSwitch -> { @@ -225,7 +225,7 @@ class NSClientAddAckWorker( .blockingGet() rxBus.send(EventNSClientNewLog("DBADD", "Acked ProfileSwitch " + pair.value.interfaceIDs.nightscoutId)) // Send new if waiting - dataSyncSelector.processChangedTemporaryBasalsCompat() + dataSyncSelector.processChangedProfileSwitchesCompat() } is DeviceStatus -> { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/services/NSClientService.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/services/NSClientService.kt index 9a12b70670..4a765e49e6 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/services/NSClientService.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/services/NSClientService.kt @@ -594,7 +594,7 @@ class NSClientService : DaggerService() { } } - fun dbUpdate(collection: String, _id: String?, data: JSONObject?, originalObject: Any) { + fun dbUpdate(collection: String, _id: String?, data: JSONObject?, originalObject: Any, progress: String) { try { if (_id == null) return if (!isConnected || !hasWriteAuth) return @@ -603,20 +603,20 @@ class NSClientService : DaggerService() { message.put("_id", _id) message.put("data", data) socket?.emit("dbUpdate", message, NSUpdateAck("dbUpdate", _id, aapsLogger, rxBus, originalObject)) - rxBus.send(EventNSClientNewLog("DBUPDATE $collection", "Sent " + originalObject.javaClass.simpleName + " " + _id)) + rxBus.send(EventNSClientNewLog("DBUPDATE $collection", "Sent " + originalObject.javaClass.simpleName + " " + _id + " " + progress)) } catch (e: JSONException) { aapsLogger.error("Unhandled exception", e) } } - fun dbAdd(collection: String, data: JSONObject, originalObject: Any) { + fun dbAdd(collection: String, data: JSONObject, originalObject: Any, progress: String) { try { if (!isConnected || !hasWriteAuth) return val message = JSONObject() message.put("collection", collection) message.put("data", data) socket?.emit("dbAdd", message, NSAddAck(aapsLogger, rxBus, originalObject)) - rxBus.send(EventNSClientNewLog("DBADD $collection", "Sent " + originalObject.javaClass.simpleName + " " + data)) + rxBus.send(EventNSClientNewLog("DBADD $collection", "Sent " + originalObject.javaClass.simpleName + " " + data + " " + progress)) } catch (e: JSONException) { aapsLogger.error("Unhandled exception", e) } diff --git a/database/src/main/java/info/nightscout/androidaps/database/AppRepository.kt b/database/src/main/java/info/nightscout/androidaps/database/AppRepository.kt index d2150de8bc..5529d23655 100644 --- a/database/src/main/java/info/nightscout/androidaps/database/AppRepository.kt +++ b/database/src/main/java/info/nightscout/androidaps/database/AppRepository.kt @@ -78,6 +78,11 @@ open class AppRepository @Inject internal constructor( database.glucoseValueDao.getModifiedFrom(lastId) .subscribeOn(Schedulers.io()) + fun getLastGlucoseValueIdWrapped(): Single> = + database.glucoseValueDao.getLastId() + .subscribeOn(Schedulers.io()) + .toWrappedSingle() + /* * returns a Pair of the next entity to sync and the ID of the "update". * The update id might either be the entry id itself if it is a new entry - or the id @@ -151,6 +156,11 @@ open class AppRepository @Inject internal constructor( fun deleteAllTempTargetEntries() = database.temporaryTargetDao.deleteAllEntries() + fun getLastTempTargetIdWrapped(): Single> = + database.temporaryTargetDao.getLastId() + .subscribeOn(Schedulers.io()) + .toWrappedSingle() + // USER ENTRY fun getAllUserEntries(): Single> = database.userEntryDao.getAll() @@ -217,6 +227,11 @@ open class AppRepository @Inject internal constructor( .map { if (!ascending) it.reversed() else it } .subscribeOn(Schedulers.io()) + fun getLastProfileSwitchIdWrapped(): Single> = + database.profileSwitchDao.getLastId() + .subscribeOn(Schedulers.io()) + .toWrappedSingle() + // EFFECTIVE PROFILE SWITCH /* * returns a Pair of the next entity to sync and the ID of the "update". @@ -267,6 +282,11 @@ open class AppRepository @Inject internal constructor( fun deleteAllEffectiveProfileSwitches() = database.effectiveProfileSwitchDao.deleteAllEntries() + fun getLastEffectiveProfileSwitchIdWrapped(): Single> = + database.effectiveProfileSwitchDao.getLastId() + .subscribeOn(Schedulers.io()) + .toWrappedSingle() + // THERAPY EVENT /* * returns a Pair of the next entity to sync and the ID of the "update". @@ -329,6 +349,11 @@ open class AppRepository @Inject internal constructor( database.therapyEventDao.compatGetTherapyEventDataFromToTime(from, to) .subscribeOn(Schedulers.io()) + fun getLastTherapyEventIdWrapped(): Single> = + database.therapyEventDao.getLastId() + .subscribeOn(Schedulers.io()) + .toWrappedSingle() + // FOOD /* * returns a Pair of the next entity to sync and the ID of the "update". @@ -360,6 +385,11 @@ open class AppRepository @Inject internal constructor( fun deleteAllFoods() = database.foodDao.deleteAllEntries() + fun getLastFoodIdWrapped(): Single> = + database.foodDao.getLastId() + .subscribeOn(Schedulers.io()) + .toWrappedSingle() + // BOLUS /* * returns a Pair of the next entity to sync and the ID of the "update". @@ -421,6 +451,10 @@ open class AppRepository @Inject internal constructor( fun deleteAllBoluses() = database.bolusDao.deleteAllEntries() + fun getLastBolusIdWrapped(): Single> = + database.bolusDao.getLastId() + .subscribeOn(Schedulers.io()) + .toWrappedSingle() // CARBS private fun expandCarbs(carbs: Carbs): List = @@ -529,6 +563,11 @@ open class AppRepository @Inject internal constructor( fun deleteAllCarbs() = database.carbsDao.deleteAllEntries() + fun getLastCarbsIdWrapped(): Single> = + database.carbsDao.getLastId() + .subscribeOn(Schedulers.io()) + .toWrappedSingle() + // BOLUS CALCULATOR RESULT /* * returns a Pair of the next entity to sync and the ID of the "update". @@ -566,6 +605,11 @@ open class AppRepository @Inject internal constructor( fun deleteAllBolusCalculatorResults() = database.bolusCalculatorResultDao.deleteAllEntries() + fun getLastBolusCalculatorResultIdWrapped(): Single> = + database.bolusCalculatorResultDao.getLastId() + .subscribeOn(Schedulers.io()) + .toWrappedSingle() + // DEVICE STATUS fun insert(deviceStatus: DeviceStatus): Long = database.deviceStatusDao.insert(deviceStatus) @@ -586,6 +630,11 @@ open class AppRepository @Inject internal constructor( database.deviceStatusDao.getModifiedFrom(lastId) .subscribeOn(Schedulers.io()) + fun getLastDeviceStatusIdWrapped(): Single> = + database.deviceStatusDao.getLastId() + .subscribeOn(Schedulers.io()) + .toWrappedSingle() + // TEMPORARY BASAL /* * returns a Pair of the next entity to sync and the ID of the "update". @@ -643,6 +692,11 @@ open class AppRepository @Inject internal constructor( fun getOldestTemporaryBasalRecord(): TemporaryBasal? = database.temporaryBasalDao.getOldestRecord() + fun getLastTemporaryBasalIdWrapped(): Single> = + database.temporaryBasalDao.getLastId() + .subscribeOn(Schedulers.io()) + .toWrappedSingle() + // EXTENDED BOLUS /* * returns a Pair of the next entity to sync and the ID of the "update". @@ -707,6 +761,10 @@ open class AppRepository @Inject internal constructor( .map { if (!ascending) it.reversed() else it } .subscribeOn(Schedulers.io()) + fun getLastExtendedBolusIdWrapped(): Single> = + database.extendedBolusDao.getLastId() + .subscribeOn(Schedulers.io()) + .toWrappedSingle() } @Suppress("USELESS_CAST") diff --git a/database/src/main/java/info/nightscout/androidaps/database/daos/BolusCalculatorResultDao.kt b/database/src/main/java/info/nightscout/androidaps/database/daos/BolusCalculatorResultDao.kt index a1f5788368..600a01de0b 100644 --- a/database/src/main/java/info/nightscout/androidaps/database/daos/BolusCalculatorResultDao.kt +++ b/database/src/main/java/info/nightscout/androidaps/database/daos/BolusCalculatorResultDao.kt @@ -18,6 +18,9 @@ internal interface BolusCalculatorResultDao : TraceableDao + @Query("SELECT * FROM $TABLE_BOLUS_CALCULATOR_RESULTS WHERE isValid = 1 AND timestamp >= :timestamp AND referenceId IS NULL ORDER BY id DESC") fun getBolusCalculatorResultsFromTime(timestamp: Long): Single> diff --git a/database/src/main/java/info/nightscout/androidaps/database/daos/BolusDao.kt b/database/src/main/java/info/nightscout/androidaps/database/daos/BolusDao.kt index 326e08e1dd..2483cb04cc 100644 --- a/database/src/main/java/info/nightscout/androidaps/database/daos/BolusDao.kt +++ b/database/src/main/java/info/nightscout/androidaps/database/daos/BolusDao.kt @@ -18,6 +18,9 @@ internal interface BolusDao : TraceableDao { @Query("DELETE FROM $TABLE_BOLUSES") override fun deleteAllEntries() + @Query("SELECT id FROM $TABLE_BOLUSES ORDER BY id DESC limit 1") + fun getLastId(): Maybe + @Query("SELECT * FROM $TABLE_BOLUSES WHERE timestamp = :timestamp AND referenceId IS NULL") fun findByTimestamp(timestamp: Long): Bolus? diff --git a/database/src/main/java/info/nightscout/androidaps/database/daos/CarbsDao.kt b/database/src/main/java/info/nightscout/androidaps/database/daos/CarbsDao.kt index 025eb04a02..fabed9ae77 100644 --- a/database/src/main/java/info/nightscout/androidaps/database/daos/CarbsDao.kt +++ b/database/src/main/java/info/nightscout/androidaps/database/daos/CarbsDao.kt @@ -17,6 +17,9 @@ internal interface CarbsDao : TraceableDao { @Query("DELETE FROM $TABLE_CARBS") override fun deleteAllEntries() + @Query("SELECT id FROM $TABLE_CARBS ORDER BY id DESC limit 1") + fun getLastId(): Maybe + @Query("SELECT * FROM $TABLE_CARBS WHERE nightscoutId = :nsId AND referenceId IS NULL") fun findByNSId(nsId: String): Carbs? diff --git a/database/src/main/java/info/nightscout/androidaps/database/daos/DeviceStatusDao.kt b/database/src/main/java/info/nightscout/androidaps/database/daos/DeviceStatusDao.kt index 239081d607..23fdc1a05c 100644 --- a/database/src/main/java/info/nightscout/androidaps/database/daos/DeviceStatusDao.kt +++ b/database/src/main/java/info/nightscout/androidaps/database/daos/DeviceStatusDao.kt @@ -25,6 +25,9 @@ internal interface DeviceStatusDao { @Query("DELETE FROM $TABLE_DEVICE_STATUS") fun deleteAllEntries() + @Query("SELECT id FROM $TABLE_DEVICE_STATUS ORDER BY id DESC limit 1") + fun getLastId(): Maybe + @Query("DELETE FROM $TABLE_DEVICE_STATUS WHERE id NOT IN (SELECT MAX(id) FROM $TABLE_DEVICE_STATUS)") fun deleteAllEntriesExceptLast() diff --git a/database/src/main/java/info/nightscout/androidaps/database/daos/EffectiveProfileSwitchDao.kt b/database/src/main/java/info/nightscout/androidaps/database/daos/EffectiveProfileSwitchDao.kt index c6944d05ad..592c21bd0b 100644 --- a/database/src/main/java/info/nightscout/androidaps/database/daos/EffectiveProfileSwitchDao.kt +++ b/database/src/main/java/info/nightscout/androidaps/database/daos/EffectiveProfileSwitchDao.kt @@ -17,6 +17,9 @@ internal interface EffectiveProfileSwitchDao : TraceableDao + @Query("SELECT * FROM $TABLE_EFFECTIVE_PROFILE_SWITCHES WHERE isValid = 1 AND referenceId IS NULL ORDER BY id ASC LIMIT 1") fun getOldestEffectiveProfileSwitchRecord(): EffectiveProfileSwitch? diff --git a/database/src/main/java/info/nightscout/androidaps/database/daos/ExtendedBolusDao.kt b/database/src/main/java/info/nightscout/androidaps/database/daos/ExtendedBolusDao.kt index 8f318577f8..657ee3b7ff 100644 --- a/database/src/main/java/info/nightscout/androidaps/database/daos/ExtendedBolusDao.kt +++ b/database/src/main/java/info/nightscout/androidaps/database/daos/ExtendedBolusDao.kt @@ -18,6 +18,9 @@ internal interface ExtendedBolusDao : TraceableDao { @Query("DELETE FROM $TABLE_EXTENDED_BOLUSES") override fun deleteAllEntries() + @Query("SELECT id FROM $TABLE_EXTENDED_BOLUSES ORDER BY id DESC limit 1") + fun getLastId(): Maybe + @Query("SELECT * FROM $TABLE_EXTENDED_BOLUSES WHERE timestamp = :timestamp AND referenceId IS NULL") fun findByTimestamp(timestamp: Long): ExtendedBolus? diff --git a/database/src/main/java/info/nightscout/androidaps/database/daos/FoodDao.kt b/database/src/main/java/info/nightscout/androidaps/database/daos/FoodDao.kt index b7fe24d3a0..bac97a3cb6 100644 --- a/database/src/main/java/info/nightscout/androidaps/database/daos/FoodDao.kt +++ b/database/src/main/java/info/nightscout/androidaps/database/daos/FoodDao.kt @@ -17,6 +17,9 @@ internal interface FoodDao : TraceableDao { @Query("DELETE FROM $TABLE_FOODS") override fun deleteAllEntries() + @Query("SELECT id FROM $TABLE_FOODS ORDER BY id DESC limit 1") + fun getLastId(): Maybe + @Query("SELECT * FROM $TABLE_FOODS WHERE nightscoutId = :nsId AND referenceId IS NULL") fun findByNSId(nsId: String): Food? diff --git a/database/src/main/java/info/nightscout/androidaps/database/daos/GlucoseValueDao.kt b/database/src/main/java/info/nightscout/androidaps/database/daos/GlucoseValueDao.kt index 0738101140..608eec3093 100644 --- a/database/src/main/java/info/nightscout/androidaps/database/daos/GlucoseValueDao.kt +++ b/database/src/main/java/info/nightscout/androidaps/database/daos/GlucoseValueDao.kt @@ -16,6 +16,9 @@ internal interface GlucoseValueDao : TraceableDao { @Query("DELETE FROM $TABLE_GLUCOSE_VALUES") override fun deleteAllEntries() + @Query("SELECT id FROM $TABLE_GLUCOSE_VALUES ORDER BY id DESC limit 1") + fun getLastId(): Maybe + @Query("SELECT * FROM $TABLE_GLUCOSE_VALUES WHERE nightscoutId = :nsId AND referenceId IS NULL") fun findByNSIdMaybe(nsId: String): Maybe diff --git a/database/src/main/java/info/nightscout/androidaps/database/daos/ProfileSwitchDao.kt b/database/src/main/java/info/nightscout/androidaps/database/daos/ProfileSwitchDao.kt index c992e592be..598679864b 100644 --- a/database/src/main/java/info/nightscout/androidaps/database/daos/ProfileSwitchDao.kt +++ b/database/src/main/java/info/nightscout/androidaps/database/daos/ProfileSwitchDao.kt @@ -19,6 +19,9 @@ internal interface ProfileSwitchDao : ProfileSwitchDaoWorkaround { @Query("DELETE FROM $TABLE_PROFILE_SWITCHES") override fun deleteAllEntries() + @Query("SELECT id FROM $TABLE_PROFILE_SWITCHES ORDER BY id DESC limit 1") + fun getLastId(): Maybe + @Query("SELECT * FROM $TABLE_PROFILE_SWITCHES WHERE timestamp = :timestamp AND referenceId IS NULL") fun findByTimestamp(timestamp: Long): ProfileSwitch? diff --git a/database/src/main/java/info/nightscout/androidaps/database/daos/TemporaryBasalDao.kt b/database/src/main/java/info/nightscout/androidaps/database/daos/TemporaryBasalDao.kt index a6c46075c5..f2cdd2617e 100644 --- a/database/src/main/java/info/nightscout/androidaps/database/daos/TemporaryBasalDao.kt +++ b/database/src/main/java/info/nightscout/androidaps/database/daos/TemporaryBasalDao.kt @@ -18,6 +18,9 @@ internal interface TemporaryBasalDao : TraceableDao { @Query("DELETE FROM $TABLE_TEMPORARY_BASALS") override fun deleteAllEntries() + @Query("SELECT id FROM $TABLE_TEMPORARY_BASALS ORDER BY id DESC limit 1") + fun getLastId(): Maybe + @Query("SELECT * FROM $TABLE_TEMPORARY_BASALS WHERE temporaryId = :temporaryId") fun findByTempId(temporaryId: Long): TemporaryBasal? diff --git a/database/src/main/java/info/nightscout/androidaps/database/daos/TemporaryTargetDao.kt b/database/src/main/java/info/nightscout/androidaps/database/daos/TemporaryTargetDao.kt index 7a5eaee720..065eb76595 100644 --- a/database/src/main/java/info/nightscout/androidaps/database/daos/TemporaryTargetDao.kt +++ b/database/src/main/java/info/nightscout/androidaps/database/daos/TemporaryTargetDao.kt @@ -17,6 +17,9 @@ internal interface TemporaryTargetDao : TraceableDao { @Query("DELETE FROM $TABLE_TEMPORARY_TARGETS") override fun deleteAllEntries() + @Query("SELECT id FROM $TABLE_TEMPORARY_TARGETS ORDER BY id DESC limit 1") + fun getLastId(): Maybe + @Query("SELECT * FROM $TABLE_TEMPORARY_TARGETS WHERE nightscoutId = :nsId AND referenceId IS NULL") fun findByNSId(nsId: String): TemporaryTarget? diff --git a/database/src/main/java/info/nightscout/androidaps/database/daos/TherapyEventDao.kt b/database/src/main/java/info/nightscout/androidaps/database/daos/TherapyEventDao.kt index fcec0b020d..dea04b81b2 100644 --- a/database/src/main/java/info/nightscout/androidaps/database/daos/TherapyEventDao.kt +++ b/database/src/main/java/info/nightscout/androidaps/database/daos/TherapyEventDao.kt @@ -16,6 +16,9 @@ internal interface TherapyEventDao : TraceableDao { @Query("DELETE FROM $TABLE_THERAPY_EVENTS") override fun deleteAllEntries() + @Query("SELECT id FROM $TABLE_THERAPY_EVENTS ORDER BY id DESC limit 1") + fun getLastId(): Maybe + @Query("SELECT * FROM $TABLE_THERAPY_EVENTS WHERE type = :type AND timestamp = :timestamp AND referenceId IS NULL") fun findByTimestamp(type: TherapyEvent.Type, timestamp: Long): TherapyEvent?