| 1 |
- var trio_determineBasal;(()=>{var e={2982:(e,r,t)=>{var a=t(3531);function o(e,r){r||(r=0);var t=Math.pow(10,r);return Math.round(Math.round(e*t*1e7)/1e7)/t}function n(e,r){return"mmol/L"===r.out_units?o(.0555*e,1):Math.round(e)}e.exports=function(e,r,t,i,s,l,d,u,m,c,g,h,v,B,b){var p=i.min_bg,M=B.overrideTarget;0!=M&&6!=M&&B.useOverride&&!i.temptargetSet&&(p=M);const f=B.smbIsOff,_=B.advancedSettings,x=B.isfAndCr,S=B.isf,y=B.cr,G=B.smbMinutes,w=B.uamMinutes;let C=B.currentTDD;var O="",U="",D=h.useNewFormula,T=0,A=p,I=new Date;c&&(I=new Date(c));const F=B.weightedAverage;var R=1,j=i.sens,E=i.carb_ratio;B.useOverride&&(R=B.overridePercentage/100,x?(j/=R,E/=R):(y&&(E/=R),S&&(j/=R)));const q=i.weightPercentage,W=B.average_total_data,P=Math.min(i.autosens_min,i.autosens_max),k=Math.max(i.autosens_min,i.autosens_max);(k==P||k<1||P>1)&&(D=!1,console.log("Dynamic ISF disabled due to current autosens settings"));const L=e.glucose,z=h.adjustmentFactor,H=h.adjustmentFactorSigmoid,N=h.sigmoid,Z=p;var $,J=!1,K="",Q=1;W>0&&(Q=F/W),Q>1?(Q=o(Q=Math.min(Q,i.autosens_max),2),i.autosens_max):Q<1&&(Q=o(Q=Math.max(Q,i.autosens_min),2),i.autosens_min),$=", Basal ratio: "+Q,(i.high_temptarget_raises_sensitivity||i.exercise_mode)&&(J=!0),Z>=118&&J&&(D=!1,K="Dynamic ISF temporarily off due to a high temp target/exercising. Current min target: "+Z);var V=", Dynamic ratios log: ",X=", AF: "+(N?H:z),Y="BG: "+L+" mg/dl ("+(.0555*L).toPrecision(2)+" mmol/l)",ee="",re="";const te=h.curve,ae=i.insulinPeakTime,oe=h.useCustomPeakTime;var ne=55,ie=65;switch(te){case"rapid-acting":ie=65;break;case"ultra-rapid":ie=50}oe?(ne=120-ae,console.log("Custom insulinpeakTime set to :"+ae+", insulinFactor: "+ne)):(ne=120-ie,console.log("insulinFactor set to : "+ne)),tdd_before=C,q<1&&F>1&&(C=F,console.log("Using weighted TDD average: "+o(C,2)+" U, instead of past 24 h ("+o(tdd_before,2)+" U), weight: "+q),re=", Weighted TDD: "+o(C,2)+" U");var se="";if(D)if(N){const e=P,r=k-e;var le=k-1;1==k&&(le=k+.01-1);const t=.0555*(L-p)*H*Q+Math.log10(1/le-e/le)/Math.log10(Math.E);de=r/(1+Math.exp(-t))+e,ee=", Sigmoid function"}else{var de=j*z*C*Math.log(L/ne+1)/1800;ee=", Logarithmic formula"}var ue="",me="";if(D&&C>0){ue=", Dynamic ISF: On",de>k?(K=", Dynamic ISF limited by autosens_max setting: "+k+" ("+o(de,2)+"), ",me=", Autosens/Dynamic Limit: "+k+" ("+o(de,2)+")",de=k):de<P&&(K=", Dynamic ISF limited by autosens_min setting: "+P+" ("+o(de,2)+"). ",me=", Autosens/Dynamic Limit: "+P+" ("+o(de,2)+")",de=P);const e=j/de;s.ratio=de,se=". 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).",O+=V+Y+X+ee+(K+=N?se:", Dynamic autosens.ratio set to "+o(de,2)+" with ISF: "+e.toPrecision(3)+" mg/dl/U ("+(.0555*e).toPrecision(3)+" mmol/l/U)")+ue+re}else O+=V+"Dynamic Settings disabled";console.log(O),D?D&&i.tddAdjBasal?U+=ue+ee+me+X+$:D&&!i.tddAdjBasal&&(U+=ue+ee+me+X):U+="",.5!==i.smb_delivery_ratio&&(U+=", SMB Ratio: "+Math.min(i.smb_delivery_ratio,1)),""!==b&&"Nothing changed"!==b&&(U+=", Middleware: "+b);var ce={},ge=new Date(I);if(void 0===i||void 0===i.current_basal)return ce.error="Error: could not get current basal rate",ce;var he=a(i.current_basal,i)*R,ve=he;B.useOverride&&(0==B.duration?console.log("Profile Override is active. Override "+o(100*R,0)+"%. Override Duration: Enabled indefinitely"):console.log("Profile Override is active. Override "+o(100*R,0)+"%. Override Expires in: "+B.duration+" min."));var Be,be=new Date(e.date),pe=o((I-be)/60/1e3,1),Me=e.glucose,fe=e.noise;Be=e.delta>-.5?"+"+o(e.delta,0):o(e.delta,0);var _e=Math.min(e.delta,e.short_avgdelta),xe=Math.min(e.short_avgdelta,e.long_avgdelta),Se=Math.max(e.delta,e.short_avgdelta,e.long_avgdelta);if((Me<=10||38===Me||fe>=3)&&(ce.reason="CGM is calibrating, in ??? state, or noise is high"),Me>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(Me,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")),pe>12||pe<-5?ce.reason="If current system time "+I+" is correct, then BG data is too old. The last BG data was read "+pe+"m ago at "+be:0===e.short_avgdelta&&0===e.long_avgdelta&&(e.last_cal&&e.last_cal<3?ce.reason="CGM was just calibrated":ce.reason="CGM data is unchanged ("+n(Me,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"),Me<=10||38===Me||fe>=3||pe>12||pe<-5||0===e.short_avgdelta&&0===e.long_avgdelta)return r.rate>=ve?(ce.reason+=". Canceling high temp basal of "+r.rate,ce.deliverAt=ge,ce.temp="absolute",ce.duration=0,ce.rate=0,ce):0===r.rate&&r.duration>30?(ce.reason+=". Shortening "+r.duration+"m long zero temp to 30m. ",ce.deliverAt=ge,ce.temp="absolute",ce.duration=30,ce.rate=0,ce):(ce.reason+=". Temp "+r.rate+" <= current basal "+ve+"U/hr; doing nothing. ",ce);var ye,Ge,we,Ce,Oe=i.max_iob;if(void 0!==p&&(Ge=p),void 0!==i.max_bg&&(we=p),void 0!==i.enableSMB_high_bg_target&&(Ce=i.enableSMB_high_bg_target),void 0===p)return ce.error="Error: could not determine target_bg. ",ce;ye=p;var Ue,De=i.exercise_mode||i.high_temptarget_raises_sensitivity,Te=100;if(Ue=i.half_basal_exercise_target,De&&i.temptargetSet&&ye>Te||i.low_temptarget_lowers_sensitivity&&i.temptargetSet&&ye<Te){var Ae=Ue-Te;sensitivityRatio=Ae*(Ae+ye-Te)<=0?i.autosens_max:Ae/(Ae+ye-Te),sensitivityRatio=Math.min(sensitivityRatio,i.autosens_max),sensitivityRatio=o(sensitivityRatio,2),process.stderr.write("Sensitivity ratio set to "+sensitivityRatio+" based on temp target of "+ye+"; ")}else void 0!==s&&s&&(sensitivityRatio=s.ratio,0===M||6===M||M===i.min_bg||i.temptargetSet||(ye=M,console.log("Current Override Profile Target: "+n(M,i)+" "+i.out_units)),process.stderr.write("Autosens ratio: "+sensitivityRatio+"; "));if(i.temptargetSet&&ye<Te&&D&&L>=ye&&sensitivityRatio<de&&(s.ratio=de*(Te/ye),s.ratio=Math.min(s.ratio,i.autosens_max),sensitivityRatio=o(s.ratio,2),console.log("Dynamic ratio increased from "+o(de,2)+" to "+o(s.ratio,2)+" due to a low temp target ("+ye+").")),sensitivityRatio&&!D?(ve=i.current_basal*R*sensitivityRatio,(ve=a(ve,i))!==he?process.stderr.write("Adjusting basal from "+he+" to "+ve+"; "):process.stderr.write("Basal unchanged: "+ve+"; ")):D&&i.tddAdjBasal&&(ve=i.current_basal*Q*R,ve=a(ve,i),W>0&&(process.stderr.write("TDD-adjustment of basals activated, using tdd24h_14d_Ratio "+o(Q,2)+", TDD 24h = "+o(tdd_before,2)+"U, Weighted average TDD = "+o(F,2)+"U, (Weight percentage = "+q+"), Total data of TDDs (up to 14 days) average = "+o(W,2)+"U. "),ve!==he*R?process.stderr.write("Adjusting basal from "+he*R+" U/h to "+ve+" U/h; "):process.stderr.write("Basal unchanged: "+ve+" U/h; "))),i.temptargetSet);else if(void 0!==s&&s&&(i.sensitivity_raises_target&&s.ratio<1||i.resistance_lowers_target&&s.ratio>1)){Ge=o((Ge-60)/s.ratio)+60,we=o((we-60)/s.ratio)+60;var Ie=o((ye-60)/s.ratio)+60;ye===(Ie=Math.max(80,Ie))?process.stderr.write("target_bg unchanged: "+n(Ie,i)+"; "):process.stderr.write("target_bg from "+n(Ie,i)+" to "+n(Ie,i)+"; "),ye=Ie}var Fe=n(ye,i);ye!=p&&(Fe=0!==M&&6!==M&&M!==ye?n(p,i)+"→"+n(M,i)+"→"+n(ye,i):n(p,i)+"→"+n(ye,i));var Re=200,je=200,Ee=200;if(e.noise>=2){var qe=Math.max(1.1,i.noisyCGMTargetMultiplier);Math.min(250,i.maxRaw),Re=o(Math.min(200,Ge*qe)),je=o(Math.min(200,ye*qe)),Ee=o(Math.min(200,we*qe)),process.stderr.write("Raising target_bg for noisy / raw CGM data, from "+n(Ie,i)+" to "+n(je,i)+"; "),Ge=Re,ye=je,we=Ee}A=Ge-.5*(Ge-40),A=Math.min(Math.max(i.threshold_setting,A,60),120),console.error(`Threshold set to ${n(A,i)}`);var We="",Pe=(o(j,1),j);if(void 0!==s&&s&&((Pe=o(Pe=j/sensitivityRatio,1))!==j?process.stderr.write("ISF from "+n(j,i)+" to "+n(Pe,i)):process.stderr.write("ISF unchanged: "+n(Pe,i)),We+="Autosens ratio: "+o(sensitivityRatio,2)+", ISF: "+n(j,i)+"→"+n(Pe,i)),console.error("CR:"+E),void 0===t)return ce.error="Error: iob_data undefined. ",ce;var ke,Le=t;if(t.length,t.length>1&&(t=Le[0]),void 0===t.activity||void 0===t.iob)return ce.error="Error: iob_data missing some property. ",ce;var ze=((ke=void 0!==t.lastTemp?o((new Date(I).getTime()-t.lastTemp.date)/6e4):0)+r.duration)%30;if(console.error("currenttemp:"+r.rate+" lastTempAge:"+ke+"m, tempModulus:"+ze+"m"),ce.temp="absolute",ce.deliverAt=ge,u&&r&&t.lastTemp&&r.rate!==t.lastTemp.rate&&ke>10&&r.duration)return ce.reason="Warning: currenttemp rate "+r.rate+" != lastTemp rate "+t.lastTemp.rate+" from pumphistory; canceling temp",d.setTempBasal(0,0,i,ce,r);if(r&&t.lastTemp&&r.duration>0){var He=ke-t.lastTemp.duration;if(He>5&&ke>10)return ce.reason="Warning: currenttemp running but lastTemp from pumphistory ended "+He+"m ago; canceling temp",d.setTempBasal(0,0,i,ce,r)}var Ne=o(-t.activity*Pe*5,5),Ze=o(Ne,2),$e=o(6*(_e-Ze));$e<0&&($e=o(6*(xe-Ze)))<0&&($e=o(6*(e.long_avgdelta-Ze)));var Je,Ke=(Je=t.iob>0?o(Me-t.iob*Pe):o(Me-t.iob*Math.min(Pe,j)))+$e;if(void 0===Ke||isNaN(Ke))return ce.error="Error: could not calculate eventualBG. Sensitivity: "+Pe+" Deviation: "+$e,ce;var Qe,Ve=function(e,r,t){return o(t+(e-r)/24,1)}(ye,Ke,Ze);ce={temp:"absolute",bg:Me,tick:Be,eventualBG:Ke,insulinReq:0,reservoir:m,deliverAt:ge,sensitivityRatio,CR:o(E,1),current_target:ye,insulinForManualBolus:T,manualBolusErrorString:0,minDelta:_e,expectedDelta:Ve,minGuardBG:Qe,minPredBG:_r,threshold:n(A,i)};var Xe=[],Ye=[],er=[],rr=[];Xe.push(Me),Ye.push(Me),rr.push(Me),er.push(Me);let tr=!1;f?(console.error("SMBs are always off."),tr=!1):tr=function(e,r,t,a,o,i,s,l){if(s.smbIsScheduledOff){let e=new Date(l.getHours()),r=s.start,t=s.end;if(r<t&&e>=r&&e<t)return console.error("SMB disabled: current time is in SMB disabled scheduled"),!1;if(r>t&&(e>=r||e<t))return console.error("SMB disabled: current time is in SMB disabled scheduled"),!1;if(0==r&&0==t)return console.error("SMB disabled: current time is in SMB disabled scheduled"),!1;if(r==t&&e==r)return console.error("SMB disabled: current time is in SMB disabled scheduled"),!1}return r?!e.allowSMB_with_high_temptarget&&e.temptargetSet&&o>100?(console.error("SMB disabled due to high temptarget of "+o),!1):!0===t.bwFound&&!1===e.A52_risk_enable?(console.error("SMB disabled due to Bolus Wizard activity in the last 6 hours."),!1):s.shouldProtectDueToHIGH?(console.error("Invalid CGM (HIGH). SMBs disabled."),!1):!0===e.enableSMB_always?(t.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&&t.mealCOB?(t.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 "+t.mealCOB),!0):!0===e.enableSMB_after_carbs&&t.carbs?(t.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?(t.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&&a>=i?(console.error("Checking BG to see if High for SMB enablement."),console.error("Current BG",a," | High BG ",i),t.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,Me,ye,Ce,B,I);var ar,or=i.enableUAM,nr=0;nr=o(_e-Ze,1);var ir=o(_e-Ze,1);csf=Pe/E,console.error("profile.sens:"+n(j,i)+", sens:"+n(Pe,i)+", CSF:"+o(csf,1));var sr=o(30*csf*5/60,1);nr>sr&&(console.error("Limiting carb impact from "+nr+" to "+sr+"mg/dL/5m (30g/h)"),nr=sr);var lr=3;sensitivityRatio&&(lr/=sensitivityRatio);var dr=lr;if(l.carbs){lr=Math.max(lr,l.mealCOB/20);var ur=o((new Date(I).getTime()-l.lastCarbTime)/6e4),mr=(l.carbs-l.mealCOB)/l.carbs;dr=o(dr=lr+1.5*ur/60,1),console.error("Last carbs "+ur+" minutes ago; remainingCATime:"+dr+"hours; "+o(100*mr,1)+"% carbs absorbed")}var cr=Math.max(0,nr/5*60*dr/2)/csf,gr=90,hr=1;i.remainingCarbsCap&&(gr=Math.min(90,i.remainingCarbsCap)),i.remainingCarbsFraction&&(hr=Math.min(1,i.remainingCarbsFraction));var vr=1-hr,Br=Math.max(0,l.mealCOB-cr-l.carbs*vr),br=(Br=Math.min(gr,Br))*csf*5/60/(dr/2);br=Math.round(1e7*br)/1e7;var pr=o(l.slopeFromMaxDeviation,2),Mr=o(l.slopeFromMinDeviation,2),fr=Math.min(pr,-Mr/3);ar=0===nr?0:Math.min(60*dr/5/2,Math.max(0,l.mealCOB*csf/nr)),console.error("Carb Impact:"+nr+"mg/dL per 5m; CI Duration:"+o(5*ar/60*2,1)+"hours; remaining CI ("+dr/2+"h peak):"+o(br,1)+"mg/dL per 5m");var _r,xr,Sr,yr,Gr,wr=999,Cr=999,Or=999,Ur=999,Dr=999,Tr=999,Ar=999,Ir=Ke,Fr=Me,Rr=Me,jr=0,Er=[],qr=[];try{Le.forEach((function(e){var r=o(-e.activity*Pe*5,2),t=o(-e.iobWithZeroTemp.activity*Pe*5,2),a=Je,n=nr*(1-Math.min(1,Ye.length/12));!0===(D&&!N)?(Ir=Ye[Ye.length-1]+o(-e.activity*(1800/(C*z*Math.log(Math.max(Ye[Ye.length-1],39)/ne+1)))*5,2)+n,a=rr[rr.length-1]+o(-e.iobWithZeroTemp.activity*(1800/(C*z*Math.log(Math.max(rr[rr.length-1],39)/ne+1)))*5,2),console.log("Dynamic ISF (Logarithmic Formula) )adjusted predictions for IOB and ZT: IOBpredBG: "+o(Ir,2)+" , ZTpredBG: "+o(a,2))):(Ir=Ye[Ye.length-1]+r+n,a=rr[rr.length-1]+t);var i=Math.max(0,Math.max(0,nr)*(1-Xe.length/Math.max(2*ar,1))),s=Math.min(Xe.length,12*dr-Xe.length),l=Math.max(0,s/(dr/2*12)*br);Er.push(o(l,0)),qr.push(o(i,0)),COBpredBG=Xe[Xe.length-1]+r+Math.min(0,n)+i+l;var d=Math.max(0,ir+er.length*fr),u=Math.max(0,ir*(1-er.length/Math.max(36,1))),m=Math.min(d,u);m>0&&(jr=o(5*(er.length+1)/60,1)),!0===(D&&!N)?(UAMpredBG=er[er.length-1]+o(-e.activity*(1800/(C*z*Math.log(Math.max(er[er.length-1],39)/ne+1)))*5,2)+Math.min(0,n)+m,console.log("Dynamic ISF (Logarithmic Formula) adjusted prediction for UAM: UAMpredBG: "+o(UAMpredBG,2))):UAMpredBG=er[er.length-1]+r+Math.min(0,n)+m,Ye.length<48&&Ye.push(Ir),Xe.length<48&&Xe.push(COBpredBG),er.length<48&&er.push(UAMpredBG),rr.length<48&&rr.push(a),COBpredBG<Ur&&(Ur=o(COBpredBG)),UAMpredBG<Dr&&(Dr=o(UAMpredBG)),Ir<Tr&&(Tr=o(Ir)),a<Ar&&(Ar=o(a)),Ye.length>18&&Ir<wr&&(wr=o(Ir)),Ir>Fr&&(Fr=Ir),(ar||br>0)&&Xe.length>18&&COBpredBG<Cr&&(Cr=o(COBpredBG)),(ar||br>0)&&COBpredBG>Fr&&(Rr=COBpredBG),or&&er.length>12&&UAMpredBG<Or&&(Or=o(UAMpredBG)),or&&UAMpredBG>Fr&&UAMpredBG}))}catch(e){console.error("Problem with iobArray. Optional feature Advanced Meal Assist disabled")}l.mealCOB&&(console.error("predCIs (mg/dL/5m):"+qr.join(" ")),console.error("remainingCIs: "+Er.join(" "))),ce.predBGs={},Ye.forEach((function(e,r,t){t[r]=o(Math.min(401,Math.max(39,e)))}));for(var Wr=Ye.length-1;Wr>12&&Ye[Wr-1]===Ye[Wr];Wr--)Ye.pop();for(ce.predBGs.IOB=Ye,Sr=o(Ye[Ye.length-1]),rr.forEach((function(e,r,t){t[r]=o(Math.min(401,Math.max(39,e)))})),Wr=rr.length-1;Wr>6&&!(rr[Wr-1]>=rr[Wr]||rr[Wr]<=ye);Wr--)rr.pop();if(ce.predBGs.ZT=rr,o(rr[rr.length-1]),l.mealCOB>0&&(nr>0||br>0)){for(Xe.forEach((function(e,r,t){t[r]=o(Math.min(1500,Math.max(39,e)))})),Wr=Xe.length-1;Wr>12&&Xe[Wr-1]===Xe[Wr];Wr--)Xe.pop();ce.predBGs.COB=Xe,yr=o(Xe[Xe.length-1]),Ke=Math.max(Ke,o(Xe[Xe.length-1])),console.error("COBpredBG: "+o(Xe[Xe.length-1]))}if(nr>0||br>0){if(or){for(er.forEach((function(e,r,t){t[r]=o(Math.min(401,Math.max(39,e)))})),Wr=er.length-1;Wr>12&&er[Wr-1]===er[Wr];Wr--)er.pop();ce.predBGs.UAM=er,Gr=o(er[er.length-1]),er[er.length-1]&&(Ke=Math.max(Ke,o(er[er.length-1])))}ce.eventualBG=Ke}console.error("UAM Impact:"+ir+"mg/dL per 5m; UAM Duration:"+jr+"hours"),wr=Math.max(39,wr),Cr=Math.max(39,Cr),Or=Math.max(39,Or),_r=o(wr);var Pr=l.mealCOB/l.carbs;xr=o(Or<999&&Cr<999?(1-Pr)*UAMpredBG+Pr*COBpredBG:Cr<999?(Ir+COBpredBG)/2:Or<999?(Ir+UAMpredBG)/2:Ir),Ar>xr&&(xr=Ar),Qe=o(Qe=ar||br>0?or?Pr*Ur+(1-Pr)*Dr:Ur:or?Dr:Tr);var kr=Or;if(Ar<A)kr=(Or+Ar)/2;else if(Ar<ye){var Lr=(Ar-A)/(ye-A);kr=(Or+(Or*Lr+Ar*(1-Lr)))/2}else Ar>Or&&(kr=(Or+Ar)/2);if(kr=o(kr),l.carbs)if(!or&&Cr<999)_r=o(Math.max(wr,Cr));else if(Cr<999){var zr=Pr*Cr+(1-Pr)*kr;_r=o(Math.max(wr,Cr,zr))}else _r=or?kr:Qe;else or&&(_r=o(Math.max(wr,kr)));_r=Math.min(_r,xr),process.stderr.write("minPredBG: "+_r+" minIOBPredBG: "+wr+" minZTGuardBG: "+Ar),Cr<999&&process.stderr.write(" minCOBPredBG: "+Cr),Or<999&&process.stderr.write(" minUAMPredBG: "+Or),console.error(" avgPredBG:"+xr+" COB/Carbs:"+l.mealCOB+"/"+l.carbs),Rr>Me&&(_r=Math.min(_r,Rr)),ce.COB=l.mealCOB,ce.IOB=t.iob,ce.BGI=n(Ze,i),ce.deviation=n($e,i),ce.ISF=n(Pe,i),ce.CR=o(E,1),ce.target_bg=n(ye,i),ce.current_target=o(ye,0),ce.reason=We+", COB: "+ce.COB+", Dev: "+ce.deviation+", BGI: "+ce.BGI+", CR: "+ce.CR+", Target: "+Fe+", minPredBG "+n(_r,i)+", minGuardBG "+n(Qe,i)+", IOBpredBG "+n(Sr,i),yr>0&&(ce.reason+=", COBpredBG "+n(yr,i)),Gr>0&&(ce.reason+=", UAMpredBG "+n(Gr,i)),ce.reason+=U,ce.reason+="; ";var Hr=Je;Hr<40&&(Hr=Math.min(Qe,Hr));var Nr,Zr=A-Hr,$r=240,Jr=240;if(l.mealCOB>0&&(nr>0||br>0)){for(Wr=0;Wr<Xe.length;Wr++)if(Xe[Wr]<Ge){$r=5*Wr;break}for(Wr=0;Wr<Xe.length;Wr++)if(Xe[Wr]<A){Jr=5*Wr;break}}else{for(Wr=0;Wr<Ye.length;Wr++)if(Ye[Wr]<Ge){$r=5*Wr;break}for(Wr=0;Wr<Ye.length;Wr++)if(Ye[Wr]<A){Jr=5*Wr;break}}tr&&Qe<A&&(console.error("minGuardBG "+n(Qe,i)+" projected below "+n(A,i)+" - disabling SMB"),ce.manualBolusErrorString=1,ce.minGuardBG=Qe,ce.insulinForManualBolus=o((ce.eventualBG-ce.target_bg)/Pe,2),tr=!1),void 0===i.maxDelta_bg_threshold&&(Nr=.2),void 0!==i.maxDelta_bg_threshold&&(Nr=Math.min(i.maxDelta_bg_threshold,.4)),Se>Nr*Me&&(console.error("maxDelta "+n(Se,i)+" > "+100*Nr+"% of BG "+n(Me,i)+" - disabling SMB"),ce.reason+="maxDelta "+n(Se,i)+" > "+100*Nr+"% of BG "+n(Me,i)+" - SMB disabled!, ",tr=!1),console.error("BG projected to remain above "+n(Ge,i)+" for "+$r+"minutes"),(Jr<240||$r<60)&&console.error("BG projected to remain above "+n(A,i)+" for "+Jr+"minutes");var Kr=Jr,Qr=i.current_basal*R*Pe*Kr/60,Vr=Math.max(0,l.mealCOB-.25*l.carbs),Xr=(Zr-Qr)/csf-Vr;Qr=o(Qr),Xr=o(Xr),console.error("naive_eventualBG:",Je,"bgUndershoot:",Zr,"zeroTempDuration:",Kr,"zeroTempEffect:",Qr,"carbsReq:",Xr),"Could not parse clock data"==l.reason?console.error("carbsReq unknown: Could not parse clock data"):Xr>=i.carbsReqThreshold&&Jr<=45&&(ce.carbsReq=Xr,ce.reason+=Xr+" add'l carbs req w/in "+Jr+"m; ");var Yr=0;if(Me<A&&t.iob<-i.current_basal*R*20/60&&_e>0&&_e>Ve)ce.reason+="IOB "+t.iob+" < "+o(-i.current_basal*R*20/60,2),ce.reason+=" and minDelta "+n(_e,i)+" > expectedDelta "+n(Ve,i)+"; ";else if(Me<A||Qe<A)return ce.reason+="minGuardBG "+n(Qe,i)+"<"+n(A,i),Zr=ye-Qe,Qe<A&&(ce.manualBolusErrorString=2,ce.minGuardBG=Qe),ce.insulinForManualBolus=o((Ke-ye)/Pe,2),Yr=o(Zr/Pe*60/i.current_basal*R),Yr=30*o(Yr/30),Yr=Math.min(120,Math.max(30,Yr)),d.setTempBasal(0,Yr,i,ce,r);if(i.skip_neutral_temps&&ce.deliverAt.getMinutes()>=55){if(!tr)return ce.reason+="; Canceling temp at "+(60-ce.deliverAt.getMinutes())+"min before turn of the hour to avoid beeping of MDT. SMB are disabled anyways.",d.setTempBasal(0,0,i,ce,r);console.error(60-ce.deliverAt.getMinutes()+"min before turn of the hour, but SMB's are enabled - not skipping neutral temps.")}var et=0,rt=ve,tt=0;if(Ke<Ge){if(ce.reason+="Eventual BG "+n(Ke,i)+" < "+n(Ge,i),_e>Ve&&_e>0&&!Xr)return Je<40?(ce.reason+=", naive_eventualBG < 40. ",d.setTempBasal(0,30,i,ce,r)):(e.delta>_e?ce.reason+=", but Delta "+n(Be,i)+" > expectedDelta "+n(Ve,i):ce.reason+=", but Min. Delta "+_e.toFixed(2)+" > Exp. Delta "+n(Ve,i),r.duration>15&&a(ve,i)===a(r.rate,i)?(ce.reason+=", temp "+r.rate+" ~ req "+ve+"U/hr. ",ce):(ce.reason+="; setting current basal of "+ve+" as temp. ",d.setTempBasal(ve,30,i,ce,r)));et=o(et=2*Math.min(0,(Ke-ye)/Pe),2);var at=Math.min(0,(Je-ye)/Pe);at=o(at,2),_e<0&&_e>Ve&&(et=o(et*(_e/Ve),2)),rt=a(rt=ve+2*et,i),tt=r.duration*(r.rate-ve)/60;var ot=Math.min(et,at);if(console.log("naiveInsulinReq:"+at),tt<ot-.3*ve)return ce.reason+=", "+r.duration+"m@"+r.rate.toFixed(2)+" is a lot less than needed. ",d.setTempBasal(rt,30,i,ce,r);if(void 0!==r.rate&&r.duration>5&&rt>=.8*r.rate)return ce.reason+=", temp "+r.rate+" ~< req "+rt+"U/hr. ",ce;if(rt<=0){if((Yr=o((Zr=ye-Je)/Pe*60/i.current_basal*R))<0?Yr=0:(Yr=30*o(Yr/30),Yr=Math.min(120,Math.max(0,Yr))),Yr>0)return ce.reason+=", setting "+Yr+"m zero temp. ",d.setTempBasal(rt,Yr,i,ce,r)}else ce.reason+=", setting "+rt+"U/hr. ";return d.setTempBasal(rt,30,i,ce,r)}if(_e<Ve&&(ce.minDelta=_e,ce.expectedDelta=Ve,(Ve-_e>=2||Ve+-1*_e>=2)&&(ce.manualBolusErrorString=_e>=0&&Ve>0?3:_e<0&&Ve<=0||_e<0&&Ve>=0?4:5),ce.insulinForManualBolus=o((ce.eventualBG-ce.target_bg)/Pe,2),!u||!tr))return e.delta<_e?ce.reason+="Eventual BG "+n(Ke,i)+" > "+n(Ge,i)+" but Delta "+n(Be,i)+" < Exp. Delta "+n(Ve,i):ce.reason+="Eventual BG "+n(Ke,i)+" > "+n(Ge,i)+" but Min. Delta "+_e.toFixed(2)+" < Exp. Delta "+n(Ve,i),r.duration>15&&a(ve,i)===a(r.rate,i)?(ce.reason+=", temp "+r.rate+" ~ req "+ve+"U/hr. ",ce):(ce.reason+="; setting current basal of "+ve+" as temp. ",d.setTempBasal(ve,30,i,ce,r));if(Math.min(Ke,_r)<we&&(_r<Ge&&Ke>Ge&&(ce.manualBolusErrorString=6,ce.insulinForManualBolus=o((ce.eventualBG-ce.target_bg)/Pe,2)),ce.minPredBG=_r,!u||!tr))return ce.reason+=n(Ke,i)+"-"+n(_r,i)+" in range: no temp required",r.duration>15&&a(ve,i)===a(r.rate,i)?(ce.reason+=", temp "+r.rate+" ~ req "+ve+"U/hr. ",ce):(ce.reason+="; setting current basal of "+ve+" as temp. ",d.setTempBasal(ve,30,i,ce,r));if(Ke>=we&&(ce.reason+="Eventual BG "+n(Ke,i)+" >= "+n(we,i)+", ",Ke>we&&(ce.insulinForManualBolus=o((Ke-ye)/Pe,2))),t.iob>Oe)return ce.reason+="IOB "+o(t.iob,2)+" > max_iob "+Oe,r.duration>15&&a(ve,i)===a(r.rate,i)?(ce.reason+=", temp "+r.rate+" ~ req "+ve+"U/hr. ",ce):(ce.reason+="; setting current basal of "+ve+" as temp. ",d.setTempBasal(ve,30,i,ce,r));et=o((Math.min(_r,Ke)-ye)/Pe,2),T=o((Ke-ye)/Pe,2),et>Oe-t.iob?(console.error("SMB limited by maxIOB: "+Oe-t.iob+" (. insulinReq: "+et+" U)"),ce.reason+="max_iob "+Oe+", ",et=Oe-t.iob):console.error("SMB not limited by maxIOB ( insulinReq: "+et+" U)."),T>Oe-t.iob?(console.error("Ev. Bolus limited by maxIOB: "+Oe-t.iob+" (. insulinForManualBolus: "+T+" U)"),ce.reason+="max_iob "+Oe+", "):console.error("Ev. Bolus would not be limited by maxIOB ( insulinForManualBolus: "+T+" U)."),rt=a(rt=ve+2*et,i),et=o(et,3),ce.insulinReq=et;var nt=o((new Date(I).getTime()-t.lastBolusTime)/6e4,1);if(u&&tr&&Me>A){var it=30;void 0!==i.maxSMBBasalMinutes&&(it=i.maxSMBBasalMinutes);var st=30;void 0!==i.maxUAMSMBBasalMinutes&&(st=i.maxUAMSMBBasalMinutes),B.useOverride&&_&&G!==it&&(console.error("SMB Max Minutes - setting overriden from "+it+" to "+G),it=G),B.useOverride&&_&&w!==st&&(console.error("UAM Max Minutes - setting overriden from "+st+" to "+w),st=w);var lt=o(l.mealCOB/E,3),dt=0;void 0===it?(dt=o(i.current_basal*R*30/60,1),console.error("smbMinutesSetting undefined: defaulting to 30m"),et>dt&&console.error("SMB limited by maxBolus: "+dt+" ( "+et+" U)")):t.iob>lt&&t.iob>0?(console.error("IOB"+t.iob+"> COB"+l.mealCOB+"; mealInsulinReq ="+lt),st?(console.error("maxUAMSMBBasalMinutes: "+st+", profile.current_basal: "+i.current_basal*R),dt=o(i.current_basal*R*st/60,1)):(console.error("maxUAMSMBBasalMinutes undefined: defaulting to 30m"),dt=o(i.current_basal*R*30/60,1)),et>dt?console.error("SMB limited by maxUAMSMBBasalMinutes [ "+st+"m ]: "+dt+"U ( "+et+"U )"):console.error("SMB is not limited by maxUAMSMBBasalMinutes. ( insulinReq: "+et+"U )")):(console.error(".maxSMBBasalMinutes: "+it+", profile.current_basal: "+i.current_basal*R),et>(dt=o(i.current_basal*R*it/60,1))?console.error("SMB limited by maxSMBBasalMinutes: "+it+"m ]: "+dt+"U ( insulinReq: "+et+"U )"):console.error("SMB is not limited by maxSMBBasalMinutes. ( insulinReq: "+et+"U )"));var ut=i.bolus_increment,mt=1/ut,ct=Math.min(i.smb_delivery_ratio,1);.5!=ct&&console.error("SMB Delivery Ratio changed from default 0.5 to "+o(ct,2));var gt=Math.min(et*ct,dt);gt=Math.floor(gt*mt)/mt,Yr=o((ye-(Je+wr)/2)/Pe*60/i.current_basal*R),et>0&><ut&&(Yr=0);var ht=0;Yr<=0?Yr=0:Yr>=30?(Yr=30*o(Yr/30),Yr=Math.min(60,Math.max(0,Yr))):(ht=o(ve*Yr/30,2),Yr=30),ce.reason+=" insulinReq "+et,gt>=dt&&(ce.reason+="; maxBolus "+dt),Yr>0&&(ce.reason+="; setting "+Yr+"m low temp of "+ht+"U/h"),ce.reason+=". ";var vt=3;i.SMBInterval&&(vt=Math.min(10,Math.max(1,i.SMBInterval)));var Bt=o(vt-nt,0),bt=o(60*(vt-nt),0)%60;if(console.error("naive_eventualBG "+Je+","+Yr+"m "+ht+"U/h temp needed; last bolus "+nt+"m ago; maxBolus: "+dt),nt>vt?gt>0&&(ce.units=gt,ce.reason+="Microbolusing "+gt+"U. "):ce.reason+="Waiting "+Bt+"m "+bt+"s to microbolus again. ",Yr>0)return ce.rate=ht,ce.duration=Yr,ce}var pt=d.getMaxSafeBasal(i);return B.shouldProtectDueToHIGH?d.setTempBasal(i.current_basal,30,i,ce,r):(rt>pt&&(ce.reason+="adj. req. rate: "+rt+" to maxSafeBasal: "+o(pt,2)+", ",rt=a(pt,i)),(tt=r.duration*(r.rate-ve)/60)>=2*et?(ce.reason+=r.duration+"m@"+r.rate.toFixed(2)+" > 2 * insulinReq. Setting temp basal of "+rt+"U/hr. ",d.setTempBasal(rt,30,i,ce,r)):void 0===r.duration||0===r.duration?(ce.reason+="no temp, setting "+rt+"U/hr. ",d.setTempBasal(rt,30,i,ce,r)):r.duration>5&&a(rt,i)<=a(r.rate,i)?(ce.reason+="temp "+r.rate+" >~ req "+rt+"U/hr. ",ce):(ce.reason+="temp "+r.rate+"<"+rt+"U/hr. ",d.setTempBasal(rt,30,i,ce,r)))}},3531:(e,r,t)=>{var a=t(2296);e.exports=function(e,r){var t=20;return void 0!==r&&"string"==typeof r.model&&(a(r.model,"54")||a(r.model,"23"))&&(t=40),e<1?Math.round(e*t)/t:e<10?Math.round(20*e)/20:Math.round(10*e)/10}},1873:(e,r,t)=>{var a=t(9325).Symbol;e.exports=a},4932:e=>{e.exports=function(e,r){for(var t=-1,a=null==e?0:e.length,o=Array(a);++t<a;)o[t]=r(e[t],t,e);return o}},7133:e=>{e.exports=function(e,r,t){return e==e&&(void 0!==t&&(e=e<=t?e:t),void 0!==r&&(e=e>=r?e:r)),e}},2552:(e,r,t)=>{var a=t(1873),o=t(659),n=t(9350),i=a?a.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)}},7556:(e,r,t)=>{var a=t(1873),o=t(4932),n=t(6449),i=t(4394),s=a?a.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 t=r+"";return"0"==t&&1/r==-1/0?"-0":t}},4128:(e,r,t)=>{var a=t(1800),o=/^\s+/;e.exports=function(e){return e?e.slice(0,a(e)+1).replace(o,""):e}},4840:(e,r,t)=>{var a="object"==typeof t.g&&t.g&&t.g.Object===Object&&t.g;e.exports=a},659:(e,r,t)=>{var a=t(1873),o=Object.prototype,n=o.hasOwnProperty,i=o.toString,s=a?a.toStringTag:void 0;e.exports=function(e){var r=n.call(e,s),t=e[s];try{e[s]=void 0;var a=!0}catch(e){}var o=i.call(e);return a&&(r?e[s]=t:delete e[s]),o}},9350:e=>{var r=Object.prototype.toString;e.exports=function(e){return r.call(e)}},9325:(e,r,t)=>{var a=t(4840),o="object"==typeof self&&self&&self.Object===Object&&self,n=a||o||Function("return this")();e.exports=n},1800:e=>{var r=/\s/;e.exports=function(e){for(var t=e.length;t--&&r.test(e.charAt(t)););return t}},2296:(e,r,t)=>{var a=t(7133),o=t(7556),n=t(1489),i=t(3222);e.exports=function(e,r,t){e=i(e),r=o(r);var s=e.length,l=t=void 0===t?s:a(n(t),0,s);return(t-=r.length)>=0&&e.slice(t,l)==r}},6449:e=>{var r=Array.isArray;e.exports=r},3805:e=>{e.exports=function(e){var r=typeof e;return null!=e&&("object"==r||"function"==r)}},346:e=>{e.exports=function(e){return null!=e&&"object"==typeof e}},4394:(e,r,t)=>{var a=t(2552),o=t(346);e.exports=function(e){return"symbol"==typeof e||o(e)&&"[object Symbol]"==a(e)}},7400:(e,r,t)=>{var a=t(6993),o=1/0;e.exports=function(e){return e?(e=a(e))===o||e===-1/0?17976931348623157e292*(e<0?-1:1):e==e?e:0:0===e?e:0}},1489:(e,r,t)=>{var a=t(7400);e.exports=function(e){var r=a(e),t=r%1;return r==r?t?r-t:r:0}},6993:(e,r,t)=>{var a=t(4128),o=t(3805),n=t(4394),i=/^[-+]0x[0-9a-f]+$/i,s=/^0b[01]+$/i,l=/^0o[0-7]+$/i,d=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=a(e);var t=s.test(e);return t||l.test(e)?d(e.slice(2),t?2:8):i.test(e)?NaN:+e}},3222:(e,r,t)=>{var a=t(7556);e.exports=function(e){return null==e?"":a(e)}}},r={};function t(a){var o=r[a];if(void 0!==o)return o.exports;var n=r[a]={exports:{}};return e[a](n,n.exports,t),n.exports}t.g=function(){if("object"==typeof globalThis)return globalThis;try{return this||new Function("return this")()}catch(e){if("object"==typeof window)return window}}();var a=t(2982);trio_determineBasal=a})();
|