From 42cc37ebe1ab119441909e4a28cce6cfe88e4054 Mon Sep 17 00:00:00 2001 From: Dana Lewis Date: Wed, 16 Sep 2015 15:29:16 -0700 Subject: [PATCH 01/12] Create README.md This is following up on #21 (@oskarpearson's suggested README - wasn't sure how else to put in my suggestions so doing it here). Here's my $.02 on what should be articulated in the readme, looking forward to additional input. cc @channemann and @scottleibrand for review; @bewest @audiefile and @alimazaheri have already given some initial input. --- README.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..8467143 --- /dev/null +++ b/README.md @@ -0,0 +1,27 @@ +# openaps-js + +Javascript plugins for openaps + +This is part of a series of tools to support a self-driven DIY +implementation based on the OpenAPS reference design. The tools may be +categorized as *monitor* (collecting data about environment, and +operational status of devices and/or aggregating as much data as is +relevant into one place), *predict* (make predictions about what should +happen next), or *control* (enacting changes, and feeding more data back +into the *monitor*). + +By proceeding using these tools or any piece within, you agree to the +copyright (see LICENSE.txt for more information) and release any +contributors from liability. + +*Note:* This is intended to be a set of tools to support a self-driven DIY +implementation and any person choosing to use these tools is solely +responsible for testing and implement these tools independently or +together as a system. The [DIY part of OpenAPS is important] +(http://bit.ly/1NBbZtO). While formal training or experience as an +engineer or a developer is not required, what *is* required is a growth +mindset to learn what are essentially "building blocks" to implement an +OpenAPS instance. This is not a "set and forget" system; it requires +diligent and consistent testing and monitoring to ensure each piece of +the system is monitoring, predicting, and performing as desired. The +performance and quality of your system lies solely with you. From 3742ae0696a654b3c5a1a530f042b0c5f1b318d5 Mon Sep 17 00:00:00 2001 From: Scott Leibrand Date: Sun, 20 Sep 2015 00:16:37 -0700 Subject: [PATCH 02/12] rm all requestedtemp* to make sure we're not using an old suggestion --- bin/loop.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/loop.sh b/bin/loop.sh index fec7abe..7036499 100755 --- a/bin/loop.sh +++ b/bin/loop.sh @@ -123,7 +123,7 @@ tail clock.json tail currenttemp.json # make sure we're not using an old suggestion -rm requestedtemp.json* +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 From f7120fe3c2bff85430c7851edc12d3c90e5eaab3 Mon Sep 17 00:00:00 2001 From: Scott Leibrand Date: Sun, 20 Sep 2015 16:36:53 -0700 Subject: [PATCH 03/12] removing everything removes max_iob.json and infrequently-changed settings. just remove anything ephemeral. --- bin/loop.sh | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/bin/loop.sh b/bin/loop.sh index 955d81f..cdc89f9 100755 --- a/bin/loop.sh +++ b/bin/loop.sh @@ -27,8 +27,12 @@ cd ~/openaps-dev/ || die "can't cd ~/openaps-dev/" # remove all requestedtemp* files on startup, just in case an old process is in an endless loop rm requestedtemp* 2>/dev/null -# delete any json files older than 10m to make triply sure we avoid using stale data -find *.json -mmin +10 -exec rm {} \; 2>/dev/null > /dev/null +# delete any recent-history json files older than 30m to make triply sure we avoid using stale data +find pumphistory*.json* -mmin +30 -exec rm {} \; 2>/dev/null > /dev/null +find clock*.json* -mmin +30 -exec rm {} \; 2>/dev/null > /dev/null +find *temp*.json* -mmin +30 -exec rm {} \; 2>/dev/null > /dev/null +find glucose*.json* -mmin +30 -exec rm {} \; 2>/dev/null > /dev/null +find iob*.json* -mmin +30 -exec rm {} \; 2>/dev/null > /dev/null # remove any old stale lockfiles find /tmp/openaps.lock -mmin +10 -exec rm {} \; 2>/dev/null > /dev/null From 8b495c8880961eaf841662c003180b221b6d7585 Mon Sep 17 00:00:00 2001 From: Scott Leibrand Date: Sun, 20 Sep 2015 17:05:01 -0700 Subject: [PATCH 04/12] forgot to add profile, rT, offline to one instance of setTempBasal() --- bin/determine-basal.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/determine-basal.js b/bin/determine-basal.js index 4a38130..fbcce80 100644 --- a/bin/determine-basal.js +++ b/bin/determine-basal.js @@ -263,7 +263,7 @@ function init() { rT.reason = "Eventual BG " + eventualBG + ">" + profile.max_bg + ", "; if (basal_iob > max_iob) { rT.reason = "basal_iob " + basal_iob + " > max_iob " + max_iob; - return determinebasal.setTempBasal(0, 0); + return determinebasal.setTempBasal(0, 0, profile, rT, offline); } // otherwise, calculate 30m high-temp required to get projected BG down to target // insulinReq is the additional insulin required to get down to max bg: From fe07b7cb9e0b0f670452aefedb9d8fe139a6d8ec Mon Sep 17 00:00:00 2001 From: Scott Leibrand Date: Sun, 20 Sep 2015 17:31:57 -0700 Subject: [PATCH 05/12] remove windows byte-order mark and add #!/usr/bin/env node --- bin/sendtempbasal-Azure.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/bin/sendtempbasal-Azure.js b/bin/sendtempbasal-Azure.js index 5f23730..28cef43 100644 --- a/bin/sendtempbasal-Azure.js +++ b/bin/sendtempbasal-Azure.js @@ -1,4 +1,6 @@ -/* +#!/usr/bin/env node + +/* Send Temporary Basal to Azure Copyright (c) 2015 OpenAPS Contributors From e19ea6745066cff515ee1f3f14f7d8ed0745a38f Mon Sep 17 00:00:00 2001 From: Scott Leibrand Date: Sun, 20 Sep 2015 17:43:13 -0700 Subject: [PATCH 06/12] use cwd + '/' instead of './' --- bin/sendtempbasal-Azure.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/bin/sendtempbasal-Azure.js b/bin/sendtempbasal-Azure.js index 28cef43..14bcdfd 100644 --- a/bin/sendtempbasal-Azure.js +++ b/bin/sendtempbasal-Azure.js @@ -29,9 +29,10 @@ if (!module.parent) { } } -var glucose_data = require('./' + glucose_input); -var enacted_temps = require('./' + enacted_temps_input); -var iob_data = require('./' + iob_input); +var cwd = process.cwd(); +var glucose_data = require(cwd + '/' + glucose_input); +var enacted_temps = require(cwd + '/' + enacted_temps_input); +var iob_data = require(cwd + '/' + iob_input); From 796e0628d027ad0b7a40a4e275642738a9189c40 Mon Sep 17 00:00:00 2001 From: Scott Leibrand Date: Sun, 20 Sep 2015 17:54:54 -0700 Subject: [PATCH 07/12] take webapi host as an argument instead of hardcoding --- bin/sendtempbasal-Azure.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/bin/sendtempbasal-Azure.js b/bin/sendtempbasal-Azure.js index 14bcdfd..32b6038 100644 --- a/bin/sendtempbasal-Azure.js +++ b/bin/sendtempbasal-Azure.js @@ -23,8 +23,9 @@ if (!module.parent) { var iob_input = process.argv.slice(2, 3).pop() var enacted_temps_input = process.argv.slice(3, 4).pop() var glucose_input = process.argv.slice(4, 5).pop() - if (!iob_input || !enacted_temps_input || !glucose_input) { - console.log('usage: ', process.argv.slice(0, 2), ' '); + var webapi = process.argv.slice(5, 6).pop() + if (!iob_input || !enacted_temps_input || !glucose_input || !webapi) { + console.log('usage: ', process.argv.slice(0, 2), ' <[your_webapi].azurewebsites.net>'); process.exit(1); } } @@ -49,7 +50,7 @@ var data = JSON.stringify({ ); var options = { - host: '[your_webapi].azurewebsites.net', + host: webapi, port: '443', path: '/api/openapstempbasals', method: 'POST', From e313fee9f96cc47f074f0c6e26cb0aa3a5e21dad Mon Sep 17 00:00:00 2001 From: Scott Leibrand Date: Sun, 20 Sep 2015 18:37:20 -0700 Subject: [PATCH 08/12] change site environment variable to NIGHTSCOUT_HOST --- bin/ns-upload.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bin/ns-upload.sh b/bin/ns-upload.sh index b8f6e6d..7805de3 100755 --- a/bin/ns-upload.sh +++ b/bin/ns-upload.sh @@ -15,5 +15,5 @@ cat $HISTORY | \ > $OUTPUT -# requires API_SECRET and site to be set in calling environment (i.e. in crontab) -curl -s -X POST --data-binary @$OUTPUT -H "API-SECRET: $API_SECRET" -H "content-type: application/json" $site/api/v1/entries.json >/dev/null && ( touch /tmp/openaps.online && echo "Uploaded $OUTPUT to $site." ) || echo "Unable to upload to $site." +# requires API_SECRET and NIGHTSCOUT_HOST to be set in calling environment (i.e. in crontab) +curl -s -X POST --data-binary @$OUTPUT -H "API-SECRET: $API_SECRET" -H "content-type: application/json" $NIGHTSCOUT_HOST/api/v1/entries.json >/dev/null && ( touch /tmp/openaps.online && echo "Uploaded $OUTPUT to $NIGHTSCOUT_HOST" ) || echo "Unable to upload to $NIGHTSCOUT_HOST" From de99c2945ccbb55c2921ce277b4e375438b7d2ac Mon Sep 17 00:00:00 2001 From: Scott Leibrand Date: Sun, 20 Sep 2015 18:38:12 -0700 Subject: [PATCH 09/12] functionalize enact and getcurrenttemp, add send-tempbasal-Azure if AZURE_SQL_API_HOST exists, and fine-tune failsafes --- bin/loop.sh | 46 +++++++++++++++++++++++++++++----------------- 1 file changed, 29 insertions(+), 17 deletions(-) diff --git a/bin/loop.sh b/bin/loop.sh index cdc89f9..757e57a 100755 --- a/bin/loop.sh +++ b/bin/loop.sh @@ -25,12 +25,11 @@ die() { # for now, make sure we're running in ~/openaps-dev/, or die. cd ~/openaps-dev/ || die "can't cd ~/openaps-dev/" -# remove all requestedtemp* files on startup, just in case an old process is in an endless loop -rm requestedtemp* 2>/dev/null -# delete any recent-history json files older than 30m to make triply sure we avoid using stale data +# delete any recent-history json files older than 30m to make doubly sure we avoid using stale data find pumphistory*.json* -mmin +30 -exec rm {} \; 2>/dev/null > /dev/null find clock*.json* -mmin +30 -exec rm {} \; 2>/dev/null > /dev/null -find *temp*.json* -mmin +30 -exec rm {} \; 2>/dev/null > /dev/null +find request*.json* -mmin +30 -exec rm {} \; 2>/dev/null > /dev/null +find current*.json* -mmin +30 -exec rm {} \; 2>/dev/null > /dev/null find glucose*.json* -mmin +30 -exec rm {} \; 2>/dev/null > /dev/null find iob*.json* -mmin +30 -exec rm {} \; 2>/dev/null > /dev/null @@ -87,12 +86,15 @@ getpumpstatus() { 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 +getcurrenttemp() { + openaps report invoke currenttemp.json.new 2>/dev/null || ( echo -n "!" >> /var/log/openaps/easy.log && return 1 ) + 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 +} querypump() { #openaps pumpquery 2>/dev/null || ( echo -n "!" >> /var/log/openaps/easy.log && return 1 ) openaps report invoke clock.json.new 2>/dev/null || ( echo -n "!" >> /var/log/openaps/easy.log && return 1 ) 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 - openaps report invoke currenttemp.json.new 2>/dev/null || ( echo -n "!" >> /var/log/openaps/easy.log && return 1 ) - 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 + getcurrenttemp || return 1 openaps report invoke pumphistory.json.new 2>/dev/null || ( echo -n "!" >> /var/log/openaps/easy.log && return 1 ) 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 @@ -109,6 +111,24 @@ 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 ) } +enact() { + retries=5 + retry=0 + until openaps enact; do + retry=`expr $retry + 1` + echo "enact failed; retry $retry" + if [ $retry -ge $retries ]; then bail "Failed to enact temp after $retries retries"; return $?; fi + sleep 10; + done + if tail enactedtemp.json; then + ( echo && cat enactedtemp.json | egrep -i "bg|dur|rate|re|tic|tim" | sort -r ) >> /var/log/openaps/easy.log + cat iob.json | json_pp | grep '"iob' >> /var/log/openaps/easy.log + if $AZURE_SQL_API_HOST; then + send-tempbasal-Azure iob.json enactedtemp.json glucose.json $AZURE_SQL_API_HOST + fi + return 0 + fi +} # get updated pump settings (basal schedules, targets, ISF, etc.) getpumpsettings() { ~/openaps-js/bin/pumpsettings.sh; } @@ -191,15 +211,7 @@ execute() { cat requestedtemp.json | json_pp | grep reason >> /var/log/openaps/easy.log if grep -q rate requestedtemp.json; then echo "Enacting temp" - retries=5 - retry=0 - until openaps enact; do - retry=`expr $retry + 1` - echo "enact failed; retry $retry" - if [ $retry -ge $retries ]; then bail "Failed to enact temp after $retries retries"; return $?; fi - sleep 10; - done - tail enactedtemp.json && ( echo && cat enactedtemp.json | egrep -i "bg|dur|rate|re|tic|tim" | sort -r ) >> /var/log/openaps/easy.log && cat iob.json | json_pp | grep '"iob' >> /var/log/openaps/easy.log && return 0 + enact fi } @@ -253,11 +265,11 @@ while(true); do getglucose && openaps report invoke requestedtemp.online.json && cat requestedtemp.online.json | json_pp | grep reason >> /var/log/openaps/easy.log # set a new reservoir baseline and watch for changes (boluses) openaps report invoke reservoir.json.new 2>/dev/null || echo -n "!" >> /var/log/openaps/easy.log && rsync -tu reservoir.json.new reservoir.json - openaps report invoke currenttemp.json.new 2>/dev/null || echo -n "!" >> /var/log/openaps/easy.log + getcurrenttemp until actionrequired; do touch /tmp/openaps.lock getglucose && openaps report invoke requestedtemp.online.json && cat requestedtemp.online.json | json_pp | grep reason >> /var/log/openaps/easy.log - openaps report invoke currenttemp.json.new 2>/dev/null || echo -n "!" >> /var/log/openaps/easy.log + getcurrenttemp openaps report invoke reservoir.json.new 2>/dev/null || echo -n "!" >> /var/log/openaps/easy.log openaps report invoke clock.json.new 2>/dev/null || 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 From a06aa40d421bcd23a1b570a558c7e0a0044de51c Mon Sep 17 00:00:00 2001 From: Scott Leibrand Date: Sun, 20 Sep 2015 18:51:45 -0700 Subject: [PATCH 10/12] only try ns-upload if NIGHTSCOUT_HOST exists --- bin/loop.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bin/loop.sh b/bin/loop.sh index 757e57a..a47ef24 100755 --- a/bin/loop.sh +++ b/bin/loop.sh @@ -102,9 +102,9 @@ querypump() { } # try to upload pumphistory data upload() { - #findpumphistory && ~/bin/openaps-mongo.sh & - ~/openaps-js/bin/ns-upload.sh - #ping -c 1 google.com > /dev/null && touch /tmp/openaps.online + if $NIGHTSCOUT_HOST; then + ~/openaps-js/bin/ns-upload.sh + fi } # 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() { From f6e1ccb1e5954869dd337f5c33c2703a2545325d Mon Sep 17 00:00:00 2001 From: Scott Leibrand Date: Sun, 20 Sep 2015 18:56:52 -0700 Subject: [PATCH 11/12] syntax --- bin/loop.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bin/loop.sh b/bin/loop.sh index a47ef24..bcfec3b 100755 --- a/bin/loop.sh +++ b/bin/loop.sh @@ -102,7 +102,7 @@ querypump() { } # try to upload pumphistory data upload() { - if $NIGHTSCOUT_HOST; then + if [ ! -z "$NIGHTSCOUT_HOST" ]; then ~/openaps-js/bin/ns-upload.sh fi } @@ -123,7 +123,7 @@ enact() { if tail enactedtemp.json; then ( echo && cat enactedtemp.json | egrep -i "bg|dur|rate|re|tic|tim" | sort -r ) >> /var/log/openaps/easy.log cat iob.json | json_pp | grep '"iob' >> /var/log/openaps/easy.log - if $AZURE_SQL_API_HOST; then + if [ ! -z "$AZURE_SQL_API_HOST" ]; then send-tempbasal-Azure iob.json enactedtemp.json glucose.json $AZURE_SQL_API_HOST fi return 0 From 142c99eced5fbb7f7b746dc58e019f2cd127d2dd Mon Sep 17 00:00:00 2001 From: Scott Leibrand Date: Tue, 22 Sep 2015 20:00:43 -0700 Subject: [PATCH 12/12] don't delete current_basal_profile.json --- bin/loop.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/loop.sh b/bin/loop.sh index bcfec3b..b745f88 100755 --- a/bin/loop.sh +++ b/bin/loop.sh @@ -29,7 +29,7 @@ cd ~/openaps-dev/ || die "can't cd ~/openaps-dev/" find pumphistory*.json* -mmin +30 -exec rm {} \; 2>/dev/null > /dev/null find clock*.json* -mmin +30 -exec rm {} \; 2>/dev/null > /dev/null find request*.json* -mmin +30 -exec rm {} \; 2>/dev/null > /dev/null -find current*.json* -mmin +30 -exec rm {} \; 2>/dev/null > /dev/null +find current*temp*.json* -mmin +30 -exec rm {} \; 2>/dev/null > /dev/null find glucose*.json* -mmin +30 -exec rm {} \; 2>/dev/null > /dev/null find iob*.json* -mmin +30 -exec rm {} \; 2>/dev/null > /dev/null