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