Przeglądaj źródła

Deduplicate Dexcom Share readings to fix delta always showing zero (#661)

Dexcom Share returns each reading twice when both the iPhone Dexcom
app and the Apple Watch app upload to the same account (~9-10 s apart,
same SGV). Without deduplication the two most recent entries in bgData
were always identical, producing delta = 0.

The NS fetch path already had inline deduplication. Extract it into a
shared helper (deduplicateBGReadings) and apply it to the Dexcom-only
path as well.
Jonas Björkert 2 miesięcy temu
rodzic
commit
012a154cdf
1 zmienionych plików z 17 dodań i 13 usunięć
  1. 17 13
      LoopFollow/Controllers/Nightscout/BGData.swift

+ 17 - 13
LoopFollow/Controllers/Nightscout/BGData.swift

@@ -37,7 +37,7 @@ extension MainViewController {
             if graphHours > 24, IsNightscoutEnabled() {
             if graphHours > 24, IsNightscoutEnabled() {
                 self.webLoadNSBGData(dexData: data)
                 self.webLoadNSBGData(dexData: data)
             } else {
             } else {
-                self.ProcessDexBGData(data: data, sourceName: "Dexcom")
+                self.ProcessDexBGData(data: self.deduplicateBGReadings(data), sourceName: "Dexcom")
             }
             }
         }
         }
     }
     }
@@ -70,18 +70,7 @@ extension MainViewController {
                         nsData[i].date.round(FloatingPointRoundingRule.toNearestOrEven)
                         nsData[i].date.round(FloatingPointRoundingRule.toNearestOrEven)
                     }
                     }
 
 
-                    var nsData2: [ShareGlucoseData] = []
-                    var lastAddedTime = Double.infinity
-                    var lastAddedSGV: Int?
-                    let minInterval: Double = 30
-
-                    for reading in nsData {
-                        if (lastAddedSGV == nil || lastAddedSGV != reading.sgv) || (lastAddedTime - reading.date >= minInterval) {
-                            nsData2.append(reading)
-                            lastAddedTime = reading.date
-                            lastAddedSGV = reading.sgv
-                        }
-                    }
+                    var nsData2 = self.deduplicateBGReadings(nsData)
 
 
                     // merge NS and Dex data if needed; use recent Dex data and older NS data
                     // merge NS and Dex data if needed; use recent Dex data and older NS data
                     var sourceName = "Nightscout"
                     var sourceName = "Nightscout"
@@ -117,6 +106,21 @@ extension MainViewController {
         }
         }
     }
     }
 
 
+    /// Removes consecutive duplicate readings (same SGV within 30 s). Expects newest-first input.
+    func deduplicateBGReadings(_ readings: [ShareGlucoseData]) -> [ShareGlucoseData] {
+        var result: [ShareGlucoseData] = []
+        var lastTime = Double.infinity
+        var lastSGV: Int?
+        for reading in readings {
+            if lastSGV == nil || lastSGV != reading.sgv || lastTime - reading.date >= 30 {
+                result.append(reading)
+                lastTime = reading.date
+                lastSGV = reading.sgv
+            }
+        }
+        return result
+    }
+
     /// Processes incoming BG data.
     /// Processes incoming BG data.
     func ProcessDexBGData(data: [ShareGlucoseData], sourceName: String) {
     func ProcessDexBGData(data: [ShareGlucoseData], sourceName: String) {
         let graphHours = 24 * Storage.shared.downloadDays.value
         let graphHours = 24 * Storage.shared.downloadDays.value