diff --git a/xdrip/Constants/ConstantsNightScout.swift b/xdrip/Constants/ConstantsNightScout.swift index 356ca3f5..06c793f0 100644 --- a/xdrip/Constants/ConstantsNightScout.swift +++ b/xdrip/Constants/ConstantsNightScout.swift @@ -11,4 +11,7 @@ enum ConstantsNightScout { /// if the time between the last and last but one reading is less than minimiumTimeBetweenTwoReadingsInMinutes, then the last reading will not be uploaded - except if there's been a disconnect in between these two readings static let minimiumTimeBetweenTwoReadingsInMinutes = 4.75 + /// there's al imit of 102400 bytes to upload to NightScout, this corresponds on average to 400 readings. Setting a lower maximum value to avoid to bypass this limit. + static let maxReadingsToUpload = 300 + } diff --git a/xdrip/Managers/NightScout/NightScoutUploadManager.swift b/xdrip/Managers/NightScout/NightScoutUploadManager.swift index 91004886..6d4541fd 100644 --- a/xdrip/Managers/NightScout/NightScoutUploadManager.swift +++ b/xdrip/Managers/NightScout/NightScoutUploadManager.swift @@ -291,11 +291,23 @@ public class NightScoutUploadManager:NSObject { } // get latest readings, filter : minimiumTimeBetweenTwoReadingsInMinutes beteen two readings, except for the first if a dis/reconnect occured since the latest reading - let bgReadingsToUpload = bgReadingsAccessor.getLatestBgReadings(limit: nil, fromDate: timeStamp, forSensor: nil, ignoreRawData: true, ignoreCalculatedValue: false).filter(minimumTimeBetweenTwoReadingsInMinutes: ConstantsNightScout.minimiumTimeBetweenTwoReadingsInMinutes, lastConnectionStatusChangeTimeStamp: lastConnectionStatusChangeTimeStamp, timeStampLastProcessedBgReading: timeStamp) + var bgReadingsToUpload = bgReadingsAccessor.getLatestBgReadings(limit: nil, fromDate: timeStamp, forSensor: nil, ignoreRawData: true, ignoreCalculatedValue: false).filter(minimumTimeBetweenTwoReadingsInMinutes: ConstantsNightScout.minimiumTimeBetweenTwoReadingsInMinutes, lastConnectionStatusChangeTimeStamp: lastConnectionStatusChangeTimeStamp, timeStampLastProcessedBgReading: timeStamp) if bgReadingsToUpload.count > 0 { trace(" number of readings to upload : %{public}@", log: self.oslog, category: ConstantsLog.categoryNightScoutUploadManager, type: .info, bgReadingsToUpload.count.description) + // there's a limit of payload size to upload to NightScout + // if size is > maximum, then we'll have to call the upload function again, this variable will be used in completionHandler + let callAgainNeeded = bgReadingsToUpload.count > ConstantsNightScout.maxReadingsToUpload + + if callAgainNeeded { + trace(" restricting readings to upload to %{public}@", log: self.oslog, category: ConstantsLog.categoryNightScoutUploadManager, type: .info, ConstantsNightScout.maxReadingsToUpload.description) + } + + // limit the amount of readings to upload to avoid passing this limit + // we start with the oldest readings + bgReadingsToUpload = bgReadingsToUpload.suffix(ConstantsNightScout.maxReadingsToUpload) + // map readings to dictionaryRepresentation let bgReadingsDictionaryRepresentation = bgReadingsToUpload.map({$0.dictionaryRepresentationForNightScoutUpload}) @@ -311,6 +323,19 @@ public class NightScoutUploadManager:NSObject { UserDefaults.standard.timeStampLatestNightScoutUploadedBgReading = timeStampLastReadingToUpload + // callAgainNeeded means we've limit the amount of readings because size was too big + // if so a new upload is needed + if callAgainNeeded { + + // do this in the main thread because the readings are fetched with the main mainManagedObjectContext + DispatchQueue.main.async { + + self.uploadBgReadingsToNightScout(siteURL: siteURL, apiKey: apiKey, lastConnectionStatusChangeTimeStamp: lastConnectionStatusChangeTimeStamp) + + } + + } + } }) @@ -387,6 +412,8 @@ public class NightScoutUploadManager:NSObject { // transform dataToUpload to json let dateToUploadAsJSON = try JSONSerialization.data(withJSONObject: dataToUpload, options: []) + trace(" size of data to upload : %{public}@", log: self.oslog, category: ConstantsLog.categoryNightScoutUploadManager, type: .info, dateToUploadAsJSON.count.description) + if let url = URL(string: siteURL), var uRLComponents = URLComponents(url: url.appendingPathComponent(path), resolvingAgainstBaseURL: false) { if UserDefaults.standard.nightScoutPort != 0 {