OpenAPS.swift 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. func test() {
  7. processQueue.async {
  8. let now = Date()
  9. print("START at \(now)")
  10. let pumphistory = self.loadJSON(name: "pumphistory")
  11. let profile = self.loadJSON(name: "profile")
  12. let basalProfile = self.loadJSON(name: "basal_profile")
  13. let clock = self.loadJSON(name: "clock")
  14. let carbs = self.loadJSON(name: "carbhistory")
  15. let glucose = self.loadJSON(name: "glucose")
  16. let currentTemp = self.loadJSON(name: "temp_basal")
  17. let reservoir = 100
  18. let tsMilliseconds: Double = 1_527_924_300_000
  19. let autosensResult = self.autosense(
  20. pumpHistory: pumphistory,
  21. profile: profile,
  22. carbs: carbs,
  23. glucose: glucose,
  24. basalprofile: basalProfile,
  25. temptargets: "null"
  26. )
  27. print("AUTOSENS: \(autosensResult)")
  28. let iobResult = self.iob(
  29. pumphistory: pumphistory,
  30. profile: profile,
  31. clock: clock,
  32. autosens: autosensResult,
  33. pumphistory24: "null"
  34. )
  35. print("IOB: \(iobResult)")
  36. let mealResult = self.meal(
  37. pumphistory: pumphistory,
  38. profile: profile,
  39. basalProfile: basalProfile,
  40. clock: clock,
  41. carbs: carbs,
  42. glucose: glucose
  43. )
  44. print("MEAL: \(mealResult)")
  45. let glucoseStatus = self.glucoseGetLast(glucose: glucose)
  46. print("GLUCOSE STATUS: \(glucoseStatus)")
  47. let suggested = self.determineBasal(
  48. glucoseStatus: glucoseStatus,
  49. currentTemp: currentTemp,
  50. iob: iobResult,
  51. profile: profile,
  52. aurosens: autosensResult,
  53. meal: mealResult,
  54. microBolusAllowed: true,
  55. reservoir: reservoir,
  56. tsMilliseconds: tsMilliseconds
  57. )
  58. print("SUGGESTED: \(suggested)")
  59. let finishDate = Date()
  60. print("FINISH at \(finishDate), duration \(finishDate.timeIntervalSince(now)) s")
  61. }
  62. }
  63. private func iob(pumphistory: JSON, profile: JSON, clock: JSON, autosens: JSON, pumphistory24: JSON) -> JSON {
  64. dispatchPrecondition(condition: .onQueue(processQueue))
  65. return jsWorker.inCommonContext { worker in
  66. worker.evaluate(script: Script(name: "iob-bundle"))
  67. worker.evaluate(script: Script(name: "prepare-iob"))
  68. return worker.call(function: "generate", with: [
  69. pumphistory,
  70. profile,
  71. clock,
  72. autosens,
  73. pumphistory24
  74. ])
  75. }
  76. }
  77. private func meal(pumphistory: JSON, profile: JSON, basalProfile: JSON, clock: JSON, carbs: JSON, glucose: JSON) -> JSON {
  78. dispatchPrecondition(condition: .onQueue(processQueue))
  79. return jsWorker.inCommonContext { worker in
  80. worker.evaluate(script: Script(name: "meal-bundle"))
  81. worker.evaluate(script: Script(name: "prepare-meal"))
  82. return worker.call(function: "generate", with: [
  83. pumphistory,
  84. profile,
  85. basalProfile,
  86. clock,
  87. carbs,
  88. glucose
  89. ])
  90. }
  91. }
  92. private func glucoseGetLast(glucose: JSON) -> JSON {
  93. dispatchPrecondition(condition: .onQueue(processQueue))
  94. return jsWorker.inCommonContext { worker in
  95. worker.evaluate(script: Script(name: "glucose-get-last-bundle"))
  96. return worker.call(function: "freeaps", with: [glucose])
  97. }
  98. }
  99. private func determineBasal(
  100. glucoseStatus: JSON,
  101. currentTemp: JSON,
  102. iob: JSON,
  103. profile: JSON,
  104. aurosens: JSON,
  105. meal: JSON,
  106. microBolusAllowed: Bool,
  107. reservoir: Int,
  108. tsMilliseconds: Double
  109. ) -> JSON {
  110. dispatchPrecondition(condition: .onQueue(processQueue))
  111. return jsWorker.inCommonContext { worker in
  112. worker.evaluate(script: Script(name: "basal-set-temp-bundle"))
  113. worker.evaluate(script: Script(name: "prepare-determine-basal"))
  114. let funcKey = "tempBasalFunctions"
  115. worker.evaluate(script: Script(name: "determine-basal-bundle"))
  116. return worker.call(
  117. function: "freeaps",
  118. with: [
  119. glucoseStatus,
  120. currentTemp,
  121. iob,
  122. profile,
  123. aurosens,
  124. meal,
  125. funcKey,
  126. microBolusAllowed,
  127. reservoir,
  128. tsMilliseconds
  129. ]
  130. )
  131. }
  132. }
  133. private func autosense(
  134. pumpHistory: JSON,
  135. profile: JSON,
  136. carbs: JSON,
  137. glucose: JSON,
  138. basalprofile: JSON,
  139. temptargets: JSON
  140. ) -> JSON {
  141. dispatchPrecondition(condition: .onQueue(processQueue))
  142. return jsWorker.inCommonContext { worker in
  143. worker.evaluate(script: Script(name: "autosens-bundle"))
  144. worker.evaluate(script: Script(name: "prepare-autosens"))
  145. return worker.call(
  146. function: "generate",
  147. with: [
  148. pumpHistory,
  149. profile,
  150. carbs,
  151. glucose,
  152. basalprofile,
  153. temptargets
  154. ]
  155. )
  156. }
  157. }
  158. private func loadJSON(name: String) -> String {
  159. try! String(contentsOf: Bundle.main.url(forResource: "json/\(name)", withExtension: "json")!)
  160. }
  161. }