Skip to content

Commit

Permalink
Optimize oref0-pump-loop, and bug fixes (openaps#772)
Browse files Browse the repository at this point in the history
* standardize on openaps report invoke

* not-yet-tested refactoring of monitor-pump, meal.json, etc.

* more refactoring for speed

* redirect output to the right files

* don't display full traceback on time set errors

* only run refresh_ commands if we don't have new glucose data yet

* remove end-of-line whitespace

* update pump_loop_completed before glucose-fresh checks

* reset pump clock if >55s off; better error messaging

* syntax

* nest ifs so we quit immediately if fresh

* hide red-herring 'date: invalid date ‘@’' message

* don't redirect oref0-meal stderr to meal.json

* more accurate log message

* show monitor/status.json if it's bolusing

* allow bolus and bolus wizard timestamps to differ by 1 second

* boluswizard uses current.carb_input not current.carbs

* warn if Treatment carbs unclassified

* profile.A52_risk_enable variable reference

* don't wait_for_bg after a Warning

* replace () with { ;}

* use try and retry functions

* retry basal_profile.json pull if it fails

* Couldn't

* curly braces and clearer error

* syntax

* exclude .pyc files from git

* lots more try, move suspended check to fail function

* log when Reducing insulinReq for expectedDelta

* try inside smb_suggest

* grammar

* wait_for_bg if too old; clarify error messages

* not sure how that got removed

* touch pump_loop_completed to wait for a new BG

* newlines

* check that profile has a valid current_basal

* differentiate things we can continue from if they fail

* "grep: enact/smb-suggested.json: No such file or directory" is not useful

* rename refresh_pumphistory_and_meal for error clarity

* continuing

* "grep: enact/smb-suggested.json: No such file or directory" is not useful

* install ntpdate

* only show reason too old if it's recent

* make sure status.json is fresh before checking if it says pump is suspended

* use try_return to more clearly log when ESC or bolus fails

* move calculate_iob into merge_pumphistory as it requires pumphistory-merged.json

* syntax
  • Loading branch information
scottleibrand authored Nov 12, 2017
1 parent 83ff660 commit a3f82a8
Show file tree
Hide file tree
Showing 9 changed files with 207 additions and 124 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@ npm-debug.log
bin/__pycache__

package-lock.json

*.pyc
2 changes: 1 addition & 1 deletion bin/oref0-ns-loop.sh
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ function ns_temptargets {
cat settings/ns-temptargets.json.new | jq .[0].duration | egrep -q [0-9] && mv settings/ns-temptargets.json.new settings/ns-temptargets.json
# TODO: merge local-temptargets.json with ns-temptargets.json
#openaps report invoke settings/ns-temptargets.json settings/profile.json
echo -n "Refreshed NS temptargets: "
echo -n "Latest NS temptargets: "
cat settings/ns-temptargets.json | jq -c -C '.[0] | { target: .targetBottom, duration: .duration, start: .created_at }'
# delete any local-temptarget files last modified more than 24h ago
find settings/local-temptarget* -mmin +1440 -exec rm {} \;
Expand Down
274 changes: 170 additions & 104 deletions bin/oref0-pump-loop.sh

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions bin/oref0-set-device-clocks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ checkNTP() { ntp-wait -n 1 -v || ( sudo /etc/init.d/ntp restart && ntp-wait -n 1

if checkNTP; then
sudo ntpdate -s -b time.nist.gov
echo Setting pump and CGM time to $(date)
openaps use $PUMP set_clock --to now
openaps use $CGM UpdateTime --to now
echo Setting pump time to $(date)
openaps use $PUMP set_clock --to now 2>&1 >/dev/null | tail -1
echo Setting CGM time to $(date)
openaps use $CGM UpdateTime --to now 2>&1 >/dev/null | tail -1
fi
28 changes: 15 additions & 13 deletions bin/oref0-setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ if [[ -z "$DIR" || -z "$serial" ]]; then
serial=$REPLY
echocolor "Ok, $serial it is."
echo

read -p "Do you have an x12 (i.e. 512 or 712) pump? y/[N] " -r
if [[ $REPLY =~ ^[Yy]$ ]]; then
pumpmodel=x12
Expand Down Expand Up @@ -626,21 +626,21 @@ if [[ $REPLY =~ ^[Yy]$ ]]; then
cp $HOME/src/oref0/headless/interfaces.ap /etc/network/ || die "Couldn't copy interfaces.ap"
cp /etc/network/interfaces /etc/network/interfaces.client || die "Couldn't copy interfaces.client"
#Stop automatic startup of hostapd & dnsmasq
update-rc.d -f hostapd remove
update-rc.d -f dnsmasq remove
update-rc.d -f hostapd remove
update-rc.d -f dnsmasq remove
# Edit /etc/hostapd/hostapd.conf for wifi using Hostname
sed -i.bak -e "s/ssid=OpenAPS/ssid=${HOSTNAME}/" /etc/hostapd/hostapd.conf
# Add Commands to /etc/rc.local
# Add Commands to /etc/rc.local
# Interrupt Kernel Messages
if ! grep -q 'sudo dmesg -n 1' /etc/rc.local; then
sed -i.bak -e '$ i sudo dmesg -n 1' /etc/rc.local
fi
fi
# Add to /etc/rc.local to check if in hotspot mode and turn back to client mode during bootup
if ! grep -q 'cp /etc/network/interfaces.client /etc/network/interfaces' /etc/rc.local; then
sed -i.bak -e "$ i if [ -f /etc/network/interfaces.client ]; then\n\tif grep -q '#wpa-' /etc/network/interfaces; then\n\t\tsudo ifdown wlan0\n\t\tsudo cp /etc/network/interfaces.client /etc/network/interfaces\n\t\tsudo ifup wlan0\n\tfi\nfi" /etc/rc.local || die "Couldn't modify /etc/rc.local"
fi
fi
fi

# add/configure devices
if [[ ${CGM,,} =~ "g5" || ${CGM,,} =~ "g5-upload" ]]; then
openaps use cgm config --G5
Expand Down Expand Up @@ -848,19 +848,19 @@ if [[ $REPLY =~ ^[Yy]$ ]]; then
done
touch /tmp/reboot-required
fi

# disable IPv6
if ! grep -q 'net.ipv6.conf.all.disable_ipv6=1' /etc/sysctl.conf; then
sudo echo 'net.ipv6.conf.all.disable_ipv6=1' >> /etc/sysctl.conf
fi
fi
if ! grep -q 'net.ipv6.conf.default.disable_ipv6=1' /etc/sysctl.conf; then
sudo echo 'net.ipv6.conf.default.disable_ipv6=1' >> /etc/sysctl.conf
fi
fi
if ! grep -q 'net.ipv6.conf.lo.disable_ipv6=1' /etc/sysctl.conf; then
sudo echo 'net.ipv6.conf.lo.disable_ipv6=1' >> /etc/sysctl.conf
fi
fi
sudo sysctl -p

# Install EdisonVoltage
if [[ "$ttyport" =~ "spidev5.1" ]]; then
if egrep -i "edison" /etc/passwd 2>/dev/null; then
Expand Down Expand Up @@ -927,7 +927,7 @@ if [[ $REPLY =~ ^[Yy]$ ]]; then
echo "settings removed, getting ready to add x12 settings"
openaps alias add get-settings "report invoke settings/model.json settings/bg_targets.json settings/insulin_sensitivities_raw.json settings/insulin_sensitivities.json settings/carb_ratios.json settings/profile.json" || die "Could not add x12 settings"
else
sudo apt-get -y install bc jq
sudo apt-get -y install bc jq ntpdate
cd $directory || die "Can't cd $directory"
for type in supermicrobolus; do
echo importing $type file
Expand Down Expand Up @@ -1026,6 +1026,8 @@ if [[ $REPLY =~ ^[Yy]$ ]]; then
(crontab -l; crontab -l | grep -q "oref0-radio-reboot" || echo "* * * * * oref0-radio-reboot") | crontab -
fi
(crontab -l; crontab -l | grep -q "cd $directory && ( ps aux | grep -v grep | grep bash | grep -q 'bin/oref0-pump-loop'" || echo "* * * * * cd $directory && ( ps aux | grep -v grep | grep bash | grep -q 'bin/oref0-pump-loop' || oref0-pump-loop ) 2>&1 | tee -a /var/log/openaps/pump-loop.log") | crontab -
# try to start oref0-pump-loop every 30s
(crontab -l; crontab -l | grep -q "cd $directory && sleep 30 && ( ps aux | grep -v grep | grep bash | grep -q 'bin/oref0-pump-loop'" || echo "* * * * * cd $directory && sleep 30 && ( ps aux | grep -v grep | grep bash | grep -q 'bin/oref0-pump-loop' || oref0-pump-loop ) 2>&1 | tee -a /var/log/openaps/pump-loop.log") | crontab -
if [[ ! -z "$BT_PEB" ]]; then
(crontab -l; crontab -l | grep -q "cd $directory && ( ps aux | grep -v grep | grep -q 'peb-urchin-status $BT_PEB '" || echo "* * * * * cd $directory && ( ps aux | grep -v grep | grep -q 'peb-urchin-status $BT_PEB' || peb-urchin-status $BT_PEB ) 2>&1 | tee -a /var/log/openaps/urchin-loop.log") | crontab -
fi
Expand Down
4 changes: 2 additions & 2 deletions lib/determine-basal/determine-basal.js
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ var determine_basal = function determine_basal(glucose_status, currenttemp, iob_
// enable SMB/UAM (if enabled in preferences) while we have COB
} else if (profile.enableSMB_with_COB && meal_data.mealCOB) {
if (meal_data.bwCarbs) {
if (A52_risk_enable) {
if (profile.A52_risk_enable) {
console.error("Warning: SMB enabled with Bolus Wizard carbs: be sure to easy bolus 30s before using Bolus Wizard")
enableSMB=true;
} else {
Expand Down Expand Up @@ -1009,7 +1009,7 @@ var determine_basal = function determine_basal(glucose_status, currenttemp, iob_
// to the what fraction of expectedDelta we're dropping at
if (minDelta < 0 && minDelta > expectedDelta) {
var newinsulinReq = round(( insulinReq * (1 - (minDelta / expectedDelta)) ), 2);
//console.error("Reducing insulinReq from " + insulinReq + " to " + newinsulinReq);
console.error("Reducing insulinReq from " + insulinReq + " to " + newinsulinReq + " for minDelta " + minDelta + " vs. expectedDelta " + expectedDelta);
insulinReq = newinsulinReq;
}
// if that would put us over max_iob, then reduce accordingly
Expand Down
10 changes: 9 additions & 1 deletion lib/meal/history.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ function arrayHasElementWithSameTimestampAndProperty(array,t,propname) {
for (var j=0; j < array.length; j++) {
var element = array[j];
if (element.timestamp == t && element[propname] != undefined) return true;
if ( element[propname] != undefined ) {
eDate = new Date(element.timestamp);
tDate = new Date(t);
tMin = new Date(tDate.getTime() - 2000);
tMax = new Date(tDate.getTime() + 2000);
//console.error(tDate, tMin, tMax);
if (eDate > tMin && eDate < tMax) return true;
}
}
return false;
}
Expand Down Expand Up @@ -49,7 +57,7 @@ function findMealInputs (inputs) {
var temp = {};
temp.timestamp = current.timestamp;
temp.carbs = current.carb_input;
temp.bwCarbs = current.carbs;
temp.bwCarbs = current.carb_input;

// don't enter the treatment if there's another treatment with the same exact timestamp
// to prevent duped carb entries from multiple sources
Expand Down
4 changes: 4 additions & 0 deletions lib/meal/total.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ function recentCarbs(opts, time) {
} else if (treatment.bwCarbs >= 1) {
bwCarbs += parseFloat(treatment.bwCarbs);
bwFound = true;
} else {
console.error("Treatment carbs unclassified:",treatment);
}
//console.error(treatment.carbs, maxCarbs, treatmentDate);
carbs += parseFloat(treatment.carbs);
Expand Down Expand Up @@ -97,10 +99,12 @@ function recentCarbs(opts, time) {

// if currentDeviation is null or maxDeviation is 0, set mealCOB to 0 for zombie-carb safety
if (typeof(c.currentDeviation) === 'undefined' || c.currentDeviation === null) {
console.error("");
console.error("Warning: setting mealCOB to 0 because currentDeviation is null/undefined");
mealCOB = 0;
}
if (typeof(c.maxDeviation) === 'undefined' || c.maxDeviation === null) {
console.error("");
console.error("Warning: setting mealCOB to 0 because maxDeviation is 0 or undefined");
mealCOB = 0;
}
Expand Down
Binary file removed www/app.pyc
Binary file not shown.

0 comments on commit a3f82a8

Please sign in to comment.