carbs.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. var getTime = require('../medtronic-clock');
  2. function carbRatioLookup (inputs, profile, now) {
  3. if (typeof(now) === 'undefined') {
  4. now = new Date();
  5. }
  6. var carbratio_data = inputs.carbratio;
  7. if (typeof(carbratio_data) !== "undefined" && typeof(carbratio_data.schedule) !== "undefined") {
  8. var carbRatio;
  9. if ((carbratio_data.units === "grams") || (carbratio_data.units === "exchanges")) {
  10. //carbratio_data.schedule.sort(function (a, b) { return a.offset > b.offset });
  11. carbRatio = carbratio_data.schedule[carbratio_data.schedule.length - 1];
  12. for (var i = 0; i < carbratio_data.schedule.length - 1; i++) {
  13. if ((now >= getTime(carbratio_data.schedule[i].offset)) && (now < getTime(carbratio_data.schedule[i + 1].offset))) {
  14. carbRatio = carbratio_data.schedule[i];
  15. break;
  16. }
  17. }
  18. // disallow impossibly high/low carbRatios due to bad decoding
  19. if (carbRatio.ratio < 1 || carbRatio.ratio > 150) {
  20. console.error("Error: carbRatio of " + carbRatio.ratio + " out of bounds.");
  21. return;
  22. }
  23. if (carbratio_data.units === "exchanges") {
  24. carbRatio.ratio = 12 / carbRatio.ratio
  25. }
  26. return carbRatio.ratio;
  27. } else {
  28. console.error("Error: Unsupported carb_ratio units " + carbratio_data.units);
  29. return;
  30. }
  31. //return carbRatio.ratio;
  32. //profile.carbratio = carbRatio.ratio;
  33. } else { return; }
  34. }
  35. carbRatioLookup.carbRatioLookup = carbRatioLookup;
  36. exports = module.exports = carbRatioLookup;