| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591 |
- import Foundation
- import Testing
- @testable import Trio
- @Suite("Bolus Calculator Tests") struct BolusCalculatorTests: Injectable {
- @Injected() var calculator: BolusCalculationManager!
- @Injected() var settingsManager: SettingsManager!
- @Injected() var fileStorage: FileStorage!
- @Injected() var apsManager: APSManager!
- let resolver = TrioApp().resolver
- init() {
- injectServices(resolver)
- }
- @Test("Calculator is correctly initialized") func testCalculatorInitialization() {
- #expect(calculator != nil, "BolusCalculationManager should be injected")
- #expect(calculator is BaseBolusCalculationManager, "Calculator should be of type BaseBolusCalculationManager")
- }
- @Test("Calculate insulin for standard meal") func testStandardMealCalculation() async throws {
- // STEP 1: Setup test scenario
- // We need to provide a CalculationInput struct
- let carbs: Decimal = 80
- let currentBG: Decimal = 180 // 80 points above target, should result in 2U correction
- let deltaBG: Decimal = 5 // Rising trend, should add small correction
- let target: Decimal = 100
- let isf: Decimal = 40
- let carbRatio: Decimal = 10 // Should result in 8U for carbs
- let iob: Decimal = 1.0 // Should subtract from final result
- let cob: Int16 = 20
- let useFattyMealCorrectionFactor: Bool = false
- let useSuperBolus: Bool = false
- let fattyMealFactor: Decimal = 0.8
- let sweetMealFactor: Decimal = 2
- let basal: Decimal = 1.5
- let fraction: Decimal = 0.8
- let maxBolus: Decimal = 10
- // STEP 2: Create calculation input
- let input = CalculationInput(
- carbs: carbs,
- currentBG: currentBG,
- deltaBG: deltaBG,
- target: target,
- isf: isf,
- carbRatio: carbRatio,
- iob: iob,
- cob: cob,
- useFattyMealCorrectionFactor: useFattyMealCorrectionFactor,
- fattyMealFactor: fattyMealFactor,
- useSuperBolus: useSuperBolus,
- sweetMealFactor: sweetMealFactor,
- basal: basal,
- fraction: fraction,
- maxBolus: maxBolus
- )
- // STEP 3: Calculate insulin
- let result = await calculator.calculateInsulin(input: input)
- // STEP 4: Verify results
- // Expected calculation breakdown:
- // wholeCob = 80g + 20g COB = 100g
- // wholeCobInsulin = 100g ÷ 10 g/U = 10U
- // targetDifference = currentBG - target = 180 - 100 = 80 mg/dL
- // targetDifferenceInsulin = 80 mg/dL ÷ 40 mg/dL/U = 2U
- // fifteenMinutesInsulin = 5 mg/dL ÷ 40 mg/dL/U = 0.125U
- // correctionInsulin = targetDifferenceInsulin = 2U
- // iobInsulinReduction = 1U
- // superBolusInsulin = 0U (disabled)
- // no adjustment for fatty meals (disabled)
- // wholeCalc = round(wholeCobInsulin + correctionInsulin + fifteenMinutesInsulin - iobInsulinReduction, 3) = 11.125U
- // insulinCalculated = round(wholeCalc × fraction, 3) = 8.9U
- // Calculate expected values with proper rounding using roundBolus method from the apsManager
- let wholeCobInsulin = apsManager.roundBolus(amount: Decimal(100) / Decimal(10)) // 10U
- let targetDifferenceInsulin = apsManager.roundBolus(amount: Decimal(80) / Decimal(40)) // 2U
- let fifteenMinutesInsulin = apsManager.roundBolus(amount: Decimal(5) / Decimal(40)) // 0.125U
- let wholeCalc = wholeCobInsulin + targetDifferenceInsulin + fifteenMinutesInsulin - Decimal(1) // 11.125U
- let expectedInsulinCalculated = apsManager.roundBolus(amount: wholeCalc * fraction) // 8.9U
- #expect(
- result.insulinCalculated == expectedInsulinCalculated,
- """
- Incorrect insulin calculation
- Expected: \(expectedInsulinCalculated)U
- Actual: \(result.insulinCalculated)U
- Components from CalculationResult:
- - insulinCalculated: \(result.insulinCalculated)U (expected: \(expectedInsulinCalculated)U)
- - wholeCalc: \(result.wholeCalc)U (expected: \(wholeCalc)U)
- - correctionInsulin: \(result.correctionInsulin)U (expected: \(targetDifferenceInsulin)U)
- - iobInsulinReduction: \(result.iobInsulinReduction)U (expected: 1U)
- - superBolusInsulin: \(result.superBolusInsulin)U (expected: 0U)
- - targetDifference: \(result.targetDifference) mg/dL (expected: 80 mg/dL)
- - targetDifferenceInsulin: \(result.targetDifferenceInsulin)U (expected: \(targetDifferenceInsulin)U)
- - fifteenMinutesInsulin: \(result.fifteenMinutesInsulin)U (expected: \(fifteenMinutesInsulin)U)
- - wholeCob: \(result.wholeCob)g (expected: 100g)
- - wholeCobInsulin: \(result.wholeCobInsulin)U (expected: \(wholeCobInsulin)U)
- """
- )
- // Verify each component from CalculationResult struct with rounded values
- #expect(
- result.insulinCalculated == expectedInsulinCalculated,
- "Final calculated insulin amount should be \(expectedInsulinCalculated)U"
- )
- #expect(result.wholeCalc == wholeCalc, "Total calculation before fraction should be \(wholeCalc)U")
- #expect(
- result.correctionInsulin == targetDifferenceInsulin,
- "Insulin for BG correction should be \(targetDifferenceInsulin)U"
- )
- #expect(result.iobInsulinReduction == -1.0, "Absolute IOB reduction amount should be 1U, hence -1U")
- #expect(result.superBolusInsulin == 0, "Additional insulin for super bolus should be 0U")
- #expect(result.targetDifference == 80, "Difference from target BG should be 80 mg/dL")
- #expect(
- result.targetDifferenceInsulin == targetDifferenceInsulin,
- "Insulin needed for target difference should be \(targetDifferenceInsulin)U"
- )
- #expect(
- result.fifteenMinutesInsulin == fifteenMinutesInsulin,
- "Trend-based insulin adjustment should be \(fifteenMinutesInsulin)U"
- )
- #expect(result.wholeCob == 100, "Total carbs (COB + new carbs) should be 100g")
- #expect(result.wholeCobInsulin == wholeCobInsulin, "Insulin for total carbs should be \(wholeCobInsulin)U")
- }
- @Test("Calculate insulin for fatty meal") func testFattyMealCalculation() async throws {
- // STEP 1: Setup test scenario
- // We need to provide a CalculationInput struct
- let carbs: Decimal = 80
- let currentBG: Decimal = 180 // 80 points above target, should result in 2U correction
- let deltaBG: Decimal = 5 // Rising trend, should add small correction
- let target: Decimal = 100
- let isf: Decimal = 40
- let carbRatio: Decimal = 10 // Should result in 8U for carbs
- let iob: Decimal = 1.0 // Should subtract from final result
- let cob: Int16 = 20
- let useFattyMealCorrectionFactor: Bool = true // now set to true
- let useSuperBolus: Bool = false
- let fattyMealFactor: Decimal = 0.8
- let sweetMealFactor: Decimal = 2
- let basal: Decimal = 1.5
- let fraction: Decimal = 0.8
- let maxBolus: Decimal = 10
- // STEP 2: Create calculation input
- let input = CalculationInput(
- carbs: carbs,
- currentBG: currentBG,
- deltaBG: deltaBG,
- target: target,
- isf: isf,
- carbRatio: carbRatio,
- iob: iob,
- cob: cob,
- useFattyMealCorrectionFactor: useFattyMealCorrectionFactor,
- fattyMealFactor: fattyMealFactor,
- useSuperBolus: useSuperBolus,
- sweetMealFactor: sweetMealFactor,
- basal: basal,
- fraction: fraction,
- maxBolus: maxBolus
- )
- // STEP 3: Calculate insulin with fatty meal enabled
- let fattyMealResult = await calculator.calculateInsulin(input: input)
- // STEP 4: Calculate insulin with fatty meal disabled for comparison
- let standardInput = CalculationInput(
- carbs: carbs,
- currentBG: currentBG,
- deltaBG: deltaBG,
- target: target,
- isf: isf,
- carbRatio: carbRatio,
- iob: iob,
- cob: cob,
- useFattyMealCorrectionFactor: false, // Disabled for comparison
- fattyMealFactor: fattyMealFactor,
- useSuperBolus: useSuperBolus,
- sweetMealFactor: sweetMealFactor,
- basal: basal,
- fraction: fraction,
- maxBolus: maxBolus
- )
- let standardResult = await calculator.calculateInsulin(input: standardInput)
- // STEP 5: Verify results
- // Fatty meal should reduce the insulin amount by the fatty meal factor (0.8)
- let expectedReduction = fattyMealFactor
- let actualReduction = Decimal(
- (Double(fattyMealResult.insulinCalculated) / Double(standardResult.insulinCalculated) * 10.0).rounded() / 10.0
- )
- #expect(
- actualReduction == expectedReduction,
- """
- Fatty meal calculation incorrect
- Expected reduction factor: \(expectedReduction)
- Actual reduction factor: \(actualReduction)
- Standard calculation: \(standardResult.insulinCalculated)U
- Fatty meal calculation: \(fattyMealResult.insulinCalculated)U
- """
- )
- }
- @Test("Calculate insulin with super bolus") func testSuperBolusCalculation() async throws {
- // STEP 1: Setup test scenario
- // We need to provide a CalculationInput struct
- let carbs: Decimal = 80
- let currentBG: Decimal = 180 // 80 points above target, should result in 2U correction
- let deltaBG: Decimal = 5 // Rising trend, should add small correction
- let target: Decimal = 100
- let isf: Decimal = 40
- let carbRatio: Decimal = 10 // Should result in 8U for carbs
- let iob: Decimal = 1.0 // Should subtract from final result
- let cob: Int16 = 20
- let useFattyMealCorrectionFactor: Bool = false
- let useSuperBolus: Bool = true // Super bolus enabled
- let fattyMealFactor: Decimal = 0.8
- let sweetMealFactor: Decimal = 2
- let basal: Decimal = 1.5 // Will be added to insulin calculation when super bolus is enabled
- let fraction: Decimal = 0.8
- let maxBolus: Decimal = 10
- // STEP 2: Create calculation input with super bolus enabled
- let input = CalculationInput(
- carbs: carbs,
- currentBG: currentBG,
- deltaBG: deltaBG,
- target: target,
- isf: isf,
- carbRatio: carbRatio,
- iob: iob,
- cob: cob,
- useFattyMealCorrectionFactor: useFattyMealCorrectionFactor,
- fattyMealFactor: fattyMealFactor,
- useSuperBolus: useSuperBolus,
- sweetMealFactor: sweetMealFactor,
- basal: basal,
- fraction: fraction,
- maxBolus: maxBolus
- )
- // STEP 3: Calculate insulin with super bolus enabled
- let superBolusResult = await calculator.calculateInsulin(input: input)
- // STEP 4: Calculate insulin with super bolus disabled for comparison
- let standardInput = CalculationInput(
- carbs: carbs,
- currentBG: currentBG,
- deltaBG: deltaBG,
- target: target,
- isf: isf,
- carbRatio: carbRatio,
- iob: iob,
- cob: cob,
- useFattyMealCorrectionFactor: useFattyMealCorrectionFactor,
- fattyMealFactor: fattyMealFactor,
- useSuperBolus: false, // Disabled for comparison
- sweetMealFactor: sweetMealFactor,
- basal: basal,
- fraction: fraction,
- maxBolus: maxBolus
- )
- let standardResult = await calculator.calculateInsulin(input: standardInput)
- // STEP 5: Verify results
- // Super bolus should add basal rate * sweetMealFactor to the insulin calculation
- let expectedSuperBolusInsulin = basal * sweetMealFactor
- #expect(
- superBolusResult.superBolusInsulin == expectedSuperBolusInsulin,
- """
- Super bolus insulin incorrect
- Expected: \(expectedSuperBolusInsulin)U (basal \(basal)U × sweetMealFactor \(sweetMealFactor))
- Actual: \(superBolusResult.superBolusInsulin)U
- """
- )
- #expect(
- superBolusResult.insulinCalculated > standardResult.insulinCalculated,
- """
- Super bolus calculation incorrect
- Expected super bolus calculation to be higher than standard
- Super bolus: \(superBolusResult.insulinCalculated)U
- Standard: \(standardResult.insulinCalculated)U
- Difference: \(superBolusResult.insulinCalculated - standardResult.insulinCalculated)U
- """
- )
- // The difference should be the difference of super bolus (= standard dose + the basal rate * sweetMealFactor) limited by max bolus, and the standard dose.
- let actualDifference = (superBolusResult.insulinCalculated - standardResult.insulinCalculated)
- let expectedDifference = min(superBolusResult.insulinCalculated, maxBolus) - standardResult.insulinCalculated
- #expect(
- actualDifference == expectedDifference,
- """
- Super bolus difference incorrect
- Expected difference: min(\(expectedSuperBolusInsulin), \(maxBolus)) U (basal \(basal)U × sweetMealFactor \(sweetMealFactor) + standard dose \(standardResult
- .insulinCalculated)) - standard dose \(standardResult.insulinCalculated)
- Actual difference: \(actualDifference)U
- Standard result: \(standardResult)
- SuperBolus result: \(superBolusResult)
- """
- )
- }
- @Test("Calculate insulin with zero carbs") func testZeroCarbsCalculation() async throws {
- // Given
- let carbs: Decimal = 0
- // When
- let result = await calculator.handleBolusCalculation(
- carbs: carbs,
- useFattyMealCorrection: false,
- useSuperBolus: false
- )
- // Then
- #expect(result.wholeCobInsulin == 0, "Zero carbs should require no insulin for carbs")
- }
- @Test("Verify settings retrieval") func testGetSettings() async throws {
- // Given - Save original settings to restore later
- let originalSettings = settingsManager.settings
- // Setup test settings
- let expectedUnits = GlucoseUnits.mgdL
- let expectedFraction: Decimal = 0.7
- let expectedFattyMealFactor: Decimal = 0.8
- let expectedSweetMealFactor: Decimal = 2
- let expectedMaxCarbs: Decimal = 150
- // Update settings through settings manager
- settingsManager.settings.units = expectedUnits
- settingsManager.settings.overrideFactor = expectedFraction
- settingsManager.settings.fattyMealFactor = expectedFattyMealFactor
- settingsManager.settings.sweetMealFactor = expectedSweetMealFactor
- settingsManager.settings.maxCarbs = expectedMaxCarbs
- // Save settings to storage
- fileStorage.save(settingsManager.settings, as: OpenAPS.Settings.settings)
- // When
- let (units, fraction, fattyMealFactor, sweetMealFactor, maxCarbs) = await getSettings()
- // Then
- #expect(units == expectedUnits, "Units should match settings")
- #expect(fraction == expectedFraction, "Override factor should match settings")
- #expect(fattyMealFactor == expectedFattyMealFactor, "Fatty meal factor should match settings")
- #expect(sweetMealFactor == expectedSweetMealFactor, "Sweet meal factor should match settings")
- #expect(maxCarbs == expectedMaxCarbs, "Max carbs should match settings")
- // Cleanup - Restore original settings
- settingsManager.settings = originalSettings
- fileStorage.save(originalSettings, as: OpenAPS.Settings.settings)
- }
- @Test("Verify getCurrentSettingValue returns correct values based on time") func testGetCurrentSettingValue() async throws {
- // STEP 1: Backup current settings
- let originalBasalProfile = await fileStorage.retrieveAsync(OpenAPS.Settings.basalProfile, as: [BasalProfileEntry].self)
- let originalCarbRatios = await fileStorage.retrieveAsync(OpenAPS.Settings.carbRatios, as: CarbRatios.self)
- let originalBGTargets = await fileStorage.retrieveAsync(OpenAPS.Settings.bgTargets, as: BGTargets.self)
- let originalISFValues = await fileStorage.retrieveAsync(
- OpenAPS.Settings.insulinSensitivities,
- as: InsulinSensitivities.self
- )
- // STEP 2: Setup test data with known values
- // Note: Entries must be sorted by time for the algorithm to work correctly
- let basalProfile = [
- BasalProfileEntry(start: "00:00", minutes: 0, rate: 1.0), // 12:00 AM - 6:00 AM: 1.0
- BasalProfileEntry(start: "06:00", minutes: 360, rate: 1.2), // 6:00 AM - 12:00 PM: 1.2
- BasalProfileEntry(start: "12:00", minutes: 720, rate: 1.1), // 12:00 PM - 6:00 PM: 1.1
- BasalProfileEntry(start: "18:00", minutes: 1080, rate: 0.9) // 6:00 PM - 12:00 AM: 0.9
- ]
- let carbRatios = CarbRatios(
- units: .grams,
- schedule: [
- CarbRatioEntry(start: "00:00", offset: 0, ratio: 10), // 12:00 AM - 12:00 PM: 10
- CarbRatioEntry(start: "12:00", offset: 720, ratio: 12) // 12:00 PM - 12:00 AM: 12
- ]
- )
- let bgTargets = BGTargets(
- units: .mgdL,
- userPreferredUnits: .mgdL,
- targets: [
- BGTargetEntry(low: 100, high: 120, start: "00:00", offset: 0), // 12:00 AM - 8:00 AM: 100
- BGTargetEntry(low: 90, high: 110, start: "08:00", offset: 480) // 8:00 AM - 12:00 AM: 90
- ]
- )
- let isfValues = InsulinSensitivities(
- units: .mgdL,
- userPreferredUnits: .mgdL,
- sensitivities: [
- InsulinSensitivityEntry(sensitivity: 40, offset: 0, start: "00:00"), // 12:00 AM - 2:00 PM: 40
- InsulinSensitivityEntry(sensitivity: 45, offset: 840, start: "14:00") // 2:00 PM - 12:00 AM: 45
- ]
- )
- // STEP 3: Store test data
- fileStorage.save(basalProfile, as: OpenAPS.Settings.basalProfile)
- fileStorage.save(carbRatios, as: OpenAPS.Settings.carbRatios)
- fileStorage.save(bgTargets, as: OpenAPS.Settings.bgTargets)
- fileStorage.save(isfValues, as: OpenAPS.Settings.insulinSensitivities)
- // STEP 4: Define test cases with specific times and expected values
- // Format: (hour, minute, [setting type: expected value])
- let testTimes: [(hour: Int, minute: Int, expected: [SettingType: Decimal])] = [
- // Test midnight values (00:00)
- (
- hour: 0, minute: 0,
- expected: [
- .basal: 1.0, // First basal rate
- .carbRatio: 10, // First carb ratio
- .bgTarget: 100, // First target
- .isf: 40 // First ISF
- ]
- ),
- // Test mid-morning values (7:00)
- (
- hour: 7, minute: 0,
- expected: [
- .basal: 1.2, // Second basal rate (after 6:00)
- .carbRatio: 10, // Still first carb ratio
- .bgTarget: 100, // Still first target
- .isf: 40 // Still first ISF
- ]
- ),
- // Test afternoon values (15:00)
- (
- hour: 15, minute: 0,
- expected: [
- .basal: 1.1, // Third basal rate (after 12:00)
- .carbRatio: 12, // Second carb ratio (after 12:00)
- .bgTarget: 90, // Second target
- .isf: 45 // Second ISF (after 14:00)
- ]
- )
- ]
- // STEP 5: Test each time point
- for testTime in testTimes {
- // Create a date object for the test time
- let calendar = Calendar.current
- var components = calendar.dateComponents([.year, .month, .day], from: Date())
- components.hour = testTime.hour
- components.minute = testTime.minute
- components.second = 0
- guard let testDate = calendar.date(from: components) else {
- throw TestError("Failed to create test date")
- }
- // Test each setting type at this time
- for (type, expectedValue) in testTime.expected {
- // Get the actual value for this setting at the test time
- let value = await getCurrentSettingValue(for: type, at: testDate)
- // Compare with expected value
- #expect(
- value == expectedValue,
- """
- Failed at \(testTime.hour):\(String(format: "%02d", testTime.minute))
- Setting: \(type)
- Expected: \(expectedValue)
- Actual: \(value)
- """
- )
- }
- }
- // STEP 6: Cleanup - Restore original settings
- if let originalBasalProfile = originalBasalProfile {
- fileStorage.save(originalBasalProfile, as: OpenAPS.Settings.basalProfile)
- }
- if let originalCarbRatios = originalCarbRatios {
- fileStorage.save(originalCarbRatios, as: OpenAPS.Settings.carbRatios)
- }
- if let originalBGTargets = originalBGTargets {
- fileStorage.save(originalBGTargets, as: OpenAPS.Settings.bgTargets)
- }
- if let originalISFValues = originalISFValues {
- fileStorage.save(originalISFValues, as: OpenAPS.Settings.insulinSensitivities)
- }
- }
- }
- // Copied over from BolusCalculationManager as they are not included in the protocol definition (and I don´t want them to be included)
- extension BolusCalculatorTests {
- private enum SettingType {
- case basal
- case carbRatio
- case bgTarget
- case isf
- }
- /// Retrieves current settings from the SettingsManager
- /// - Returns: Tuple containing units, fraction, fattyMealFactor, sweetMealFactor, and maxCarbs settings
- private func getSettings() async -> (
- units: GlucoseUnits,
- fraction: Decimal,
- fattyMealFactor: Decimal,
- sweetMealFactor: Decimal,
- maxCarbs: Decimal
- ) {
- return (
- units: settingsManager.settings.units,
- fraction: settingsManager.settings.overrideFactor,
- fattyMealFactor: settingsManager.settings.fattyMealFactor,
- sweetMealFactor: settingsManager.settings.sweetMealFactor,
- maxCarbs: settingsManager.settings.maxCarbs
- )
- }
- /// Gets the current setting value for a specific setting type based on the time of day
- /// - Parameter type: The type of setting to retrieve (basal, carbRatio, bgTarget, or isf)
- /// - Returns: The current decimal value for the specified setting type
- private func getCurrentSettingValue(for type: SettingType, at date: Date) async -> Decimal {
- let calendar = Calendar.current
- let midnight = calendar.startOfDay(for: date)
- let minutesSinceMidnight = calendar.dateComponents([.minute], from: midnight, to: date).minute ?? 0
- switch type {
- case .basal:
- let profile = await getBasalProfile()
- return profile.last { $0.minutes <= minutesSinceMidnight }?.rate ?? 0
- case .carbRatio:
- let ratios = await getCarbRatios()
- return ratios.schedule.last { $0.offset <= minutesSinceMidnight }?.ratio ?? 0
- case .bgTarget:
- let targets = await getBGTargets()
- return targets.targets.last { $0.offset <= minutesSinceMidnight }?.low ?? 0
- case .isf:
- let sensitivities = await getISFValues()
- return sensitivities.sensitivities.last { $0.offset <= minutesSinceMidnight }?.sensitivity ?? 0
- }
- }
- /// Retrieves the pump settings from storage
- /// - Returns: PumpSettings object containing pump configuration
- private func getPumpSettings() async -> PumpSettings {
- await fileStorage.retrieveAsync(OpenAPS.Settings.settings, as: PumpSettings.self)
- ?? PumpSettings(from: OpenAPS.defaults(for: OpenAPS.Settings.settings))
- ?? PumpSettings(insulinActionCurve: 10, maxBolus: 10, maxBasal: 2)
- }
- /// Retrieves the basal profile from storage
- /// - Returns: Array of BasalProfileEntry objects
- private func getBasalProfile() async -> [BasalProfileEntry] {
- await fileStorage.retrieveAsync(OpenAPS.Settings.basalProfile, as: [BasalProfileEntry].self)
- ?? [BasalProfileEntry](from: OpenAPS.defaults(for: OpenAPS.Settings.basalProfile))
- ?? []
- }
- /// Retrieves carb ratios from storage
- /// - Returns: CarbRatios object containing carb ratio schedule
- private func getCarbRatios() async -> CarbRatios {
- await fileStorage.retrieveAsync(OpenAPS.Settings.carbRatios, as: CarbRatios.self)
- ?? CarbRatios(from: OpenAPS.defaults(for: OpenAPS.Settings.carbRatios))
- ?? CarbRatios(units: .grams, schedule: [])
- }
- /// Retrieves blood glucose targets from storage
- /// - Returns: BGTargets object containing target schedule
- private func getBGTargets() async -> BGTargets {
- await fileStorage.retrieveAsync(OpenAPS.Settings.bgTargets, as: BGTargets.self)
- ?? BGTargets(from: OpenAPS.defaults(for: OpenAPS.Settings.bgTargets))
- ?? BGTargets(units: .mgdL, userPreferredUnits: .mgdL, targets: [])
- }
- /// Retrieves insulin sensitivity factors from storage
- /// - Returns: InsulinSensitivities object containing sensitivity schedule
- private func getISFValues() async -> InsulinSensitivities {
- await fileStorage.retrieveAsync(OpenAPS.Settings.insulinSensitivities, as: InsulinSensitivities.self)
- ?? InsulinSensitivities(from: OpenAPS.defaults(for: OpenAPS.Settings.insulinSensitivities))
- ?? InsulinSensitivities(
- units: .mgdL,
- userPreferredUnits: .mgdL,
- sensitivities: []
- )
- }
- }
|