OpenAPSSwift.swift 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. import Foundation
  2. struct OpenAPSSwift {
  3. static func makeProfile(
  4. preferences: JSON,
  5. pumpSettings: JSON,
  6. bgTargets: JSON,
  7. basalProfile: JSON,
  8. isf: JSON,
  9. carbRatio: JSON,
  10. tempTargets: JSON,
  11. model: JSON,
  12. trioSettings: JSON
  13. ) -> OrefFunctionResult {
  14. do {
  15. let preferences = try JSONBridge.preferences(from: preferences)
  16. let pumpSettings = try JSONBridge.pumpSettings(from: pumpSettings)
  17. let bgTargets = try JSONBridge.bgTargets(from: bgTargets)
  18. let basalProfile = try JSONBridge.basalProfile(from: basalProfile)
  19. let isf = try JSONBridge.insulinSensitivities(from: isf)
  20. let carbRatio = try JSONBridge.carbRatios(from: carbRatio)
  21. let tempTargets = try JSONBridge.tempTargets(from: tempTargets)
  22. let model = JSONBridge.model(from: model)
  23. let trioSettings = try JSONBridge.trioSettings(from: trioSettings)
  24. let profile = try ProfileGenerator.generate(
  25. pumpSettings: pumpSettings,
  26. bgTargets: bgTargets,
  27. basalProfile: basalProfile,
  28. isf: isf,
  29. preferences: preferences,
  30. carbRatios: carbRatio,
  31. tempTargets: tempTargets,
  32. model: model,
  33. trioSettings: trioSettings
  34. )
  35. return try .success(JSONBridge.to(profile))
  36. } catch {
  37. return .failure(error)
  38. }
  39. }
  40. static func determineBasal(
  41. glucose: JSON,
  42. currentTemp _: JSON,
  43. iob: JSON,
  44. profile: JSON,
  45. autosens: JSON,
  46. meal: JSON,
  47. microBolusAllowed: Bool,
  48. reservoir _: JSON,
  49. pumpHistory: JSON,
  50. preferences: JSON,
  51. basalProfile: JSON,
  52. trioCustomOrefVariables _: JSON,
  53. clock: Date
  54. ) -> (OrefFunctionResult, DetermineBasalInputs?) {
  55. var determineBasalInputs: DetermineBasalInputs?
  56. do {
  57. // FIXME: figure out the types for the commented out vars
  58. let glucose = try JSONBridge.glucose(from: glucose)
  59. // currentTemp: JSON,
  60. let iob = try JSONBridge.iobResult(from: iob)
  61. let profile = try JSONBridge.profile(from: profile)
  62. let autosens = try JSONBridge.autosens(from: autosens)
  63. let meal = try JSONBridge.computedCarbs(from: meal)
  64. let microBolusAllowed = microBolusAllowed
  65. // reservoir: JSON
  66. let pumpHistory = try JSONBridge.pumpHistory(from: pumpHistory)
  67. let preferences = try JSONBridge.preferences(from: preferences)
  68. let basalProfile = try JSONBridge.basalProfile(from: basalProfile)
  69. // trioCustomOrefVariables: JSON
  70. determineBasalInputs = DetermineBasalInputs(
  71. glucose: glucose,
  72. iob: iob,
  73. profile: profile,
  74. autosens: autosens,
  75. meal: meal,
  76. microBolusAllowed: microBolusAllowed,
  77. pumpHistory: pumpHistory,
  78. preferences: preferences,
  79. basalProfile: basalProfile,
  80. clock: clock
  81. )
  82. /*
  83. let result = DeterminationGenerator.generate(profile: profile, currentTemp: <#T##TempBasal#>, iobData: iob, mealData: meal, autosensData: autosens, reservoirData: <#T##Reservoir#>, glucoseStatus: <#T##GlucoseStatus?#>, currentTime: clock)
  84. */
  85. // FIXME: fill in with result once we have it
  86. // return (.success(JSONBridge.to(result)), determineBasalInputs)
  87. return (.success(RawJSON.null), determineBasalInputs)
  88. } catch {
  89. return (.failure(error), determineBasalInputs)
  90. }
  91. }
  92. static func meal(
  93. pumphistory: JSON,
  94. profile: JSON,
  95. basalProfile: JSON,
  96. clock: JSON,
  97. carbs: JSON,
  98. glucose: JSON
  99. ) -> (OrefFunctionResult, MealInputs?) {
  100. var mealInputs: MealInputs?
  101. do {
  102. let pumpHistory = try JSONBridge.pumpHistory(from: pumphistory)
  103. let profile = try JSONBridge.profile(from: profile)
  104. let basalProfile = try JSONBridge.basalProfile(from: basalProfile)
  105. let clock = try JSONBridge.clock(from: clock)
  106. let carbs = try JSONBridge.carbs(from: carbs)
  107. let glucose = try JSONBridge.glucose(from: glucose)
  108. mealInputs = MealInputs(
  109. pumpHistory: pumpHistory,
  110. profile: profile,
  111. basalProfile: basalProfile,
  112. clock: clock,
  113. carbs: carbs,
  114. glucose: glucose
  115. )
  116. let mealResult = try MealGenerator.generate(
  117. pumpHistory: pumpHistory,
  118. profile: profile,
  119. basalProfile: basalProfile,
  120. clock: clock,
  121. carbHistory: carbs,
  122. glucoseHistory: glucose
  123. )
  124. return try (.success(JSONBridge.to(mealResult)), mealInputs)
  125. } catch {
  126. return (.failure(error), mealInputs)
  127. }
  128. }
  129. static func iob(pumphistory: JSON, profile: JSON, clock: JSON, autosens: JSON) -> (OrefFunctionResult, IobInputs?) {
  130. var iobInputs: IobInputs?
  131. do {
  132. let pumpHistory = try JSONBridge.pumpHistory(from: pumphistory)
  133. let profile = try JSONBridge.profile(from: profile)
  134. let clock = try JSONBridge.clock(from: clock)
  135. let autosens = try JSONBridge.autosens(from: autosens)
  136. iobInputs = IobInputs(history: pumpHistory, profile: profile, clock: clock, autosens: autosens)
  137. let iobResult = try IobGenerator.generate(
  138. history: pumpHistory,
  139. profile: profile,
  140. clock: clock,
  141. autosens: autosens
  142. )
  143. return try (.success(JSONBridge.to(iobResult)), iobInputs)
  144. } catch {
  145. return (.failure(error), iobInputs)
  146. }
  147. }
  148. static func autosense(
  149. glucose _: JSON,
  150. pumpHistory _: JSON,
  151. basalprofile _: JSON,
  152. profile _: JSON,
  153. carbs _: JSON,
  154. temptargets _: JSON
  155. ) -> OrefFunctionResult {
  156. .failure(NSError(domain: "Some error", code: 1, userInfo: nil))
  157. }
  158. }