-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathPumpEvent+helper.swift
More file actions
345 lines (300 loc) · 11 KB
/
PumpEvent+helper.swift
File metadata and controls
345 lines (300 loc) · 11 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
import CoreData
import Foundation
extension PumpEventStored {
static func fetch(_ predicate: NSPredicate, ascending: Bool, fetchLimit: Int? = nil) -> NSFetchRequest<PumpEventStored> {
let request = PumpEventStored.fetchRequest()
request.sortDescriptors = [NSSortDescriptor(key: "timestamp", ascending: ascending)]
request.resultType = .managedObjectResultType
request.predicate = predicate
if let fetchLimit = fetchLimit {
request.fetchLimit = fetchLimit
}
return request
}
// Preview
@discardableResult static func makePreviewEvents(count: Int, provider: CoreDataStack) -> [PumpEventStored] {
let context = provider.persistentContainer.viewContext
let events = (0 ..< count).map { index -> PumpEventStored in
let event = PumpEventStored(context: context)
event.id = UUID().uuidString
event.timestamp = Date.now.addingTimeInterval(Double(index) * -300) // Every 5 minutes
event.type = EventType.bolus.rawValue
event.isUploadedToNS = false
event.isUploadedToHealth = false
event.isUploadedToTidepool = false
// Add a bolus
let bolus = BolusStored(context: context)
bolus.amount = 2.5 as NSDecimalNumber
bolus.isExternal = false
bolus.isSMB = false
bolus.pumpEvent = event
return event
}
try? context.save()
return events
}
}
public extension PumpEventStored {
enum EventType: String, JSON {
case bolus = "Bolus"
case smb = "SMB"
case isExternal = "External Insulin"
case mealBolus = "Meal Bolus"
case correctionBolus = "Correction Bolus"
case snackBolus = "Snack Bolus"
case bolusWizard = "BolusWizard"
case tempBasal = "TempBasal"
case tempBasalDuration = "TempBasalDuration"
case pumpSuspend = "PumpSuspend"
case pumpResume = "PumpResume"
case pumpAlarm = "PumpAlarm"
case pumpBattery = "PumpBattery"
case rewind = "Rewind"
case prime = "Prime"
case journalCarbs = "JournalEntryMealMarker"
case nsNote = "Note"
case nsTempBasal = "Temp Basal"
case nsCarbCorrection = "Carb Correction"
case nsTempTarget = "Temporary Target"
case nsInsulinChange = "Insulin Change"
case nsSiteChange = "Site Change"
case nsBatteryChange = "Pump Battery Change"
case nsAnnouncement = "Announcement"
case nsSensorChange = "Sensor Start"
case nsExercise = "Exercise"
case capillaryGlucose = "BG Check"
}
enum TempType: String, JSON {
case absolute
case percent
}
}
extension NSPredicate {
static var pumpHistoryLast1440Minutes: NSPredicate {
let date = Date.oneDayAgoInMinutes
return NSPredicate(format: "timestamp >= %@", date as NSDate)
}
static var pumpHistoryLast24h: NSPredicate {
let date = Date.oneDayAgo
return NSPredicate(format: "timestamp >= %@", date as NSDate)
}
static var pumpHistoryForStats: NSPredicate {
let date = Date.threeMonthsAgo
return NSPredicate(format: "pumpEvent.timestamp >= %@", date as NSDate)
}
static var recentPumpHistory: NSPredicate {
let date = Date.twentyMinutesAgo
return NSPredicate(
format: "type == %@ AND timestamp >= %@",
PumpEventStored.EventType.tempBasal.rawValue,
date as NSDate
)
}
static var lastPumpBolus: NSPredicate {
let date = Date.twentyMinutesAgo
return NSPredicate(format: "timestamp >= %@ AND bolus.isExternal == %@", date as NSDate, false as NSNumber)
}
static func duplicates(_ date: Date) -> NSPredicate {
NSPredicate(format: "timestamp == %@", date as NSDate)
}
static var pumpEventsNotYetUploadedToNightscout: NSPredicate {
let date = Date.oneDayAgo
return NSPredicate(format: "timestamp >= %@ AND isUploadedToNS == %@", date as NSDate, false as NSNumber)
}
static var pumpEventsNotYetUploadedToHealth: NSPredicate {
let date = Date.oneDayAgo
return NSPredicate(format: "timestamp >= %@ AND isUploadedToHealth == %@", date as NSDate, false as NSNumber)
}
static var pumpEventsNotYetUploadedToTidepool: NSPredicate {
let date = Date.oneDayAgo
return NSPredicate(format: "timestamp >= %@ AND isUploadedToTidepool == %@", date as NSDate, false as NSNumber)
}
}
// Declare helper structs ("data transfer objects" = DTO) to utilize parsing a flattened pump history
struct BolusDTO: Codable {
var id: String
var timestamp: String
var amount: Double
var isExternal: Bool
var isSMB: Bool
var duration: Int
var _type: String = EventType.bolus.rawValue
}
struct TempBasalDTO: Codable {
var id: String
var timestamp: String
var temp: String
var rate: Double
var _type: String = EventType.tempBasal.rawValue
}
struct TempBasalDurationDTO: Codable {
var id: String
var timestamp: String
var duration: Int
var _type: String = EventType.tempBasalDuration.rawValue
private enum CodingKeys: String, CodingKey {
case id
case timestamp
case duration = "duration (min)"
case _type
}
}
struct SuspendDTO: Codable {
var id: String
var timestamp: String
var _type: String = EventType.pumpSuspend.rawValue
}
struct ResumeDTO: Codable {
var id: String
var timestamp: String
var _type: String = EventType.pumpResume.rawValue
}
struct RewindDTO: Codable {
var id: String
var timestamp: String
var _type: String = EventType.rewind.rawValue
}
struct PrimeDTO: Codable {
var id: String
var timestamp: String
var _type: String = EventType.prime.rawValue
}
// Mask distinct DTO subtypes with a common enum that conforms to Encodable
enum PumpEventDTO: Encodable {
case bolus(BolusDTO)
case tempBasal(TempBasalDTO)
case tempBasalDuration(TempBasalDurationDTO)
case suspend(SuspendDTO)
case resume(ResumeDTO)
case rewind(RewindDTO)
case prime(PrimeDTO)
func encode(to encoder: Encoder) throws {
switch self {
case let .bolus(bolus):
try bolus.encode(to: encoder)
case let .tempBasal(tempBasal):
try tempBasal.encode(to: encoder)
case let .tempBasalDuration(tempBasalDuration):
try tempBasalDuration.encode(to: encoder)
case let .suspend(suspend):
try suspend.encode(to: encoder)
case let .resume(resume):
try resume.encode(to: encoder)
case let .rewind(rewind):
try rewind.encode(to: encoder)
case let .prime(prime):
try prime.encode(to: encoder)
}
}
var isResume: Bool {
if case .resume = self { return true }
return false
}
var isSuspend: Bool {
if case .suspend = self { return true }
return false
}
var timestampDate: Date? {
switch self {
case let .bolus(dto):
return PumpEventStored.dateFormatter.date(from: dto.timestamp)
case let .tempBasal(dto):
return PumpEventStored.dateFormatter.date(from: dto.timestamp)
case let .tempBasalDuration(dto):
return PumpEventStored.dateFormatter.date(from: dto.timestamp)
case let .suspend(dto):
return PumpEventStored.dateFormatter.date(from: dto.timestamp)
case let .resume(dto):
return PumpEventStored.dateFormatter.date(from: dto.timestamp)
case let .rewind(dto):
return PumpEventStored.dateFormatter.date(from: dto.timestamp)
case let .prime(dto):
return PumpEventStored.dateFormatter.date(from: dto.timestamp)
}
}
}
// Extension with helper functions to map pump events to DTO objects via uniform masking enum
extension PumpEventStored {
static let dateFormatter: ISO8601DateFormatter = {
let formatter = ISO8601DateFormatter()
formatter.formatOptions = [.withInternetDateTime, .withFractionalSeconds]
return formatter
}()
func toBolusDTOEnum() -> PumpEventDTO? {
guard let timestamp = timestamp, let bolus = bolus, let amount = bolus.amount else {
return nil
}
let bolusDTO = BolusDTO(
id: id ?? UUID().uuidString,
timestamp: PumpEventStored.dateFormatter.string(from: timestamp),
amount: amount.doubleValue,
isExternal: bolus.isExternal,
isSMB: bolus.isSMB,
duration: 0
)
return .bolus(bolusDTO)
}
func toTempBasalDTOEnum() -> PumpEventDTO? {
guard let id = id, let timestamp = timestamp, let tempBasal = tempBasal, let rate = tempBasal.rate else {
return nil
}
let tempBasalDTO = TempBasalDTO(
id: "_\(id)",
timestamp: PumpEventStored.dateFormatter.string(from: timestamp),
temp: tempBasal.tempType ?? "unknown",
rate: rate.doubleValue
)
return .tempBasal(tempBasalDTO)
}
func toTempBasalDurationDTOEnum() -> PumpEventDTO? {
guard let id = id, let timestamp = timestamp, let tempBasal = tempBasal else {
return nil
}
let tempBasalDurationDTO = TempBasalDurationDTO(
id: id,
timestamp: PumpEventStored.dateFormatter.string(from: timestamp),
duration: Int(tempBasal.duration)
)
return .tempBasalDuration(tempBasalDurationDTO)
}
func toPumpSuspendDTO() -> PumpEventDTO? {
guard let id = id, let timestamp = timestamp, let type = type, type == EventType.pumpSuspend.rawValue else {
return nil
}
let suspendDTO = SuspendDTO(
id: id,
timestamp: PumpEventStored.dateFormatter.string(from: timestamp)
)
return .suspend(suspendDTO)
}
func toPumpResumeDTO() -> PumpEventDTO? {
guard let id = id, let timestamp = timestamp, let type = type, type == EventType.pumpResume.rawValue else {
return nil
}
let resumeDTO = ResumeDTO(
id: id,
timestamp: PumpEventStored.dateFormatter.string(from: timestamp)
)
return .resume(resumeDTO)
}
func toRewindDTO() -> PumpEventDTO? {
guard let id = id, let timestamp = timestamp, let type = type, type == EventType.rewind.rawValue else {
return nil
}
let rewindDTO = RewindDTO(
id: id,
timestamp: PumpEventStored.dateFormatter.string(from: timestamp)
)
return .rewind(rewindDTO)
}
func toPrimeDTO() -> PumpEventDTO? {
guard let id = id, let timestamp = timestamp, let type = type, type == EventType.prime.rawValue else {
return nil
}
let primeDTO = PrimeDTO(
id: id,
timestamp: PumpEventStored.dateFormatter.string(from: timestamp)
)
return .prime(primeDTO)
}
}