Skip to content

Commit

Permalink
Refactor B30
Browse files Browse the repository at this point in the history
No loops. More intuitive variables. Change description and conditionals.
  • Loading branch information
Jon-b-m committed Jan 28, 2025
1 parent 79877bd commit d607d56
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 23 deletions.
43 changes: 22 additions & 21 deletions FreeAPS/Resources/javascript/autoisf/autoisf.js
Original file line number Diff line number Diff line change
Expand Up @@ -446,32 +446,33 @@ const MillisecondsPerMinute = 60 * 1000

// B30
function aimi(profile, pumpHistory, dynamicVariables, glucose_status) {
// Guards
if (!profile.iaps.closedLoop) {
return
}
// Needs either a TT or a profile override < the set B30 target level
if (!(profile.temptargetSet && profile.min_bg < profile.iaps.b30targetLevel || dynamicVariables.useOverride && dynamicVariables.overrideTarget > 6 && dynamicVariables.overrideTarget < profile.iaps.b30targetLevel)) {
return
}

let minutesRemaining = profile.iaps.b30_duration;
let lastBolus = 0;
let lastBolusAge = minutesRemaining + 1;
let allowed_duration = profile.iaps.b30_duration;
let last_bolus_amount = 0;
let minutes_ago = allowed_duration + 1;
const minimal_bolus = profile.iaps.iTime_Start_Bolus;
let rate = profile.current_basal;
let now = new Date();
// Guards
if (!(profile.temptargetSet || (dynamicVariables.useOverride && dynamicVariables.overrideTarget > 6))) {
return
}
if (!((profile.min_bg <= profile.iaps.b30targetLevel) || (dynamicVariables.overrideTarget <= profile.iaps.b30targetLevel))) {
return
}
// Bolus age and bolus limit guards
for (let i = 0; i < pumpHistory.length; i++) {
if (pumpHistory[i]._type === "Bolus" && !(pumpHistory[i].isSMB) && pumpHistory[i].amount > profile.iaps.iTime_Start_Bolus) {
let bolusTime = new Date(pumpHistory[i].timestamp);
lastBolusAge = round( (now - bolusTime) / MillisecondsPerMinute, 1);
lastBolus = pumpHistory[i].amount;
break;
}

//Find Last Manual bolus
let bolus = pumpHistory.find((element) => element._type === "Bolus" && !element.isSMB);

// Update bolus amount and bolus minutes ago
if (bolus) {
let bolusTime = new Date(bolus.timestamp);
minutes_ago = round( (now - bolusTime) / MillisecondsPerMinute, 1);
last_bolus_amount = bolus.amount;
}
if (!(lastBolus >= profile.iaps.iTime_Start_Bolus && lastBolusAge <= minutesRemaining)) {

if (!(last_bolus_amount >= minimal_bolus && minutes_ago <= allowed_duration)) {
return
}
// Suggested B30 basal rate.
Expand All @@ -484,9 +485,9 @@ function aimi(profile, pumpHistory, dynamicVariables, glucose_status) {
addMessage("SMBs disabled (B30)")
}
// Logs
console.log("B30 is running. Time remaining: " + round((minutesRemaining - lastBolusAge), 1) + "min");
console.log("B30 is running. Time remaining: " + round((allowed_duration - minutes_ago), 1) + "min");
console.log("B30 Suggested Basal Rate: " + profile.basal_rate + " U/h.");
addMessage("B30 active, min remaining: " + round((minutesRemaining - lastBolusAge), 1));
addMessage("B30 active, min remaining: " + round((allowed_duration - minutes_ago), 1));
addReason("B30 Active");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2347,7 +2347,7 @@ Enact a temp Basal or a temp target */
"Target Level for B30 to be enacted" = "Target Level for B30 to be enacted";

/* */
"An EatingSoon Override Target (or a Temporary Target) needs to be activated to start the B30 adaption. Target needs to be below or equal this setting for B30 to start. Default is 90 mg/dl. If you cancel this EatingSoon Target, the B30 basal rate will stop." = "An EatingSoon Override Target (or a Temporary Target) needs to be activated to start the B30 adaption. Target needs to be below or equal this setting for B30 to start. Default is 90 mg/dl. If you cancel this EatingSoon Target, the B30 basal rate will stop.";
"An EatingSoon Override Target (or a Temporary Target) needs to be activated to start the B30 adaption. Target needs to be below this setting for B30 to start. Default is 90 mg/dl. If you cancel this EatingSoon Target, the B30 basal rate will stop." = "An EatingSoon Override Target (or a Temporary Target) needs to be activated to start the B30 adaption. Target needs to be below this setting for B30 to start. Default is 90 mg/dl. If you cancel this EatingSoon Target, the B30 basal rate will stop.";

/* */
"Minimum Start Bolus size" = "Minimum Start Bolus size";
Expand Down
2 changes: 1 addition & 1 deletion FreeAPS/Sources/Modules/AutoISF/View/AutoISFRootView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ extension AutoISF {
.onTapGesture {
info(
header: "Target Level for B30 to be enacted",
body: "An EatingSoon Override Target (or a Temporary Target) needs to be activated to start the B30 adaption. Target needs to be below or equal this setting for B30 to start. Default is 90 mg/dl. If you cancel this EatingSoon Target, the B30 basal rate will stop.",
body: "An EatingSoon Override Target (or a Temporary Target) needs to be activated to start the B30 adaption. Target needs to be below this setting for B30 to start. Default is 90 mg/dl. If you cancel this EatingSoon Target, the B30 basal rate will stop.",
useGraphics: nil
)
}
Expand Down

0 comments on commit d607d56

Please sign in to comment.