Skip to content

Commit 69477c9

Browse files
committed
Deduplicate Dex Share readings before use
Multiple Dexcom uploaders (e.g. G7 iPhone app + Apple Watch) each write readings to Dexcom's cloud, causing the Share API to return 2+ entries per 5-minute slot. With the API's hard cap of 288 readings, duplicates consume the budget and the returned data covers only ~15h instead of 24h. Dedup Dex Share data with the same 30-second window used for Nightscout, so both the NS-supplement path and the direct ProcessDexBGData path receive clean, deduplicated readings.
1 parent eeb9d6e commit 69477c9

1 file changed

Lines changed: 16 additions & 3 deletions

File tree

LoopFollow/Controllers/Nightscout/BGData.swift

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,26 @@ extension MainViewController {
3333
return
3434
}
3535

36+
// Dexcom Share can return duplicate readings when multiple uploaders
37+
// write to the same Dexcom account. Dedup before any further use.
38+
var dedupedData: [ShareGlucoseData] = []
39+
var lastDexTime = Double.infinity
40+
var lastDexSGV: Int?
41+
for reading in data {
42+
if lastDexSGV == nil || lastDexSGV != reading.sgv || (lastDexTime - reading.date >= 30) {
43+
dedupedData.append(reading)
44+
lastDexTime = reading.date
45+
lastDexSGV = reading.sgv
46+
}
47+
}
48+
3649
// Supplement with NS if Dex data doesn't cover the full requested window.
3750
let dexCutoff = dateTimeUtils.getNowTimeIntervalUTC() - Double(graphHours) * 3600
38-
let dexCoversFull = data.last.map { $0.date <= dexCutoff } ?? false
51+
let dexCoversFull = dedupedData.last.map { $0.date <= dexCutoff } ?? false
3952
if !dexCoversFull, IsNightscoutEnabled() {
40-
self.webLoadNSBGData(dexData: data)
53+
self.webLoadNSBGData(dexData: dedupedData)
4154
} else {
42-
self.ProcessDexBGData(data: self.deduplicateBGReadings(data), sourceName: "Dexcom")
55+
self.ProcessDexBGData(data: dedupedData, sourceName: "Dexcom")
4356
}
4457
}
4558
}

0 commit comments

Comments
 (0)