OpenAPS.swift 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 vmQueue = DispatchQueue(label: "DispatchQueue.JSVirtualMachine")
  11. func determineBasal() {
  12. let jsWorker = JavaScriptWorker()
  13. let scripts = [
  14. Script(name: "prepare"),
  15. Script(name: "basal-set-temp"),
  16. Script(name: "determine-basal"),
  17. Script(name: "glucose-get-last")
  18. ]
  19. scripts.forEach { jsWorker.evaluate(script: $0) }
  20. let glucose = loadJSON(name: "glucose")
  21. let currentTemp = loadJSON(name: "temp_basal")
  22. let iobData = loadJSON(name: "iob")
  23. let profile = loadJSON(name: "profile")
  24. let autosensData = Autosens(ratio: 1.0)
  25. let mealData = loadJSON(name: "meal")
  26. let tempBasalFunctions: OpenAPSName = .tempBasalFunctions
  27. let microBolusAllowed = true
  28. let reservoir = 100
  29. let tsMilliseconds = 1527924300000
  30. let glucoseStatus = jsWorker.call(function: .getLastGlucose, with: [glucose])
  31. let result = jsWorker.call(
  32. function: .determineBasal,
  33. with: [
  34. glucoseStatus,
  35. currentTemp,
  36. iobData,
  37. profile,
  38. autosensData,
  39. mealData,
  40. tempBasalFunctions,
  41. microBolusAllowed,
  42. reservoir,
  43. tsMilliseconds
  44. ]
  45. )
  46. print(result)
  47. print(jsWorker["logError"]!.toString()!)
  48. }
  49. private func loadJSON(name: String) -> String {
  50. try! String(contentsOf: Bundle.main.url(forResource: "json/\(name)", withExtension: "json")!)
  51. }
  52. }
  53. enum OpenAPSName: String {
  54. case tempBasalFunctions
  55. case getLastGlucose
  56. case determineBasal = "determine_basal"
  57. }
  58. extension JavaScriptWorker {
  59. func call(function: OpenAPSName, with arguments: [JSON]) -> JSON {
  60. call(function: function.string, with: arguments)
  61. }
  62. }