Jon Fawcett 6 лет назад
Родитель
Сommit
34c6fce105

+ 4 - 4
LoopFollow/Controllers/Graphs.swift

@@ -46,7 +46,7 @@ extension MainViewController {
         }
         }
         
         
         // Add Prediction Data
         // Add Prediction Data
-        if predictionData.count > 0 && bgData.count > 0 {
+        if predictionData.count > 0 && bgData.count > 0 && UserDefaultsRepository.graphPrediction.value {
             var startingTime = bgChartEntry[bgChartEntry.count - 1].x + 300
             var startingTime = bgChartEntry[bgChartEntry.count - 1].x + 300
             var i = 0
             var i = 0
             // Add 1 hour of predictions
             // Add 1 hour of predictions
@@ -100,7 +100,7 @@ extension MainViewController {
         // create Basal graph data
         // create Basal graph data
         var chartEntry = [ChartDataEntry]()
         var chartEntry = [ChartDataEntry]()
         var maxBasal = 1.0
         var maxBasal = 1.0
-        if basalData.count > 0 {
+        if basalData.count > 0  && UserDefaultsRepository.graphBasal.value {
             for i in 0..<basalData.count{
             for i in 0..<basalData.count{
                 let value = ChartDataEntry(x: Double(basalData[i].date), y: Double(basalData[i].basalRate))
                 let value = ChartDataEntry(x: Double(basalData[i].date), y: Double(basalData[i].basalRate))
                 chartEntry.append(value)
                 chartEntry.append(value)
@@ -123,7 +123,7 @@ extension MainViewController {
         
         
         // Boluses
         // Boluses
         var chartEntryBolus = [ChartDataEntry]()
         var chartEntryBolus = [ChartDataEntry]()
-        if bolusData.count > 0 {
+        if bolusData.count > 0  && UserDefaultsRepository.graphBolus.value {
             for i in 0..<bolusData.count{
             for i in 0..<bolusData.count{
                 let value = ChartDataEntry(x: Double(bolusData[i].date), y: Double(bolusData[i].sgv + 10), data: String(bolusData[i].value))
                 let value = ChartDataEntry(x: Double(bolusData[i].date), y: Double(bolusData[i].sgv + 10), data: String(bolusData[i].value))
                 chartEntryBolus.append(value)
                 chartEntryBolus.append(value)
@@ -145,7 +145,7 @@ extension MainViewController {
         
         
         // Carbs
         // Carbs
         var chartEntryCarbs = [ChartDataEntry]()
         var chartEntryCarbs = [ChartDataEntry]()
-        if carbData.count > 0 {
+        if carbData.count > 0  && UserDefaultsRepository.graphCarbs.value {
             for i in 0..<carbData.count{
             for i in 0..<carbData.count{
                 let value = ChartDataEntry(x: Double(carbData[i].date), y: Double(carbData[i].sgv + 30), data: String(carbData[i].value))
                 let value = ChartDataEntry(x: Double(carbData[i].date), y: Double(carbData[i].sgv + 30), data: String(carbData[i].value))
                 chartEntryCarbs.append(value)
                 chartEntryCarbs.append(value)

+ 80 - 85
LoopFollow/Controllers/NightScout.swift

@@ -253,92 +253,86 @@ extension MainViewController {
           if consoleLogging == true {print("in updatePump")}
           if consoleLogging == true {print("in updatePump")}
           if jsonDeviceStatus.count == 0 {
           if jsonDeviceStatus.count == 0 {
             return
             return
-          }
-          
-          //only grabbing one record since ns sorts by {created_at : -1}
-          let lastDeviceStatus = jsonDeviceStatus[0] as [String : AnyObject]?
+        }
           
           
-          //pump and uploader
-          let formatter = ISO8601DateFormatter()
-          formatter.formatOptions = [.withFullDate,
-                                     .withTime,
-                                     .withDashSeparatorInDate,
-                                     .withColonSeparatorInTime]
-          if let lastPumpRecord = lastDeviceStatus?["pump"] as! [String : AnyObject]? {
-              if let lastPumpTime = formatter.date(from: (lastPumpRecord["clock"] as! String))?.timeIntervalSince1970  {
-                  if let reservoirData = lastPumpRecord["reservoir"] as? Double
-                  {
-                      tableData[5].value = String(format:"%.0f", reservoirData) + "U"
-                  } else {
-                      tableData[5].value = "50+U"
-                  }
-                  
-                  if let uploader = lastDeviceStatus?["uploader"] as? [String:AnyObject] {
-                      let upbat = uploader["battery"] as! Double
-                      tableData[4].value = String(format:"%.0f", upbat) + "%"
-                  }
-              }
-          }
-              
-          // Loop
-          if let lastLoopRecord = lastDeviceStatus?["loop"] as! [String : AnyObject]? {
-              if let lastLoopTime = formatter.date(from: (lastLoopRecord["timestamp"] as! String))?.timeIntervalSince1970  {
+        //only grabbing one record since ns sorts by {created_at : -1}
+        let lastDeviceStatus = jsonDeviceStatus[0] as [String : AnyObject]?
 
 
-                  UserDefaultsRepository.alertLastLoopTime.value = lastLoopTime
-                  
-                  if let failure = lastLoopRecord["failureReason"] {
-                      LoopStatusLabel.text = "⚠"
-                  }
-                  else
-                  {
-                      if let enacted = lastLoopRecord["enacted"] as? [String:AnyObject] {
-                          if let lastTempBasal = enacted["rate"] as? Double {
-                         //     tableData[2].value = String(format:"%.1f", lastTempBasal)
-                          }
-                      }
-                      if let iobdata = lastLoopRecord["iob"] as? [String:AnyObject] {
-                          tableData[0].value = String(format:"%.1f", (iobdata["iob"] as! Double))
-                      }
-                      if let cobdata = lastLoopRecord["cob"] as? [String:AnyObject] {
-                          tableData[1].value = String(format:"%.0f", cobdata["cob"] as! Double)
-                      }
-                      if let predictdata = lastLoopRecord["predicted"] as? [String:AnyObject] {
-                          let prediction = predictdata["values"] as! [Double]
-                          PredictionLabel.text = String(Int(prediction.last!))
-                          PredictionLabel.textColor = UIColor.systemPurple
-                          predictionData.removeAll()
-                          var i = 1
-                          while i <= 12 {
-                              predictionData.append(prediction[i])
-                              i += 1
-                          }
-                          
-                      }
-                      
-                      
-                      
-                      if let loopStatus = lastLoopRecord["recommendedTempBasal"] as? [String:AnyObject] {
-                          if let tempBasalTime = formatter.date(from: (loopStatus["timestamp"] as! String))?.timeIntervalSince1970 {
-                              if tempBasalTime > lastLoopTime {
-                                  LoopStatusLabel.text = "⏀"
-                                 } else {
-                                  LoopStatusLabel.text = "↻"
-                              }
-                          }
-                         
-                      } else {
-                          LoopStatusLabel.text = "↻"
-                      }
-                      
-                  }
-                  if ((TimeInterval(Date().timeIntervalSince1970) - lastLoopTime) / 60) > 10 {
-                      LoopStatusLabel.text = "⚠"
-                  }
-              }
-              
-              
+        //pump and uploader
+        let formatter = ISO8601DateFormatter()
+        formatter.formatOptions = [.withFullDate,
+                                 .withTime,
+                                 .withDashSeparatorInDate,
+                                 .withColonSeparatorInTime]
+        if let lastPumpRecord = lastDeviceStatus?["pump"] as! [String : AnyObject]? {
+            if let lastPumpTime = formatter.date(from: (lastPumpRecord["clock"] as! String))?.timeIntervalSince1970  {
+                if let reservoirData = lastPumpRecord["reservoir"] as? Double {
+                    tableData[5].value = String(format:"%.0f", reservoirData) + "U"
+                } else {
+                    tableData[5].value = "50+U"
+                }
+
+                if let uploader = lastDeviceStatus?["uploader"] as? [String:AnyObject] {
+                    let upbat = uploader["battery"] as! Double
+                    tableData[4].value = String(format:"%.0f", upbat) + "%"
+                }
+            }
+        }
               
               
-          }
+        // Loop
+        if let lastLoopRecord = lastDeviceStatus?["loop"] as! [String : AnyObject]? {
+            if let lastLoopTime = formatter.date(from: (lastLoopRecord["timestamp"] as! String))?.timeIntervalSince1970  {
+                UserDefaultsRepository.alertLastLoopTime.value = lastLoopTime
+                if let failure = lastLoopRecord["failureReason"] {
+                    LoopStatusLabel.text = "X"
+                } else {
+                    if let enacted = lastLoopRecord["enacted"] as? [String:AnyObject] {
+                        if let lastTempBasal = enacted["rate"] as? Double {
+                            // tableData[2].value = String(format:"%.1f", lastTempBasal)
+                        }
+                    }
+                    if let iobdata = lastLoopRecord["iob"] as? [String:AnyObject] {
+                        tableData[0].value = String(format:"%.1f", (iobdata["iob"] as! Double))
+                    }
+                    if let cobdata = lastLoopRecord["cob"] as? [String:AnyObject] {
+                        tableData[1].value = String(format:"%.0f", cobdata["cob"] as! Double)
+                    }
+                    if let predictdata = lastLoopRecord["predicted"] as? [String:AnyObject] {
+                        let prediction = predictdata["values"] as! [Double]
+                        PredictionLabel.text = String(Int(prediction.last!))
+                        PredictionLabel.textColor = UIColor.systemPurple
+                        predictionData.removeAll()
+                        if UserDefaultsRepository.downloadPrediction.value {
+                        var i = 1
+                        while i <= 12 {
+                            predictionData.append(prediction[i])
+                            i += 1
+                        }
+                        }
+                    }
+                    if let loopStatus = lastLoopRecord["recommendedTempBasal"] as? [String:AnyObject] {
+                        if let tempBasalTime = formatter.date(from: (loopStatus["timestamp"] as! String))?.timeIntervalSince1970 {
+                            var lastBGTime = lastLoopTime
+                            if bgData.count > 0 {
+                                lastBGTime = bgData[bgData.count - 1].date
+                            }
+                            if tempBasalTime > lastBGTime {
+                                LoopStatusLabel.text = "⏀"
+                            } else {
+                                LoopStatusLabel.text = "↻"
+                            }
+                        }
+                    } else {
+                        LoopStatusLabel.text = "↻"
+                    }
+
+                }
+
+                if ((TimeInterval(Date().timeIntervalSince1970) - lastLoopTime) / 60) > 10 {
+                    LoopStatusLabel.text = "⚠"
+                }
+            } // end lastLoopTime
+        } // end lastLoop Record
           
           
           var oText = "" as String
           var oText = "" as String
         currentOverride = 1.0
         currentOverride = 1.0
@@ -563,6 +557,7 @@ extension MainViewController {
       
       
         // NS Temp Basal Web Call
         // NS Temp Basal Web Call
       func WebLoadNSTempBasals() {
       func WebLoadNSTempBasals() {
+        if !UserDefaultsRepository.downloadBasal.value { return }
         
         
         let yesterdayString = dateTimeUtils.nowMinus24HoursTimeInterval()
         let yesterdayString = dateTimeUtils.nowMinus24HoursTimeInterval()
 
 
@@ -740,7 +735,7 @@ extension MainViewController {
     
     
     // NS Bolus Web Call
     // NS Bolus Web Call
       func webLoadNSBoluses(){
       func webLoadNSBoluses(){
-        
+        if !UserDefaultsRepository.downloadBolus.value { return }
         let yesterdayString = dateTimeUtils.nowMinus24HoursTimeInterval()
         let yesterdayString = dateTimeUtils.nowMinus24HoursTimeInterval()
         let urlUser = UserDefaultsRepository.url.value
         let urlUser = UserDefaultsRepository.url.value
           var searchString = "find[eventType]=Correction%20Bolus&find[created_at][$gte]=" + yesterdayString
           var searchString = "find[eventType]=Correction%20Bolus&find[created_at][$gte]=" + yesterdayString
@@ -811,7 +806,7 @@ extension MainViewController {
     
     
     // NS Carb Web Call
     // NS Carb Web Call
       func webLoadNSCarbs(){
       func webLoadNSCarbs(){
-        
+        if !UserDefaultsRepository.downloadCarbs.value { return }
         let yesterdayString = dateTimeUtils.nowMinus24HoursTimeInterval()
         let yesterdayString = dateTimeUtils.nowMinus24HoursTimeInterval()
         let urlUser = UserDefaultsRepository.url.value
         let urlUser = UserDefaultsRepository.url.value
           var searchString = "find[eventType]=Meal%20Bolus&find[created_at][$gte]=" + yesterdayString
           var searchString = "find[eventType]=Meal%20Bolus&find[created_at][$gte]=" + yesterdayString

+ 5 - 1
LoopFollow/ViewControllers/MainViewController.swift

@@ -149,10 +149,12 @@ class MainViewController: UIViewController, UITableViewDataSource, ChartViewDele
         infoTable.rowHeight = 25
         infoTable.rowHeight = 25
         infoTable.dataSource = self
         infoTable.dataSource = self
         
         
+        startTimer(time: timeInterval)
         // Load Data
         // Load Data
         if UserDefaultsRepository.url.value != "" {
         if UserDefaultsRepository.url.value != "" {
             nightscoutLoader()
             nightscoutLoader()
-        } 
+        }
+        
     }
     }
     
     
     override func viewWillAppear(_ animated: Bool) {
     override func viewWillAppear(_ animated: Bool) {
@@ -187,6 +189,7 @@ class MainViewController: UIViewController, UITableViewDataSource, ChartViewDele
     
     
     // Check Alarm Timer
     // Check Alarm Timer
     func startCheckAlarmTimer(time: TimeInterval) {
     func startCheckAlarmTimer(time: TimeInterval) {
+        
         checkAlarmTimer = Timer.scheduledTimer(timeInterval: time,
         checkAlarmTimer = Timer.scheduledTimer(timeInterval: time,
                                      target: self,
                                      target: self,
                                      selector: #selector(MainViewController.checkAlarmTimerDidEnd(_:)),
                                      selector: #selector(MainViewController.checkAlarmTimerDidEnd(_:)),
@@ -201,6 +204,7 @@ class MainViewController: UIViewController, UITableViewDataSource, ChartViewDele
                                      selector: #selector(MainViewController.viewTimerDidEnd(_:)),
                                      selector: #selector(MainViewController.viewTimerDidEnd(_:)),
                                      userInfo: nil,
                                      userInfo: nil,
                                      repeats: false)
                                      repeats: false)
+        
     }
     }
     
     
     // Check for new data when timer ends
     // Check for new data when timer ends

+ 62 - 1
LoopFollow/ViewControllers/SettingsViewController.swift

@@ -54,7 +54,7 @@ class SettingsViewController: FormViewController {
         buildAlarmSettings()
         buildAlarmSettings()
         buildGraphSettings()
         buildGraphSettings()
         buildWatchSettings()
         buildWatchSettings()
-        
+        buildDebugSettings()
         
         
         
         
             
             
@@ -300,6 +300,67 @@ class SettingsViewController: FormViewController {
         }
         }
     }
     }
  
  
+    func buildDebugSettings() {
+        form
+            +++ Section("Debug Settings")
 
 
+        <<< SwitchRow("downloadBasal"){ row in
+            row.title = "Download Basal"
+            row.value = UserDefaultsRepository.downloadBasal.value
+        }.onChange { [weak self] row in
+                    guard let value = row.value else { return }
+                    UserDefaultsRepository.downloadBasal.value = value
+            }
+            <<< SwitchRow("graphBasal"){ row in
+            row.title = "Graph Basal"
+            row.value = UserDefaultsRepository.graphBasal.value
+        }.onChange { [weak self] row in
+                    guard let value = row.value else { return }
+                    UserDefaultsRepository.graphBasal.value = value
+            }
+            <<< SwitchRow("downloadBolus"){ row in
+                row.title = "Download Bolus"
+                row.value = UserDefaultsRepository.downloadBolus.value
+            }.onChange { [weak self] row in
+                        guard let value = row.value else { return }
+                        UserDefaultsRepository.downloadBolus.value = value
+                }
+           <<< SwitchRow("graphBolus"){ row in
+               row.title = "Graph Bolus"
+               row.value = UserDefaultsRepository.graphBolus.value
+           }.onChange { [weak self] row in
+                       guard let value = row.value else { return }
+                       UserDefaultsRepository.graphBolus.value = value
+               }
+            <<< SwitchRow("downloadCarbs"){ row in
+                row.title = "Download Carbs"
+                row.value = UserDefaultsRepository.downloadCarbs.value
+            }.onChange { [weak self] row in
+                        guard let value = row.value else { return }
+                        UserDefaultsRepository.downloadCarbs.value = value
+                }
+              <<< SwitchRow("graphCarbs"){ row in
+                  row.title = "Graph Carbs"
+                  row.value = UserDefaultsRepository.graphCarbs.value
+              }.onChange { [weak self] row in
+                          guard let value = row.value else { return }
+                          UserDefaultsRepository.graphCarbs.value = value
+                  }
+            
+        <<< SwitchRow("downloadPrediction"){ row in
+                 row.title = "Download Prediction"
+                 row.value = UserDefaultsRepository.downloadPrediction.value
+             }.onChange { [weak self] row in
+                         guard let value = row.value else { return }
+                         UserDefaultsRepository.downloadPrediction.value = value
+                 }
+        <<< SwitchRow("graphPrediction"){ row in
+            row.title = "Graph Prediction"
+            row.value = UserDefaultsRepository.graphPrediction.value
+        }.onChange { [weak self] row in
+                    guard let value = row.value else { return }
+                    UserDefaultsRepository.graphPrediction.value = value
+            }
+    }
 
 
 }
 }

+ 11 - 0
LoopFollow/repository/UserDefaults.swift

@@ -39,6 +39,17 @@ class UserDefaultsRepository {
             UIApplication.shared.isIdleTimerDisabled = screenlock
             UIApplication.shared.isIdleTimerDisabled = screenlock
     })
     })
     
     
+    // Debug Settings
+        static let downloadBasal = UserDefaultsValue<Bool>(key: "downloadBasal", default: true)
+        static let downloadBolus = UserDefaultsValue<Bool>(key: "downloadBolus", default: true)
+        static let downloadCarbs = UserDefaultsValue<Bool>(key: "downloadCarbs", default: true)
+        static let downloadPrediction = UserDefaultsValue<Bool>(key: "downloadPrediction", default: true)
+       static let graphBasal = UserDefaultsValue<Bool>(key: "graphBasal", default: true)
+        static let graphBolus = UserDefaultsValue<Bool>(key: "graphBolus", default: true)
+        static let graphCarbs = UserDefaultsValue<Bool>(key: "graphCarbs", default: true)
+        static let graphPrediction = UserDefaultsValue<Bool>(key: "graphPrediction", default: true)
+    
+    
     
     
     // Watch Calendar Settings
     // Watch Calendar Settings
     static let calendarIdentifier = UserDefaultsValue<String>(key: "calendarIdentifier", default: "")
     static let calendarIdentifier = UserDefaultsValue<String>(key: "calendarIdentifier", default: "")