| 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="autoISF ParabolicFit: ";function m(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,d=1,u=1,c=n;if(n>e)m=(d=i)+((l=t[1])-d)/((s=a[1])-(u=n))*(e-u);else if(s<e)m=(d=i=t[o-1])+(l-d)/(s-(u=n=a[o-1]))*(e-u);else for(var g=0;g<=o;g++){if(i=t[g],(n=a[g])==e){m=i;break}if(n>e){m=d+(i-d)/(n-(u=c))*(e-u);break}d=i,c=n}return m*=e>100?r.higher_ISFrange_weight:e>40?r.lower_ISFrange_weight:r.delta_ISFrange_weight}e.exports=function(e,r,a,d,u,c,g,p,b,v){var h={},f=new Date;if(v&&(f=v),void 0===d||void 0===d.current_basal)return h.error="Error: could not get current basal rate",h;var B=t(d.current_basal,d),_=B,M=new Date;v&&(M=v);var x,y=new Date(e.date),S=o((M-y)/60/1e3,1),C=e.glucose,G=e.noise;x=e.delta>-.5?"+"+o(e.delta,0):o(e.delta,0);var w=Math.min(e.delta,e.short_avgdelta),O=Math.min(e.short_avgdelta,e.long_avgdelta),F=Math.max(e.delta,e.short_avgdelta,e.long_avgdelta);(C<=10||38===C||G>=3)&&(h.reason="CGM is calibrating, in ??? state, or noise is high");if(C>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(C,d)+"+"+n(e.delta,d)+") for 5m w/ "+n(e.short_avgdelta,d)+" mg/dL ~15m change & "+n(e.long_avgdelta,2)+" mg/dL ~45m change"),console.error("Simulator mode detected (",e.device,"): continuing anyway")):!0),S>12||S<-5?h.reason="If current system time "+M+" is correct, then BG data is too old. The last BG data was read "+S+"m ago at "+y:0===e.short_avgdelta&&0===e.long_avgdelta&&(e.last_cal&&e.last_cal<3?h.reason="CGM was just calibrated":h.reason="CGM data is unchanged ("+n(C,d)+"+"+n(e.delta,d)+") for 5m w/ "+n(e.short_avgdelta,d)+" mg/dL ~15m change & "+n(e.long_avgdelta,d)+" mg/dL ~45m change"),C<=10||38===C||G>=3||S>12||S<-5||0===e.short_avgdelta&&0===e.long_avgdelta)return r.rate>=_?(h.reason+=". Canceling high temp basal of "+r.rate,h.deliverAt=f,h.temp="absolute",h.duration=0,h.rate=0,h):0===r.rate&&r.duration>30?(h.reason+=". Shortening "+r.duration+"m long zero temp to 30m. ",h.deliverAt=f,h.temp="absolute",h.duration=30,h.rate=0,h):(h.reason+=". Temp "+r.rate+" <= current basal "+_+"U/hr; doing nothing. ",h);var I,T,A,D=d.max_iob;if(void 0!==d.min_bg&&(T=d.min_bg),void 0!==d.max_bg&&(A=d.max_bg),void 0===d.min_bg||void 0===d.max_bg)return h.error="Error: could not determine target_bg. ",h;I=(d.min_bg+d.max_bg)/2;var U=d.exercise_mode||d.high_temptarget_raises_sensitivity,R=100;if(d.half_basal_exercise_target)var j=d.half_basal_exercise_target;else j=160;if(U&&d.temptargetSet&&I>R||d.low_temptarget_lowers_sensitivity&&d.temptargetSet&&I<R){var q=j-R;q+I-R>0?(sensitivityRatio=q/(q+I-R),sensitivityRatio=Math.min(sensitivityRatio,d.autosens_max),sensitivityRatio=o(sensitivityRatio,2)):sensitivityRatio=d.autosens_max,process.stderr.write("Sensitivity ratio set to "+sensitivityRatio+" based on temp target of "+I+"; ")}else void 0!==u&&u&&(sensitivityRatio=u.ratio,process.stderr.write("Autosens ratio: "+sensitivityRatio+"; "));if(sensitivityRatio&&(_=d.current_basal*sensitivityRatio,(_=t(_,d))!==B?process.stderr.write("Adjusting basal from "+B+" to "+_+"; "):process.stderr.write("Basal unchanged: "+_+"; ")),d.temptargetSet);else if(void 0!==u&&u&&(d.sensitivity_raises_target&&u.ratio<1||d.resistance_lowers_target&&u.ratio>1)){T=o((T-60)/u.ratio)+60,A=o((A-60)/u.ratio)+60;var E=o((I-60)/u.ratio)+60;I===(E=Math.max(80,E))?process.stderr.write("target_bg unchanged: "+E+"; "):process.stderr.write("target_bg from "+I+" to "+E+"; "),I=E}if(e.noise>=2){var W=Math.max(1.1,d.noisyCGMTargetMultiplier),P=(Math.min(250,d.maxRaw),o(Math.min(200,T*W))),z=o(Math.min(200,I*W)),L=o(Math.min(200,A*W));process.stderr.write("Raising target_bg for noisy / raw CGM data, from "+I+" to "+z+"; "),T=P,I=z,A=L}var k=T-.5*(T-40),N=o(d.sens,1),Z=d.sens;if(void 0!==u&&u&&((Z=o(Z=d.sens/sensitivityRatio,1))!==N?process.stderr.write("ISF from "+n(N,d)+" to "+n(Z,d)):process.stderr.write("ISF unchanged: "+n(Z,d)),i+="Autosense: Ratio "+sensitivityRatio+", ISF "+n(N,d)+" -> "+n(Z,d)+"; "),console.error("CR:"+d.carb_ratio),Z=function(e,r,a,t,s,d,u,c){if(!a.use_autoisf)return console.error("autoISF disabled in Preferences"),e;var g=t.autoISF_duration,p=t.autoISF_average,b=t.dura_p,v=t.delta_pl,h=t.delta_pn,f=t.r_squ,B=t.bg_acceleration,_=t.pp_debug;l+="BG-accel: "+n(B,a)+", PF-minutes: "+b+", PF-corr: "+n(f,a)+", PF-nextDelta: "+n(h,a)+", PF-lastDelta: "+n(v,a)+", regular Delta: "+n(t.delta,a),console.error(_+l),i+="BG Parabolic Fit: nextDelta "+n(h,a)+", lastDelta "+n(v,a)+", regularDelta "+n(t.delta,a)+"; ";var M=a.autoisf_max,x=!1;if(s.mealCOB>0&&!a.enableautoisf_with_COB)console.error("BG dependant autoISF by-passed; preferences disabled mealCOB of "+o(s.mealCOB,1));else{var y=1,S=1,C=r+10-p,G=1+m(100-C,a);if(console.error("bg_ISF adaptation is "+n(G,a)),M<G&&console.error("bg_ISF adaptation "+n(G,a)+" limited by autoisf_max "+M),G<1)return Math.min(720,o(a.sens/G,1));G>1&&(x=!0);var w=t.delta;C>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>=(d-s.lastCarbTime)/1e3/3600?1!=(y=1+Math.max(0,w*a.postmeal_ISF_weight))&&(x=!0,M<y?console.error("pp_ISF adaptation"+n(y,a)+"limited by autoisf_max",M):console.error("pp_ISF adaptation is"+n(y,a))):(S=m(w,a),C>-20&&(S*=.5),1!=(S=1+S)&&(x=!0,M<S?console.error("delta_ISF adaptation "+n(S,a)+" limited by autoisf_max ",M):console.error("delta_ISF adaptation is "+n(S,a))))}var O=1,F=a.autoisf_hourlychange;s.mealCOB>0&&!a.enableautoisf_with_COB?console.error("autoISF by-passed; preferences disabled mealCOB of "+o(s.mealCOB,1)):g<10?console.error("autoISF by-passed; BG is only "+g+"m at level "+p):p<=r?console.error("autoISF by-passed; avg. glucose "+p+" below target "+n(r,a)):(O+=g/60*(F/r)*(p-r),x=!0,console.error("autoISF reports ISF "+n(e,a)+" did not do it for "+g+"m; go more aggressive by "+o(O,2)),M<O&&console.error("autoISF adaptation "+o(O,2)+" limited by autoisf_max "+M));if(x){var I=Math.max(Math.min(M,Math.max(O,G,S,y)),c);e=o(a.sens/I,1),i+="autoISF -> "+n(e,a)+", Ratio "+o(I,2)+"; "}return e}(Z,I,d,e,c,v,0,sensitivityRatio),void 0===a)return h.error="Error: iob_data undefined. ",h;var $,H=a;if(a.length,a.length>1&&(a=H[0]),void 0===a.activity||void 0===a.iob)return h.error="Error: iob_data missing some property. ",h;var J=(($=void 0!==a.lastTemp?o((new Date(M).getTime()-a.lastTemp.date)/6e4):0)+r.duration)%30;if(console.error("currenttemp:",r,"lastTempAge:",$,"m","tempModulus:",J,"m"),h.temp="absolute",h.deliverAt=f,p&&r&&a.lastTemp&&r.rate!==a.lastTemp.rate&&$>10&&r.duration)return h.reason="Warning: currenttemp rate "+r.rate+" != lastTemp rate "+a.lastTemp.rate+" from pumphistory; canceling temp",g.setTempBasal(0,0,d,h,r);if(r&&a.lastTemp&&r.duration>0){var K=$-a.lastTemp.duration;if(K>5&&$>10)return h.reason="Warning: currenttemp running but lastTemp from pumphistory ended "+K+"m ago; canceling temp",g.setTempBasal(0,0,d,h,r)}var Q=o(-a.activity*Z*5,2),V=o(6*(w-Q));if(V<0&&(V=o(6*(O-Q)))<0&&(V=o(6*(e.long_avgdelta-Q))),a.iob>0)var X=o(C-a.iob*Z);else X=o(C-a.iob*Math.min(Z,d.sens));var Y=X+V;if(void 0===Y||isNaN(Y))return h.error="Error: could not calculate eventualBG. ",h;var ee=function(e,r,a){return o(a+(e-r)/24,1)}(I,Y,Q);h={temp:"absolute",bg:C,tick:x,eventualBG:Y,insulinReq:0,reservoir:b,deliverAt:f,sensitivityRatio};var re=[],ae=[],te=[],oe=[];re.push(C),ae.push(C),oe.push(C),te.push(C);var ne=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)}(d,p,c,I),ie=d.enableUAM,se=0,le=0;se=o(w-Q,1);var me=o(w-Q,1);csf=Z/d.carb_ratio,console.error("profile.sens:"+n(d.sens,d)+", sens:"+n(Z,d)+", CSF:"+o(csf,1));var de=o(30*csf*5/60,1);se>de&&(console.error("Limiting carb impact from",se,"to",de,"mg/dL/5m (",30,"g/h )"),se=de);var ue=3;sensitivityRatio&&(ue/=sensitivityRatio);var ce=ue;if(c.carbs){ue=Math.max(ue,c.mealCOB/20);var ge=o((new Date(M).getTime()-c.lastCarbTime)/6e4),pe=(c.carbs-c.mealCOB)/c.carbs;ce=o(ce=ue+1.5*ge/60,1),console.error("Last carbs",ge,"minutes ago; remainingCATime:",ce,"hours;",o(100*pe)+"% carbs absorbed")}var be=Math.max(0,se/5*60*ce/2)/csf,ve=90,he=1;d.remainingCarbsCap&&(ve=Math.min(90,d.remainingCarbsCap)),d.remainingCarbsFraction&&(he=Math.min(1,d.remainingCarbsFraction));var fe=1-he,Be=Math.max(0,c.mealCOB-be-c.carbs*fe),_e=(Be=Math.min(ve,Be))*csf*5/60/(ce/2),Me=o(c.slopeFromMaxDeviation,2),xe=o(c.slopeFromMinDeviation,2),ye=Math.min(Me,-xe/3),Se=0;0===se?le=0:!0===d.floating_carbs?(le=Math.min(60*ce/5/2,Math.max(0,c.carbs*csf/se)),Se=Math.min(60*ce/5/2,Math.max(0,c.mealCOB*csf/se)),c.carbs>0&&(i+="Floating Carbs CID:"+o(le,1)+" / MealCarbs:"+o(c.carbs,1)+" vs. Not Floating:"+o(Se,1)+" / MealCOB:"+o(c.mealCOB,1)+"; ",console.error("Floating Carbs CID:"+o(le,1)+" / MealCarbs:"+o(c.carbs,1)+" vs. Not Floating:"+o(Se,1)+" / MealCOB:"+o(c.mealCOB,1)))):le=Math.min(60*ce/5/2,Math.max(0,c.mealCOB*csf/se)),console.error("Carb Impact:",se,"mg/dL per 5m; CI Duration:",o(5*le/60*2,1),"hours; remaining CI (",ce/2,"h peak):",o(_e,1),"mg/dL per 5m");var Ce,Ge,we,Oe,Fe,Ie=999,Te=999,Ae=999,De=C,Ue=999,Re=999,je=999,qe=999,Ee=Y,We=C,Pe=C,ze=0,Le=[],ke=[];try{H.forEach((function(e){var r=o(-e.activity*Z*5,2),a=o(-e.iobWithZeroTemp.activity*Z*5,2),t=se*(1-Math.min(1,ae.length/12));Ee=ae[ae.length-1]+r+t;var n=oe[oe.length-1]+a,i=Math.max(0,Math.max(0,se)*(1-re.length/Math.max(2*le,1))),s=Math.min(re.length,12*ce-re.length),l=Math.max(0,s/(ce/2*12)*_e);i+l,Le.push(o(l,0)),ke.push(o(i,0)),COBpredBG=re[re.length-1]+r+Math.min(0,t)+i+l;var m=Math.max(0,me+te.length*ye),d=Math.max(0,me*(1-te.length/Math.max(36,1))),u=Math.min(m,d);u>0&&(ze=o(5*(te.length+1)/60,1)),UAMpredBG=te[te.length-1]+r+Math.min(0,t)+u,ae.length<48&&ae.push(Ee),re.length<48&&re.push(COBpredBG),te.length<48&&te.push(UAMpredBG),oe.length<48&&oe.push(n),COBpredBG<Ue&&(Ue=o(COBpredBG)),UAMpredBG<Re&&(Re=o(UAMpredBG)),Ee<je&&(je=o(Ee)),n<qe&&(qe=o(n));ae.length>18&&Ee<Ie&&(Ie=o(Ee)),Ee>We&&(We=Ee),(le||_e>0)&&re.length>18&&COBpredBG<Te&&(Te=o(COBpredBG)),(le||_e>0)&&COBpredBG>We&&(Pe=COBpredBG),ie&&te.length>12&&UAMpredBG<Ae&&(Ae=o(UAMpredBG)),ie&&UAMpredBG>We&&UAMpredBG}))}catch(e){console.error("Problem with iobArray. Optional feature Advanced Meal Assist disabled")}c.mealCOB&&(console.error("predCIs (mg/dL/5m):",ke.join(" ")),console.error("remainingCIs: ",Le.join(" "))),h.predBGs={},ae.forEach((function(e,r,a){a[r]=o(Math.min(401,Math.max(39,e)))}));for(var Ne=ae.length-1;Ne>12&&ae[Ne-1]===ae[Ne];Ne--)ae.pop();for(h.predBGs.IOB=ae,we=o(ae[ae.length-1]),oe.forEach((function(e,r,a){a[r]=o(Math.min(401,Math.max(39,e)))})),Ne=oe.length-1;Ne>6&&!(oe[Ne-1]>=oe[Ne]||oe[Ne]<=I);Ne--)oe.pop();if(h.predBGs.ZT=oe,o(oe[oe.length-1]),c.mealCOB>0&&(se>0||_e>0)){for(re.forEach((function(e,r,a){a[r]=o(Math.min(401,Math.max(39,e)))})),Ne=re.length-1;Ne>12&&re[Ne-1]===re[Ne];Ne--)re.pop();h.predBGs.COB=re,Oe=o(re[re.length-1]),Y=Math.max(Y,o(re[re.length-1]))}if(se>0||_e>0){if(ie){for(te.forEach((function(e,r,a){a[r]=o(Math.min(401,Math.max(39,e)))})),Ne=te.length-1;Ne>12&&te[Ne-1]===te[Ne];Ne--)te.pop();h.predBGs.UAM=te,Fe=o(te[te.length-1]),te[te.length-1]&&(Y=Math.max(Y,o(te[te.length-1])))}h.eventualBG=Y}console.error("UAM Impact:",me,"mg/dL per 5m; UAM Duration:",ze,"hours"),Ie=Math.max(39,Ie),Te=Math.max(39,Te),Ae=Math.max(39,Ae),Ce=o(Ie);var Ze=c.mealCOB/c.carbs;Ge=o(Ae<999&&Te<999?(1-Ze)*UAMpredBG+Ze*COBpredBG:Te<999?(Ee+COBpredBG)/2:Ae<999?(Ee+UAMpredBG)/2:Ee),qe>Ge&&(Ge=qe),De=o(De=le||_e>0?ie?Ze*Ue+(1-Ze)*Re:Ue:ie?Re:je);var $e=Ae;if(qe<k)$e=(Ae+qe)/2;else if(qe<I){var He=(qe-k)/(I-k);$e=(Ae+(Ae*He+qe*(1-He)))/2}else qe>Ae&&($e=(Ae+qe)/2);if($e=o($e),c.carbs)if(!ie&&Te<999)Ce=o(Math.max(Ie,Te));else if(Te<999){var Je=Ze*Te+(1-Ze)*$e;Ce=o(Math.max(Ie,Te,Je))}else Ce=ie?$e:De;else ie&&(Ce=o(Math.max(Ie,$e)));Ce=Math.min(Ce,Ge),process.stderr.write("minPredBG: "+Ce+" minIOBPredBG: "+Ie+" minZTGuardBG: "+qe),Te<999&&process.stderr.write(" minCOBPredBG: "+Te),Ae<999&&process.stderr.write(" minUAMPredBG: "+Ae),console.error(" avgPredBG:",Ge,"COB:",c.mealCOB,"/",c.carbs),Pe>C&&(Ce=Math.min(Ce,Pe)),h.COB=c.mealCOB,h.IOB=a.iob,h.BGI=Q,h.deviation=n(V,d),h.ISF=n(Z,d),h.CR=o(d.carb_ratio,2),h.target_bg=n(I,d),h.reason=i+"minPredBG "+n(Ce,d)+", minGuardBG "+n(De,d)+", IOBpredBG "+n(we,d),Oe>0&&(h.reason+=", COBpredBG "+n(Oe,d)),Fe>0&&(h.reason+=", UAMpredBG "+n(Fe,d)),h.reason+="; ";var Ke=X;Ke<40&&(Ke=Math.min(De,Ke));var Qe,Ve=k-Ke,Xe=240,Ye=240;if(c.mealCOB>0&&(se>0||_e>0)){for(Ne=0;Ne<re.length;Ne++)if(re[Ne]<T){Xe=5*Ne;break}for(Ne=0;Ne<re.length;Ne++)if(re[Ne]<k){Ye=5*Ne;break}}else{for(Ne=0;Ne<ae.length;Ne++)if(ae[Ne]<T){Xe=5*Ne;break}for(Ne=0;Ne<ae.length;Ne++)if(ae[Ne]<k){Ye=5*Ne;break}}ne&&De<k&&(console.error("minGuardBG",n(De,d),"projected below",n(k,d),"- disabling SMB"),ne=!1),void 0===d.maxDelta_bg_threshold&&(Qe=.2),void 0!==d.maxDelta_bg_threshold&&(Qe=Math.min(d.maxDelta_bg_threshold,.3)),F>Qe*C&&(console.error("maxDelta "+n(F,d)+" > "+100*Qe+"% of BG "+n(C,d)+" - disabling SMB"),h.reason+="maxDelta "+n(F,d)+" > "+100*Qe+"% of BG "+n(C,d)+": SMB disabled; ",ne=!1),console.error("BG projected to remain above",n(T,d),"for",Xe,"minutes"),(Ye<240||Xe<60)&&console.error("BG projected to remain above",n(k,d),"for",Ye,"minutes");var er=Ye,rr=d.current_basal*Z*er/60,ar=Math.max(0,c.mealCOB-.25*c.carbs),tr=(Ve-rr)/csf-ar;if(rr=o(rr),tr=o(tr),console.error("naive_eventualBG:",X,"bgUndershoot:",Ve,"zeroTempDuration:",er,"zeroTempEffect:",rr,"carbsReq:",tr),tr>=d.carbsReqThreshold&&Ye<=45&&(h.carbsReq=tr,h.reason+=tr+" add'l carbs req w/in "+Ye+"m; "),C<k&&a.iob<20*-d.current_basal/60&&w>0&&w>ee)h.reason+="IOB "+a.iob+" < "+o(20*-d.current_basal/60,2),h.reason+=" and minDelta "+n(w,d)+" > expectedDelta "+n(ee,d)+"; ";else if(C<k||De<k){h.reason+="minGuardBG "+n(De,d)+"<"+n(k,d);var or=(Ve=I-De)/Z,nr=o(60*or/d.current_basal);return nr=30*o(nr/30),nr=Math.min(120,Math.max(30,nr)),g.setTempBasal(0,nr,d,h,r)}if(d.skip_neutral_temps&&h.deliverAt.getMinutes()>=55)return h.reason+="; Canceling temp at "+h.deliverAt.getMinutes()+"m past the hour. ",g.setTempBasal(0,0,d,h,r);if(Y<T){if(h.reason+="Eventual BG "+n(Y,d)+" < "+n(T,d),w>ee&&w>0&&!tr)return X<40?(h.reason+=", naive_eventualBG < 40. ",g.setTempBasal(0,30,d,h,r)):(e.delta>w?h.reason+=", but Delta "+n(x,d)+" > expectedDelta "+n(ee,d):h.reason+=", but Min. Delta "+w.toFixed(2)+" > Exp. Delta "+n(ee,d),r.duration>15&&t(_,d)===t(r.rate,d)?(h.reason+=", temp "+r.rate+" ~ req "+_+"U/hr. ",h):(h.reason+="; setting current basal of "+_+" as temp. ",g.setTempBasal(_,30,d,h,r)));var ir=2*Math.min(0,(Y-I)/Z);ir=o(ir,2);var sr=Math.min(0,(X-I)/Z);if(sr=o(sr,2),w<0&&w>ee)ir=o(ir*(w/ee),2);var lr=_+2*ir;lr=t(lr,d);var mr=r.duration*(r.rate-_)/60;if(mr<Math.min(ir,sr)-.3*_)return h.reason+=", "+r.duration+"m@"+r.rate.toFixed(2)+" is a lot less than needed. ",g.setTempBasal(lr,30,d,h,r);if(void 0!==r.rate&&r.duration>5&&lr>=.8*r.rate)return h.reason+=", temp "+r.rate+" ~< req "+lr+"U/hr. ",h;if(lr<=0){if((nr=o(60*(or=(Ve=I-X)/Z)/d.current_basal))<0?nr=0:(nr=30*o(nr/30),nr=Math.min(120,Math.max(0,nr))),nr>0)return h.reason+=", setting "+nr+"m zero temp. ",g.setTempBasal(lr,nr,d,h,r)}else h.reason+=", setting "+lr+"U/hr. ";return g.setTempBasal(lr,30,d,h,r)}if(w<ee&&(!p||!ne))return e.delta<w?h.reason+="Eventual BG "+n(Y,d)+" > "+n(T,d)+" but Delta "+n(x,d)+" < Exp. Delta "+n(ee,d):h.reason+="Eventual BG "+n(Y,d)+" > "+n(T,d)+" but Min. Delta "+w.toFixed(2)+" < Exp. Delta "+n(ee,d),r.duration>15&&t(_,d)===t(r.rate,d)?(h.reason+=", temp "+r.rate+" ~ req "+_+"U/hr. ",h):(h.reason+="; setting current basal of "+_+" as temp. ",g.setTempBasal(_,30,d,h,r));if(Math.min(Y,Ce)<A&&(!p||!ne))return h.reason+=n(Y,d)+"-"+n(Ce,d)+" in range: no temp required",r.duration>15&&t(_,d)===t(r.rate,d)?(h.reason+=", temp "+r.rate+" ~ req "+_+"U/hr. ",h):(h.reason+="; setting current basal of "+_+" as temp. ",g.setTempBasal(_,30,d,h,r));if(Y>=A&&(h.reason+="Eventual BG "+n(Y,d)+" >= "+n(A,d)+", "),a.iob>D)return h.reason+="IOB "+o(a.iob,2)+" > max_iob "+D,r.duration>15&&t(_,d)===t(r.rate,d)?(h.reason+=", temp "+r.rate+" ~ req "+_+"U/hr. ",h):(h.reason+="; setting current basal of "+_+" as temp. ",g.setTempBasal(_,30,d,h,r));(ir=o((Math.min(Ce,Y)-I)/Z,2))>D-a.iob&&(h.reason+="max_iob "+D+", ",ir=D-a.iob),lr=t(lr=_+2*ir,d),ir=o(ir,3),h.insulinReq=ir;var dr=o((new Date(M).getTime()-a.lastBolusTime)/6e4,1);if(p&&ne&&C>k){var ur=o(c.mealCOB/d.carb_ratio,3);if(d.use_autoisf)cr=d.smb_max_range_extension;else{console.error("autoISF disabled, SMB range extension disabled");var cr=1}if(cr>1&&console.error("SMB max range extended from default by factor "+cr),void 0===d.maxSMBBasalMinutes){var gr=o(cr*d.current_basal*30/60,1);console.error("profile.maxSMBBasalMinutes undefined: defaulting to 30m")}else a.iob>ur&&a.iob>0?(console.error("IOB",a.iob,"> COB",c.mealCOB+"; mealInsulinReq =",ur),d.maxUAMSMBBasalMinutes?(console.error("profile.maxUAMSMBBasalMinutes:",d.maxUAMSMBBasalMinutes,"profile.current_basal:",d.current_basal),gr=o(cr*d.current_basal*d.maxUAMSMBBasalMinutes/60,1)):(console.error("profile.maxUAMSMBBasalMinutes undefined: defaulting to 30m"),gr=o(30*d.current_basal/60,1))):(console.error("profile.maxSMBBasalMinutes:",d.maxSMBBasalMinutes,"profile.current_basal:",d.current_basal),gr=o(cr*d.current_basal*d.maxSMBBasalMinutes/60,1));var pr=d.bolus_increment,br=1/pr;if(d.use_autoisf){var vr=function(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}(d,C,I);s="SMB Delivery Ratio "+o(vr,2)+", ",h.reason+=s}else console.error("autoISF disabled, don't adjust SMB Delivery Ratio"),vr=.5;vr>.5&&console.error("SMB Delivery Ratio increased from default 0.5 to "+o(vr,2));var hr=Math.min(ir*vr,gr);hr=Math.floor(hr*br)/br,nr=o(60*(or=(I-(X+Ie)/2)/Z)/d.current_basal),ir>0&&hr<pr&&(nr=0);var fr=0;nr<=0?nr=0:nr>=30?(nr=30*o(nr/30),nr=Math.min(60,Math.max(0,nr))):(fr=o(_*nr/30,2),nr=30),h.reason+=" insulinReq "+ir,hr>=gr&&(h.reason+="; maxBolus "+gr),nr>0&&(h.reason+="; setting "+nr+"m low temp of "+fr+"U/h"),h.reason+=". ";var Br=3;d.SMBInterval&&(Br=Math.min(10,Math.max(1,d.SMBInterval)));var _r=o(Br-dr,0),Mr=o(60*(Br-dr),0)%60;if(console.error("naive_eventualBG",X+",",nr+"m "+fr+"U/h temp needed; last bolus",dr+"m ago; maxBolus: "+gr),dr>Br?hr>0&&(h.units=hr,h.reason+="Microbolusing "+hr+"U. "):h.reason+="Waiting "+_r+"m "+Mr+"s to microbolus again. ",nr>0)return h.rate=fr,h.duration=nr,h}var xr=g.getMaxSafeBasal(d);return lr>xr&&(h.reason+="adj. req. rate: "+lr+" to maxSafeBasal: "+xr+", ",lr=t(xr,d)),(mr=r.duration*(r.rate-_)/60)>=2*ir?(h.reason+=r.duration+"m@"+r.rate.toFixed(2)+" > 2 * insulinReq. Setting temp basal of "+lr+"U/hr. ",g.setTempBasal(lr,30,d,h,r)):void 0===r.duration||0===r.duration?(h.reason+="no temp, setting "+lr+"U/hr. ",g.setTempBasal(lr,30,d,h,r)):r.duration>5&&t(lr,d)<=t(r.rate,d)?(h.reason+="temp "+r.rate+" >~ req "+lr+"U/hr. ",h):(h.reason+="temp "+r.rate+"<"+lr+"U/hr. ",g.setTempBasal(lr,30,d,h,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})();
|