determine-basal.js 27 KB

1
  1. var freeaps_determineBasal;(()=>{var e={5546:(e,r,a)=>{var t=a(6880);function o(e,r){r||(r=0);var a=Math.pow(10,r);return Math.round(e*a)/a}function n(e,r){return"mmol/L"===r.out_units?o(e/18,1):Math.round(e)}var i="",s="",l="",m="",u="",d="",c="",g="",b="";function f(e,r){var a=[2,7,12,16,20,50,60,80,90,100,110,150,180,200],t=[0,0,.4,.7,.7,-.5,-.5,-.3,-.2,0,0,.5,.7,.7],o=a.length-1,n=a[0],i=t[0],s=a[o],l=t[o],m=1,u=1,d=1,c=n;if(n>e)m=(u=i)+((l=t[1])-u)/((s=a[1])-(d=n))*(e-d);else if(s<e)m=(u=i=t[o-1])+(l-u)/(s-(d=n=a[o-1]))*(e-d);else for(var g=0;g<=o;g++){if(i=t[g],(n=a[g])==e){m=i;break}if(n>e){m=u+(i-u)/(n-(d=c))*(e-d);break}u=i,c=n}return m*=e>100?r.higher_ISFrange_weight:e>40?r.lower_ISFrange_weight:r.delta_ISFrange_weight}function p(e,r,a){if(void 0===e.smb_delivery_ratio_bg_range||0===e.smb_delivery_ratio_bg_range)return console.error("SMB delivery ratio set to fixed value "+e.smb_delivery_ratio),e.smb_delivery_ratio;var t=Math.min(e.smb_delivery_ratio_min,e.smb_delivery_ratio_max);if(r<=a)return console.error("SMB delivery ratio limited by minimum value "+t),t;var n=Math.max(e.smb_delivery_ratio_min,e.smb_delivery_ratio_max);if(r>=a+e.smb_delivery_ratio_bg_range)return console.error("SMB delivery ratio limited by maximum value "+n),n;var i=t+(n-t)*(r-a)/e.smb_delivery_ratio_bg_range;return console.error("SMB delivery ratio set to interpolated value "+o(i,2)),i}e.exports=function(e,r,a,h,v,_,B,M,x,y){var S="",C="",F="";if(void 0!==v&&v){const r=v.ratio,a=h.curve,t=h.insulinPeakTime,n=h.useCustomPeakTime,i=h.autoseens_min,s=h.autosens_max,l=h.adjustmentFactor,m=h.enableChris,u=h.enableDynamicCR;var I=55,w=e.glucose;switch(a){case"rapid-acting":I=55;break;case"ultra-rapid":I=t<75&&1==n?120-t:70}if(1==m||1==u){1==h.useNewFormula?(tddformula=", Formula: Logarithmic, AF: "+l,F=", TDD: "+o(1800*r/(h.sens*l*Math.log(w/I+1)),2)+" U"):(C=", Formula: Original, AF: "+l,F=", TDD: "+o(277700*r/(h.sens*l*w),2)+" U"),r!=i&&r!=s||(console.error("tdd: N/A"),0,F=", TDD: N/A");var G="";G=1==m?" On/":" Off/";S=F+C+(", Dynamic ISF/CR: "+(G+=1==u?"On":"Off"))}}var O={},T=new Date;if(y&&(T=y),void 0===h||void 0===h.current_basal)return O.error="Error: could not get current basal rate",O;var A=t(h.current_basal,h),D=A,R=new Date;y&&(R=y);var U,j=new Date(e.date),P=o((R-j)/60/1e3,1),k=e.glucose,q=e.noise;U=e.delta>-.5?"+"+o(e.delta,0):o(e.delta,0);var E=Math.min(e.delta,e.short_avgdelta),W=Math.min(e.short_avgdelta,e.long_avgdelta),z=Math.max(e.delta,e.short_avgdelta,e.long_avgdelta);(k<=10||38===k||q>=3)&&(O.reason="CGM is calibrating, in ??? state, or noise is high");if(k>60&&0==e.delta&&e.short_avgdelta>-1&&e.short_avgdelta<1&&e.long_avgdelta>-1&&e.long_avgdelta<1&&("fakecgm"==e.device?(console.error("CGM data is unchanged ("+n(k,h)+"+"+n(e.delta,h)+") for 5m w/ "+n(e.short_avgdelta,h)+" mg/dL ~15m change & "+n(e.long_avgdelta,2)+" mg/dL ~45m change"),console.error("Simulator mode detected ("+e.device+"): continuing anyway")):!0),P>12||P<-5?O.reason="If current system time "+R+" is correct, then BG data is too old. The last BG data was read "+P+"m ago at "+j:0===e.short_avgdelta&&0===e.long_avgdelta&&(e.last_cal&&e.last_cal<3?O.reason="CGM was just calibrated":O.reason="CGM data is unchanged ("+n(k,h)+"+"+n(e.delta,h)+") for 5m w/ "+n(e.short_avgdelta,h)+" mg/dL ~15m change & "+n(e.long_avgdelta,h)+" mg/dL ~45m change"),k<=10||38===k||q>=3||P>12||P<-5||0===e.short_avgdelta&&0===e.long_avgdelta)return r.rate>=D?(O.reason+=". Canceling high temp basal of "+r.rate,O.deliverAt=T,O.temp="absolute",O.duration=0,O.rate=0,O):0===r.rate&&r.duration>30?(O.reason+=". Shortening "+r.duration+"m long zero temp to 30m. ",O.deliverAt=T,O.temp="absolute",O.duration=30,O.rate=0,O):(O.reason+=". Temp "+r.rate+" <= current basal "+D+"U/hr; doing nothing. ",O);var L,N,Z,$=h.max_iob;if(void 0!==h.min_bg&&(N=h.min_bg),void 0!==h.max_bg&&(Z=h.max_bg),void 0===h.min_bg||void 0===h.max_bg)return O.error="Error: could not determine target_bg. ",O;L=(h.min_bg+h.max_bg)/2;var H=h.exercise_mode||h.high_temptarget_raises_sensitivity,J=100,K=160;if(h.half_basal_exercise_target&&(K=h.half_basal_exercise_target),H&&h.temptargetSet&&L>J||h.low_temptarget_lowers_sensitivity&&h.temptargetSet&&L<J){var Q=K-J;Q+L-J>0?(sensitivityRatio=Q/(Q+L-J),sensitivityRatio=Math.min(sensitivityRatio,h.autosens_max),sensitivityRatio=o(sensitivityRatio,2)):sensitivityRatio=h.autosens_max,process.stderr.write("Sensitivity ratio set to "+sensitivityRatio+" based on temp target of "+L+"; ")}else void 0!==v&&v&&(sensitivityRatio=v.ratio,process.stderr.write("Autosens ratio: "+sensitivityRatio+"; "));if(sensitivityRatio&&(D=h.current_basal*sensitivityRatio,(D=t(D,h))!==A?process.stderr.write("Adjusting basal from "+A+" to "+D+"; "):process.stderr.write("Basal unchanged: "+D+"; ")),h.temptargetSet);else if(void 0!==v&&v&&(h.sensitivity_raises_target&&v.ratio<1||h.resistance_lowers_target&&v.ratio>1)){N=o((N-60)/v.ratio)+60,Z=o((Z-60)/v.ratio)+60;var V=o((L-60)/v.ratio)+60;L===(V=Math.max(80,V))?process.stderr.write("target_bg unchanged: "+V+"; "):process.stderr.write("target_bg from "+L+" to "+V+"; "),L=V}var X=200,Y=200,ee=200;if(e.noise>=2){var re=Math.max(1.1,h.noisyCGMTargetMultiplier);Math.min(250,h.maxRaw);X=o(Math.min(200,N*re)),Y=o(Math.min(200,L*re)),ee=o(Math.min(200,Z*re)),process.stderr.write("Raising target_bg for noisy / raw CGM data, from "+L+" to "+Y+"; "),N=X,L=Y,Z=ee}var ae=N-.5*(N-40),te=o(h.sens,1),oe=h.sens;if(void 0!==v&&v&&((oe=o(oe=h.sens/sensitivityRatio,1))!==te?process.stderr.write("ISF from "+n(te,h)+" to "+n(oe,h)):process.stderr.write("ISF unchanged: "+n(oe,h)),i+="Autosens ratio: "+o(sensitivityRatio,2)+", ISF: "+n(te,h)+"→"+n(oe,h)),console.error("CR:"+h.carb_ratio),oe=function(e,r,a,t,h,v,_,B){if(!a.use_autoisf)return console.error("autoISF disabled in Preferences"),e;var M=t.dura_p,x=t.delta_pl,y=t.delta_pn,S=t.r_squ,C=t.bg_acceleration,F=t.parabola_fit_a0,I=t.parabola_fit_a1,w=t.parabola_fit_a2,G=t.autoISF_duration,O=t.autoISF_average,T=a.autoisf_max,A=!1,D=1,R=1,U=1,j=r+10-O;if(!(h.mealCOB>0)||a.enableautoisf_with_COB){var P=t.pp_debug;if(d+="BG-accel: "+o(C,3)+", PF-minutes: "+M+", PF-corr: "+o(S,4)+", PF-nextDelta: "+n(y,a)+", PF-lastDelta: "+n(x,a)+", regular Delta: "+n(t.delta,a),console.error(P+d+" , Weights Accel/Brake: "+a.bgAccel_ISF_weight+" / "+a.bgBrake_ISF_weight),a.enable_BG_acceleration){var k=C;if(0!=t.parabola_fit_a2){var q=-I/2/w*5,E=o(F-q*q/25*w,1);(q=o(q,1))<0&&k<0?(b="saw max of "+n(E,a)+", about "+-q+" min ago",console.error("Parabolic fit "+b)):q<0&&k>0?(b="saw min of "+n(E,a)+", about "+-q+" min ago",console.error("Parabolic fit "+b)):q>0&&k<0?(b="predicts max of "+n(E,a)+", in about "+q+"min",console.error("Parabolic fit "+b)):q>0&&k>0&&(b="predicts min of "+n(E,a)+", in about "+q+" min",console.error("Parabolic fit "+b))}var W=S;if(W<=.9)b="acce_ISF by-passed, as correlation, "+o(W,3)+", is too low",console.error("Parabolic fit "+b),c+=", Parabolic Fit, "+b;else{c+=", Parabolic Fit, "+b+", lastΔ: "+n(x,a)+", nextΔ: "+n(y,a)+", Corr "+o(S,3)+", BG-Accel: "+o(k,2);var z=10*(W-.9),L=1;t.glucose<a.target_bg&&k>1&&(L=.5),U=1+k*L*(k<0?a.bgBrake_ISF_weight:a.bgAccel_ISF_weight)*z,console.error("Original result for acce_ISF: "+o(U,2)),1!=U&&(A=!0,c+=", acce-ISF Ratio: "+o(U,2))}}else console.error("autoISF BG accelertion adaption disabled in Preferences");var N=p(a,t.glucose,r);i+=", SMB Delivery Ratio:, "+o(N,2)+c+", autoISF";var Z=1+f(100-j,a);console.error("bg_ISF adaptation is "+o(Z,2)),Z<1&&U>1&&(g="bg-ISF adaptation lifted to "+o(Z*=U,2)+", as BG accelerates already",s="(lifted by "+o(U,2)+")",console.error(g));var $=1;if(Z<1)return($=Math.min(Z,U))<a.autoisf_min&&(g="final ISF factor "+o($,2)+" limited by autoisf_min "+a.autoisf_min,console.error(g),$=a.autoisf_min),s=" (lmtd.)",earlysens=Math.min(720,o(a.sens/Math.min(B,$),1)),console.error("early Return autoISF: "+n(earlysens,a)),i+=", bg-ISF Ratio: "+o(Z,2)+s+", ISF: "+n(earlysens,a),earlysens;Z>1&&(A=!0,i+=", bg-ISF Ratio: "+o(Z,2));var H=t.delta;j>0?console.error("delta_ISF adaptation by-passed as average glucose < "+n(r+10,a)):t.short_avgdelta<0?console.error("delta_ISF adaptation by-passed as no rise or too short lived"):a.enableppisf_always||a.postmeal_ISF_duration>=(v-h.lastCarbTime)/1e3/3600?(D=1+Math.max(0,H*a.postmeal_ISF_weight),console.error("pp_ISF adaptation is "+o(D,2)),m=", pp-ISF Ratio: "+o(D,2),1!=D&&(A=!0)):(R=f(H,a),j>-20&&(R*=.5),R=1+R,console.error("delta_ISF adaptation is "+o(R,2)),u=", Δ-ISF Ratio: "+o(R,2),1!=R&&(A=!0));var J=1,K=a.autoisf_hourlychange;return h.mealCOB>0&&!a.enableautoisf_with_COB?console.error("dura_ISF by-passed; preferences disabled mealCOB of "+o(h.mealCOB,1)):G<10?console.error("dura_ISF by-passed; BG is only "+G+"m at level "+O):O<=r?console.error("dura_ISF by-passed; avg. glucose "+O+" below target "+n(r,a)):(J+=G/60*(K/r)*(O-r),A=!0,l=", Duration: "+G+", Avg: "+n(O,a)+", dura-ISF Ratio: "+o(J,2),console.error("dura_ISF adaptation is "+o(J,2)+" because ISF "+e+" did not do it for "+o(G,1)+"m")),$=1,A?($=Math.max(J,Z,R,U,D),console.error("autoISF adaption ratios:"),console.error(" dura "+o(J,2)),console.error(" bg "+o(Z,2)),console.error(" delta "+o(R,2)),console.error(" pp "+o(D,2)),console.error(" accel "+o(U,2)),U<1&&(console.error("strongest ISF factor "+o($,2)+" weakened to "+o($*U,2)+" as bg decelerates already"),$*=U),$<a.autoisf_min?(console.error("final ISF factor "+o($,2)+" limited by autoisf_min "+a.autoisf_min),$=a.autoisf_min):$>T&&(console.error("final ISF factor "+o($,2)+" limited by autoisf_max "+T),$=T),$>=1&&(e=o(a.sens/Math.max($,B),1)),$<1&&(e=o(a.sens/Math.min($,B),1))):$=B,i+=m+u+l+", Ratio: "+o($,2)+", ISF: "+n(e,a),console.error("Inside autoISF: Ratio "+o($,2)+" resulting in "+n(e,a)),e}console.error("BG dependant autoISF by-passed; preferences disabled mealCOB of "+o(h.mealCOB,1))}(oe,L,h,e,_,y,0,sensitivityRatio),void 0===a)return O.error="Error: iob_data undefined. ",O;var ne,ie=a;if(a.length,a.length>1&&(a=ie[0]),void 0===a.activity||void 0===a.iob)return O.error="Error: iob_data missing some property. ",O;var se=((ne=void 0!==a.lastTemp?o((new Date(R).getTime()-a.lastTemp.date)/6e4):0)+r.duration)%30;if(console.error("currenttemp:"+r.rate+" lastTempAge:"+ne+"m, tempModulus:"+se+"m"),O.temp="absolute",O.deliverAt=T,M&&r&&a.lastTemp&&r.rate!==a.lastTemp.rate&&ne>10&&r.duration)return O.reason="Warning: currenttemp rate "+r.rate+" != lastTemp rate "+a.lastTemp.rate+" from pumphistory; canceling temp",B.setTempBasal(0,0,h,O,r);if(r&&a.lastTemp&&r.duration>0){var le=ne-a.lastTemp.duration;if(le>5&&ne>10)return O.reason="Warning: currenttemp running but lastTemp from pumphistory ended "+le+"m ago; canceling temp",B.setTempBasal(0,0,h,O,r)}var me=o(-a.activity*oe*5,2),ue=o(6*(E-me));ue<0&&(ue=o(6*(W-me)))<0&&(ue=o(6*(e.long_avgdelta-me)));var de=k,ce=(de=a.iob>0?o(k-a.iob*oe):o(k-a.iob*Math.min(oe,h.sens)))+ue;if(void 0===ce||isNaN(ce))return O.error="Error: could not calculate eventualBG. Sensitivity: "+oe+" Deviation: "+ue,O;var ge=function(e,r,a){return o(a+(e-r)/24,1)}(L,ce,me);O={temp:"absolute",bg:k,tick:U,eventualBG:ce,insulinReq:0,reservoir:x,deliverAt:T,sensitivityRatio};var be=[],fe=[],pe=[],he=[];be.push(k),fe.push(k),he.push(k),pe.push(k);var ve=function(e,r,a,t){return r?!e.allowSMB_with_high_temptarget&&e.temptargetSet&&t>100?(console.error("SMB disabled due to high temptarget of",t),!1):!0===a.bwFound&&!1===e.A52_risk_enable?(console.error("SMB disabled due to Bolus Wizard activity in the last 6 hours."),!1):!0===e.enableSMB_always?(a.bwFound?console.error("Warning: SMB enabled within 6h of using Bolus Wizard: be sure to easy bolus 30s before using Bolus Wizard"):console.error("SMB enabled due to enableSMB_always"),!0):!0===e.enableSMB_with_COB&&a.mealCOB?(a.bwCarbs?console.error("Warning: SMB enabled with Bolus Wizard carbs: be sure to easy bolus 30s before using Bolus Wizard"):console.error("SMB enabled for COB of",a.mealCOB),!0):!0===e.enableSMB_after_carbs&&a.carbs?(a.bwCarbs?console.error("Warning: SMB enabled with Bolus Wizard carbs: be sure to easy bolus 30s before using Bolus Wizard"):console.error("SMB enabled for 6h after carb entry"),!0):!0===e.enableSMB_with_temptarget&&e.temptargetSet&&t<100?(a.bwFound?console.error("Warning: SMB enabled within 6h of using Bolus Wizard: be sure to easy bolus 30s before using Bolus Wizard"):console.error("SMB enabled for temptarget of",n(t,e)),!0):(console.error("SMB disabled (no enableSMB preferences active or no condition satisfied)"),!1):(console.error("SMB disabled (!microBolusAllowed)"),!1)}(h,M,_,L),_e=h.enableUAM,Be=0,Me=0;Be=o(E-me,1);var xe=o(E-me,1);csf=oe/h.carb_ratio,console.error("profile.sens:"+n(h.sens,h)+", sens:"+n(oe,h)+", CSF:"+o(csf,1));var ye=o(30*csf*5/60,1);Be>ye&&(console.error("Limiting carb impact from "+Be+" to "+ye+"mg/dL/5m (30g/h)"),Be=ye);var Se=3;sensitivityRatio&&(Se/=sensitivityRatio);var Ce=Se;if(_.carbs){Se=Math.max(Se,_.mealCOB/20);var Fe=o((new Date(R).getTime()-_.lastCarbTime)/6e4),Ie=(_.carbs-_.mealCOB)/_.carbs;Ce=o(Ce=Se+1.5*Fe/60,1),console.error("Last carbs "+Fe+" minutes ago; remainingCATime:"+Ce+"hours; "+o(100*Ie)+"% carbs absorbed")}var we=Math.max(0,Be/5*60*Ce/2)/csf,Ge=90,Oe=1;h.remainingCarbsCap&&(Ge=Math.min(90,h.remainingCarbsCap)),h.remainingCarbsFraction&&(Oe=Math.min(1,h.remainingCarbsFraction));var Te=1-Oe,Ae=Math.max(0,_.mealCOB-we-_.carbs*Te),De=(Ae=Math.min(Ge,Ae))*csf*5/60/(Ce/2),Re=o(_.slopeFromMaxDeviation,2),Ue=o(_.slopeFromMinDeviation,2),je=Math.min(Re,-Ue/3),Pe=0;0===Be?Me=0:!0===h.floating_carbs?(Me=Math.min(60*Ce/5/2,Math.max(0,_.carbs*csf/Be)),Pe=Math.min(60*Ce/5/2,Math.max(0,_.mealCOB*csf/Be)),_.carbs>0&&(i+=", Floating Carbs:, CID: "+o(Me,1)+", MealCarbs: "+o(_.carbs,1)+", Not Floating:, CID: "+o(Pe,1)+", MealCOB: "+o(_.mealCOB,1),console.error("Floating Carbs CID: "+o(Me,1)+" / MealCarbs: "+o(_.carbs,1)+" vs. Not Floating:"+o(Pe,1)+" / MealCOB:"+o(_.mealCOB,1)))):Me=Math.min(60*Ce/5/2,Math.max(0,_.mealCOB*csf/Be)),console.error("Carb Impact:"+Be+"mg/dL per 5m; CI Duration:"+o(5*Me/60*2,1)+"hours; remaining CI ("+Ce/2+"h peak):",o(De,1)+"mg/dL per 5m");var ke,qe,Ee,We,ze,Le=999,Ne=999,Ze=999,$e=k,He=999,Je=999,Ke=999,Qe=999,Ve=ce,Xe=k,Ye=k,er=0,rr=[],ar=[];try{ie.forEach((function(e){var r=o(-e.activity*oe*5,2),a=o(-e.iobWithZeroTemp.activity*oe*5,2),t=Be*(1-Math.min(1,fe.length/12));Ve=fe[fe.length-1]+r+t;var n=he[he.length-1]+a,i=Math.max(0,Math.max(0,Be)*(1-be.length/Math.max(2*Me,1))),s=Math.min(be.length,12*Ce-be.length),l=Math.max(0,s/(Ce/2*12)*De);i+l,rr.push(o(l,0)),ar.push(o(i,0)),COBpredBG=be[be.length-1]+r+Math.min(0,t)+i+l;var m=Math.max(0,xe+pe.length*je),u=Math.max(0,xe*(1-pe.length/Math.max(36,1))),d=Math.min(m,u);d>0&&(er=o(5*(pe.length+1)/60,1)),UAMpredBG=pe[pe.length-1]+r+Math.min(0,t)+d,fe.length<48&&fe.push(Ve),be.length<48&&be.push(COBpredBG),pe.length<48&&pe.push(UAMpredBG),he.length<48&&he.push(n),COBpredBG<He&&(He=o(COBpredBG)),UAMpredBG<Je&&(Je=o(UAMpredBG)),Ve<Ke&&(Ke=o(Ve)),n<Qe&&(Qe=o(n));fe.length>18&&Ve<Le&&(Le=o(Ve)),Ve>Xe&&(Xe=Ve),(Me||De>0)&&be.length>18&&COBpredBG<Ne&&(Ne=o(COBpredBG)),(Me||De>0)&&COBpredBG>Xe&&(Ye=COBpredBG),_e&&pe.length>12&&UAMpredBG<Ze&&(Ze=o(UAMpredBG)),_e&&UAMpredBG>Xe&&UAMpredBG}))}catch(e){console.error("Problem with iobArray. Optional feature Advanced Meal Assist disabled")}_.mealCOB&&(console.error("predCIs (mg/dL/5m):"+ar.join(" ")),console.error("remainingCIs: "+rr.join(" "))),O.predBGs={},fe.forEach((function(e,r,a){a[r]=o(Math.min(401,Math.max(39,e)))}));for(var tr=fe.length-1;tr>12&&fe[tr-1]===fe[tr];tr--)fe.pop();for(O.predBGs.IOB=fe,Ee=o(fe[fe.length-1]),he.forEach((function(e,r,a){a[r]=o(Math.min(401,Math.max(39,e)))})),tr=he.length-1;tr>6&&!(he[tr-1]>=he[tr]||he[tr]<=L);tr--)he.pop();if(O.predBGs.ZT=he,o(he[he.length-1]),_.mealCOB>0&&(Be>0||De>0)){for(be.forEach((function(e,r,a){a[r]=o(Math.min(401,Math.max(39,e)))})),tr=be.length-1;tr>12&&be[tr-1]===be[tr];tr--)be.pop();O.predBGs.COB=be,We=o(be[be.length-1]),ce=Math.max(ce,o(be[be.length-1]))}if(Be>0||De>0){if(_e){for(pe.forEach((function(e,r,a){a[r]=o(Math.min(401,Math.max(39,e)))})),tr=pe.length-1;tr>12&&pe[tr-1]===pe[tr];tr--)pe.pop();O.predBGs.UAM=pe,ze=o(pe[pe.length-1]),pe[pe.length-1]&&(ce=Math.max(ce,o(pe[pe.length-1])))}O.eventualBG=ce}console.error("UAM Impact:"+xe+"mg/dL per 5m; UAM Duration:"+er+"hours"),Le=Math.max(39,Le),Ne=Math.max(39,Ne),Ze=Math.max(39,Ze),ke=o(Le);var or=_.mealCOB/_.carbs;qe=o(Ze<999&&Ne<999?(1-or)*UAMpredBG+or*COBpredBG:Ne<999?(Ve+COBpredBG)/2:Ze<999?(Ve+UAMpredBG)/2:Ve),Qe>qe&&(qe=Qe),$e=o($e=Me||De>0?_e?or*He+(1-or)*Je:He:_e?Je:Ke);var nr=Ze;if(Qe<ae)nr=(Ze+Qe)/2;else if(Qe<L){var ir=(Qe-ae)/(L-ae);nr=(Ze+(Ze*ir+Qe*(1-ir)))/2}else Qe>Ze&&(nr=(Ze+Qe)/2);if(nr=o(nr),_.carbs)if(!_e&&Ne<999)ke=o(Math.max(Le,Ne));else if(Ne<999){var sr=or*Ne+(1-or)*nr;ke=o(Math.max(Le,Ne,sr))}else ke=_e?nr:$e;else _e&&(ke=o(Math.max(Le,nr)));ke=Math.min(ke,qe),process.stderr.write("minPredBG: "+ke+" minIOBPredBG: "+Le+" minZTGuardBG: "+Qe),Ne<999&&process.stderr.write(" minCOBPredBG: "+Ne),Ze<999&&process.stderr.write(" minUAMPredBG: "+Ze),console.error(" avgPredBG:"+qe+" COB/Carbs:"+_.mealCOB+"/"+_.carbs),Ye>k&&(ke=Math.min(ke,Ye)),O.COB=_.mealCOB,O.IOB=a.iob,O.BGI=n(me,h),O.deviation=n(ue,h),O.ISF=n(oe,h),O.CR=o(h.carb_ratio,2),O.target_bg=n(L,h),O.reason=i+", COB: "+O.COB+", Dev: "+O.deviation+", BGI: "+O.BGI+", CR: "+O.CR+", Target: "+O.target_bg+", minPredBG "+n(ke,h)+", minGuardBG "+n($e,h)+", IOBpredBG "+n(Ee,h)+S,We>0&&(O.reason+=", COBpredBG "+n(We,h)),ze>0&&(O.reason+=", UAMpredBG "+n(ze,h)),O.reason+="; ";var lr=de;lr<40&&(lr=Math.min($e,lr));var mr,ur=ae-lr,dr=240,cr=240;if(_.mealCOB>0&&(Be>0||De>0)){for(tr=0;tr<be.length;tr++)if(be[tr]<N){dr=5*tr;break}for(tr=0;tr<be.length;tr++)if(be[tr]<ae){cr=5*tr;break}}else{for(tr=0;tr<fe.length;tr++)if(fe[tr]<N){dr=5*tr;break}for(tr=0;tr<fe.length;tr++)if(fe[tr]<ae){cr=5*tr;break}}ve&&$e<ae&&(console.error("minGuardBG "+n($e,h)+" projected below "+n(ae,h)+" - disabling SMB"),ve=!1),void 0===h.maxDelta_bg_threshold&&(mr=.2),void 0!==h.maxDelta_bg_threshold&&(mr=Math.min(h.maxDelta_bg_threshold,.4)),z>mr*k&&(console.error("maxDelta "+n(z,h)+" > "+100*mr+"% of BG "+n(k,h)+" - disabling SMB"),O.reason+="maxDelta "+n(z,h)+" > "+100*mr+"% of BG "+n(k,h)+" - SMB disabled!, ",ve=!1),console.error("BG projected to remain above "+n(N,h)+" for "+dr+"minutes"),(cr<240||dr<60)&&console.error("BG projected to remain above "+n(ae,h)+" for "+cr+"minutes");var gr=cr,br=h.current_basal*oe*gr/60,fr=Math.max(0,_.mealCOB-.25*_.carbs),pr=(ur-br)/csf-fr;br=o(br),pr=o(pr),console.error("naive_eventualBG:"+de+" bgUndershoot:"+ur+" zeroTempDuration:"+gr+" zeroTempEffect:"+br+" carbsReq:"+pr),pr>=h.carbsReqThreshold&&cr<=45&&(O.carbsReq=pr,O.reason+=pr+" add'l carbs req w/in "+cr+"m; ");var hr=0;if(k<ae&&a.iob<20*-h.current_basal/60&&E>0&&E>ge)O.reason+="IOB "+a.iob+" < "+o(20*-h.current_basal/60,2),O.reason+=" and minDelta "+n(E,h)+" > expectedDelta "+n(ge,h)+"; ";else if(k<ae||$e<ae)return O.reason+="minGuardBG "+n($e,h)+"<"+n(ae,h),hr=o(60*((ur=L-$e)/oe)/h.current_basal),hr=30*o(hr/30),hr=Math.min(120,Math.max(30,hr)),B.setTempBasal(0,hr,h,O,r);if(h.skip_neutral_temps&&O.deliverAt.getMinutes()>=55)return O.reason+="; Canceling temp at "+O.deliverAt.getMinutes()+"m past the hour. ",B.setTempBasal(0,0,h,O,r);var vr=0,_r=D;if(ce<N){if(O.reason+="Eventual BG "+n(ce,h)+" < "+n(N,h),E>ge&&E>0&&!pr)return de<40?(O.reason+=", naive_eventualBG < 40. ",B.setTempBasal(0,30,h,O,r)):(e.delta>E?O.reason+=", but Delta "+n(U,h)+" > expectedDelta "+n(ge,h):O.reason+=", but Min. Delta "+E.toFixed(2)+" > Exp. Delta "+n(ge,h),r.duration>15&&t(D,h)===t(r.rate,h)?(O.reason+=", temp "+r.rate+" ~ req "+D+"U/hr. ",O):(O.reason+="; setting current basal of "+D+" as temp. ",B.setTempBasal(D,30,h,O,r)));vr=o(vr=2*Math.min(0,(ce-L)/oe),2);var Br=Math.min(0,(de-L)/oe);if(Br=o(Br,2),E<0&&E>ge)vr=o(vr*(E/ge),2);if(_r=t(_r=D+2*vr,h),r.duration*(r.rate-D)/60<Math.min(vr,Br)-.3*D)return O.reason+=", "+r.duration+"m@"+r.rate.toFixed(2)+" is a lot less than needed. ",B.setTempBasal(_r,30,h,O,r);if(void 0!==r.rate&&r.duration>5&&_r>=.8*r.rate)return O.reason+=", temp "+r.rate+" ~< req "+_r+"U/hr. ",O;if(_r<=0){if((hr=o(60*((ur=L-de)/oe)/h.current_basal))<0?hr=0:(hr=30*o(hr/30),hr=Math.min(120,Math.max(0,hr))),hr>0)return O.reason+=", setting "+hr+"m zero temp. ",B.setTempBasal(_r,hr,h,O,r)}else O.reason+=", setting "+_r+"U/hr. ";return B.setTempBasal(_r,30,h,O,r)}if(E<ge&&(!M||!ve))return e.delta<E?O.reason+="Eventual BG "+n(ce,h)+" > "+n(N,h)+" but Delta "+n(U,h)+" < Exp. Delta "+n(ge,h):O.reason+="Eventual BG "+n(ce,h)+" > "+n(N,h)+" but Min. Delta "+E.toFixed(2)+" < Exp. Delta "+n(ge,h),r.duration>15&&t(D,h)===t(r.rate,h)?(O.reason+=", temp "+r.rate+" ~ req "+D+"U/hr. ",O):(O.reason+="; setting current basal of "+D+" as temp. ",B.setTempBasal(D,30,h,O,r));if(Math.min(ce,ke)<Z&&(!M||!ve))return O.reason+=n(ce,h)+"-"+n(ke,h)+" in range: no temp required",r.duration>15&&t(D,h)===t(r.rate,h)?(O.reason+=", temp "+r.rate+" ~ req "+D+"U/hr. ",O):(O.reason+="; setting current basal of "+D+" as temp. ",B.setTempBasal(D,30,h,O,r));if(ce>=Z&&(O.reason+="Eventual BG "+n(ce,h)+" >= "+n(Z,h)+", "),a.iob>$)return O.reason+="IOB "+o(a.iob,2)+" > max_iob "+$,r.duration>15&&t(D,h)===t(r.rate,h)?(O.reason+=", temp "+r.rate+" ~ req "+D+"U/hr. ",O):(O.reason+="; setting current basal of "+D+" as temp. ",B.setTempBasal(D,30,h,O,r));(vr=o((Math.min(ke,ce)-L)/oe,2))>$-a.iob&&(O.reason+="max_iob "+$+", ",vr=$-a.iob),_r=t(_r=D+2*vr,h),vr=o(vr,3),O.insulinReq=vr;var Mr=o((new Date(R).getTime()-a.lastBolusTime)/6e4,1);if(M&&ve&&k>ae){var xr=o(_.mealCOB/h.carb_ratio,3);if(h.use_autoisf)yr=h.smb_max_range_extension;else{console.error("autoISF disabled, SMB range extension disabled");var yr=1}yr>1&&console.error("SMB max range extended from default by factor "+yr);var Sr=0;void 0===h.maxSMBBasalMinutes?(Sr=o(yr*h.current_basal*30/60,1),console.error("profile.maxSMBBasalMinutes undefined: defaulting to 30m")):a.iob>xr&&a.iob>0?(console.error("IOB",a.iob,"> COB",_.mealCOB+"; mealInsulinReq =",xr),h.maxUAMSMBBasalMinutes?(console.error("profile.maxUAMSMBBasalMinutes:",h.maxUAMSMBBasalMinutes,"profile.current_basal:",h.current_basal),Sr=o(yr*h.current_basal*h.maxUAMSMBBasalMinutes/60,1)):(console.error("profile.maxUAMSMBBasalMinutes undefined: defaulting to 30m"),Sr=o(30*h.current_basal/60,1))):(console.error("profile.maxSMBBasalMinutes:",h.maxSMBBasalMinutes,"profile.current_basal:",h.current_basal),Sr=o(yr*h.current_basal*h.maxSMBBasalMinutes/60,1));var Cr=h.bolus_increment,Fr=1/Cr;if(h.use_autoisf)var Ir=p(h,k,L);else console.error("autoISF disabled, don't adjust SMB Delivery Ratio"),Ir=.5;Ir>.5&&console.error("SMB Delivery Ratio increased from default 0.5 to "+o(Ir,2));var wr=Math.min(vr*Ir,Sr);wr=Math.floor(wr*Fr)/Fr,hr=o(60*((L-(de+Le)/2)/oe)/h.current_basal),vr>0&&wr<Cr&&(hr=0);var Gr=0;hr<=0?hr=0:hr>=30?(hr=30*o(hr/30),hr=Math.min(60,Math.max(0,hr))):(Gr=o(D*hr/30,2),hr=30),O.reason+=" insulinReq "+vr,wr>=Sr&&(O.reason+="; maxBolus "+Sr),hr>0&&(O.reason+="; setting "+hr+"m low temp of "+Gr+"U/h"),O.reason+=". ";var Or=3;h.SMBInterval&&(Or=Math.min(10,Math.max(1,h.SMBInterval)));var Tr=o(Or-Mr,0),Ar=o(60*(Or-Mr),0)%60;if(console.error("naive_eventualBG",de+",",hr+"m "+Gr+"U/h temp needed; last bolus",Mr+"m ago; maxBolus: "+Sr),Mr>Or?wr>0&&(O.units=wr,O.reason+="Microbolusing "+wr+"U. "):O.reason+="Waiting "+Tr+"m "+Ar+"s to microbolus again. ",hr>0)return O.rate=Gr,O.duration=hr,O}var Dr=B.getMaxSafeBasal(h);return _r>Dr&&(O.reason+="adj. req. rate: "+_r+" to maxSafeBasal: "+Dr+", ",_r=t(Dr,h)),r.duration*(r.rate-D)/60>=2*vr?(O.reason+=r.duration+"m@"+r.rate.toFixed(2)+" > 2 * insulinReq. Setting temp basal of "+_r+"U/hr. ",B.setTempBasal(_r,30,h,O,r)):void 0===r.duration||0===r.duration?(O.reason+="no temp, setting "+_r+"U/hr. ",B.setTempBasal(_r,30,h,O,r)):r.duration>5&&t(_r,h)<=t(r.rate,h)?(O.reason+="temp "+r.rate+" >~ req "+_r+"U/hr. ",O):(O.reason+="temp "+r.rate+"<"+_r+"U/hr. ",B.setTempBasal(_r,30,h,O,r))}},6880:(e,r,a)=>{var t=a(6654);e.exports=function(e,r){var a=20;void 0!==r&&"string"==typeof r.model&&(t(r.model,"54")||t(r.model,"23"))&&(a=40);return e<1?Math.round(e*a)/a:e<10?Math.round(20*e)/20:Math.round(10*e)/10}},2705:(e,r,a)=>{var t=a(5639).Symbol;e.exports=t},9932:e=>{e.exports=function(e,r){for(var a=-1,t=null==e?0:e.length,o=Array(t);++a<t;)o[a]=r(e[a],a,e);return o}},9750:e=>{e.exports=function(e,r,a){return e==e&&(void 0!==a&&(e=e<=a?e:a),void 0!==r&&(e=e>=r?e:r)),e}},4239:(e,r,a)=>{var t=a(2705),o=a(9607),n=a(2333),i=t?t.toStringTag:void 0;e.exports=function(e){return null==e?void 0===e?"[object Undefined]":"[object Null]":i&&i in Object(e)?o(e):n(e)}},531:(e,r,a)=>{var t=a(2705),o=a(9932),n=a(1469),i=a(3448),s=t?t.prototype:void 0,l=s?s.toString:void 0;e.exports=function e(r){if("string"==typeof r)return r;if(n(r))return o(r,e)+"";if(i(r))return l?l.call(r):"";var a=r+"";return"0"==a&&1/r==-Infinity?"-0":a}},7561:(e,r,a)=>{var t=a(7990),o=/^\s+/;e.exports=function(e){return e?e.slice(0,t(e)+1).replace(o,""):e}},1957:(e,r,a)=>{var t="object"==typeof a.g&&a.g&&a.g.Object===Object&&a.g;e.exports=t},9607:(e,r,a)=>{var t=a(2705),o=Object.prototype,n=o.hasOwnProperty,i=o.toString,s=t?t.toStringTag:void 0;e.exports=function(e){var r=n.call(e,s),a=e[s];try{e[s]=void 0;var t=!0}catch(e){}var o=i.call(e);return t&&(r?e[s]=a:delete e[s]),o}},2333:e=>{var r=Object.prototype.toString;e.exports=function(e){return r.call(e)}},5639:(e,r,a)=>{var t=a(1957),o="object"==typeof self&&self&&self.Object===Object&&self,n=t||o||Function("return this")();e.exports=n},7990:e=>{var r=/\s/;e.exports=function(e){for(var a=e.length;a--&&r.test(e.charAt(a)););return a}},6654:(e,r,a)=>{var t=a(9750),o=a(531),n=a(554),i=a(9833);e.exports=function(e,r,a){e=i(e),r=o(r);var s=e.length,l=a=void 0===a?s:t(n(a),0,s);return(a-=r.length)>=0&&e.slice(a,l)==r}},1469:e=>{var r=Array.isArray;e.exports=r},3218:e=>{e.exports=function(e){var r=typeof e;return null!=e&&("object"==r||"function"==r)}},7005:e=>{e.exports=function(e){return null!=e&&"object"==typeof e}},3448:(e,r,a)=>{var t=a(4239),o=a(7005);e.exports=function(e){return"symbol"==typeof e||o(e)&&"[object Symbol]"==t(e)}},8601:(e,r,a)=>{var t=a(4841),o=1/0;e.exports=function(e){return e?(e=t(e))===o||e===-1/0?17976931348623157e292*(e<0?-1:1):e==e?e:0:0===e?e:0}},554:(e,r,a)=>{var t=a(8601);e.exports=function(e){var r=t(e),a=r%1;return r==r?a?r-a:r:0}},4841:(e,r,a)=>{var t=a(7561),o=a(3218),n=a(3448),i=/^[-+]0x[0-9a-f]+$/i,s=/^0b[01]+$/i,l=/^0o[0-7]+$/i,m=parseInt;e.exports=function(e){if("number"==typeof e)return e;if(n(e))return NaN;if(o(e)){var r="function"==typeof e.valueOf?e.valueOf():e;e=o(r)?r+"":r}if("string"!=typeof e)return 0===e?e:+e;e=t(e);var a=s.test(e);return a||l.test(e)?m(e.slice(2),a?2:8):i.test(e)?NaN:+e}},9833:(e,r,a)=>{var t=a(531);e.exports=function(e){return null==e?"":t(e)}}},r={};function a(t){var o=r[t];if(void 0!==o)return o.exports;var n=r[t]={exports:{}};return e[t](n,n.exports,a),n.exports}a.g=function(){if("object"==typeof globalThis)return globalThis;try{return this||new Function("return this")()}catch(e){if("object"==typeof window)return window}}();var t=a(5546);freeaps_determineBasal=t})();