carbs.js 1.6 KB

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