2017-08-14 12:20:12 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
function reason(rT, msg) {
|
|
|
|
rT.reason = (rT.reason ? rT.reason + '. ' : '') + msg;
|
|
|
|
console.error(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
var tempBasalFunctions = {};
|
|
|
|
|
|
|
|
tempBasalFunctions.getMaxSafeBasal = function getMaxSafeBasal(profile) {
|
|
|
|
|
2018-11-03 09:03:46 +01:00
|
|
|
var max_daily_safety_multiplier = (isNaN(profile.max_daily_safety_multiplier) || profile.max_daily_safety_multiplier == null) ? 3 : profile.max_daily_safety_multiplier;
|
|
|
|
var current_basal_safety_multiplier = (isNaN(profile.current_basal_safety_multiplier) || profile.current_basal_safety_multiplier == null) ? 4 : profile.current_basal_safety_multiplier;
|
|
|
|
|
|
|
|
return Math.min(profile.max_basal, max_daily_safety_multiplier * profile.max_daily_basal, current_basal_safety_multiplier * profile.current_basal);
|
2017-08-14 12:20:12 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
tempBasalFunctions.setTempBasal = function setTempBasal(rate, duration, profile, rT, currenttemp) {
|
|
|
|
//var maxSafeBasal = Math.min(profile.max_basal, 3 * profile.max_daily_basal, 4 * profile.current_basal);
|
2018-11-03 09:03:46 +01:00
|
|
|
|
2017-08-14 12:20:12 +02:00
|
|
|
var maxSafeBasal = tempBasalFunctions.getMaxSafeBasal(profile);
|
2018-11-03 09:03:46 +01:00
|
|
|
var round_basal = require('./round-basal');
|
|
|
|
|
|
|
|
if (rate < 0) {
|
|
|
|
rate = 0;
|
|
|
|
} else if (rate > maxSafeBasal) {
|
|
|
|
rate = maxSafeBasal;
|
2017-08-14 12:20:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var suggestedRate = round_basal(rate, profile);
|
2018-11-03 09:03:46 +01:00
|
|
|
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 ) {
|
2017-09-12 21:04:04 +02:00
|
|
|
rT.reason += " "+currenttemp.duration+"m left and " + currenttemp.rate + " ~ req " + suggestedRate + "U/hr: no temp required";
|
2017-08-14 12:20:12 +02:00
|
|
|
return rT;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (suggestedRate === profile.current_basal) {
|
2018-11-03 09:03:46 +01:00
|
|
|
if (profile.skip_neutral_temps === true) {
|
2017-08-14 12:20:12 +02:00
|
|
|
if (typeof(currenttemp) !== 'undefined' && typeof(currenttemp.duration) !== 'undefined' && currenttemp.duration > 0) {
|
|
|
|
reason(rT, 'Suggested rate is same as profile rate, a temp basal is active, canceling current temp');
|
|
|
|
rT.duration = 0;
|
|
|
|
rT.rate = 0;
|
|
|
|
return rT;
|
|
|
|
} else {
|
|
|
|
reason(rT, 'Suggested rate is same as profile rate, no temp basal is active, doing nothing');
|
|
|
|
return rT;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
reason(rT, 'Setting neutral temp basal of ' + profile.current_basal + 'U/hr');
|
|
|
|
rT.duration = duration;
|
|
|
|
rT.rate = suggestedRate;
|
|
|
|
return rT;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
rT.duration = duration;
|
|
|
|
rT.rate = suggestedRate;
|
|
|
|
return rT;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-11-03 09:03:46 +01:00
|
|
|
module.exports = tempBasalFunctions;
|