forked from LoopKit/LoopKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeviceDataManager.swift
More file actions
133 lines (106 loc) · 4.16 KB
/
DeviceDataManager.swift
File metadata and controls
133 lines (106 loc) · 4.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
//
// DeviceDataManager.swift
// LoopKit
//
// Created by Nathan Racklyeft on 3/18/16.
// Copyright © 2016 Nathan Racklyeft. All rights reserved.
//
import Foundation
import HealthKit
import LoopKit
import LoopAlgorithm
class DeviceDataManager {
init() async {
healthStore = HKHealthStore()
let cacheStore = PersistenceController(directoryURL: FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).first!)
let observationInterval: TimeInterval = .hours(24)
carbSampleStore = HealthKitSampleStore(
healthStore: healthStore,
observeHealthKitSamplesFromOtherApps: false,
type: HealthKitSampleStore.carbType,
observationStart: Date().addingTimeInterval(-observationInterval),
observationEnabled: false)
carbStore = CarbStore(
healthKitSampleStore: carbSampleStore,
cacheStore: cacheStore,
cacheLength: observationInterval,
provenanceIdentifier: HKSource.default().bundleIdentifier
)
doseSampleStore = HealthKitSampleStore(
healthStore: healthStore,
observeHealthKitSamplesFromOtherApps: false,
type: HealthKitSampleStore.insulinQuantityType,
observationStart: Date().addingTimeInterval(-observationInterval),
observationEnabled: false)
doseStore = await DoseStore(
healthKitSampleStore: doseSampleStore,
cacheStore: cacheStore,
longestEffectDuration: ExponentialInsulinModelPreset.rapidActingAdult.effectDuration,
provenanceIdentifier: HKSource.default().bundleIdentifier
)
glucoseSampleStore = HealthKitSampleStore(
healthStore: healthStore,
observeHealthKitSamplesFromOtherApps: false,
type: HealthKitSampleStore.glucoseType,
observationStart: Date().addingTimeInterval(-observationInterval),
observationEnabled: false)
glucoseStore = await GlucoseStore(
healthKitSampleStore: glucoseSampleStore,
cacheStore: cacheStore,
provenanceIdentifier: HKSource.default().bundleIdentifier)
}
// Data stores
let healthStore: HKHealthStore
let carbSampleStore: HealthKitSampleStore!
let carbStore: CarbStore!
let doseSampleStore: HealthKitSampleStore!
let doseStore: DoseStore
let glucoseSampleStore: HealthKitSampleStore!
let glucoseStore: GlucoseStore!
// Settings
var basalRateSchedule = UserDefaults.standard.basalRateSchedule {
didSet {
UserDefaults.standard.basalRateSchedule = basalRateSchedule
}
}
var carbRatioSchedule = UserDefaults.standard.carbRatioSchedule {
didSet {
UserDefaults.standard.carbRatioSchedule = carbRatioSchedule
}
}
var insulinSensitivitySchedule = UserDefaults.standard.insulinSensitivitySchedule {
didSet {
UserDefaults.standard.insulinSensitivitySchedule = insulinSensitivitySchedule
}
}
var glucoseTargetRangeSchedule = UserDefaults.standard.glucoseTargetRangeSchedule {
didSet {
UserDefaults.standard.glucoseTargetRangeSchedule = glucoseTargetRangeSchedule
}
}
public var preMealTargetRange: DoubleRange? = UserDefaults.standard.preMealTargetRange {
didSet {
UserDefaults.standard.preMealTargetRange = preMealTargetRange
}
}
public var legacyWorkoutTargetRange: DoubleRange? = UserDefaults.standard.legacyWorkoutTargetRange {
didSet {
UserDefaults.standard.legacyWorkoutTargetRange = legacyWorkoutTargetRange
}
}
var pumpID = UserDefaults.standard.pumpID {
didSet {
UserDefaults.standard.pumpID = pumpID
if pumpID != oldValue {
Task {
try await doseStore.resetPumpData()
}
}
}
}
// MARK: CarbStoreDelegate
func carbStoreHasUpdatedCarbData(_ carbStore: CarbStore) {}
func carbStore(_ carbStore: CarbStore, didError error: CarbStore.CarbStoreError) {
print("carbstore error: \(error)")
}
}