OpenAPSSwift.swift 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. clock: Date
  14. ) -> (OrefFunctionResult, MakeProfileInputs?) {
  15. var makeProfileInputs: MakeProfileInputs?
  16. do {
  17. let preferences = try JSONBridge.preferences(from: preferences)
  18. let pumpSettings = try JSONBridge.pumpSettings(from: pumpSettings)
  19. let bgTargets = try JSONBridge.bgTargets(from: bgTargets)
  20. let basalProfile = try JSONBridge.basalProfile(from: basalProfile)
  21. let isf = try JSONBridge.insulinSensitivities(from: isf)
  22. let carbRatio = try JSONBridge.carbRatios(from: carbRatio)
  23. let tempTargets = try JSONBridge.tempTargets(from: tempTargets)
  24. let model = JSONBridge.model(from: model)
  25. let trioSettings = try JSONBridge.trioSettings(from: trioSettings)
  26. makeProfileInputs = MakeProfileInputs(
  27. preferences: preferences,
  28. pumpSettings: pumpSettings,
  29. bgTargets: bgTargets,
  30. basalProfile: basalProfile,
  31. isf: isf,
  32. carbRatios: carbRatio,
  33. tempTargets: tempTargets,
  34. model: model,
  35. trioSettings: trioSettings,
  36. clock: clock
  37. )
  38. let profile = try ProfileGenerator.generate(
  39. pumpSettings: pumpSettings,
  40. bgTargets: bgTargets,
  41. basalProfile: basalProfile,
  42. isf: isf,
  43. preferences: preferences,
  44. carbRatios: carbRatio,
  45. tempTargets: tempTargets,
  46. model: model,
  47. clock: clock
  48. )
  49. return (try .success(JSONBridge.to(profile)), makeProfileInputs)
  50. } catch {
  51. return (.failure(error), makeProfileInputs)
  52. }
  53. }
  54. static func determineBasal(
  55. glucose: JSON,
  56. currentTemp: JSON,
  57. iob: JSON,
  58. profile: JSON,
  59. autosens: JSON,
  60. meal: JSON,
  61. microBolusAllowed: Bool,
  62. reservoir: JSON,
  63. pumpHistory: JSON,
  64. preferences: JSON,
  65. basalProfile: JSON,
  66. trioCustomOrefVariables: JSON,
  67. clock: Date
  68. ) -> (OrefFunctionResult, DetermineBasalInputs?) {
  69. var determineBasalInputs: DetermineBasalInputs?
  70. print(reservoir)
  71. do {
  72. let glucose = try JSONBridge.glucose(from: glucose)
  73. let currentTemp = try JSONBridge.currentTemp(from: currentTemp)
  74. let iob = try JSONBridge.iobResult(from: iob)
  75. let profile = try JSONBridge.profile(from: profile)
  76. let autosens = try JSONBridge.autosens(from: autosens)
  77. let meal = try JSONBridge.computedCarbs(from: meal)
  78. let microBolusAllowed = microBolusAllowed
  79. let reservoir = Decimal(string: reservoir.rawJSON)
  80. let pumpHistory = try JSONBridge.pumpHistory(from: pumpHistory)
  81. let preferences = try JSONBridge.preferences(from: preferences)
  82. let basalProfile = try JSONBridge.basalProfile(from: basalProfile)
  83. let trioCustomOrefVariables = try JSONBridge.trioCustomOrefVariables(from: trioCustomOrefVariables)
  84. determineBasalInputs = DetermineBasalInputs(
  85. glucose: glucose,
  86. currentTemp: currentTemp,
  87. iob: iob,
  88. profile: profile,
  89. autosens: autosens,
  90. meal: meal,
  91. microBolusAllowed: microBolusAllowed,
  92. reservoir: reservoir,
  93. pumpHistory: pumpHistory,
  94. preferences: preferences,
  95. basalProfile: basalProfile,
  96. trioCustomOrefVariables: trioCustomOrefVariables,
  97. clock: clock
  98. )
  99. guard let mealData = meal, let autosensData = autosens else {
  100. return (.failure(DeterminationError.missingInputs), determineBasalInputs)
  101. }
  102. let rawDetermination = try DeterminationGenerator.generate(
  103. profile: profile,
  104. preferences: preferences,
  105. currentTemp: currentTemp,
  106. iobData: iob,
  107. mealData: mealData,
  108. autosensData: autosensData,
  109. reservoirData: reservoir ?? 100,
  110. glucose: glucose,
  111. microBolusAllowed: microBolusAllowed,
  112. trioCustomOrefVariables: trioCustomOrefVariables,
  113. currentTime: clock
  114. )
  115. return (try .success(JSONBridge.to(rawDetermination)), determineBasalInputs)
  116. } catch let determinationError as DeterminationError {
  117. // if we get a determination error we want to return it as a JSON
  118. // object that is { "error": "some error" }
  119. do {
  120. let response = try JSONBridge.to(DeterminationErrorResponse(error: determinationError.localizedDescription))
  121. return (.success(response), determineBasalInputs)
  122. } catch {
  123. return (.failure(determinationError), determineBasalInputs)
  124. }
  125. } catch {
  126. return (.failure(error), determineBasalInputs)
  127. }
  128. }
  129. static func meal(
  130. pumphistory: JSON,
  131. profile: JSON,
  132. basalProfile: JSON,
  133. clock: JSON,
  134. carbs: JSON,
  135. glucose: JSON
  136. ) -> (OrefFunctionResult, MealInputs?) {
  137. var mealInputs: MealInputs?
  138. do {
  139. let pumpHistory = try JSONBridge.pumpHistory(from: pumphistory)
  140. let profile = try JSONBridge.profile(from: profile)
  141. let basalProfile = try JSONBridge.basalProfile(from: basalProfile)
  142. let clock = try JSONBridge.clock(from: clock)
  143. let carbs = try JSONBridge.carbs(from: carbs)
  144. let glucose = try JSONBridge.glucose(from: glucose)
  145. mealInputs = MealInputs(
  146. pumpHistory: pumpHistory,
  147. profile: profile,
  148. basalProfile: basalProfile,
  149. clock: clock,
  150. carbs: carbs,
  151. glucose: glucose
  152. )
  153. let mealResult = try MealGenerator.generate(
  154. pumpHistory: pumpHistory,
  155. profile: profile,
  156. basalProfile: basalProfile,
  157. clock: clock,
  158. carbHistory: carbs,
  159. glucoseHistory: glucose
  160. )
  161. return try (.success(JSONBridge.to(mealResult)), mealInputs)
  162. } catch {
  163. return (.failure(error), mealInputs)
  164. }
  165. }
  166. static func iob(pumphistory: JSON, profile: JSON, clock: JSON, autosens: JSON) -> (OrefFunctionResult, IobInputs?) {
  167. var iobInputs: IobInputs?
  168. do {
  169. let pumpHistory = try JSONBridge.pumpHistory(from: pumphistory)
  170. let profile = try JSONBridge.profile(from: profile)
  171. let clock = try JSONBridge.clock(from: clock)
  172. let autosens = try JSONBridge.autosens(from: autosens)
  173. iobInputs = IobInputs(history: pumpHistory, profile: profile, clock: clock, autosens: autosens)
  174. let iobResult = try IobGenerator.generate(
  175. history: pumpHistory,
  176. profile: profile,
  177. clock: clock,
  178. autosens: autosens
  179. )
  180. return try (.success(JSONBridge.to(iobResult)), iobInputs)
  181. } catch {
  182. return (.failure(error), iobInputs)
  183. }
  184. }
  185. static func autosense(
  186. glucose: JSON,
  187. pumpHistory: JSON,
  188. basalProfile: JSON,
  189. profile: JSON,
  190. carbs: JSON,
  191. tempTargets: JSON,
  192. clock: JSON,
  193. includeDeviationsForTesting: Bool = false
  194. ) -> (OrefFunctionResult, AutosensInputs?) {
  195. var autosensInputs: AutosensInputs?
  196. do {
  197. let glucose = try JSONBridge.glucose(from: glucose)
  198. let pumpHistory = try JSONBridge.pumpHistory(from: pumpHistory)
  199. let basalProfile = try JSONBridge.basalProfile(from: basalProfile)
  200. let profile = try JSONBridge.profile(from: profile)
  201. let carbs = try JSONBridge.carbs(from: carbs)
  202. let tempTargets = try JSONBridge.tempTargets(from: tempTargets)
  203. let clock = try JSONBridge.clock(from: clock)
  204. autosensInputs = AutosensInputs(
  205. glucose: glucose,
  206. history: pumpHistory,
  207. basalProfile: basalProfile,
  208. profile: profile,
  209. carbs: carbs,
  210. tempTargets: tempTargets,
  211. clock: clock
  212. )
  213. // this logic is from prepare/autosens.js
  214. let ratio8h = try AutosensGenerator.generate(
  215. glucose: glucose,
  216. pumpHistory: pumpHistory,
  217. basalProfile: basalProfile,
  218. profile: profile,
  219. carbs: carbs,
  220. tempTargets: tempTargets,
  221. maxDeviations: 96,
  222. clock: clock,
  223. includeDeviationsForTesting: includeDeviationsForTesting
  224. )
  225. let ratio24h = try AutosensGenerator.generate(
  226. glucose: glucose,
  227. pumpHistory: pumpHistory,
  228. basalProfile: basalProfile,
  229. profile: profile,
  230. carbs: carbs,
  231. tempTargets: tempTargets,
  232. maxDeviations: 288,
  233. clock: clock,
  234. includeDeviationsForTesting: includeDeviationsForTesting
  235. )
  236. let lowestRatio = ratio8h.ratio < ratio24h.ratio ? ratio8h : ratio24h
  237. return try (.success(JSONBridge.to(lowestRatio)), autosensInputs)
  238. } catch {
  239. return (.failure(error), autosensInputs)
  240. }
  241. }
  242. }