From 521d40a0455436b49f87459e590dde63ff9fd3f4 Mon Sep 17 00:00:00 2001 From: Ben West Date: Wed, 14 Oct 2015 14:31:34 -0700 Subject: [PATCH 01/12] add fail and warmup utilities --- bin/mm-stick.sh | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/bin/mm-stick.sh b/bin/mm-stick.sh index 00c7bfa..0552985 100755 --- a/bin/mm-stick.sh +++ b/bin/mm-stick.sh @@ -18,15 +18,27 @@ Usage: $0 [{scan,diagnose,help},...] scan - Print the local location of a plugged in stick. diagnose - Run python -m decocare.stick \$(python -m decocare.scan) + warmup - Runs scan and diagnose with no output. + Exits 0 on success, non-zero exit code + otherwise. insert - Insert usbserial kernel module. remove - Remove usbserial kernel module. udev-info - Print udev information about the stick. list-usb - List usb information about the stick. reset-usb - Reset entire usb stack. WARNING, be careful. + fail - Always return a failing exit code. help - This message. EOF } +function print_fail ( ) { + cat < /dev/null + ;; remove) eval modprobe -r usbserial ;; @@ -89,6 +104,10 @@ case $OPERATION in done done ;; + fail) + print_fail $* + exit 1 + ;; *|help) print_help ;; From b50bfad6ac381ba5711dc73001e2497ca80bf78c Mon Sep 17 00:00:00 2001 From: Scott Leibrand Date: Wed, 14 Oct 2015 21:34:05 -0700 Subject: [PATCH 02/12] rm loop.sh and replace with symlink to openaps-sh/loop.sh --- bin/loop.sh | 152 +--------------------------------------------------- 1 file changed, 1 insertion(+), 151 deletions(-) mode change 100755 => 120000 bin/loop.sh diff --git a/bin/loop.sh b/bin/loop.sh deleted file mode 100755 index 7036499..0000000 --- a/bin/loop.sh +++ /dev/null @@ -1,151 +0,0 @@ -#!/bin/bash - -# Attempt to read from a Carelink reader, upload data, and calculate the new -# glucose value. -# -# Released under MIT license. See the accompanying LICENSE.txt file for -# full terms and conditions -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -# THE SOFTWARE. - -PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin - -die() { - echo "$@" | tee -a /var/log/openaps/easy.log - exit 1 -} - -# remove any old stale lockfiles -find /tmp/openaps.lock -mmin +10 -exec rm {} \; 2>/dev/null > /dev/null - -# only one process can talk to the pump at a time -ls /tmp/openaps.lock >/dev/null 2>/dev/null && die "OpenAPS already running: exiting" && exit - -echo "No lockfile: continuing" -touch /tmp/openaps.lock - -# make sure decocare can talk to the Carelink USB stick -~/decocare/insert.sh 2>/dev/null >/dev/null -python -m decocare.stick $(python -m decocare.scan) >/dev/null && echo "decocare.scan OK" || sudo ~/openaps-js/bin/fix-dead-carelink.sh | tee -a /var/log/openaps/easy.log - -# sometimes git gets stuck -find ~/openaps-dev/.git/index.lock -mmin +5 -exec rm {} \; 2>/dev/null > /dev/null -cd ~/openaps-dev && ( git status > /dev/null || ( mv ~/openaps-dev/.git /tmp/.git-`date +%s`; cd && openaps init openaps-dev && cd openaps-dev ) ) -# sometimes openaps.ini gets truncated -openaps report show > /dev/null || cp openaps.ini.bak openaps.ini - -function finish { - rm /tmp/openaps.lock -} -trap finish EXIT - -# define functions for everything we'll be doing - -# get glucose data, either from attached CGM or from Share -getglucose() { - echo "Querying CGM" - ( ( openaps report invoke glucose.json.new || openaps report invoke glucose.json.new ) && grep -v '"glucose": 5' glucose.json.new | grep glucose ) || share2-bridge file glucose.json.new - if diff -u glucose.json glucose.json.new; then - echo No new glucose data - else - grep glucose glucose.json.new | head -1 | awk '{print $2}' | while read line; do echo -n " $line "; done >> /var/log/openaps/easy.log \ - && rsync -tu glucose.json.new glucose.json \ - && git commit -m"glucose.json has glucose data: committing" glucose.json - fi -} -# get pump status (suspended, etc.) -getpumpstatus() { - echo "Checking pump status" - openaps status || echo -n "!" >> /var/log/openaps/easy.log - grep -q status status.json.new && ( rsync -tu status.json.new status.json && echo -n "." >> /var/log/openaps/easy.log ) || echo -n "!" >> /var/log/openaps/easy.log -} -# query pump, and update pump data files if successful -querypump() { - openaps pumpquery || openaps pumpquery || echo -n "!" >> /var/log/openaps/easy.log - findclocknew && grep T clock.json.new && ( rsync -tu clock.json.new clock.json && echo -n "." >> /var/log/openaps/easy.log ) || echo -n "!" >> /var/log/openaps/easy.log - grep -q temp currenttemp.json.new && ( rsync -tu currenttemp.json.new currenttemp.json && echo -n "." >> /var/log/openaps/easy.log ) || echo -n "!" >> /var/log/openaps/easy.log - grep -q timestamp pumphistory.json.new && ( rsync -tu pumphistory.json.new pumphistory.json && echo -n "." >> /var/log/openaps/easy.log ) || echo -n "!" >> /var/log/openaps/easy.log - upload -} -# try to upload pumphistory data -upload() { - #findpumphistory && ~/bin/openaps-mongo.sh & - ping -c 1 google.com > /dev/null && touch /tmp/openaps.online -} -# if we haven't uploaded successfully in 10m, use offline mode (if no temp running, set current basal as temp to show the loop is working) -suggest() { - openaps suggest || echo -n "!" >> /var/log/openaps/easy.log - grep -q "too old" requestedtemp.online.json || ( find /tmp/openaps.online -mmin -10 | egrep -q '.*' && rsync -tu requestedtemp.online.json requestedtemp.json || rsync -tu requestedtemp.offline.json requestedtemp.json ) -} -# get updated pump settings (basal schedules, targets, ISF, etc.) -getpumpsettings() { ~/openaps-js/bin/pumpsettings.sh; } - -# functions for making sure we have up-to-date data before proceeding -findclocknew() { find clock.json.new -mmin -10 | egrep -q '.*'; } -findglucose() { find glucose.json -mmin -10 | egrep -q '.*'; } -findpumphistory() { find pumphistory.json -mmin -10 | egrep -q '.*'; } -findrequestedtemp() { find requestedtemp.json -mmin -10 | egrep -q '.*'; } -# write out current status to pebble.json -pebble() { ~/openaps-js/bin/pebble.sh; } - - -# main event loop - -getglucose -head -15 glucose.json - -numprocs=$(fuser -n file $(python -m decocare.scan) 2>&1 | wc -l) -if [[ $numprocs -gt 0 ]] ; then - die "Carelink USB already in use or not available." -fi - -getpumpstatus -echo "Querying pump" && querypump - -upload - -# get glucose again in case the pump queries took awhile -getglucose - -# if we're offline, set the clock to the pump/CGM time -~/openaps-js/bin/clockset.sh - -# dump out a "what we're about to try to do" report -suggest && pebble - -tail clock.json -tail currenttemp.json - -# make sure we're not using an old suggestion -rm requestedtemp* -# if we can't run suggest, it might be because our pumpsettings are missing or screwed up" -suggest || ( getpumpsettings && suggest ) || die "Can't calculate IOB or basal" -pebble -tail profile.json -tail iob.json -tail requestedtemp.json - -# don't act on stale glucose data -findglucose && grep -q glucose glucose.json || die "No recent glucose data" -# execute/enact the requested temp -cat requestedtemp.json | json_pp | grep reason >> /var/log/openaps/easy.log -grep -q rate requestedtemp.json && ( openaps enact || openaps enact ) && tail enactedtemp.json && ( echo && cat enactedtemp.json | egrep -i "bg|rate|dur|re|tic|tim" | sort -r ) >> /var/log/openaps/easy.log && cat iob.json | json_pp | grep '"iob' >> /var/log/openaps/easy.log - -echo "Re-querying pump" -query pump - -# unlock in case upload is really slow -rm /tmp/openaps.lock -pebble -upload - -# if another instance didn't start while we were uploading, refresh pump settings -ls /tmp/openaps.lock >/dev/null 2>/dev/null && die "OpenAPS already running: exiting" && exit -touch /tmp/openaps.lock -getpumpsettings diff --git a/bin/loop.sh b/bin/loop.sh new file mode 120000 index 0000000..3efe45d --- /dev/null +++ b/bin/loop.sh @@ -0,0 +1 @@ +../../openaps-sh/loop.sh \ No newline at end of file From b844c0ce542bf0565399ba6ae3932d34cd2672d0 Mon Sep 17 00:00:00 2001 From: Scott Leibrand Date: Wed, 14 Oct 2015 22:00:21 -0700 Subject: [PATCH 03/12] check /sys/devices/platform/bcm2708_usb/buspower and /sys/devices/platform/soc/3f980000.usb/buspower and use whichever exists --- bin/reset-usb.sh | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100755 bin/reset-usb.sh diff --git a/bin/reset-usb.sh b/bin/reset-usb.sh new file mode 100755 index 0000000..19d5dfe --- /dev/null +++ b/bin/reset-usb.sh @@ -0,0 +1,33 @@ +#!/bin/sh + +# Power-cycle the Raspberry Pi USB bus to reset attached USB devices +# +# Released under MIT license. See the accompanying LICENSE.txt file for +# full terms and conditions +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +# THE SOFTWARE. + +# Raspberry Pi 1 running Raspbian Wheezy +FILE=/sys/devices/platform/bcm2708_usb/buspower +if [ ! -e $FILE ]; then +# Raspberry Pi 2 running Raspbian Jessie + FILE=/sys/devices/platform/soc/3f980000.usb/buspower +fi +if [ -e $FILE ]; then + echo "Power-cycling USB to fix dead Carelink stick" + sleep 0.1 + echo 0 > $FILE + sleep 1 + echo 1 > $FILE + sleep 2 +else + echo "Could not find a known USB power control device. Checking /sys/devices/platform/:" + find /sys/devices/platform/* | grep buspower +fi + From 435c5c2fd2dc45eac3126134e83c86c2349131fc Mon Sep 17 00:00:00 2001 From: Scott Leibrand Date: Wed, 14 Oct 2015 22:03:07 -0700 Subject: [PATCH 04/12] rename reset-wifi-dongle.sh to reset-usb.sh --- bin/reset-wifi-dongle.sh | 21 --------------------- 1 file changed, 21 deletions(-) delete mode 100755 bin/reset-wifi-dongle.sh diff --git a/bin/reset-wifi-dongle.sh b/bin/reset-wifi-dongle.sh deleted file mode 100755 index b728725..0000000 --- a/bin/reset-wifi-dongle.sh +++ /dev/null @@ -1,21 +0,0 @@ -#!/bin/sh - -# Power-cycle the Raspberry Pi USB bus to reset attached USB devices -# -# Released under MIT license. See the accompanying LICENSE.txt file for -# full terms and conditions -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -# THE SOFTWARE. - -echo "Power-cycling USB to fix dead Carelink stick" -sleep 0.1 -echo 0 > /sys/devices/platform/bcm2708_usb/buspower -sleep 1 -echo 1 > /sys/devices/platform/bcm2708_usb/buspower -sleep 2 From 5dcc09b54bd434b9ef5bb70ea221ef022bdef78a Mon Sep 17 00:00:00 2001 From: Scott Leibrand Date: Fri, 16 Oct 2015 22:30:15 -0700 Subject: [PATCH 05/12] hard-code lower bounds for min_bg and max_bg in case pump is set too low, or units are wrong --- bin/oref0-get-profile.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/bin/oref0-get-profile.js b/bin/oref0-get-profile.js index 8c9d564..3b9771d 100755 --- a/bin/oref0-get-profile.js +++ b/bin/oref0-get-profile.js @@ -53,8 +53,9 @@ function bgTargetsLookup(){ break; } } - profile.max_bg = bgTargets.high; - profile.min_bg = bgTargets.low; + // hard-code lower bounds for min_bg and max_bg in case pump is set too low, or units are wrong + profile.max_bg = max(100,bgTargets.high); + profile.min_bg = max(90,bgTargets.low); } function carbRatioLookup() { From 58aa795acefd726c619002394425b5465bc4208e Mon Sep 17 00:00:00 2001 From: Scott Leibrand Date: Fri, 16 Oct 2015 22:40:17 -0700 Subject: [PATCH 06/12] hard-code upper bound for min_bg in case pump is set too high --- bin/oref0-get-profile.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/bin/oref0-get-profile.js b/bin/oref0-get-profile.js index 3b9771d..30dbeea 100755 --- a/bin/oref0-get-profile.js +++ b/bin/oref0-get-profile.js @@ -56,6 +56,8 @@ function bgTargetsLookup(){ // hard-code lower bounds for min_bg and max_bg in case pump is set too low, or units are wrong profile.max_bg = max(100,bgTargets.high); profile.min_bg = max(90,bgTargets.low); + // hard-code upper bound for min_bg in case pump is set too high + profile.min_bg = min(200,profile.min_bg); } function carbRatioLookup() { From 513a89ce8b03bb4efd650770dc939cff169abcff Mon Sep 17 00:00:00 2001 From: Scott Leibrand Date: Fri, 16 Oct 2015 22:55:44 -0700 Subject: [PATCH 07/12] this should cause get-profile to fail if bg_target units aren't in mg/dL --- bin/oref0-get-profile.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/bin/oref0-get-profile.js b/bin/oref0-get-profile.js index 30dbeea..98fa572 100755 --- a/bin/oref0-get-profile.js +++ b/bin/oref0-get-profile.js @@ -45,6 +45,11 @@ function bgTargetsLookup(){ var now = new Date(); //bgtargets_data.targets.sort(function (a, b) { return a.offset > b.offset }); + if (bgtargets_data.units != "mg/dL") { + console.error("bg_target units of " + bgtargets_data.units + " not supported: please use read_bg_targets_mg_dl."); + break; + } + var bgTargets = bgtargets_data.targets[bgtargets_data.targets.length - 1] for (var i = 0; i < bgtargets_data.targets.length - 1; i++) { From 718476bfe4ff5bbb8b8df172cfaa7256277916fc Mon Sep 17 00:00:00 2001 From: Ben West Date: Sat, 17 Oct 2015 13:31:41 -0700 Subject: [PATCH 08/12] fix js errors, error in case of unexpected units Eliminate illegal js, break keyword is not allowed outside of loops/switches. Cause process to error out in case of mmol. Prevent errors like this: foo$ oref0-get-profile monitor/pump-history.json tagrgets-mm ol.json /usr/local/lib/node_modules/openaps-js/bin/oref0-get-profile.js:50 break; ^^^^^ SyntaxError: Illegal break statement at Module._compile (module.js:439:25) at Object.Module._extensions..js (module.js:474:10) at Module.load (module.js:356:32) at Function.Module._load (module.js:312:12) at Function.Module.runMain (module.js:497:10) at startup (node.js:119:16) at node.js:901:3 bewest@hither:~/Documents/foo$ In favor of errors like this: o$ oref0-get-profile monitor/pump-history.json tagrgets-mmol.json monitor/insulin-sensitivities.json monitor/active-basal-profile.json monitor/carb-ratios.json max_iob.json BG Target data is expected to be expressed in mg/dL. Found mmol/L in tagrgets-mmol.json . bewest@hither:~/Documents/foo$ --- bin/oref0-get-profile.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/bin/oref0-get-profile.js b/bin/oref0-get-profile.js index 98fa572..85ab779 100755 --- a/bin/oref0-get-profile.js +++ b/bin/oref0-get-profile.js @@ -45,10 +45,6 @@ function bgTargetsLookup(){ var now = new Date(); //bgtargets_data.targets.sort(function (a, b) { return a.offset > b.offset }); - if (bgtargets_data.units != "mg/dL") { - console.error("bg_target units of " + bgtargets_data.units + " not supported: please use read_bg_targets_mg_dl."); - break; - } var bgTargets = bgtargets_data.targets[bgtargets_data.targets.length - 1] @@ -122,6 +118,11 @@ if (!module.parent) { var cwd = process.cwd() var pumpsettings_data = require(cwd + '/' + pumpsettings_input); var bgtargets_data = require(cwd + '/' + bgtargets_input); + if (bgtargets_data.units !== 'mg/dL') { + console.log('BG Target data is expected to be expressed in mg/dL.' + , 'Found', bgtargets_data.units, 'in', bgtargets_input, '.'); + process.exit(2); + } var isf_data = require(cwd + '/' + isf_input); var basalprofile_data = require(cwd + '/' + basalprofile_input); var carbratio_data = require(cwd + '/' + carbratio_input);; From 0644cb1e51d0cda2c8215f4b3624c5fd38ef94f0 Mon Sep 17 00:00:00 2001 From: Ben West Date: Sat, 17 Oct 2015 13:34:14 -0700 Subject: [PATCH 09/12] make sure the package.json matches the actual file If the file names don't match, the package will refuse to install. --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index abdf4ec..bef7800 100644 --- a/package.json +++ b/package.json @@ -23,7 +23,7 @@ "send-tempbasal-Azure": "./bin/send-tempbasal-Azure.js", "oref0-get-profile": "./bin/oref0-get-profile.js", "oref0-ifttt-notify": "./bin/oref0-ifttt-notify", - "reset-wifi-dongle": "bin/reset-wifi-dongle.sh", + "oref0-reset-usb": "bin/reset-usb.sh", "mm-format-ns-glucose": "./bin/mm-format-ns-glucose.sh", "mm-format-ns-pump-history": "./bin/mm-format-ns-pump-history.sh", "oref0": "./bin/oref0.sh", From eda93d32d566299403a544dd734a5709c84f25d4 Mon Sep 17 00:00:00 2001 From: Ben West Date: Sat, 17 Oct 2015 14:09:08 -0700 Subject: [PATCH 10/12] allow tests to run/pass --- Makefile | 2 +- package.json | 4 ++++ tests/determine-basal.test.js | 4 ++-- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index fc6528e..6e0d73e 100644 --- a/Makefile +++ b/Makefile @@ -3,4 +3,4 @@ TESTS=tests/*.js all: test test: - mocha ${TESTS} + ./node_modules/.bin/mocha ${TESTS} diff --git a/package.json b/package.json index bef7800..dc4f2c9 100644 --- a/package.json +++ b/package.json @@ -35,5 +35,9 @@ "homepage": "https://github.com/openaps/openaps-js", "dependencies": { "share2nightscout-bridge": "^0.1.5" + }, + "devDependencies": { + "mocha": "^2.3.3", + "should": "^7.1.0" } } diff --git a/tests/determine-basal.test.js b/tests/determine-basal.test.js index e296b34..f72970e 100644 --- a/tests/determine-basal.test.js +++ b/tests/determine-basal.test.js @@ -3,7 +3,7 @@ require('should'); describe('setTempBasal', function ( ) { - var determinebasal = require('../bin/determine-basal')(); + var determinebasal = require('../bin/oref0-determine-basal')(); //function setTempBasal(rate, duration, profile, requestedTemp) @@ -56,7 +56,7 @@ describe('setTempBasal', function ( ) { }); describe('determine-basal', function ( ) { - var determinebasal = require('../bin/determine-basal')(); + var determinebasal = require('../bin/oref0-determine-basal')(); //function determine_basal(glucose_status, currenttemp, iob_data, profile) From d1abb74df84e53f62deb618f06483e409e843abd Mon Sep 17 00:00:00 2001 From: Ben West Date: Sat, 17 Oct 2015 15:36:25 -0700 Subject: [PATCH 11/12] name package to oref0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index dc4f2c9..8eb544a 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "name": "openaps-js", + "name": "oref0", "version": "0.0.8", "description": "openaps js plugins", "scripts": { From 609159c1b246eede6660b7d2fb54fb24b72ec2ef Mon Sep 17 00:00:00 2001 From: Ben West Date: Sat, 17 Oct 2015 15:36:48 -0700 Subject: [PATCH 12/12] just change permission to make it executable --- bin/send-tempbasal-Azure.js | 0 1 file changed, 0 insertions(+), 0 deletions(-) mode change 100644 => 100755 bin/send-tempbasal-Azure.js diff --git a/bin/send-tempbasal-Azure.js b/bin/send-tempbasal-Azure.js old mode 100644 new mode 100755