diff --git a/bin/oref0-autosens-loop.sh b/bin/oref0-autosens-loop.sh index a811d68e3..44f001777 100755 --- a/bin/oref0-autosens-loop.sh +++ b/bin/oref0-autosens-loop.sh @@ -33,11 +33,11 @@ function completed_recently { # openaps use detect-sensitivity shell monitor/glucose.json settings/pumphistory-24h-zoned.json settings/insulin_sensitivities.json settings/basal_profile.json settings/profile.json monitor/carbhistory.json settings/temptargets.json function autosens { # only run autosens if pumphistory-24h is newer than autosens - if find monitor/ -newer settings/autosens.json | grep -q pumphistory-24h-zoned.json \ - || find settings/ -size -5c | grep -q autosens.json \ - || ! find settings/ | grep -q autosens \ + if find monitor/ -newer settings/autosens.json | grep pumphistory-24h-zoned.json \ + || find settings/ -size -5c | grep autosens.json \ + || ! find settings/ | grep autosens \ || ! find settings/autosens.json >/dev/null; then - if oref0-detect-sensitivity monitor/glucose.json monitor/pumphistory-24h-zoned.json settings/insulin_sensitivities.json settings/basal_profile.json settings/profile.json monitor/carbhistory.json settings/temptargets.json > settings/autosens.json.new && cat settings/autosens.json.new | jq .ratio | grep -q [0-9]; then + if oref0-detect-sensitivity monitor/glucose.json monitor/pumphistory-24h-zoned.json settings/insulin_sensitivities.json settings/basal_profile.json settings/profile.json monitor/carbhistory.json settings/temptargets.json > settings/autosens.json.new && cat settings/autosens.json.new | jq .ratio | grep "[0-9]"; then mv settings/autosens.json.new settings/autosens.json echo -n "Autosens refreshed: " else diff --git a/bin/oref0-bash-common-functions.sh b/bin/oref0-bash-common-functions.sh index f4262d43f..9a8d5b3e7 100755 --- a/bin/oref0-bash-common-functions.sh +++ b/bin/oref0-bash-common-functions.sh @@ -542,7 +542,7 @@ function wait_for_silence { echo -n . # returns true if it hears pump comms, false otherwise if ! listen -t $waitfor's' 2>&4 ; then - echo "No interfering pump comms detected from other rigs (this is a good thing!)" + echo " All clear." echo -n "Continuing oref0-pump-loop at "; date return 0 else diff --git a/bin/oref0-cron-every-minute.sh b/bin/oref0-cron-every-minute.sh index 2874b2be4..f84f79e52 100755 --- a/bin/oref0-cron-every-minute.sh +++ b/bin/oref0-cron-every-minute.sh @@ -129,7 +129,7 @@ if [[ ! -z "$BT_PEB" || ! -z "$BT_MAC" ]]; then fi if [[ ! -z "$PUSHOVER_TOKEN" && ! -z "$PUSHOVER_USER" ]]; then - oref0-pushover $PUSHOVER_TOKEN $PUSHOVER_USER 2>&1 >> /var/log/openaps/pushover.log & + #oref0-pushover $PUSHOVER_TOKEN $PUSHOVER_USER 2>&1 >> /var/log/openaps/pushover.log & fi # if disk has less than 10MB free, delete something and logrotate diff --git a/bin/oref0-ns-loop.sh b/bin/oref0-ns-loop.sh index 100b2d844..34e929e92 100755 --- a/bin/oref0-ns-loop.sh +++ b/bin/oref0-ns-loop.sh @@ -25,7 +25,7 @@ main() { fi fi - pushover_snooze + #pushover_snooze ns_temptargets || die "ns_temptargets failed" ns_meal_carbs || echo "ns_meal_carbs failed" battery_status @@ -73,7 +73,7 @@ function get_ns_bg { # if ns-glucose.json data is <10m old, no more than 5m in the future, and valid (>38), # copy cgm/ns-glucose.json over to cgm/glucose.json if it's newer valid_glucose=$(find_valid_ns_glucose) - if echo $valid_glucose | grep -q glucose; then + if echo $valid_glucose | grep glucose >/dev/null; then echo Found recent valid BG: echo $valid_glucose | colorize_json '.[0] | { glucose: .glucose, dateString: .dateString }' cp -pu cgm/ns-glucose.json cgm/glucose.json @@ -104,7 +104,7 @@ function find_valid_ns_glucose { function ns_temptargets { #openaps report invoke settings/temptargets.json settings/profile.json >/dev/null nightscout ns $NIGHTSCOUT_HOST $API_SECRET temp_targets > settings/ns-temptargets.json.new - cat settings/ns-temptargets.json.new | jq .[0].duration | egrep -q [0-9] && mv settings/ns-temptargets.json.new settings/ns-temptargets.json + cat settings/ns-temptargets.json.new | jq .[0].duration | egrep "[0-9]" >/dev/null && mv settings/ns-temptargets.json.new settings/ns-temptargets.json # TODO: merge local-temptargets.json with ns-temptargets.json #openaps report invoke settings/ns-temptargets.json settings/profile.json echo -n "Latest NS temptargets: " @@ -119,11 +119,11 @@ function ns_temptargets { dir_name=~/test_data/oref0-get-profile$(date +"%Y-%m-%d-%H%M")-ns #echo dir_name = $dir_name - mkdir -p $dir_name + # mkdir -p $dir_name #cp settings/settings.json settings/bg_targets.json settings/insulin_sensitivities.json settings/basal_profile.json preferences.json settings/carb_ratios.json settings/temptargets.json settings/model.json settings/autotune.json $dir_name run_remote_command 'oref0-get-profile settings/settings.json settings/bg_targets.json settings/insulin_sensitivities.json settings/basal_profile.json preferences.json settings/carb_ratios.json settings/temptargets.json --model=settings/model.json --autotune settings/autotune.json' | jq . > settings/profile.json.new || die "Couldn't refresh profile" - if cat settings/profile.json.new | jq . | grep -q basal; then + if cat settings/profile.json.new | jq . | grep basal > /dev/null; then mv settings/profile.json.new settings/profile.json else die "Invalid profile.json.new after refresh" @@ -134,11 +134,11 @@ function ns_temptargets { function ns_meal_carbs { #openaps report invoke monitor/carbhistory.json >/dev/null nightscout ns $NIGHTSCOUT_HOST $API_SECRET carb_history > monitor/carbhistory.json.new - cat monitor/carbhistory.json.new | jq .[0].carbs | egrep -q [0-9] && mv monitor/carbhistory.json.new monitor/carbhistory.json + cat monitor/carbhistory.json.new | jq .[0].carbs | egrep "[0-9]" >/dev/null && mv monitor/carbhistory.json.new monitor/carbhistory.json dir_name=~/test_data/oref0-meal$(date +"%Y-%m-%d-%H%M") #echo dir_name = $dir_name - mkdir -p $dir_name + # mkdir -p $dir_name #cp monitor/pumphistory-24h-zoned.json settings/profile.json monitor/clock-zoned.json monitor/glucose.json settings/basal_profile.json monitor/carbhistory.json $dir_name @@ -191,7 +191,7 @@ function upload { function upload_ns_status { set -o pipefail #echo Uploading devicestatus - grep -q iob monitor/iob.json || die "IOB not found" + grep iob monitor/iob.json >/dev/null || die "IOB not found" # set the timestamp on enact/suggested.json to match the deliverAt time touch -d $(cat enact/suggested.json | jq .deliverAt | sed 's/"//g') enact/suggested.json if ! file_is_recent_and_min_size enact/suggested.json 10; then @@ -200,12 +200,12 @@ function upload_ns_status { return 1 fi ns_status_file_name=ns-status$(date +"%Y-%m-%d-%T").json - format_ns_status $ns_status_file_name && grep -q iob upload/$ns_status_file_name || die "Couldn't generate ns-status.json" + format_ns_status $ns_status_file_name && grep iob upload/$ns_status_file_name >/dev/null || die "Couldn't generate ns-status.json" # Delete files older than 24 hours. find upload -maxdepth 1 -mmin +1440 -type f -name "ns-status*.json" -delete # Upload the files one by one according to their order. ls upload/ns-status*.json | while read -r file_name ; do - if ! grep -q iob $file_name ; then + if ! grep iob $file_name >/dev/null ; then #echo deleteing file $file_name rm $file_name continue @@ -239,7 +239,7 @@ function upload_recent_treatments { } function latest_ns_treatment_time { - nightscout latest-openaps-treatment $NIGHTSCOUT_HOST $API_SECRET | jq -r .created_at + date -Is -d $(nightscout latest-openaps-treatment $NIGHTSCOUT_HOST $API_SECRET | jq -r .created_at) } #nightscout cull-latest-openaps-treatments monitor/pumphistory-zoned.json settings/model.json $(openaps latest-ns-treatment-time) > upload/latest-treatments.json diff --git a/bin/oref0-pump-loop.sh b/bin/oref0-pump-loop.sh index 816fed2d0..3ff08fa50 100755 --- a/bin/oref0-pump-loop.sh +++ b/bin/oref0-pump-loop.sh @@ -68,9 +68,17 @@ main() { fi fi touch /tmp/pump_loop_completed -r /tmp/pump_loop_enacted + # run pushover immediately after completing loop for more timely carbsReq notifications without race conditions + PUSHOVER_TOKEN="$(get_pref_string .pushover_token "")" + PUSHOVER_USER="$(get_pref_string .pushover_user "")" + if [[ ! -z "$PUSHOVER_TOKEN" && ! -z "$PUSHOVER_USER" ]]; then + oref0-pushover $PUSHOVER_TOKEN $PUSHOVER_USER # 2>&1 >> /var/log/openaps/pushover.log & + fi + # before each of these (optional) refresh checks, make sure we don't have fresh glucose data # if we do, then skip the optional checks to finish up this loop and start the next one if ! glucose-fresh; then + wait_for_silence $upto10s if onbattery; then refresh_profile 30 else @@ -317,7 +325,7 @@ function smb_suggest { } function determine_basal { - cat monitor/meal.json + #cat monitor/meal.json update_glucose_noise @@ -480,7 +488,7 @@ function refresh_after_bolus_or_enact { function unsuspend_if_no_temp { # If temp basal duration is zero, unsuspend pump - if (cat monitor/temp_basal.json | jq '. | select(.duration == 0)' | grep -q duration); then + if (cat monitor/temp_basal.json | jq '. | select(.duration == 0)' | grep duration); then if check_pref_bool .unsuspend_if_no_temp false; then echo Temp basal has ended: unsuspending pump mdt resume 2>&3 @@ -600,7 +608,7 @@ function refresh_pumphistory_and_meal { dir_name=~/test_data/oref0-meal$(date +"%Y-%m-%d-%H%M") #echo dir_name = $dir_name - mkdir -p $dir_name + # mkdir -p $dir_name #cp monitor/pumphistory-24h-zoned.json settings/profile.json monitor/clock-zoned.json monitor/glucose.json settings/basal_profile.json monitor/carbhistory.json $dir_name if ! retry_return run_remote_command 'oref0-meal monitor/pumphistory-24h-zoned.json settings/profile.json monitor/clock-zoned.json monitor/glucose.json settings/basal_profile.json monitor/carbhistory.json' > monitor/meal.json.new ; then echo; echo "Couldn't calculate COB" @@ -608,7 +616,7 @@ function refresh_pumphistory_and_meal { fi try_return check_cp_meal || return 1 echo -n "refreshed: " - cat monitor/meal.json + cat monitor/meal.json | jq -cC . } function check_cp_meal { @@ -631,7 +639,7 @@ function check_cp_meal { function calculate_iob { dir_name=~/test_data/oref0-calculate-iob$(date +"%Y-%m-%d-%H%M") #echo dir_name = $dir_name - mkdir -p $dir_name + # mkdir -p $dir_name #cp monitor/pumphistory-24h-zoned.json settings/profile.json monitor/clock-zoned.json settings/autosens.json $dir_name run_remote_command 'oref0-calculate-iob monitor/pumphistory-24h-zoned.json settings/profile.json monitor/clock-zoned.json settings/autosens.json' > monitor/iob.json.new || { echo; echo "Couldn't calculate IOB"; fail "$@"; } @@ -687,7 +695,7 @@ function get_settings { #dir_name=~/test_data/oref0-get-profile$(date +"%Y-%m-%d-%H%M")-pump #echo dir_name = $dir_name - mkdir -p $dir_name + # mkdir -p $dir_name #cp settings/settings.json settings/bg_targets.json settings/insulin_sensitivities.json settings/basal_profile.json preferences.json settings/carb_ratios.json settings/temptargets.json settings/model.json $dir_name run_remote_command 'oref0-get-profile settings/settings.json settings/bg_targets.json settings/insulin_sensitivities.json settings/basal_profile.json preferences.json settings/carb_ratios.json settings/temptargets.json --model=settings/model.json' 2>&3 | jq . > settings/pumpprofile.json.new || { echo "Couldn't refresh pumpprofile"; fail "$@"; } @@ -701,7 +709,7 @@ function get_settings { # generate settings/profile.json.new with autotune dir_name=~/test_data/oref0-get-profile$(date +"%Y-%m-%d-%H%M")-pump-auto #echo dir_name = $dir_name - mkdir -p $dir_name + # mkdir -p $dir_name #cp settings/settings.json settings/bg_targets.json settings/insulin_sensitivities.json settings/basal_profile.json preferences.json settings/carb_ratios.json settings/temptargets.json settings/model.json settings/autotune.json $dir_name run_remote_command 'oref0-get-profile settings/settings.json settings/bg_targets.json settings/insulin_sensitivities.json settings/basal_profile.json preferences.json settings/carb_ratios.json settings/temptargets.json --model=settings/model.json --autotune settings/autotune.json' | jq . > settings/profile.json.new || { echo "Couldn't refresh profile"; fail "$@"; } @@ -736,7 +744,7 @@ function onbattery { function wait_for_bg { if [ "$(get_pref_string .cgm '')" == "mdt" ]; then echo "MDT CGM configured; not waiting" - elif egrep -q "Warning:" enact/smb-suggested.json 2>&3; then + elif egrep -q "Warning:" enact/smb-suggested.json 2>&3 || egrep -q "Could not parse clock data" monitor/meal.json 2>&3; then echo "Retrying without waiting for new BG" elif egrep -q "Waiting [0](\.[0-9])?m ([0-6]?[0-9]s )?to microbolus again." enact/smb-suggested.json 2>&3; then echo "Retrying microbolus without waiting for new BG" diff --git a/bin/oref0-pushover.sh b/bin/oref0-pushover.sh index bc13dd099..d31859870 100755 --- a/bin/oref0-pushover.sh +++ b/bin/oref0-pushover.sh @@ -76,21 +76,38 @@ else PRIORITY_OPTIONS="" fi -date +#date -if file_is_recent monitor/pushover-sent $SNOOZE; then - echo "Last pushover sent less than $SNOOZE minutes ago." -elif ! file_is_recent "$FILE"; then +#function pushover_snooze { +# check Nightscout to see if another rig has already sent a carbsReq pushover recently + URL=$NIGHTSCOUT_HOST/api/v1/devicestatus.json?count=100 + if [[ "${API_SECRET}" =~ "token=" ]]; then + URL="${URL}&${API_SECRET}" + else + CURL_AUTH='-H api-secret:'${API_SECRET} + fi + + if snooze=$(curl -s ${CURL_AUTH} ${URL} | jq '.[] | select(.snooze=="carbsReq") | select(.date>'$(date +%s -d "10 minutes ago")')' | jq -s .[0].date | noquotes | grep -v null); then + #echo $snooze + #echo date -Is -d @$snooze; echo + touch -d $(date -Is -d @$snooze) monitor/pushover-sent + #ls -la monitor/pushover-sent | awk '{print $8,$9}' + fi +#} + +if ! file_is_recent "$FILE"; then echo "$FILE more than 5 minutes old" exit -elif ! cat $FILE | egrep "add'l|maxBolus"; then - echo "No additional carbs or bolus required." -elif [[ $ONLYFOR =~ "carb" ]] && ! cat $FILE | egrep "add'l"; then - echo "No additional carbs required." -elif [[ $ONLYFOR =~ "insulin" ]] && ! cat $FILE | egrep "maxBolus"; then - echo "No additional insulin required." +elif ! cat $FILE | egrep "add'l|maxBolus" > /dev/null; then + echo -n "No carbsReq. " +elif [[ $ONLYFOR =~ "carb" ]] && ! cat $FILE | egrep "add'l" > /dev/null; then + echo -n "No carbsReq. " +elif [[ $ONLYFOR =~ "insulin" ]] && ! cat $FILE | egrep "maxBolus" > /dev/null; then + echo -n "No additional insulin required. " +elif file_is_recent monitor/pushover-sent $SNOOZE; then + echo -n "Last pushover sent less than $SNOOZE minutes ago. " else - curl -s -F token=$TOKEN -F user=$USER $SOUND_OPTION -F priority=$PRIORITY $PRIORITY_OPTIONS -F "message=$(jq -c "{bg, tick, carbsReq, insulinReq, reason}|del(.[] | nulls)" $FILE) - $(hostname)" https://api.pushover.net/1/messages.json && touch monitor/pushover-sent && echo '{"date":'$(epochtime_now)',"device":"openaps://'$(hostname)'","snooze":"carbsReq"}' | tee /tmp/snooze.json && ns-upload $NIGHTSCOUT_HOST $API_SECRET devicestatus.json /tmp/snooze.json + curl -s -F token=$TOKEN -F user=$USER $SOUND_OPTION -F priority=$PRIORITY $PRIORITY_OPTIONS -F "message=$(jq -c "{bg, tick, carbsReq, insulinReq, reason}|del(.[] | nulls)" $FILE) - $(hostname)" https://api.pushover.net/1/messages.json | jq .status| grep 1 >/dev/null && touch monitor/pushover-sent && echo '{"date":'$(epochtime_now)',"device":"openaps://'$(hostname)'","snooze":"carbsReq"}' > /tmp/snooze.json && ns-upload $NIGHTSCOUT_HOST $API_SECRET devicestatus.json /tmp/snooze.json >/dev/null && echo "carbsReq pushover sent." echo fi @@ -106,6 +123,8 @@ source $HOME/.bash_profile key=${MAKER_KEY:-"null"} carbsReq=`jq .carbsReq ${FILE}` tick=`jq .tick ${FILE}` +tick="${tick%\"}" +tick="${tick#\"}" bgNow=`jq .bg ${FILE}` delta=`echo "${tick}" | tr -d +` delta="${delta%\"}" @@ -119,50 +138,68 @@ pushoverGlances=$(get_prefs_json | jq -M '.pushoverGlances') if [ "${pushoverGlances}" == "null" -o "${pushoverGlances}" == "false" ]; then echo "pushoverGlances not enabled in preferences.json" else + # if pushoverGlances is a number instead of just true, use it to set the minutes allowed between glances + re='^[0-9]+$' + if [[ ${pushoverGlances} =~ $re ]]; then + glanceDelay=${pushoverGlances} + else + glanceDelay=10 + fi GLANCES="monitor/last_glance" GLUCOSE="monitor/glucose.json" if [ ! -f $GLANCES ]; then - # First time through it will get created older than 10 minutes so it'll fire - touch $GLANCES && touch -r $GLANCES -d '-11 mins' $GLANCES + # First time through it will get created 1h old so it'll fire + touch $GLANCES && touch -r $GLANCES -d '-60 mins' $GLANCES + fi + + if snooze=$(curl -s ${CURL_AUTH} ${URL} | jq '.[] | select(.snooze=="glance") | select(.date>'$(date +%s -d "$glanceDelay minutes ago")')' | jq -s .[0].date | noquotes | grep -v null); then + #echo $snooze + #echo date -Is -d @$snooze; echo + touch -d $(date -Is -d @$snooze) $GLANCES + #ls -la $GLANCES | awk '{print $8,$9}' fi - if test `find $GLANCES -mmin +10` + if test `find $GLANCES -mmin +$glanceDelay` || cat $FILE | egrep "add'l" >/dev/null then - enactTime=$(ls -l --time-style=+"%l:%M" ${FILE} | awk '{printf ($6)}') - + curTime=$(ls -l --time-style=+"%l:%M" ${FILE} | awk '{printf ($6)}') + lastDirection=`jq -M '.[0] .direction' $GLUCOSE` lastDirection="${lastDirection%\"}" lastDirection="${lastDirection#\"}" + rate=`jq -M '.rate' monitor/temp_basal.json` + duration=`jq -M '.duration' monitor/temp_basal.json` #echo lastDirection=$lastDirection if [ "${lastDirection}" == "SingleUp" ]; then - direction="+" + direction="↑" elif [ "${lastDirection}" == "FortyFiveUp" ]; then - direction="++" + direction="↗" elif [ "${lastDirection}" == "DoubleUp" ]; then - direction="+++" + direction="↑↑" elif [ "${lastDirection}" == "SingleDown" ]; then - direction="-" + direction="↓" elif [ "${lastDirection}" == "FortyFiveDown" ]; then - direction="--" + direction="↘" elif [ "${lastDirection}" == "DoubleDown" ]; then - direction="---" + direction="↓↓" else - direction="" # default for NONE or Flat + direction="→" # default for NONE or Flat fi - if [ test cat $FILE | egrep "add'l" ]; then - subtext="cr ${carbsReq}g" - else - subtext="e${enactTime}" + title="${bgNow} ${tick} ${direction} @ ${curTime}" + text="IOB ${iob}, COB ${cob}" + if cat $FILE | egrep "add'l" >/dev/null; then + carbsMsg="${carbsReq}g req " fi - text="${bgNow}${direction}" - title="cob ${cob}, iob ${iob}" + subtext="$carbsMsg${rate}U/h ${duration}m" # echo "pushover glance text=${text} subtext=${subtext} delta=${delta} title=${title} battery percent=${battery}" - curl -s -F "token=$TOKEN" -F "user=$USER" -F "text=${text}" -F "subtext=${subtext}" -F "count=$bgNow" -F "percent=${battery}" -F "title=${title}" https://api.pushover.net/1/glances.json + curl -s -F "token=$TOKEN" -F "user=$USER" -F "text=${text}" -F "subtext=${subtext}" -F "count=$bgNow" -F "percent=${battery}" -F "title=${title}" https://api.pushover.net/1/glances.json | jq .status| grep 1 >/dev/null && echo '{"date":'$(epochtime_now)',"device":"openaps://'$(hostname)'","snooze":"glance"}' > /tmp/snooze.json && ns-upload $NIGHTSCOUT_HOST $API_SECRET devicestatus.json /tmp/snooze.json >/dev/null && echo "Glance uploaded and snoozed" touch $GLANCES + else + echo -n "Pushover glance last updated less than $glanceDelay minutes ago @ " + ls -la $GLANCES | awk '{print $8}' fi fi @@ -174,7 +211,7 @@ fi # call with this event that will read out in human language the additional carbs and other # vital facts. It will leave a voice mail if not answered. -if [[ "$MAKER_KEY" != "null" ]] && cat $FILE | egrep "add'l"; then +if ! [ -z "$MAKER_KEY" ] && [[ "$MAKER_KEY" != "null" ]] && cat $FILE | egrep "add'l"; then if file_is_recent monitor/ifttt-sent 60; then echo "carbsReq=${carbsReq} but last IFTTT event sent less than 60 minutes ago." else diff --git a/lib/determine-basal/determine-basal.js b/lib/determine-basal/determine-basal.js index fcda1352b..a599fc8c6 100644 --- a/lib/determine-basal/determine-basal.js +++ b/lib/determine-basal/determine-basal.js @@ -895,7 +895,9 @@ var maxDelta_bg_threshold; zeroTempEffect = round(zeroTempEffect); carbsReq = round(carbsReq); console.error("naive_eventualBG:",naive_eventualBG,"bgUndershoot:",bgUndershoot,"zeroTempDuration:",zeroTempDuration,"zeroTempEffect:",zeroTempEffect,"carbsReq:",carbsReq); - if ( carbsReq >= profile.carbsReqThreshold && minutesAboveThreshold <= 45 ) { + if ( meal_data.reason == "Could not parse clock data" ) { + console.error("carbsReq unknown: Could not parse clock data"); + } else if ( carbsReq >= profile.carbsReqThreshold && minutesAboveThreshold <= 45 ) { rT.carbsReq = carbsReq; rT.reason += carbsReq + " add'l carbs req w/in " + minutesAboveThreshold + "m; "; }