Jonas Björkert 1 год назад
Родитель
Сommit
4cb919e457

+ 2 - 0
Trio/Sources/Models/Determination.swift

@@ -19,6 +19,7 @@ struct Determination: JSON, Equatable {
     let reservoir: Decimal?
     var isf: Decimal?
     var timestamp: Date?
+    var tdd: Decimal?
     var current_target: Decimal?
     let insulinForManualBolus: Decimal?
     let manualBolusErrorString: Decimal?
@@ -59,6 +60,7 @@ extension Determination {
         case timestamp
         case isf = "ISF"
         case current_target
+        case tdd
         case insulinForManualBolus
         case manualBolusErrorString
         case minDelta

+ 55 - 0
Trio/Sources/Services/Network/Nightscout/NightscoutManager.swift

@@ -504,6 +504,14 @@ final class BaseNightscoutManager: NightscoutManager, Injectable {
             return
         }
 
+        // Fetch the latest TDD from Core Data
+        let tdd: Decimal? = await backgroundContext.perform {
+            let request: NSFetchRequest<TDDStored> = TDDStored.fetchRequest()
+            request.sortDescriptors = [NSSortDescriptor(key: "date", ascending: false)]
+            request.fetchLimit = 1
+            return (try? self.backgroundContext.fetch(request).first)?.total as? Decimal
+        }
+
         // Suggested / Enacted
         async let enactedDeterminationID = determinationStorage
             .fetchLastDeterminationObjectID(predicate: NSPredicate.enactedDeterminationsNotYetUploadedToNightscout)
@@ -543,6 +551,10 @@ final class BaseNightscoutManager: NightscoutManager, Injectable {
                 suggestion.minPredBG = suggestion.minPredBG?.asMmolL
                 suggestion.threshold = suggestion.threshold?.asMmolL
             }
+
+            suggestion.reason = injectTDD(into: suggestion.reason, tdd: tdd)
+            suggestion.tdd = tdd
+
             // Check whether the last suggestion that was uploaded is the same that is fetched again when we are attempting to upload the enacted determination
             // Apparently we are too fast; so the flag update is not fast enough to have the predicate filter last suggestion out
             // If this check is truthy, set suggestion to nil so it's not uploaded again
@@ -566,6 +578,15 @@ final class BaseNightscoutManager: NightscoutManager, Injectable {
             fetchedEnactedDetermination = modifiedFetchedEnactedDetermination
         }
 
+        if let fetchedEnacted = fetchedEnactedDetermination {
+            var modifiedFetchedEnactedDetermination = fetchedEnacted
+            modifiedFetchedEnactedDetermination.tdd = tdd
+
+            modifiedFetchedEnactedDetermination.reason = injectTDD(into: modifiedFetchedEnactedDetermination.reason, tdd: tdd)
+
+            fetchedEnactedDetermination = modifiedFetchedEnactedDetermination
+        }
+
         // Gather all relevant data for OpenAPS Status
         let iob = await fetchedIOBEntry
 
@@ -1453,3 +1474,37 @@ extension BaseNightscoutManager {
         return updatedReason
     }
 }
+
+extension BaseNightscoutManager {
+    /// Injects TDD into the reason string
+    func injectTDD(into reason: String, tdd: Decimal?) -> String {
+        guard let tdd = tdd else {
+            return reason
+        }
+
+        let tddString = "TDD: \(tdd) U"
+
+        // Define the regex pattern to match prediction terms followed by their values
+        // The pattern matches any of the terms, followed by optional non-digit characters,
+        // and then a number (integer or decimal)
+        let pattern = "(minPredBG|minGuardBG|IOBpredBG|COBpredBG|UAMpredBG)[^\\d]*\\d+(\\.\\d+)?"
+
+        guard let regex = try? NSRegularExpression(pattern: pattern, options: []) else {
+            return reason + ", \(tddString)"
+        }
+
+        let range = NSRange(location: 0, length: reason.utf16.count)
+
+        let matches = regex.matches(in: reason, options: [], range: range)
+
+        if let lastMatch = matches.last {
+            // Get the insertion point, which is the end of the last match
+            let insertionIndex = reason.index(reason.startIndex, offsetBy: lastMatch.range.upperBound)
+
+            let newReason = String(reason[..<insertionIndex]) + tddString + String(reason[insertionIndex...])
+            return newReason
+        } else {
+            return reason + ", \(tddString)"
+        }
+    }
+}