round-basal.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. var endsWith = require('lodash/endsWith');
  2. var round_basal = function round_basal(basal, profile) {
  3. /* x23 and x54 pumps change basal increment depending on how much basal is being delivered:
  4. 0.025u for 0.025 < x < 0.975
  5. 0.05u for 1 < x < 9.95
  6. 0.1u for 10 < x
  7. To round numbers nicely for the pump, use a scale factor of (1 / increment). */
  8. var lowest_rate_scale = 20;
  9. // Has profile even been passed in?
  10. if (typeof profile !== 'undefined')
  11. {
  12. // Make sure optional model has been set
  13. if (typeof profile.model === 'string')
  14. {
  15. if (endsWith(profile.model, "54") || endsWith(profile.model, "23"))
  16. {
  17. lowest_rate_scale = 40;
  18. }
  19. }
  20. }
  21. var rounded_basal = basal;
  22. // Shouldn't need to check against 0 as pumps can't deliver negative basal anyway?
  23. if (basal < 1)
  24. {
  25. rounded_basal = Math.round(basal * lowest_rate_scale) / lowest_rate_scale;
  26. }
  27. else if (basal < 10)
  28. {
  29. rounded_basal = Math.round(basal * 20) / 20;
  30. }
  31. else
  32. {
  33. rounded_basal = Math.round(basal * 10) / 10;
  34. }
  35. return rounded_basal;
  36. }
  37. exports = module.exports = round_basal