| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205 |
- import Combine
- import CoreData
- import Foundation
- import JavaScriptCore
- final class OpenAPS {
- private let jsWorker = JavaScriptWorker()
- private let processQueue = DispatchQueue(label: "OpenAPS.processQueue", qos: .utility)
- private let storage: FileStorage
- private let tddStorage: TDDStorage
- let context = CoreDataStack.shared.newTaskContext()
- let jsonConverter = JSONConverter()
- init(storage: FileStorage, tddStorage: TDDStorage) {
- self.storage = storage
- self.tddStorage = tddStorage
- }
- static let dateFormatter: ISO8601DateFormatter = {
- let formatter = ISO8601DateFormatter()
- formatter.formatOptions = [.withInternetDateTime, .withFractionalSeconds]
- return formatter
- }()
- // Helper function to convert a Decimal? to NSDecimalNumber?
- func decimalToNSDecimalNumber(_ value: Decimal?) -> NSDecimalNumber? {
- guard let value = value else { return nil }
- return NSDecimalNumber(decimal: value)
- }
- // Use the helper function for cleaner code
- func processDetermination(_ determination: Determination) async {
- await context.perform {
- let newOrefDetermination = OrefDetermination(context: self.context)
- newOrefDetermination.id = UUID()
- newOrefDetermination.insulinSensitivity = self.decimalToNSDecimalNumber(determination.isf)
- newOrefDetermination.currentTarget = self.decimalToNSDecimalNumber(determination.current_target)
- newOrefDetermination.eventualBG = determination.eventualBG.map(NSDecimalNumber.init)
- newOrefDetermination.deliverAt = determination.deliverAt
- newOrefDetermination.carbRatio = self.decimalToNSDecimalNumber(determination.carbRatio)
- newOrefDetermination.glucose = self.decimalToNSDecimalNumber(determination.bg)
- newOrefDetermination.reservoir = self.decimalToNSDecimalNumber(determination.reservoir)
- newOrefDetermination.insulinReq = self.decimalToNSDecimalNumber(determination.insulinReq)
- newOrefDetermination.temp = determination.temp?.rawValue ?? "absolute"
- newOrefDetermination.rate = self.decimalToNSDecimalNumber(determination.rate)
- newOrefDetermination.reason = determination.reason
- newOrefDetermination.duration = self.decimalToNSDecimalNumber(determination.duration)
- newOrefDetermination.iob = self.decimalToNSDecimalNumber(determination.iob)
- newOrefDetermination.threshold = self.decimalToNSDecimalNumber(determination.threshold)
- newOrefDetermination.minDelta = self.decimalToNSDecimalNumber(determination.minDelta)
- newOrefDetermination.sensitivityRatio = self.decimalToNSDecimalNumber(determination.sensitivityRatio)
- newOrefDetermination.expectedDelta = self.decimalToNSDecimalNumber(determination.expectedDelta)
- newOrefDetermination.cob = Int16(Int(determination.cob ?? 0))
- newOrefDetermination.smbToDeliver = determination.units.map { NSDecimalNumber(decimal: $0) }
- newOrefDetermination.carbsRequired = Int16(Int(determination.carbsReq ?? 0))
- newOrefDetermination.isUploadedToNS = false
- if let predictions = determination.predictions {
- ["iob": predictions.iob, "zt": predictions.zt, "cob": predictions.cob, "uam": predictions.uam]
- .forEach { type, values in
- if let values = values {
- let forecast = Forecast(context: self.context)
- forecast.id = UUID()
- forecast.type = type
- forecast.date = Date()
- forecast.orefDetermination = newOrefDetermination
- for (index, value) in values.enumerated() {
- let forecastValue = ForecastValue(context: self.context)
- forecastValue.index = Int32(index)
- forecastValue.value = Int32(value)
- forecast.addToForecastValues(forecastValue)
- }
- newOrefDetermination.addToForecasts(forecast)
- }
- }
- }
- }
- // First save the current Determination to Core Data
- await attemptToSaveContext()
- }
- func attemptToSaveContext() async {
- await context.perform {
- do {
- guard self.context.hasChanges else { return }
- try self.context.save()
- } catch {
- debugPrint("\(DebuggingIdentifiers.failed) \(#file) \(#function) Failed to save Determination to Core Data")
- }
- }
- }
- // fetch glucose to pass it to the meal function and to determine basal
- private func fetchAndProcessGlucose(fetchLimit: Int?) async throws -> String {
- let results = try await CoreDataStack.shared.fetchEntitiesAsync(
- ofType: GlucoseStored.self,
- onContext: context,
- predicate: NSPredicate.predicateForOneDayAgoInMinutes,
- key: "date",
- ascending: false,
- fetchLimit: fetchLimit,
- batchSize: 48
- )
- return try await context.perform {
- guard let glucoseResults = results as? [GlucoseStored] else {
- throw CoreDataError.fetchError(function: #function, file: #file)
- }
- // convert to JSON
- return self.jsonConverter.convertToJSON(glucoseResults)
- }
- }
- private func fetchAndProcessCarbs(additionalCarbs: Decimal? = nil, carbsDate: Date? = nil) async throws -> String {
- let results = try await CoreDataStack.shared.fetchEntitiesAsync(
- ofType: CarbEntryStored.self,
- onContext: context,
- predicate: NSPredicate.predicateForOneDayAgo,
- key: "date",
- ascending: false
- )
- let json = try await context.perform {
- guard let carbResults = results as? [CarbEntryStored] else {
- throw CoreDataError.fetchError(function: #function, file: #file)
- }
- var jsonArray = self.jsonConverter.convertToJSON(carbResults)
- if let additionalCarbs = additionalCarbs {
- let formattedDate = carbsDate.map { ISO8601DateFormatter().string(from: $0) } ?? ISO8601DateFormatter()
- .string(from: Date())
- let additionalEntry = [
- "carbs": Double(additionalCarbs),
- "actualDate": formattedDate,
- "id": UUID().uuidString,
- "note": NSNull(),
- "protein": 0,
- "created_at": formattedDate,
- "isFPU": false,
- "fat": 0,
- "enteredBy": "Trio"
- ] as [String: Any]
- // Assuming jsonArray is a String, convert it to a list of dictionaries first
- if let jsonData = jsonArray.data(using: .utf8) {
- var jsonList = try? JSONSerialization.jsonObject(with: jsonData, options: []) as? [[String: Any]]
- jsonList?.append(additionalEntry)
- // Convert back to JSON string
- if let updatedJsonData = try? JSONSerialization
- .data(withJSONObject: jsonList ?? [], options: .prettyPrinted)
- {
- jsonArray = String(data: updatedJsonData, encoding: .utf8) ?? jsonArray
- }
- }
- }
- return jsonArray
- }
- return json
- }
- private func fetchPumpHistoryObjectIDs() async throws -> [NSManagedObjectID]? {
- let results = try await CoreDataStack.shared.fetchEntitiesAsync(
- ofType: PumpEventStored.self,
- onContext: context,
- predicate: NSPredicate.pumpHistoryLast1440Minutes,
- key: "timestamp",
- ascending: false,
- batchSize: 50
- )
- return try await context.perform {
- guard let pumpEventResults = results as? [PumpEventStored] else {
- throw CoreDataError.fetchError(function: #function, file: #file)
- }
- return pumpEventResults.map(\.objectID)
- }
- }
- private func parsePumpHistory(
- _ pumpHistoryObjectIDs: [NSManagedObjectID],
- simulatedBolusAmount: Decimal? = nil
- ) async -> String {
- // Return an empty JSON object if the list of object IDs is empty
- guard !pumpHistoryObjectIDs.isEmpty else { return "{}" }
- // Execute all operations on the background context
- return await context.perform {
- // Load and map pump events to DTOs
- var dtos = self.loadAndMapPumpEvents(pumpHistoryObjectIDs)
- // Optionally add the IOB as a DTO
- if let simulatedBolusAmount = simulatedBolusAmount {
- let simulatedBolusDTO = self.createSimulatedBolusDTO(simulatedBolusAmount: simulatedBolusAmount)
- dtos.insert(simulatedBolusDTO, at: 0)
- }
- // Convert the DTOs to JSON
- return self.jsonConverter.convertToJSON(dtos)
- }
- }
- private func loadAndMapPumpEvents(_ pumpHistoryObjectIDs: [NSManagedObjectID]) -> [PumpEventDTO] {
- OpenAPS.loadAndMapPumpEvents(pumpHistoryObjectIDs, from: context)
- }
- /// Fetches and parses pump events, expose this as static and not private for testing
- static func loadAndMapPumpEvents(
- _ pumpHistoryObjectIDs: [NSManagedObjectID],
- from context: NSManagedObjectContext
- ) -> [PumpEventDTO] {
- // Load the pump events from the object IDs
- let pumpHistory: [PumpEventStored] = pumpHistoryObjectIDs
- .compactMap { context.object(with: $0) as? PumpEventStored }
- // Create the DTOs
- let dtos: [PumpEventDTO] = pumpHistory.flatMap { event -> [PumpEventDTO] in
- var eventDTOs: [PumpEventDTO] = []
- if let bolusDTO = event.toBolusDTOEnum() {
- eventDTOs.append(bolusDTO)
- }
- if let tempBasalDurationDTO = event.toTempBasalDurationDTOEnum() {
- eventDTOs.append(tempBasalDurationDTO)
- }
- if let tempBasalDTO = event.toTempBasalDTOEnum() {
- eventDTOs.append(tempBasalDTO)
- }
- if let pumpSuspendDTO = event.toPumpSuspendDTO() {
- eventDTOs.append(pumpSuspendDTO)
- }
- if let pumpResumeDTO = event.toPumpResumeDTO() {
- eventDTOs.append(pumpResumeDTO)
- }
- if let rewindDTO = event.toRewindDTO() {
- eventDTOs.append(rewindDTO)
- }
- if let primeDTO = event.toPrimeDTO() {
- eventDTOs.append(primeDTO)
- }
- return eventDTOs
- }
- return dtos
- }
- private func createSimulatedBolusDTO(simulatedBolusAmount: Decimal) -> PumpEventDTO {
- let oneSecondAgo = Calendar.current
- .date(
- byAdding: .second,
- value: -1,
- to: Date()
- )! // adding -1s to the current Date ensures that oref actually uses the mock entry to calculate iob and not guard it away
- let dateFormatted = PumpEventStored.dateFormatter.string(from: oneSecondAgo)
- let bolusDTO = BolusDTO(
- id: UUID().uuidString,
- timestamp: dateFormatted,
- amount: Double(simulatedBolusAmount),
- isExternal: false,
- isSMB: true,
- duration: 0,
- _type: "Bolus"
- )
- return .bolus(bolusDTO)
- }
- func determineBasal(
- currentTemp: TempBasal,
- clock: Date,
- useSwiftOref: Bool,
- simulatedCarbsAmount: Decimal? = nil,
- simulatedBolusAmount: Decimal? = nil,
- simulatedCarbsDate: Date? = nil,
- simulation: Bool = false
- ) async throws -> Determination? {
- debug(.openAPS, "Start determineBasal")
- // temp_basal
- let tempBasal = currentTemp.rawJSON
- // Perform asynchronous calls in parallel
- async let pumpHistoryObjectIDs = fetchPumpHistoryObjectIDs() ?? []
- async let carbs = fetchAndProcessCarbs(additionalCarbs: simulatedCarbsAmount ?? 0, carbsDate: simulatedCarbsDate)
- async let glucose = fetchAndProcessGlucose(fetchLimit: 72)
- async let prepareTrioCustomOrefVariables = prepareTrioCustomOrefVariables()
- 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 = storage.retrieveAsync(OpenAPS.Settings.preferences, as: Preferences.self) ?? Preferences()
- async let hasSufficientTddForDynamic = tddStorage.hasSufficientTDD()
- // Await the results of asynchronous tasks
- let (
- pumpHistoryJSON,
- carbsAsJSON,
- glucoseAsJSON,
- trioCustomOrefVariables,
- profile,
- basalProfile,
- autosens,
- reservoir,
- hasSufficientTdd
- ) = await (
- try parsePumpHistory(await pumpHistoryObjectIDs, simulatedBolusAmount: simulatedBolusAmount),
- try carbs,
- try glucose,
- try prepareTrioCustomOrefVariables,
- profileAsync,
- basalAsync,
- autosenseAsync,
- reservoirAsync,
- try hasSufficientTddForDynamic
- )
- // Meal calculation
- let meal = try await self.meal(
- pumphistory: pumpHistoryJSON,
- profile: profile,
- basalProfile: basalProfile,
- clock: clock,
- carbs: carbsAsJSON,
- glucose: glucoseAsJSON,
- useSwiftOref: useSwiftOref
- )
- // IOB calculation
- let iob = try await self.iob(
- pumphistory: pumpHistoryJSON,
- profile: profile,
- clock: clock,
- autosens: autosens.isEmpty ? .null : autosens,
- useSwiftOref: useSwiftOref
- )
- // TODO: refactor this to core data
- if !simulation {
- storage.save(iob, as: Monitor.iob)
- }
- var preferences = await preferencesAsync
- if !hasSufficientTdd, preferences.useNewFormula || (preferences.useNewFormula && preferences.sigmoid) {
- debug(.openAPS, "Insufficient TDD for dynamic formula; disabling for determine basal run.")
- preferences.useNewFormula = false
- preferences.sigmoid = false
- }
- // 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,
- trioCustomOrefVariables: trioCustomOrefVariables,
- useSwiftOref: useSwiftOref
- )
- debug(.openAPS, "\(simulation ? "[SIMULATION]" : "") OREF DETERMINATION: \(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
- if !simulation {
- // save to core data asynchronously
- await processDetermination(determination)
- }
- return determination
- } else {
- debug(
- .openAPS,
- "\(DebuggingIdentifiers.failed) No determination data. orefDetermination: \(orefDetermination), Determination(from: orefDetermination): \(String(describing: Determination(from: orefDetermination))), deliverAt: \(String(describing: Determination(from: orefDetermination)?.deliverAt))"
- )
- throw APSError.apsError(message: "No determination data.")
- }
- }
- func prepareTrioCustomOrefVariables() async throws -> RawJSON {
- try await context.perform {
- // Retrieve user preferences
- let userPreferences = self.storage.retrieve(OpenAPS.Settings.preferences, as: Preferences.self)
- let weightPercentage = userPreferences?.weightPercentage ?? 1.0
- let maxSMBBasalMinutes = userPreferences?.maxSMBBasalMinutes ?? 30
- let maxUAMBasalMinutes = userPreferences?.maxUAMSMBBasalMinutes ?? 30
- // Fetch historical events for Total Daily Dose (TDD) calculation
- let tenDaysAgo = Date().addingTimeInterval(-10.days.timeInterval)
- let twoHoursAgo = Date().addingTimeInterval(-2.hours.timeInterval)
- let historicalTDDData = try self.fetchHistoricalTDDData(from: tenDaysAgo)
- // Fetch the last active Override
- let activeOverrides = try self.fetchActiveOverrides()
- let isOverrideActive = activeOverrides.first?.enabled ?? false
- let overridePercentage = Decimal(activeOverrides.first?.percentage ?? 100)
- let isOverrideIndefinite = activeOverrides.first?.indefinite ?? true
- let disableSMBs = activeOverrides.first?.smbIsOff ?? false
- let overrideTargetBG = activeOverrides.first?.target?.decimalValue ?? 0
- // Calculate averages for Total Daily Dose (TDD)
- let totalTDD = historicalTDDData.compactMap { ($0["total"] as? NSDecimalNumber)?.decimalValue }.reduce(0, +)
- let totalDaysCount = max(historicalTDDData.count, 1)
- // Fetch recent TDD data for the past two hours
- let recentTDDData = historicalTDDData.filter { ($0["date"] as? Date ?? Date()) >= twoHoursAgo }
- let recentDataCount = max(recentTDDData.count, 1)
- let recentTotalTDD = recentTDDData.compactMap { ($0["total"] as? NSDecimalNumber)?.decimalValue }
- .reduce(0, +)
- let currentTDD = historicalTDDData.last?["total"] as? Decimal ?? 0
- let averageTDDLastTwoHours = recentTotalTDD / Decimal(recentDataCount)
- let averageTDDLastTenDays = totalTDD / Decimal(totalDaysCount)
- let weightedTDD = weightPercentage * averageTDDLastTwoHours + (1 - weightPercentage) * averageTDDLastTenDays
- let glucose = try self.fetchGlucose()
- // Prepare Trio's custom oref variables
- let trioCustomOrefVariablesData = TrioCustomOrefVariables(
- average_total_data: currentTDD > 0 ? averageTDDLastTenDays : 0,
- weightedAverage: currentTDD > 0 ? weightedTDD : 1,
- currentTDD: currentTDD,
- past2hoursAverage: currentTDD > 0 ? averageTDDLastTwoHours : 0,
- date: Date(),
- overridePercentage: overridePercentage,
- useOverride: isOverrideActive,
- duration: activeOverrides.first?.duration?.decimalValue ?? 0,
- unlimited: isOverrideIndefinite,
- overrideTarget: overrideTargetBG,
- smbIsOff: disableSMBs,
- advancedSettings: activeOverrides.first?.advancedSettings ?? false,
- isfAndCr: activeOverrides.first?.isfAndCr ?? false,
- isf: activeOverrides.first?.isf ?? false,
- cr: activeOverrides.first?.cr ?? false,
- smbIsScheduledOff: activeOverrides.first?.smbIsScheduledOff ?? false,
- start: (activeOverrides.first?.start ?? 0) as Decimal,
- end: (activeOverrides.first?.end ?? 0) as Decimal,
- smbMinutes: activeOverrides.first?.smbMinutes?.decimalValue ?? maxSMBBasalMinutes,
- uamMinutes: activeOverrides.first?.uamMinutes?.decimalValue ?? maxUAMBasalMinutes
- )
- // Save and return contents of Trio's custom oref variables
- self.storage.save(trioCustomOrefVariablesData, as: OpenAPS.Monitor.trio_custom_oref_variables)
- return self.loadFileFromStorage(name: Monitor.trio_custom_oref_variables)
- }
- }
- func autosense(useSwiftOref: Bool) async throws -> Autosens? {
- debug(.openAPS, "Start autosens")
- // Perform asynchronous calls in parallel
- async let pumpHistoryObjectIDs = fetchPumpHistoryObjectIDs() ?? []
- async let carbs = fetchAndProcessCarbs()
- async let glucose = fetchAndProcessGlucose(fetchLimit: nil)
- async let getProfile = loadFileFromStorageAsync(name: Settings.profile)
- async let getBasalProfile = loadFileFromStorageAsync(name: Settings.basalProfile)
- async let getTempTargets = loadFileFromStorageAsync(name: Settings.tempTargets)
- // Await the results of asynchronous tasks
- let (pumpHistoryJSON, carbsAsJSON, glucoseAsJSON, profile, basalProfile, tempTargets) = await (
- try parsePumpHistory(await pumpHistoryObjectIDs),
- try carbs,
- try glucose,
- getProfile,
- getBasalProfile,
- getTempTargets
- )
- // Autosense
- let autosenseResult = try await autosense(
- glucose: glucoseAsJSON,
- pumpHistory: pumpHistoryJSON,
- basalprofile: basalProfile,
- profile: profile,
- carbs: carbsAsJSON,
- temptargets: tempTargets,
- useSwiftOref: useSwiftOref
- )
- debug(.openAPS, "AUTOSENS: \(autosenseResult)")
- if var autosens = Autosens(from: autosenseResult) {
- autosens.timestamp = Date()
- await storage.saveAsync(autosens, as: Settings.autosense)
- return autosens
- } else {
- return nil
- }
- }
- func createProfiles(useSwiftOref: Bool) async throws {
- debug(.openAPS, "Start creating pump profile and user profile")
- // Load required settings and profiles asynchronously
- async let getPumpSettings = loadFileFromStorageAsync(name: Settings.settings)
- async let getBGTargets = loadFileFromStorageAsync(name: Settings.bgTargets)
- async let getBasalProfile = loadFileFromStorageAsync(name: Settings.basalProfile)
- async let getISF = loadFileFromStorageAsync(name: Settings.insulinSensitivities)
- async let getCR = loadFileFromStorageAsync(name: Settings.carbRatios)
- async let getTempTargets = loadFileFromStorageAsync(name: Settings.tempTargets)
- async let getModel = loadFileFromStorageAsync(name: Settings.model)
- async let getTrioSettingDefaults = loadFileFromStorageAsync(name: Trio.settings)
- let (pumpSettings, bgTargets, basalProfile, isf, cr, tempTargets, model, trioSettings) = await (
- getPumpSettings,
- getBGTargets,
- getBasalProfile,
- getISF,
- getCR,
- getTempTargets,
- getModel,
- getTrioSettingDefaults
- )
- // Retrieve user preferences, or set defaults if not available
- let preferences = storage.retrieve(OpenAPS.Settings.preferences, as: Preferences.self) ?? Preferences()
- let defaultHalfBasalTarget = preferences.halfBasalExerciseTarget
- var adjustedPreferences = preferences
- // Check for active Temp Targets and adjust HBT if necessary
- try await context.perform {
- // Check if a Temp Target is active and check HBT differs from setting and adjust
- if let activeTempTarget = try self.fetchActiveTempTargets().first,
- activeTempTarget.enabled,
- let targetValue = activeTempTarget.target?.decimalValue
- {
- // Compute effective HBT - handles both custom HBT and standard TT (where HBT might need adjustment)
- let effectiveHBT = TempTargetCalculations.computeEffectiveHBT(
- tempTargetHalfBasalTarget: activeTempTarget.halfBasalTarget?.decimalValue,
- settingHalfBasalTarget: defaultHalfBasalTarget,
- target: targetValue,
- autosensMax: preferences.autosensMax
- )
- if let effectiveHBT, effectiveHBT != defaultHalfBasalTarget {
- adjustedPreferences.halfBasalExerciseTarget = effectiveHBT
- let percentage = Int(TempTargetCalculations.computeAdjustedPercentage(
- halfBasalTarget: effectiveHBT,
- target: targetValue,
- autosensMax: preferences.autosensMax
- ))
- debug(
- .openAPS,
- "TempTarget: target=\(targetValue), HBT=\(defaultHalfBasalTarget), effectiveHBT=\(effectiveHBT), percentage=\(percentage)%, adjustmentType=Custom"
- )
- }
- }
- // Overwrite the lowTTlowersSens if autosensMax does not support it
- if preferences.lowTemptargetLowersSensitivity, preferences.autosensMax <= 1 {
- adjustedPreferences.lowTemptargetLowersSensitivity = false
- debug(.openAPS, "Setting lowTTlowersSens to false due to insufficient autosensMax: \(preferences.autosensMax)")
- }
- }
- do {
- let pumpProfile = try await makeProfile(
- preferences: adjustedPreferences,
- pumpSettings: pumpSettings,
- bgTargets: bgTargets,
- basalProfile: basalProfile,
- isf: isf,
- carbRatio: cr,
- tempTargets: tempTargets,
- model: model,
- autotune: RawJSON.null,
- trioSettings: trioSettings,
- useSwiftOref: useSwiftOref
- )
- let profile = try await makeProfile(
- preferences: adjustedPreferences,
- pumpSettings: pumpSettings,
- bgTargets: bgTargets,
- basalProfile: basalProfile,
- isf: isf,
- carbRatio: cr,
- tempTargets: tempTargets,
- model: model,
- autotune: RawJSON.null,
- trioSettings: trioSettings,
- useSwiftOref: useSwiftOref
- )
- // Save the profiles
- await storage.saveAsync(pumpProfile, as: Settings.pumpProfile)
- await storage.saveAsync(profile, as: Settings.profile)
- } catch {
- debug(
- .apsManager,
- "\(DebuggingIdentifiers.failed) \(#file) \(#function) Failed to create pump profile and normal profile: \(error)"
- )
- throw error
- }
- }
- private func iob(pumphistory: JSON, profile: JSON, clock: JSON, autosens: JSON, useSwiftOref: Bool) async throws -> RawJSON {
- // FIXME: For now we'll just remove duplicate suspends here (ISSUE-399)
- var pumphistory = pumphistory
- if let pumpHistoryArray = try? JSONBridge.pumpHistory(from: pumphistory) {
- pumphistory = pumpHistoryArray.removingDuplicateSuspendResumeEvents().rawJSON
- }
- let startJavascriptAt = Date()
- let jsResult = await iobJavascript(pumphistory: pumphistory, profile: profile, clock: clock, autosens: autosens)
- let javascriptDuration = Date().timeIntervalSince(startJavascriptAt)
- // Important: we want to make sure that this flag ensures that none
- // of the native code runs
- guard useSwiftOref else {
- return try jsResult.returnOrThrow()
- }
- let startSwiftAt = Date()
- let (swiftResult, iobInputs) = OpenAPSSwift
- .iob(pumphistory: pumphistory, profile: profile, clock: clock, autosens: autosens)
- let swiftDuration = Date().timeIntervalSince(startSwiftAt)
- JSONCompare.logDifferences(
- function: .iob,
- swift: swiftResult,
- swiftDuration: swiftDuration,
- javascript: jsResult,
- javascriptDuration: javascriptDuration,
- iobInputs: iobInputs
- )
- return try jsResult.returnOrThrow()
- }
- func iobJavascript(pumphistory: JSON, profile: JSON, clock: JSON, autosens: JSON) async -> OrefFunctionResult {
- do {
- let result = try await withCheckedThrowingContinuation { continuation in
- jsWorker.inCommonContext { worker in
- worker.evaluateBatch(scripts: [
- Script(name: Prepare.log),
- Script(name: Bundle.iob),
- Script(name: Prepare.iob)
- ])
- let result = worker.call(function: Function.generate, with: [
- pumphistory,
- profile,
- clock,
- autosens
- ])
- continuation.resume(returning: result)
- }
- }
- return .success(result)
- } catch {
- return .failure(error)
- }
- }
- private func meal(
- pumphistory: JSON,
- profile: JSON,
- basalProfile: JSON,
- clock: JSON,
- carbs: JSON,
- glucose: JSON,
- useSwiftOref: Bool
- ) async throws -> RawJSON {
- let startJavascriptAt = Date()
- let jsResult = await mealJavascript(
- pumphistory: pumphistory,
- profile: profile,
- basalProfile: basalProfile,
- clock: clock,
- carbs: carbs,
- glucose: glucose
- )
- let javascriptDuration = Date().timeIntervalSince(startJavascriptAt)
- // Important: we want to make sure that this flag ensures that none
- // of the native code runs
- guard useSwiftOref else {
- return try jsResult.returnOrThrow()
- }
- let startSwiftAt = Date()
- let (swiftResult, mealInputs) = OpenAPSSwift
- .meal(
- pumphistory: pumphistory,
- profile: profile,
- basalProfile: basalProfile,
- clock: clock,
- carbs: carbs,
- glucose: glucose
- )
- let swiftDuration = Date().timeIntervalSince(startSwiftAt)
- JSONCompare.logDifferences(
- function: .meal,
- swift: swiftResult,
- swiftDuration: swiftDuration,
- javascript: jsResult,
- javascriptDuration: javascriptDuration,
- mealInputs: mealInputs
- )
- return try jsResult.returnOrThrow()
- }
- private func mealJavascript(
- pumphistory: JSON,
- profile: JSON,
- basalProfile: JSON,
- clock: JSON,
- carbs: JSON,
- glucose: JSON
- ) async -> OrefFunctionResult {
- do {
- let result = try await withCheckedThrowingContinuation { continuation in
- jsWorker.inCommonContext { worker in
- worker.evaluateBatch(scripts: [
- Script(name: Prepare.log),
- Script(name: Bundle.meal),
- Script(name: Prepare.meal)
- ])
- let result = worker.call(function: Function.generate, with: [
- pumphistory,
- profile,
- clock,
- glucose,
- basalProfile,
- carbs
- ])
- continuation.resume(returning: result)
- }
- }
- return .success(result)
- } catch {
- return .failure(error)
- }
- }
- private func autosense(
- glucose: JSON,
- pumpHistory: JSON,
- basalprofile: JSON,
- profile: JSON,
- carbs: JSON,
- temptargets: JSON,
- useSwiftOref: Bool
- ) async throws -> RawJSON {
- let startJavascriptAt = Date()
- let jsResult = await autosenseJavascript(
- glucose: glucose,
- pumpHistory: pumpHistory,
- basalprofile: basalprofile,
- profile: profile,
- carbs: carbs,
- temptargets: temptargets
- )
- let javascriptDuration = Date().timeIntervalSince(startJavascriptAt)
- // Important: we want to make sure that this flag ensures that none
- // of the native code runs
- guard useSwiftOref else {
- return try jsResult.returnOrThrow()
- }
- let startSwiftAt = Date()
- let (swiftResult, autosensInputs) = OpenAPSSwift
- .autosense(
- glucose: glucose,
- pumpHistory: pumpHistory,
- basalProfile: basalprofile,
- profile: profile,
- carbs: carbs,
- tempTargets: temptargets,
- clock: Date()
- )
- let swiftDuration = Date().timeIntervalSince(startSwiftAt)
- JSONCompare.logDifferences(
- function: .autosens,
- swift: swiftResult,
- swiftDuration: swiftDuration,
- javascript: jsResult,
- javascriptDuration: javascriptDuration,
- autosensInputs: autosensInputs
- )
- return try jsResult.returnOrThrow()
- }
- private func autosenseJavascript(
- glucose: JSON,
- pumpHistory: JSON,
- basalprofile: JSON,
- profile: JSON,
- carbs: JSON,
- temptargets: JSON
- ) async -> OrefFunctionResult {
- do {
- let result = try await withCheckedThrowingContinuation { continuation in
- jsWorker.inCommonContext { worker in
- worker.evaluateBatch(scripts: [
- Script(name: Prepare.log),
- Script(name: Bundle.autosens),
- Script(name: Prepare.autosens)
- ])
- let result = worker.call(function: Function.generate, with: [
- glucose,
- pumpHistory,
- basalprofile,
- profile,
- carbs,
- temptargets
- ])
- continuation.resume(returning: result)
- }
- }
- return .success(result)
- } catch {
- return .failure(error)
- }
- }
- private func determineBasal(
- glucose: JSON,
- currentTemp: JSON,
- iob: JSON,
- profile: JSON,
- autosens: JSON,
- meal: JSON,
- microBolusAllowed: Bool,
- reservoir: JSON,
- pumpHistory: JSON,
- preferences: JSON,
- basalProfile: JSON,
- trioCustomOrefVariables: JSON,
- useSwiftOref: Bool
- ) async throws -> RawJSON {
- let clock = Date()
- let startJavascriptAt = Date()
- let jsResult = await determineBasalJavascript(
- glucose: glucose,
- currentTemp: currentTemp,
- iob: iob,
- profile: profile,
- autosens: autosens,
- meal: meal,
- microBolusAllowed: microBolusAllowed,
- reservoir: reservoir,
- pumpHistory: pumpHistory,
- preferences: preferences,
- basalProfile: basalProfile,
- trioCustomOrefVariables: trioCustomOrefVariables,
- clock: clock
- )
- let javascriptDuration = Date().timeIntervalSince(startJavascriptAt)
- // Important: we want to make sure that this flag ensures that none
- // of the native code runs
- guard useSwiftOref else {
- return try jsResult.returnOrThrow()
- }
- let startSwiftAt = Date()
- let (swiftResult, determineBasalInputs) = OpenAPSSwift.determineBasal(
- glucose: glucose,
- currentTemp: currentTemp,
- iob: iob,
- profile: profile,
- autosens: autosens,
- meal: meal,
- microBolusAllowed: microBolusAllowed,
- reservoir: reservoir,
- pumpHistory: pumpHistory,
- preferences: preferences,
- basalProfile: basalProfile,
- trioCustomOrefVariables: trioCustomOrefVariables,
- clock: clock
- )
- let swiftDuration = Date().timeIntervalSince(startSwiftAt)
- JSONCompare.logDifferences(
- function: .determineBasal,
- swift: swiftResult,
- swiftDuration: swiftDuration,
- javascript: jsResult,
- javascriptDuration: javascriptDuration,
- determineBasalInputs: determineBasalInputs
- )
- return try jsResult.returnOrThrow()
- }
- private func determineBasalJavascript(
- glucose: JSON,
- currentTemp: JSON,
- iob: JSON,
- profile: JSON,
- autosens: JSON,
- meal: JSON,
- microBolusAllowed: Bool,
- reservoir: JSON,
- pumpHistory: JSON,
- preferences: JSON,
- basalProfile: JSON,
- trioCustomOrefVariables: JSON,
- clock: Date
- ) async -> OrefFunctionResult {
- do {
- let result = try await withCheckedThrowingContinuation { continuation in
- jsWorker.inCommonContext { worker in
- worker.evaluateBatch(scripts: [
- Script(name: Prepare.log),
- Script(name: Prepare.determineBasal),
- Script(name: Bundle.basalSetTemp),
- Script(name: Bundle.getLastGlucose),
- Script(name: Bundle.determineBasal)
- ])
- if let middleware = self.middlewareScript(name: OpenAPS.Middleware.determineBasal) {
- worker.evaluate(script: middleware)
- }
- let result = worker.call(function: Function.generate, with: [
- iob,
- currentTemp,
- glucose,
- profile,
- autosens,
- meal,
- microBolusAllowed,
- reservoir,
- clock,
- pumpHistory,
- preferences,
- basalProfile,
- trioCustomOrefVariables
- ])
- continuation.resume(returning: result)
- }
- }
- return .success(result)
- } catch {
- return .failure(error)
- }
- }
- private func exportDefaultPreferences() -> RawJSON {
- dispatchPrecondition(condition: .onQueue(processQueue))
- return jsWorker.inCommonContext { worker in
- worker.evaluateBatch(scripts: [
- Script(name: Prepare.log),
- Script(name: Bundle.profile),
- Script(name: Prepare.profile)
- ])
- return worker.call(function: Function.exportDefaults, with: [])
- }
- }
- // use `internal` protection to expose to unit tests
- func makeProfileJavascript(
- preferences: JSON,
- pumpSettings: JSON,
- bgTargets: JSON,
- basalProfile: JSON,
- isf: JSON,
- carbRatio: JSON,
- tempTargets: JSON,
- model: JSON,
- autotune: JSON,
- trioSettings: JSON
- ) async -> OrefFunctionResult {
- do {
- let result = try await withCheckedThrowingContinuation { continuation in
- jsWorker.inCommonContext { worker in
- worker.evaluateBatch(scripts: [
- Script(name: Prepare.log),
- Script(name: Bundle.profile),
- Script(name: Prepare.profile)
- ])
- let result = worker.call(function: Function.generate, with: [
- pumpSettings,
- bgTargets,
- isf,
- basalProfile,
- preferences,
- carbRatio,
- tempTargets,
- model,
- autotune,
- trioSettings
- ])
- continuation.resume(returning: result)
- }
- }
- return .success(result)
- } catch {
- return .failure(error)
- }
- }
- private func makeProfile(
- preferences: JSON,
- pumpSettings: JSON,
- bgTargets: JSON,
- basalProfile: JSON,
- isf: JSON,
- carbRatio: JSON,
- tempTargets: JSON,
- model: JSON,
- autotune: JSON,
- trioSettings: JSON,
- useSwiftOref: Bool
- ) async throws -> RawJSON {
- let startJavascriptAt = Date()
- let jsResult = await makeProfileJavascript(
- preferences: preferences,
- pumpSettings: pumpSettings,
- bgTargets: bgTargets,
- basalProfile: basalProfile,
- isf: isf,
- carbRatio: carbRatio,
- tempTargets: tempTargets,
- model: model,
- autotune: autotune,
- trioSettings: trioSettings
- )
- let javascriptDuration = Date().timeIntervalSince(startJavascriptAt)
- // Important: we want to make sure that this flag ensures that none
- // of the native code runs
- guard useSwiftOref else {
- return try jsResult.returnOrThrow()
- }
- let startSwiftAt = Date()
- let swiftResult = OpenAPSSwift.makeProfile(
- preferences: preferences,
- pumpSettings: pumpSettings,
- bgTargets: bgTargets,
- basalProfile: basalProfile,
- isf: isf,
- carbRatio: carbRatio,
- tempTargets: tempTargets,
- model: model,
- trioSettings: trioSettings
- )
- let swiftDuration = Date().timeIntervalSince(startSwiftAt)
- JSONCompare.logDifferences(
- function: .makeProfile,
- swift: swiftResult,
- swiftDuration: swiftDuration,
- javascript: jsResult,
- javascriptDuration: javascriptDuration
- )
- return try jsResult.returnOrThrow()
- }
- private func loadJSON(name: String) -> String {
- try! String(contentsOf: Foundation.Bundle.main.url(forResource: "json/\(name)", withExtension: "json")!)
- }
- private func loadFileFromStorage(name: String) -> RawJSON {
- storage.retrieveRaw(name) ?? OpenAPS.defaults(for: name)
- }
- private func loadFileFromStorageAsync(name: String) async -> RawJSON {
- await withCheckedContinuation { continuation in
- DispatchQueue.global(qos: .userInitiated).async {
- let result = self.storage.retrieveRaw(name) ?? OpenAPS.defaults(for: name)
- continuation.resume(returning: result)
- }
- }
- }
- private func middlewareScript(name: String) -> Script? {
- if let body = storage.retrieveRaw(name) {
- return Script(name: name, body: body)
- }
- if let url = Foundation.Bundle.main.url(forResource: "javascript/\(name)", withExtension: "") {
- do {
- let body = try String(contentsOf: url)
- return Script(name: name, body: body)
- } catch {
- debug(.openAPS, "Failed to load script \(name): \(error)")
- }
- }
- return nil
- }
- static func defaults(for file: String) -> RawJSON {
- let prefix = file.hasSuffix(".json") ? "json/defaults" : "javascript"
- guard let url = Foundation.Bundle.main.url(forResource: "\(prefix)/\(file)", withExtension: "") else {
- return ""
- }
- return (try? String(contentsOf: url)) ?? ""
- }
- func processAndSave(forecastData: [String: [Int]]) {
- let currentDate = Date()
- context.perform {
- for (type, values) in forecastData {
- self.createForecast(type: type, values: values, date: currentDate, context: self.context)
- }
- do {
- guard self.context.hasChanges else { return }
- try self.context.save()
- } catch {
- print(error.localizedDescription)
- }
- }
- }
- func createForecast(type: String, values: [Int], date: Date, context: NSManagedObjectContext) {
- let forecast = Forecast(context: context)
- forecast.id = UUID()
- forecast.date = date
- forecast.type = type
- for (index, value) in values.enumerated() {
- let forecastValue = ForecastValue(context: context)
- forecastValue.value = Int32(value)
- forecastValue.index = Int32(index)
- forecastValue.forecast = forecast
- }
- }
- }
- // Non-Async fetch methods for trio_custom_oref_variables
- extension OpenAPS {
- func fetchActiveTempTargets() throws -> [TempTargetStored] {
- try CoreDataStack.shared.fetchEntities(
- ofType: TempTargetStored.self,
- onContext: context,
- predicate: NSPredicate.lastActiveTempTarget,
- key: "date",
- ascending: false,
- fetchLimit: 1
- ) as? [TempTargetStored] ?? []
- }
- func fetchActiveOverrides() throws -> [OverrideStored] {
- try CoreDataStack.shared.fetchEntities(
- ofType: OverrideStored.self,
- onContext: context,
- predicate: NSPredicate.lastActiveOverride,
- key: "date",
- ascending: false,
- fetchLimit: 1
- ) as? [OverrideStored] ?? []
- }
- func fetchHistoricalTDDData(from date: Date) throws -> [[String: Any]] {
- try CoreDataStack.shared.fetchEntities(
- ofType: TDDStored.self,
- onContext: context,
- predicate: NSPredicate(format: "date > %@ AND total > 0", date as NSDate),
- key: "date",
- ascending: true,
- propertiesToFetch: ["date", "total"]
- ) as? [[String: Any]] ?? []
- }
- func fetchGlucose() throws -> [GlucoseStored] {
- let results = try CoreDataStack.shared.fetchEntities(
- ofType: GlucoseStored.self,
- onContext: context,
- predicate: NSPredicate.predicateFor30MinAgo,
- key: "date",
- ascending: false,
- fetchLimit: 4
- )
- return try context.perform {
- guard let glucoseResults = results as? [GlucoseStored] else {
- throw CoreDataError.fetchError(function: #function, file: #file)
- }
- return glucoseResults
- }
- }
- }
|