Skip to content

Commit

Permalink
Merge pull request #105 from openaps/dev
Browse files Browse the repository at this point in the history
release dev to master for 0.2.0
  • Loading branch information
scottleibrand committed May 16, 2016
2 parents 36264a0 + 1979e98 commit d320460
Show file tree
Hide file tree
Showing 59 changed files with 3,605 additions and 435 deletions.
26 changes: 22 additions & 4 deletions bin/clockset.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,33 @@

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

CLOCK=${1-monitor/clock.json}
GLUCOSE=${2-monitor/glucose.json}

die() { echo "$@" ; exit 1; }
self=$(basename $0)
function usage ( ) {

cat <<EOF
$self
$self - Restart NTP, manipulate realtime clock
EOF
}

case "$1" in
--help|help|-h)
usage
exit 0
;;
esac


ntp-wait -n 1 -v && die "NTP already synchronized." || ( sudo /etc/init.d/ntp restart && ntp-wait -n 1 -v && die "NTP re-synchronized." )

cd ~/openaps-dev
( cat clock.json; echo ) | sed 's/"//g' | sed "s/$/`date +%z`/" | while read line; do date -u -d $line +"%F %R:%S"; done > fake-hwclock.data
( cat $CLOCK; echo ) | sed 's/"//g' | sed "s/$/`date +%z`/" | while read line; do date -u -d $line +"%F %R:%S"; done > fake-hwclock.data
grep : fake-hwclock.data && sudo cp fake-hwclock.data /etc/fake-hwclock.data
sudo fake-hwclock load
grep -q display_time glucose.json && grep display_time glucose.json | head -1 | awk '{print $2}' | sed "s/,//" | sed 's/"//g' | sed "s/$/`date +%z`/" | while read line; do date -u -d $line +"%F %R:%S"; done > fake-hwclock.data
grep -q dateString glucose.json && grep dateString glucose.json | head -1 | awk '{print $2}' | sed "s/,//" | sed 's/"//g' |while read line; do date -u -d $line +"%F %R:%S"; done > fake-hwclock.data
grep -q display_time $GLUCOSE && grep display_time $GLUCOSE | head -1 | awk '{print $2}' | sed "s/,//" | sed 's/"//g' | sed "s/$/`date +%z`/" | while read line; do date -u -d $line +"%F %R:%S"; done > fake-hwclock.data
grep -q dateString $GLUCOSE && grep dateString $GLUCOSE | head -1 | awk '{print $2}' | sed "s/,//" | sed 's/"//g' |while read line; do date -u -d $line +"%F %R:%S"; done > fake-hwclock.data
grep : fake-hwclock.data && sudo cp fake-hwclock.data /etc/fake-hwclock.data
sudo fake-hwclock load
15 changes: 15 additions & 0 deletions bin/mm-format-ns-glucose.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,21 @@
NSONLY=""
test "$1" = "--oref0" && NSONLY="this.glucose = this.sgv" && shift

self=$(basename $0)
function usage ( ) {

cat <<EOT
$self [--oref0] <medtronic-glucose.json>
$self - Format Medtronic glucose data into something acceptable to Nightscout.
EOT
}

case "$1" in
--help|-h|help)
usage
exit 0
esac

HISTORY=${1-glucosehistory.json}
OUTPUT=${2-/dev/fd/1}
#TZ=${3-$(date +%z)}
Expand Down
25 changes: 22 additions & 3 deletions bin/mm-format-ns-profile.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,32 @@

# Author: Ben West

self=$(basename $0)
SETTINGS=${1-monitor/settings.json}
CARBS=${2-monitor/carb-ratios.json}
BASALRATES=${3-monitor/active-basal-profile.json}
SENSITIVITIES=${4-monitor/insulin-sensitivities.json}
TARGETS=${4-monitor/bg-targets.json}
OUTPUT=${2-/dev/fd/1}
TARGETS=${5-monitor/bg-targets.json}
OUTPUT=${6-/dev/fd/1}
# DIA
# CARBRATIO
#TZ=${3-$(date +%z)}

function usage ( ) {
cat <<EOF
$self: Format known pump data into Nightscout "profile".
Profile documents allow Nightscout to establish a common set of settings for
therapy, including the type of units used, the timezone, carb-ratios, active
basal profiles, insulin sensitivities, and BG targets. This compiles the
separate pump reports into a single profile document for Nightscout.
Usage:
$self pump-settings carb-ratios active-basal-profile insulin-sensitivities bg-targets
Examples:
bewest@bewest-MacBookPro:~/Documents/openaps$ mm-format-ns-profile monitor/settings.json monitor/carb-ratios.json monitor/active-basal-profile.json monitor/insulin-sensitivities.json monitor/bg-targets.json
EOF
}

Expand Down Expand Up @@ -132,7 +146,12 @@ function stub ( ) {
}
EOF
}

case $1 in
--help|config|help|-h)
usage
exit 0
;;
esac
stub $SETTINGS | fix-dates \
| add-carbs $CARBS \
| add-basals $BASALRATES \
Expand Down
16 changes: 16 additions & 0 deletions bin/mm-format-ns-pump-history.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,22 @@
# Author: Ben West
# Maintainer: Scott Leibrand

self=$(basename $0)
function usage ( ) {

cat <<EOT
$self <medtronic-pump-history.json>
$self - Format Medtronic pump-history data into something acceptable to Nightscout.
EOT
}

case "$1" in
--help|-h|help)
usage
exit 0
esac


HISTORY=${1-pumphistory.json}
OUTPUT=${2-/dev/fd/1}
#TZ=${3-$(date +%z)}
Expand Down
14 changes: 14 additions & 0 deletions bin/mm-format-ns-treatments.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,20 @@ MODEL=${2-model.json}
OUTPUT=${3-/dev/fd/1}
#TZ=${3-$(date +%z)}
self=$(basename $0)
function usage ( ) {

cat <<EOT
$self <pump-history-zoned.json> <model.json>
$self - Format medtronic history data into Nightscout treatments data.
EOT
}

case "$1" in
--help|-h|help)
usage
exit 0
esac


# | json -e "this.type = 'mm://openaps/$self'" \
model=$(json -f $MODEL)
Expand Down
14 changes: 14 additions & 0 deletions bin/mm-format-oref0-glucose.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,23 @@
# Maintainer: @tghoward

# Written for decocare v0.0.18. Will need updating the the decocare json format changes.
self=$(basename $0)
HISTORY=${1-glucosehistory.json}
OUTPUT=${2-/dev/fd/1}
#TZ=${3-$(date +%z)}
function usage ( ) {

cat <<EOT
$self <glucose-history.json>
$self - Format medtronic glucose data into oref0 format.
EOT
}

case "$1" in
--help|-h|help)
usage
exit 0
esac

cat $HISTORY | \
json -e "this.medtronic = this._type;" | \
Expand Down
2 changes: 1 addition & 1 deletion bin/mm-stick.sh
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ case $OPERATION in
print_fail $*
exit 1
;;
*|help)
*|help|--help|-h)
print_help
;;
esac
Expand Down
181 changes: 180 additions & 1 deletion bin/nightscout.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,206 @@ NAME=${1-help}
shift
PROGRAM="ns-${NAME}"
COMMAND=$(which $PROGRAM | head -n 1)
NIGHTSCOUT_DEBUG=${NIGHTSCOUT_DEBUG-0}

function help_message ( ) {
cat <<EOF
Usage:
$self <cmd>
* latest-openaps-treatment
* cull-latest-openaps-treatments
* get
* upload
* dedupe-treatments
* hash-api-secret
* status
* upload-entries
* autoconfigure-device-crud
EOF
}

function setup_help ( ) {

cat <<EOF
$self autoconfigure-device-crud <NIGHTSCOUT_HOST> <API_SECRET>
sets up:
openaps use ns shell get entries.json 'count=10'
openaps use ns shell upload treatments.json recently/combined-treatments.json
EOF
}

function ns_help ( ) {
cat <<EOF
TODO: improve help
openaps use ns shell get entries.json 'count=10'
openaps use ns shell upload treatments.json recently/combined-treatments.json
-h This message.
get type args Get records of type from
Nightscout matching args.
upload endpoint file Upload a file to the Nightscout endpoint.
latest-treatment-time - get latest treatment time from Nightscout
format-recent-history-treatments history model - Formats medtronic pump
history and model into
Nightscout compatible
treatments.
format-recent-type ZONE type file - Selects elements from the
file where the elements would
satisfy a gap in the last 1000
Nightscout records.
upload-non-empty-treatments file - Upload a non empty treatments
file to Nightscout.
lsgaps tz entries - Re-use openaps timezone device
to find gaps in a type (entries)
by default.
upload-non-empty-type type file
status - Retrieve status
preflight - NS preflight
EOF
}
case $NAME in
latest-openaps-treatment)
ns-get treatments.json'?find[enteredBy]=/openaps:\/\//&count=1' $* | json 0
;;
ns)
NIGHTSCOUT_HOST=$1
API_SECRET=$2
OP=$3
shift
shift
shift

case $OP in
-h|--help|help)
ns_help
exit 0
;;
get)
exec ns-get host $NIGHTSCOUT_HOST $*
exit 0
;;
preflight)
STATUS=$(ns-get host $NIGHTSCOUT_HOST status.json | json status)
if [[ $STATUS = "ok" ]] ; then
echo "true" | json -j
exit 0
else
echo "false" | json -j
exit 1
fi
;;
status)
ns-get host $NIGHTSCOUT_HOST status.json | json
;;
lsgaps)
ZONE=${1-'tz'}
TYPE=${2-'entries'}
ns-get host $NIGHTSCOUT_HOST "${TYPE}.json" 'count=300' \
| openaps use $ZONE \
rezone --astimezone --date dateString - \
| openaps use $ZONE \
lsgaps --minutes 5 --after now --date dateString -


;;
format-recent-type)
ZONE=${1-'tz'}
TYPE=${2-'entries'}
FILE=${3-''}
# nightscout ns $NIGHTSCOUT_HOST $API_SECRET
test -z ${ZONE} && "Missing first argument, ZONE, usually is set to tz" && exit 1
test -z ${TYPE} && "Missing second argument, TYPE, one of: entries, treatments, devicestatus, profiles." && exit 1
test ! -e ${FILE} && "Third argument, contents to upload, FILE, does not exist" && exit 1
test ! -r ${FILE} && "Third argument, contents to upload, FILE, not readable." && exit 1
openaps use ns shell lsgaps ${ZONE} ${TYPE} \
| openaps use ${ZONE} select --current now --gaps - ${FILE} | json
;;
latest-entries-time)
PREVIOUS_TIME=$(ns-get host $NIGHTSCOUT_HOST entries.json 'find[type]=sgv' | json 0)
test -z "${PREVIOUS_TIME}" && echo -n 0 || echo $PREVIOUS_TIME | json -j dateString
exit 0
;;
latest-treatment-time)
PREVIOUS_TIME=$(ns-get host $NIGHTSCOUT_HOST treatments.json'?find[enteredBy]=/openaps:\/\//&count=1' | json 0)
test -z "${PREVIOUS_TIME}" && echo -n 0 || echo $PREVIOUS_TIME | json -j created_at
exit 0
# exec ns-get host $NIGHTSCOUT_HOST $*
;;
format-recent-history-treatments)
HISTORY=$1
MODEL=$2
LAST_TIME=$(nightscout ns $NIGHTSCOUT_HOST $API_SECRET latest-treatment-time | json)
exec nightscout cull-latest-openaps-treatments $HISTORY $MODEL ${LAST_TIME}
exit 0

;;
upload-non-empty-type)
TYPE=${1-entries.json}
FILE=$2
test $(cat $FILE | json -a | wc -l) -lt 1 && echo "Nothing to upload." > /dev/stderr && cat $FILE && exit 0
exec ns-upload $NIGHTSCOUT_HOST $API_SECRET $TYPE $FILE
;;
upload-non-empty-treatments)
test $(cat $1 | json -a | wc -l) -lt 1 && echo "Nothing to upload." > /dev/stderr && cat $1 && exit 0
exec ns-upload $NIGHTSCOUT_HOST $API_SECRET treatments.json $1

;;
upload)
exec ns-upload $NIGHTSCOUT_HOST $API_SECRET $*
;;
*)
echo "Unknown request:" $OP
ns_help
exit 1;
;;
esac
exit 0

;;
hash-api-secret)
if [[ -z "$1" ]] ; then
echo "Missing plain Nightscout passphrase".
echo "Usage: $self hash-api-secret 'myverylongsecret'".
exit 1;
fi
API_SECRET=$(echo -n $1 | sha1sum | cut -d ' ' -f 1 | tr -d "\n")
echo $API_SECRET
;;
autoconfigure-device-crud)
NIGHTSCOUT_HOST=$1
PLAIN_NS_SECRET=$2
API_SECRET=$($self hash-api-secret $2)
case $1 in
help|-h|--help)
setup_help
exit 0
;;
esac
# openaps device add ns-get host
test -z "$NIGHTSCOUT_HOST" && setup_help && exit 1;
test -z "$API_SECRET" && setup_help && exit 1;
openaps device add ns process --require "oper" nightscout ns "NIGHTSCOUT_HOST" "API_SECRET"
openaps device show ns --json | json \
| json -e "this.extra.args = this.extra.args.replace(' NIGHTSCOUT_HOST ', ' $NIGHTSCOUT_HOST ')" \
| json -e "this.extra.args = this.extra.args.replace(' API_SECRET', ' $API_SECRET')" \
| openaps import
;;
cull-latest-openaps-treatments)
INPUT=$1
MODEL=$2
LAST_TIME=$3
mm-format-ns-treatments $INPUT $MODEL | json -c "this.created_at > '$LAST_TIME'"
;;
help)
help|--help|-h)
help_message
exit 0
;;
*)
test -n "$COMMAND" && exec $COMMAND $*
Expand Down
Loading

0 comments on commit d320460

Please sign in to comment.