DeviceDataManager.swift 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. //
  2. // DeviceDataManager.swift
  3. // LoopKit
  4. //
  5. // Created by Nathan Racklyeft on 3/18/16.
  6. // Copyright © 2016 Nathan Racklyeft. All rights reserved.
  7. //
  8. import Foundation
  9. import HealthKit
  10. import LoopKit
  11. class DeviceDataManager {
  12. init() {
  13. healthStore = HKHealthStore()
  14. let cacheStore = PersistenceController(directoryURL: FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).first!)
  15. let observationInterval: TimeInterval = .hours(24)
  16. carbSampleStore = HealthKitSampleStore(
  17. healthStore: healthStore,
  18. observeHealthKitSamplesFromOtherApps: false,
  19. type: HealthKitSampleStore.carbType,
  20. observationStart: Date().addingTimeInterval(-observationInterval),
  21. observationEnabled: false)
  22. carbStore = CarbStore(
  23. healthKitSampleStore: carbSampleStore,
  24. cacheStore: cacheStore,
  25. cacheLength: observationInterval,
  26. defaultAbsorptionTimes: (fast: .minutes(30), medium: .hours(3), slow: .hours(5)),
  27. carbRatioSchedule: carbRatioSchedule,
  28. insulinSensitivitySchedule: insulinSensitivitySchedule,
  29. provenanceIdentifier: HKSource.default().bundleIdentifier
  30. )
  31. doseSampleStore = HealthKitSampleStore(
  32. healthStore: healthStore,
  33. observeHealthKitSamplesFromOtherApps: false,
  34. type: HealthKitSampleStore.insulinQuantityType,
  35. observationStart: Date().addingTimeInterval(-observationInterval),
  36. observationEnabled: false)
  37. doseStore = DoseStore(
  38. healthKitSampleStore: doseSampleStore,
  39. cacheStore: cacheStore,
  40. insulinModelProvider: PresetInsulinModelProvider(defaultRapidActingModel: ExponentialInsulinModelPreset.rapidActingAdult),
  41. longestEffectDuration: ExponentialInsulinModelPreset.rapidActingAdult.effectDuration,
  42. basalProfile: basalRateSchedule,
  43. insulinSensitivitySchedule: insulinSensitivitySchedule,
  44. provenanceIdentifier: HKSource.default().bundleIdentifier
  45. )
  46. glucoseSampleStore = HealthKitSampleStore(
  47. healthStore: healthStore,
  48. observeHealthKitSamplesFromOtherApps: false,
  49. type: HealthKitSampleStore.glucoseType,
  50. observationStart: Date().addingTimeInterval(-observationInterval),
  51. observationEnabled: false)
  52. glucoseStore = GlucoseStore(
  53. healthKitSampleStore: glucoseSampleStore,
  54. cacheStore: cacheStore,
  55. provenanceIdentifier: HKSource.default().bundleIdentifier)
  56. }
  57. // Data stores
  58. let healthStore: HKHealthStore
  59. let carbSampleStore: HealthKitSampleStore!
  60. let carbStore: CarbStore!
  61. let doseSampleStore: HealthKitSampleStore!
  62. let doseStore: DoseStore
  63. let glucoseSampleStore: HealthKitSampleStore!
  64. let glucoseStore: GlucoseStore!
  65. // Settings
  66. var basalRateSchedule = UserDefaults.standard.basalRateSchedule {
  67. didSet {
  68. UserDefaults.standard.basalRateSchedule = basalRateSchedule
  69. doseStore.basalProfile = basalRateSchedule
  70. }
  71. }
  72. var carbRatioSchedule = UserDefaults.standard.carbRatioSchedule {
  73. didSet {
  74. UserDefaults.standard.carbRatioSchedule = carbRatioSchedule
  75. carbStore?.carbRatioSchedule = carbRatioSchedule
  76. }
  77. }
  78. var insulinSensitivitySchedule = UserDefaults.standard.insulinSensitivitySchedule {
  79. didSet {
  80. UserDefaults.standard.insulinSensitivitySchedule = insulinSensitivitySchedule
  81. carbStore?.insulinSensitivitySchedule = insulinSensitivitySchedule
  82. doseStore.insulinSensitivitySchedule = insulinSensitivitySchedule
  83. }
  84. }
  85. var glucoseTargetRangeSchedule = UserDefaults.standard.glucoseTargetRangeSchedule {
  86. didSet {
  87. UserDefaults.standard.glucoseTargetRangeSchedule = glucoseTargetRangeSchedule
  88. }
  89. }
  90. public var preMealTargetRange: DoubleRange? = UserDefaults.standard.preMealTargetRange {
  91. didSet {
  92. UserDefaults.standard.preMealTargetRange = preMealTargetRange
  93. }
  94. }
  95. public var legacyWorkoutTargetRange: DoubleRange? = UserDefaults.standard.legacyWorkoutTargetRange {
  96. didSet {
  97. UserDefaults.standard.legacyWorkoutTargetRange = legacyWorkoutTargetRange
  98. }
  99. }
  100. var pumpID = UserDefaults.standard.pumpID {
  101. didSet {
  102. UserDefaults.standard.pumpID = pumpID
  103. if pumpID != oldValue {
  104. doseStore.resetPumpData()
  105. }
  106. }
  107. }
  108. // MARK: CarbStoreDelegate
  109. func carbStoreHasUpdatedCarbData(_ carbStore: CarbStore) {}
  110. func carbStore(_ carbStore: CarbStore, didError error: CarbStore.CarbStoreError) {
  111. print("carbstore error: \(error)")
  112. }
  113. }