Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
156 commits
Select commit Hold shift + click to select a range
1d0d4aa
use openaps --nogit option
scottleibrand Jul 15, 2017
148a422
Merge branch 'master' into nogit
scottleibrand Jul 15, 2017
6d808f2
--nogit for myopenaps-cgm-loop too
scottleibrand Jul 15, 2017
f5317c9
remove git-specific stuff
scottleibrand Jul 15, 2017
4da025d
enable SMB/UAM (if enabled in preferences) for a full 6 hours after a…
scottleibrand Jul 22, 2017
418cc97
refresh pumphistory when pump is suspended and we can't enact
scottleibrand Jul 22, 2017
b097c7f
Merge branch 'dev' into gather-when-suspended
scottleibrand Jul 29, 2017
9d96590
Merge branch 'dev' into nogit
scottleibrand Jul 29, 2017
8d204bd
Merge branch 'gather-when-suspended' into nogit
scottleibrand Jul 29, 2017
d338151
check whether decocare-0.0.31 has been installed
scottleibrand Jul 29, 2017
bcef7ad
Merge branch 'update-decocare' into nogit
scottleibrand Jul 29, 2017
ee7271e
automatically install openaps 0.2.1 with --nogit support
scottleibrand Jul 29, 2017
e744e4e
check radio twice, and mmtune if both checks fail
scottleibrand Jul 30, 2017
7590fbf
use any_pump_comms function
scottleibrand Jul 30, 2017
2504e55
debugging
scottleibrand Jul 30, 2017
77ea0df
Calling out the number one beginner error
danamlewis Aug 8, 2017
68ee1ce
oref0-pump-loop reliability and debugging improvements (#575)
scottleibrand Aug 11, 2017
e9478ec
if currentDeviation is null or maxDeviation is 0, set mealCOB to 0 fo…
scottleibrand Aug 11, 2017
454bc1f
Merge branch 'dev' into nogit
scottleibrand Aug 12, 2017
5c54df4
version bump
scottleibrand Aug 12, 2017
61839af
Merge branch 'dev' into bad-deviation
scottleibrand Aug 12, 2017
90612ac
Merge branch 'dev' into number-one-beginner-error-people-refuse-to-ch…
scottleibrand Aug 12, 2017
d603bfb
Merge branch 'bad-deviation' into 0.6.0-dev
scottleibrand Aug 12, 2017
279750d
Merge branch 'number-one-beginner-error-people-refuse-to-check-on-the…
scottleibrand Aug 12, 2017
58a987e
speed up COB calculation
scottleibrand Aug 12, 2017
ffa7ede
Merge remote-tracking branch 'origin/cob-speedup' into 0.6.0-dev
scottleibrand Aug 12, 2017
296d560
remove ' that is confusing vim; indentation
scottleibrand Aug 12, 2017
c8fbd37
chmod +x bin/oref0_nightscout_check.py
scottleibrand Aug 12, 2017
71809c5
refresh meal.json after merge_pumphistory
scottleibrand Aug 12, 2017
7bb2033
newline
scottleibrand Aug 12, 2017
6f0d305
Merge branch 'separate-meal-calcs' into 0.6.0-dev
scottleibrand Aug 12, 2017
3c989c9
don't SMB unless bg > threshold (#602)
scottleibrand Aug 13, 2017
9683781
Exponential curves (#568)
sulkaharo Aug 13, 2017
0859697
Speed up COB calculation by only converting pumphistory to treatments…
scottleibrand Aug 13, 2017
88862cc
remove double declaration of biobContrib (#605)
AdrianLxM Aug 13, 2017
2319c2e
first attempt at allowing unsuspend without SMB (#596)
scottleibrand Aug 14, 2017
c4030a6
Clean up old unused stuff, fix whitespace, etc. (#595)
scottleibrand Aug 14, 2017
623da17
Decocare 0.1.0 (#597)
scottleibrand Aug 14, 2017
65faebf
fix percent grep
scottleibrand Aug 14, 2017
3e8eccd
check for percent mode after failure to make it more obvious
scottleibrand Aug 14, 2017
07f2c88
fix whitespace
scottleibrand Aug 14, 2017
62b527b
stop using DIA for anything but actual IOB/activity calcs (#599)
scottleibrand Aug 14, 2017
82d0b25
don't allow target_bg below 80
scottleibrand Aug 14, 2017
18b066d
only updatePreferences with displayedDefaults
scottleibrand Aug 14, 2017
e2ee0a6
move dia = 3 code so not to interfere with bolus snooze
scottleibrand Aug 14, 2017
46bffa2
Merge branch 'dev' into 0.6.0-dev
scottleibrand Aug 14, 2017
f4a9624
use profile.sens instead of autosens-adjusted sens (#618)
scottleibrand Aug 19, 2017
9a604ce
only print minDelta < expectedDelta when it's true
scottleibrand Aug 21, 2017
7a99a00
Merge branch 'dev' into 0.6.0-dev
scottleibrand Aug 23, 2017
dd6e1a6
don't overreact to negative deviations from a big negative delta (#615)
scottleibrand Aug 23, 2017
159b4da
make carbsReqThreshold configurable (#607)
scottleibrand Aug 23, 2017
6cb71c1
force minimum DIA of 3h (#606)
scottleibrand Aug 23, 2017
e355b0c
Timezone fix to 0.6.0-dev (#624)
scottleibrand Aug 23, 2017
42f4ffd
Merge branch '0.6.0-dev' of github.com:openaps/oref0 into 0.6.0-dev
scottleibrand Aug 23, 2017
6155121
Merge remote-tracking branch 'origin/dev' into 0.6.0-dev
scottleibrand Aug 23, 2017
112b143
ignore bolusing status if pump is suspended
scottleibrand Aug 24, 2017
955bfbe
clarify bolus snooze reason (for #626)
scottleibrand Aug 25, 2017
15f6a43
microBolus 1/2 the insulinReq if IOB doesn't yet cover COB (#621)
scottleibrand Aug 27, 2017
594a085
model remainingCarbs as a /\ shaped bilinear curve (#620)
scottleibrand Aug 27, 2017
99e7944
for purposes of categorizing boluses as SMBs to add to basalIOB, use …
scottleibrand Aug 28, 2017
1e331ec
add max_daily_basal to fix tests
scottleibrand Sep 2, 2017
384b514
always mmtune on falure if mmtune.json is more than 15m old
scottleibrand Sep 3, 2017
041b223
don't necessarily mmtune if pump_loop_completed within 15m
scottleibrand Sep 3, 2017
d536fbf
Merge branch 'always-mmtune-when-old' into 0.6.0-dev
scottleibrand Sep 3, 2017
04d7382
Always mmtune if pump_loop_completed > 15m old (#635)
scottleibrand Sep 4, 2017
3c970f1
stop letting high temp run (#633)
scottleibrand Sep 4, 2017
b60f341
remove -g as it breaks autotune's time filters
scottleibrand Sep 4, 2017
bb15929
Merge branch 'fix-autotune' into 0.6.0-dev
scottleibrand Sep 4, 2017
067ccac
Revert "remove -g as it breaks autotune's time filters"
scottleibrand Sep 4, 2017
621ba48
Merge branch 'dev' into 0.6.0-dev
scottleibrand Sep 5, 2017
d3d3e52
Allow nonzero low temps for SMB (#634)
scottleibrand Sep 5, 2017
abdb96a
Merge branch 'dev' into 0.6.0-dev
scottleibrand Sep 5, 2017
8962fce
don't allow maxSMBBasalMinutes > 30 unless IOB (and COB) are > 0 (#639)
scottleibrand Sep 7, 2017
52f4b30
COB-aware autosens (#629)
scottleibrand Sep 7, 2017
6c2456f
carbs: should only include carbs actually used in calculating COB (#640)
scottleibrand Sep 7, 2017
9c89a54
weight COBpredBG vs. UAMpredBG based on how many carbs remain as COB …
scottleibrand Sep 7, 2017
8cfd6a7
Additional remainingCI debugging (#645)
scottleibrand Sep 7, 2017
4519a13
remove autosens avgDelta-bgi < 6 check (#643)
scottleibrand Sep 7, 2017
9a659a7
Merge branch 'dev' into 0.6.0-dev
scottleibrand Sep 8, 2017
05658ed
Merge branch 'dev' into 0.6.0-dev
scottleibrand Sep 9, 2017
392f032
Disable IPv6 for rig stability purposes (#649)
efidoman Sep 10, 2017
9e78f10
Autotune: calculate CR directly, and use CR*CSF to help calculate ISF…
scottleibrand Sep 10, 2017
d498552
Make remainingCI time responsive to temp targets (#646)
scottleibrand Sep 10, 2017
656fc6f
Merge branch 'dev' into 0.6.0-dev
scottleibrand Sep 10, 2017
30f21a2
Merge pull request #592 from openaps/0.6.0-dev
danamlewis Sep 19, 2017
8e6a44f
only display rate and duration from enact/smb-enacted.json (#654)
scottleibrand Sep 20, 2017
d78d880
don't adjust temptargets (#656)
scottleibrand Sep 20, 2017
1f08518
avoid divide by zero (NaN) in CI Duration (#657)
scottleibrand Sep 20, 2017
d5dbfe9
adjust remainingCATime gradually from 3h up to 4h @ 25% of carbs abso…
scottleibrand Sep 20, 2017
f9ee0b5
adjust remainingCATime for <50% absorbed, exclusive of temptarget adj…
scottleibrand Sep 20, 2017
d61fa92
adjust remainingCATime longer when <50% of carbs remain and we aren't…
scottleibrand Sep 20, 2017
6494a7e
actually allow positive remainingCATimeAdjustment
scottleibrand Sep 20, 2017
9de3a99
round away floating point math
scottleibrand Sep 20, 2017
1d78bb2
comment out remainingCATimeAdjustment for temptargets
scottleibrand Sep 20, 2017
40a937d
round away floating point math
scottleibrand Sep 20, 2017
55c9b42
only adjust remainingCATime if there are meal_data.carbs
scottleibrand Sep 20, 2017
071e5d4
only display remainingCI debugging with COB
scottleibrand Sep 20, 2017
235a085
make SMB model failure explicit
scottleibrand Sep 20, 2017
70b8f97
echo -n
scottleibrand Sep 20, 2017
1d1041e
skip bolusing check for 512/712
scottleibrand Sep 20, 2017
b356e5d
grep quietly
scottleibrand Sep 20, 2017
320d69a
0.5.4
scottleibrand Sep 20, 2017
39d6b83
Merge pull request #659 from openaps/512-support
danamlewis Sep 20, 2017
6a0e831
display curve in preferences by default (#658)
scottleibrand Sep 21, 2017
19a2e36
Less aggressive UAM (#655)
scottleibrand Sep 21, 2017
0c9cc1c
proceed to enact and set clock if pumphistory-zoned is in the future …
scottleibrand Sep 21, 2017
0c76235
calculate autosens ratio with both 8h and 24h of data (#653)
scottleibrand Sep 21, 2017
ccf11cc
Merge branch 'master' into dev
scottleibrand Sep 21, 2017
14e6a29
disable SMB if predicted BG drops below threshold (#661)
scottleibrand Sep 21, 2017
901163d
Refresh pumphistory before setting temps and after SMB/enact (#663)
scottleibrand Sep 21, 2017
1ce5d70
autotune/autosens on by default; don't ask about ~/myopenaps (#664)
scottleibrand Sep 22, 2017
6874788
Update oref0-setup.sh (#666)
jaylagorio Sep 23, 2017
0430c9a
Merge branch 'dev' into fractionCOBAbsorbed-remainingCATime
scottleibrand Sep 24, 2017
c1f353f
more explicit CATimeAdjustment; extend remainingCATimeMin for >90g
scottleibrand Sep 24, 2017
af0ef3a
allow SMBs to consider COBPredBGs > 400
scottleibrand Sep 24, 2017
ec80ac2
check max_iob against net IOB, not just basaliob
scottleibrand Sep 24, 2017
d0fc003
always SMB 1/2 of insulinReq
scottleibrand Sep 24, 2017
266a3b5
remainingCATimeMax = 6
scottleibrand Sep 24, 2017
ed7257d
start setting minIOBPredBG at 70m for insulinPeakTime of 55m
scottleibrand Sep 24, 2017
8cbe050
x12 automation (#665)
danamlewis Sep 24, 2017
bdaf2c9
Merge branch 'dev' into fractionCOBAbsorbed-remainingCATime
scottleibrand Sep 24, 2017
c003116
remove old commented-out let-it-run code
scottleibrand Sep 24, 2017
4fdf0c2
PLGS mode for minGuardBG < threshold
scottleibrand Sep 24, 2017
bacd73c
zero temp if minGuardBG < threshold regardless of delta
scottleibrand Sep 24, 2017
b29b41e
add comment
scottleibrand Sep 24, 2017
4e3d0a0
wait for bolus to complete and refresh pumphistory after SMB/enact (#…
scottleibrand Sep 24, 2017
0649d27
enable autosens and autotune by default (#669)
scottleibrand Sep 24, 2017
32319a6
make sure another rig isn't also talking before proceeding to SMB (#670)
scottleibrand Sep 24, 2017
390888c
Put pump_loop_completed in /tmp/ (#672)
jaylagorio Sep 24, 2017
2989ea2
Revert "Update oref0-setup.sh (#666)"
scottleibrand Sep 24, 2017
014426a
revert incorrect /tmp/pump_loop_completed change
scottleibrand Sep 24, 2017
e915dac
print hour numbers in autosens output (#671)
scottleibrand Sep 25, 2017
597412d
properly calculate minguardBG if COB==0 and UAM is disabled (#673)
scottleibrand Sep 25, 2017
2110ca2
Merge remote-tracking branch 'origin/dev' into fractionCOBAbsorbed-re…
scottleibrand Sep 25, 2017
361c2ff
fix test
scottleibrand Sep 25, 2017
8545216
Merge pull request #662 from openaps/fractionCOBAbsorbed-remainingCATime
danamlewis Sep 25, 2017
d192e52
recalculate IOB and predBGs after each loop (#674)
scottleibrand Sep 25, 2017
02aca03
add allDeviations to meal.json output for UAM monitoring (#675)
scottleibrand Sep 26, 2017
bc8d4de
remove extra minPredBG in rT object (#676)
scottleibrand Sep 26, 2017
fc938f5
autotune de-biasing (#668)
scottleibrand Sep 26, 2017
a3834b4
randomize wait_for_silence before smb_verify_reservoir (#679)
scottleibrand Sep 28, 2017
7d1e805
use lastCarbAge to calculate remainingCATime (#678)
scottleibrand Sep 29, 2017
3bd4605
add an enableSMB_always option (#677)
scottleibrand Sep 29, 2017
a81f007
recalculate IOB before re-doing smb-suggested.json (#680)
scottleibrand Sep 29, 2017
c65ee94
round basalNeeded to .01 to make it easier to read (#681)
scottleibrand Sep 29, 2017
13cd96f
look ahead an additional 15m to get insulin started faster (#683)
scottleibrand Oct 2, 2017
0d80ef0
curl openaps-install.sh (#697)
scottleibrand Oct 6, 2017
9b9a87c
remove ,, for backwards compatibility with bash 3.x on Mac
scottleibrand Oct 6, 2017
cad536b
Merge branch 'dev' of github.com:openaps/oref0 into dev
scottleibrand Oct 6, 2017
8be8b33
don't run loop if rig is too hot (#689)
scottleibrand Oct 6, 2017
30c9b9f
Attempting to --nogit by default for existing user installs
danamlewis Oct 6, 2017
9c8a1a5
Merge pull request #699 from openaps/no-git-default
danamlewis Oct 6, 2017
fc990c2
Blend minGuardBG (#698)
scottleibrand Oct 7, 2017
9ba6bb0
Don't send stale pushovers (#701)
scottleibrand Oct 7, 2017
8eec3aa
typofix
scottleibrand Oct 7, 2017
ff47d46
go back to using a 90m minPredBG lookahead for all insulin types (#695)
scottleibrand Oct 7, 2017
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions bin/ns-get.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ EOF
CURL_AUTH=""

# use token authentication if the user has a token set in their API_SECRET environment variable
if [[ "${API_SECRET,,}" =~ "token=" ]]; then
if [[ "${API_SECRET}" =~ "token=" ]]; then
if [[ -z ${QUERY} ]]; then
REPORT_ENDPOINT=$NIGHTSCOUT_HOST/api/v1/${REPORT}'?'${API_SECRET}
else
Expand All @@ -46,7 +46,7 @@ case $1 in
OUTPUT=${5-/dev/fd/1}

# use token authentication if the user has a token set in their API_SECRET environment variable
if [[ "${API_SECRET,,}" =~ "token=" ]]; then
if [[ "${API_SECRET}" =~ "token=" ]]; then
if [[ -z ${QUERY} ]]; then
REPORT_ENDPOINT=$NIGHTSCOUT_HOST/api/v1/${REPORT}'?'${API_SECRET}
else
Expand Down
4 changes: 2 additions & 2 deletions bin/openaps-bootstrap.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ ifdown wlan0; ifup wlan0
sleep 10
echo -ne "\nWifi SSID: "; iwgetid -r
sleep 5
# TODO check for options to fix the certificate activation error message for https
cd /tmp/; wget --no-check-certificate https://raw.githubusercontent.com/openaps/docs/dev/scripts/openaps-install.sh; bash ./openaps-install.sh
curl https://raw.githubusercontent.com/openaps/oref0/master/bin/openaps-install.sh > /tmp/openaps-install.sh
bash /tmp/openaps-install.sh
)
24 changes: 19 additions & 5 deletions bin/oref0-autotune-prep.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

var generate = require('oref0/lib/autotune-prep');
function usage ( ) {
console.error('usage: ', process.argv.slice(0, 2), '<pumphistory.json> <profile.json> <glucose.json> [carbhistory.json] [autotune/glucose.json]');
console.error('usage: ', process.argv.slice(0, 2), '<pumphistory.json> <profile.json> <glucose.json> [pumpprofile.json] [carbhistory.json] [autotune/glucose.json]');
}

if (!module.parent) {
Expand All @@ -33,8 +33,9 @@ if (!module.parent) {
}
var profile_input = process.argv.slice(3, 4).pop();
var glucose_input = process.argv.slice(4, 5).pop();
var carb_input = process.argv.slice(5, 6).pop()
var prepped_glucose_input = process.argv.slice(6, 7).pop()
var pumpprofile_input = process.argv.slice(5, 6).pop()
var carb_input = process.argv.slice(6, 7).pop()
var prepped_glucose_input = process.argv.slice(7, 8).pop()

if ( !pumphistory_input || !profile_input || !glucose_input ) {
usage( );
Expand All @@ -51,10 +52,23 @@ if (!module.parent) {
return console.error("Could not parse input data: ", e);
}

var pumpprofile_data = { };
if (typeof pumpprofile_input != 'undefined') {
try {
pumpprofile_data = JSON.parse(fs.readFileSync(pumpprofile_input, 'utf8'));
} catch (e) {
console.error("Warning: could not parse "+pumpprofile_input);
}
}

// disallow impossibly low carbRatios due to bad decoding
if ( typeof(profile_data.carb_ratio) == 'undefined' || profile_data.carb_ratio < 2 ) {
console.log('{ "carbs": 0, "mealCOB": 0, "reason": "carb_ratio ' + profile_data.carb_ratio + ' out of bounds" }');
return console.error("Error: carb_ratio " + profile_data.carb_ratio + " out of bounds");
if ( typeof(pumpprofile_data.carb_ratio) == 'undefined' || pumpprofile_data.carb_ratio < 2 ) {
console.log('{ "carbs": 0, "mealCOB": 0, "reason": "carb_ratios ' + profile_data.carb_ratio + ' and ' + pumpprofile_data.carb_ratio + ' out of bounds" }');
return console.error("Error: carb_ratios " + profile_data.carb_ratio + ' and ' + pumpprofile_data.carb_ratio + " out of bounds");
} else {
profile_data.carb_ratio = pumpprofile_data.carb_ratio;
}
}

try {
Expand Down
16 changes: 8 additions & 8 deletions bin/oref0-autotune-recommends-report.sh
Original file line number Diff line number Diff line change
Expand Up @@ -64,20 +64,20 @@ csf_new=$(cat $directory/autotune/profile.json | jq '.csf')
carb_ratio_new=$(cat $directory/autotune/profile.json | jq '.carb_ratio')

# Print Header Info
printf "%-${parameter_width}s| %-${data_width}s| %-${data_width}s\n" "Parameter" "Current" "Autotune" >> $report_file
printf "%-${parameter_width}s| %-${data_width}s| %-${data_width}s\n" "Parameter" "Pump" "Autotune" >> $report_file
printf "%s\n" "-------------------------------------" >> $report_file

# Print ISF, CSF and Carb Ratio Recommendations
printf "%-${parameter_width}s| %-${data_width}.3f| %-${data_width}.3f\n" "ISF [mg/dL/U]" $isf_current $isf_new >> $report_file
if [ $csf_current != null ]; then
printf "%-${parameter_width}s| %-${data_width}.3f| %-${data_width}.3f\n" "CSF [mg/dL/g]" $csf_current $csf_new >> $report_file
else
printf "%-${parameter_width}s| %-${data_width}s| %-${data_width}.3f\n" "CSF [mg/dL/g]" "n/a" $csf_new >> $report_file
fi
printf "%-${parameter_width}s| %-${data_width}.3f| %-${data_width}.3f\n" "Carb Ratio [g]" $carb_ratio_current $carb_ratio_new >> $report_file
# if [ $csf_current != null ]; then
# printf "%-${parameter_width}s| %-${data_width}.3f| %-${data_width}.3f\n" "CSF [mg/dL/g]" $csf_current $csf_new >> $report_file
# else
# printf "%-${parameter_width}s| %-${data_width}s| %-${data_width}.3f\n" "CSF [mg/dL/g]" "n/a" $csf_new >> $report_file
# fi
printf "%-${parameter_width}s| %-${data_width}.3f| %-${data_width}.3f\n" "Carb Ratio[g/U]" $carb_ratio_current $carb_ratio_new >> $report_file

# Print Basal Profile Recommendations
printf "%-${parameter_width}s| %-${data_width}s|\n" "Basal Profile [unit/hour]" "-" >> $report_file
printf "%-${parameter_width}s| %-${data_width}s|\n" "Basals [U/hr]" "-" >> $report_file

# Build time_list array of H:M in 30 minute increments to mirror pump basal schedule
time_list=()
Expand Down
137 changes: 67 additions & 70 deletions bin/oref0-autotune.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/bash

# This script sets up an easy test environment for autotune, allowing the user to vary parameters
# like start/end date and number of runs.
# like start/end date.
#
# Required Inputs:
# DIR, (--dir=<OpenAPS Directory>)
Expand All @@ -10,8 +10,6 @@
# Optional Inputs:
# END_DATE, (--end-date=<YYYY-MM-DD>)
# if no end date supplied, assume we want a months worth or until day before current day
# NUMBER_OF_RUNS (--runs=<integer, number of runs desired>)
# if no number of runs designated, then default to 5
# EXPORT_EXCEL (--xlsx=<filenameofexcel>)
# export to excel. Disabled by default
# TERMINAL_LOGGING (--log <true/false(true)>
Expand Down Expand Up @@ -39,7 +37,8 @@ DIR=""
NIGHTSCOUT_HOST=""
START_DATE=""
END_DATE=""
NUMBER_OF_RUNS=1 # Default to a single run if not otherwise specified
START_DAYS_AGO=1 # Default to yesterday if not otherwise specified
END_DAYS_AGO=1 # Default to yesterday if not otherwise specified
EXPORT_EXCEL="" # Default is to not export to Microsoft Excel
TERMINAL_LOGGING=true
RECOMMENDS_REPORT=true
Expand All @@ -50,8 +49,8 @@ if [ -n "${API_SECRET_READ}" ]; then
fi

if [[ -z "$API_SECRET" ]]; then
echo "ERROR: API_SECRET is not set when calling oref0-autotune.sh"
exit 1
echo "Warning: API_SECRET is not set when calling oref0-autotune.sh"
# exit 1
fi

# If we are running OS X, we need to use a different version
Expand Down Expand Up @@ -96,8 +95,12 @@ case $i in
END_DATE=`date --date="$END_DATE" +%Y-%m-%d`
shift # past argument=value
;;
-r=*|--runs=*)
NUMBER_OF_RUNS="${i#*=}"
-t=*|--start-days-ago=*)
START_DAYS_AGO="${i#*=}"
shift # past argument=value
;;
-d=*|--end-days-ago=*)
END_DAYS_AGO="${i#*=}"
shift # past argument=value
;;
-x=*|--xlsx=*)
Expand All @@ -116,28 +119,31 @@ case $i in
esac
done

# remove any trailing / from NIGHTSCOUT_HOST
NIGHTSCOUT_HOST=$(echo $NIGHTSCOUT_HOST | sed 's/\/$//g')

if [[ -z "$DIR" || -z "$NIGHTSCOUT_HOST" ]]; then
echo "Usage: oref0-autotune <--dir=myopenaps_directory> <--ns-host=https://mynightscout.azurewebsites.net> [--start-date=YYYY-MM-DD] [--end-date=YYYY-MM-DD] [--runs=number_of_runs] [--xlsx=autotune.xlsx] [--log=(true)|false]"
echo "Usage: oref0-autotune <--dir=myopenaps_directory> <--ns-host=https://mynightscout.azurewebsites.net> [--start-days-ago=number_of_days] [--end-days-ago=number_of_days] [--start-date=YYYY-MM-DD] [--end-date=YYYY-MM-DD] [--xlsx=autotune.xlsx] [--log=(true)|false]"
exit 1
fi
if [[ -z "$START_DATE" ]]; then
# Default start date of yesterday
START_DATE=`date --date="1 day ago" +%Y-%m-%d`
START_DATE=`date --date="$START_DAYS_AGO days ago" +%Y-%m-%d`
fi
if [[ -z "$END_DATE" ]]; then
# Default end-date as this morning at midnight in order to not get partial day samples for now
# (ISF/CSF adjustments are still single values across each day)
END_DATE=`date --date="1 day ago" +%Y-%m-%d`
END_DATE=`date --date="$END_DAYS_AGO days ago" +%Y-%m-%d`
fi

if [[ -z "$UNKNOWN_OPTION" ]] ; then # everything is ok
echo "Running oref0-autotune --dir=$DIR --ns-host=$NIGHTSCOUT_HOST --start-date=$START_DATE --runs=$NUMBER_OF_RUNS --end-date=$END_DATE"
echo "Running oref0-autotune --dir=$DIR --ns-host=$NIGHTSCOUT_HOST --start-date=$START_DATE --end-date=$END_DATE"
else
echo "Unknown options. Exiting"
exit 1
fi

# Get profile for testing copied to home directory. "openaps" is my loop directory name.
# Get profile for testing copied to home directory.
cd $directory && mkdir -p autotune
cp settings/pumpprofile.json autotune/profile.pump.json || die "Cannot copy settings/pumpprofile.json"
# This allows manual users to be able to run autotune by simply creating a settings/pumpprofile.json file.
Expand All @@ -157,15 +163,6 @@ if [[ $TERMINAL_LOGGING = "true" ]]; then
exec &> >(tee -a autotune.$(date +%Y-%m-%d-%H%M%S).log)
fi

# Pull Nightscout Data
echo "Grabbing NIGHTSCOUT treatments.json for date range..."

# Get Nightscout carb and insulin Treatments
query="find%5Bcreated_at%5D%5B%24gte%5D=`date --date="$START_DATE -4 hours" -Iminutes`&find%5Bcreated_at%5D%5B%24lte%5D=`date --date="$END_DATE +1 days" -Iminutes`"
echo Query: $NIGHTSCOUT_HOST/$query
ns-get host $NIGHTSCOUT_HOST treatments.json $query > ns-treatments.json || die "Couldn't download ns-treatments.json"
ls -la ns-treatments.json || die "No ns-treatments.json downloaded"

# Build date list for autotune iteration
date_list=()
date=$START_DATE;
Expand All @@ -179,76 +176,76 @@ do
fi
done

echo "Grabbing NIGHTSCOUT entries/sgv.json for date range..."
echo "Grabbing NIGHTSCOUT treatments.json and entries/sgv.json for date range..."

# Get Nightscout BG (sgv.json) Entries
for i in "${date_list[@]}"
do
query="find%5Bdate%5D%5B%24gte%5D=`(date -d $i +%s | tr -d '\n'; echo 000)`&find%5Bdate%5D%5B%24lte%5D=`(date --date="$i +1 days" +%s | tr -d '\n'; echo 000)`&count=1000"
echo Query: $NIGHTSCOUT_HOST $query
ns-get host $NIGHTSCOUT_HOST entries/sgv.json $query > ns-entries.$i.json || die "Couldn't download ns-entries.$i.json"
ls -la ns-entries.$i.json || die "No ns-entries.$i.json downloaded"
done

echo "Running $NUMBER_OF_RUNS runs from $START_DATE to $END_DATE"
sleep 2

# Do iterative runs over date range, save autotune.json (prepped data) and input/output
# profile.json
# Loop 1: Run 1 to Number of Runs specified by user or by default (1)
for run_number in $(seq 1 $NUMBER_OF_RUNS)
do
# Loop 2: Iterate through Date Range
for i in "${date_list[@]}"
do
cp profile.json profile.$run_number.$i.json
query="find%5Bdate%5D%5B%24gte%5D=`(date -d $i +%s | tr -d '\n'; echo 000)`&find%5Bdate%5D%5B%24lte%5D=`(date --date="$i +1 days" +%s | tr -d '\n'; echo 000)`&count=1000"
echo Query: $NIGHTSCOUT_HOST $query
ns-get host $NIGHTSCOUT_HOST entries/sgv.json $query > ns-entries.$i.json || die "Couldn't download ns-entries.$i.json"
ls -la ns-entries.$i.json || die "No ns-entries.$i.json downloaded"

# Get Nightscout carb and insulin Treatments
# echo $i $START_DATE;
#query="find%5Bdate%5D%5B%24gte%5D=`(date -d $i +%s | tr -d'\n'; echo 000)`&find%5Bdate%5D%5B%24lte%5D=`(date --date="$i +1 days" +%s | tr -d '\n'; echo 000)`&count=1000"
# to capture UTC-dated treatments, we need to capture an extra 12h on either side, plus the DIA lookback
# 18h = 12h for timezones + 6h for DIA; 36h = 24h for end-of-day + 12h for timezones
query="find%5Bcreated_at%5D%5B%24gte%5D=`date --date="$i -18 hours" -Iminutes`&find%5Bcreated_at%5D%5B%24lte%5D=`date --date="$i +36 hours" -Iminutes`"
echo Query: $NIGHTSCOUT_HOST/$query
ns-get host $NIGHTSCOUT_HOST treatments.json $query > ns-treatments.$i.json || die "Couldn't download ns-treatments.$i.json"
ls -la ns-treatments.$i.json || die "No ns-treatments.$i.json downloaded"


# Do iterative runs over date range, save autotune.json (prepped data) and input/output profile.json
cp profile.json profile.$i.json
# Autotune Prep (required args, <pumphistory.json> <profile.json> <glucose.json>), output prepped glucose
# data or <autotune/glucose.json> below
echo "oref0-autotune-prep ns-treatments.json profile.json ns-entries.$i.json > autotune.$run_number.$i.json"
oref0-autotune-prep ns-treatments.json profile.json ns-entries.$i.json > autotune.$run_number.$i.json \
|| die "Could not run oref0-autotune-prep ns-treatments.json profile.json ns-entries.$i.json"
echo "oref0-autotune-prep ns-treatments.$i.json profile.json ns-entries.$i.json profile.pump.json > autotune.$i.json"
oref0-autotune-prep ns-treatments.$i.json profile.json ns-entries.$i.json profile.pump.json > autotune.$i.json \
|| die "Could not run oref0-autotune-prep ns-treatments.$i.json profile.json ns-entries.$i.json"

# Autotune (required args, <autotune/glucose.json> <autotune/autotune.json> <settings/profile.json>),
# output autotuned profile or what will be used as <autotune/autotune.json> in the next iteration
echo "oref0-autotune-core autotune.$run_number.$i.json profile.json profile.pump.json > newprofile.$run_number.$i.json"
if ! oref0-autotune-core autotune.$run_number.$i.json profile.json profile.pump.json > newprofile.$run_number.$i.json; then
echo "oref0-autotune-core autotune.$i.json profile.json profile.pump.json > newprofile.$i.json"
if ! oref0-autotune-core autotune.$i.json profile.json profile.pump.json > newprofile.$i.json; then
if cat profile.json | jq --exit-status .carb_ratio==null; then
echo "ERROR: profile.json contains null carb_ratio: using profile.pump.json"
cp profile.pump.json profile.json
exit
else
die "Could not run oref0-autotune-core autotune.$run_number.$i.json profile.json profile.pump.json"
die "Could not run oref0-autotune-core autotune.$i.json profile.json profile.pump.json"
fi
else
# Copy tuned profile produced by autotune to profile.json for use with next day of data
cp newprofile.$run_number.$i.json profile.json
cp newprofile.$i.json profile.json
fi

done # End Date Range Iteration
done # End Number of Runs Loop

if ! [[ -z "$EXPORT_EXCEL" ]]; then
echo Exporting to $EXPORT_EXCEL
oref0-autotune-export-to-xlsx --dir $DIR --output $EXPORT_EXCEL
fi
if ! [[ -z "$EXPORT_EXCEL" ]]; then
echo Exporting to $EXPORT_EXCEL
oref0-autotune-export-to-xlsx --dir $DIR --output $EXPORT_EXCEL
fi

# Create Summary Report of Autotune Recommendations and display in the terminal
if [[ $RECOMMENDS_REPORT == "true" ]]; then
# Set the report file name, so we can let the user know where it is and cat
# it to the screen
report_file=$directory/autotune/autotune_recommendations.log
# Create Summary Report of Autotune Recommendations and display in the terminal
if [[ $RECOMMENDS_REPORT == "true" ]]; then
# Set the report file name, so we can let the user know where it is and cat
# it to the screen
report_file=$directory/autotune/autotune_recommendations.log

echo
echo "Autotune pump profile recommendations:"
echo "---------------------------------------------------------"
echo
echo "Autotune pump profile recommendations:"
echo "---------------------------------------------------------"

# Let the user know where the Autotune Recommendations are logged
echo "Recommendations Log File: $report_file"
echo
# Let the user know where the Autotune Recommendations are logged
echo "Recommendations Log File: $report_file"
echo

# Run the Autotune Recommends Report
oref0-autotune-recommends-report $directory
# Run the Autotune Recommends Report
oref0-autotune-recommends-report $directory

# Go ahead and echo autotune_recommendations.log to the terminal, minus blank lines
cat $report_file | egrep -v "\| *\| *$"
fi
# Go ahead and echo autotune_recommendations.log to the terminal, minus blank lines
cat $report_file | egrep -v "\| *\| *$"
fi

done # End Date Range Iteration
Loading