Преглед изворни кода

Merge branch 'core-data-sync-trio' of github.com:nightscout/Trio-dev into watch

Deniz Cengiz пре 1 година
родитељ
комит
2a90ddb70f
1 измењених фајлова са 46 додато и 6 уклоњено
  1. 46 6
      Trio/Sources/Services/Network/Nightscout/NightscoutManager.swift

+ 46 - 6
Trio/Sources/Services/Network/Nightscout/NightscoutManager.swift

@@ -133,11 +133,22 @@ final class BaseNightscoutManager: NightscoutManager, Injectable {
 
         registerHandlers()
         setupNotification()
+
+        /// Ensure that Nightscout Manager holds the `lastEnactedDetermination`, if one exists, on initialization.
+        /// We have to set this here in `init()`, so there's a `lastEnactedDetermination` available after an app restart
+        /// for `uploadStatus()`, as within that fuction `lastEnactedDetermination` is reassigned at the very end of the function.
+        /// This way, we ensure the latest enacted determination is always part of `devicestatus` and avoid having instances
+        /// where the first uploaded non-enacted determination (i.e., "suggested"), lacks the "enacted" data.
+        Task {
+            async let lastEnactedDeterminationID = determinationStorage
+                .fetchLastDeterminationObjectID(predicate: NSPredicate.enactedDetermination)
+
+            self.lastEnactedDetermination = await determinationStorage
+                .getOrefDeterminationNotYetUploadedToNightscout(lastEnactedDeterminationID)
+        }
     }
 
     private func subscribe() {
-//        broadcaster.register(TempTargetsObserver.self, observer: self)
-
         _ = reachabilityManager.startListening(onQueue: processQueue) { status in
             debug(.nightscout, "Network status: \(status)")
         }
@@ -393,6 +404,24 @@ final class BaseNightscoutManager: NightscoutManager, Injectable {
         }
     }
 
+    /// Asynchronously uploads the current status to Nightscout, including OpenAPS status, pump status, and uploader details.
+    ///
+    /// This function gathers and processes various pieces of information such as the "enacted" and "suggested" determinations,
+    /// pump battery and reservoir levels, insulin-on-board (IOB), and the uploader's battery status. It ensures that only
+    /// valid determinations are uploaded by filtering out duplicates and handling unit conversions based on the user's
+    /// settings. If the status upload is successful, it updates the determination storage to mark them as uploaded.
+    ///
+    /// Key steps:
+    /// - Fetch the last unuploaded enacted and suggested determinations from the storage.
+    /// - Retrieve pump-related data such as battery, reservoir, and status.
+    /// - Parse determinations to ensure they are properly formatted for Nightscout, including unit conversions if needed.
+    /// - Construct an `OpenAPSStatus` object with relevant information for upload.
+    /// - Construct a `NightscoutStatus` object with all gathered data.
+    /// - Attempt to upload the status to Nightscout. On success, update the storage to mark determinations as uploaded.
+    /// - Schedule a task to upload pod age data separately.
+    ///
+    /// - Note: Ensure `nightscoutAPI` is initialized and `isUploadEnabled` is set to `true` before invoking this function.
+    /// - Returns: Nothing.
     func uploadStatus() async {
         guard let nightscout = nightscoutAPI, isUploadEnabled else {
             debug(.nightscout, "NS API not available or upload disabled. Aborting NS Status upload.")
@@ -463,13 +492,19 @@ final class BaseNightscoutManager: NightscoutManager, Injectable {
 
         // Gather all relevant data for OpenAPS Status
         let iob = await fetchedIOBEntry
+
+        let suggestedToUpload = modifiedSuggestedDetermination ?? lastSuggestedDetermination
+        let enactedToUpload = fetchedEnactedDetermination ?? lastEnactedDetermination
+
         let openapsStatus = OpenAPSStatus(
             iob: iob?.first,
-            suggested: modifiedSuggestedDetermination,
-            enacted: settingsManager.settings.closedLoop ? fetchedEnactedDetermination : nil,
+            suggested: suggestedToUpload,
+            enacted: settingsManager.settings.closedLoop ? enactedToUpload : nil,
             version: Bundle.main.releaseVersionNumber ?? "Unknown"
         )
 
+        debug(.nightscout, "To be uploaded openapsStatus: \(openapsStatus)")
+
         // Gather all relevant data for NS Status
         let battery = await fetchedBattery
         let reservoir = await fetchedReservoir
@@ -507,8 +542,13 @@ final class BaseNightscoutManager: NightscoutManager, Injectable {
                 await updateOrefDeterminationAsUploaded([suggested])
             }
 
-            lastEnactedDetermination = fetchedEnactedDetermination
-            lastSuggestedDetermination = fetchedSuggestedDetermination
+            if let lastEnactedDetermination = fetchedEnactedDetermination {
+                self.lastEnactedDetermination = lastEnactedDetermination
+            }
+
+            if let lastSuggestedDetermination = fetchedSuggestedDetermination {
+                self.lastSuggestedDetermination = lastSuggestedDetermination
+            }
 
             debug(.nightscout, "NSDeviceStatus with Determination uploaded")
         } catch {