Skip to content

Commit

Permalink
Merge branch 'dev' into nodejs-8
Browse files Browse the repository at this point in the history
  • Loading branch information
scottleibrand committed Aug 12, 2017
2 parents 4b5b86b + 514fbfb commit 32c032a
Show file tree
Hide file tree
Showing 16 changed files with 169 additions and 57 deletions.
13 changes: 13 additions & 0 deletions bin/monitor-xdrip.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash

date
cp -rf xdrip/glucose.json xdrip/last-glucose.json
curl --compressed -s http://localhost:5000/api/v1/entries?count=288 | json -e "this.glucose = this.sgv" > xdrip/glucose.json
if ! cmp --silent xdrip/glucose.json xdrip/last-glucose.json; then
if cat xdrip/glucose.json | json -c "minAgo=(new Date()-new Date(this.dateString))/60/1000; return minAgo < 10 && minAgo > -5 && this.glucose > 38" | grep -q glucose; then
cp -up $directory/xdrip/glucose.json $directory/monitor/glucose.json
else
echo No recent glucose data downloaded from xDrip.
diff xdrip/glucose.json xdrip/last-glucose.json
fi
fi
7 changes: 5 additions & 2 deletions bin/ns-get.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ $self host <NIGHTSCOUT_HOST|localhost:1337> <entries.json> [QUERY] [stdout|-]
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 [[ -z ${QUERY} ]]; then
Expand All @@ -32,6 +34,7 @@ if [[ "${API_SECRET,,}" =~ "token=" ]]; then
fi
else
REPORT_ENDPOINT=$NIGHTSCOUT_HOST/api/v1/${REPORT}'?'${QUERY}
CURL_AUTH='-H "api-secret: ${API_SECRET}"'
fi

case $1 in
Expand All @@ -54,7 +57,7 @@ case $1 in
fi
test -z "$NIGHTSCOUT_HOST" && usage && exit 1;

curl ${CURL_FLAGS} $REPORT_ENDPOINT | $NIGHTSCOUT_FORMAT
curl ${CURL_AUTH} ${CURL_FLAGS} $REPORT_ENDPOINT | $NIGHTSCOUT_FORMAT

;;
type)
Expand All @@ -66,7 +69,7 @@ case $1 in
;;
*)
test -z "$NIGHTSCOUT_HOST" && usage && exit 1;
curl ${CURL_FLAGS} $REPORT_ENDPOINT | $NIGHTSCOUT_FORMAT
curl ${CURL_AUTH} ${CURL_FLAGS} $REPORT_ENDPOINT | $NIGHTSCOUT_FORMAT
;;
esac

Expand Down
33 changes: 20 additions & 13 deletions bin/oref0-autotune-export-to-xlsx.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,9 @@ def excel_init_workbook(workbook):
# sort filenames. First on date and then on run number
# put settings/profile.js
def sortedFilenames():
filelist=glob.glob("settings/profile.json")
filelist=filelist+glob.glob("settings/pumpprofile.json")
profiles=glob.glob("autotune/profile*.json")
filelist=glob.glob("../settings/profile.json")
filelist=filelist+glob.glob("../settings/pumpprofile.json")
profiles=glob.glob("profile*.json")
listdateandrun=[]
for i in profiles:
date, run = parseDateAndRun(i)
Expand All @@ -155,9 +155,9 @@ def sortedFilenames():

if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Export oref0 autotune files to Microsoft Excel')
parser.add_argument('-d', '--dir', help='autotune directory', default='.')
parser.add_argument('-d', '--dir', help='openaps directory', default='.')
parser.add_argument('-o', '--output', help='default autotune.xlsx', default='autotune.xlsx')
parser.add_argument('--version', action='version', version='%(prog)s 0.0.3-dev')
parser.add_argument('--version', action='version', version='%(prog)s 0.0.4-dev')
args = parser.parse_args()

# change to openaps directory
Expand All @@ -172,13 +172,20 @@ def sortedFilenames():
f=open(filename, 'r')
print "Adding %s to Excel" % filename
j=json.load(f)
basalProfile=j['basalprofile']
isfProfile=j['isfProfile']['sensitivities']
expandedBasal=expandProfile(basalProfile, 'rate', 'minutes')
expandedIsf=expandProfile(isfProfile, 'sensitivity', 'offset')
write_timebased_profile(worksheetBasal, row, expandedBasal, excel_2decimals_format)
write_timebased_profile(worksheetIsf, row, expandedIsf, excel_integer_format)
write_profile(worksheetProfile, row, j, excel_integer_format)
row=row+1
try:
basalProfile=j['basalprofile']
isfProfile=j['isfProfile']['sensitivities']
expandedBasal=expandProfile(basalProfile, 'rate', 'minutes')
expandedIsf=expandProfile(isfProfile, 'sensitivity', 'offset')
write_timebased_profile(worksheetBasal, row, expandedBasal, excel_2decimals_format)
write_timebased_profile(worksheetIsf, row, expandedIsf, excel_integer_format)
write_profile(worksheetProfile, row, j, excel_integer_format)
row=row+1
except Exception as e:
if j.has_key('error'):
print "Skipping file. Error: %s " % j['error']
else:
print "Skipping file. Exception: %s" % e

workbook.close()
print "Written %d lines to Excel" % row
13 changes: 8 additions & 5 deletions bin/oref0-autotune-recommends-report.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
#
# Example usage: ~/src/oref0/bin/oref0-autotune-recommends-report.sh <OpenAPS Loop Directory Path>

# fix problems with locales with printf output
LC_NUMERIC=en_US.UTF-8

die() {
echo "$@"
exit 1
Expand Down Expand Up @@ -65,16 +68,16 @@ printf "%-${parameter_width}s| %-${data_width}s| %-${data_width}s\n" "Parameter"
printf "%s\n" "-------------------------------------" >> $report_file

# Print ISF, CSF and Carb Ratio Recommendations
printf "%-${parameter_width}s| %-${data_width}.3f| %-${data_width}.3f\n" "ISF" $isf_current $isf_new >> $report_file
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" $csf_current $csf_new >> $report_file
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" "n/a" $csf_new >> $report_file
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" $carb_ratio_current $carb_ratio_new >> $report_file
printf "%-${parameter_width}s| %-${data_width}.3f| %-${data_width}.3f\n" "Carb Ratio [g]" $carb_ratio_current $carb_ratio_new >> $report_file

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

# Build time_list array of H:M in 30 minute increments to mirror pump basal schedule
time_list=()
Expand Down
36 changes: 16 additions & 20 deletions bin/oref0-autotune.sh
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,13 @@ TERMINAL_LOGGING=true
RECOMMENDS_REPORT=true
UNKNOWN_OPTION=""

if [ -n "${API_SECRET_READ}" ]; then
HASHED_API_SECRET_READ=`echo -n ${API_SECRET_READ}|sha1sum|cut -f1 -d '-'|cut -f1 -d ' '`
if [ -n "${API_SECRET_READ}" ]; then
echo "WARNING: API_SECRET_READ is deprecated starting with oref 0.6.x. The Nightscout authentication information is now used from the API_SECRET environment variable"
fi

if [[ -z "$API_SECRET" ]]; then
echo "ERROR: 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 @@ -134,12 +139,12 @@ fi

# Get profile for testing copied to home directory. "openaps" is my loop directory name.
cd $directory && mkdir -p autotune
cp settings/pumpprofile.json autotune/profile.pump.json
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.
if [[ `uname` == 'Darwin' ]] ; then
cp settings/pumpprofile.json settings/profile.json
cp settings/pumpprofile.json settings/profile.json || die "Cannot copy settings/pumpprofile.json"
else
cp -up settings/pumpprofile.json settings/profile.json
cp -up settings/pumpprofile.json settings/profile.json || die "Cannot copy settings/pumpprofile.json"
fi
# If a previous valid settings/autotune.json exists, use that; otherwise start from settings/profile.json
cp settings/autotune.json autotune/profile.json && cat autotune/profile.json | json | grep -q start || cp autotune/profile.pump.json autotune/profile.json
Expand All @@ -156,13 +161,9 @@ fi
echo "Grabbing NIGHTSCOUT treatments.json for date range..."

# Get Nightscout carb and insulin Treatments
url="$NIGHTSCOUT_HOST/api/v1/treatments.json?find\[created_at\]\[\$gte\]=`date --date="$START_DATE -4 hours" -Iminutes`&find\[created_at\]\[\$lte\]=`date --date="$END_DATE +1 days" -Iminutes`"
echo $url
if [ -n "${HASHED_API_SECRET_READ}" ]; then
curl ${CURL_FLAGS} -H "api-secret: ${HASHED_API_SECRET_READ}" -s $url > ns-treatments.json || die "Couldn't download ns-treatments.json"
else
curl ${CURL_FLAGS} -s $url > ns-treatments.json || die "Couldn't download ns-treatments.json"
fi
query="find\[created_at\]\[\$gte\]=`date --date="$START_DATE -4 hours" -Iminutes`&find\[created_at\]\[\$lte\]=`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
Expand All @@ -183,14 +184,9 @@ echo "Grabbing NIGHTSCOUT entries/sgv.json for date range..."
# Get Nightscout BG (sgv.json) Entries
for i in "${date_list[@]}"
do
url="$NIGHTSCOUT_HOST/api/v1/entries/sgv.json?find\[date\]\[\$gte\]=`(date -d $i +%s | tr -d '\n'; echo 000)`&find\[date\]\[\$lte\]=`(date --date="$i +1 days" +%s | tr -d '\n'; echo 000)`&count=1000"
echo $url
if [ -n "${HASHED_API_SECRET_READ}" ]; then
curl ${CURL_FLAGS} -H "api-secret: ${HASHED_API_SECRET_READ}" -s $url > ns-entries.$i.json || die "Couldn't download ns-entries.$i.json"
else
curl ${CURL_FLAGS} -s $url > ns-entries.$i.json || die "Couldn't download ns-entries.$i.json"
fi

query="find\[date\]\[\$gte\]=`(date -d $i +%s | tr -d '\n'; echo 000)`&find\[date\]\[\$lte\]=`(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

Expand Down
5 changes: 4 additions & 1 deletion bin/oref0-pump-loop.sh
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ function smb_verify_status {
&& if grep -q '"suspended": true' monitor/status.json; then
echo -n "Pump suspended; "
unsuspend_if_no_temp
gather
fi
}
Expand Down Expand Up @@ -316,7 +317,9 @@ function preflight {
# reset radio, init world wide pump (if applicable), mmtune, and wait_for_silence 60 if no signal
function mmtune {
# TODO: remove reset_spi_serial.py once oref0_init_pump_comms.py is fixed to do it correctly
reset_spi_serial.py 2>/dev/null
if [[ $port == "/dev/spidev5.1" ]]; then
reset_spi_serial.py 2>/dev/null
fi
oref0_init_pump_comms.py
echo -n "Listening for 30s silence before mmtuning: "
for i in $(seq 1 800); do
Expand Down
14 changes: 11 additions & 3 deletions bin/oref0-setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,12 @@ if [[ $REPLY =~ ^[Yy]$ ]]; then
mkdir -p xdrip || die "Can't mkdir xdrip"
fi

# check whether decocare-0.0.31 has been installed
if ! ls /usr/local/lib/python2.7/dist-packages/decocare-0.0.31-py2.7.egg/ 2>/dev/null >/dev/null; then
# install decocare with setuptools since 0.0.31 (with the 6.4U/h fix) isn't published properly to pypi
sudo easy_install -U decocare || die "Can't easy_install decocare"
fi

mkdir -p $HOME/src/
if [ -d "$HOME/src/oref0/" ]; then
echo "$HOME/src/oref0/ already exists; pulling latest"
Expand All @@ -547,12 +553,12 @@ if [[ $REPLY =~ ^[Yy]$ ]]; then
#fi

cd $directory || die "Can't cd $directory"
if [[ "$max_iob" == "0" && -z "$max_daily_safety_multiplier" && -z "&current_basal_safety_multiplier" && -z "$bolussnooze_dia_divisor" && -z "$min_5m_carbimpact" ]]; then
if [[ "$max_iob" == "0" && -z "$max_daily_safety_multiplier" && -z "$current_basal_safety_multiplier" && -z "$bolussnooze_dia_divisor" && -z "$min_5m_carbimpact" ]]; then
oref0-get-profile --exportDefaults > preferences.json || die "Could not run oref0-get-profile"
else
preferences_from_args=()
if [[ "$max_iob" != "0" ]]; then
preferences_from_args+="\"max_iob\":$max_iob "
preferences_from_args+="\"max_iob\":$max_iob "
fi
if [[ ! -z "$max_daily_safety_multiplier" ]]; then
preferences_from_args+="\"max_daily_safety_multiplier\":$max_daily_safety_multiplier "
Expand Down Expand Up @@ -835,6 +841,8 @@ if [[ $REPLY =~ ^[Yy]$ ]]; then
cd $HOME/src/EdisonVoltage
make voltage
fi
# Add module needed for EdisonVoltage to work on jubilinux 0.2.0
grep iio_basincove_gpadc /etc/modules-load.d/modules.conf || echo iio_basincove_gpadc >> /etc/modules-load.d/modules.conf
cd $directory || die "Can't cd $directory"
for type in edisonbattery; do
echo importing $type file
Expand Down Expand Up @@ -959,7 +967,7 @@ if [[ $REPLY =~ ^[Yy]$ ]]; then
(crontab -l; crontab -l | grep -q "cd $directory-cgm-loop && oref0-truncate-git-history" || echo "* * * * * cd $directory-cgm-loop && oref0-truncate-git-history") | crontab -
(crontab -l; crontab -l | grep -q "cd $directory-cgm-loop && ps aux | grep -v grep | grep -q 'openaps monitor-cgm'" || echo "* * * * * cd $directory-cgm-loop && ps aux | grep -v grep | grep -q 'openaps monitor-cgm' || ( date; openaps monitor-cgm) | tee -a /var/log/openaps/cgm-loop.log; cp -up monitor/glucose-raw-merge.json $directory/cgm/glucose.json ; cp -up $directory/cgm/glucose.json $directory/monitor/glucose.json") | crontab -
elif [[ ${CGM,,} =~ "xdrip" ]]; then
(crontab -l; crontab -l | grep -q "cd $directory && ps aux | grep -v grep | grep -q 'openaps monitor-xdrip'" || echo "* * * * * cd $directory && ps aux | grep -v grep | grep -q 'openaps monitor-xdrip' || ( date; cp -rf xdrip/glucose.json xdrip/last-glucose.json; openaps monitor-xdrip) | tee -a /var/log/openaps/xdrip-loop.log; cmp --silent xdrip/glucose.json xdrip/last-glucose.json || cp -up $directory/xdrip/glucose.json $directory/monitor/glucose.json") | crontab -
(crontab -l; crontab -l | grep -q "cd $directory && ps aux | grep -v grep | grep -q 'monitor-xdrip.sh'" || echo "* * * * * cd $directory && ps aux | grep -v grep | grep -q 'monitor-xdrip.sh' || monitor-xdrip.sh | tee -a /var/log/openaps/xdrip-loop.log") | crontab -
(crontab -l; crontab -l | grep -q "xDripAPS.py" || echo "@reboot python $HOME/.xDripAPS/xDripAPS.py") | crontab -
elif [[ $ENABLE =~ dexusb ]]; then
(crontab -l; crontab -l | grep -q "@reboot .*dexusb-cgm" || echo "@reboot cd $directory && /usr/bin/python -u /usr/local/bin/oref0-dexusb-cgm-loop >> /var/log/openaps/cgm-dexusb-loop.log 2>&1" ) | crontab -
Expand Down
18 changes: 10 additions & 8 deletions lib/bolus.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ function reduce (treatments) {
function in_previous (ev) {
var found = false;
previous.forEach(function (elem) {
if (elem.timestamp == ev.timestamp && ev._type == elem._type) {
if (elem.timestamp === ev.timestamp && ev._type === elem._type) {
found = true;
}
});
Expand All @@ -30,7 +30,7 @@ function reduce (treatments) {

function bolus (ev, remaining) {
if (!ev) { console.error('XXX', ev, remaining); return; }
if (ev._type == 'BolusWizard') {
if (ev._type === 'BolusWizard') {
state.carbs = ev.carb_input.toString( );
state.ratio = ev.carb_ratio.toString( );
if (ev.bg) {
Expand All @@ -43,20 +43,22 @@ function reduce (treatments) {
previous.push(ev);
}

if (ev._type == 'Bolus') {
if (ev._type === 'Bolus') {
state.duration = ev.duration.toString( );
// if (state.square || state.bolus) { }
// state.insulin = (state.insulin ? state.insulin : 0) + ev.amount;
if (ev.duration && ev.duration > 0) {
state.square = ev;
} else {
state.bolus = ev;
if (state.bolus) {
state.bolus.amount = state.bolus.amount + ev.amount;
} else
state.bolus = ev;
}
state.created_at = state.timestamp = ev.timestamp;
previous.push(ev);
}


if (remaining && remaining.length > 0) {
if (state.bolus && state.wizard) {
// skip to end
Expand All @@ -69,8 +71,8 @@ function reduce (treatments) {
// console.error("remaining", remaining);
state.eventType = '<none>';

state.insulin = (state.square ? state.square.amount : 0) +
(state.bolus ? state.bolus.amount : 0);
state.insulin = (state.insulin ? state.insulin : 0) + (state.square ? state.square.amount : 0) +
(state.bolus ? state.bolus.amount : 0);
var has_insulin = state.insulin && state.insulin > 0;
var has_carbs = state.carbs && state.carbs > 0;
var has_wizard = state.wizard ? true : false;
Expand Down Expand Up @@ -151,7 +153,7 @@ function reduce (treatments) {
switch (current._type) {
case 'Bolus':
case 'BolusWizard':
var tail = within_minutes_from(current, treatments.slice(index+1), 4);
var tail = within_minutes_from(current, treatments.slice(index+1), 2);
bolus(current, tail);
break;
default:
Expand Down
10 changes: 7 additions & 3 deletions lib/determine-basal/determine-basal.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,15 +261,19 @@ var determine_basal = function determine_basal(glucose_status, currenttemp, iob_
// disable SMB when a high temptarget is set
if (profile.temptargetSet && target_bg > 100) {
enableSMB=false;
// enable SMB (if enabled in preferences) for DIA hours after bolus
// enable SMB/UAM (if enabled in preferences) for DIA hours after bolus
} else if (profile.enableSMB_with_bolus && bolusiob > 0.1) {
enableSMB=true;
// enable SMB (if enabled in preferences) while we have COB
// enable SMB/UAM (if enabled in preferences) while we have COB
} else if (profile.enableSMB_with_COB && meal_data.mealCOB) {
enableSMB=true;
// enable SMB (if enabled in preferences) if a low temptarget is set
// enable SMB/UAM (if enabled in preferences) if a low temptarget is set
} else if (profile.enableSMB_with_temptarget && (profile.temptargetSet && target_bg < 100)) {
enableSMB=true;
// enable SMB/UAM (if enabled in preferences) for a full 6 hours after any carb entry
// (6 hours is defined in carbWindow in lib/meal/total.js)
} else if (profile.enableSMB_after_carbs && meal_data.carbs) {
enableSMB=true;
}
// enable UAM (if enabled in preferences) for DIA hours after bolus, or if SMB is enabled
var enableUAM=(profile.enableUAM && (bolusiob > 0.1 || enableSMB));
Expand Down
9 changes: 9 additions & 0 deletions lib/iob/history.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,15 @@ function calcTempTreatments (inputs) {
temp.date = temp.started_at.getTime();
temp.insulin = current.amount;
tempBoluses.push(temp);
} else if (current.eventType == "Meal Bolus" || current.eventType == "Correction Bolus" || current.eventType == "Snack Bolus" || current.eventType == "Bolus Wizard") {
//imports treatments entered through Nightscout Care Portal
//"Bolus Wizard" refers to the Nightscout Bolus Wizard, not the Medtronic Bolus Wizard
var temp = {};
temp.timestamp = current.created_at;
temp.started_at = new Date(tz(temp.timestamp));
temp.date = temp.started_at.getTime();
temp.insulin = current.insulin;
tempBoluses.push(temp);
} else if (current.enteredBy == "xdrip") {
var temp = {};
temp.timestamp = current.timestamp;
Expand Down

0 comments on commit 32c032a

Please sign in to comment.