|
@@ -12,14 +12,12 @@ protocol NightscoutManager: GlucoseSource {
|
|
|
func deleteCarbs(withID id: String) async
|
|
func deleteCarbs(withID id: String) async
|
|
|
func deleteInsulin(withID id: String) async
|
|
func deleteInsulin(withID id: String) async
|
|
|
func deleteGlucose(withID id: String, withDate date: Date) async
|
|
func deleteGlucose(withID id: String, withDate date: Date) async
|
|
|
- func deleteManualGlucose(withID id: String, withDate date: Date) async
|
|
|
|
|
func uploadDeviceStatus() async throws
|
|
func uploadDeviceStatus() async throws
|
|
|
func uploadGlucose() async
|
|
func uploadGlucose() async
|
|
|
func uploadCarbs() async
|
|
func uploadCarbs() async
|
|
|
func uploadPumpHistory() async
|
|
func uploadPumpHistory() async
|
|
|
func uploadOverrides() async
|
|
func uploadOverrides() async
|
|
|
func uploadTempTargets() async
|
|
func uploadTempTargets() async
|
|
|
- func uploadManualGlucose() async
|
|
|
|
|
func uploadProfiles() async throws
|
|
func uploadProfiles() async throws
|
|
|
func uploadNoteTreatment(note: String) async
|
|
func uploadNoteTreatment(note: String) async
|
|
|
func importSettings() async -> ScheduledNightscoutProfile?
|
|
func importSettings() async -> ScheduledNightscoutProfile?
|
|
@@ -55,7 +53,7 @@ final class BaseNightscoutManager: NightscoutManager, Injectable {
|
|
|
/// coalesce into a single upload run for that pipeline.
|
|
/// coalesce into a single upload run for that pipeline.
|
|
|
let uploadPipelineInterval: [NightscoutUploadPipeline: TimeInterval] = [
|
|
let uploadPipelineInterval: [NightscoutUploadPipeline: TimeInterval] = [
|
|
|
.carbs: 2, .pumpHistory: 2, .overrides: 2, .tempTargets: 2,
|
|
.carbs: 2, .pumpHistory: 2, .overrides: 2, .tempTargets: 2,
|
|
|
- .glucose: 2, .manualGlucose: 2, .deviceStatus: 2
|
|
|
|
|
|
|
+ .glucose: 2, .deviceStatus: 2
|
|
|
]
|
|
]
|
|
|
|
|
|
|
|
/// Subjects used to request an upload pipeline. The pipeline applies a throttle so
|
|
/// Subjects used to request an upload pipeline. The pipeline applies a throttle so
|
|
@@ -96,7 +94,6 @@ final class BaseNightscoutManager: NightscoutManager, Injectable {
|
|
|
case .overrides: await uploadOverrides()
|
|
case .overrides: await uploadOverrides()
|
|
|
case .tempTargets: await uploadTempTargets()
|
|
case .tempTargets: await uploadTempTargets()
|
|
|
case .glucose: await uploadGlucose()
|
|
case .glucose: await uploadGlucose()
|
|
|
- case .manualGlucose: await uploadManualGlucose()
|
|
|
|
|
case .deviceStatus:
|
|
case .deviceStatus:
|
|
|
do { try await uploadDeviceStatus() }
|
|
do { try await uploadDeviceStatus() }
|
|
|
catch { debug(.nightscout, "deviceStatus upload failed: \(error)") }
|
|
catch { debug(.nightscout, "deviceStatus upload failed: \(error)") }
|
|
@@ -354,19 +351,6 @@ final class BaseNightscoutManager: NightscoutManager, Injectable {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- func deleteManualGlucose(withID id: String, withDate date: Date) async {
|
|
|
|
|
- guard let nightscout = nightscoutAPI, isUploadEnabled else { return }
|
|
|
|
|
-
|
|
|
|
|
- do {
|
|
|
|
|
- try await nightscout.deleteManualGlucose(withId: id, withDate: date)
|
|
|
|
|
- } catch {
|
|
|
|
|
- debug(
|
|
|
|
|
- .nightscout,
|
|
|
|
|
- "\(DebuggingIdentifiers.failed) Failed to delete Manual Glucose from Nightscout with error: \(error)"
|
|
|
|
|
- )
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
private func fetchBattery() async -> Battery {
|
|
private func fetchBattery() async -> Battery {
|
|
|
await backgroundContext.perform {
|
|
await backgroundContext.perform {
|
|
|
do {
|
|
do {
|
|
@@ -818,17 +802,6 @@ final class BaseNightscoutManager: NightscoutManager, Injectable {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- func uploadManualGlucose() async {
|
|
|
|
|
- do {
|
|
|
|
|
- try await uploadManualGlucose(glucoseStorage.getManualGlucoseNotYetUploadedToNightscout())
|
|
|
|
|
- } catch {
|
|
|
|
|
- debug(
|
|
|
|
|
- .nightscout,
|
|
|
|
|
- "\(DebuggingIdentifiers.failed) failed to upload manual glucose with error: \(error)"
|
|
|
|
|
- )
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
func uploadPumpHistory() async {
|
|
func uploadPumpHistory() async {
|
|
|
do {
|
|
do {
|
|
|
try await uploadPumpHistory(pumpHistoryStorage.getPumpHistoryNotYetUploadedToNightscout())
|
|
try await uploadPumpHistory(pumpHistoryStorage.getPumpHistoryNotYetUploadedToNightscout())
|
|
@@ -974,47 +947,6 @@ final class BaseNightscoutManager: NightscoutManager, Injectable {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- private func uploadManualGlucose(_ treatments: [NightscoutTreatment]) async {
|
|
|
|
|
- guard !treatments.isEmpty, let nightscout = nightscoutAPI, isUploadEnabled else {
|
|
|
|
|
- return
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- do {
|
|
|
|
|
- for chunk in treatments.chunks(ofCount: 100) {
|
|
|
|
|
- try await nightscout.uploadTreatments(Array(chunk))
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // If successful, update the isUploadedToNS property of the GlucoseStored objects
|
|
|
|
|
- await updateManualGlucoseAsUploaded(treatments)
|
|
|
|
|
-
|
|
|
|
|
- debug(.nightscout, "Treatments uploaded")
|
|
|
|
|
- } catch {
|
|
|
|
|
- debug(.nightscout, String(describing: error))
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- private func updateManualGlucoseAsUploaded(_ treatments: [NightscoutTreatment]) async {
|
|
|
|
|
- await backgroundContext.perform {
|
|
|
|
|
- let ids = treatments.map(\.id) as NSArray
|
|
|
|
|
- let fetchRequest: NSFetchRequest<GlucoseStored> = GlucoseStored.fetchRequest()
|
|
|
|
|
- fetchRequest.predicate = NSPredicate(format: "id IN %@", ids)
|
|
|
|
|
-
|
|
|
|
|
- do {
|
|
|
|
|
- let results = try self.backgroundContext.fetch(fetchRequest)
|
|
|
|
|
- for result in results {
|
|
|
|
|
- result.isUploadedToNS = true
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- guard self.backgroundContext.hasChanges else { return }
|
|
|
|
|
- try self.backgroundContext.save()
|
|
|
|
|
- } catch let error as NSError {
|
|
|
|
|
- debugPrint(
|
|
|
|
|
- "\(DebuggingIdentifiers.failed) \(#file) \(#function) Failed to update isUploadedToNS: \(error.userInfo)"
|
|
|
|
|
- )
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
private func uploadCarbs(_ treatments: [NightscoutTreatment]) async {
|
|
private func uploadCarbs(_ treatments: [NightscoutTreatment]) async {
|
|
|
guard !treatments.isEmpty, let nightscout = nightscoutAPI, isUploadEnabled else {
|
|
guard !treatments.isEmpty, let nightscout = nightscoutAPI, isUploadEnabled else {
|
|
|
return
|
|
return
|