Selaa lähdekoodia

Merge pull request #1 from ivalkou/autotune

Autotune
Ivan 5 vuotta sitten
vanhempi
commit
b70a53c2a4

+ 2 - 2
FreeAPS.xcodeproj/project.pbxproj

@@ -970,7 +970,7 @@
 				CODE_SIGN_ENTITLEMENTS = FreeAPS/Resources/FreeAPS.entitlements;
 				CODE_SIGN_STYLE = Automatic;
 				DEVELOPMENT_ASSET_PATHS = "\"FreeAPS/Preview Content\"";
-				DEVELOPMENT_TEAM = BA7ZHP4963;
+				DEVELOPMENT_TEAM = 777258T3K8;
 				ENABLE_PREVIEWS = YES;
 				INFOPLIST_FILE = FreeAPS/Resources/Info.plist;
 				IPHONEOS_DEPLOYMENT_TARGET = 14.0;
@@ -993,7 +993,7 @@
 				CODE_SIGN_ENTITLEMENTS = FreeAPS/Resources/FreeAPS.entitlements;
 				CODE_SIGN_STYLE = Automatic;
 				DEVELOPMENT_ASSET_PATHS = "\"FreeAPS/Preview Content\"";
-				DEVELOPMENT_TEAM = BA7ZHP4963;
+				DEVELOPMENT_TEAM = 777258T3K8;
 				ENABLE_PREVIEWS = YES;
 				INFOPLIST_FILE = FreeAPS/Resources/Info.plist;
 				IPHONEOS_DEPLOYMENT_TARGET = 14.0;

Tiedoston diff-näkymää rajattu, sillä se on liian suuri
+ 1 - 0
FreeAPS/Resources/javascript/bundle/autotune-core.js


Tiedoston diff-näkymää rajattu, sillä se on liian suuri
+ 2 - 0
FreeAPS/Resources/javascript/bundle/autotune-prep.js


+ 17 - 0
FreeAPS/Resources/javascript/prepare/autotune-core.js

@@ -0,0 +1,17 @@
+function generate(prepped_glucose_data,previous_autotune_data,pumpprofile_data){
+  if (!pumpprofile_data.useCustomPeakTime) {
+      previous_autotune_data.dia = pumpprofile_data.dia;
+      previous_autotune_data.insulinPeakTime = pumpprofile_data.insulinPeakTime;
+    }
+
+    // Always keep the curve value up to date with what's in the user preferences
+    previous_autotune_data.curve = pumpprofile_data.curve;
+
+    inputs = {
+        preppedGlucose: prepped_glucose_data
+      , previousAutotune: previous_autotune_data
+      , pumpProfile: pumpprofile_data
+    };
+
+    return freeaps(inputs);
+}

+ 47 - 0
FreeAPS/Resources/javascript/prepare/autotune-prep.js

@@ -0,0 +1,47 @@
+function generate(pumphistory_data,profile_data,glucose_data,pumpprofile_data,carb_data={ },categorize_uam_as_basal=false,tune_insulin_curve=false){
+    if ( typeof(profile_data.carb_ratio) === 'undefined' || profile_data.carb_ratio < 2 ) {
+        if ( typeof(pumpprofile_data.carb_ratio) === 'undefined' || pumpprofile_data.carb_ratio < 2 ) {
+            console.log('{ "carbs": 0, "mealCOB": 0, "reason": "carb_ratios ' + profile_data.carb_ratio + ' and ' + pumpprofile_data.carb_ratio + ' out of bounds" }');
+            return console.error("Error: carb_ratios " + profile_data.carb_ratio + ' and ' + pumpprofile_data.carb_ratio + " out of bounds");
+        } else {
+            profile_data.carb_ratio = pumpprofile_data.carb_ratio;
+        }
+    }
+
+    // get insulin curve from pump profile that is maintained
+    profile_data.curve = pumpprofile_data.curve;
+
+    // Pump profile has an up to date copy of useCustomPeakTime from preferences
+    // If the preferences file has useCustomPeakTime use the previous autotune dia and PeakTime.
+    // Otherwise, use data from pump profile.
+    if (!pumpprofile_data.useCustomPeakTime) {
+      profile_data.dia = pumpprofile_data.dia;
+      profile_data.insulinPeakTime = pumpprofile_data.insulinPeakTime;
+    }
+
+    // Always keep the curve value up to date with what's in the user preferences
+    profile_data.curve = pumpprofile_data.curve;
+
+    // Have to sort history - NS sort doesn't account for different zulu and local timestamps
+    pumphistory_data.sort( function( firstValue, secondValue ){
+        try{
+            var a = new Date(firstValue.timestamp);
+            var b = new Date(secondValue.timestamp);
+            return b.getTime() - a.getTime();
+        }catch(e){
+            return 0;
+        }
+    } );
+
+    inputs = {
+      history: pumphistory_data
+    , profile: profile_data
+    , pumpprofile: pumpprofile_data
+    , carbs: carb_data
+    , glucose: glucose_data
+    , categorize_uam_as_basal: categorize_uam_as_basal
+    , tune_insulin_curve: tune_insulin_curve
+    };
+
+    return freeaps(inputs);
+}

+ 59 - 0
FreeAPS/Sources/APS/OpenAPS/OpenAPS.swift

@@ -64,6 +64,25 @@ final class OpenAPS {
                 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 autotuneResult = self.autotuneRun(
+                autotuneprepareddata: autotunePreppedGlucose,
+                previousautotuneresult: profile,
+                pumpprofile: profile
+            )
+
+            print("AUTOTUNE RESULT: \(autotuneResult)")
+
             let finishDate = Date()
             print("FINISH at \(finishDate), duration \(finishDate.timeIntervalSince(now)) s")
         }
@@ -100,6 +119,46 @@ final class OpenAPS {
         }
     }
 
+    private func autotunePrepare(
+        pumphistory: JSON,
+        profile: JSON,
+        glucose: JSON,
+        pumpprofile: JSON,
+        categorizeUamAsBasal: Bool,
+        tuneInsulinCurve: Bool
+    ) -> JSON {
+        dispatchPrecondition(condition: .onQueue(processQueue))
+        return jsWorker.inCommonContext { worker in
+            worker.evaluate(script: Script(name: "bundle/autotune-prep"))
+            worker.evaluate(script: Script(name: "prepare/autotune-prep"))
+            return worker.call(function: "generate", with: [
+                pumphistory,
+                profile,
+                glucose,
+                pumpprofile,
+                categorizeUamAsBasal,
+                tuneInsulinCurve
+            ])
+        }
+    }
+
+    private func autotuneRun(
+        autotuneprepareddata: JSON,
+        previousautotuneresult: JSON,
+        pumpprofile: JSON
+    ) -> JSON {
+        dispatchPrecondition(condition: .onQueue(processQueue))
+        return jsWorker.inCommonContext { worker in
+            worker.evaluate(script: Script(name: "bundle/autotune-core"))
+            worker.evaluate(script: Script(name: "prepare/autotune-core"))
+            return worker.call(function: "generate", with: [
+                autotuneprepareddata,
+                previousautotuneresult,
+                pumpprofile
+            ])
+        }
+    }
+
     private func glucoseGetLast(glucose: JSON) -> JSON {
         dispatchPrecondition(condition: .onQueue(processQueue))
         return jsWorker.inCommonContext { worker in