OpenAPS.swift 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. import Foundation
  2. import JavaScriptCore
  3. final class OpenAPS {
  4. private let jsWorker = JavaScriptWorker()
  5. private let processQueue = DispatchQueue(label: "OpenAPS.processQueue", qos: .utility)
  6. private let storage: FileStorage
  7. init(storage: FileStorage) {
  8. self.storage = storage
  9. }
  10. func test() {
  11. processQueue.async {
  12. let now = Date()
  13. print("START at \(now)")
  14. let pumphistory = self.loadJSON(name: "pumphistory")
  15. let profile = self.loadJSON(name: "profile")
  16. let basalProfile = self.loadJSON(name: "basal_profile")
  17. let clock = self.loadJSON(name: "clock")
  18. let carbs = self.loadJSON(name: "carbhistory")
  19. let glucose = self.loadJSON(name: "glucose")
  20. let currentTemp = self.loadJSON(name: "temp_basal")
  21. let reservoir = 100
  22. let tsMilliseconds: Double = 1_527_924_300_000
  23. let preferences = self.exportDefaultPreferences()
  24. print("DEFAULT PREFERENCES: \(preferences)")
  25. let autosensResult = self.autosense(
  26. pumpHistory: pumphistory,
  27. profile: profile,
  28. carbs: carbs,
  29. glucose: glucose,
  30. basalprofile: basalProfile,
  31. temptargets: "null"
  32. )
  33. print("AUTOSENS: \(autosensResult)")
  34. try? self.storage.save(autosensResult, as: Settings.autosense)
  35. let iobResult = self.iob(
  36. pumphistory: pumphistory,
  37. profile: profile,
  38. clock: clock,
  39. autosens: autosensResult,
  40. pumphistory24: "null"
  41. )
  42. print("IOB: \(iobResult)")
  43. let mealResult = self.meal(
  44. pumphistory: pumphistory,
  45. profile: profile,
  46. basalProfile: basalProfile,
  47. clock: clock,
  48. carbs: carbs,
  49. glucose: glucose
  50. )
  51. print("MEAL: \(mealResult)")
  52. let glucoseStatus = self.glucoseGetLast(glucose: glucose)
  53. print("GLUCOSE STATUS: \(glucoseStatus)")
  54. let suggested = self.determineBasal(
  55. glucoseStatus: glucoseStatus,
  56. currentTemp: currentTemp,
  57. iob: iobResult,
  58. profile: profile,
  59. aurosens: autosensResult,
  60. meal: mealResult,
  61. microBolusAllowed: true,
  62. reservoir: reservoir,
  63. tsMilliseconds: tsMilliseconds
  64. )
  65. print("SUGGESTED: \(suggested)")
  66. let autotunePreppedGlucose = self.autotunePrepare(
  67. pumphistory: pumphistory,
  68. profile: profile,
  69. glucose: glucose,
  70. pumpprofile: profile,
  71. categorizeUamAsBasal: true,
  72. tuneInsulinCurve: false
  73. )
  74. print("AUTOTUNE PREP: \(autotunePreppedGlucose)")
  75. let previousAutotune = try? self.storage.retrieve(Settings.autotune, as: RawJSON.self)
  76. let autotuneResult = self.autotuneRun(
  77. autotunePreparedData: autotunePreppedGlucose,
  78. previousAutotuneResult: previousAutotune ?? profile,
  79. pumpProfile: profile
  80. )
  81. try? self.storage.save(autotuneResult, as: Settings.autotune)
  82. print("AUTOTUNE RESULT: \(autotuneResult)")
  83. let finishDate = Date()
  84. print("FINISH at \(finishDate), duration \(finishDate.timeIntervalSince(now)) s")
  85. }
  86. }
  87. private func iob(pumphistory: JSON, profile: JSON, clock: JSON, autosens: JSON, pumphistory24: JSON) -> RawJSON {
  88. dispatchPrecondition(condition: .onQueue(processQueue))
  89. return jsWorker.inCommonContext { worker in
  90. worker.evaluate(script: Script(name: Bundle.iob))
  91. worker.evaluate(script: Script(name: Prepare.iob))
  92. return worker.call(function: Function.generate, with: [
  93. pumphistory,
  94. profile,
  95. clock,
  96. autosens,
  97. pumphistory24
  98. ])
  99. }
  100. }
  101. private func meal(pumphistory: JSON, profile: JSON, basalProfile: JSON, clock: JSON, carbs: JSON, glucose: JSON) -> RawJSON {
  102. dispatchPrecondition(condition: .onQueue(processQueue))
  103. return jsWorker.inCommonContext { worker in
  104. worker.evaluate(script: Script(name: Bundle.meal))
  105. worker.evaluate(script: Script(name: Prepare.meal))
  106. return worker.call(function: Function.generate, with: [
  107. pumphistory,
  108. profile,
  109. basalProfile,
  110. clock,
  111. carbs,
  112. glucose
  113. ])
  114. }
  115. }
  116. private func autotunePrepare(
  117. pumphistory: JSON,
  118. profile: JSON,
  119. glucose: JSON,
  120. pumpprofile: JSON,
  121. categorizeUamAsBasal: Bool,
  122. tuneInsulinCurve: Bool
  123. ) -> RawJSON {
  124. dispatchPrecondition(condition: .onQueue(processQueue))
  125. return jsWorker.inCommonContext { worker in
  126. worker.evaluate(script: Script(name: Bundle.autotunePrep))
  127. worker.evaluate(script: Script(name: Prepare.autotunePrep))
  128. return worker.call(function: Function.generate, with: [
  129. pumphistory,
  130. profile,
  131. glucose,
  132. pumpprofile,
  133. categorizeUamAsBasal,
  134. tuneInsulinCurve
  135. ])
  136. }
  137. }
  138. private func autotuneRun(
  139. autotunePreparedData: JSON,
  140. previousAutotuneResult: JSON,
  141. pumpProfile: JSON
  142. ) -> RawJSON {
  143. dispatchPrecondition(condition: .onQueue(processQueue))
  144. return jsWorker.inCommonContext { worker in
  145. worker.evaluate(script: Script(name: Bundle.autotuneCore))
  146. worker.evaluate(script: Script(name: Prepare.autotuneCore))
  147. return worker.call(function: Function.generate, with: [
  148. autotunePreparedData,
  149. previousAutotuneResult,
  150. pumpProfile
  151. ])
  152. }
  153. }
  154. private func glucoseGetLast(glucose: JSON) -> RawJSON {
  155. dispatchPrecondition(condition: .onQueue(processQueue))
  156. return jsWorker.inCommonContext { worker in
  157. worker.evaluate(script: Script(name: Bundle.getLastGlucose))
  158. return worker.call(function: Function.freeaps, with: [glucose])
  159. }
  160. }
  161. private func determineBasal(
  162. glucoseStatus: JSON,
  163. currentTemp: JSON,
  164. iob: JSON,
  165. profile: JSON,
  166. aurosens: JSON,
  167. meal: JSON,
  168. microBolusAllowed: Bool,
  169. reservoir: Int,
  170. tsMilliseconds: Double
  171. ) -> RawJSON {
  172. dispatchPrecondition(condition: .onQueue(processQueue))
  173. return jsWorker.inCommonContext { worker in
  174. worker.evaluate(script: Script(name: Bundle.basalSetTemp))
  175. worker.evaluate(script: Script(name: Prepare.determineBasal))
  176. worker.evaluate(script: Script(name: Bundle.determineBasal))
  177. return worker.call(
  178. function: Function.freeaps,
  179. with: [
  180. glucoseStatus,
  181. currentTemp,
  182. iob,
  183. profile,
  184. aurosens,
  185. meal,
  186. Function.tempBasalFunctions,
  187. microBolusAllowed,
  188. reservoir,
  189. tsMilliseconds
  190. ]
  191. )
  192. }
  193. }
  194. private func autosense(
  195. pumpHistory: JSON,
  196. profile: JSON,
  197. carbs: JSON,
  198. glucose: JSON,
  199. basalprofile: JSON,
  200. temptargets: JSON
  201. ) -> RawJSON {
  202. dispatchPrecondition(condition: .onQueue(processQueue))
  203. return jsWorker.inCommonContext { worker in
  204. worker.evaluate(script: Script(name: Bundle.autosens))
  205. worker.evaluate(script: Script(name: Prepare.autosens))
  206. return worker.call(
  207. function: Function.generate,
  208. with: [
  209. pumpHistory,
  210. profile,
  211. carbs,
  212. glucose,
  213. basalprofile,
  214. temptargets
  215. ]
  216. )
  217. }
  218. }
  219. private func exportDefaultPreferences() -> RawJSON {
  220. dispatchPrecondition(condition: .onQueue(processQueue))
  221. return jsWorker.inCommonContext { worker in
  222. worker.evaluate(script: Script(name: Bundle.profile))
  223. worker.evaluate(script: Script(name: Prepare.profile))
  224. return worker.call(function: Function.exportDefaults, with: [])
  225. }
  226. }
  227. private func makeProfile(
  228. preferences: JSON,
  229. pumpSettings: JSON,
  230. bgTargets: JSON,
  231. basalProfile: JSON,
  232. isf: JSON,
  233. carbRatio: JSON,
  234. tempTargets: JSON,
  235. model: JSON,
  236. autotune: JSON
  237. ) -> RawJSON {
  238. dispatchPrecondition(condition: .onQueue(processQueue))
  239. return jsWorker.inCommonContext { worker in
  240. worker.evaluate(script: Script(name: Bundle.profile))
  241. worker.evaluate(script: Script(name: Prepare.profile))
  242. return worker.call(
  243. function: Function.generate,
  244. with: [
  245. preferences,
  246. pumpSettings,
  247. bgTargets,
  248. basalProfile,
  249. isf,
  250. carbRatio,
  251. tempTargets,
  252. model,
  253. autotune
  254. ]
  255. )
  256. }
  257. }
  258. private func loadJSON(name: String) -> String {
  259. try! String(contentsOf: Foundation.Bundle.main.url(forResource: "json/\(name)", withExtension: "json")!)
  260. }
  261. }