forked from LoopKit/LoopKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMockCGMManager.swift
More file actions
906 lines (759 loc) · 35 KB
/
MockCGMManager.swift
File metadata and controls
906 lines (759 loc) · 35 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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
//
// MockCGMManager.swift
// LoopKit
//
// Created by Michael Pangburn on 11/20/18.
// Copyright © 2018 LoopKit Authors. All rights reserved.
//
import HealthKit
import LoopKit
import LoopKitUI // TODO: DeviceStatusBadge references should live in MockKitUI
import LoopTestingKit
import UIKit
public struct MockCGMState: GlucoseDisplayable {
public var isStateValid: Bool
public var currentGlucose: HKQuantity?
public var trendType: GlucoseTrend?
public var trendRate: HKQuantity?
public var isLocal: Bool {
return true
}
public var glucoseRangeCategory: GlucoseRangeCategory?
public let unit: HKUnit = .milligramsPerDeciliter
public var glucoseAlertingEnabled: Bool
public var samplesShouldBeUploaded: Bool
private var cgmLowerLimitValue: Double
// HKQuantity isn't codable
public var cgmLowerLimit: HKQuantity {
get {
return HKQuantity.init(unit: unit, doubleValue: cgmLowerLimitValue)
}
set {
var newDoubleValue = newValue.doubleValue(for: unit)
if newDoubleValue >= urgentLowGlucoseThresholdValue {
newDoubleValue = urgentLowGlucoseThresholdValue - 1
}
cgmLowerLimitValue = newDoubleValue
}
}
private var urgentLowGlucoseThresholdValue: Double
public var urgentLowGlucoseThreshold: HKQuantity {
get {
return HKQuantity.init(unit: unit, doubleValue: urgentLowGlucoseThresholdValue)
}
set {
var newDoubleValue = newValue.doubleValue(for: unit)
if newDoubleValue <= cgmLowerLimitValue {
newDoubleValue = cgmLowerLimitValue + 1
}
if newDoubleValue >= lowGlucoseThresholdValue {
newDoubleValue = lowGlucoseThresholdValue - 1
}
urgentLowGlucoseThresholdValue = newDoubleValue
}
}
private var lowGlucoseThresholdValue: Double
public var lowGlucoseThreshold: HKQuantity {
get {
return HKQuantity.init(unit: unit, doubleValue: lowGlucoseThresholdValue)
}
set {
var newDoubleValue = newValue.doubleValue(for: unit)
if newDoubleValue <= urgentLowGlucoseThresholdValue {
newDoubleValue = urgentLowGlucoseThresholdValue + 1
}
if newDoubleValue >= highGlucoseThresholdValue {
newDoubleValue = highGlucoseThresholdValue - 1
}
lowGlucoseThresholdValue = newDoubleValue
}
}
private var highGlucoseThresholdValue: Double
public var highGlucoseThreshold: HKQuantity {
get {
return HKQuantity.init(unit: unit, doubleValue: highGlucoseThresholdValue)
}
set {
var newDoubleValue = newValue.doubleValue(for: unit)
if newDoubleValue <= lowGlucoseThresholdValue {
newDoubleValue = lowGlucoseThresholdValue + 1
}
if newDoubleValue >= cgmUpperLimitValue {
newDoubleValue = cgmUpperLimitValue - 1
}
highGlucoseThresholdValue = newDoubleValue
}
}
private var cgmUpperLimitValue: Double
public var cgmUpperLimit: HKQuantity {
get {
return HKQuantity.init(unit: unit, doubleValue: cgmUpperLimitValue)
}
set {
var newDoubleValue = newValue.doubleValue(for: unit)
if newDoubleValue <= highGlucoseThresholdValue {
newDoubleValue = highGlucoseThresholdValue + 1
}
cgmUpperLimitValue = newDoubleValue
}
}
public var cgmStatusHighlight: MockCGMStatusHighlight?
public var cgmStatusBadge: MockCGMStatusBadge?
public var cgmLifecycleProgress: MockCGMLifecycleProgress? {
didSet {
if cgmLifecycleProgress != oldValue {
setProgressColor()
}
}
}
public var progressWarningThresholdPercentValue: Double? {
didSet {
if progressWarningThresholdPercentValue != oldValue {
setProgressColor()
}
}
}
public var progressCriticalThresholdPercentValue: Double? {
didSet {
if progressCriticalThresholdPercentValue != oldValue {
setProgressColor()
}
}
}
public var cgmBatteryChargeRemaining: Double? = 1
private mutating func setProgressColor() {
guard var cgmLifecycleProgress = cgmLifecycleProgress else {
return
}
if let progressCriticalThresholdPercentValue = progressCriticalThresholdPercentValue,
cgmLifecycleProgress.percentComplete >= progressCriticalThresholdPercentValue
{
cgmLifecycleProgress.progressState = .critical
} else if let progressWarningThresholdPercentValue = progressWarningThresholdPercentValue,
cgmLifecycleProgress.percentComplete >= progressWarningThresholdPercentValue
{
cgmLifecycleProgress.progressState = .warning
} else {
cgmLifecycleProgress.progressState = .normalCGM
}
self.cgmLifecycleProgress = cgmLifecycleProgress
}
public init(isStateValid: Bool = true,
glucoseRangeCategory: GlucoseRangeCategory? = nil,
glucoseAlertingEnabled: Bool = false,
samplesShouldBeUploaded: Bool = false,
urgentLowGlucoseThresholdValue: Double = 55,
lowGlucoseThresholdValue: Double = 80,
highGlucoseThresholdValue: Double = 200,
cgmLowerLimitValue: Double = 40,
cgmUpperLimitValue: Double = 400,
cgmStatusHighlight: MockCGMStatusHighlight? = nil,
cgmLifecycleProgress: MockCGMLifecycleProgress? = nil,
progressWarningThresholdPercentValue: Double? = nil,
progressCriticalThresholdPercentValue: Double? = nil)
{
self.isStateValid = isStateValid
self.glucoseRangeCategory = glucoseRangeCategory
self.glucoseAlertingEnabled = glucoseAlertingEnabled
self.samplesShouldBeUploaded = samplesShouldBeUploaded
self.urgentLowGlucoseThresholdValue = urgentLowGlucoseThresholdValue
self.lowGlucoseThresholdValue = lowGlucoseThresholdValue
self.highGlucoseThresholdValue = highGlucoseThresholdValue
self.cgmLowerLimitValue = cgmLowerLimitValue
self.cgmUpperLimitValue = cgmUpperLimitValue
self.cgmStatusHighlight = cgmStatusHighlight
self.cgmLifecycleProgress = cgmLifecycleProgress
self.progressWarningThresholdPercentValue = progressWarningThresholdPercentValue
self.progressCriticalThresholdPercentValue = progressCriticalThresholdPercentValue
setProgressColor()
}
}
public struct MockCGMStatusHighlight: DeviceStatusHighlight {
public var localizedMessage: String
public var imageName: String {
switch alertIdentifier {
case MockCGMManager.submarine.identifier:
return "dot.radiowaves.left.and.right"
case MockCGMManager.buzz.identifier:
return "clock"
default:
return "exclamationmark.circle.fill"
}
}
public var state: DeviceStatusHighlightState{
switch alertIdentifier {
case MockCGMManager.submarine.identifier:
return .normalCGM
case MockCGMManager.buzz.identifier:
return .warning
default:
return .critical
}
}
public var alertIdentifier: Alert.AlertIdentifier
}
public struct MockCGMStatusBadge: DeviceStatusBadge {
public var image: UIImage? {
return badgeType.image
}
public var state: DeviceStatusBadgeState {
switch badgeType {
case .lowBattery:
return .critical
case .calibrationRequested:
return .warning
}
}
public var badgeType: MockCGMStatusBadgeType
public enum MockCGMStatusBadgeType: Int, CaseIterable {
case lowBattery
case calibrationRequested
var image: UIImage? {
switch self {
case .lowBattery:
return UIImage(frameworkImage: "battery.circle.fill")
case .calibrationRequested:
return UIImage(frameworkImage: "drop.circle.fill")
}
}
}
init(badgeType: MockCGMStatusBadgeType) {
self.badgeType = badgeType
}
}
public struct MockCGMLifecycleProgress: DeviceLifecycleProgress, Equatable {
public var percentComplete: Double
public var progressState: DeviceLifecycleProgressState
public init(percentComplete: Double, progressState: DeviceLifecycleProgressState = .normalCGM) {
self.percentComplete = percentComplete
self.progressState = progressState
}
}
extension MockCGMLifecycleProgress: RawRepresentable {
public typealias RawValue = [String: Any]
public init?(rawValue: RawValue) {
guard let percentComplete = rawValue["percentComplete"] as? Double,
let progressStateRawValue = rawValue["progressState"] as? DeviceLifecycleProgressState.RawValue,
let progressState = DeviceLifecycleProgressState(rawValue: progressStateRawValue) else
{
return nil
}
self.percentComplete = percentComplete
self.progressState = progressState
}
public var rawValue: RawValue {
let rawValue: RawValue = [
"percentComplete": percentComplete,
"progressState": progressState.rawValue,
]
return rawValue
}
}
public final class MockCGMManager: TestingCGMManager {
public static let pluginIdentifier: String = "MockCGMManager"
public static let localizedTitle = "CGM Simulator"
public var localizedTitle: String {
return MockCGMManager.localizedTitle
}
public struct MockAlert {
public let sound: Alert.Sound
public let identifier: Alert.AlertIdentifier
public let foregroundContent: Alert.Content
public let backgroundContent: Alert.Content
public let interruptionLevel: Alert.InterruptionLevel
}
let alerts: [Alert.AlertIdentifier: MockAlert] = [
submarine.identifier: submarine, buzz.identifier: buzz, critical.identifier: critical, signalLoss.identifier: signalLoss
]
public static let submarine = MockAlert(sound: .sound(name: "sub.caf"), identifier: "submarine",
foregroundContent: Alert.Content(title: "Alert: FG Title", body: "Alert: Foreground Body", acknowledgeActionButtonLabel: "FG OK"),
backgroundContent: Alert.Content(title: "Alert: BG Title", body: "Alert: Background Body", acknowledgeActionButtonLabel: "BG OK"),
interruptionLevel: .timeSensitive)
public static let critical = MockAlert(sound: .sound(name: "critical.caf"), identifier: "critical",
foregroundContent: Alert.Content(title: "Critical Alert: FG Title", body: "Critical Alert: Foreground Body", acknowledgeActionButtonLabel: "Critical FG OK"),
backgroundContent: Alert.Content(title: "Critical Alert: BG Title", body: "Critical Alert: Background Body", acknowledgeActionButtonLabel: "Critical BG OK"),
interruptionLevel: .critical)
public static let buzz = MockAlert(sound: .vibrate, identifier: "buzz",
foregroundContent: Alert.Content(title: "Alert: FG Title", body: "FG bzzzt", acknowledgeActionButtonLabel: "Buzz"),
backgroundContent: Alert.Content(title: "Alert: BG Title", body: "BG bzzzt", acknowledgeActionButtonLabel: "Buzz"),
interruptionLevel: .active)
public static let signalLoss = MockAlert(sound: .sound(name: "critical.caf"),
identifier: "signalLoss",
foregroundContent: Alert.Content(title: "Signal Loss", body: "CGM simulator signal loss", acknowledgeActionButtonLabel: "Dismiss"),
backgroundContent: Alert.Content(title: "Signal Loss", body: "CGM simulator signal loss", acknowledgeActionButtonLabel: "Dismiss"),
interruptionLevel: .critical)
private let lockedMockSensorState = Locked(MockCGMState(isStateValid: true))
public var mockSensorState: MockCGMState {
get {
lockedMockSensorState.value
}
set {
lockedMockSensorState.mutate { $0 = newValue }
self.notifyStatusObservers(cgmManagerStatus: self.cgmManagerStatus)
}
}
public var glucoseDisplay: GlucoseDisplayable? {
return mockSensorState
}
public var cgmManagerStatus: CGMManagerStatus {
return CGMManagerStatus(hasValidSensorSession: dataSource.isValidSession, lastCommunicationDate: lastCommunicationDate, device: device)
}
private var lastCommunicationDate: Date? = nil
public var testingDevice: HKDevice {
return MockCGMDataSource.device
}
public var device: HKDevice? {
return testingDevice
}
public weak var cgmManagerDelegate: CGMManagerDelegate? {
get {
return delegate.delegate
}
set {
delegate.delegate = newValue
}
}
public var delegateQueue: DispatchQueue! {
get {
return delegate.queue
}
set {
delegate.queue = newValue
}
}
private let delegate = WeakSynchronizedDelegate<CGMManagerDelegate>()
private let lockedDataSource = Locked(MockCGMDataSource(model: .noData))
public var dataSource: MockCGMDataSource {
get {
lockedDataSource.value
}
set {
lockedDataSource.mutate { $0 = newValue }
self.notifyStatusObservers(cgmManagerStatus: self.cgmManagerStatus)
}
}
private var glucoseUpdateTimer: Timer?
public init() {
setupGlucoseUpdateTimer()
}
// MARK: Handling CGM Manager Status observers
private var statusObservers = WeakSynchronizedSet<CGMManagerStatusObserver>()
public func addStatusObserver(_ observer: CGMManagerStatusObserver, queue: DispatchQueue) {
statusObservers.insert(observer, queue: queue)
}
public func removeStatusObserver(_ observer: CGMManagerStatusObserver) {
statusObservers.removeElement(observer)
}
private func notifyStatusObservers(cgmManagerStatus: CGMManagerStatus) {
delegate.notify { delegate in
delegate?.cgmManagerDidUpdateState(self)
delegate?.cgmManager(self, didUpdate: self.cgmManagerStatus)
}
statusObservers.forEach { observer in
observer.cgmManager(self, didUpdate: cgmManagerStatus)
}
}
public init?(rawState: RawStateValue) {
if let mockSensorStateRawValue = rawState["mockSensorState"] as? MockCGMState.RawValue,
let mockSensorState = MockCGMState(rawValue: mockSensorStateRawValue) {
self.lockedMockSensorState.value = mockSensorState
} else {
self.lockedMockSensorState.value = MockCGMState(isStateValid: true)
}
if let dataSourceRawValue = rawState["dataSource"] as? MockCGMDataSource.RawValue,
let dataSource = MockCGMDataSource(rawValue: dataSourceRawValue) {
self.lockedDataSource.value = dataSource
} else {
self.lockedDataSource.value = MockCGMDataSource(model: .sineCurve(parameters: (baseGlucose: HKQuantity(unit: .milligramsPerDeciliter, doubleValue: 110), amplitude: HKQuantity(unit: .milligramsPerDeciliter, doubleValue: 20), period: TimeInterval(hours: 6), referenceDate: Date())))
}
setupGlucoseUpdateTimer()
}
deinit {
glucoseUpdateTimer?.invalidate()
}
public var rawState: RawStateValue {
return [
"mockSensorState": mockSensorState.rawValue,
"dataSource": dataSource.rawValue
]
}
public let isOnboarded = true // No distinction between created and onboarded
public let appURL: URL? = nil
public let providesBLEHeartbeat = false
public let managedDataInterval: TimeInterval? = nil
public var shouldSyncToRemoteService: Bool {
return self.mockSensorState.samplesShouldBeUploaded
}
public var healthKitStorageDelayEnabled: Bool {
get {
MockCGMManager.healthKitStorageDelay == fixedHealthKitStorageDelay
}
set {
MockCGMManager.healthKitStorageDelay = newValue ? fixedHealthKitStorageDelay : 0
}
}
public let fixedHealthKitStorageDelay: TimeInterval = .minutes(2)
public static var healthKitStorageDelay: TimeInterval = 0
private func logDeviceComms(_ type: DeviceLogEntryType, message: String) {
self.delegate.delegate?.deviceManager(self, logEventForDeviceIdentifier: "mockcgm", type: type, message: message, completion: nil)
}
private func sendCGMReadingResult(_ result: CGMReadingResult) {
if case .newData(let samples) = result,
let currentValue = samples.first
{
mockSensorState.currentGlucose = currentValue.quantity
mockSensorState.trendType = currentValue.trend
mockSensorState.trendRate = currentValue.trendRate
mockSensorState.glucoseRangeCategory = glucoseRangeCategory(for: currentValue.quantitySample)
issueAlert(for: currentValue)
}
self.delegate.notify { delegate in
delegate?.cgmManager(self, hasNew: result)
}
}
public func glucoseRangeCategory(for glucose: GlucoseSampleValue) -> GlucoseRangeCategory? {
switch glucose.quantity {
case ...mockSensorState.cgmLowerLimit:
return glucose.wasUserEntered ? .urgentLow : .belowRange
case mockSensorState.cgmLowerLimit..<mockSensorState.urgentLowGlucoseThreshold:
return .urgentLow
case mockSensorState.urgentLowGlucoseThreshold..<mockSensorState.lowGlucoseThreshold:
return .low
case mockSensorState.lowGlucoseThreshold..<mockSensorState.highGlucoseThreshold:
return .normal
case mockSensorState.highGlucoseThreshold..<mockSensorState.cgmUpperLimit:
return .high
default:
return glucose.wasUserEntered ? .high : .aboveRange
}
}
public func fetchNewDataIfNeeded(_ completion: @escaping (CGMReadingResult) -> Void) {
let now = Date()
logDeviceComms(.send, message: "Fetch new data")
dataSource.fetchNewData { (result) in
switch result {
case .error(let error):
self.logDeviceComms(.error, message: "Error fetching new data: \(error)")
case .newData(let samples):
self.lastCommunicationDate = now
self.logDeviceComms(.receive, message: "New data received: \(samples)")
case .unreliableData:
self.lastCommunicationDate = now
self.logDeviceComms(.receive, message: "Unreliable data received")
case .noData:
self.lastCommunicationDate = now
self.logDeviceComms(.receive, message: "No new data")
}
completion(result)
}
}
public func backfillData(datingBack duration: TimeInterval) {
let now = Date()
self.logDeviceComms(.send, message: "backfillData(\(duration))")
dataSource.backfillData(from: DateInterval(start: now.addingTimeInterval(-duration), end: now)) { result in
switch result {
case .error(let error):
self.logDeviceComms(.error, message: "Backfill error: \(error)")
case .newData(let samples):
self.logDeviceComms(.receive, message: "Backfill data: \(samples)")
case .unreliableData:
self.logDeviceComms(.receive, message: "Backfill data unreliable")
case .noData:
self.logDeviceComms(.receive, message: "Backfill empty")
}
self.sendCGMReadingResult(result)
}
}
public func updateGlucoseUpdateTimer() {
glucoseUpdateTimer?.invalidate()
setupGlucoseUpdateTimer()
}
private func setupGlucoseUpdateTimer() {
glucoseUpdateTimer = Timer.scheduledTimer(withTimeInterval: dataSource.dataPointFrequency.frequency, repeats: true) { [weak self] _ in
guard let self = self else { return }
self.fetchNewDataIfNeeded() { result in
self.sendCGMReadingResult(result)
}
}
}
public func injectGlucoseSamples(_ pastSamples: [NewGlucoseSample], futureSamples: [NewGlucoseSample]) {
guard !pastSamples.isEmpty else { return }
sendCGMReadingResult(CGMReadingResult.newData(pastSamples.map { NewGlucoseSample($0, device: device) } ))
if !futureSamples.isEmpty {
dataSource.model = .scenario(pastSamples: pastSamples, futureSamples: futureSamples)
}
}
public func trigger(action: DeviceAction) {}
public func acceptDefaultsAndSkipOnboarding() {
// TODO: Unimplemented as it's not needed for HF. Ticket to complete below.
// https://tidepool.atlassian.net/browse/LOOP-4598
}
}
fileprivate extension NewGlucoseSample {
init(_ other: NewGlucoseSample, device: HKDevice?) {
self.init(date: other.date,
quantity: other.quantity,
condition: other.condition,
trend: other.trend,
trendRate: other.trendRate,
isDisplayOnly: other.isDisplayOnly,
wasUserEntered: other.wasUserEntered,
syncIdentifier: other.syncIdentifier,
syncVersion: other.syncVersion,
device: device)
}
}
// MARK: Alert Stuff
extension MockCGMManager {
public func getSoundBaseURL() -> URL? {
return Bundle(for: type(of: self)).bundleURL
}
public func getSounds() -> [Alert.Sound] {
return alerts.map { $1.sound }
}
public var hasRetractableAlert: Bool {
// signal loss alerts can only be removed by switching the CGM data source
return currentAlertIdentifier != nil && currentAlertIdentifier != MockCGMManager.signalLoss.identifier
}
public var currentAlertIdentifier: Alert.AlertIdentifier? {
return mockSensorState.cgmStatusHighlight?.alertIdentifier
}
public func issueAlert(identifier: Alert.AlertIdentifier, trigger: Alert.Trigger, delay: TimeInterval?, metadata: Alert.Metadata? = nil) {
guard let alert = alerts[identifier] else {
return
}
delegate.notifyDelayed(by: delay ?? 0) { delegate in
self.logDeviceComms(.delegate, message: "\(#function): \(identifier) \(trigger)")
delegate?.issueAlert(Alert(identifier: Alert.Identifier(managerIdentifier: self.pluginIdentifier, alertIdentifier: identifier),
foregroundContent: alert.foregroundContent,
backgroundContent: alert.backgroundContent,
trigger: trigger,
interruptionLevel: alert.interruptionLevel,
sound: alert.sound,
metadata: metadata))
}
// updating the status highlight
setStatusHighlight(MockCGMStatusHighlight(localizedMessage: alert.foregroundContent.title, alertIdentifier: alert.identifier))
}
public func issueSignalLossAlert() {
issueAlert(identifier: MockCGMManager.signalLoss.identifier, trigger: .immediate, delay: nil)
}
public func retractSignalLossAlert() {
retractAlert(identifier: MockCGMManager.signalLoss.identifier)
}
public func acknowledgeAlert(alertIdentifier: Alert.AlertIdentifier, completion: @escaping (Error?) -> Void) {
self.logDeviceComms(.delegateResponse, message: "\(#function): Alert \(alertIdentifier) acknowledged.")
completion(nil)
}
public func retractCurrentAlert() {
guard hasRetractableAlert, let identifier = currentAlertIdentifier else { return }
retractAlert(identifier: identifier)
}
public func retractAlert(identifier: Alert.AlertIdentifier) {
delegate.notify { $0?.retractAlert(identifier: Alert.Identifier(managerIdentifier: self.pluginIdentifier, alertIdentifier: identifier)) }
// updating the status highlight
if mockSensorState.cgmStatusHighlight?.alertIdentifier == identifier {
setStatusHighlight(nil)
}
}
public func setStatusHighlight(_ statusHighlight: MockCGMStatusHighlight?) {
mockSensorState.cgmStatusHighlight = statusHighlight
if statusHighlight == nil,
case .signalLoss = dataSource.model
{
// restore signal loss status highlight
issueSignalLossAlert()
}
}
private func issueAlert(for glucose: NewGlucoseSample) {
guard mockSensorState.glucoseAlertingEnabled else {
return
}
let alertTitle: String
let glucoseAlertIdentifier: String
let interruptionLevel: Alert.InterruptionLevel
switch glucose.quantity {
case ...mockSensorState.urgentLowGlucoseThreshold:
alertTitle = "Urgent Low Glucose Alert"
glucoseAlertIdentifier = "glucose.value.low.urgent"
interruptionLevel = .critical
case mockSensorState.urgentLowGlucoseThreshold..<mockSensorState.lowGlucoseThreshold:
alertTitle = "Low Glucose Alert"
glucoseAlertIdentifier = "glucose.value.low"
interruptionLevel = .timeSensitive
case mockSensorState.highGlucoseThreshold...:
alertTitle = "High Glucose Alert"
glucoseAlertIdentifier = "glucose.value.high"
interruptionLevel = .timeSensitive
default:
return
}
let alertIdentifier = Alert.Identifier(managerIdentifier: self.pluginIdentifier,
alertIdentifier: glucoseAlertIdentifier)
let alertContent = Alert.Content(title: alertTitle,
body: "The glucose measurement received triggered this alert",
acknowledgeActionButtonLabel: "Dismiss")
let alert = Alert(identifier: alertIdentifier,
foregroundContent: alertContent,
backgroundContent: alertContent,
trigger: .immediate,
interruptionLevel: interruptionLevel)
delegate.notify { delegate in
delegate?.issueAlert(alert)
}
}
}
//MARK: Device Status Badge stuff
extension MockCGMManager {
public func requestCalibration(_ requestCalibration: Bool) {
mockSensorState.cgmStatusBadge = requestCalibration ? MockCGMStatusBadge(badgeType: .calibrationRequested) : nil
checkAndSetBatteryBadge()
}
public var cgmBatteryChargeRemaining: Double? {
get {
return mockSensorState.cgmBatteryChargeRemaining
}
set {
mockSensorState.cgmBatteryChargeRemaining = newValue
checkAndSetBatteryBadge()
}
}
public var isCalibrationRequested: Bool {
return mockSensorState.cgmStatusBadge?.badgeType == .calibrationRequested
}
private func checkAndSetBatteryBadge() {
// calibration badge is the highest priority
guard mockSensorState.cgmStatusBadge?.badgeType != .calibrationRequested else {
return
}
guard let cgmBatteryChargeRemaining = mockSensorState.cgmBatteryChargeRemaining,
cgmBatteryChargeRemaining > 0.5 else
{
mockSensorState.cgmStatusBadge = MockCGMStatusBadge(badgeType: .lowBattery)
return
}
mockSensorState.cgmStatusBadge = nil
}
}
extension MockCGMManager {
public var debugDescription: String {
return """
## MockCGMManager
state: \(mockSensorState)
dataSource: \(dataSource)
"""
}
}
extension MockCGMState: RawRepresentable {
public typealias RawValue = [String: Any]
public init?(rawValue: RawValue) {
guard let isStateValid = rawValue["isStateValid"] as? Bool,
let glucoseAlertingEnabled = rawValue["glucoseAlertingEnabled"] as? Bool,
let urgentLowGlucoseThresholdValue = rawValue["urgentLowGlucoseThresholdValue"] as? Double,
let lowGlucoseThresholdValue = rawValue["lowGlucoseThresholdValue"] as? Double,
let highGlucoseThresholdValue = rawValue["highGlucoseThresholdValue"] as? Double,
let cgmLowerLimitValue = rawValue["cgmLowerLimitValue"] as? Double,
let cgmUpperLimitValue = rawValue["cgmUpperLimitValue"] as? Double else
{
return nil
}
self.isStateValid = isStateValid
self.glucoseAlertingEnabled = glucoseAlertingEnabled
self.samplesShouldBeUploaded = rawValue["samplesShouldBeUploaded"] as? Bool ?? false
self.urgentLowGlucoseThresholdValue = urgentLowGlucoseThresholdValue
self.lowGlucoseThresholdValue = lowGlucoseThresholdValue
self.highGlucoseThresholdValue = highGlucoseThresholdValue
self.cgmLowerLimitValue = cgmLowerLimitValue
self.cgmUpperLimitValue = cgmUpperLimitValue
if let glucoseRangeCategoryRawValue = rawValue["glucoseRangeCategory"] as? GlucoseRangeCategory.RawValue {
self.glucoseRangeCategory = GlucoseRangeCategory(rawValue: glucoseRangeCategoryRawValue)
}
if let localizedMessage = rawValue["localizedMessage"] as? String,
let alertIdentifier = rawValue["alertIdentifier"] as? Alert.AlertIdentifier
{
self.cgmStatusHighlight = MockCGMStatusHighlight(localizedMessage: localizedMessage, alertIdentifier: alertIdentifier)
}
if let statusBadgeTypeRawValue = rawValue["statusBadgeType"] as? MockCGMStatusBadge.MockCGMStatusBadgeType.RawValue,
let statusBadgeType = MockCGMStatusBadge.MockCGMStatusBadgeType(rawValue: statusBadgeTypeRawValue)
{
self.cgmStatusBadge = MockCGMStatusBadge(badgeType: statusBadgeType)
}
if let cgmLifecycleProgressRawValue = rawValue["cgmLifecycleProgress"] as? MockCGMLifecycleProgress.RawValue {
self.cgmLifecycleProgress = MockCGMLifecycleProgress(rawValue: cgmLifecycleProgressRawValue)
}
self.progressWarningThresholdPercentValue = rawValue["progressWarningThresholdPercentValue"] as? Double
self.progressCriticalThresholdPercentValue = rawValue["progressCriticalThresholdPercentValue"] as? Double
self.cgmBatteryChargeRemaining = rawValue["cgmBatteryChargeRemaining"] as? Double
if let trendRateValue = rawValue["trendRateValue"] as? Double {
self.trendRate = HKQuantity(unit: .milligramsPerDeciliterPerMinute, doubleValue: trendRateValue)
}
if let trendTypeRaw = rawValue["trendType"] as? GlucoseTrend.RawValue {
self.trendType = GlucoseTrend(rawValue: trendTypeRaw)
}
if let currentGlucoseValue = rawValue["currentGlucoseValue"] as? Double {
self.currentGlucose = HKQuantity(unit: .milligramsPerDeciliter, doubleValue: currentGlucoseValue)
}
setProgressColor()
}
public var rawValue: RawValue {
var rawValue: RawValue = [
"isStateValid": isStateValid,
"glucoseAlertingEnabled": glucoseAlertingEnabled,
"samplesShouldBeUploaded": samplesShouldBeUploaded,
"urgentLowGlucoseThresholdValue": urgentLowGlucoseThresholdValue,
"lowGlucoseThresholdValue": lowGlucoseThresholdValue,
"highGlucoseThresholdValue": highGlucoseThresholdValue,
"cgmLowerLimitValue": cgmLowerLimitValue,
"cgmUpperLimitValue": cgmUpperLimitValue,
]
if let glucoseRangeCategory = glucoseRangeCategory {
rawValue["glucoseRangeCategory"] = glucoseRangeCategory.rawValue
}
if let cgmStatusHighlight = cgmStatusHighlight {
rawValue["localizedMessage"] = cgmStatusHighlight.localizedMessage
rawValue["alertIdentifier"] = cgmStatusHighlight.alertIdentifier
}
if let cgmStatusBadgeType = cgmStatusBadge?.badgeType {
rawValue["statusBadgeType"] = cgmStatusBadgeType.rawValue
}
if let cgmLifecycleProgress = cgmLifecycleProgress {
rawValue["cgmLifecycleProgress"] = cgmLifecycleProgress.rawValue
}
if let progressWarningThresholdPercentValue = progressWarningThresholdPercentValue {
rawValue["progressWarningThresholdPercentValue"] = progressWarningThresholdPercentValue
}
if let progressCriticalThresholdPercentValue = progressCriticalThresholdPercentValue {
rawValue["progressCriticalThresholdPercentValue"] = progressCriticalThresholdPercentValue
}
if let cgmBatteryChargeRemaining = cgmBatteryChargeRemaining {
rawValue["cgmBatteryChargeRemaining"] = cgmBatteryChargeRemaining
}
rawValue["trendRateValue"] = trendRate?.doubleValue(for: .milligramsPerDeciliterPerMinute)
rawValue["trendType"] = trendType?.rawValue
rawValue["currentGlucoseValue"] = currentGlucose?.doubleValue(for: .milligramsPerDeciliter)
return rawValue
}
}
extension MockCGMState: CustomDebugStringConvertible {
public var debugDescription: String {
return """
## MockCGMState
* isStateValid: \(isStateValid)
* glucoseAlertingEnabled: \(glucoseAlertingEnabled)
* samplesShouldBeUploaded: \(samplesShouldBeUploaded)
* urgentLowGlucoseThresholdValue: \(urgentLowGlucoseThresholdValue)
* lowGlucoseThresholdValue: \(lowGlucoseThresholdValue)
* highGlucoseThresholdValue: \(highGlucoseThresholdValue)
* cgmLowerLimitValue: \(cgmLowerLimitValue)
* cgmUpperLimitValue: \(cgmUpperLimitValue)
* highGlucoseThresholdValue: \(highGlucoseThresholdValue)
* glucoseRangeCategory: \(glucoseRangeCategory as Any)
* cgmStatusHighlight: \(cgmStatusHighlight as Any)
* cgmStatusBadge: \(cgmStatusBadge as Any)
* cgmLifecycleProgress: \(cgmLifecycleProgress as Any)
* progressWarningThresholdPercentValue: \(progressWarningThresholdPercentValue as Any)
* progressCriticalThresholdPercentValue: \(progressCriticalThresholdPercentValue as Any)
* cgmBatteryChargeRemaining: \(cgmBatteryChargeRemaining as Any)
"""
}
}