| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293 |
- import Foundation
- import JavaScriptCore
- final class OpenAPS {
- private let jsWorker = JavaScriptWorker()
- private let processQueue = DispatchQueue(label: "OpenAPS.processQueue", qos: .utility)
- private let storage: FileStorage
- init(storage: FileStorage) {
- self.storage = storage
- }
- func test() {
- processQueue.async {
- let now = Date()
- print("START at \(now)")
- let pumphistory = self.loadJSON(name: "pumphistory")
- let profile = self.loadJSON(name: "profile")
- let basalProfile = self.loadJSON(name: "basal_profile")
- let clock = self.loadJSON(name: "clock")
- let carbs = self.loadJSON(name: "carbhistory")
- let glucose = self.loadJSON(name: "glucose")
- let currentTemp = self.loadJSON(name: "temp_basal")
- let reservoir = 100
- let tsMilliseconds: Double = 1_527_924_300_000
- let preferences = self.exportDefaultPreferences()
- print("DEFAULT PREFERENCES: \(preferences)")
- let autosensResult = self.autosense(
- pumpHistory: pumphistory,
- profile: profile,
- carbs: carbs,
- glucose: glucose,
- basalprofile: basalProfile,
- temptargets: "null"
- )
- print("AUTOSENS: \(autosensResult)")
- try? self.storage.save(autosensResult, as: Settings.autosense)
- let iobResult = self.iob(
- pumphistory: pumphistory,
- profile: profile,
- clock: clock,
- autosens: autosensResult,
- pumphistory24: "null"
- )
- print("IOB: \(iobResult)")
- let mealResult = self.meal(
- pumphistory: pumphistory,
- profile: profile,
- basalProfile: basalProfile,
- clock: clock,
- carbs: carbs,
- glucose: glucose
- )
- print("MEAL: \(mealResult)")
- let glucoseStatus = self.glucoseGetLast(glucose: glucose)
- print("GLUCOSE STATUS: \(glucoseStatus)")
- let suggested = self.determineBasal(
- glucoseStatus: glucoseStatus,
- currentTemp: currentTemp,
- iob: iobResult,
- profile: profile,
- aurosens: autosensResult,
- meal: mealResult,
- microBolusAllowed: true,
- reservoir: reservoir,
- tsMilliseconds: tsMilliseconds
- )
- print("SUGGESTED: \(suggested)")
- let autotunePreppedGlucose = self.autotunePrepare(
- pumphistory: pumphistory,
- profile: profile,
- glucose: glucose,
- pumpprofile: profile,
- categorizeUamAsBasal: true,
- tuneInsulinCurve: false
- )
- print("AUTOTUNE PREP: \(autotunePreppedGlucose)")
- let previousAutotune = try? self.storage.retrieve(Settings.autotune, as: RawJSON.self)
- let autotuneResult = self.autotuneRun(
- autotunePreparedData: autotunePreppedGlucose,
- previousAutotuneResult: previousAutotune ?? profile,
- pumpProfile: profile
- )
- try? self.storage.save(autotuneResult, as: Settings.autotune)
- print("AUTOTUNE RESULT: \(autotuneResult)")
- let finishDate = Date()
- print("FINISH at \(finishDate), duration \(finishDate.timeIntervalSince(now)) s")
- }
- }
- private func iob(pumphistory: JSON, profile: JSON, clock: JSON, autosens: JSON, pumphistory24: JSON) -> RawJSON {
- dispatchPrecondition(condition: .onQueue(processQueue))
- return jsWorker.inCommonContext { worker in
- worker.evaluate(script: Script(name: Bundle.iob))
- worker.evaluate(script: Script(name: Prepare.iob))
- return worker.call(function: Function.generate, with: [
- pumphistory,
- profile,
- clock,
- autosens,
- pumphistory24
- ])
- }
- }
- private func meal(pumphistory: JSON, profile: JSON, basalProfile: JSON, clock: JSON, carbs: JSON, glucose: JSON) -> RawJSON {
- dispatchPrecondition(condition: .onQueue(processQueue))
- return jsWorker.inCommonContext { worker in
- worker.evaluate(script: Script(name: Bundle.meal))
- worker.evaluate(script: Script(name: Prepare.meal))
- return worker.call(function: Function.generate, with: [
- pumphistory,
- profile,
- basalProfile,
- clock,
- carbs,
- glucose
- ])
- }
- }
- private func autotunePrepare(
- pumphistory: JSON,
- profile: JSON,
- glucose: JSON,
- pumpprofile: JSON,
- categorizeUamAsBasal: Bool,
- tuneInsulinCurve: Bool
- ) -> RawJSON {
- dispatchPrecondition(condition: .onQueue(processQueue))
- return jsWorker.inCommonContext { worker in
- worker.evaluate(script: Script(name: Bundle.autotunePrep))
- worker.evaluate(script: Script(name: Prepare.autotunePrep))
- return worker.call(function: Function.generate, with: [
- pumphistory,
- profile,
- glucose,
- pumpprofile,
- categorizeUamAsBasal,
- tuneInsulinCurve
- ])
- }
- }
- private func autotuneRun(
- autotunePreparedData: JSON,
- previousAutotuneResult: JSON,
- pumpProfile: JSON
- ) -> RawJSON {
- dispatchPrecondition(condition: .onQueue(processQueue))
- return jsWorker.inCommonContext { worker in
- worker.evaluate(script: Script(name: Bundle.autotuneCore))
- worker.evaluate(script: Script(name: Prepare.autotuneCore))
- return worker.call(function: Function.generate, with: [
- autotunePreparedData,
- previousAutotuneResult,
- pumpProfile
- ])
- }
- }
- private func glucoseGetLast(glucose: JSON) -> RawJSON {
- dispatchPrecondition(condition: .onQueue(processQueue))
- return jsWorker.inCommonContext { worker in
- worker.evaluate(script: Script(name: Bundle.getLastGlucose))
- return worker.call(function: Function.freeaps, with: [glucose])
- }
- }
- private func determineBasal(
- glucoseStatus: JSON,
- currentTemp: JSON,
- iob: JSON,
- profile: JSON,
- aurosens: JSON,
- meal: JSON,
- microBolusAllowed: Bool,
- reservoir: Int,
- tsMilliseconds: Double
- ) -> RawJSON {
- dispatchPrecondition(condition: .onQueue(processQueue))
- return jsWorker.inCommonContext { worker in
- worker.evaluate(script: Script(name: Bundle.basalSetTemp))
- worker.evaluate(script: Script(name: Prepare.determineBasal))
- worker.evaluate(script: Script(name: Bundle.determineBasal))
- return worker.call(
- function: Function.freeaps,
- with: [
- glucoseStatus,
- currentTemp,
- iob,
- profile,
- aurosens,
- meal,
- Function.tempBasalFunctions,
- microBolusAllowed,
- reservoir,
- tsMilliseconds
- ]
- )
- }
- }
- private func autosense(
- pumpHistory: JSON,
- profile: JSON,
- carbs: JSON,
- glucose: JSON,
- basalprofile: JSON,
- temptargets: JSON
- ) -> RawJSON {
- dispatchPrecondition(condition: .onQueue(processQueue))
- return jsWorker.inCommonContext { worker in
- worker.evaluate(script: Script(name: Bundle.autosens))
- worker.evaluate(script: Script(name: Prepare.autosens))
- return worker.call(
- function: Function.generate,
- with: [
- pumpHistory,
- profile,
- carbs,
- glucose,
- basalprofile,
- temptargets
- ]
- )
- }
- }
- private func exportDefaultPreferences() -> RawJSON {
- dispatchPrecondition(condition: .onQueue(processQueue))
- return jsWorker.inCommonContext { worker in
- worker.evaluate(script: Script(name: Bundle.profile))
- worker.evaluate(script: Script(name: Prepare.profile))
- return worker.call(function: Function.exportDefaults, with: [])
- }
- }
- private func makeProfile(
- preferences: JSON,
- pumpSettings: JSON,
- bgTargets: JSON,
- basalProfile: JSON,
- isf: JSON,
- carbRatio: JSON,
- tempTargets: JSON,
- model: JSON,
- autotune: JSON
- ) -> RawJSON {
- dispatchPrecondition(condition: .onQueue(processQueue))
- return jsWorker.inCommonContext { worker in
- worker.evaluate(script: Script(name: Bundle.profile))
- worker.evaluate(script: Script(name: Prepare.profile))
- return worker.call(
- function: Function.generate,
- with: [
- preferences,
- pumpSettings,
- bgTargets,
- basalProfile,
- isf,
- carbRatio,
- tempTargets,
- model,
- autotune
- ]
- )
- }
- }
- private func loadJSON(name: String) -> String {
- try! String(contentsOf: Foundation.Bundle.main.url(forResource: "json/\(name)", withExtension: "json")!)
- }
- }
|