Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/0.7.0-dev' into 1043-to-0.7.0-dev
Browse files Browse the repository at this point in the history
  • Loading branch information
scottleibrand committed Jul 18, 2018
2 parents 39540a7 + e9d6710 commit 2606a3d
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 70 deletions.
141 changes: 87 additions & 54 deletions bin/oref0-pump-loop.sh
Original file line number Diff line number Diff line change
Expand Up @@ -128,76 +128,109 @@ function fail {

# The function "check_duty_cycle" checks if the loop has to run and it returns 0 if so.
# It exits the script with code 0 otherwise.
# The desicion is based on the time since last *successful* loop.
# The decision is based on the time since last *successful* loop.
# !Note duty cycle times are set in seconds.
#
# Additionally it may start an "emergency action" if enabled.
# Possible actions are usb power cycling or reboot the system.
# The EMERGENCY_ACTION variable sets the allowable time between successful loops.
# If no loop has completed in that time, it performs the enabled actions.
# !Note to enable a emergency action use 0 to enable and 1 to disable
# Possible actions are USB power cycling, SPI reset or reboot the system.
# The variables SPI_RESET, USB_RESET and REBOOT are setting the allowable time between successful loops.
# If no loop has completed in that time, it performs the respective action.
# !Note SPI reset is just tested for Pi0 and my produce errors on edison rigs.
# !Note to enable an emergency action put a number in seconds when it should start and a 0 to disable.
#
# The intention is two fold:
# The intention is two fold:
# First the battery consumption is reduced (Pump and Pi) if the loop runs less often.
# This is most dramatic for Enlite CGM, where wait_for_bg can't be used.
# Secondly, if Carelink USB is used with Enlite, and wait_for_silence can't be used, this
# prevents the loop from disrupting the communication between the pump and enlite sensors.
# prevents the loop from disrupting the communication between the pump and Enlite sensors.
#
# Use DUTY_CYCLE=0 (default) if you don't want to limit the loop
#
# Suggestion for Carelink USB users are
# DUTY_CYCLE=120
# EMERGENCY_ACTION=900
# REBOOT_ENABLE=0 #0=true
# USB_RESET_ENABLE=0 #0=true
# Suggestion for Carelink USB users are
# DUTY_CYCLE=120
# SPI_RESET=0
# USB_RESET=300
# REBOOT=900
#
# Suggestion for PI HAT + MDT users are
# DUTY_CYCLE=120
# SPI_RESET=0
# USB_RESET=0
# REBOOT=900
#
# Default is DUTY_CYCLE=0 to disable this feature.
DUTY_CYCLE=${DUTY_CYCLE:-0}

EMERGENCY_ACTION=${EMERGENCY_ACTION:-900}
REBOOT_ENABLE=${REBOOT_ENABLE:-1} #0=true
USB_RESET_ENABLE=${USB_RESET_ENABLE:-1} #0=true

function check_duty_cycle {
if [ "$DUTY_CYCLE" -gt "0" ]; then
if [ -e /tmp/pump_loop_success ]; then
DIFF_SECONDS=$(expr $(date +%s) - $(stat -c %Y /tmp/pump_loop_success))

if ([ $USB_RESET_ENABLE ] || [ $REBOOT_ENABLE ]) && [ "$DIFF_SECONDS" -gt "$EMERGENCY_ACTION" ]; then
if [ $USB_RESET_ENABLE ]; then
USB_RESET_DIFF=$EMERGENCY_ACTION
if [ -e /tmp/usp_power_cycled ]; then
USB_RESET_DIFF=$(expr $(date +%s) - $(stat -c %Y /tmp/usp_power_cycled))
fi
DUTY_CYCLE=${DUTY_CYCLE:-0} #0=off, other = delay in seconds

if [ "$USB_RESET_DIFF" -gt "$EMERGENCY_ACTION" ]; then
# file is old --> power-cycling is long time ago (most probably not this round) --> power-cycling
echo -n "$DIFF_SECONDS (of $DUTY_CYCLE) since last run --> trying to reset USB... "
/usr/local/bin/oref0-reset-usb 2>&3 >&4
touch /tmp/usp_power_cycled
echo " done. --> start new cycle."
return 0 #return to loop routine
fi
fi
# if usb reset doesn't help or is not enabled --> reboot system
if [ $REBOOT_ENABLE ]; then
echo "$DIFF_SECONDS (of $DUTY_CYCLE) since last run --> rebooting."
sudo shutdown -r now
exit 0
fi
elif [ "$DIFF_SECONDS" -gt "$DUTY_CYCLE" ]; then
echo "$DIFF_SECONDS (of $DUTY_CYCLE) since last run --> start new cycle."
return 0
else
echo "$DIFF_SECONDS (of $DUTY_CYCLE) since last run --> stop now."
exit 0
SPI_RESET=${SPI_RESET:-0} #0=off, other = delay in seconds
USB_RESET=${USB_RESET:-0} #0=off, other = delay in seconds
REBOOT=${REBOOT:-0} #0=off, other = delay in seconds

function check_duty_cycle {
if [ -e /tmp/pump_loop_success ]; then
DIFF_SECONDS=$(expr $(date +%s) - $(stat -c %Y /tmp/pump_loop_success))

if [ "$SPI_RESET" -gt "0" ] && [ "$DIFF_SECONDS" -gt "$SPI_RESET" ]; then
# try to reset usb to fix potential communication problems causing loop fails
SPI_RESET_DIFF=$SPI_RESET
if [ -e /tmp/spi_reset ]; then
SPI_RESET_DIFF=$(expr $(date +%s) - $(stat -c %Y /tmp/spi_reset))
fi
else
echo "/tmp/pump_loop_success does not exist; create it to start the loop duty cycle."
# if pump_loop_success does not exist, use the system uptime
touch -d "$(cat /proc/uptime | awk '{print $1}') seconds ago" /tmp/pump_loop_success

if [ "$SPI_RESET_DIFF" -ge "$SPI_RESET" ]; then
# file is old --> power-cycling is long time ago (most probably not this round) --> power-cycling
echo -n "$DIFF_SECONDS (of $DUTY_CYCLE) since last run --> try to reset spi... "
rmmod spi_bcm2835 2>&3 >&4
sleep 1
modprobe spi_bcm2835 2>&3 >&4
touch /tmp/spi_reset
echo "$(date '+%Y-%m-%d %H:%M:%S') SPI Reset" >> /var/log/openaps/hard_reset.log
echo " done. --> start new cycle."
return 0 #return to loop routine
fi
fi

if [ "$USB_RESET" -gt "0" ] && [ "$DIFF_SECONDS" -gt "$USB_RESET" ]; then
# try to reset usb to fix potential communication problems causing loop fails
USB_RESET_DIFF=$USB_RESET
if [ -e /tmp/usb_power_cycled ]; then
USB_RESET_DIFF=$(expr $(date +%s) - $(stat -c %Y /tmp/usb_power_cycled))
fi

if [ "$USB_RESET_DIFF" -ge "$USB_RESET" ]; then
# file is old --> power-cycling is long time ago (most probably not this round) --> power-cycling
echo -n "$DIFF_SECONDS (of $DUTY_CYCLE) since last run --> try to reset usb... "
/usr/local/bin/oref0-reset-usb 2>&3 >&4
touch /tmp/usb_power_cycled
echo "$(date '+%Y-%m-%d %H:%M:%S') USB Reset" >> /var/log/openaps/hard_reset.log
echo " done. --> start new cycle."
return 0 #return to loop routine
fi
fi

if [ "$REBOOT" -gt "0" ] && [ "$DIFF_SECONDS" -gt "$REBOOT" ]; then
# if usb reset doesn't help or is not enabled --> reboot system
echo "$DIFF_SECONDS (of $DUTY_CYCLE) since last run --> emergency reboot."
echo "$(date '+%Y-%m-%d %H:%M:%S') reboot system" >> /var/log/openaps/hard_reset.log
sudo shutdown -r now
exit 0
fi

if [ "$DUTY_CYCLE" -gt "0" ] && [ "$DIFF_SECONDS" -gt "$DUTY_CYCLE" ]; then
echo "$DIFF_SECONDS (of $DUTY_CYCLE) since last run --> start new cycle."
return 0
elif [ "$DUTY_CYCLE" -eq "0" ]; then
#fast exit if duty cycling is disabled
echo "duty cycling disabled; start loop"
return 0
else
echo "$DIFF_SECONDS (of $DUTY_CYCLE) since last run --> stop now."
exit 0
fi
else
echo "/tmp/pump_loop_success does not exist; create it to start the loop duty cycle."
# do not use timestamp from system uptime, since this could result in a endless reboot loop...
touch /tmp/pump_loop_success
return 0
fi
}

Expand Down
2 changes: 1 addition & 1 deletion lib/determine-basal/determine-basal.js
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ var determine_basal = function determine_basal(glucose_status, currenttemp, iob_
//console.error(csf * meal_data.carbs);
// meal_carbimpact (mg/dL/5m) = CSF (mg/dL/g) * carbs (g) / 6 (h) * (1h/60m) * 5 (m/5m) * 2 (for linear decay)
//var meal_carbimpact = round((csf * meal_data.carbs / 6 / 60 * 5 * 2),1)
var remainingCATimeMin = 3; // h; before carb absorption starts
var remainingCATimeMin = 3; // h; duration of expected not-yet-observed carb absorption
// adjust remainingCATime (instead of CR) for autosens
remainingCATimeMin = remainingCATimeMin / sensitivityRatio;
// 20 g/h means that anything <= 60g will get a remainingCATimeMin, 80g will get 4h, and 120g 6h
Expand Down
37 changes: 24 additions & 13 deletions tests/json-wf.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,29 @@ red='\033[1;31m'
green='\033[1;32m'
nocolor='\033[0m'

for jsonfile in $(find ${myloc}/.. -name '*.json'); do
realfile=$(readlink -f ${jsonfile})
output=$(jq . ${jsonfile} 2>&1)
ret=$?
# accumulate failures
rc=$((rc|ret))
if [ ${ret} -eq 0 ]; then
echo -e "${realfile} ${green}valid${nocolor}"
else
echo -e "${red}${realfile} invalid:${nocolor}"
echo -e "${red}${realfile} ${output}${nocolor}"
fi
done
jsonfiles=$(find ${myloc}/.. -name '*.json')
#echo jq -s . ${jsonfiles} 2>&1
output=$(jq -s . ${jsonfiles} 2>&1)
ret=$?
#echo $ret
# accumulate failures
rc=$((rc|ret))
#echo $rc
if ! [ ${ret} -eq 0 ]; then
for jsonfile in $(find ${myloc}/.. -name '*.json'); do
realfile=$(readlink -f ${jsonfile})
output=$(jq . ${jsonfile} 2>&1)
ret=$?
# accumulate failures
rc=$((rc|ret))
if ! [ ${ret} -eq 0 ]; then
echo -e "${red}${realfile} invalid:${nocolor}"
echo -e "${red}${realfile} ${output}${nocolor}"
#else
#echo -e "${realfile} ${green}valid${nocolor}"
fi
done
fi

#echo ${rc}
exit ${rc}
5 changes: 3 additions & 2 deletions tests/tests-in-shell.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ describe("shell-script tests", function() {
encoding: "UTF-8",
});

should.equal(utilProcess.status, 0, "Bash unit test returned failure.");
//console.error(utilProcess.error);
should.equal(utilProcess.status, 0, "Bash unit test returned failure: run " + testFile + " manualy for details.");
});
});
});
});

0 comments on commit 2606a3d

Please sign in to comment.