basal-set-temp.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. 'use strict';
  2. function reason(rT, msg) {
  3. rT.reason = (rT.reason ? rT.reason + '. ' : '') + msg;
  4. console.error(msg);
  5. }
  6. var tempBasalFunctions = {};
  7. tempBasalFunctions.getMaxSafeBasal = function getMaxSafeBasal(profile) {
  8. var max_daily_safety_multiplier = (isNaN(profile.max_daily_safety_multiplier) || profile.max_daily_safety_multiplier === null) ? 3 : profile.max_daily_safety_multiplier;
  9. var current_basal_safety_multiplier = (isNaN(profile.current_basal_safety_multiplier) || profile.current_basal_safety_multiplier === null) ? 4 : profile.current_basal_safety_multiplier;
  10. return Math.min(profile.max_basal, max_daily_safety_multiplier * profile.max_daily_basal, current_basal_safety_multiplier * profile.current_basal);
  11. };
  12. tempBasalFunctions.setTempBasal = function setTempBasal(rate, duration, profile, rT, currenttemp) {
  13. //var maxSafeBasal = Math.min(profile.max_basal, 3 * profile.max_daily_basal, 4 * profile.current_basal);
  14. var maxSafeBasal = tempBasalFunctions.getMaxSafeBasal(profile);
  15. var round_basal = require('./round-basal');
  16. if (rate < 0) {
  17. rate = 0;
  18. } else if (rate > maxSafeBasal) {
  19. rate = maxSafeBasal;
  20. }
  21. var suggestedRate = round_basal(rate, profile);
  22. if (typeof(currenttemp) !== 'undefined' && typeof(currenttemp.duration) !== 'undefined' && typeof(currenttemp.rate) !== 'undefined' && currenttemp.duration > (duration-10) && currenttemp.duration <= 120 && suggestedRate <= currenttemp.rate * 1.2 && suggestedRate >= currenttemp.rate * 0.8 && duration > 0 ) {
  23. rT.reason += " "+currenttemp.duration+"m left and " + currenttemp.rate + " ~ req " + suggestedRate + "U/hr: no temp required";
  24. return rT;
  25. }
  26. if (suggestedRate === profile.current_basal) {
  27. if (profile.skip_neutral_temps === true) {
  28. if (typeof(currenttemp) !== 'undefined' && typeof(currenttemp.duration) !== 'undefined' && currenttemp.duration > 0) {
  29. reason(rT, 'Suggested rate is same as profile rate, a temp basal is active, canceling current temp');
  30. rT.duration = 0;
  31. rT.rate = 0;
  32. return rT;
  33. } else {
  34. reason(rT, 'Suggested rate is same as profile rate, no temp basal is active, doing nothing');
  35. return rT;
  36. }
  37. } else {
  38. reason(rT, 'Setting neutral temp basal of ' + profile.current_basal + 'U/hr');
  39. rT.duration = duration;
  40. rT.rate = suggestedRate;
  41. return rT;
  42. }
  43. } else {
  44. rT.duration = duration;
  45. rT.rate = suggestedRate;
  46. return rT;
  47. }
  48. };
  49. module.exports = tempBasalFunctions;