| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- import Foundation
- struct OpenAPSSwift {
- static func makeProfile(
- preferences: JSON,
- pumpSettings: JSON,
- bgTargets: JSON,
- basalProfile: JSON,
- isf: JSON,
- carbRatio: JSON,
- tempTargets: JSON,
- model: JSON,
- trioSettings: JSON,
- clock: Date
- ) -> (OrefFunctionResult) {
- do {
- let preferences = try JSONBridge.preferences(from: preferences)
- let pumpSettings = try JSONBridge.pumpSettings(from: pumpSettings)
- let bgTargets = try JSONBridge.bgTargets(from: bgTargets)
- let basalProfile = try JSONBridge.basalProfile(from: basalProfile)
- let isf = try JSONBridge.insulinSensitivities(from: isf)
- let carbRatio = try JSONBridge.carbRatios(from: carbRatio)
- let tempTargets = try JSONBridge.tempTargets(from: tempTargets)
- let model = JSONBridge.model(from: model)
- let trioSettings = try JSONBridge.trioSettings(from: trioSettings)
- let profile = try ProfileGenerator.generate(
- pumpSettings: pumpSettings,
- bgTargets: bgTargets,
- basalProfile: basalProfile,
- isf: isf,
- preferences: preferences,
- carbRatios: carbRatio,
- tempTargets: tempTargets,
- model: model,
- clock: clock
- )
- return (try .success(JSONBridge.to(profile)))
- } catch {
- return (.failure(error))
- }
- }
- static 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,
- clock: Date
- ) -> (OrefFunctionResult) {
- do {
- let glucose = try JSONBridge.glucose(from: glucose)
- let currentTemp = try JSONBridge.currentTemp(from: currentTemp)
- let iob = try JSONBridge.iobResult(from: iob)
- let profile = try JSONBridge.profile(from: profile)
- let autosens = try JSONBridge.autosens(from: autosens)
- let meal = try JSONBridge.computedCarbs(from: meal)
- let microBolusAllowed = microBolusAllowed
- let reservoir = Decimal(string: reservoir.rawJSON)
- let pumpHistory = try JSONBridge.pumpHistory(from: pumpHistory)
- let preferences = try JSONBridge.preferences(from: preferences)
- let basalProfile = try JSONBridge.basalProfile(from: basalProfile)
- let trioCustomOrefVariables = try JSONBridge.trioCustomOrefVariables(from: trioCustomOrefVariables)
- guard let mealData = meal, let autosensData = autosens else {
- return .failure(DeterminationError.missingInputs)
- }
- let rawDetermination = try DeterminationGenerator.generate(
- profile: profile,
- preferences: preferences,
- currentTemp: currentTemp,
- iobData: iob,
- mealData: mealData,
- autosensData: autosensData,
- reservoirData: reservoir ?? 100,
- glucose: glucose,
- microBolusAllowed: microBolusAllowed,
- trioCustomOrefVariables: trioCustomOrefVariables,
- currentTime: clock
- )
- return try .success(JSONBridge.to(rawDetermination))
- } catch let determinationError as DeterminationError {
- // if we get a determination error we want to return it as a JSON
- // object that is { "error": "some error" }
- do {
- let response = try JSONBridge.to(DeterminationErrorResponse(error: determinationError.localizedDescription))
- return .success(response)
- } catch {
- return .failure(determinationError)
- }
- } catch {
- return .failure(error)
- }
- }
- static func meal(
- pumphistory: JSON,
- profile: JSON,
- basalProfile: JSON,
- clock: JSON,
- carbs: JSON,
- glucose: JSON
- ) -> (OrefFunctionResult) {
- do {
- let pumpHistory = try JSONBridge.pumpHistory(from: pumphistory)
- let profile = try JSONBridge.profile(from: profile)
- let basalProfile = try JSONBridge.basalProfile(from: basalProfile)
- let clock = try JSONBridge.clock(from: clock)
- let carbs = try JSONBridge.carbs(from: carbs)
- let glucose = try JSONBridge.glucose(from: glucose)
- let mealResult = try MealGenerator.generate(
- pumpHistory: pumpHistory,
- profile: profile,
- basalProfile: basalProfile,
- clock: clock,
- carbHistory: carbs,
- glucoseHistory: glucose
- )
- return try .success(JSONBridge.to(mealResult))
- } catch {
- return .failure(error)
- }
- }
- static func iob(pumphistory: JSON, profile: JSON, clock: JSON, autosens: JSON) -> (OrefFunctionResult) {
- do {
- let pumpHistory = try JSONBridge.pumpHistory(from: pumphistory)
- let profile = try JSONBridge.profile(from: profile)
- let clock = try JSONBridge.clock(from: clock)
- let autosens = try JSONBridge.autosens(from: autosens)
- let iobResult = try IobGenerator.generate(
- history: pumpHistory,
- profile: profile,
- clock: clock,
- autosens: autosens
- )
- return try .success(JSONBridge.to(iobResult))
- } catch {
- return .failure(error)
- }
- }
- static func autosense(
- glucose: JSON,
- pumpHistory: JSON,
- basalProfile: JSON,
- profile: JSON,
- carbs: JSON,
- tempTargets: JSON,
- clock: JSON,
- includeDeviationsForTesting: Bool = false
- ) -> (OrefFunctionResult) {
- do {
- let glucose = try JSONBridge.glucose(from: glucose)
- let pumpHistory = try JSONBridge.pumpHistory(from: pumpHistory)
- let basalProfile = try JSONBridge.basalProfile(from: basalProfile)
- let profile = try JSONBridge.profile(from: profile)
- let carbs = try JSONBridge.carbs(from: carbs)
- let tempTargets = try JSONBridge.tempTargets(from: tempTargets)
- let clock = try JSONBridge.clock(from: clock)
- // this logic is from prepare/autosens.js
- let ratio8h = try AutosensGenerator.generate(
- glucose: glucose,
- pumpHistory: pumpHistory,
- basalProfile: basalProfile,
- profile: profile,
- carbs: carbs,
- tempTargets: tempTargets,
- maxDeviations: 96,
- clock: clock,
- includeDeviationsForTesting: includeDeviationsForTesting
- )
- let ratio24h = try AutosensGenerator.generate(
- glucose: glucose,
- pumpHistory: pumpHistory,
- basalProfile: basalProfile,
- profile: profile,
- carbs: carbs,
- tempTargets: tempTargets,
- maxDeviations: 288,
- clock: clock,
- includeDeviationsForTesting: includeDeviationsForTesting
- )
- let lowestRatio = ratio8h.ratio < ratio24h.ratio ? ratio8h : ratio24h
- return try .success(JSONBridge.to(lowestRatio))
- } catch {
- return .failure(error)
- }
- }
- }
|