autotune-prep.js 2.2 KB

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