             carbRatioSchedule: carbRatioSchedule,
             insulinSensitivitySchedule: insulinSensitivitySchedule
         )
-        
+
         var idleListeningEnabled = true
-        
+
         if let pumpID = pumpID {
             let pumpState = PumpState(pumpID: pumpID)
-            
+
             if let timeZone = NSUserDefaults.standardUserDefaults().pumpTimeZone {
                 pumpState.timeZone = timeZone
             }
-            
+
             if let pumpModelNumber = NSUserDefaults.standardUserDefaults().pumpModelNumber {
                 if let model = PumpModel(rawValue: pumpModelNumber) {
                     pumpState.pumpModel = model
-                    
+
                     idleListeningEnabled = model.larger
                 }
             }
-            
+
             self.pumpState = pumpState
         }
-        
+
         rileyLinkManager = RileyLinkDeviceManager(
             pumpState: self.pumpState,
             autoConnectIDs: connectedPeripheralIDs
         )
         rileyLinkManager.idleListeningEnabled = idleListeningEnabled
-        
+
         if let  settings = NSBundle.mainBundle().remoteSettings,
             username = settings["ShareAccountName"],
             password = settings["ShareAccountPassword"]

            }
        }
    }
    
    @objc private func receivedRileyLinkTimerTickNotification(note: NSNotification) {
        assertCurrentPumpData()
        
        backfillGlucoseFromShareIfNeeded()
    }
    
    func connectToRileyLink(device: RileyLinkDevice) {
        connectedPeripheralIDs.insert(device.peripheral.identifier.UUIDString)
        
        rileyLinkManager.connectDevice(device)
        
        AnalyticsManager.didChangeRileyLinkConnectionState()
    }
    
    func disconnectFromRileyLink(device: RileyLinkDevice) {
        connectedPeripheralIDs.remove(device.peripheral.identifier.UUIDString)
        
        rileyLinkManager.disconnectDevice(device)
        
        AnalyticsManager.didChangeRileyLinkConnectionState()
    }
    
    // MARK: Pump data
    
    var latestPumpStatus: MySentryPumpStatusMessageBody?
    
    var latestReservoirValue: ReservoirValue?
    
    /**
     Handles receiving a MySentry status message, which are only posted by MM x23 pumps.
     
     This message has two important pieces of info about the pump: reservoir volume and battery.
     
     Because the RileyLink must actively listen for these packets, they are not the most reliable heartbeat. However, we can still use them to assert glucose data is current.
     
     - parameter status: The status message body
     - parameter device: The RileyLink that received the message
     */
    private func updatePumpStatus(status: MySentryPumpStatusMessageBody, fromDevice device: RileyLinkDevice) {
        status.pumpDateComponents.timeZone = pumpState?.timeZone
        
        // The pump sends the same message 3x, so ignore it if we've already seen it.
        guard status != latestPumpStatus, let pumpDate = status.pumpDateComponents.date else {
            return
        }
        
        latestPumpStatus = status
        
        backfillGlucoseFromShareIfNeeded()
        
        // Sentry packets are sent in groups of 3, 5s apart. Wait 11s before allowing the loop data to continue to avoid conflicting comms.
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(11 * NSEC_PER_SEC)), dispatch_get_global_queue(QOS_CLASS_UTILITY, 0)) {
            self.updateReservoirVolume(status.reservoirRemainingUnits, atDate: pumpDate, withTimeLeft: NSTimeInterval(minutes: Double(status.reservoirRemainingMinutes)))
        }
        
        // Check for an empty battery. Sentry packets are still broadcast for a few hours after this value reaches 0.
        if status.batteryRemainingPercent == 0 {
            NotificationManager.sendPumpBatteryLowNotification()
        }
        
        nightscoutUploader?.handlePumpStatus(status, device: device.deviceURI)
    }
    
    /**
     Store a new reservoir volume and notify observers of new pump data.
     
     - parameter units:    The number of units remaining
     - parameter date:     The date the reservoir was read
     - parameter timeLeft: The approximate time before the reservoir is empty

            guard newValue?.characters.count == 6 && newValue != pumpState?.pumpID else {
                return
            }

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

                if let timeZone = self.pumpState?.timeZone {
                    pumpState.timeZone = timeZone
                }

                self.pumpState = pumpState
            } else {
                self.pumpState = nil
            }

            nightscoutUploader?.pumpID = pumpID

            doseStore.pumpID = pumpID

            NSUserDefaults.standardUserDefaults().pumpID = pumpID
        }
    }

    var pumpState: PumpState? {
        didSet {
            rileyLinkManager.pumpState = pumpState

            if let oldValue = oldValue {
                NSNotificationCenter.defaultCenter().removeObserver(self, name: PumpState.ValuesDidChangeNotification, object: oldValue)
            }

            if let pumpState = pumpState {
                NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(pumpStateValuesDidChange(_:)), name: PumpState.ValuesDidChangeNotification, object: pumpState)
            }
        }
    }

    @objc private func pumpStateValuesDidChange(note: NSNotification) {
        switch note.userInfo?[PumpState.PropertyKey] as? String {
        case "timeZone"?:
            NSUserDefaults.standardUserDefaults().pumpTimeZone = pumpState?.timeZone

            if let pumpTimeZone = pumpState?.timeZone {
                if let basalRateSchedule = basalRateSchedule {
                    self.basalRateSchedule = BasalRateSchedule(dailyItems: basalRateSchedule.items, timeZone: pumpTimeZone)
                }

                if let carbRatioSchedule = carbRatioSchedule {
                    self.carbRatioSchedule = CarbRatioSchedule(unit: carbRatioSchedule.unit, dailyItems: carbRatioSchedule.items, timeZone: pumpTimeZone)
                }

                if let insulinSensitivitySchedule = insulinSensitivitySchedule {
                    self.insulinSensitivitySchedule = InsulinSensitivitySchedule(unit: insulinSensitivitySchedule.unit, dailyItems: insulinSensitivitySchedule.items, timeZone: pumpTimeZone)
                }

                if let glucoseTargetRangeSchedule = glucoseTargetRangeSchedule {
                    self.glucoseTargetRangeSchedule = GlucoseRangeSchedule(unit: glucoseTargetRangeSchedule.unit, dailyItems: glucoseTargetRangeSchedule.items, timeZone: pumpTimeZone)
                }
