             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"]
+            username = settings["ShareAccountName"],
+            password = settings["ShareAccountPassword"]
             where !username.isEmpty && !password.isEmpty
         {
             shareClient = ShareClient(username: username, password: password)
         } else {
             shareClient = nil
         }
-
+        
+        if let  settings = NSBundle.mainBundle().remoteSettings,
+            siteURL = settings["NightscoutSiteURL"],
+            APISecret = settings["NightscoutAPISecret"]
+        {
+            nightscoutUploader = NightscoutUploader()
+            nightscoutUploader!.siteURL = siteURL
+            nightscoutUploader!.APISecret = APISecret
+        } else {
+            nightscoutUploader = nil
+        }
+        
         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)
         }
-
+        
         loopManager = LoopDataManager(deviceDataManager: self)
         NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(loopDataDidUpdateNotification(_:)), name: LoopDataManager.LoopDataUpdatedNotification, object: loopManager)
-
+        
         watchManager = WatchDataManager(deviceDataManager: self)
-
+        
         carbStore?.delegate = self
-
+        
         defer {
             transmitterID = NSUserDefaults.standardUserDefaults().transmitterID
         }

            case (.NeedsConfiguration, .None):
                break
            }

            NSUserDefaults.standardUserDefaults().transmitterID = transmitterID
        }
    }

    // MARK: Loop model inputs

    var basalRateSchedule: BasalRateSchedule? = NSUserDefaults.standardUserDefaults().basalRateSchedule {
        didSet {
            doseStore.basalProfile = basalRateSchedule

            NSUserDefaults.standardUserDefaults().basalRateSchedule = basalRateSchedule

            AnalyticsManager.didChangeBasalRateSchedule()
        }
    }

    var carbRatioSchedule: CarbRatioSchedule? = NSUserDefaults.standardUserDefaults().carbRatioSchedule {
        didSet {
            carbStore?.carbRatioSchedule = carbRatioSchedule

            NSUserDefaults.standardUserDefaults().carbRatioSchedule = carbRatioSchedule

            AnalyticsManager.didChangeCarbRatioSchedule()
        }
    }

    var insulinActionDuration: NSTimeInterval? = NSUserDefaults.standardUserDefaults().insulinActionDuration {
        didSet {
            doseStore.insulinActionDuration = insulinActionDuration

            NSUserDefaults.standardUserDefaults().insulinActionDuration = insulinActionDuration

            if oldValue != insulinActionDuration {
                AnalyticsManager.didChangeInsulinActionDuration()
            }
        }
    }

    var insulinSensitivitySchedule: InsulinSensitivitySchedule? = NSUserDefaults.standardUserDefaults().insulinSensitivitySchedule {
        didSet {
            carbStore?.insulinSensitivitySchedule = insulinSensitivitySchedule
            doseStore.insulinSensitivitySchedule = insulinSensitivitySchedule

            NSUserDefaults.standardUserDefaults().insulinSensitivitySchedule = insulinSensitivitySchedule

            AnalyticsManager.didChangeInsulinSensitivitySchedule()
        }
    }

    var glucoseTargetRangeSchedule: GlucoseRangeSchedule? = NSUserDefaults.standardUserDefaults().glucoseTargetRangeSchedule {
        didSet {
            NSUserDefaults.standardUserDefaults().glucoseTargetRangeSchedule = glucoseTargetRangeSchedule

            AnalyticsManager.didChangeGlucoseTargetRangeSchedule()
        }
    }

    var maximumBasalRatePerHour: Double? = NSUserDefaults.standardUserDefaults().maximumBasalRatePerHour {
        didSet {
            NSUserDefaults.standardUserDefaults().maximumBasalRatePerHour = maximumBasalRatePerHour

            AnalyticsManager.didChangeMaximumBasalRate()
        }
    }

    var maximumBolus: Double? = NSUserDefaults.standardUserDefaults().maximumBolus {
        didSet {
            NSUserDefaults.standardUserDefaults().maximumBolus = maximumBolus

            AnalyticsManager.didChangeMaximumBolus()
        }
    }

    // MARK: - CarbKit

    let carbStore: CarbStore?

    // MARK: CarbStoreDelegate

    func carbStore(_: CarbStore, didError error: CarbStore.Error) {
        logger?.addError(error, fromSource: "CarbStore")
    }

    // MARK: - GlucoseKit

    let glucoseStore: GlucoseStore? = GlucoseStore()

    // MARK: - InsulinKit

    let doseStore: DoseStore

    // MARK: - WatchKit

    private(set) var watchManager: WatchDataManager!

    @objc private func loopDataDidUpdateNotification(_: NSNotification) {
        watchManager.updateWatch()
    }

    // MARK: - Initialization

    static let sharedManager = DeviceDataManager()

    private(set) var loopManager: LoopDataManager!

    init() {
        let pumpID = NSUserDefaults.standardUserDefaults().pumpID

        doseStore = DoseStore(
            pumpID: pumpID,
            insulinActionDuration: insulinActionDuration,

            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
            }
            
            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)
                }
