| 1 |
- var freeaps_determineBasal;(()=>{var e={5546:(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(.0555*e,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 limitLoged 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 limitLoged 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,F,I){var D,G,O,T=0,U="",R="",A=0,P=(F=0,0),j=0,k=0,L=0;function q(e,a){var r=e.getTime();return new Date(r+36e5*a)}function W(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 E(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 N(e,a){var r=0,t=a,o=(e-a)/36e5,i=0,n=o,s=0;do{if(o>0){var l=E(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+=W((m=C[e].rate)*i),o-=i,console.log("Dynamic ratios log: scheduled insulin added: "+W(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+=W((m=C[e].rate)*i),o-=i,console.log("Dynamic ratios log: scheduled insulin added: "+W(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+=W((m=C[e].rate)*i),o-=i,console.log("Dynamic ratios log: scheduled insulin added: "+W(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 Z=S.length-1;if(Z>=0)var $=new Date(S[Z].timestamp);else $=new Date;var H,J,K=new Date(S[0].timestamp);("TempBasalDuration"==S[0]._type&&(K=new Date),(T=(K-$)/36e5)<23.5)?(k=N($,(H=24-T,J=$.getTime(),new Date(J-36e5*H))),U="24 hours of data is required for an accurate tdd calculation. Currently only "+T.toPrecision(3)+" hours of pump history data are available. Using your pump scheduled basals to fill in the missing hours. Scheduled basals added: "+k.toPrecision(5)+" U. "):U="";for(let e=0;e<S.length;e++)"Bolus"==S[e]._type&&(j+=S[e].amount);for(let e=1;e<S.length;e++)if("TempBasal"==S[e]._type&&S[e].rate>0){A=e,L=S[e].rate;var Q=S[e-1]["duration (min)"]/60,V=Q,X=new Date(S[e-1].timestamp),Y=X;do{if(e--,0==e){Y=new Date;break}if("TempBasal"==S[e]._type||"PumpSuspend"==S[e]._type){Y=new Date(S[e].timestamp);break}}while(e>0);var ee=(Y-X)/36e5;ee<V&&(Q=ee),P+=W(L*Q),e=A}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>0&&(--t,"TempBasal"==S[t]._type)){r=new Date(S[t].timestamp);break}}while(t>0);(r-a)/36e5>0&&(k+=N(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 ae=r;let t=e;do{if(--t,t>=0&&("TempBasal"==S[t]._type||"PumpSuspend"==S[t]._type)){ae=new Date(S[t].timestamp);break}}while(t>0);if(0==e&&"TempBasalDuration"==S[0]._type&&(ae=new Date,a=S[e]["duration (min)"]/60),(ae-r)/36e5-a>0){k+=N(ae,q(r,a))}}var re=F=j+P+k;G=". Bolus insulin: "+j.toPrecision(5)+" U",O=". Temporary basal insulin: "+P.toPrecision(5)+" U",D=". Insulin with scheduled basal rate: "+k.toPrecision(5)+" U",R=U+(". tdd past 24h is: "+F.toPrecision(5)+" U")+G+O+D,tddReason=", TDD: "+o(F,2)+" U";const te=e.glucose;var oe=w.enableChris,ie=w.enableDynamicCR;const ne=b.autosens_min,se=b.autosens_max,le=w.adjustmentFactor,me=b.min_bg;var ue=!1,de="";const ce=I.weightedAverage,ge=b.weightPercentage;1!=b.high_temptarget_raises_sensitivity&&1!=b.exercise_mode||(ue=!0),1==b.use_autoisf&&1==oe&&(b.use_autoisf=!1),me>=118&&1==ue&&(b.use_autoisf=!1,oe=!1,de="Dynamic ISF temporarily off due to a high temp target/exercising. Current min target: "+me);var fe=", Dynamic ratios log: ",pe=", AF: "+le,he="BG: "+te+" mg/dl ("+(.0555*te).toPrecision(2)+" mmol/l). ",be="",ve="";const _e=w.curve,Be=w.insulinPeakTime,Me=w.useCustomPeakTime;var ye=55;switch(_e){case"rapid-acting":ye=55;break;case"ultra-rapid":ye=Be<75&&1==Me?120-Be:70}if(ge<1&&(re=F,F=ce,console.log("Using weighted TDD average: "+F.toPrecision(4)+" U, instead of past 24 h ("+re.toPrecision(2)+" U), weight: "+ge),ve=", Weighted TDD: "+F.toPrecision(4)+" U"),1==w.useNewFormula){var xe=b.sens*le*F*Math.log(te/ye+1)/1800;be=", Logarithmic formula. InsulinFactor: "+ye}else{xe=b.sens*le*F*te/277700;be=", Original formula"}var Se=b.carb_ratio,we="",Ce="";if(1==oe&&F>0){if(we=", Dynamic ISF/CR: On/",xe>se?(de=", Dynamic ISF hit limitLog by autosens_max setting: "+se+" ("+xe.toPrecision(3)+"), ",Ce=", Autosens/Dynamic Limit: "+se+" ("+xe.toPrecision(3)+")",xe=se):xe<ne&&(de=", Dynamic ISF hit limitLog by autosens_min setting: "+ne+" ("+xe.toPrecision(3)+"). ",Ce=", Autosens/Dynamic Limit: "+ne+" ("+xe.toPrecision(3)+")",xe=ne),1==ie){we+="On";var Fe=" CR: "+(Se=o(Se/xe,2))+" g/U";b.carb_ratio=Se}else Fe=" CR: "+Se+" g/U",we+="Off";var Ie=b.sens/xe;de+="Dynamic autosens.ratio set to "+xe.toPrecision(3)+" with ISF: "+Ie.toPrecision(3)+" mg/dl/U ("+(.0555*Ie).toPrecision(3)+" mmol/l/U). "+we,v.ratio=xe,R+=fe+he+pe+be+de+Fe+ve}else R+=0==oe&&1==ie?fe+he+pe+be+"Dynamic ISF is off."+Fe+ve:fe+"Dynamic ISF is off. Dynamic CR is off.";console.log(R),tddReason+=0==oe&&0==ie?"":we+be+Ce+ve;var De={},Ge=new Date;if(x&&(Ge=x),void 0===b||void 0===b.current_basal)return De.error="Error: could not get current basal rate",De;var Oe=t(b.current_basal,b),Te=Oe,Ue=new Date;x&&(Ue=x);var Re,Ae=new Date(e.date),Pe=o((Ue-Ae)/60/1e3,1),je=e.glucose,ke=e.noise;Re=e.delta>-.5?"+"+o(e.delta,0):o(e.delta,0);var Le=Math.min(e.delta,e.short_avgdelta),qe=Math.min(e.short_avgdelta,e.long_avgdelta),We=Math.max(e.delta,e.short_avgdelta,e.long_avgdelta);(je<=10||38===je||ke>=3)&&(De.reason="CGM is calibrating, in ??? state, or noise is high");if(je>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(je,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),Pe>12||Pe<-5?De.reason="If current system time "+Ue+" is correct, then BG data is too old. The last BG data was read "+Pe+"m ago at "+Ae:0===e.short_avgdelta&&0===e.long_avgdelta&&(e.last_cal&&e.last_cal<3?De.reason="CGM was just calibrated":De.reason="CGM data is unchanged ("+i(je,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"),je<=10||38===je||ke>=3||Pe>12||Pe<-5||0===e.short_avgdelta&&0===e.long_avgdelta)return a.rate>=Te?(De.reason+=". Canceling high temp basal of "+a.rate,De.deliverAt=Ge,De.temp="absolute",De.duration=0,De.rate=0,De):0===a.rate&&a.duration>30?(De.reason+=". Shortening "+a.duration+"m long zero temp to 30m. ",De.deliverAt=Ge,De.temp="absolute",De.duration=30,De.rate=0,De):(De.reason+=". Temp "+a.rate+" <= current basal "+Te+"U/hr; doing nothing. ",De);var Ee,ze,Ne,Ze=b.max_iob;if(void 0!==b.min_bg&&(ze=b.min_bg),void 0!==b.max_bg&&(Ne=b.max_bg),void 0===b.min_bg||void 0===b.max_bg)return De.error="Error: could not determine target_bg. ",De;Ee=(b.min_bg+b.max_bg)/2;var $e=b.exercise_mode||b.high_temptarget_raises_sensitivity,He=100,Je=160;if(b.half_basal_exercise_target&&(Je=b.half_basal_exercise_target),$e&&b.temptargetSet&&Ee>He||b.low_temptarget_lowers_sensitivity&&b.temptargetSet&&Ee<He){var Ke=Je-He;Ke+Ee-He>0?(sensitivityRatio=Ke/(Ke+Ee-He),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 "+Ee+"; ")}else void 0!==v&&v&&(sensitivityRatio=v.ratio,process.stderr.write("Autosens ratio: "+sensitivityRatio+"; "));if(sensitivityRatio&&(Te=b.current_basal*sensitivityRatio,(Te=t(Te,b))!==Oe?process.stderr.write("Adjusting basal from "+Oe+" to "+Te+"; "):process.stderr.write("Basal unchanged: "+Te+"; ")),b.temptargetSet);else if(void 0!==v&&v&&(b.sensitivity_raises_target&&v.ratio<1||b.resistance_lowers_target&&v.ratio>1)){ze=o((ze-60)/v.ratio)+60,Ne=o((Ne-60)/v.ratio)+60;var Qe=o((Ee-60)/v.ratio)+60;Ee===(Qe=Math.max(80,Qe))?process.stderr.write("target_bg unchanged: "+Qe+"; "):process.stderr.write("target_bg from "+Ee+" to "+Qe+"; "),Ee=Qe}var Ve=200,Xe=200,Ye=200;if(e.noise>=2){var ea=Math.max(1.1,b.noisyCGMTargetMultiplier);Math.min(250,b.maxRaw);Ve=o(Math.min(200,ze*ea)),Xe=o(Math.min(200,Ee*ea)),Ye=o(Math.min(200,Ne*ea)),process.stderr.write("Raising target_bg for noisy / raw CGM data, from "+Ee+" to "+Xe+"; "),ze=Ve,Ee=Xe,Ne=Ye}var aa=ze-.5*(ze-40),ra=o(b.sens,1),ta=b.sens;if(void 0!==v&&v&&((ta=o(ta=b.sens/sensitivityRatio,1))!==ra?process.stderr.write("ISF from "+i(ra,b)+" to "+i(ta,b)):process.stderr.write("ISF unchanged: "+i(ta,b)),n+="Autosens ratio: "+o(sensitivityRatio,2)+", ISF: "+i(ra,b)+"→"+i(ta,b)),console.error("CR:"+b.carb_ratio),ta=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,D=t.autoISF_duration,G=t.autoISF_average,O=r.autoisf_max,T=!1,U=1,R=1,A=1,P=a+10-G;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 L=-F/2/I*5,q=o(C-L*L/25*I,1);(L=o(L,1))<0&&k<0?(f="saw max of "+i(q,r)+", about "+-L+" min ago",console.error("Parabolic fit "+f)):L<0&&k>0?(f="saw min of "+i(q,r)+", about "+-L+" min ago",console.error("Parabolic fit "+f)):L>0&&k<0?(f="predicts max of "+i(q,r)+", in about "+L+"min",console.error("Parabolic fit "+f)):L>0&&k>0&&(f="predicts min of "+i(q,r)+", in about "+L+" 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 E=10*(W-.9),z=1;t.glucose<r.target_bg&&k>1&&(z=.5),A=1+k*z*(k<0?r.bgBrake_ISF_weight:r.bgAccel_ISF_weight)*E,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)+" limitLoged 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?(U=1+Math.max(0,H*r.postmeal_ISF_weight),console.error("pp_ISF adaptation is "+o(U,2)),m=", pp-ISF Ratio: "+o(U,2),1!=U&&(T=!0)):(R=p(H,r),P>-20&&(R*=.5),R=1+R,console.error("delta_ISF adaptation is "+o(R,2)),u=", Δ-ISF Ratio: "+o(R,2),1!=R&&(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)):D<10?console.error("dura_ISF by-passed; BG is only "+D+"m at level "+G):G<=a?console.error("dura_ISF by-passed; avg. glucose "+G+" below target "+i(a,r)):(J+=D/60*(K/a)*(G-a),T=!0,l=", Duration: "+D+", Avg: "+i(G,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(D,1)+"m")),$=1,T?($=Math.max(J,Z,R,A,U),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(U,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)+" limitLoged by autoisf_min "+r.autoisf_min),$=r.autoisf_min):$>O&&(console.error("final ISF factor "+o($,2)+" limitLoged 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))}(ta,Ee,b,e,_,x,0,sensitivityRatio),void 0===r)return De.error="Error: iob_data undefined. ",De;var oa,ia=r;if(r.length,r.length>1&&(r=ia[0]),void 0===r.activity||void 0===r.iob)return De.error="Error: iob_data missing some property. ",De;var na=((oa=void 0!==r.lastTemp?o((new Date(Ue).getTime()-r.lastTemp.date)/6e4):0)+a.duration)%30;if(console.error("currenttemp:"+a.rate+" lastTempAge:"+oa+"m, tempModulus:"+na+"m"),De.temp="absolute",De.deliverAt=Ge,M&&a&&r.lastTemp&&a.rate!==r.lastTemp.rate&&oa>10&&a.duration)return De.reason="Warning: currenttemp rate "+a.rate+" != lastTemp rate "+r.lastTemp.rate+" from pumphistory; canceling temp",B.setTempBasal(0,0,b,De,a);if(a&&r.lastTemp&&a.duration>0){var sa=oa-r.lastTemp.duration;if(sa>5&&oa>10)return De.reason="Warning: currenttemp running but lastTemp from pumphistory ended "+sa+"m ago; canceling temp",B.setTempBasal(0,0,b,De,a)}var la=o(-r.activity*ta*5,2),ma=o(6*(Le-la));ma<0&&(ma=o(6*(qe-la)))<0&&(ma=o(6*(e.long_avgdelta-la)));var ua=je,da=(ua=r.iob>0?o(je-r.iob*ta):o(je-r.iob*Math.min(ta,b.sens)))+ma;if(void 0===da||isNaN(da))return De.error="Error: could not calculate eventualBG. Sensitivity: "+ta+" Deviation: "+ma,De;var ca=function(e,a,r){return o(r+(e-a)/24,1)}(Ee,da,la);De={temp:"absolute",bg:je,tick:Re,eventualBG:da,insulinReq:0,reservoir:y,deliverAt:Ge,sensitivityRatio,TDD:re};var ga=[],fa=[],pa=[],ha=[];ga.push(je),fa.push(je),ha.push(je),pa.push(je);var ba=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,_,Ee),va=b.enableUAM,_a=0,Ba=0;_a=o(Le-la,1);var Ma=o(Le-la,1);csf=ta/b.carb_ratio,console.error("profile.sens:"+i(b.sens,b)+", sens:"+i(ta,b)+", CSF:"+o(csf,1));var ya=o(30*csf*5/60,1);_a>ya&&(console.error("Limiting carb impact from "+_a+" to "+ya+"mg/dL/5m (30g/h)"),_a=ya);var xa=3;sensitivityRatio&&(xa/=sensitivityRatio);var Sa=xa;if(_.carbs){xa=Math.max(xa,_.mealCOB/20);var wa=o((new Date(Ue).getTime()-_.lastCarbTime)/6e4),Ca=(_.carbs-_.mealCOB)/_.carbs;Sa=o(Sa=xa+1.5*wa/60,1),console.error("Last carbs "+wa+" minutes ago; remainingCATime:"+Sa+"hours; "+o(100*Ca)+"% carbs absorbed")}var Fa=Math.max(0,_a/5*60*Sa/2)/csf,Ia=90,Da=1;b.remainingCarbsCap&&(Ia=Math.min(90,b.remainingCarbsCap)),b.remainingCarbsFraction&&(Da=Math.min(1,b.remainingCarbsFraction));var Ga=1-Da,Oa=Math.max(0,_.mealCOB-Fa-_.carbs*Ga),Ta=(Oa=Math.min(Ia,Oa))*csf*5/60/(Sa/2),Ua=o(_.slopeFromMaxDeviation,2),Ra=o(_.slopeFromMinDeviation,2),Aa=Math.min(Ua,-Ra/3),Pa=0;0===_a?Ba=0:!0===b.floating_carbs?(Ba=Math.min(60*Sa/5/2,Math.max(0,_.carbs*csf/_a)),Pa=Math.min(60*Sa/5/2,Math.max(0,_.mealCOB*csf/_a)),_.carbs>0&&(n+=", Floating Carbs:, CID: "+o(Ba,1)+", MealCarbs: "+o(_.carbs,1)+", Not Floating:, CID: "+o(Pa,1)+", MealCOB: "+o(_.mealCOB,1),console.error("Floating Carbs CID: "+o(Ba,1)+" / MealCarbs: "+o(_.carbs,1)+" vs. Not Floating:"+o(Pa,1)+" / MealCOB:"+o(_.mealCOB,1)))):Ba=Math.min(60*Sa/5/2,Math.max(0,_.mealCOB*csf/_a)),console.error("Carb Impact:"+_a+"mg/dL per 5m; CI Duration:"+o(5*Ba/60*2,1)+"hours; remaining CI ("+Sa/2+"h peak):",o(Ta,1)+"mg/dL per 5m");var ja,ka,La,qa,Wa,Ea=999,za=999,Na=999,Za=je,$a=999,Ha=999,Ja=999,Ka=999,Qa=da,Va=je,Xa=je,Ya=0,er=[],ar=[];try{ia.forEach((function(e){var a=o(-e.activity*ta*5,2),r=o(-e.iobWithZeroTemp.activity*ta*5,2),t=_a*(1-Math.min(1,fa.length/12));Qa=fa[fa.length-1]+a+t;var i=ha[ha.length-1]+r,n=Math.max(0,Math.max(0,_a)*(1-ga.length/Math.max(2*Ba,1))),s=Math.min(ga.length,12*Sa-ga.length),l=Math.max(0,s/(Sa/2*12)*Ta);n+l,er.push(o(l,0)),ar.push(o(n,0)),COBpredBG=ga[ga.length-1]+a+Math.min(0,t)+n+l;var m=Math.max(0,Ma+pa.length*Aa),u=Math.max(0,Ma*(1-pa.length/Math.max(36,1))),d=Math.min(m,u);d>0&&(Ya=o(5*(pa.length+1)/60,1)),UAMpredBG=pa[pa.length-1]+a+Math.min(0,t)+d,fa.length<48&&fa.push(Qa),ga.length<48&&ga.push(COBpredBG),pa.length<48&&pa.push(UAMpredBG),ha.length<48&&ha.push(i),COBpredBG<$a&&($a=o(COBpredBG)),UAMpredBG<Ha&&(Ha=o(UAMpredBG)),Qa<Ja&&(Ja=o(Qa)),i<Ka&&(Ka=o(i));fa.length>18&&Qa<Ea&&(Ea=o(Qa)),Qa>Va&&(Va=Qa),(Ba||Ta>0)&&ga.length>18&&COBpredBG<za&&(za=o(COBpredBG)),(Ba||Ta>0)&&COBpredBG>Va&&(Xa=COBpredBG),va&&pa.length>12&&UAMpredBG<Na&&(Na=o(UAMpredBG)),va&&UAMpredBG>Va&&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: "+er.join(" "))),De.predBGs={},fa.forEach((function(e,a,r){r[a]=o(Math.min(401,Math.max(39,e)))}));for(var rr=fa.length-1;rr>12&&fa[rr-1]===fa[rr];rr--)fa.pop();for(De.predBGs.IOB=fa,La=o(fa[fa.length-1]),ha.forEach((function(e,a,r){r[a]=o(Math.min(401,Math.max(39,e)))})),rr=ha.length-1;rr>6&&!(ha[rr-1]>=ha[rr]||ha[rr]<=Ee);rr--)ha.pop();if(De.predBGs.ZT=ha,o(ha[ha.length-1]),_.mealCOB>0&&(_a>0||Ta>0)){for(ga.forEach((function(e,a,r){r[a]=o(Math.min(401,Math.max(39,e)))})),rr=ga.length-1;rr>12&&ga[rr-1]===ga[rr];rr--)ga.pop();De.predBGs.COB=ga,qa=o(ga[ga.length-1]),da=Math.max(da,o(ga[ga.length-1]))}if(_a>0||Ta>0){if(va){for(pa.forEach((function(e,a,r){r[a]=o(Math.min(401,Math.max(39,e)))})),rr=pa.length-1;rr>12&&pa[rr-1]===pa[rr];rr--)pa.pop();De.predBGs.UAM=pa,Wa=o(pa[pa.length-1]),pa[pa.length-1]&&(da=Math.max(da,o(pa[pa.length-1])))}De.eventualBG=da}console.error("UAM Impact:"+Ma+"mg/dL per 5m; UAM Duration:"+Ya+"hours"),Ea=Math.max(39,Ea),za=Math.max(39,za),Na=Math.max(39,Na),ja=o(Ea);var tr=_.mealCOB/_.carbs;ka=o(Na<999&&za<999?(1-tr)*UAMpredBG+tr*COBpredBG:za<999?(Qa+COBpredBG)/2:Na<999?(Qa+UAMpredBG)/2:Qa),Ka>ka&&(ka=Ka),Za=o(Za=Ba||Ta>0?va?tr*$a+(1-tr)*Ha:$a:va?Ha:Ja);var or=Na;if(Ka<aa)or=(Na+Ka)/2;else if(Ka<Ee){var ir=(Ka-aa)/(Ee-aa);or=(Na+(Na*ir+Ka*(1-ir)))/2}else Ka>Na&&(or=(Na+Ka)/2);if(or=o(or),_.carbs)if(!va&&za<999)ja=o(Math.max(Ea,za));else if(za<999){var nr=tr*za+(1-tr)*or;ja=o(Math.max(Ea,za,nr))}else ja=va?or:Za;else va&&(ja=o(Math.max(Ea,or)));ja=Math.min(ja,ka),process.stderr.write("minPredBG: "+ja+" minIOBPredBG: "+Ea+" minZTGuardBG: "+Ka),za<999&&process.stderr.write(" minCOBPredBG: "+za),Na<999&&process.stderr.write(" minUAMPredBG: "+Na),console.error(" avgPredBG:"+ka+" COB/Carbs:"+_.mealCOB+"/"+_.carbs),Xa>je&&(ja=Math.min(ja,Xa)),De.COB=_.mealCOB,De.IOB=r.iob,De.BGI=i(la,b),De.deviation=i(ma,b),De.ISF=i(ta,b),De.CR=o(b.carb_ratio,2),De.target_bg=i(Ee,b),De.TDD=o(re,2),De.reason=n+", COB: "+De.COB+", Dev: "+De.deviation+", BGI: "+De.BGI+", CR: "+De.CR+", Target: "+De.target_bg+", minPredBG "+i(ja,b)+", minGuardBG "+i(Za,b)+", IOBpredBG "+i(La,b)+tddReason,qa>0&&(De.reason+=", COBpredBG "+i(qa,b)),Wa>0&&(De.reason+=", UAMpredBG "+i(Wa,b)),De.reason+="; ";var sr=ua;sr<40&&(sr=Math.min(Za,sr));var lr,mr=aa-sr,ur=240,dr=240;if(_.mealCOB>0&&(_a>0||Ta>0)){for(rr=0;rr<ga.length;rr++)if(ga[rr]<ze){ur=5*rr;break}for(rr=0;rr<ga.length;rr++)if(ga[rr]<aa){dr=5*rr;break}}else{for(rr=0;rr<fa.length;rr++)if(fa[rr]<ze){ur=5*rr;break}for(rr=0;rr<fa.length;rr++)if(fa[rr]<aa){dr=5*rr;break}}ba&&Za<aa&&(console.error("minGuardBG "+i(Za,b)+" projected below "+i(aa,b)+" - disabling SMB"),ba=!1),void 0===b.maxDelta_bg_threshold&&(lr=.2),void 0!==b.maxDelta_bg_threshold&&(lr=Math.min(b.maxDelta_bg_threshold,.4)),We>lr*je&&(console.error("maxDelta "+i(We,b)+" > "+100*lr+"% of BG "+i(je,b)+" - disabling SMB"),De.reason+="maxDelta "+i(We,b)+" > "+100*lr+"% of BG "+i(je,b)+" - SMB disabled!, ",ba=!1),console.error("BG projected to remain above "+i(ze,b)+" for "+ur+"minutes"),(dr<240||ur<60)&&console.error("BG projected to remain above "+i(aa,b)+" for "+dr+"minutes");var cr=dr,gr=b.current_basal*ta*cr/60,fr=Math.max(0,_.mealCOB-.25*_.carbs),pr=(mr-gr)/csf-fr;gr=o(gr),pr=o(pr),console.error("naive_eventualBG:"+ua+" bgUndershoot:"+mr+" zeroTempDuration:"+cr+" zeroTempEffect:"+gr+" carbsReq:"+pr),pr>=b.carbsReqThreshold&&dr<=45&&(De.carbsReq=pr,De.reason+=pr+" add'l carbs req w/in "+dr+"m; ");var hr=0;if(je<aa&&r.iob<20*-b.current_basal/60&&Le>0&&Le>ca)De.reason+="IOB "+r.iob+" < "+o(20*-b.current_basal/60,2),De.reason+=" and minDelta "+i(Le,b)+" > expectedDelta "+i(ca,b)+"; ";else if(je<aa||Za<aa)return De.reason+="minGuardBG "+i(Za,b)+"<"+i(aa,b),hr=o(60*((mr=Ee-Za)/ta)/b.current_basal),hr=30*o(hr/30),hr=Math.min(120,Math.max(30,hr)),B.setTempBasal(0,hr,b,De,a);if(b.skip_neutral_temps&&De.deliverAt.getMinutes()>=55)return De.reason+="; Canceling temp at "+De.deliverAt.getMinutes()+"m past the hour. ",B.setTempBasal(0,0,b,De,a);var br=0,vr=Te;if(da<ze){if(De.reason+="Eventual BG "+i(da,b)+" < "+i(ze,b),Le>ca&&Le>0&&!pr)return ua<40?(De.reason+=", naive_eventualBG < 40. ",B.setTempBasal(0,30,b,De,a)):(e.delta>Le?De.reason+=", but Delta "+i(Re,b)+" > expectedDelta "+i(ca,b):De.reason+=", but Min. Delta "+Le.toFixed(2)+" > Exp. Delta "+i(ca,b),a.duration>15&&t(Te,b)===t(a.rate,b)?(De.reason+=", temp "+a.rate+" ~ req "+Te+"U/hr. ",De):(De.reason+="; setting current basal of "+Te+" as temp. ",B.setTempBasal(Te,30,b,De,a)));br=o(br=2*Math.min(0,(da-Ee)/ta),2);var _r=Math.min(0,(ua-Ee)/ta);if(_r=o(_r,2),Le<0&&Le>ca)br=o(br*(Le/ca),2);if(vr=t(vr=Te+2*br,b),a.duration*(a.rate-Te)/60<Math.min(br,_r)-.3*Te)return De.reason+=", "+a.duration+"m@"+a.rate.toFixed(2)+" is a lot less than needed. ",B.setTempBasal(vr,30,b,De,a);if(void 0!==a.rate&&a.duration>5&&vr>=.8*a.rate)return De.reason+=", temp "+a.rate+" ~< req "+vr+"U/hr. ",De;if(vr<=0){if((hr=o(60*((mr=Ee-ua)/ta)/b.current_basal))<0?hr=0:(hr=30*o(hr/30),hr=Math.min(120,Math.max(0,hr))),hr>0)return De.reason+=", setting "+hr+"m zero temp. ",B.setTempBasal(vr,hr,b,De,a)}else De.reason+=", setting "+vr+"U/hr. ";return B.setTempBasal(vr,30,b,De,a)}if(Le<ca&&(!M||!ba))return e.delta<Le?De.reason+="Eventual BG "+i(da,b)+" > "+i(ze,b)+" but Delta "+i(Re,b)+" < Exp. Delta "+i(ca,b):De.reason+="Eventual BG "+i(da,b)+" > "+i(ze,b)+" but Min. Delta "+Le.toFixed(2)+" < Exp. Delta "+i(ca,b),a.duration>15&&t(Te,b)===t(a.rate,b)?(De.reason+=", temp "+a.rate+" ~ req "+Te+"U/hr. ",De):(De.reason+="; setting current basal of "+Te+" as temp. ",B.setTempBasal(Te,30,b,De,a));if(Math.min(da,ja)<Ne&&(!M||!ba))return De.reason+=i(da,b)+"-"+i(ja,b)+" in range: no temp required",a.duration>15&&t(Te,b)===t(a.rate,b)?(De.reason+=", temp "+a.rate+" ~ req "+Te+"U/hr. ",De):(De.reason+="; setting current basal of "+Te+" as temp. ",B.setTempBasal(Te,30,b,De,a));if(da>=Ne&&(De.reason+="Eventual BG "+i(da,b)+" >= "+i(Ne,b)+", "),r.iob>Ze)return De.reason+="IOB "+o(r.iob,2)+" > max_iob "+Ze,a.duration>15&&t(Te,b)===t(a.rate,b)?(De.reason+=", temp "+a.rate+" ~ req "+Te+"U/hr. ",De):(De.reason+="; setting current basal of "+Te+" as temp. ",B.setTempBasal(Te,30,b,De,a));(br=o((Math.min(ja,da)-Ee)/ta,2))>Ze-r.iob&&(De.reason+="max_iob "+Ze+", ",br=Ze-r.iob),vr=t(vr=Te+2*br,b),br=o(br,3),De.insulinReq=br;var Br=o((new Date(Ue).getTime()-r.lastBolusTime)/6e4,1);if(M&&ba&&je>aa){var Mr=o(_.mealCOB/b.carb_ratio,3);if(b.use_autoisf)yr=b.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 xr=0;void 0===b.maxSMBBasalMinutes?(xr=o(yr*b.current_basal*30/60,1),console.error("profile.maxSMBBasalMinutes undefined: defaulting to 30m")):r.iob>Mr&&r.iob>0?(console.error("IOB",r.iob,"> COB",_.mealCOB+"; mealInsulinReq =",Mr),b.maxUAMSMBBasalMinutes?(console.error("profile.maxUAMSMBBasalMinutes:",b.maxUAMSMBBasalMinutes,"profile.current_basal:",b.current_basal),xr=o(yr*b.current_basal*b.maxUAMSMBBasalMinutes/60,1)):(console.error("profile.maxUAMSMBBasalMinutes undefined: defaulting to 30m"),xr=o(30*b.current_basal/60,1))):(console.error("profile.maxSMBBasalMinutes:",b.maxSMBBasalMinutes,"profile.current_basal:",b.current_basal),xr=o(yr*b.current_basal*b.maxSMBBasalMinutes/60,1));var Sr=b.bolus_increment,wr=1/Sr,Cr=h(b,je,Ee);Cr>.5&&console.error("SMB Delivery Ratio increased from default 0.5 to "+o(Cr,2));var Fr=Math.min(br*Cr,xr);Fr=Math.floor(Fr*wr)/wr,hr=o(60*((Ee-(ua+Ea)/2)/ta)/b.current_basal),br>0&&Fr<Sr&&(hr=0);var Ir=0;hr<=0?hr=0:hr>=30?(hr=30*o(hr/30),hr=Math.min(60,Math.max(0,hr))):(Ir=o(Te*hr/30,2),hr=30),De.reason+=" insulinReq "+br,Fr>=xr&&(De.reason+="; maxBolus "+xr),hr>0&&(De.reason+="; setting "+hr+"m low temp of "+Ir+"U/h"),De.reason+=". ";var Dr=3;b.SMBInterval&&(Dr=Math.min(10,Math.max(1,b.SMBInterval)));var Gr=o(Dr-Br,0),Or=o(60*(Dr-Br),0)%60;if(console.error("naive_eventualBG",ua+",",hr+"m "+Ir+"U/h temp needed; last bolus",Br+"m ago; maxBolus: "+xr),Br>Dr?Fr>0&&(De.units=Fr,De.reason+="Microbolusing "+Fr+"U. "):De.reason+="Waiting "+Gr+"m "+Or+"s to microbolus again. ",hr>0)return De.rate=Ir,De.duration=hr,De}var Tr=B.getMaxSafeBasal(b);return vr>Tr&&(De.reason+="adj. req. rate: "+vr+" to maxSafeBasal: "+Tr+", ",vr=t(Tr,b)),a.duration*(a.rate-Te)/60>=2*br?(De.reason+=a.duration+"m@"+a.rate.toFixed(2)+" > 2 * insulinReq. Setting temp basal of "+vr+"U/hr. ",B.setTempBasal(vr,30,b,De,a)):void 0===a.duration||0===a.duration?(De.reason+="no temp, setting "+vr+"U/hr. ",B.setTempBasal(vr,30,b,De,a)):a.duration>5&&t(vr,b)<=t(a.rate,b)?(De.reason+="temp "+a.rate+" >~ req "+vr+"U/hr. ",De):(De.reason+="temp "+a.rate+"<"+vr+"U/hr. ",B.setTempBasal(vr,30,b,De,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(5546);freeaps_determineBasal=t})();
|