From fec3e71aef003be8a6a604428a6d473f0596a095 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Tue, 31 Oct 2023 11:32:43 +0100 Subject: [PATCH] NSC: fix sync if there is messed up date in phone --- .../sync/nsShared/NsIncomingDataProcessor.kt | 36 +++++++++++++------ .../sync/nsclientV3/workers/LoadBgWorker.kt | 2 +- .../workers/LoadTreatmentsWorker.kt | 4 +-- 3 files changed, 29 insertions(+), 13 deletions(-) diff --git a/plugins/sync/src/main/kotlin/app/aaps/plugins/sync/nsShared/NsIncomingDataProcessor.kt b/plugins/sync/src/main/kotlin/app/aaps/plugins/sync/nsShared/NsIncomingDataProcessor.kt index 4051ca563f..88dd660b88 100644 --- a/plugins/sync/src/main/kotlin/app/aaps/plugins/sync/nsShared/NsIncomingDataProcessor.kt +++ b/plugins/sync/src/main/kotlin/app/aaps/plugins/sync/nsShared/NsIncomingDataProcessor.kt @@ -81,10 +81,15 @@ class NsIncomingDataProcessor @Inject constructor( ) } + /** + * Preprocess list of SGVs + * + * @return true if there was an accepted SGV + */ @Suppress("SpellCheckingInspection") - fun processSgvs(sgvs: Any) { + fun processSgvs(sgvs: Any): Boolean { - if (!nsClientSource.isEnabled() && !sp.getBoolean(app.aaps.core.utils.R.string.key_ns_receive_cgm, false)) return + if (!nsClientSource.isEnabled() && !sp.getBoolean(app.aaps.core.utils.R.string.key_ns_receive_cgm, false)) return false var latestDateInReceivedData: Long = 0 aapsLogger.debug(LTag.NSCLIENT, "Received NS Data: $sgvs") @@ -104,16 +109,24 @@ class NsIncomingDataProcessor @Inject constructor( glucoseValues += sgv } } - activePlugin.activeNsClient?.updateLatestBgReceivedIfNewer(latestDateInReceivedData) - // Was that sgv more less 5 mins ago ? - if (T.msecs(dateUtil.now() - latestDateInReceivedData).mins() < 5L) { - rxBus.send(EventDismissNotification(Notification.NS_ALARM)) - rxBus.send(EventDismissNotification(Notification.NS_URGENT_ALARM)) + if (latestDateInReceivedData > 0) { + activePlugin.activeNsClient?.updateLatestBgReceivedIfNewer(latestDateInReceivedData) + // Was that sgv more less 5 mins ago ? + if (T.msecs(dateUtil.now() - latestDateInReceivedData).mins() < 5L) { + rxBus.send(EventDismissNotification(Notification.NS_ALARM)) + rxBus.send(EventDismissNotification(Notification.NS_URGENT_ALARM)) + } + storeDataForDb.glucoseValues.addAll(glucoseValues) } - storeDataForDb.glucoseValues.addAll(glucoseValues) + return latestDateInReceivedData > 0 } - fun processTreatments(treatments: List) { + /** + * Preprocess list of treatments + * + * @return true if there was an accepted treatment + */ + fun processTreatments(treatments: List): Boolean { try { var latestDateInReceivedData: Long = 0 for (treatment in treatments) { @@ -189,11 +202,14 @@ class NsIncomingDataProcessor @Inject constructor( } } } - activePlugin.activeNsClient?.updateLatestTreatmentReceivedIfNewer(latestDateInReceivedData) + if (latestDateInReceivedData > 0) + activePlugin.activeNsClient?.updateLatestTreatmentReceivedIfNewer(latestDateInReceivedData) + return latestDateInReceivedData > 0 } catch (error: Exception) { aapsLogger.error("Error: ", error) rxBus.send(EventNSClientNewLog("◄ ERROR", error.localizedMessage)) } + return false } fun processFood(data: Any) { diff --git a/plugins/sync/src/main/kotlin/app/aaps/plugins/sync/nsclientV3/workers/LoadBgWorker.kt b/plugins/sync/src/main/kotlin/app/aaps/plugins/sync/nsclientV3/workers/LoadBgWorker.kt index e2bbe80482..360fe14072 100644 --- a/plugins/sync/src/main/kotlin/app/aaps/plugins/sync/nsclientV3/workers/LoadBgWorker.kt +++ b/plugins/sync/src/main/kotlin/app/aaps/plugins/sync/nsclientV3/workers/LoadBgWorker.kt @@ -65,7 +65,7 @@ class LoadBgWorker( sp.putBoolean(app.aaps.core.utils.R.string.key_objectives_bg_is_available_in_ns, true) // Schedule processing of fetched data and continue of loading continueLoading = response.code != 304 - nsIncomingDataProcessor.processSgvs(sgvs) + continueLoading = nsIncomingDataProcessor.processSgvs(sgvs) } else { // End first load if (isFirstLoad) { diff --git a/plugins/sync/src/main/kotlin/app/aaps/plugins/sync/nsclientV3/workers/LoadTreatmentsWorker.kt b/plugins/sync/src/main/kotlin/app/aaps/plugins/sync/nsclientV3/workers/LoadTreatmentsWorker.kt index 0b6e7f7840..e32b3419e1 100644 --- a/plugins/sync/src/main/kotlin/app/aaps/plugins/sync/nsclientV3/workers/LoadTreatmentsWorker.kt +++ b/plugins/sync/src/main/kotlin/app/aaps/plugins/sync/nsclientV3/workers/LoadTreatmentsWorker.kt @@ -58,8 +58,8 @@ class LoadTreatmentsWorker( val action = if (isFirstLoad) "RCV-F" else "RCV" rxBus.send(EventNSClientNewLog("◄ $action", "${treatments.size} TRs from ${dateUtil.dateAndTimeAndSecondsString(lastLoaded)}")) // Schedule processing of fetched data and continue of loading - continueLoading = response.code != 304 - nsIncomingDataProcessor.processTreatments(response.values) + continueLoading = + response.code != 304 && nsIncomingDataProcessor.processTreatments(response.values) } else { // End first load if (isFirstLoad) {