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