| 1 |
- var freeaps_determineBasal;(()=>{var e={5546:(e,t,a)=>{var r=a(6880);function o(e,t){t||(t=0);var a=Math.pow(10,t);return Math.round(e*a)/a}function n(e,t){return"mmol/L"===t.out_units?o(.0555*e,1):Math.round(e)}e.exports=function(e,t,a,i,s,l,m,u,d,c,g,h,p,f){var b=i.min_bg,B=0,v="",M="",_="",y="",x="",w=0,D=0,S=0,T=0,C=0,U=0;const G=f.weightedAverage,R=i.weightPercentage,O=f.average_total_data;function A(e,t){var a=e.getTime();return new Date(a+36e5*t)}function I(e){var t=i.bolus_increment;.025!=t&&(t=.05);var a=e/t;return a>=1?o(Math.floor(a)*t,5):0}function j(e){function t(e){return e<10&&(e="0"+e),e}return t(e.getHours())+":"+t(e.getMinutes())+":00"}function F(e,t){var a=new Date("1/1/1999 "+e),r=new Date("1/1/1999 "+t);return(a.getTime()-r.getTime())/36e5}function P(e,t){var a=0,r=t,o=(e-t)/36e5,n=0,i=o,s=0;do{if(o>0){var l=j(r),m=p[0].rate;for(let e=0;e<p.length;e++){var u=p[e].start;if(l==u){if(e+1<p.length){o>=(s=F(p[e+1].start,p[e].start))?n=s:o<s&&(n=o)}else if(e+1==p.length){let t=p[0].start;o>=(s=24-F(p[e].start,t))?n=s:o<s&&(n=o)}a+=I((m=p[e].rate)*n),o-=n,console.log("Dynamic ratios log: scheduled insulin added: "+I(m*n)+" U. Bas duration: "+n.toPrecision(3)+" h. Base Rate: "+m+" U/h. Time :"+l),r=A(r,n)}else if(l>u)if(e+1<p.length){var d=p[e+1].start;l<d&&(o>=(s=F(d,l))?n=s:o<s&&(n=o),a+=I((m=p[e].rate)*n),o-=n,console.log("Dynamic ratios log: scheduled insulin added: "+I(m*n)+" U. Bas duration: "+n.toPrecision(3)+" h. Base Rate: "+m+" U/h. Time :"+l),r=A(r,n))}else if(e==p.length-1){o>=(s=F("23:59:59",l))?n=s:o<s&&(n=o),a+=I((m=p[e].rate)*n),o-=n,console.log("Dynamic ratios log: scheduled insulin added: "+I(m*n)+" U. Bas duration: "+n.toPrecision(3)+" h. Base Rate: "+m+" U/h. Time :"+l),r=A(r,n)}}}}while(o>0&&o<i);return a}if(g.length){let e=g.length-1;var q=new Date(g[e].timestamp),W=new Date(g[0].timestamp);if("TempBasalDuration"==g[0]._type&&(W=new Date),(B=(W-q)/36e5)<23.9&&B>21)C=P(q,(k=24-B,E=q.getTime(),new Date(E-36e5*k))),y="24 hours of data is required for an accurate tdd calculation. Currently only "+B.toPrecision(3)+" hours of pump history data are available. Using your pump scheduled basals to fill in the missing hours. Scheduled basals added: "+C.toPrecision(5)+" U. ";else B<21?(Y=!1,enableDynamicCR=!1):y=""}else console.log("Pumphistory is empty!"),Y=!1,enableDynamicCR=!1;var k,E;for(let e=0;e<g.length;e++)"Bolus"==g[e]._type&&(T+=g[e].amount);for(let e=1;e<g.length;e++)if("TempBasal"==g[e]._type&&g[e].rate>0){w=e,U=g[e].rate;var L=g[e-1]["duration (min)"]/60,z=L,N=new Date(g[e-1].timestamp),Z=N,H=0;do{if(e--,0==e){Z=new Date;break}if("TempBasal"==g[e]._type||"PumpSuspend"==g[e]._type){Z=new Date(g[e].timestamp);break}var $=e-2;if($>=0&&"Rewind"==g[$]._type){let e=g[$].timestamp;for(;$-1>=0&&"Prime"==g[$-=1]._type;)H=(g[$].timestamp-e)/36e5;H>=L&&(Z=e,H=0)}}while(e>0);var J=(Z-N)/36e5;J<z&&(L=J),S+=I(U*(L-H)),e=w}for(let e=0;e<g.length;e++)if(0,0==g[e]["duration (min)"]||"PumpResume"==g[e]._type){let t=new Date(g[e].timestamp),a=t,r=e;do{if(r>0&&(--r,"TempBasal"==g[r]._type)){a=new Date(g[r].timestamp);break}}while(r>0);(a-t)/36e5>0&&(C+=P(a,t))}for(let e=g.length-1;e>0;e--)if("TempBasalDuration"==g[e]._type){let t=g[e]["duration (min)"]/60,a=new Date(g[e].timestamp);var K=a;let r=e;do{if(--r,r>=0&&("TempBasal"==g[r]._type||"PumpSuspend"==g[r]._type)){K=new Date(g[r].timestamp);break}}while(r>0);if(0==e&&"TempBasalDuration"==g[0]._type&&(K=new Date,t=g[e]["duration (min)"]/60),(K-a)/36e5-t>0){C+=P(K,A(a,t))}}var Q,V={TDD:o(D=T+S+C,5),bolus:o(T,5),temp_basal:o(S,5),scheduled_basal:o(C,5)};B>21?(M=". Bolus insulin: "+T.toPrecision(5)+" U",_=". Temporary basal insulin: "+S.toPrecision(5)+" U",v=". Insulin with scheduled basal rate: "+C.toPrecision(5)+" U",x=y+(" TDD past 24h is: "+D.toPrecision(5)+" U")+M+_+v,tddReason=", Total insulin: "+o(D,2)+" U, "+o(T/D*100,0)+"% Bolus "+o((S+C)/D*100,0)+"% Basal"):tddReason=", TDD: Not enough pumpData (< 21h)";const X=e.glucose;var Y=h.useNewFormula;const ee=h.enableDynamicCR,te=Math.min(i.autosens_min,i.autosens_max),ae=Math.max(i.autosens_min,i.autosens_max),re=h.adjustmentFactor,oe=i.min_bg;var ne="",ie=1,se="";O>0&&(ie=G/O),se=ie>1?"Basal adjustment with a 24 hour to total average (up to 14 days of data) TDD ratio (limited by Autosens max setting). Basal Ratio: "+(ie=o(ie=Math.min(ie,i.autosens_max),2))+". Upper limit = Autosens max ("+i.autosens_max+")":ie<1?"Basal adjustment with a 24 hour to to total average (up to 14 days of data) TDD ratio (limited by Autosens min setting). Basal Ratio: "+(ie=o(ie=Math.max(ie,i.autosens_min),2))+". Lower limit = Autosens min ("+i.autosens_min+")":"Basal adjusted with a 24 hour to total average (up to 14 days of data) TDD ratio: "+ie,se=", Basal ratio: "+ie,oe>=118&&(Y=!1,ne="Dynamic ISF temporarily off due to a high temp target/exercising. Current min target: "+oe);var le=", Dynamic ratios log: ",me=", AF: "+re,ue="BG: "+X+" mg/dl ("+(.0555*X).toPrecision(2)+" mmol/l)",de="",ce="";const ge=h.curve,he=h.insulinPeakTime,pe=h.useCustomPeakTime;var fe=55,be=65;switch(ge){case"rapid-acting":be=65;break;case"ultra-rapid":be=50}pe?(fe=120-he,console.log("Custom insulinpeakTime set to :"+he+", insulinFactor: "+fe)):(fe=120-be,console.log("insulinFactor set to : "+fe)),Q=D,R<1&&G>0&&(D=G,console.log("Using weighted TDD average: "+o(D,2)+" U, instead of past 24 h ("+o(Q,2)+" U), weight: "+R),ce=", Weighted TDD: "+o(D,2)+" U");const Be=h.sigmoid;var ve="";if(Y){var Me=i.sens*re*D*Math.log(X/fe+1)/1800;de=", Logarithmic formula"}var _e="";if(Y&&Be){const e=te,t=ae-e,a=.0555*(X-i.min_bg);var ye=ie;_e=", tdd_factor: "+o(ye,1);const r=ae-1,n=Math.log10(1/r-e/r)/Math.log10(Math.E),s=a*re*ye+n;Me=t/(1+Math.exp(-s))+e,de=", Sigmoid function"}var xe=i.carb_ratio;const we=o(i.carb_ratio,1);var De="",Se="";if(Y&&D>0){if(De=", Dynamic ISF/CR: On/",Me>ae?(ne=", Dynamic ISF limited by autosens_max setting: "+ae+" ("+o(Me,2)+"), ",Se=", Autosens/Dynamic Limit: "+ae+" ("+o(Me,2)+")",Me=ae):Me<te&&(ne=", Dynamic ISF limjted by autosens_min setting: "+te+" ("+o(Me,2)+"). ",Se=", Autosens/Dynamic Limit: "+te+" ("+o(Me,2)+")",Me=te),ee){De+="On";var Te=Me;Me>1&&(Te=(Me-1)/2+1);var Ce=" CR: "+(xe=o(xe/Te,2))+" g/U";i.carb_ratio=xe}else Ce=" CR: "+xe+" g/U",De+="Off";const e=i.sens/Me;ve=". Using Sigmoid function, the autosens ratio has been adjusted with sigmoid factor to: "+o(s.ratio,2)+". New ISF = "+o(e,2)+" mg/dl ("+o(.0555*e,2)+" (mmol/l). CR adjusted from "+o(xe,2)+" to "+o(i.carb_ratio,2)+" ("+o(.0555*i.carb_ratio,2)+" mmol/l).",ne+=Be?ve:", Dynamic autosens.ratio set to "+o(Me,2)+" with ISF: "+e.toPrecision(3)+" mg/dl/U ("+(.0555*e).toPrecision(3)+" mmol/l/U)",s.ratio=Me,x+=le+ue+me+de+ne+De+Ce+ce}else x+=le+"Dynamic Settings disabled";console.log(x),Y||ee?Y&&i.tddAdjBasal?tddReason+=De+de+Se+me+se:Y&&!i.tddAdjBasal&&(tddReason+=De+de+Se+me):tddReason+="";var Ue={},Ge=new Date;if(c&&(Ge=c),void 0===i||void 0===i.current_basal)return Ue.error="Error: could not get current basal rate",Ue;var Re=r(i.current_basal,i),Oe=Re,Ae=new Date;c&&(Ae=c);var Ie,je=new Date(e.date),Fe=o((Ae-je)/60/1e3,1),Pe=e.glucose,qe=e.noise;Ie=e.delta>-.5?"+"+o(e.delta,0):o(e.delta,0);var We=Math.min(e.delta,e.short_avgdelta),ke=Math.min(e.short_avgdelta,e.long_avgdelta),Ee=Math.max(e.delta,e.short_avgdelta,e.long_avgdelta);(Pe<=10||38===Pe||qe>=3)&&(Ue.reason="CGM is calibrating, in ??? state, or noise is high");if(Pe>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(Pe,i)+"+"+n(e.delta,i)+") for 5m w/ "+n(e.short_avgdelta,i)+" mg/dL ~15m change & "+n(e.long_avgdelta,2)+" mg/dL ~45m change"),console.error("Simulator mode detected ("+e.device+"): continuing anyway")):!0),Fe>12||Fe<-5?Ue.reason="If current system time "+Ae+" is correct, then BG data is too old. The last BG data was read "+Fe+"m ago at "+je:0===e.short_avgdelta&&0===e.long_avgdelta&&(e.last_cal&&e.last_cal<3?Ue.reason="CGM was just calibrated":Ue.reason="CGM data is unchanged ("+n(Pe,i)+"+"+n(e.delta,i)+") for 5m w/ "+n(e.short_avgdelta,i)+" mg/dL ~15m change & "+n(e.long_avgdelta,i)+" mg/dL ~45m change"),Pe<=10||38===Pe||qe>=3||Fe>12||Fe<-5||0===e.short_avgdelta&&0===e.long_avgdelta)return t.rate>=Oe?(Ue.reason+=". Canceling high temp basal of "+t.rate,Ue.deliverAt=Ge,Ue.temp="absolute",Ue.duration=0,Ue.rate=0,Ue):0===t.rate&&t.duration>30?(Ue.reason+=". Shortening "+t.duration+"m long zero temp to 30m. ",Ue.deliverAt=Ge,Ue.temp="absolute",Ue.duration=30,Ue.rate=0,Ue):(Ue.reason+=". Temp "+t.rate+" <= current basal "+Oe+"U/hr; doing nothing. ",Ue);var Le,ze,Ne,Ze,He=i.max_iob;if(void 0!==i.min_bg&&(ze=i.min_bg),void 0!==i.max_bg&&(Ne=i.max_bg),void 0!==i.enableSMB_high_bg_target&&(Ze=i.enableSMB_high_bg_target),void 0===i.min_bg||void 0===i.max_bg)return Ue.error="Error: could not determine target_bg. ",Ue;Le=(i.min_bg+i.max_bg)/2;var $e=100,Je=160;if(i.half_basal_exercise_target&&(Je=i.half_basal_exercise_target),i.temptargetSet&&Le>$e||i.low_temptarget_lowers_sensitivity&&i.temptargetSet&&Le<$e){var Ke=Je-$e;sensitivityRatio=Ke*(Ke+Le-$e)<=0?i.autosens_max:Ke/(Ke+Le-$e),sensitivityRatio=Math.min(sensitivityRatio,i.autosens_max),sensitivityRatio=o(sensitivityRatio,2),process.stderr.write("Sensitivity ratio set to "+sensitivityRatio+" based on temp target of "+Le+"; ")}else void 0!==s&&s&&(sensitivityRatio=s.ratio,process.stderr.write("Autosens ratio: "+sensitivityRatio+"; "));if(i.temptargetSet&&Le<$e&&Y&&X>=Le&&sensitivityRatio<Me&&(s.ratio=Me*($e/Le),s.ratio=Math.min(s.ratio,i.autosens_max),sensitivityRatio=o(s.ratio,2),console.log("Dynamic ratio increased from "+o(Me,2)+" to "+o(s.ratio,2)+" due to a low temp target ("+Le+").")),sensitivityRatio&&!Y?(Oe=i.current_basal*sensitivityRatio,Oe=r(Oe,i)):Y&&i.tddAdjBasal&&(Oe=i.current_basal*ie,Oe=r(Oe,i),process.stderr.write("TDD-adjustment of basals activated, using tdd24h_14d_Ratio "+o(ie,2)+", TDD 24h = "+o(Q,2)+"U, Weighted average TDD = "+o(G,2)+"U, (Weight percentage = "+R+"), Total data of TDDs (up to 14 days) average = "+o(O,2)+"U. "),Oe!==Re?process.stderr.write("Adjusting basal from "+Re+" U/h to "+Oe+" U/h; "):process.stderr.write("Basal unchanged: "+Oe+" U/h; ")),i.temptargetSet);else if(void 0!==s&&s&&(i.sensitivity_raises_target&&s.ratio<1||i.resistance_lowers_target&&s.ratio>1)){ze=o((ze-60)/s.ratio)+60,Ne=o((Ne-60)/s.ratio)+60;var Qe=o((Le-60)/s.ratio)+60;Le===(Qe=Math.max(80,Qe))?process.stderr.write("target_bg unchanged: "+Qe+"; "):process.stderr.write("target_bg from "+Le+" to "+Qe+"; "),Le=Qe}var Ve=200,Xe=200,Ye=200;if(e.noise>=2){var et=Math.max(1.1,i.noisyCGMTargetMultiplier);Math.min(250,i.maxRaw);Ve=o(Math.min(200,ze*et)),Xe=o(Math.min(200,Le*et)),Ye=o(Math.min(200,Ne*et)),process.stderr.write("Raising target_bg for noisy / raw CGM data, from "+Le+" to "+Xe+"; "),ze=Ve,Le=Xe,Ne=Ye}var tt=ze-.5*(ze-40),at=i.threshold_setting;at>tt&&at<=120&&at>=65?(console.error("Threshold changed in settings from "+n(tt,i)+" to "+n(at,i)+". "),tt=at):console.error("Current threshold: "+n(tt,i));var rt="",ot=o(i.sens,1),nt=i.sens;if(void 0!==s&&s&&((nt=o(nt=i.sens/sensitivityRatio,1))!==ot?process.stderr.write("ISF from "+n(ot,i)+" to "+n(nt,i)):process.stderr.write("ISF unchanged: "+n(nt,i)),rt+="Autosens ratio: "+o(sensitivityRatio,2)+", ISF: "+n(ot,i)+"→"+n(nt,i)),console.error("CR:"+i.carb_ratio),void 0===a)return Ue.error="Error: iob_data undefined. ",Ue;var it,st=a;if(a.length,a.length>1&&Q>0&&(a=st[0]),void 0===a.activity||void 0===a.iob)return Ue.error="Error: iob_data missing some property. ",Ue;var lt=((it=void 0!==a.lastTemp?o((new Date(Ae).getTime()-a.lastTemp.date)/6e4):0)+t.duration)%30;if(console.error("currenttemp:"+t.rate+" lastTempAge:"+it+"m, tempModulus:"+lt+"m"),Ue.temp="absolute",Ue.deliverAt=Ge,u&&t&&a.lastTemp&&t.rate!==a.lastTemp.rate&&it>10&&t.duration)return Ue.reason="Warning: currenttemp rate "+t.rate+" != lastTemp rate "+a.lastTemp.rate+" from pumphistory; canceling temp",m.setTempBasal(0,0,i,Ue,t);if(t&&a.lastTemp&&t.duration>0){var mt=it-a.lastTemp.duration;if(mt>5&&it>10)return Ue.reason="Warning: currenttemp running but lastTemp from pumphistory ended "+mt+"m ago; canceling temp",m.setTempBasal(0,0,i,Ue,t)}var ut=o(-a.activity*nt*5,2),dt=o(6*(We-ut));dt<0&&(dt=o(6*(ke-ut)))<0&&(dt=o(6*(e.long_avgdelta-ut)));var ct=Pe,gt=(ct=a.iob>0?o(Pe-a.iob*nt):o(Pe-a.iob*Math.min(nt,i.sens)))+dt;if(void 0===gt||isNaN(gt))return Ue.error="Error: could not calculate eventualBG. Sensitivity: "+nt+" Deviation: "+dt,Ue;var ht=function(e,t,a){return o(a+(e-t)/24,1)}(Le,gt,ut);Ue={temp:"absolute",bg:Pe,tick:Ie,eventualBG:gt,insulinReq:0,reservoir:d,deliverAt:Ge,sensitivityRatio,TDD:Q,insulin:V};var pt=[],ft=[],bt=[],Bt=[];pt.push(Pe),ft.push(Pe),Bt.push(Pe),bt.push(Pe);var vt=function(e,t,a,r,o,i){return t?!e.allowSMB_with_high_temptarget&&e.temptargetSet&&o>100?(console.error("SMB disabled due to high temptarget of "+o),!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&&o<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(o,e)),!0):!0===e.enableSMB_high_bg&&null!==i&&r>=i?(console.error("Checking BG to see if High for SMB enablement."),console.error("Current BG",r," | High BG ",i),a.bwFound?console.error("Warning: High BG SMB enabled within 6h of using Bolus Wizard: be sure to easy bolus 30s before using Bolus Wizard"):console.error("High BG detected. Enabling SMB."),!0):(console.error("SMB disabled (no enableSMB preferences active or no condition satisfied)"),!1):(console.error("SMB disabled (!microBolusAllowed)"),!1)}(i,u,l,Pe,Le,Ze),Mt=i.enableUAM,_t=0,yt=0;_t=o(We-ut,1);var xt=o(We-ut,1);csf=nt/i.carb_ratio,console.error("profile.sens:"+n(i.sens,i)+", sens:"+n(nt,i)+", CSF:"+o(csf,1));var wt=o(30*csf*5/60,1);_t>wt&&(console.error("Limiting carb impact from "+_t+" to "+wt+"mg/dL/5m (30g/h)"),_t=wt);var Dt=3;sensitivityRatio&&(Dt/=sensitivityRatio);var St=Dt;if(l.carbs){Dt=Math.max(Dt,l.mealCOB/20);var Tt=o((new Date(Ae).getTime()-l.lastCarbTime)/6e4),Ct=(l.carbs-l.mealCOB)/l.carbs;St=o(St=Dt+1.5*Tt/60,1),console.error("Last carbs "+Tt+" minutes ago; remainingCATime:"+St+"hours; "+o(100*Ct,1)+"% carbs absorbed")}var Ut=Math.max(0,_t/5*60*St/2)/csf,Gt=90,Rt=1;i.remainingCarbsCap&&(Gt=Math.min(90,i.remainingCarbsCap)),i.remainingCarbsFraction&&(Rt=Math.min(1,i.remainingCarbsFraction));var Ot=1-Rt,At=Math.max(0,l.mealCOB-Ut-l.carbs*Ot),It=(At=Math.min(Gt,At))*csf*5/60/(St/2),jt=o(l.slopeFromMaxDeviation,2),Ft=o(l.slopeFromMinDeviation,2),Pt=Math.min(jt,-Ft/3);yt=0===_t?0:Math.min(60*St/5/2,Math.max(0,l.mealCOB*csf/_t)),console.error("Carb Impact:"+_t+"mg/dL per 5m; CI Duration:"+o(5*yt/60*2,1)+"hours; remaining CI ("+St/2+"h peak):"+o(It,1)+"mg/dL per 5m");var qt,Wt,kt,Et,Lt,zt=999,Nt=999,Zt=999,Ht=Pe,$t=999,Jt=999,Kt=999,Qt=999,Vt=gt,Xt=Pe,Yt=Pe,ea=0,ta=[],aa=[];try{st.forEach((function(e){var t=o(-e.activity*nt*5,2),a=o(-e.iobWithZeroTemp.activity*nt*5,2),r=ct,n=_t*(1-Math.min(1,ft.length/12));if(!0===(Y&&!Be))Vt=ft[ft.length-1]+o(-e.activity*(1800/(D*re*Math.log(Math.max(ft[ft.length-1],39)/fe+1)))*5,2)+n,r=Bt[Bt.length-1]+o(-e.iobWithZeroTemp.activity*(1800/(D*re*Math.log(Math.max(Bt[Bt.length-1],39)/fe+1)))*5,2),console.log("Dynamic ISF (Logarithmic Formula) )adjusted predictions for IOB and ZT: IOBpredBG: "+o(Vt,2)+" , ZTpredBG: "+o(r,2));else Vt=ft[ft.length-1]+t+n,r=Bt[Bt.length-1]+a;var i=Math.max(0,Math.max(0,_t)*(1-pt.length/Math.max(2*yt,1))),s=Math.min(pt.length,12*St-pt.length),l=Math.max(0,s/(St/2*12)*It);i+l,ta.push(o(l,0)),aa.push(o(i,0)),COBpredBG=pt[pt.length-1]+t+Math.min(0,n)+i+l;var m=Math.max(0,xt+bt.length*Pt),u=Math.max(0,xt*(1-bt.length/Math.max(36,1))),d=Math.min(m,u);if(d>0&&(ea=o(5*(bt.length+1)/60,1)),!0===(Y&&!Be))UAMpredBG=bt[bt.length-1]+o(-e.activity*(1800/(D*re*Math.log(Math.max(bt[bt.length-1],39)/fe+1)))*5,2)+Math.min(0,n)+d,console.log("Dynamic ISF (Logarithmic Formula) adjusted prediction for UAM: UAMpredBG: "+o(UAMpredBG,2));else UAMpredBG=bt[bt.length-1]+t+Math.min(0,n)+d;ft.length<48&&ft.push(Vt),pt.length<48&&pt.push(COBpredBG),bt.length<48&&bt.push(UAMpredBG),Bt.length<48&&Bt.push(r),COBpredBG<$t&&($t=o(COBpredBG)),UAMpredBG<Jt&&(Jt=o(UAMpredBG)),Vt<Kt&&(Kt=o(Vt)),r<Qt&&(Qt=o(r));ft.length>18&&Vt<zt&&(zt=o(Vt)),Vt>Xt&&(Xt=Vt),(yt||It>0)&&pt.length>18&&COBpredBG<Nt&&(Nt=o(COBpredBG)),(yt||It>0)&&COBpredBG>Xt&&(Yt=COBpredBG),Mt&&bt.length>12&&UAMpredBG<Zt&&(Zt=o(UAMpredBG)),Mt&&UAMpredBG>Xt&&UAMpredBG}))}catch(e){console.error("Problem with iobArray. Optional feature Advanced Meal Assist disabled")}l.mealCOB&&(console.error("predCIs (mg/dL/5m):"+aa.join(" ")),console.error("remainingCIs: "+ta.join(" "))),Ue.predBGs={},ft.forEach((function(e,t,a){a[t]=o(Math.min(401,Math.max(39,e)))}));for(var ra=ft.length-1;ra>12&&ft[ra-1]===ft[ra];ra--)ft.pop();for(Ue.predBGs.IOB=ft,kt=o(ft[ft.length-1]),Bt.forEach((function(e,t,a){a[t]=o(Math.min(401,Math.max(39,e)))})),ra=Bt.length-1;ra>6&&!(Bt[ra-1]>=Bt[ra]||Bt[ra]<=Le);ra--)Bt.pop();if(Ue.predBGs.ZT=Bt,o(Bt[Bt.length-1]),l.mealCOB>0&&(_t>0||It>0)){for(pt.forEach((function(e,t,a){a[t]=o(Math.min(401,Math.max(39,e)))})),ra=pt.length-1;ra>12&&pt[ra-1]===pt[ra];ra--)pt.pop();Ue.predBGs.COB=pt,Et=o(pt[pt.length-1]),gt=Math.max(gt,o(pt[pt.length-1]))}if(_t>0||It>0){if(Mt){for(bt.forEach((function(e,t,a){a[t]=o(Math.min(401,Math.max(39,e)))})),ra=bt.length-1;ra>12&&bt[ra-1]===bt[ra];ra--)bt.pop();Ue.predBGs.UAM=bt,Lt=o(bt[bt.length-1]),bt[bt.length-1]&&(gt=Math.max(gt,o(bt[bt.length-1])))}Ue.eventualBG=gt}console.error("UAM Impact:"+xt+"mg/dL per 5m; UAM Duration:"+ea+"hours"),zt=Math.max(39,zt),Nt=Math.max(39,Nt),Zt=Math.max(39,Zt),qt=o(zt);var oa=l.mealCOB/l.carbs;Wt=o(Zt<999&&Nt<999?(1-oa)*UAMpredBG+oa*COBpredBG:Nt<999?(Vt+COBpredBG)/2:Zt<999?(Vt+UAMpredBG)/2:Vt),Qt>Wt&&(Wt=Qt),Ht=o(Ht=yt||It>0?Mt?oa*$t+(1-oa)*Jt:$t:Mt?Jt:Kt);var na=Zt;if(Qt<tt)na=(Zt+Qt)/2;else if(Qt<Le){var ia=(Qt-tt)/(Le-tt);na=(Zt+(Zt*ia+Qt*(1-ia)))/2}else Qt>Zt&&(na=(Zt+Qt)/2);if(na=o(na),l.carbs)if(!Mt&&Nt<999)qt=o(Math.max(zt,Nt));else if(Nt<999){var sa=oa*Nt+(1-oa)*na;qt=o(Math.max(zt,Nt,sa))}else qt=Mt?na:Ht;else Mt&&(qt=o(Math.max(zt,na)));qt=Math.min(qt,Wt),process.stderr.write("minPredBG: "+qt+" minIOBPredBG: "+zt+" minZTGuardBG: "+Qt),Nt<999&&process.stderr.write(" minCOBPredBG: "+Nt),Zt<999&&process.stderr.write(" minUAMPredBG: "+Zt),console.error(" avgPredBG:"+Wt+" COB/Carbs:"+l.mealCOB+"/"+l.carbs),Yt>Pe&&(qt=Math.min(qt,Yt)),Ue.COB=l.mealCOB,Ue.IOB=a.iob,Ue.BGI=n(ut,i),Ue.deviation=n(dt,i),Ue.ISF=n(nt,i),Ue.CR=o(i.carb_ratio,1),Ue.target_bg=n(Le,i),Ue.TDD=o(Q,2);var la=Ue.CR;we!=Ue.CR&&(la=we+"→"+Ue.CR);var ma=Ue.target_bg;Le!=b&&(ma=n(b,i)+"→"+Ue.target_bg),Ue.reason=rt+", COB: "+Ue.COB+", Dev: "+Ue.deviation+", BGI: "+Ue.BGI+", CR: "+la+", Target: "+ma+", minPredBG "+n(qt,i)+", minGuardBG "+n(Ht,i)+", IOBpredBG "+n(kt,i),Et>0&&(Ue.reason+=", COBpredBG "+n(Et,i)),Lt>0&&(Ue.reason+=", UAMpredBG "+n(Lt,i)),Ue.reason+=tddReason+_e,Ue.reason+="; ";var ua=ct;ua<40&&(ua=Math.min(Ht,ua));var da,ca=tt-ua,ga=240,ha=240;if(l.mealCOB>0&&(_t>0||It>0)){for(ra=0;ra<pt.length;ra++)if(pt[ra]<ze){ga=5*ra;break}for(ra=0;ra<pt.length;ra++)if(pt[ra]<tt){ha=5*ra;break}}else{for(ra=0;ra<ft.length;ra++)if(ft[ra]<ze){ga=5*ra;break}for(ra=0;ra<ft.length;ra++)if(ft[ra]<tt){ha=5*ra;break}}vt&&Ht<tt&&(console.error("minGuardBG "+n(Ht,i)+" projected below "+n(tt,i)+" - disabling SMB"),vt=!1),void 0===i.maxDelta_bg_threshold&&(da=.2),void 0!==i.maxDelta_bg_threshold&&(da=Math.min(i.maxDelta_bg_threshold,.4)),Ee>da*Pe&&(console.error("maxDelta "+n(Ee,i)+" > "+100*da+"% of BG "+n(Pe,i)+" - disabling SMB"),Ue.reason+="maxDelta "+n(Ee,i)+" > "+100*da+"% of BG "+n(Pe,i)+" - SMB disabled!, ",vt=!1),console.error("BG projected to remain above "+n(ze,i)+" for "+ga+"minutes"),(ha<240||ga<60)&&console.error("BG projected to remain above "+n(tt,i)+" for "+ha+"minutes");var pa=ha,fa=i.current_basal*nt*pa/60,ba=Math.max(0,l.mealCOB-.25*l.carbs),Ba=(ca-fa)/csf-ba;fa=o(fa),Ba=o(Ba),console.error("naive_eventualBG:",ct,"bgUndershoot:",ca,"zeroTempDuration:",pa,"zeroTempEffect:",fa,"carbsReq:",Ba),"Could not parse clock data"==l.reason?console.error("carbsReq unknown: Could not parse clock data"):Ba>=i.carbsReqThreshold&&ha<=45&&(Ue.carbsReq=Ba,Ue.reason+=Ba+" add'l carbs req w/in "+ha+"m; ");var va=0;if(Pe<tt&&a.iob<20*-i.current_basal/60&&We>0&&We>ht)Ue.reason+="IOB "+a.iob+" < "+o(20*-i.current_basal/60,2),Ue.reason+=" and minDelta "+n(We,i)+" > expectedDelta "+n(ht,i)+"; ";else if(Pe<tt||Ht<tt)return Ue.reason+="minGuardBG "+n(Ht,i)+"<"+n(tt,i),va=o(60*((ca=Le-Ht)/nt)/i.current_basal),va=30*o(va/30),va=Math.min(120,Math.max(30,va)),m.setTempBasal(0,va,i,Ue,t);if(i.skip_neutral_temps&&Ue.deliverAt.getMinutes()>=55)return Ue.reason+="; Canceling temp at "+Ue.deliverAt.getMinutes()+"m past the hour. ",m.setTempBasal(0,0,i,Ue,t);var Ma=0,_a=Oe;if(gt<ze){if(Ue.reason+="Eventual BG "+n(gt,i)+" < "+n(ze,i),We>ht&&We>0&&!Ba)return ct<40?(Ue.reason+=", naive_eventualBG < 40. ",m.setTempBasal(0,30,i,Ue,t)):(e.delta>We?Ue.reason+=", but Delta "+n(Ie,i)+" > expectedDelta "+n(ht,i):Ue.reason+=", but Min. Delta "+We.toFixed(2)+" > Exp. Delta "+n(ht,i),t.duration>15&&r(Oe,i)===r(t.rate,i)?(Ue.reason+=", temp "+t.rate+" ~ req "+Oe+"U/hr. ",Ue):(Ue.reason+="; setting current basal of "+Oe+" as temp. ",m.setTempBasal(Oe,30,i,Ue,t)));Ma=o(Ma=2*Math.min(0,(gt-Le)/nt),2);var ya=Math.min(0,(ct-Le)/nt);if(ya=o(ya,2),We<0&&We>ht)Ma=o(Ma*(We/ht),2);if(_a=r(_a=Oe+2*Ma,i),t.duration*(t.rate-Oe)/60<Math.min(Ma,ya)-.3*Oe)return Ue.reason+=", "+t.duration+"m@"+t.rate.toFixed(2)+" is a lot less than needed. ",m.setTempBasal(_a,30,i,Ue,t);if(void 0!==t.rate&&t.duration>5&&_a>=.8*t.rate)return Ue.reason+=", temp "+t.rate+" ~< req "+_a+"U/hr. ",Ue;if(_a<=0){if((va=o(60*((ca=Le-ct)/nt)/i.current_basal))<0?va=0:(va=30*o(va/30),va=Math.min(120,Math.max(0,va))),va>0)return Ue.reason+=", setting "+va+"m zero temp. ",m.setTempBasal(_a,va,i,Ue,t)}else Ue.reason+=", setting "+_a+"U/hr. ";return m.setTempBasal(_a,30,i,Ue,t)}if(We<ht&&(!u||!vt))return e.delta<We?Ue.reason+="Eventual BG "+n(gt,i)+" > "+n(ze,i)+" but Delta "+n(Ie,i)+" < Exp. Delta "+n(ht,i):Ue.reason+="Eventual BG "+n(gt,i)+" > "+n(ze,i)+" but Min. Delta "+We.toFixed(2)+" < Exp. Delta "+n(ht,i),t.duration>15&&r(Oe,i)===r(t.rate,i)?(Ue.reason+=", temp "+t.rate+" ~ req "+Oe+"U/hr. ",Ue):(Ue.reason+="; setting current basal of "+Oe+" as temp. ",m.setTempBasal(Oe,30,i,Ue,t));if(Math.min(gt,qt)<Ne&&(!u||!vt))return Ue.reason+=n(gt,i)+"-"+n(qt,i)+" in range: no temp required",t.duration>15&&r(Oe,i)===r(t.rate,i)?(Ue.reason+=", temp "+t.rate+" ~ req "+Oe+"U/hr. ",Ue):(Ue.reason+="; setting current basal of "+Oe+" as temp. ",m.setTempBasal(Oe,30,i,Ue,t));if(gt>=Ne&&(Ue.reason+="Eventual BG "+n(gt,i)+" >= "+n(Ne,i)+", "),a.iob>He)return Ue.reason+="IOB "+o(a.iob,2)+" > max_iob "+He,t.duration>15&&r(Oe,i)===r(t.rate,i)?(Ue.reason+=", temp "+t.rate+" ~ req "+Oe+"U/hr. ",Ue):(Ue.reason+="; setting current basal of "+Oe+" as temp. ",m.setTempBasal(Oe,30,i,Ue,t));(Ma=o((Math.min(qt,gt)-Le)/nt,2))>He-a.iob?(console.error("SMB limited by maxIOB: "+He-a.iob+" (. insulinReq: "+Ma+" U)"),Ue.reason+="max_iob "+He+", ",Ma=He-a.iob):console.error("SMB not limited by maxIOB ( insulinReq: "+Ma+" U)."),_a=r(_a=Oe+2*Ma,i),Ma=o(Ma,3),Ue.insulinReq=Ma;var xa=o((new Date(Ae).getTime()-a.lastBolusTime)/6e4,1);if(u&&vt&&Pe>tt){var wa=o(l.mealCOB/i.carb_ratio,3),Da=0;void 0===i.maxSMBBasalMinutes?(Da=o(30*i.current_basal/60,1),console.error("profile.maxSMBBasalMinutes undefined: defaulting to 30m"),Ma>Da&&console.error("SMB limited by maxBolus: "+Da+" ( "+Ma+" U)")):a.iob>wa&&a.iob>0?(console.error("IOB"+a.iob+"> COB"+l.mealCOB+"; mealInsulinReq ="+wa),i.maxUAMSMBBasalMinutes?(console.error("profile.maxUAMSMBBasalMinutes: "+i.maxUAMSMBBasalMinutes+", profile.current_basal: "+i.current_basal),Da=o(i.current_basal*i.maxUAMSMBBasalMinutes/60,1)):(console.error("profile.maxUAMSMBBasalMinutes undefined: defaulting to 30m"),Da=o(30*i.current_basal/60,1)),Ma>Da?console.error("SMB limited by maxUAMSMBBasalMinutes [ "+i.maxUAMSMBBasalMinutes+"m ]: "+Da+"U ( "+Ma+"U )"):console.error("SMB is not limited by maxUAMSMBBasalMinutes. ( insulinReq: "+Ma+"U )")):(console.error("profile.maxSMBBasalMinutes: "+i.maxSMBBasalMinutes+", profile.current_basal: "+i.current_basal),Ma>(Da=o(i.current_basal*i.maxSMBBasalMinutes/60,1))?console.error("SMB limited by maxSMBBasalMinutes: "+i.maxSMBBasalMinutes+"m ]: "+Da+"U ( insulinReq: "+Ma+"U )"):console.error("SMB is not limited by maxSMBBasalMinutes. ( insulinReq: "+Ma+"U )"));var Sa=i.bolus_increment,Ta=1/Sa,Ca=i.smb_delivery_ratio;Ca>.5&&console.error("SMB Delivery Ratio increased from default 0.5 to "+o(Ca,2));var Ua=Math.min(Ma*Ca,Da);Ua=Math.floor(Ua*Ta)/Ta,va=o(60*((Le-(ct+zt)/2)/nt)/i.current_basal),Ma>0&&Ua<Sa&&(va=0);var Ga=0;va<=0?va=0:va>=30?(va=30*o(va/30),va=Math.min(60,Math.max(0,va))):(Ga=o(Oe*va/30,2),va=30),Ue.reason+=" insulinReq "+Ma,Ua>=Da&&(Ue.reason+="; maxBolus "+Da),va>0&&(Ue.reason+="; setting "+va+"m low temp of "+Ga+"U/h"),Ue.reason+=". ";var Ra=3;i.SMBInterval&&(Ra=Math.min(10,Math.max(1,i.SMBInterval)));var Oa=o(Ra-xa,0),Aa=o(60*(Ra-xa),0)%60;if(console.error("naive_eventualBG "+ct+","+va+"m "+Ga+"U/h temp needed; last bolus "+xa+"m ago; maxBolus: "+Da),xa>Ra?Ua>0&&(Ue.units=Ua,Ue.reason+="Microbolusing "+Ua+"U. "):Ue.reason+="Waiting "+Oa+"m "+Aa+"s to microbolus again. ",va>0)return Ue.rate=Ga,Ue.duration=va,Ue}var Ia=m.getMaxSafeBasal(i);return _a>Ia&&(Ue.reason+="adj. req. rate: "+_a+" to maxSafeBasal: "+o(Ia,2)+", ",_a=r(Ia,i)),t.duration*(t.rate-Oe)/60>=2*Ma?(Ue.reason+=t.duration+"m@"+t.rate.toFixed(2)+" > 2 * insulinReq. Setting temp basal of "+_a+"U/hr. ",m.setTempBasal(_a,30,i,Ue,t)):void 0===t.duration||0===t.duration?(Ue.reason+="no temp, setting "+_a+"U/hr. ",m.setTempBasal(_a,30,i,Ue,t)):t.duration>5&&r(_a,i)<=r(t.rate,i)?(Ue.reason+="temp "+t.rate+" >~ req "+_a+"U/hr. ",Ue):(Ue.reason+="temp "+t.rate+"<"+_a+"U/hr. ",m.setTempBasal(_a,30,i,Ue,t))}},6880:(e,t,a)=>{var r=a(6654);e.exports=function(e,t){var a=20;void 0!==t&&"string"==typeof t.model&&(r(t.model,"54")||r(t.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,t,a)=>{var r=a(5639).Symbol;e.exports=r},9932:e=>{e.exports=function(e,t){for(var a=-1,r=null==e?0:e.length,o=Array(r);++a<r;)o[a]=t(e[a],a,e);return o}},9750:e=>{e.exports=function(e,t,a){return e==e&&(void 0!==a&&(e=e<=a?e:a),void 0!==t&&(e=e>=t?e:t)),e}},4239:(e,t,a)=>{var r=a(2705),o=a(9607),n=a(2333),i=r?r.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,t,a)=>{var r=a(2705),o=a(9932),n=a(1469),i=a(3448),s=r?r.prototype:void 0,l=s?s.toString:void 0;e.exports=function e(t){if("string"==typeof t)return t;if(n(t))return o(t,e)+"";if(i(t))return l?l.call(t):"";var a=t+"";return"0"==a&&1/t==-Infinity?"-0":a}},7561:(e,t,a)=>{var r=a(7990),o=/^\s+/;e.exports=function(e){return e?e.slice(0,r(e)+1).replace(o,""):e}},1957:(e,t,a)=>{var r="object"==typeof a.g&&a.g&&a.g.Object===Object&&a.g;e.exports=r},9607:(e,t,a)=>{var r=a(2705),o=Object.prototype,n=o.hasOwnProperty,i=o.toString,s=r?r.toStringTag:void 0;e.exports=function(e){var t=n.call(e,s),a=e[s];try{e[s]=void 0;var r=!0}catch(e){}var o=i.call(e);return r&&(t?e[s]=a:delete e[s]),o}},2333:e=>{var t=Object.prototype.toString;e.exports=function(e){return t.call(e)}},5639:(e,t,a)=>{var r=a(1957),o="object"==typeof self&&self&&self.Object===Object&&self,n=r||o||Function("return this")();e.exports=n},7990:e=>{var t=/\s/;e.exports=function(e){for(var a=e.length;a--&&t.test(e.charAt(a)););return a}},6654:(e,t,a)=>{var r=a(9750),o=a(531),n=a(554),i=a(9833);e.exports=function(e,t,a){e=i(e),t=o(t);var s=e.length,l=a=void 0===a?s:r(n(a),0,s);return(a-=t.length)>=0&&e.slice(a,l)==t}},1469:e=>{var t=Array.isArray;e.exports=t},3218:e=>{e.exports=function(e){var t=typeof e;return null!=e&&("object"==t||"function"==t)}},7005:e=>{e.exports=function(e){return null!=e&&"object"==typeof e}},3448:(e,t,a)=>{var r=a(4239),o=a(7005);e.exports=function(e){return"symbol"==typeof e||o(e)&&"[object Symbol]"==r(e)}},8601:(e,t,a)=>{var r=a(4841),o=1/0;e.exports=function(e){return e?(e=r(e))===o||e===-1/0?17976931348623157e292*(e<0?-1:1):e==e?e:0:0===e?e:0}},554:(e,t,a)=>{var r=a(8601);e.exports=function(e){var t=r(e),a=t%1;return t==t?a?t-a:t:0}},4841:(e,t,a)=>{var r=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 t="function"==typeof e.valueOf?e.valueOf():e;e=o(t)?t+"":t}if("string"!=typeof e)return 0===e?e:+e;e=r(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,t,a)=>{var r=a(531);e.exports=function(e){return null==e?"":r(e)}}},t={};function a(r){var o=t[r];if(void 0!==o)return o.exports;var n=t[r]={exports:{}};return e[r](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 r=a(5546);freeaps_determineBasal=r})();
|