|
|
@@ -264,11 +264,12 @@ final class OpenAPS {
|
|
|
return .bolus(bolusDTO)
|
|
|
}
|
|
|
|
|
|
- func simulateDetermineBasal(
|
|
|
+ func determineBasal(
|
|
|
currentTemp: TempBasal,
|
|
|
clock: Date = Date(),
|
|
|
- carbs: Decimal,
|
|
|
- iob: Decimal
|
|
|
+ carbs: Decimal? = nil,
|
|
|
+ iob: Decimal? = nil,
|
|
|
+ simulation: Bool = false
|
|
|
) async throws -> Determination? {
|
|
|
debug(.openAPS, "Start determineBasal")
|
|
|
|
|
|
@@ -281,7 +282,7 @@ final class OpenAPS {
|
|
|
|
|
|
// Perform asynchronous calls in parallel
|
|
|
async let pumpHistoryObjectIDs = fetchPumpHistoryObjectIDs() ?? []
|
|
|
- async let carbs = fetchAndProcessCarbs(additionalCarbs: carbs)
|
|
|
+ async let carbs = fetchAndProcessCarbs(additionalCarbs: carbs ?? 0)
|
|
|
async let glucose = fetchAndProcessGlucose()
|
|
|
async let oref2 = oref2()
|
|
|
async let profileAsync = loadFileFromStorageAsync(name: Settings.profile)
|
|
|
@@ -312,7 +313,6 @@ final class OpenAPS {
|
|
|
reservoirAsync,
|
|
|
preferencesAsync
|
|
|
)
|
|
|
-// print("carbs: \(carbsAsJSON)")
|
|
|
|
|
|
// Meal
|
|
|
let meal = try await self.meal(
|
|
|
@@ -331,106 +331,10 @@ final class OpenAPS {
|
|
|
clock: dateFormattedAsString,
|
|
|
autosens: autosens.isEmpty ? .null : autosens
|
|
|
)
|
|
|
-// print("pumphistory : \(pumpHistoryJSON)")
|
|
|
- print("iob: \(iob)")
|
|
|
-// print("profile: \(profile)")
|
|
|
|
|
|
- // Determine basal
|
|
|
- let orefDetermination = try await determineBasal(
|
|
|
- glucose: glucoseAsJSON,
|
|
|
- currentTemp: tempBasal,
|
|
|
- iob: iob,
|
|
|
- profile: profile,
|
|
|
- autosens: autosens.isEmpty ? .null : autosens,
|
|
|
- meal: meal,
|
|
|
- microBolusAllowed: true,
|
|
|
- reservoir: reservoir,
|
|
|
- pumpHistory: pumpHistoryJSON,
|
|
|
- preferences: preferences,
|
|
|
- basalProfile: basalProfile,
|
|
|
- oref2_variables: oref2_variables
|
|
|
- )
|
|
|
-
|
|
|
-// debug(.openAPS, "oref 2 scheiß: \(oref2_variables)")
|
|
|
- debug(.openAPS, "Determinated: \(orefDetermination)")
|
|
|
-
|
|
|
- if var determination = Determination(from: orefDetermination), let deliverAt = determination.deliverAt {
|
|
|
- // set both timestamp and deliverAt to the SAME date; this will be updated for timestamp once it is enacted
|
|
|
- // AAPS does it the same way! we'll follow their example!
|
|
|
- determination.timestamp = deliverAt
|
|
|
-
|
|
|
- return determination
|
|
|
- } else {
|
|
|
- return nil
|
|
|
+ if !simulation {
|
|
|
+ storage.save(iob, as: Monitor.iob)
|
|
|
}
|
|
|
- }
|
|
|
-
|
|
|
- func determineBasal(currentTemp: TempBasal, clock: Date = Date()) async throws -> Determination? {
|
|
|
- debug(.openAPS, "Start determineBasal")
|
|
|
-
|
|
|
- // clock
|
|
|
- let dateFormatted = OpenAPS.dateFormatter.string(from: clock)
|
|
|
- let dateFormattedAsString = "\"\(dateFormatted)\""
|
|
|
-
|
|
|
- // temp_basal
|
|
|
- let tempBasal = currentTemp.rawJSON
|
|
|
-
|
|
|
- // Perform asynchronous calls in parallel
|
|
|
- async let pumpHistoryObjectIDs = fetchPumpHistoryObjectIDs() ?? []
|
|
|
- async let carbs = fetchAndProcessCarbs()
|
|
|
- async let glucose = fetchAndProcessGlucose()
|
|
|
- async let oref2 = oref2()
|
|
|
- async let profileAsync = loadFileFromStorageAsync(name: Settings.profile)
|
|
|
- async let basalAsync = loadFileFromStorageAsync(name: Settings.basalProfile)
|
|
|
- async let autosenseAsync = loadFileFromStorageAsync(name: Settings.autosense)
|
|
|
- async let reservoirAsync = loadFileFromStorageAsync(name: Monitor.reservoir)
|
|
|
- async let preferencesAsync = loadFileFromStorageAsync(name: Settings.preferences)
|
|
|
-
|
|
|
- // Await the results of asynchronous tasks
|
|
|
- let (
|
|
|
- pumpHistoryJSON,
|
|
|
- carbsAsJSON,
|
|
|
- glucoseAsJSON,
|
|
|
- oref2_variables,
|
|
|
- profile,
|
|
|
- basalProfile,
|
|
|
- autosens,
|
|
|
- reservoir,
|
|
|
- preferences
|
|
|
- ) = await (
|
|
|
- parsePumpHistory(await pumpHistoryObjectIDs),
|
|
|
- carbs,
|
|
|
- glucose,
|
|
|
- oref2,
|
|
|
- profileAsync,
|
|
|
- basalAsync,
|
|
|
- autosenseAsync,
|
|
|
- reservoirAsync,
|
|
|
- preferencesAsync
|
|
|
- )
|
|
|
-
|
|
|
- // TODO: - Save and fetch profile/basalProfile in/from UserDefaults!
|
|
|
-
|
|
|
- // Meal
|
|
|
- let meal = try await self.meal(
|
|
|
- pumphistory: pumpHistoryJSON,
|
|
|
- profile: profile,
|
|
|
- basalProfile: basalProfile,
|
|
|
- clock: dateFormattedAsString,
|
|
|
- carbs: carbsAsJSON,
|
|
|
- glucose: glucoseAsJSON
|
|
|
- )
|
|
|
-
|
|
|
- // IOB
|
|
|
- let iob = try await self.iob(
|
|
|
- pumphistory: pumpHistoryJSON,
|
|
|
- profile: profile,
|
|
|
- clock: dateFormattedAsString,
|
|
|
- autosens: autosens.isEmpty ? .null : autosens
|
|
|
- )
|
|
|
-
|
|
|
- // TODO: refactor this to core data
|
|
|
- storage.save(iob, as: Monitor.iob)
|
|
|
|
|
|
// Determine basal
|
|
|
let orefDetermination = try await determineBasal(
|
|
|
@@ -451,12 +355,11 @@ final class OpenAPS {
|
|
|
debug(.openAPS, "Determinated: \(orefDetermination)")
|
|
|
|
|
|
if var determination = Determination(from: orefDetermination), let deliverAt = determination.deliverAt {
|
|
|
- // set both timestamp and deliverAt to the SAME date; this will be updated for timestamp once it is enacted
|
|
|
- // AAPS does it the same way! we'll follow their example!
|
|
|
determination.timestamp = deliverAt
|
|
|
|
|
|
- // save to core data asynchronously
|
|
|
- await processDetermination(determination)
|
|
|
+ if !simulation {
|
|
|
+ await processDetermination(determination)
|
|
|
+ }
|
|
|
|
|
|
return determination
|
|
|
} else {
|