basal.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. var _ = require('lodash');
  2. /* Return basal rate(U / hr) at the provided timeOfDay */
  3. function basalLookup (schedules, now) {
  4. var nowDate = now;
  5. if (typeof(now) === 'undefined') {
  6. nowDate = new Date();
  7. }
  8. var basalprofile_data = _.sortBy(schedules, function(o) { return o.i; });
  9. var basalRate = basalprofile_data[basalprofile_data.length-1].rate
  10. if (basalRate === 0) {
  11. console.error("ERROR: bad basal schedule",schedules);
  12. return;
  13. }
  14. var nowMinutes = nowDate.getHours() * 60 + nowDate.getMinutes();
  15. for (var i = 0; i < basalprofile_data.length - 1; i++) {
  16. if ((nowMinutes >= basalprofile_data[i].minutes) && (nowMinutes < basalprofile_data[i + 1].minutes)) {
  17. basalRate = basalprofile_data[i].rate;
  18. break;
  19. }
  20. }
  21. return Math.round(basalRate*1000)/1000;
  22. }
  23. function maxDailyBasal (inputs) {
  24. var maxRate = _.maxBy(inputs.basals,function(o) { return Number(o.rate); });
  25. return (Number(maxRate.rate) *1000)/1000;
  26. }
  27. /*Return maximum daily basal rate(U / hr) from profile.basals */
  28. function maxBasalLookup (inputs) {
  29. return inputs.settings.maxBasal;
  30. }
  31. exports.maxDailyBasal = maxDailyBasal;
  32. exports.maxBasalLookup = maxBasalLookup;
  33. exports.basalLookup = basalLookup;