Просмотр исходного кода

NS Status uploading now; showing 'Not Enacted' though WIP

Deniz Cengiz 1 год назад
Родитель
Сommit
429cc5fcaf

+ 4 - 3
FreeAPS/Sources/APS/OpenAPS/OpenAPS.swift

@@ -259,9 +259,10 @@ final class OpenAPS {
 
 
         debug(.openAPS, "Determinated: \(orefDetermination)")
         debug(.openAPS, "Determinated: \(orefDetermination)")
 
 
-        if var determination = Determination(from: orefDetermination) {
-            // TODO: this is so DRASTICALLY wrong… FIX THIS OMFG
-            determination.timestamp = determination.deliverAt ?? clock
+        if var determination = Determination(from: orefDetermination), let deliverAt = determination.deliverAt {
+            // set both timestamp and deliverAt to the SAME date; this will be updated for timestamp once it is enacted
+            // AAPS does it the same way! we'll follow their example!
+            determination.timestamp = deliverAt
 
 
             // save to core data asynchronously
             // save to core data asynchronously
             await processDetermination(determination)
             await processDetermination(determination)

+ 1 - 1
FreeAPS/Sources/Models/Determination.swift

@@ -12,7 +12,7 @@ struct Determination: JSON, Equatable {
     let iob: Decimal?
     let iob: Decimal?
     let cob: Decimal?
     let cob: Decimal?
     var predictions: Predictions?
     var predictions: Predictions?
-    let deliverAt: Date?
+    var deliverAt: Date?
     let carbsReq: Decimal?
     let carbsReq: Decimal?
     let temp: TempType?
     let temp: TempType?
     let bg: Decimal?
     let bg: Decimal?

+ 12 - 8
FreeAPS/Sources/Services/Network/NightscoutManager.swift

@@ -335,12 +335,20 @@ final class BaseNightscoutManager: NightscoutManager, Injectable {
     }
     }
 
 
     func uploadStatus() async {
     func uploadStatus() async {
-        let determination = await determinationStorage.getOrefDeterminationNotYetUploadedToNightscout(
+        let fetchedDetermination = await determinationStorage.getOrefDeterminationNotYetUploadedToNightscout(
             await determinationStorage
             await determinationStorage
                 .fetchLastDeterminationObjectID(predicate: NSPredicate.determinationsNotYetUploadedToNightscout)
                 .fetchLastDeterminationObjectID(predicate: NSPredicate.determinationsNotYetUploadedToNightscout)
         )
         )
 
 
-        let wasDeterminationEnacted = determination?.deliverAt != nil && determination?.timestamp != nil
+        guard let determination = fetchedDetermination, let nightscout = nightscoutAPI, isUploadEnabled else {
+            debug(.nightscout, "Abort NS uploadStatus")
+            return
+        }
+
+        let wasDeterminationEnacted = determination.deliverAt != nil && determination.timestamp != nil
+
+        var suggested = determination
+        suggested.timestamp = suggested.deliverAt
 
 
         let iob = storage.retrieve(OpenAPS.Monitor.iob, as: [IOBEntry].self)
         let iob = storage.retrieve(OpenAPS.Monitor.iob, as: [IOBEntry].self)
 
 
@@ -351,14 +359,14 @@ final class BaseNightscoutManager: NightscoutManager, Injectable {
         if loopIsClosed {
         if loopIsClosed {
             openapsStatus = OpenAPSStatus(
             openapsStatus = OpenAPSStatus(
                 iob: iob?.first,
                 iob: iob?.first,
-                suggested: wasDeterminationEnacted ? nil : determination,
+                suggested: wasDeterminationEnacted ? nil : suggested,
                 enacted: wasDeterminationEnacted ? determination : nil,
                 enacted: wasDeterminationEnacted ? determination : nil,
                 version: "0.7.1"
                 version: "0.7.1"
             )
             )
         } else {
         } else {
             openapsStatus = OpenAPSStatus(
             openapsStatus = OpenAPSStatus(
                 iob: iob?.first,
                 iob: iob?.first,
-                suggested: determination,
+                suggested: determination, // in this case, we will never see an actually enacted determination, so both timestamp and deliverAt are the same
                 enacted: nil,
                 enacted: nil,
                 version: "0.7.1"
                 version: "0.7.1"
             )
             )
@@ -389,10 +397,6 @@ final class BaseNightscoutManager: NightscoutManager, Injectable {
 
 
         storage.save(status, as: OpenAPS.Upload.nsStatus)
         storage.save(status, as: OpenAPS.Upload.nsStatus)
 
 
-        guard let determination = determination, let nightscout = nightscoutAPI, isUploadEnabled else {
-            return
-        }
-
         do {
         do {
             try await nightscout.uploadStatus(status)
             try await nightscout.uploadStatus(status)
             debug(.nightscout, "Status uploaded")
             debug(.nightscout, "Status uploaded")