Skip to content

Instantly share code, notes, and snippets.

@mddub mddub/loop.sh Secret
Last active Feb 29, 2016

Embed
What would you like to do?
OPENAPS_SUCCESS=0
CGM_SUCCESS=0
PREFLIGHT_SUCCESS=0
SUGGEST_SUCCESS=0
ENACT_SUCCESS=0
UPLOAD_SUCCESS=0
(openaps use pump -h >/dev/null 2>/dev/null) && OPENAPS_SUCCESS=1 && \
openaps monitor-cgm && CGM_SUCCESS=1 && \
openaps preflight && PREFLIGHT_SUCCESS=1 && \
openaps all-pump && \
openaps report invoke enact/suggested.json && SUGGEST_SUCCESS=1 && \
openaps enact && ENACT_SUCCESS=1
if [ $ENACT_SUCCESS == 1 ]
then
cat enact/enacted.json
echo ""
fi
if [ $SUGGEST_SUCCESS == 1 ]
then
openaps upload && UPLOAD_SUCCESS=1
fi
cp sug.txt last-sug.txt
python /home/pi/bin/summarize.py last-sug.txt $OPENAPS_SUCCESS $CGM_SUCCESS $PREFLIGHT_SUCCESS $SUGGEST_SUCCESS $ENACT_SUCCESS > sug.txt
scp sug.txt $SCP_TARGET && UPLOAD_SUCCESS=1
openaps report invoke upload/ns-status.json && \
ns-upload $NIGHTSCOUT_HOST $API_SECRET devicestatus.json upload/ns-status.json
LOOP_STATE='{"created_at": "'`date -Is`'", {"loop_state": {"openaps": '$OPENAPS_SUCCESS', "cgm": '$CGM_SUCCESS', "preflight": '$PREFLIGHT_SUCCESS', "suggest": '$SUGGEST_SUCCESS', "enact": '$ENACT_SUCCESS', "upload": '$UPLOAD_SUCCESS'}}}'
echo $LOOP_STATE >> loop_state.log && git add loop_state.log && git commit -m "Loop state"
echo ""
echo $LOOP_STATE
import json
import os
import re
import sys
from datetime import datetime
def try_json(filename):
try:
return json.loads(open(filename).read())
except IOError:
return None
temp_basal = try_json('monitor/temp_basal.json')
suggested = try_json('enact/suggested.json')
enacted = try_json('enact/enacted.json')
last_output = open(sys.argv[1]).read().strip()
openaps_success, cgm_success, preflight_success, suggest_success, enact_success = [a == '1' for a in sys.argv[2:]]
if suggest_success:
time = datetime.fromtimestamp(os.stat('enact/suggested.json').st_mtime).strftime("%I:%M%P")[:-1]
else:
time = datetime.now().strftime("%I:%M%P")[:-1]
if time.startswith('0'):
time = time[1:]
out = time
if suggest_success:
iob_json = try_json('monitor/iob.json')
if iob_json:
iob = round(json.loads(open('monitor/iob.json').read())['iob'], 1)
out += ' %su' % iob
if enact_success:
out += ' %sx%s' % (round(enacted['rate'], 1), enacted['duration'])
elif suggest_success:
if temp_basal and temp_basal.get('duration', 0) > 0:
out += ' %sx%s' % (round(temp_basal['rate'], 1), temp_basal['duration'])
else:
out += ''
else:
if not openaps_success:
out += ' oa'
elif not cgm_success:
out += ' cgm'
elif not preflight_success:
out += ' pf'
elif not suggest_success:
out += ' pump'
else:
out += ' ?'
last_successful_time = '?'
persisted_time = re.search('\[(.+)\]$', last_output)
recent_time = re.search('^(\d+:\d\d(a|p))', last_output)
if persisted_time:
last_successful_time = persisted_time.group(1)
elif recent_time:
last_successful_time = recent_time.group(1)
out += ' [%s]' % last_successful_time
print out
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.