autotune-prep.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. function generate(pumphistory_data,profile_data,glucose_data,pumpprofile_data,carb_data={ },categorize_uam_as_basal=false,tune_insulin_curve=false){
  2. if ( typeof(profile_data.carb_ratio) === 'undefined' || profile_data.carb_ratio < 2 ) {
  3. if ( typeof(pumpprofile_data.carb_ratio) === 'undefined' || pumpprofile_data.carb_ratio < 2 ) {
  4. console.log('{ "carbs": 0, "mealCOB": 0, "reason": "carb_ratios ' + profile_data.carb_ratio + ' and ' + pumpprofile_data.carb_ratio + ' out of bounds" }');
  5. return console.error("Error: carb_ratios " + profile_data.carb_ratio + ' and ' + pumpprofile_data.carb_ratio + " out of bounds");
  6. } else {
  7. profile_data.carb_ratio = pumpprofile_data.carb_ratio;
  8. }
  9. }
  10. // get insulin curve from pump profile that is maintained
  11. profile_data.curve = pumpprofile_data.curve;
  12. // Pump profile has an up to date copy of useCustomPeakTime from preferences
  13. // If the preferences file has useCustomPeakTime use the previous autotune dia and PeakTime.
  14. // Otherwise, use data from pump profile.
  15. if (!pumpprofile_data.useCustomPeakTime) {
  16. profile_data.dia = pumpprofile_data.dia;
  17. profile_data.insulinPeakTime = pumpprofile_data.insulinPeakTime;
  18. }
  19. // Always keep the curve value up to date with what's in the user preferences
  20. profile_data.curve = pumpprofile_data.curve;
  21. // Have to sort history - NS sort doesn't account for different zulu and local timestamps
  22. pumphistory_data.sort( function( firstValue, secondValue ){
  23. try{
  24. var a = new Date(firstValue.timestamp);
  25. var b = new Date(secondValue.timestamp);
  26. return b.getTime() - a.getTime();
  27. }catch(e){
  28. return 0;
  29. }
  30. } );
  31. inputs = {
  32. history: pumphistory_data
  33. , profile: profile_data
  34. , pumpprofile: pumpprofile_data
  35. , carbs: carb_data
  36. , glucose: glucose_data
  37. , categorize_uam_as_basal: categorize_uam_as_basal
  38. , tune_insulin_curve: tune_insulin_curve
  39. };
  40. return freeaps_autotunePrep(inputs);
  41. }