Parcourir la source

Merge 'dontTest' into bdb.

Two new oref0 variables introduced. Enables SMBs with a high BG (new FAX settings), even when other SMBs settings are off!
Jon B.M il y a 3 ans
Parent
commit
821804f1b1

Fichier diff supprimé car celui-ci est trop grand
+ 1 - 1
FreeAPS/Resources/javascript/bundle/determine-basal.js


Fichier diff supprimé car celui-ci est trop grand
+ 1 - 1
FreeAPS/Resources/javascript/bundle/profile.js


+ 4 - 4
FreeAPS/Sources/APS/APSManager.swift

@@ -654,7 +654,7 @@ final class BaseAPSManager: APSManager, Injectable {
             storage.transaction { storage in
             storage.transaction { storage in
                 storage.append(tdd, to: file, uniqBy: \.id)
                 storage.append(tdd, to: file, uniqBy: \.id)
                 uniqEvents = storage.retrieve(file, as: [TDD].self)?
                 uniqEvents = storage.retrieve(file, as: [TDD].self)?
-                    .filter { $0.timestamp.addingTimeInterval(7.days.timeInterval) > Date() }
+                    .filter { $0.timestamp.addingTimeInterval(14.days.timeInterval) > Date() }
                     .sorted { $0.timestamp > $1.timestamp } ?? []
                     .sorted { $0.timestamp > $1.timestamp } ?? []
 
 
                 var total: Decimal = 0
                 var total: Decimal = 0
@@ -689,13 +689,13 @@ final class BaseAPSManager: APSManager, Injectable {
                     nrOfIndeces = 1
                     nrOfIndeces = 1
                 }
                 }
 
 
-                let average7 = total / indeces
+                let average14 = total / indeces
                 let average2hours = totalAmount / nrOfIndeces
                 let average2hours = totalAmount / nrOfIndeces
                 let weight = preferences.weightPercentage
                 let weight = preferences.weightPercentage
-                let weighted_average = weight * average2hours + (1 - weight) * average7
+                let weighted_average = weight * average2hours + (1 - weight) * average14
 
 
                 let averages = TDD_averages(
                 let averages = TDD_averages(
-                    average_7days: average7,
+                    average_total_data: average14,
                     weightedAverage: weighted_average,
                     weightedAverage: weighted_average,
                     past2hoursAverage: average2hours,
                     past2hoursAverage: average2hours,
                     date: Date()
                     date: Date()

+ 1 - 1
FreeAPS/Sources/APS/CGM/AppGroupSource.swift

@@ -21,7 +21,7 @@ struct AppGroupSource: GlucoseSource {
         }
         }
 
 
         debug(.deviceManager, "in fetchLastBGs")
         debug(.deviceManager, "in fetchLastBGs")
-        
+
         HeartBeatManager.shared.checkCGMBluetoothTransmitter(sharedUserDefaults: sharedDefaults)
         HeartBeatManager.shared.checkCGMBluetoothTransmitter(sharedUserDefaults: sharedDefaults)
 
 
         let decoded = try? JSONSerialization.jsonObject(with: sharedData, options: [])
         let decoded = try? JSONSerialization.jsonObject(with: sharedData, options: [])

+ 18 - 14
FreeAPS/Sources/APS/CGM/HeartBeatManager.swift

@@ -23,49 +23,50 @@ class HeartBeatManager {
     /// - parameters:
     /// - parameters:
     ///     - sharedData : shared User Defaults
     ///     - sharedData : shared User Defaults
     public func checkCGMBluetoothTransmitter(sharedUserDefaults: UserDefaults) {
     public func checkCGMBluetoothTransmitter(sharedUserDefaults: UserDefaults) {
-        
+        if let sharedTransmitterAddress = sharedUserDefaults.string(forKey: keyForcgmTransmitterDeviceAddress) {
+            debug(.deviceManager, "in checkCGMBluetoothTransmitter, sharedTransmitterAddress = \(sharedTransmitterAddress)")
+        } else {
+            debug(.deviceManager, "in checkCGMBluetoothTransmitter, sharedTransmitterAddress = nil")
+        }
+
         if !initialSetupDone {
         if !initialSetupDone {
-            
             debug(.deviceManager, "in checkCGMBluetoothTransmitter, initial setup")
             debug(.deviceManager, "in checkCGMBluetoothTransmitter, initial setup")
-            
+
             initialSetupDone = true
             initialSetupDone = true
 
 
             // set to nil, this will force recreation of bluetooth transmitter at app startup
             // set to nil, this will force recreation of bluetooth transmitter at app startup
             UserDefaults.standard.cgmTransmitterDeviceAddress = nil
             UserDefaults.standard.cgmTransmitterDeviceAddress = nil
-            
         }
         }
 
 
         if UserDefaults.standard.cgmTransmitterDeviceAddress != sharedUserDefaults
         if UserDefaults.standard.cgmTransmitterDeviceAddress != sharedUserDefaults
             .string(forKey: keyForcgmTransmitterDeviceAddress)
             .string(forKey: keyForcgmTransmitterDeviceAddress)
         {
         {
-
-            debug(.deviceManager, "UserDefaults.standard.cgmTransmitterDeviceAddress != sharedUserDefaults.string(forKey: keyForcgmTransmitterDeviceAddress)")
+            debug(
+                .deviceManager,
+                "UserDefaults.standard.cgmTransmitterDeviceAddress != sharedUserDefaults.string(forKey: keyForcgmTransmitterDeviceAddress)"
+            )
             if let sharedTransmitterAddress = sharedUserDefaults.string(forKey: keyForcgmTransmitterDeviceAddress) {
             if let sharedTransmitterAddress = sharedUserDefaults.string(forKey: keyForcgmTransmitterDeviceAddress) {
                 debug(.deviceManager, "in checkCGMBluetoothTransmitter, sharedTransmitterAddress = \(sharedTransmitterAddress)")
                 debug(.deviceManager, "in checkCGMBluetoothTransmitter, sharedTransmitterAddress = \(sharedTransmitterAddress)")
             } else {
             } else {
                 debug(.deviceManager, "in checkCGMBluetoothTransmitter, sharedTransmitterAddress = nil")
                 debug(.deviceManager, "in checkCGMBluetoothTransmitter, sharedTransmitterAddress = nil")
             }
             }
-            
+
             // assign local copy of cgmTransmitterDeviceAddress to the value stored in sharedUserDefaults (possibly nil value)
             // assign local copy of cgmTransmitterDeviceAddress to the value stored in sharedUserDefaults (possibly nil value)
             UserDefaults.standard.cgmTransmitterDeviceAddress = sharedUserDefaults
             UserDefaults.standard.cgmTransmitterDeviceAddress = sharedUserDefaults
                 .string(forKey: keyForcgmTransmitterDeviceAddress)
                 .string(forKey: keyForcgmTransmitterDeviceAddress)
 
 
             // assign new bluetoothTransmitter. If return value is nil, and if it was not nil before, and if it was currently connected then it will disconnect automatically, because there's no other reference to it, hence deinit will be called
             // assign new bluetoothTransmitter. If return value is nil, and if it was not nil before, and if it was currently connected then it will disconnect automatically, because there's no other reference to it, hence deinit will be called
             bluetoothTransmitter = setupBluetoothTransmitter(sharedData: sharedUserDefaults)
             bluetoothTransmitter = setupBluetoothTransmitter(sharedData: sharedUserDefaults)
-                
         }
         }
     }
     }
 
 
     private func setupBluetoothTransmitter(sharedData: UserDefaults) -> BluetoothTransmitter? {
     private func setupBluetoothTransmitter(sharedData: UserDefaults) -> BluetoothTransmitter? {
-        
         // if sharedUserDefaults.cgmTransmitterDeviceAddress is not nil then, create a new bluetoothTranmsitter instance
         // if sharedUserDefaults.cgmTransmitterDeviceAddress is not nil then, create a new bluetoothTranmsitter instance
         if let cgmTransmitterDeviceAddress = sharedData.string(forKey: keyForcgmTransmitterDeviceAddress) {
         if let cgmTransmitterDeviceAddress = sharedData.string(forKey: keyForcgmTransmitterDeviceAddress) {
-            
             // unwrap cgmTransmitter_CBUUID_Service and cgmTransmitter_CBUUID_Receive
             // unwrap cgmTransmitter_CBUUID_Service and cgmTransmitter_CBUUID_Receive
             if let cgmTransmitter_CBUUID_Service = sharedData.string(forKey: keyForcgmTransmitter_CBUUID_Service),
             if let cgmTransmitter_CBUUID_Service = sharedData.string(forKey: keyForcgmTransmitter_CBUUID_Service),
                let cgmTransmitter_CBUUID_Receive = sharedData.string(forKey: keycgmTransmitter_CBUUID_Receive)
                let cgmTransmitter_CBUUID_Receive = sharedData.string(forKey: keycgmTransmitter_CBUUID_Receive)
             {
             {
-                
                 // a new cgm transmitter has been setup in xDrip4iOS
                 // a new cgm transmitter has been setup in xDrip4iOS
                 // we will connect to the same transmitter here so it can be used as heartbeat
                 // we will connect to the same transmitter here so it can be used as heartbeat
                 let newBluetoothTransmitter = BluetoothTransmitter(
                 let newBluetoothTransmitter = BluetoothTransmitter(
@@ -75,8 +76,11 @@ class HeartBeatManager {
                     heartbeat: {}
                     heartbeat: {}
                 )
                 )
 
 
-                debug(.deviceManager, "in setupBluetoothTransmitter, cgmTransmitterDeviceAddress in shared user defaults is not nil")
-                
+                debug(
+                    .deviceManager,
+                    "in setupBluetoothTransmitter, cgmTransmitterDeviceAddress in shared user defaults is not nil"
+                )
+
                 return newBluetoothTransmitter
                 return newBluetoothTransmitter
 
 
             } else {
             } else {
@@ -87,7 +91,7 @@ class HeartBeatManager {
         }
         }
 
 
         debug(.deviceManager, "in setupBluetoothTransmitter, cgmTransmitterDeviceAddress in shared user defaults is nil")
         debug(.deviceManager, "in setupBluetoothTransmitter, cgmTransmitterDeviceAddress in shared user defaults is nil")
-        
+
         return nil
         return nil
     }
     }
 }
 }

+ 1 - 1
FreeAPS/Sources/APS/FetchGlucoseManager.swift

@@ -16,7 +16,7 @@ final class BaseFetchGlucoseManager: FetchGlucoseManager, Injectable {
     @Injected() var deviceDataManager: DeviceDataManager!
     @Injected() var deviceDataManager: DeviceDataManager!
 
 
     private var lifetime = Lifetime()
     private var lifetime = Lifetime()
-    private let timer = DispatchTimer(timeInterval: 1.minutes.timeInterval)
+    private let timer = DispatchTimer(timeInterval: TimeInterval(1.0))
 
 
     private lazy var dexcomSource = DexcomSource()
     private lazy var dexcomSource = DexcomSource()
     private lazy var simulatorSource = GlucoseSimulatorSource()
     private lazy var simulatorSource = GlucoseSimulatorSource()

+ 4 - 0
FreeAPS/Sources/Models/Preferences.swift

@@ -69,6 +69,8 @@ struct Preferences: JSON {
     var useWeightedAverage: Bool = false
     var useWeightedAverage: Bool = false
     var weightPercentage: Decimal = 0.65
     var weightPercentage: Decimal = 0.65
     var tddAdjBasal: Bool = false
     var tddAdjBasal: Bool = false
+    var enableSMB_high_bg: Bool = false
+    var enableSMB_high_bg_target: Decimal = 110
 }
 }
 
 
 extension Preferences {
 extension Preferences {
@@ -140,6 +142,8 @@ extension Preferences {
         case useWeightedAverage
         case useWeightedAverage
         case weightPercentage
         case weightPercentage
         case tddAdjBasal
         case tddAdjBasal
+        case enableSMB_high_bg
+        case enableSMB_high_bg_target
     }
     }
 }
 }
 
 

+ 4 - 4
FreeAPS/Sources/Models/TDD_averages.swift

@@ -1,18 +1,18 @@
 import Foundation
 import Foundation
 
 
 struct TDD_averages: JSON, Equatable {
 struct TDD_averages: JSON, Equatable {
-    var average_7days: Decimal
+    var average_total_data: Decimal
     var weightedAverage: Decimal
     var weightedAverage: Decimal
     var past2hoursAverage: Decimal
     var past2hoursAverage: Decimal
     var date: Date
     var date: Date
 
 
     init(
     init(
-        average_7days: Decimal,
+        average_total_data: Decimal,
         weightedAverage: Decimal,
         weightedAverage: Decimal,
         past2hoursAverage: Decimal,
         past2hoursAverage: Decimal,
         date: Date
         date: Date
     ) {
     ) {
-        self.average_7days = average_7days
+        self.average_total_data = average_total_data
         self.weightedAverage = weightedAverage
         self.weightedAverage = weightedAverage
         self.past2hoursAverage = past2hoursAverage
         self.past2hoursAverage = past2hoursAverage
         self.date = date
         self.date = date
@@ -21,7 +21,7 @@ struct TDD_averages: JSON, Equatable {
 
 
 extension TDD_averages {
 extension TDD_averages {
     private enum CodingKeys: String, CodingKey {
     private enum CodingKeys: String, CodingKey {
-        case average_7days
+        case average_total_data
         case weightedAverage
         case weightedAverage
         case past2hoursAverage
         case past2hoursAverage
         case date
         case date

+ 2 - 0
FreeAPS/Sources/Modules/CGM/CGMStateModel.swift

@@ -13,12 +13,14 @@ extension CGM {
         @Published var calendarIDs: [String] = []
         @Published var calendarIDs: [String] = []
         @Published var currentCalendarID: String = ""
         @Published var currentCalendarID: String = ""
         @Persisted(key: "CalendarManager.currentCalendarID") var storedCalendarID: String? = nil
         @Persisted(key: "CalendarManager.currentCalendarID") var storedCalendarID: String? = nil
+        @Published var cgmTransmitterDeviceAddress: String? = nil
 
 
         override func subscribe() {
         override func subscribe() {
             cgm = settingsManager.settings.cgm
             cgm = settingsManager.settings.cgm
             transmitterID = UserDefaults.standard.dexcomTransmitterID ?? ""
             transmitterID = UserDefaults.standard.dexcomTransmitterID ?? ""
             currentCalendarID = storedCalendarID ?? ""
             currentCalendarID = storedCalendarID ?? ""
             calendarIDs = calendarManager.calendarIDs()
             calendarIDs = calendarManager.calendarIDs()
+            cgmTransmitterDeviceAddress = UserDefaults.standard.cgmTransmitterDeviceAddress
 
 
             subscribeSetting(\.useCalendar, on: $createCalendarEvents) { createCalendarEvents = $0 }
             subscribeSetting(\.useCalendar, on: $createCalendarEvents) { createCalendarEvents = $0 }
             subscribeSetting(\.uploadGlucose, on: $uploadGlucose) { uploadGlucose = $0 }
             subscribeSetting(\.uploadGlucose, on: $uploadGlucose) { uploadGlucose = $0 }

+ 15 - 0
FreeAPS/Sources/Modules/CGM/View/CGMRootView.swift

@@ -6,6 +6,8 @@ extension CGM {
         let resolver: Resolver
         let resolver: Resolver
         @StateObject var state = StateModel()
         @StateObject var state = StateModel()
 
 
+        // @AppStorage(UserDefaults.BTKey.cgmTransmitterDeviceAddress.rawValue) private var cgmTransmitterDeviceAddress: String? = nil
+
         var body: some View {
         var body: some View {
             Form {
             Form {
                 Section {
                 Section {
@@ -45,6 +47,19 @@ extension CGM {
                     Text("Calibrations").navigationLink(to: .calibrations, from: self)
                     Text("Calibrations").navigationLink(to: .calibrations, from: self)
                 }
                 }
 
 
+                if state.cgm == .xdrip {
+                    Section(header: Text("Heartbeat")) {
+                        VStack(alignment: .leading) {
+                            if let cgmTransmitterDeviceAddress = state.cgmTransmitterDeviceAddress {
+                                Text("CGM address :")
+                                Text(cgmTransmitterDeviceAddress)
+                            } else {
+                                Text("CGM is not used as heartbeat.")
+                            }
+                        }
+                    }
+                }
+
                 Section(header: Text("Calendar")) {
                 Section(header: Text("Calendar")) {
                     Toggle("Create events in calendar", isOn: $state.createCalendarEvents)
                     Toggle("Create events in calendar", isOn: $state.createCalendarEvents)
                     if state.calendarIDs.isNotEmpty {
                     if state.calendarIDs.isNotEmpty {

+ 19 - 3
FreeAPS/Sources/Modules/PreferencesEditor/PreferencesEditorStateModel.swift

@@ -91,8 +91,6 @@ extension PreferencesEditor {
                 )
                 )
             ]
             ]
 
 
-            // MARK: - SMB fields
-
             let dynamicISF = [
             let dynamicISF = [
                 Field(
                 Field(
                     displayName: "Enable Dynamic ISF",
                     displayName: "Enable Dynamic ISF",
@@ -134,7 +132,7 @@ extension PreferencesEditor {
                     displayName: "Weighted Average of TDD. Weight of past 24 hours:",
                     displayName: "Weighted Average of TDD. Weight of past 24 hours:",
                     type: .decimal(keypath: \.weightPercentage),
                     type: .decimal(keypath: \.weightPercentage),
                     infoText: NSLocalizedString(
                     infoText: NSLocalizedString(
-                        "Has to be > 0 and <= 1.\nDefault is 0.65 (65 %) * past 24 hours. The rest will be from 7 days TDD average (0.35). To only use past 24 hours, set this to 1.\nTo avoid sudden fluctuations, an average of past 2 hours of TDD calc is used as past 24 hours TDD.",
+                        "Has to be > 0 and <= 1.\nDefault is 0.65 (65 %) * past 24 hours. The rest will be from average of total data (14 days) of all TDD calculations (0.35). To only use past 24 hours, set this to 1.\nTo avoid sudden fluctuations, an average of the past 2 hours of TDD calcs is used instead of just the current TDD (past 24 hours at this moment).",
                         comment: "Weight of past 24 hours of TDD"
                         comment: "Weight of past 24 hours of TDD"
                     ),
                     ),
                     settable: self
                     settable: self
@@ -206,6 +204,24 @@ extension PreferencesEditor {
                     settable: self
                     settable: self
                 ),
                 ),
                 Field(
                 Field(
+                    displayName: "Enable SMB With High BG",
+                    type: .boolean(keypath: \.enableSMB_high_bg),
+                    infoText: NSLocalizedString(
+                        "Enable SMBs when a high BG is detected, based on the high BG target (adjusted or profile)",
+                        comment: "Enable SMB With High BG"
+                    ),
+                    settable: self
+                ),
+                Field(
+                    displayName: "... When Blood Glucose Is Over (mg/dl):",
+                    type: .decimal(keypath: \.enableSMB_high_bg_target),
+                    infoText: NSLocalizedString(
+                        "Set the value enableSMB_high_bg will compare against to enable SMB. If BG > than this value, SMBs should enable.",
+                        comment: "... When Blood Glucose Is Over (mg/dl):"
+                    ),
+                    settable: self
+                ),
+                Field(
                     displayName: "Enable UAM",
                     displayName: "Enable UAM",
                     type: .boolean(keypath: \.enableUAM),
                     type: .boolean(keypath: \.enableUAM),
                     infoText: NSLocalizedString(
                     infoText: NSLocalizedString(

+ 3 - 2
FreeAPS/Sources/Views/TagCloudView.swift

@@ -63,11 +63,12 @@ struct TagCloudView: View {
                 return .zt
                 return .zt
             case textTag where textTag.contains("SMB Delivery Ratio:"):
             case textTag where textTag.contains("SMB Delivery Ratio:"):
                 return .uam
                 return .uam
+            case textTag where textTag.contains("Weighted avg:"),
+                 textTag where textTag.contains("Total data avg:"):
+                return .gray
             case textTag where textTag.contains("Parabolic Fit"):
             case textTag where textTag.contains("Parabolic Fit"):
                 return .loopRed
                 return .loopRed
             case textTag where textTag.contains("TDD:"),
             case textTag where textTag.contains("TDD:"),
-                 textTag where textTag.contains("Weighted avg:"),
-                 textTag where textTag.contains("Total data avg:"),
                  textTag where textTag.contains("Original formula"),
                  textTag where textTag.contains("Original formula"),
                  textTag where textTag.contains("Logarithmic formula"),
                  textTag where textTag.contains("Logarithmic formula"),
                  textTag where textTag.contains("AF:"),
                  textTag where textTag.contains("AF:"),