Skip to content

Commit 89b1e04

Browse files
committed
Merge pull request #18 from loudnate/timer-tick-notification
Register for timer tick notifications
2 parents 9c64457 + dd01b4c commit 89b1e04

File tree

3 files changed

+19
-40
lines changed

3 files changed

+19
-40
lines changed

Cartfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ github "loudnate/LoopKit" ~> 0.3
22
github "loudnate/xDripG5" ~> 0.4
33
github "loudnate/SwiftCharts" "loudnate/naterade"
44
github "mddub/dexcom-share-client-swift" ~> 0.1
5-
github "ps2/rileylink_ios" ~> 0.3.2
5+
github "ps2/rileylink_ios" "timer-tick-notification" # ~> 0.3.2
66
github "mpurland/Amplitude-iOS" "framework"

Cartfile.resolved

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ github "loudnate/Crypto" "e0ef5b498f2c373d676135dabf5d1803b8558509"
33
github "loudnate/LoopKit" "v0.3.2"
44
github "loudnate/SwiftCharts" "dc8f5fbb29e391a65995d138158139559e365a1e"
55
github "mddub/dexcom-share-client-swift" "v0.1.2"
6-
github "ps2/rileylink_ios" "v0.3.2"
6+
github "ps2/rileylink_ios" "abf4d19055cd11624f0dc1706d222878af7beff8"
77
github "loudnate/xDripG5" "0.4.1"

Loop/Managers/DeviceDataManager.swift

Lines changed: 17 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -56,27 +56,11 @@ class DeviceDataManager: CarbStoreDelegate, TransmitterDelegate {
5656

5757
// MARK: - RileyLink
5858

59-
private var rileyLinkManagerObserver: AnyObject? {
60-
willSet {
61-
if let observer = rileyLinkManagerObserver {
62-
NSNotificationCenter.defaultCenter().removeObserver(observer)
63-
}
64-
}
65-
}
66-
67-
private var rileyLinkDevicePacketObserver: AnyObject? {
68-
willSet {
69-
if let observer = rileyLinkDevicePacketObserver {
70-
NSNotificationCenter.defaultCenter().removeObserver(observer)
71-
}
72-
}
73-
}
74-
75-
private func receivedRileyLinkManagerNotification(note: NSNotification) {
59+
@objc private func receivedRileyLinkManagerNotification(note: NSNotification) {
7660
NSNotificationCenter.defaultCenter().postNotificationName(note.name, object: self, userInfo: note.userInfo)
7761
}
7862

79-
private func receivedRileyLinkPacketNotification(note: NSNotification) {
63+
@objc private func receivedRileyLinkPacketNotification(note: NSNotification) {
8064
if let
8165
device = note.object as? RileyLinkDevice,
8266
data = note.userInfo?[RileyLinkDevice.IdleMessageDataKey] as? NSData,
@@ -98,6 +82,12 @@ class DeviceDataManager: CarbStoreDelegate, TransmitterDelegate {
9882
}
9983
}
10084

85+
@objc private func receivedRileyLinkTimerTickNotification(note: NSNotification) {
86+
assertCurrentPumpData()
87+
88+
backfillGlucoseFromShareIfNeeded()
89+
}
90+
10191
func connectToRileyLink(device: RileyLinkDevice) {
10292
connectedPeripheralIDs.insert(device.peripheral.identifier.UUIDString)
10393

@@ -194,11 +184,10 @@ class DeviceDataManager: CarbStoreDelegate, TransmitterDelegate {
194184
return
195185
}
196186

197-
// TODO: Allow RileyLinkManager to enable/disable idle listening
198187
device.assertIdleListening()
199188

200189
// How long should we wait before we poll for new reservoir data?
201-
let reservoirTolerance = sentryEnabled ? NSTimeInterval(minutes: 11) : NSTimeInterval(minutes: 1)
190+
let reservoirTolerance = rileyLinkManager.idleListeningEnabled ? NSTimeInterval(minutes: 11) : NSTimeInterval(minutes: 4)
202191

203192
// If we don't yet have reservoir data, or it's old, poll for it.
204193
if latestReservoirValue == nil || latestReservoirValue!.startDate.timeIntervalSinceNow <= -reservoirTolerance {
@@ -411,7 +400,7 @@ class DeviceDataManager: CarbStoreDelegate, TransmitterDelegate {
411400
}
412401
case "pumpModel"?:
413402
if let sentrySupported = pumpState?.pumpModel?.larger where !sentrySupported {
414-
sentryEnabled = false
403+
rileyLinkManager.idleListeningEnabled = false
415404
}
416405

417406
NSUserDefaults.standardUserDefaults().pumpModelNumber = pumpState?.pumpModel?.rawValue
@@ -515,9 +504,6 @@ class DeviceDataManager: CarbStoreDelegate, TransmitterDelegate {
515504
}
516505
}
517506

518-
/// Whether the RileyLink should listen for sentry packets.
519-
var sentryEnabled: Bool = true
520-
521507
// MARK: - CarbKit
522508

523509
let carbStore: CarbStore?
@@ -564,6 +550,8 @@ class DeviceDataManager: CarbStoreDelegate, TransmitterDelegate {
564550
insulinSensitivitySchedule: insulinSensitivitySchedule
565551
)
566552

553+
var idleListeningEnabled = true
554+
567555
if let pumpID = pumpID {
568556
let pumpState = PumpState(pumpID: pumpID)
569557

@@ -575,7 +563,7 @@ class DeviceDataManager: CarbStoreDelegate, TransmitterDelegate {
575563
if let model = PumpModel(rawValue: pumpModelNumber) {
576564
pumpState.pumpModel = model
577565

578-
sentryEnabled = model.larger
566+
idleListeningEnabled = model.larger
579567
}
580568
}
581569

@@ -586,6 +574,7 @@ class DeviceDataManager: CarbStoreDelegate, TransmitterDelegate {
586574
pumpState: self.pumpState,
587575
autoConnectIDs: connectedPeripheralIDs
588576
)
577+
rileyLinkManager.idleListeningEnabled = idleListeningEnabled
589578

590579
if let settings = NSBundle.mainBundle().remoteSettings,
591580
username = settings["ShareAccountName"],
@@ -597,14 +586,9 @@ class DeviceDataManager: CarbStoreDelegate, TransmitterDelegate {
597586
shareClient = nil
598587
}
599588

600-
rileyLinkManagerObserver = NSNotificationCenter.defaultCenter().addObserverForName(nil, object: rileyLinkManager, queue: nil) { [weak self] (note) -> Void in
601-
self?.receivedRileyLinkManagerNotification(note)
602-
}
603-
604-
// TODO: Use delegation instead.
605-
rileyLinkDevicePacketObserver = NSNotificationCenter.defaultCenter().addObserverForName(RileyLinkDevice.DidReceiveIdleMessageNotification, object: nil, queue: nil) { [weak self] (note) -> Void in
606-
self?.receivedRileyLinkPacketNotification(note)
607-
}
589+
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(receivedRileyLinkManagerNotification(_:)), name: nil, object: rileyLinkManager)
590+
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(receivedRileyLinkPacketNotification(_:)), name: RileyLinkDevice.DidReceiveIdleMessageNotification, object: nil)
591+
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(receivedRileyLinkTimerTickNotification(_:)), name: RileyLinkDevice.DidUpdateTimerTickNotification, object: nil)
608592

609593
if let pumpState = pumpState {
610594
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(pumpStateValuesDidChange(_:)), name: PumpState.ValuesDidChangeNotification, object: pumpState)
@@ -621,10 +605,5 @@ class DeviceDataManager: CarbStoreDelegate, TransmitterDelegate {
621605
transmitterID = NSUserDefaults.standardUserDefaults().transmitterID
622606
}
623607
}
624-
625-
deinit {
626-
rileyLinkManagerObserver = nil
627-
rileyLinkDevicePacketObserver = nil
628-
}
629608
}
630609

0 commit comments

Comments
 (0)