diff --git a/LoopFollow/Controllers/Nightscout/BGData.swift b/LoopFollow/Controllers/Nightscout/BGData.swift index d97aba24c..6f8f764e4 100644 --- a/LoopFollow/Controllers/Nightscout/BGData.swift +++ b/LoopFollow/Controllers/Nightscout/BGData.swift @@ -33,11 +33,26 @@ extension MainViewController { return } - // Dexcom only returns 24 hrs of data. If we need more, call NS. - if graphHours > 24, IsNightscoutEnabled() { - self.webLoadNSBGData(dexData: data) + // Dexcom Share can return duplicate readings when multiple uploaders + // write to the same Dexcom account. Dedup before any further use. + var dedupedData: [ShareGlucoseData] = [] + var lastDexTime = Double.infinity + var lastDexSGV: Int? + for reading in data { + if lastDexSGV == nil || lastDexSGV != reading.sgv || (lastDexTime - reading.date >= 30) { + dedupedData.append(reading) + lastDexTime = reading.date + lastDexSGV = reading.sgv + } + } + + // Supplement with NS if Dex data doesn't cover the full requested window. + let dexCutoff = dateTimeUtils.getNowTimeIntervalUTC() - Double(graphHours) * 3600 + let dexCoversFull = dedupedData.last.map { $0.date <= dexCutoff } ?? false + if !dexCoversFull, IsNightscoutEnabled() { + self.webLoadNSBGData(dexData: dedupedData) } else { - self.ProcessDexBGData(data: data, sourceName: "Dexcom") + self.ProcessDexBGData(data: dedupedData, sourceName: "Dexcom") } } } @@ -52,7 +67,7 @@ extension MainViewController { var parameters: [String: String] = [:] let date = Calendar.current.date(byAdding: .day, value: -1 * Storage.shared.downloadDays.value, to: Date())! - parameters["count"] = "\(Storage.shared.downloadDays.value * 2 * 24 * 60 / 5)" + parameters["count"] = "\(Storage.shared.downloadDays.value * 4 * 24 * 60 / 5)" parameters["find[date][$gte]"] = "\(Int(date.timeIntervalSince1970 * 1000))" // Exclude 'cal' entries diff --git a/LoopFollow/Stats/StatsDataFetcher.swift b/LoopFollow/Stats/StatsDataFetcher.swift index ff61d6eef..cf9617c0b 100644 --- a/LoopFollow/Stats/StatsDataFetcher.swift +++ b/LoopFollow/Stats/StatsDataFetcher.swift @@ -20,7 +20,7 @@ class StatsDataFetcher { var parameters: [String: String] = [:] let utcISODateFormatter = ISO8601DateFormatter() let startDate = dataService?.startDate ?? dateTimeUtils.displayCalendar().date(byAdding: .day, value: -1 * days, to: Date())! - parameters["count"] = "\(days * 2 * 24 * 60 / 5)" + parameters["count"] = "\(days * 4 * 24 * 60 / 5)" parameters["find[dateString][$gte]"] = utcISODateFormatter.string(from: startDate) parameters["find[type][$ne]"] = "cal"