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/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 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 ;; diff --git a/bin/oref0-get-profile.js b/bin/oref0-get-profile.js index 8c9d564..85ab779 100755 --- a/bin/oref0-get-profile.js +++ b/bin/oref0-get-profile.js @@ -45,6 +45,7 @@ function bgTargetsLookup(){ var now = new Date(); //bgtargets_data.targets.sort(function (a, b) { return a.offset > b.offset }); + var bgTargets = bgtargets_data.targets[bgtargets_data.targets.length - 1] for (var i = 0; i < bgtargets_data.targets.length - 1; i++) { @@ -53,8 +54,11 @@ 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); + // hard-code upper bound for min_bg in case pump is set too high + profile.min_bg = min(200,profile.min_bg); } function carbRatioLookup() { @@ -114,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);; diff --git a/bin/reset-wifi-dongle.sh b/bin/reset-usb.sh similarity index 55% rename from bin/reset-wifi-dongle.sh rename to bin/reset-usb.sh index b728725..19d5dfe 100755 --- a/bin/reset-wifi-dongle.sh +++ b/bin/reset-usb.sh @@ -13,9 +13,21 @@ # 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 +# 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 + diff --git a/bin/send-tempbasal-Azure.js b/bin/send-tempbasal-Azure.js old mode 100644 new mode 100755 diff --git a/package.json b/package.json index abdf4ec..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": { @@ -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", @@ -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)