Pārlūkot izejas kodu

Rename variables; add logging and guards

Deniz Cengiz 1 gadu atpakaļ
vecāks
revīzija
9cc8176558

+ 1 - 0
FreeAPS/Sources/APS/CGM/CGMType.swift

@@ -108,4 +108,5 @@ enum CGMType: String, JSON, CaseIterable, Identifiable {
 enum GlucoseDataError: Error {
 enum GlucoseDataError: Error {
     case noData
     case noData
     case unreliableData
     case unreliableData
+    case noGlucoseSource
 }
 }

+ 16 - 8
FreeAPS/Sources/APS/CGM/PluginSource.swift

@@ -138,20 +138,19 @@ extension PluginSource: CGMManagerDelegate {
     func cgmManagerDidUpdateState(_ cgmManager: CGMManager) {
     func cgmManagerDidUpdateState(_ cgmManager: CGMManager) {
         dispatchPrecondition(condition: .onQueue(processQueue))
         dispatchPrecondition(condition: .onQueue(processQueue))
 
 
-        guard let trioFetchGlucoseManager = glucoseManager else {
+        guard let fetchGlucoseManager = glucoseManager else {
             debug(
             debug(
                 .deviceManager,
                 .deviceManager,
-                "Could not gracefully unwrap Trio FetchGlucoseManager upon observing LoopKit's cgmManagerDidUpdateState"
+                "Could not gracefully unwrap FetchGlucoseManager upon observing LoopKit's cgmManagerDidUpdateState"
             )
             )
             return
             return
         }
         }
-        // Adjust Trio-specific NS Upload setting value when CGM setting is changed
-        trioFetchGlucoseManager.settingsManager.settings.uploadGlucose = cgmManager.shouldSyncToRemoteService
+        // Adjust app-specific NS Upload setting value when CGM setting is changed
+        fetchGlucoseManager.settingsManager.settings.uploadGlucose = cgmManager.shouldSyncToRemoteService
 
 
-        // Update glucose source upon state change, e.g. when user switches G7 which is basically a transmitter change without removing and adding a transmitter.
-        trioFetchGlucoseManager.updateGlucoseSource(
-            cgmGlucoseSourceType: trioFetchGlucoseManager.settingsManager.settings.cgm,
-            cgmGlucosePluginId: trioFetchGlucoseManager.settingsManager.settings.cgmPluginIdentifier,
+        fetchGlucoseManager.updateGlucoseSource(
+            cgmGlucoseSourceType: fetchGlucoseManager.settingsManager.settings.cgm,
+            cgmGlucosePluginId: fetchGlucoseManager.settingsManager.settings.cgmPluginIdentifier,
             newManager: cgmManager as? CGMManagerUI
             newManager: cgmManager as? CGMManagerUI
         )
         )
     }
     }
@@ -172,6 +171,15 @@ extension PluginSource: CGMManagerDelegate {
 
 
     private func readCGMResult(readingResult: CGMReadingResult) -> Result<[BloodGlucose], Error> {
     private func readCGMResult(readingResult: CGMReadingResult) -> Result<[BloodGlucose], Error> {
         debug(.deviceManager, "PLUGIN CGM - Process CGM Reading Result launched with \(readingResult)")
         debug(.deviceManager, "PLUGIN CGM - Process CGM Reading Result launched with \(readingResult)")
+
+        if glucoseManager?.glucoseSource == nil {
+            debug(
+                .deviceManager,
+                "No glucose source available."
+            )
+            return .failure(GlucoseDataError.noGlucoseSource)
+        }
+
         switch readingResult {
         switch readingResult {
         case let .newData(values):
         case let .newData(values):
 
 

+ 22 - 19
FreeAPS/Sources/APS/FetchGlucoseManager.swift

@@ -109,7 +109,6 @@ final class BaseFetchGlucoseManager: FetchGlucoseManager, Injectable {
         self.cgmGlucoseSourceType = cgmGlucoseSourceType
         self.cgmGlucoseSourceType = cgmGlucoseSourceType
         self.cgmGlucosePluginId = cgmGlucosePluginId
         self.cgmGlucosePluginId = cgmGlucosePluginId
 
 
-        
         // if not plugin, manager is not changed and stay with the "old" value if the user come back to previous cgmtype
         // if not plugin, manager is not changed and stay with the "old" value if the user come back to previous cgmtype
         // if plugin, if the same pluginID, no change required because the manager is available
         // if plugin, if the same pluginID, no change required because the manager is available
         // if plugin, if not the same pluginID, need to reset the cgmManager
         // if plugin, if not the same pluginID, need to reset the cgmManager
@@ -124,25 +123,29 @@ final class BaseFetchGlucoseManager: FetchGlucoseManager, Injectable {
         } else {
         } else {
             saveConfigManager()
             saveConfigManager()
         }
         }
-            
+
         if glucoseSource == nil {
         if glucoseSource == nil {
-            switch self.cgmGlucoseSourceType {
-            case .none:
-                glucoseSource = nil
-            case .xdrip:
-                glucoseSource = AppGroupSource(from: "xDrip", cgmType: .xdrip)
-            case .nightscout:
-                glucoseSource = nightscoutManager
-            case .simulator:
-                glucoseSource = simulatorSource
-            case .glucoseDirect:
-                glucoseSource = AppGroupSource(from: "GlucoseDirect", cgmType: .glucoseDirect)
-            case .enlite:
-                glucoseSource = deviceDataManager
-            case .plugin:
-                glucoseSource = PluginSource(glucoseStorage: glucoseStorage, glucoseManager: self)
-            }
-            // update the config
+            debug(
+                .deviceManager,
+                "Updating glucose source."
+            )
+        }
+
+        switch self.cgmGlucoseSourceType {
+        case .none:
+            glucoseSource = nil
+        case .xdrip:
+            glucoseSource = AppGroupSource(from: "xDrip", cgmType: .xdrip)
+        case .nightscout:
+            glucoseSource = nightscoutManager
+        case .simulator:
+            glucoseSource = simulatorSource
+        case .glucoseDirect:
+            glucoseSource = AppGroupSource(from: "GlucoseDirect", cgmType: .glucoseDirect)
+        case .enlite:
+            glucoseSource = deviceDataManager
+        case .plugin:
+            glucoseSource = PluginSource(glucoseStorage: glucoseStorage, glucoseManager: self)
         }
         }
     }
     }