Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cartfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ github "loudnate/LoopKit" ~> 0.3
github "loudnate/xDripG5" ~> 0.4
github "loudnate/SwiftCharts" "loudnate/naterade"
github "mddub/dexcom-share-client-swift" ~> 0.1
github "ps2/rileylink_ios" ~> 0.3.2
github "ps2/rileylink_ios" "timer-tick-notification" # ~> 0.3.2
github "mpurland/Amplitude-iOS" "framework"
2 changes: 1 addition & 1 deletion Cartfile.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ github "loudnate/Crypto" "e0ef5b498f2c373d676135dabf5d1803b8558509"
github "loudnate/LoopKit" "v0.3.2"
github "loudnate/SwiftCharts" "dc8f5fbb29e391a65995d138158139559e365a1e"
github "mddub/dexcom-share-client-swift" "v0.1.2"
github "ps2/rileylink_ios" "v0.3.2"
github "ps2/rileylink_ios" "abf4d19055cd11624f0dc1706d222878af7beff8"
github "loudnate/xDripG5" "0.4.1"
55 changes: 17 additions & 38 deletions Loop/Managers/DeviceDataManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,27 +56,11 @@ class DeviceDataManager: CarbStoreDelegate, TransmitterDelegate {

// MARK: - RileyLink

private var rileyLinkManagerObserver: AnyObject? {
willSet {
if let observer = rileyLinkManagerObserver {
NSNotificationCenter.defaultCenter().removeObserver(observer)
}
}
}

private var rileyLinkDevicePacketObserver: AnyObject? {
willSet {
if let observer = rileyLinkDevicePacketObserver {
NSNotificationCenter.defaultCenter().removeObserver(observer)
}
}
}

private func receivedRileyLinkManagerNotification(note: NSNotification) {
@objc private func receivedRileyLinkManagerNotification(note: NSNotification) {
NSNotificationCenter.defaultCenter().postNotificationName(note.name, object: self, userInfo: note.userInfo)
}

private func receivedRileyLinkPacketNotification(note: NSNotification) {
@objc private func receivedRileyLinkPacketNotification(note: NSNotification) {
if let
device = note.object as? RileyLinkDevice,
data = note.userInfo?[RileyLinkDevice.IdleMessageDataKey] as? NSData,
Expand All @@ -98,6 +82,12 @@ class DeviceDataManager: CarbStoreDelegate, TransmitterDelegate {
}
}

@objc private func receivedRileyLinkTimerTickNotification(note: NSNotification) {
assertCurrentPumpData()

backfillGlucoseFromShareIfNeeded()
}

func connectToRileyLink(device: RileyLinkDevice) {
connectedPeripheralIDs.insert(device.peripheral.identifier.UUIDString)

Expand Down Expand Up @@ -194,11 +184,10 @@ class DeviceDataManager: CarbStoreDelegate, TransmitterDelegate {
return
}

// TODO: Allow RileyLinkManager to enable/disable idle listening
device.assertIdleListening()

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

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

NSUserDefaults.standardUserDefaults().pumpModelNumber = pumpState?.pumpModel?.rawValue
Expand Down Expand Up @@ -515,9 +504,6 @@ class DeviceDataManager: CarbStoreDelegate, TransmitterDelegate {
}
}

/// Whether the RileyLink should listen for sentry packets.
var sentryEnabled: Bool = true

// MARK: - CarbKit

let carbStore: CarbStore?
Expand Down Expand Up @@ -564,6 +550,8 @@ class DeviceDataManager: CarbStoreDelegate, TransmitterDelegate {
insulinSensitivitySchedule: insulinSensitivitySchedule
)

var idleListeningEnabled = true

if let pumpID = pumpID {
let pumpState = PumpState(pumpID: pumpID)

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

sentryEnabled = model.larger
idleListeningEnabled = model.larger
}
}

Expand All @@ -586,6 +574,7 @@ class DeviceDataManager: CarbStoreDelegate, TransmitterDelegate {
pumpState: self.pumpState,
autoConnectIDs: connectedPeripheralIDs
)
rileyLinkManager.idleListeningEnabled = idleListeningEnabled

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

rileyLinkManagerObserver = NSNotificationCenter.defaultCenter().addObserverForName(nil, object: rileyLinkManager, queue: nil) { [weak self] (note) -> Void in
self?.receivedRileyLinkManagerNotification(note)
}

// TODO: Use delegation instead.
rileyLinkDevicePacketObserver = NSNotificationCenter.defaultCenter().addObserverForName(RileyLinkDevice.DidReceiveIdleMessageNotification, object: nil, queue: nil) { [weak self] (note) -> Void in
self?.receivedRileyLinkPacketNotification(note)
}
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(receivedRileyLinkManagerNotification(_:)), name: nil, object: rileyLinkManager)
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(receivedRileyLinkPacketNotification(_:)), name: RileyLinkDevice.DidReceiveIdleMessageNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(receivedRileyLinkTimerTickNotification(_:)), name: RileyLinkDevice.DidUpdateTimerTickNotification, object: nil)

if let pumpState = pumpState {
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(pumpStateValuesDidChange(_:)), name: PumpState.ValuesDidChangeNotification, object: pumpState)
Expand All @@ -621,10 +605,5 @@ class DeviceDataManager: CarbStoreDelegate, TransmitterDelegate {
transmitterID = NSUserDefaults.standardUserDefaults().transmitterID
}
}

deinit {
rileyLinkManagerObserver = nil
rileyLinkDevicePacketObserver = nil
}
}