Просмотр исходного кода

Add: dex/NS failure notification

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

+ 4 - 0
LoopFollow.xcodeproj/project.pbxproj

@@ -159,6 +159,7 @@
 		FCC6886B24898FD800A0279D /* ObservationToken.swift in Sources */ = {isa = PBXBuildFile; fileRef = FCC6886A24898FD800A0279D /* ObservationToken.swift */; };
 		FCC6886D2489909D00A0279D /* AnyConvertible.swift in Sources */ = {isa = PBXBuildFile; fileRef = FCC6886C2489909D00A0279D /* AnyConvertible.swift */; };
 		FCC6886F2489A53800A0279D /* AppConstants.swift in Sources */ = {isa = PBXBuildFile; fileRef = FCC6886E2489A53800A0279D /* AppConstants.swift */; };
+		FCD2A27D24C9D044009F7B7B /* Globals.swift in Sources */ = {isa = PBXBuildFile; fileRef = FCD2A27C24C9D044009F7B7B /* Globals.swift */; };
 		FCD49B6C24AA536E007879DC /* DebugViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = FCD49B6B24AA536E007879DC /* DebugViewController.swift */; };
 		FCE537BC249A4D7D00F80BF8 /* carbBolusArrays.swift in Sources */ = {isa = PBXBuildFile; fileRef = FCE537BB249A4D7D00F80BF8 /* carbBolusArrays.swift */; };
 		FCEF87AC24A141A700AE6FA0 /* bgUnits.swift in Sources */ = {isa = PBXBuildFile; fileRef = FCEF87AA24A1417900AE6FA0 /* bgUnits.swift */; };
@@ -325,6 +326,7 @@
 		FCC6886C2489909D00A0279D /* AnyConvertible.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AnyConvertible.swift; sourceTree = "<group>"; };
 		FCC6886E2489A53800A0279D /* AppConstants.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AppConstants.swift; sourceTree = "<group>"; };
 		FCC688702489A57C00A0279D /* Loop Follow.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = "Loop Follow.entitlements"; sourceTree = "<group>"; };
+		FCD2A27C24C9D044009F7B7B /* Globals.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Globals.swift; sourceTree = "<group>"; };
 		FCD49B6B24AA536E007879DC /* DebugViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DebugViewController.swift; sourceTree = "<group>"; };
 		FCE537BB249A4D7D00F80BF8 /* carbBolusArrays.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = carbBolusArrays.swift; sourceTree = "<group>"; };
 		FCE537C2249AAB2600F80BF8 /* NotificationCenter.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = NotificationCenter.framework; path = System/Library/Frameworks/NotificationCenter.framework; sourceTree = SDKROOT; };
@@ -592,6 +594,7 @@
 				FCE537BB249A4D7D00F80BF8 /* carbBolusArrays.swift */,
 				FCEF87AA24A1417900AE6FA0 /* bgUnits.swift */,
 				FC1BDD2E24A232A3001B652C /* DataStructs.swift */,
+				FCD2A27C24C9D044009F7B7B /* Globals.swift */,
 			);
 			path = helpers;
 			sourceTree = "<group>";
@@ -886,6 +889,7 @@
 				FC1BDD2B24A22650001B652C /* Stats.swift in Sources */,
 				FC1BDD2D24A23204001B652C /* StatsView.swift in Sources */,
 				FCE537BC249A4D7D00F80BF8 /* carbBolusArrays.swift in Sources */,
+				FCD2A27D24C9D044009F7B7B /* Globals.swift in Sources */,
 				DDCF979A24C14DB4002C9752 /* WatchSettingsViewController.swift in Sources */,
 				FC16A98124996C07003D6245 /* DateTime.swift in Sources */,
 				FC3CAB022493B6220068A152 /* BackgroundTaskAudio.swift in Sources */,

+ 12 - 0
LoopFollow/Application/AppDelegate.swift

@@ -33,6 +33,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
            if !granted { return }
         }
         
+        UNUserNotificationCenter.current().delegate = self
+        
          return true
       }
         
@@ -124,3 +126,13 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
     }
 
 }
+
+extension AppDelegate: UNUserNotificationCenterDelegate {
+
+    func userNotificationCenter(_ center: UNUserNotificationCenter,
+           willPresent notification: UNNotification,
+           withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
+    {
+        completionHandler(.alert)
+    }
+}

+ 49 - 5
LoopFollow/Controllers/NightScout.swift

@@ -112,7 +112,11 @@ extension MainViewController {
                 if latestLoopTime == 0 {
                     webLoadNSDeviceStatus()
                
-                    webLoadNSBGData(onlyPullLastRecord: onlyPullLastRecord)
+                    if UserDefaultsRepository.shareUserName.value != "" && UserDefaultsRepository.sharePassword.value != "" {
+                        webLoadDexShare(onlyPullLastRecord: onlyPullLastRecord)
+                    } else {
+                        webLoadNSBGData(onlyPullLastRecord: onlyPullLastRecord)
+                    }
            
                     webLoadNSProfile()
                     if UserDefaultsRepository.downloadBasal.value {
@@ -145,6 +149,13 @@ extension MainViewController {
             } else {
                 // If we get an error, immediately try to pull NS BG Data
                 self.webLoadNSBGData(onlyPullLastRecord: onlyPullLastRecord)
+                
+                if globalVariables.dexVerifiedAlert < dateTimeUtils.getNowTimeIntervalUTC() + 300 {
+                    globalVariables.dexVerifiedAlert = dateTimeUtils.getNowTimeIntervalUTC()
+                    DispatchQueue.main.async {
+                        self.sendNotification(title: "Dexcom Share Error", body: "Please double check user name and password, internet connection, and sharing status.")
+                    }
+                }
             }
         }
     }
@@ -166,9 +177,11 @@ extension MainViewController {
             urlBGDataPath = urlBGDataPath + "token=" + token + "&count=" + points
         }
         guard let urlBGData = URL(string: urlBGDataPath) else {
-            
+            if globalVariables.nsVerifiedAlert < dateTimeUtils.getNowTimeIntervalUTC() + 300 {
+                globalVariables.nsVerifiedAlert = dateTimeUtils.getNowTimeIntervalUTC()
+                self.sendNotification(title: "Nightscout Error", body: "Please double check url, token, and internet connection. This may also indicate a temporary Nightscout issue")
+            }
             return
-            
         }
         var request = URLRequest(url: urlBGData)
         request.cachePolicy = URLRequest.CachePolicy.reloadIgnoringLocalCacheData
@@ -176,10 +189,18 @@ extension MainViewController {
         // Downloader
         let getBGTask = URLSession.shared.dataTask(with: request) { data, response, error in
             guard error == nil else {
+                if globalVariables.nsVerifiedAlert < dateTimeUtils.getNowTimeIntervalUTC() + 300 {
+                    globalVariables.nsVerifiedAlert = dateTimeUtils.getNowTimeIntervalUTC()
+                    self.sendNotification(title: "Nightscout Error", body: "Please double check url, token, and internet connection. This may also indicate a temporary Nightscout issue")
+                }
                 return
                 
             }
             guard let data = data else {
+                if globalVariables.nsVerifiedAlert < dateTimeUtils.getNowTimeIntervalUTC() + 300 {
+                    globalVariables.nsVerifiedAlert = dateTimeUtils.getNowTimeIntervalUTC()
+                    self.sendNotification(title: "Nightscout Error", body: "Please double check url, token, and internet connection. This may also indicate a temporary Nightscout issue")
+                }
                 return
                 
             }
@@ -193,6 +214,10 @@ extension MainViewController {
                     
                 }
             } else {
+                if globalVariables.nsVerifiedAlert < dateTimeUtils.getNowTimeIntervalUTC() + 300 {
+                    globalVariables.nsVerifiedAlert = dateTimeUtils.getNowTimeIntervalUTC()
+                    self.sendNotification(title: "Nightscout Failure", body: "Please double check url, token, and internet connection. This may also indicate a temporary Nightscout issue")
+                }
                 return
                 
             }
@@ -336,7 +361,10 @@ extension MainViewController {
         }
         let escapedAddress = urlStringDeviceStatus.addingPercentEncoding(withAllowedCharacters:NSCharacterSet.urlQueryAllowed)
         guard let urlDeviceStatus = URL(string: escapedAddress!) else {
-            
+            if globalVariables.nsVerifiedAlert < dateTimeUtils.getNowTimeIntervalUTC() + 300 {
+                globalVariables.nsVerifiedAlert = dateTimeUtils.getNowTimeIntervalUTC()
+                self.sendNotification(title: "Nightscout Failure", body: "Please double check url, token, and internet connection. This may also indicate a temporary Nightscout issue")
+            }
             return
         }
         
@@ -346,10 +374,20 @@ extension MainViewController {
         
         
         let deviceStatusTask = URLSession.shared.dataTask(with: requestDeviceStatus) { data, response, error in
+            
             guard error == nil else {
+                if globalVariables.nsVerifiedAlert < dateTimeUtils.getNowTimeIntervalUTC() + 300 {
+                    globalVariables.nsVerifiedAlert = dateTimeUtils.getNowTimeIntervalUTC()
+                    self.sendNotification(title: "Nightscout Error", body: "Please double check url, token, and internet connection. This may also indicate a temporary Nightscout issue")
+                }
                 return
             }
+            
             guard let data = data else {
+                if globalVariables.nsVerifiedAlert < dateTimeUtils.getNowTimeIntervalUTC() + 300 {
+                    globalVariables.nsVerifiedAlert = dateTimeUtils.getNowTimeIntervalUTC()
+                    self.sendNotification(title: "Nightscout Error", body: "Please double check url, token, and internet connection. This may also indicate a temporary Nightscout issue")
+                }
                 return
             }
             
@@ -360,9 +398,15 @@ extension MainViewController {
                     self.updateDeviceStatusDisplay(jsonDeviceStatus: json)
                 }
             } else {
+                if globalVariables.nsVerifiedAlert < dateTimeUtils.getNowTimeIntervalUTC() + 300 {
+                    globalVariables.nsVerifiedAlert = dateTimeUtils.getNowTimeIntervalUTC()
+                    self.sendNotification(title: "Nightscout Error", body: "Please double check url, token, and internet connection. This may also indicate a temporary Nightscout issue")
+                }
                 return
-            } }
+            }
+        }
         deviceStatusTask.resume()
+        
     }
     
     // NS Device Status Response Processor

+ 24 - 0
LoopFollow/ViewControllers/MainViewController.swift

@@ -10,6 +10,7 @@ import UIKit
 import Charts
 import EventKit
 import ShareClient
+import UserNotifications
 
 class MainViewController: UIViewController, UITableViewDataSource, ChartViewDelegate, UNUserNotificationCenterDelegate {
     
@@ -64,6 +65,7 @@ class MainViewController: UIViewController, UITableViewDataSource, ChartViewDele
     var defaults : UserDefaults?
     let consoleLogging = true
     var timeofLastBGUpdate = 0 as TimeInterval
+    var nsVerifiedAlerted = false
     
     var backgroundTask = BackgroundTask()
     
@@ -112,6 +114,7 @@ class MainViewController: UIViewController, UITableViewDataSource, ChartViewDele
     // share
     var bgDataShare: [ShareGlucoseData] = []
     var dexShare: ShareClient?;
+    var dexVerifiedAlerted = false
     
     // calendar setup
     let store = EKEventStore()
@@ -597,5 +600,26 @@ class MainViewController: UIViewController, UITableViewDataSource, ChartViewDele
     }
     
     
+    // General Notifications
+    func sendNotification(title: String, body: String)
+    {
+       // UNUserNotificationCenter.current().delegate = self
+        
+        let content = UNMutableNotificationContent()
+        content.title = title
+        content.body = body
+        content.sound = .default
+        
+        let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 1, repeats: false)
+        let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
+        UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
+        
+    }
+    
+    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
+        
+    }
+    
+    
 }
 

+ 4 - 0
LoopFollow/ViewControllers/SettingsViewController.swift

@@ -65,6 +65,7 @@ class SettingsViewController: FormViewController {
             // set the row value back to the correctly formatted URL so that the user immediately sees how it should have been written
             row.value = UserDefaultsRepository.url.value
             self.showHideNSDetails()
+            globalVariables.nsVerifiedAlert = 0
             }
         <<< TextRow(){ row in
             row.title = "NS Token"
@@ -78,6 +79,7 @@ class SettingsViewController: FormViewController {
             }
             guard let value = row.value else { return }
             UserDefaultsRepository.token.value = value
+            globalVariables.nsVerifiedAlert = 0
         }
         <<< SegmentedRow<String>("units") { row in
             row.title = "Units"
@@ -99,6 +101,7 @@ class SettingsViewController: FormViewController {
             }
             guard let value = row.value else { return }
             UserDefaultsRepository.shareUserName.value = value
+            globalVariables.dexVerifiedAlert = 0
         }
         <<< TextRow(){ row in
             row.title = "Password"
@@ -112,6 +115,7 @@ class SettingsViewController: FormViewController {
             }
             guard let value = row.value else { return }
             UserDefaultsRepository.sharePassword.value = value
+            globalVariables.dexVerifiedAlert = 0
         }
         <<< SegmentedRow<String>("shareServer") { row in
             row.title = "Server"

+ 15 - 0
LoopFollow/helpers/Globals.swift

@@ -0,0 +1,15 @@
+//
+//  Globals.swift
+//  LoopFollow
+//
+//  Created by Jon Fawcett on 7/23/20.
+//  Copyright © 2020 Jon Fawcett. All rights reserved.
+//
+
+import Foundation
+
+
+struct globalVariables {
+    static var dexVerifiedAlert: TimeInterval = 0
+    static var nsVerifiedAlert: TimeInterval = 0
+}