OpenAPS.swift 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. //
  2. // OpenAPS.swift
  3. // FreeAPS
  4. //
  5. // Created by Ivan Valkou on 12.01.2021.
  6. //
  7. import Foundation
  8. import JavaScriptCore
  9. final class OpenAPS {
  10. private let jsWorker = JavaScriptWorker()
  11. private let processQueue = DispatchQueue(label: "OpenAPS.processQueue", qos: .utility)
  12. func test() {
  13. processQueue.async {
  14. let now = Date()
  15. print("START at \(now)")
  16. let pumphistory = self.loadJSON(name: "pumphistory")
  17. let profile = self.loadJSON(name: "profile")
  18. let basalProfile = self.loadJSON(name: "basal_profile")
  19. let clock = self.loadJSON(name: "clock")
  20. let carbs = self.loadJSON(name: "carbhistory")
  21. let glucose = self.loadJSON(name: "glucose")
  22. let currentTemp = self.loadJSON(name: "temp_basal")
  23. let reservoir = 100
  24. let tsMilliseconds: Double = 1527924300000
  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 finishDate = Date()
  66. print("FINISH at \(finishDate), duration \(finishDate.timeIntervalSince(now)) s")
  67. }
  68. }
  69. private func iob(pumphistory: JSON, profile: JSON, clock: JSON, autosens: JSON, pumphistory24: JSON) -> JSON {
  70. dispatchPrecondition(condition: .onQueue(processQueue))
  71. return jsWorker.inCommonContext { worker in
  72. worker.evaluate(script: Script(name:"iob-bundle"))
  73. worker.evaluate(script: Script(name:"prepare-iob"))
  74. return worker.call(function: "generate", with: [
  75. pumphistory,
  76. profile,
  77. clock,
  78. autosens,
  79. pumphistory24
  80. ])
  81. }
  82. }
  83. private func meal(pumphistory: JSON, profile: JSON, basalProfile: JSON, clock: JSON, carbs: JSON, glucose: JSON) -> JSON {
  84. dispatchPrecondition(condition: .onQueue(processQueue))
  85. return jsWorker.inCommonContext { worker in
  86. worker.evaluate(script: Script(name:"meal-bundle"))
  87. worker.evaluate(script: Script(name:"prepare-meal"))
  88. return worker.call(function: "generate", with: [
  89. pumphistory,
  90. profile,
  91. basalProfile,
  92. clock,
  93. carbs,
  94. glucose
  95. ])
  96. }
  97. }
  98. private func glucoseGetLast(glucose: JSON) -> JSON {
  99. dispatchPrecondition(condition: .onQueue(processQueue))
  100. return jsWorker.inCommonContext { worker in
  101. worker.evaluate(script: Script(name:"glucose-get-last-bundle"))
  102. return worker.call(function: "freeaps", with: [glucose])
  103. }
  104. }
  105. private func determineBasal(
  106. glucoseStatus: JSON,
  107. currentTemp: JSON,
  108. iob: JSON,
  109. profile: JSON,
  110. aurosens: JSON,
  111. meal: JSON,
  112. microBolusAllowed: Bool,
  113. reservoir: Int,
  114. tsMilliseconds: Double
  115. ) -> JSON {
  116. dispatchPrecondition(condition: .onQueue(processQueue))
  117. return jsWorker.inCommonContext { worker in
  118. worker.evaluate(script: Script(name:"basal-set-temp-bundle"))
  119. worker.evaluate(script: Script(name:"prepare-determine-basal"))
  120. let funcKey = "tempBasalFunctions"
  121. worker.evaluate(script: Script(name:"determine-basal-bundle"))
  122. return worker.call(
  123. function: "freeaps",
  124. with: [
  125. glucoseStatus,
  126. currentTemp,
  127. iob,
  128. profile,
  129. aurosens,
  130. meal,
  131. funcKey,
  132. microBolusAllowed,
  133. reservoir,
  134. tsMilliseconds
  135. ]
  136. )
  137. }
  138. }
  139. private func autosense(
  140. pumpHistory: JSON,
  141. profile: JSON,
  142. carbs: JSON,
  143. glucose: JSON,
  144. basalprofile: JSON,
  145. temptargets: JSON
  146. ) -> JSON {
  147. dispatchPrecondition(condition: .onQueue(processQueue))
  148. return jsWorker.inCommonContext { worker in
  149. worker.evaluate(script: Script(name:"autosens-bundle"))
  150. worker.evaluate(script: Script(name:"prepare-autosens"))
  151. return worker.call(
  152. function: "generate",
  153. with: [
  154. pumpHistory,
  155. profile,
  156. carbs,
  157. glucose,
  158. basalprofile,
  159. temptargets
  160. ]
  161. )
  162. }
  163. }
  164. private func loadJSON(name: String) -> String {
  165. try! String(contentsOf: Bundle.main.url(forResource: "json/\(name)", withExtension: "json")!)
  166. }
  167. }