OpenAPS.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. let iobResult = self.iob(
  35. pumphistory: pumphistory,
  36. profile: profile,
  37. clock: clock,
  38. autosens: autosensResult,
  39. pumphistory24: "null"
  40. )
  41. print("IOB: \(iobResult)")
  42. let mealResult = self.meal(
  43. pumphistory: pumphistory,
  44. profile: profile,
  45. basalProfile: basalProfile,
  46. clock: clock,
  47. carbs: carbs,
  48. glucose: glucose
  49. )
  50. print("MEAL: \(mealResult)")
  51. let glucoseStatus = self.glucoseGetLast(glucose: glucose)
  52. print("GLUCOSE STATUS: \(glucoseStatus)")
  53. let suggested = self.determineBasal(
  54. glucoseStatus: glucoseStatus,
  55. currentTemp: currentTemp,
  56. iob: iobResult,
  57. profile: profile,
  58. aurosens: autosensResult,
  59. meal: mealResult,
  60. microBolusAllowed: true,
  61. reservoir: reservoir,
  62. tsMilliseconds: tsMilliseconds
  63. )
  64. print("SUGGESTED: \(suggested)")
  65. let autotunePreppedGlucose = self.autotunePrepare(
  66. pumphistory: pumphistory,
  67. profile: profile,
  68. glucose: glucose,
  69. pumpprofile: profile,
  70. categorizeUamAsBasal: true,
  71. tuneInsulinCurve: false
  72. )
  73. print("AUTOTUNE PREP: \(autotunePreppedGlucose)")
  74. let previousAutotune = try? self.storage.retrieve(Settings.autotune, as: RawJSON.self)
  75. let autotuneResult = self.autotuneRun(
  76. autotunePreparedData: autotunePreppedGlucose,
  77. previousAutotuneResult: previousAutotune ?? profile,
  78. pumpProfile: profile
  79. )
  80. try? self.storage.save(autotuneResult, as: Settings.autotune)
  81. print("AUTOTUNE RESULT: \(autotuneResult)")
  82. let finishDate = Date()
  83. print("FINISH at \(finishDate), duration \(finishDate.timeIntervalSince(now)) s")
  84. }
  85. }
  86. private func iob(pumphistory: JSON, profile: JSON, clock: JSON, autosens: JSON, pumphistory24: JSON) -> RawJSON {
  87. dispatchPrecondition(condition: .onQueue(processQueue))
  88. return jsWorker.inCommonContext { worker in
  89. worker.evaluate(script: Script(name: Bundle.iob))
  90. worker.evaluate(script: Script(name: Prepare.iob))
  91. return worker.call(function: Function.generate, with: [
  92. pumphistory,
  93. profile,
  94. clock,
  95. autosens,
  96. pumphistory24
  97. ])
  98. }
  99. }
  100. private func meal(pumphistory: JSON, profile: JSON, basalProfile: JSON, clock: JSON, carbs: JSON, glucose: JSON) -> RawJSON {
  101. dispatchPrecondition(condition: .onQueue(processQueue))
  102. return jsWorker.inCommonContext { worker in
  103. worker.evaluate(script: Script(name: Bundle.meal))
  104. worker.evaluate(script: Script(name: Prepare.meal))
  105. return worker.call(function: Function.generate, with: [
  106. pumphistory,
  107. profile,
  108. basalProfile,
  109. clock,
  110. carbs,
  111. glucose
  112. ])
  113. }
  114. }
  115. private func autotunePrepare(
  116. pumphistory: JSON,
  117. profile: JSON,
  118. glucose: JSON,
  119. pumpprofile: JSON,
  120. categorizeUamAsBasal: Bool,
  121. tuneInsulinCurve: Bool
  122. ) -> RawJSON {
  123. dispatchPrecondition(condition: .onQueue(processQueue))
  124. return jsWorker.inCommonContext { worker in
  125. worker.evaluate(script: Script(name: Bundle.autotunePrep))
  126. worker.evaluate(script: Script(name: Prepare.autotunePrep))
  127. return worker.call(function: Function.generate, with: [
  128. pumphistory,
  129. profile,
  130. glucose,
  131. pumpprofile,
  132. categorizeUamAsBasal,
  133. tuneInsulinCurve
  134. ])
  135. }
  136. }
  137. private func autotuneRun(
  138. autotunePreparedData: JSON,
  139. previousAutotuneResult: JSON,
  140. pumpProfile: JSON
  141. ) -> RawJSON {
  142. dispatchPrecondition(condition: .onQueue(processQueue))
  143. return jsWorker.inCommonContext { worker in
  144. worker.evaluate(script: Script(name: Bundle.autotuneCore))
  145. worker.evaluate(script: Script(name: Prepare.autotuneCore))
  146. return worker.call(function: Function.generate, with: [
  147. autotunePreparedData,
  148. previousAutotuneResult,
  149. pumpProfile
  150. ])
  151. }
  152. }
  153. private func glucoseGetLast(glucose: JSON) -> RawJSON {
  154. dispatchPrecondition(condition: .onQueue(processQueue))
  155. return jsWorker.inCommonContext { worker in
  156. worker.evaluate(script: Script(name: Bundle.getLastGlucose))
  157. return worker.call(function: Function.freeaps, with: [glucose])
  158. }
  159. }
  160. private func determineBasal(
  161. glucoseStatus: JSON,
  162. currentTemp: JSON,
  163. iob: JSON,
  164. profile: JSON,
  165. aurosens: JSON,
  166. meal: JSON,
  167. microBolusAllowed: Bool,
  168. reservoir: Int,
  169. tsMilliseconds: Double
  170. ) -> RawJSON {
  171. dispatchPrecondition(condition: .onQueue(processQueue))
  172. return jsWorker.inCommonContext { worker in
  173. worker.evaluate(script: Script(name: Bundle.basalSetTemp))
  174. worker.evaluate(script: Script(name: Prepare.determineBasal))
  175. worker.evaluate(script: Script(name: Bundle.determineBasal))
  176. return worker.call(
  177. function: Function.freeaps,
  178. with: [
  179. glucoseStatus,
  180. currentTemp,
  181. iob,
  182. profile,
  183. aurosens,
  184. meal,
  185. Function.tempBasalFunctions,
  186. microBolusAllowed,
  187. reservoir,
  188. tsMilliseconds
  189. ]
  190. )
  191. }
  192. }
  193. private func autosense(
  194. pumpHistory: JSON,
  195. profile: JSON,
  196. carbs: JSON,
  197. glucose: JSON,
  198. basalprofile: JSON,
  199. temptargets: JSON
  200. ) -> RawJSON {
  201. dispatchPrecondition(condition: .onQueue(processQueue))
  202. return jsWorker.inCommonContext { worker in
  203. worker.evaluate(script: Script(name: Bundle.autosens))
  204. worker.evaluate(script: Script(name: Prepare.autosens))
  205. return worker.call(
  206. function: Function.generate,
  207. with: [
  208. pumpHistory,
  209. profile,
  210. carbs,
  211. glucose,
  212. basalprofile,
  213. temptargets
  214. ]
  215. )
  216. }
  217. }
  218. private func exportDefaultPreferences() -> RawJSON {
  219. dispatchPrecondition(condition: .onQueue(processQueue))
  220. return jsWorker.inCommonContext { worker in
  221. worker.evaluate(script: Script(name: Bundle.profile))
  222. worker.evaluate(script: Script(name: Prepare.profile))
  223. return worker.call(function: Function.exportDefaults, with: [])
  224. }
  225. }
  226. private func makeProfile(
  227. preferences: JSON,
  228. pumpSettings: JSON,
  229. bgTargets: JSON,
  230. basalProfile: JSON,
  231. isf: JSON,
  232. carbRatio: JSON,
  233. tempTargets: JSON,
  234. model: JSON,
  235. autotune: JSON
  236. ) -> RawJSON {
  237. dispatchPrecondition(condition: .onQueue(processQueue))
  238. return jsWorker.inCommonContext { worker in
  239. worker.evaluate(script: Script(name: Bundle.profile))
  240. worker.evaluate(script: Script(name: Prepare.profile))
  241. return worker.call(
  242. function: Function.generate,
  243. with: [
  244. preferences,
  245. pumpSettings,
  246. bgTargets,
  247. basalProfile,
  248. isf,
  249. carbRatio,
  250. tempTargets,
  251. model,
  252. autotune
  253. ]
  254. )
  255. }
  256. }
  257. private func loadJSON(name: String) -> String {
  258. try! String(contentsOf: Foundation.Bundle.main.url(forResource: "json/\(name)", withExtension: "json")!)
  259. }
  260. }
  261. extension OpenAPS {
  262. private enum Bundle {
  263. static let iob = "bundle/iob"
  264. static let meal = "bundle/meal"
  265. static let autotunePrep = "bundle/autotune-prep"
  266. static let autotuneCore = "bundle/autotune-core"
  267. static let getLastGlucose = "bundle/glucose-get-last"
  268. static let basalSetTemp = "bundle/basal-set-temp"
  269. static let determineBasal = "bundle/determine-basal"
  270. static let autosens = "bundle/autosens"
  271. static let profile = "bundle/profile"
  272. }
  273. private enum Prepare {
  274. static let iob = "prepare/iob"
  275. static let meal = "prepare/meal"
  276. static let autotunePrep = "prepare/autotune-prep"
  277. static let autotuneCore = "prepare/autotune-core"
  278. static let determineBasal = "prepare/determine-basal"
  279. static let autosens = "prepare/autosens"
  280. static let profile = "prepare/profile"
  281. }
  282. private enum Settings {
  283. static let autotune = "settings/autotune.json"
  284. }
  285. private enum Function {
  286. static let freeaps = "freeaps"
  287. static let generate = "generate"
  288. static let tempBasalFunctions = "tempBasalFunctions"
  289. static let exportDefaults = "exportDefaults"
  290. }
  291. }